-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathAddress.m
More file actions
199 lines (175 loc) · 7.35 KB
/
Copy pathAddress.m
File metadata and controls
199 lines (175 loc) · 7.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
//
// Address.m
// BitStore
//
// Created by Dylan Marriott on 01.02.14.
// Copyright (c) 2014 Dylan Marriott. All rights reserved.
//
#import "Address.h"
#import "AddressListener.h"
#import "RequestHelper.h"
#import "Transaction.h"
#import "Receiver.h"
#import "MessageHelper.h"
#import "UserDefaults.h"
#import <DMListener/DMListener.h>
@implementation Address {
DMListeners* _listeners;
NSTimer* _timer;
}
- (id)init {
if (self = [super init]) {
_listeners = [[DMListeners alloc] init];
self.total = -1;
}
return self;
}
- (id)initWithCoder:(NSCoder *)decoder {
if (self = [self init]) {
self.address = [decoder decodeObjectForKey:@"address"];
self.label = [decoder decodeObjectForKey:@"label"];
self.total = [decoder decodeInt64ForKey:@"total_2"];
self.transactions = [decoder decodeObjectForKey:@"transactions"];
}
return self;
}
- (void)encodeWithCoder:(NSCoder *)encoder {
[encoder encodeObject:self.address forKey:@"address"];
[encoder encodeObject:self.label forKey:@"label"];
[encoder encodeInt64:self.total forKey:@"total_2"];
[encoder encodeObject:self.transactions forKey:@"transactions"];
}
#pragma mark - Public
- (void)addAddressListener:(id<AddressListener>)listener {
[_listeners addListener:listener];
[listener addressChanged:self];
}
- (NSArray *)listeners {
return _listeners.listeners;
}
- (void)notifyListeners {
[_listeners notifyListeners:^(id<AddressListener> listener) {
[listener addressChanged:self];
}];
}
- (void)refresh {
[self refresh:NO];
}
- (void)refresh:(BOOL)showError {
[self update:YES showError:showError];
}
- (void)startUpdate:(int)refreshRate {
_timer = [NSTimer scheduledTimerWithTimeInterval:(refreshRate)
target:self
selector:@selector(updateMeta)
userInfo:nil
repeats:YES];
}
- (void)stopUpdate {
[_timer invalidate];
_timer = nil;
}
- (long long)total {
return MAX(_total, 0);
}
- (NSString *)description {
return [NSString stringWithFormat:@"%@: %@", self.label, self.address];
}
#pragma mark - Private
- (void)updateMeta {
[self update:YES showError:NO];
}
- (void)update:(BOOL)full showError:(BOOL)showError {
BitStoreKeys* keys = [[BitStoreKeys alloc] init];
NSString* addrUrl = [NSString stringWithFormat:@"https://api.chain.com/v1/bitcoin/addresses/%@?key=%@", self.address, keys.chain];
NSString* txUrl = [NSString stringWithFormat:@"https://api.chain.com/v1/bitcoin/addresses/%@/transactions?key=%@&limit=20", self.address, keys.chain];
NSMutableArray* urls = [[NSMutableArray alloc] init];
[urls addObject:addrUrl];
if (full) {
[urls addObject:txUrl];
}
RequestHelper* rh = [[RequestHelper alloc] init];
[rh startRequestWithUrls:urls completion:^(BOOL success, NSArray* data) {
if (success) {
[self parseAddressJson:[data objectAtIndex:0]];
if (urls.count > 1) {
[self parseTransactionsJson:[data objectAtIndex:1]];
}
[self notifyListeners];
} else {
if (showError) {
[MessageHelper showMessage:l10n(@"network_error")];
}
[self notifyListeners];
}
}];
}
- (void)parseAddressJson:(NSData *)data {
NSError* error;
NSDictionary* json = [NSJSONSerialization JSONObjectWithData:data options:0 error:&error];
self.total = [[json objectForKey:@"balance"] longValue] + [[json objectForKey:@"unconfirmed_balance"] longValue];
}
- (void)parseTransactionsJson:(NSData *)data {
NSError* error;
NSArray* txs = [NSJSONSerialization JSONObjectWithData:data options:0 error:&error];
NSMutableArray* transactions = [[NSMutableArray alloc] init];
for (NSDictionary* tx in txs) {
if ([tx isKindOfClass:[NSDictionary class]]) {
Transaction* transaction = [[Transaction alloc] init];
transaction.hash = [tx objectForKey:@"hash"];
NSDateFormatter* dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"yyyy-MM-dd'T'HH:mm:ss'Z'"];
if ([tx objectForKey:@"block_time"] == [NSNull null]) {
// tx is not yet in blockchain, chain.com just returns null here
// we have our own cache, where we store the date when it first appeared for us
// good enough i guess
NSMutableDictionary* cache = [UserDefaults instance].txcache;
NSDate* d = [cache objectForKey:transaction.hash];
if (d == nil) {
d = [[NSDate alloc] init];
[cache setObject:d forKey:transaction.hash];
[UserDefaults instance].txcache = cache; // stores the cache again
}
transaction.date = d;
} else {
NSTimeInterval timeZoneOffset = [[NSTimeZone defaultTimeZone] secondsFromGMT]; // You could also use the systemTimeZone method
NSTimeInterval gmtTimeInterval = [[dateFormatter dateFromString:[tx objectForKey:@"block_time"]] timeIntervalSinceReferenceDate] + timeZoneOffset;
transaction.date = [NSDate dateWithTimeIntervalSinceReferenceDate:gmtTimeInterval];
NSMutableDictionary* cache = [UserDefaults instance].txcache;
[cache removeObjectForKey:transaction.hash];
[UserDefaults instance].txcache = cache; // stores the cache again
}
transaction.confirmations = [[tx objectForKey:@"confirmations"] intValue];
NSDictionary* senderDict = [[tx objectForKey:@"inputs"] objectAtIndex:0];
transaction.sender = [[senderDict objectForKey:@"addresses"] objectAtIndex:0];
NSMutableArray* receivers = [[NSMutableArray alloc] init];
for (NSDictionary* receiverDict in [tx objectForKey:@"outputs"]) {
NSString* address = [[receiverDict objectForKey:@"addresses"] objectAtIndex:0];
if (![address isEqualToString:transaction.sender]) {
Receiver* receiver = [[Receiver alloc] init];
receiver.address = address;
if ([transaction.sender isEqualToString:self.address] ||
(![transaction.sender isEqualToString:self.address] && [address isEqualToString:self.address])) {
// if you're the sender, add all outputs together
// if you're not the sender, only add if you're the receiver
receiver.amount += [[receiverDict objectForKey:@"value"] longValue];
}
[receivers addObject:receiver];
}
}
if (receivers.count == 0) {
NSDictionary* receiverDict = [[tx objectForKey:@"outputs"] objectAtIndex:0];
NSString* address = [[receiverDict objectForKey:@"addresses"] objectAtIndex:0];
Receiver* receiver = [[Receiver alloc] init];
receiver.address = address;
receiver.amount += [[receiverDict objectForKey:@"value"] longValue];
[receivers addObject:receiver];
}
transaction.receiver = receivers;
transaction.orgAddress = self;
[transactions addObject:transaction];
}
}
self.transactions = transactions;
}
@end