forked from blockchain/My-Wallet-V3
-
Notifications
You must be signed in to change notification settings - Fork 0
/
hdaccount_spec.js.coffee
430 lines (332 loc) · 16 KB
/
hdaccount_spec.js.coffee
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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
proxyquire = require('proxyquireify')(require)
MyWallet = undefined
HDAccount = undefined
describe "HDAccount", ->
# account = HDAccount.fromExtPublicKey("xpub6DHN1xpggNEUkLDwwBGYDmYUaNmfE2mMGKZSiP7PB5wxbp34rhHAEBhMpsjHEwZWsHY2kPmPPD1w6gxGSBe3bXQzCn2WV8FRd7ZKpsiGHMq", undefined, "Example account");
account = undefined
object =
'label': 'My great wallet'
'archived': false
'xpriv': 'xprv9zJ1cTHnqzgBXr9Uq9jXrdbk2LwApa3Vu6dquzhmckQyj1hvK9xugPNsycfveTGcTy2571Rq71daBpe1QESUsjX7d2ZHVVXEwJEwDiiMD7E'
'xpub': 'xpub6DHN1xpggNEUkLDwwBGYDmYUaNmfE2mMGKZSiP7PB5wxbp34rhHAEBhMpsjHEwZWsHY2kPmPPD1w6gxGSBe3bXQzCn2WV8FRd7ZKpsiGHMq'
'address_labels': [{"index": 0, "label": "root"}]
'cache':
'receiveAccount': 'xpub6FMWuMox3fJxEv2TSLN6jYQg6tHZBS7tKRSu7w4Q7F9K2UsSu4RxtwxfeHVhUv3csTSCRkKREpiVdr8EquBPXfBDZSMe84wmN9LzR3rwNZP'
'changeAccount': 'xpub6FMWuMox3fJxGARtaDVY6e9st4Hk5j8Ui6r7XLnBPFXPXkajXNiAfiEqBakuDKYYeRf4ERtPm1TawBqKaBWj2dsHNJT4rSsugssTnaDsz2m'
beforeEach ->
MyWallet =
syncWallet: () ->
wallet:
getHistory: () ->
spyOn(MyWallet, "syncWallet")
spyOn(MyWallet.wallet, "getHistory")
# account = new HDAccount(object)
describe "Constructor", ->
describe "without arguments", ->
beforeEach ->
KeyRing = () -> {init: () ->}
KeyChain = {}
stubs = { './wallet': MyWallet, './keyring' : KeyRing, './keychain' : KeyChain}
HDAccount = proxyquire('../src/hd-account', stubs)
it "should create an empty HDAccount with default options", ->
account = new HDAccount()
expect(account.balance).toEqual(null)
expect(account.archived).not.toBeTruthy()
expect(account.active).toBeTruthy()
expect(account.receiveIndex).toEqual(0)
expect(account.changeIndex).toEqual(0)
expect(account.maxLabeledReceiveIndex).toEqual(-1)
it "should create an HDAccount from AccountMasterKey", ->
accountZero =
toBase58: () -> "accountZeroBase58"
neutered: () ->
{
toBase58: () -> "accountZeroNeuteredBase58"
}
a = HDAccount.fromAccountMasterKey(accountZero, 0, "label")
expect(a.label).toEqual("label")
expect(a._xpriv).toEqual("accountZeroBase58")
expect(a._xpub).toEqual("accountZeroNeuteredBase58")
it "should create an HDAccount from Wallet master key", ->
masterkey =
deriveHardened: (i) ->
deriveHardened: (j) ->
deriveHardened: (k) ->
toBase58: () ->
"m/" + i + "/" + j + "/" + k
neutered: () ->
toBase58: () ->
a = HDAccount.fromWalletMasterKey(masterkey, 0, "label")
expect(a._xpriv).toEqual("m/44/0/0")
expect(a.label).toEqual("label")
it "should transform an Object to an HDAccount", ->
stubs = { './wallet': MyWallet}
HDAccount = proxyquire('../src/hd-account', stubs)
account = new HDAccount(object)
expect(account.extendedPublicKey).toEqual(object.xpub)
expect(account.extendedPrivateKey).toEqual(object.xpriv)
expect(account.label).toEqual(object.label)
expect(account.archived).toEqual(object.archived)
expect(account.receiveIndex).toEqual(0)
expect(account.changeIndex).toEqual(0)
expect(account.n_tx).toEqual(0)
expect(account.balance).toEqual(null)
expect(account.keyRing).toBeDefined()
expect(account.receiveAddress).toBeDefined()
expect(account.changeAddress).toBeDefined()
expect(account.receivingAddressesLabels.length).toEqual(1)
describe "JSON serializer", ->
it 'should hold: fromJSON . toJSON = id', ->
json1 = JSON.stringify(account, null, 2)
racc = JSON.parse(json1, HDAccount.reviver)
json2 = JSON.stringify(racc, null, 2)
expect(json1).toEqual(json2)
describe "instance", ->
beforeEach ->
stubs = { './wallet': MyWallet}
HDAccount = proxyquire('../src/hd-account', stubs)
account = new HDAccount(object)
describe ".incrementReceiveIndex", ->
it 'should increment the received index', ->
initial = account.receiveIndex
account.incrementReceiveIndex()
final = account.receiveIndex
expect(final).toEqual(initial + 1)
describe ".incrementReceiveIndexIfLast", ->
it 'should not increment the received index', ->
account._receiveIndex = 10
initial = account.receiveIndex
account.incrementReceiveIndexIfLast(5)
final = account.receiveIndex
expect(final).toEqual(initial)
it 'should increment the received index', ->
account._receiveIndex = 10
initial = account.receiveIndex
account.incrementReceiveIndexIfLast(10)
final = account.receiveIndex
expect(final).toEqual(initial + 1)
describe ".get/setLabelForReceivingAddress", ->
it 'should set the label sync and get the label', ->
fail = (reason) ->
console.log(reason)
success = () ->
account.setLabelForReceivingAddress(10, "my label").then(success).catch(fail)
expect(account._address_labels[10]).toEqual("my label")
expect(MyWallet.syncWallet).toHaveBeenCalled()
expect(account.getLabelForReceivingAddress(10)).toEqual("my label")
it "should not set a non-valid label", ->
fail = (reason) ->
except(reason).toEqual('NOT_ALPHANUMERIC')
success = () ->
account.setLabelForReceivingAddress(10, 0).then(success).catch(fail)
expect(MyWallet.syncWallet).not.toHaveBeenCalled()
it "should not set a label with a gap too wide", ->
fail = (reason) ->
except(reason).toEqual('GAP')
success = () ->
account.setLabelForReceivingAddress(100, "my label").then(success).catch(fail)
expect(MyWallet.syncWallet).not.toHaveBeenCalled()
describe "Setter", ->
it "active shoud toggle archived", ->
account.active = false
expect(account.archived).toBeTruthy()
expect(MyWallet.syncWallet).toHaveBeenCalled()
account.active = true
expect(account.archived).toBeFalsy()
it "archived should archive the account and sync wallet", ->
account.archived = true
expect(account.archived).toBeTruthy()
expect(account.active).not.toBeTruthy()
expect(MyWallet.syncWallet).toHaveBeenCalled()
it "archived should throw exception if is non-boolean set", ->
wrongSet = () -> account.archived = "failure"
expect(wrongSet).toThrow()
it "archived should call MyWallet.sync.getHistory when set to false", ->
account.archived = false
expect(MyWallet.wallet.getHistory).toHaveBeenCalled()
expect(MyWallet.syncWallet).toHaveBeenCalled()
it "balance should be set and not sync wallet", ->
account.balance = 100
expect(account.balance).toEqual(100)
expect(MyWallet.syncWallet).not.toHaveBeenCalled()
it "balance should throw exception if is non-Number set", ->
wrongSet = () -> account.balance = "failure"
expect(wrongSet).toThrow()
it "n_tx should be set and not sync wallet", ->
account.n_tx = 100
expect(account.n_tx).toEqual(100)
expect(MyWallet.syncWallet).not.toHaveBeenCalled()
it "n_tx should throw exception if is non-Number set", ->
wrongSet = () -> account.n_tx = "failure"
expect(wrongSet).toThrow()
it "label should be set and sync wallet", ->
account.label = "my label"
expect(account.label).toEqual("my label")
expect(MyWallet.syncWallet).toHaveBeenCalled()
it "label should be valid", ->
test = () ->
account.label = 0
expect(test).toThrow()
it "xpriv is read only", ->
account.extendedPrivateKey = "not allowed"
expect(account.extendedPrivateKey).not.toEqual("not allowed")
it "xpub is read only", ->
account.extendedPublicKey = "not allowed"
expect(account.extendedPublicKey).not.toEqual("not allowed")
it "receiveAddress is read only", ->
account.receiveAddress = "not allowed"
expect(account.receiveAddress).not.toEqual("not allowed")
it "changeAddress is read only", ->
account.changeAddress = "not allowed"
expect(account.changeAddress).not.toEqual("not allowed")
it "index is read only", ->
account.index = "not allowed"
expect(account.index).not.toEqual("not allowed")
it "KeyRing is read only", ->
account.keyRing = "not allowed"
expect(account.keyRing).not.toEqual("not allowed")
it "lastUsedReceiveIndex must be a number", ->
invalid = () ->
account.lastUsedReceiveIndex = "1"
valid = () ->
account.lastUsedReceiveIndex = 1
expect(invalid).toThrow()
expect(account.lastUsedReceiveIndex).toEqual(0)
expect(valid).not.toThrow()
expect(account.lastUsedReceiveIndex).toEqual(1)
it "lastUsedReceiveIndex must be a positive number", ->
invalid = () ->
account.lastUsedReceiveIndex = -534.23
expect(invalid).toThrow()
expect(account.lastUsedReceiveIndex).toEqual(0)
it "receiveIndex must be a number", ->
invalid = () ->
account.receiveIndex = "1"
valid = () ->
account.receiveIndex = 1
expect(invalid).toThrow()
expect(account.receiveIndex).toEqual(0)
expect(valid).not.toThrow()
expect(account.receiveIndex).toEqual(1)
it "receiveIndex must be a positive number", ->
invalid = () ->
account.receiveIndex = -534.34
expect(invalid).toThrow()
expect(account.receiveIndex).toEqual(0)
it "changeIndex must be a number", ->
invalid = () ->
account.changeIndex = "1"
valid = () ->
account.changeIndex = 1
expect(invalid).toThrow()
expect(account.changeIndex).toEqual(0)
expect(valid).not.toThrow()
expect(account.changeIndex).toEqual(1)
it "changeIndex must be a positive number", ->
invalid = () ->
account.changeIndex = -534.234
expect(invalid).toThrow()
expect(account.changeIndex).toEqual(0)
describe "Getter", ->
it "maxLabeledReceiveIndex should return the highest labeled index", ->
expect(account.maxLabeledReceiveIndex).toEqual(0)
account.setLabelForReceivingAddress(1, "label1")
account.setLabelForReceivingAddress(10, "label100")
expect(account.maxLabeledReceiveIndex).toEqual(10)
it "labeledReceivingAddresses should return all the labeled receiving addresses", ->
expect(account.labeledReceivingAddresses.length).toEqual(1)
account.setLabelForReceivingAddress(1, "label1")
account.setLabelForReceivingAddress(10, "label100")
expect(account.labeledReceivingAddresses.length).toEqual(3)
describe ".encrypt", ->
beforeEach ->
account = new HDAccount(object)
it 'should fail and don\'t sync when encryption fails', ->
wrongEnc = () -> account.encrypt(() -> null)
expect(wrongEnc).toThrow()
expect(MyWallet.syncWallet).not.toHaveBeenCalled()
it 'should write in a temporary field and let the original key intact', ->
originalKey = account.extendedPrivateKey
account.encrypt(() -> "encrypted key")
expect(account._temporal_xpriv).toEqual("encrypted key")
expect(account.extendedPrivateKey).toEqual(originalKey)
expect(MyWallet.syncWallet).not.toHaveBeenCalled()
it 'should do nothing if watch only account', ->
account._xpriv = null
account.encrypt(() -> "encrypted key")
expect(account.extendedPrivateKey).toEqual(null)
expect(MyWallet.syncWallet).not.toHaveBeenCalled()
it 'should do nothing if no cipher provided', ->
originalKey = account.extendedPrivateKey
account.encrypt(undefined)
expect(account.extendedPrivateKey).toEqual(originalKey)
expect(MyWallet.syncWallet).not.toHaveBeenCalled()
describe ".decrypt", ->
beforeEach ->
account = new HDAccount(object)
it 'should fail and don\'t sync when decryption fails', ->
wrongEnc = () -> account.decrypt(() -> null)
expect(wrongEnc).toThrow()
expect(MyWallet.syncWallet).not.toHaveBeenCalled()
it 'should write in a temporary field and let the original key intact', ->
originalKey = account.extendedPrivateKey
account.decrypt(() -> "decrypted key")
expect(account._temporal_xpriv).toEqual("decrypted key")
expect(account.extendedPrivateKey).toEqual(originalKey)
expect(MyWallet.syncWallet).not.toHaveBeenCalled()
it 'should do nothing if watch only account', ->
account._xpriv = null
account.decrypt(() -> "decrypted key")
expect(account.extendedPrivateKey).toEqual(null)
expect(MyWallet.syncWallet).not.toHaveBeenCalled()
it 'should do nothing if no cipher provided', ->
originalKey = account.extendedPrivateKey
account.decrypt(undefined)
expect(account.extendedPrivateKey).toEqual(originalKey)
expect(MyWallet.syncWallet).not.toHaveBeenCalled()
describe ".persist", ->
beforeEach ->
account = new HDAccount(object)
it 'should do nothing if temporary is empty', ->
originalKey = account.extendedPrivateKey
account.persist()
expect(account.extendedPrivateKey).toEqual(originalKey)
expect(MyWallet.syncWallet).not.toHaveBeenCalled()
it 'should swap and delete if we have a temporary value', ->
account._temporal_xpriv = "encrypted key"
temp = account._temporal_xpriv
account.persist()
expect(account.extendedPrivateKey).toEqual(temp)
expect(account._temporal_xpriv).not.toBeDefined()
expect(MyWallet.syncWallet).not.toHaveBeenCalled()
describe ".removeLabelForReceivingAddress", ->
it "should remove the label and sync the wallet", ->
fail = (reason) ->
console.log(reason)
resolve = () ->
account.setLabelForReceivingAddress(0, "Savings").then(resolve).catch(fail)
expect(MyWallet.syncWallet).toHaveBeenCalled()
account.removeLabelForReceivingAddress(0)
expect(MyWallet.syncWallet).toHaveBeenCalled()
expect(account.getLabelForReceivingAddress(0)).not.toEqual("Savings")
describe ".fromExtPublicKey", ->
it "should import a correct key", ->
account = HDAccount.fromExtPublicKey("xpub661MyMwAqRbcFtXgS5sYJABqqG9YLmC4Q1Rdap9gSE8NqtwybGhePY2gZ29ESFjqJoCu1Rupje8YtGqsefD265TMg7usUDFdp6W1EGMcet8", 0, "New account")
expect(account._xpriv).toEqual(null)
expect(account.label).toEqual("New account")
it "should not import a truncated key", ->
expect(() -> HDAccount.fromExtPublicKey("xpub661MyMwAqRbcFtXgS5sYJABqqG9YLmC4Q1Rdap9gSE8NqtwybGh", 0, "New account")).toThrowError('Invalid checksum')
describe ".fromExtPrivateKey", ->
it "should import a correct key", ->
account = HDAccount.fromExtPrivateKey("xprv9s21ZrQH143K3QTDL4LXw2F7HEK3wJUD2nW2nRk4stbPy6cq3jPPqjiChkVvvNKmPGJxWUtg6LnF5kejMRNNU3TGtRBeJgk33yuGBxrMPHi", undefined, "Another new account")
expect(account.label).toEqual("Another new account")
expect(account._xpub).toEqual("xpub661MyMwAqRbcFtXgS5sYJABqqG9YLmC4Q1Rdap9gSE8NqtwybGhePY2gZ29ESFjqJoCu1Rupje8YtGqsefD265TMg7usUDFdp6W1EGMcet8")
expect(account.isEncrypted).toBeFalsy()
expect(account.isUnEncrypted).toBeTruthy()
expect(account.index).toEqual(null)
it "should not import a truncated key", ->
expect(() -> HDAccount.fromExtPrivateKey("xprv9s21ZrQH143K3QTDL4LXw2F7HEK3wJUD2nW2nRk4stbPy6cq3jPPqjiChkVvvNKmPGJxWUtg6", undefined, "Another new account")).toThrowError('Invalid checksum')
describe ".factory", ->
it "should not touch already instanciated objects", ->
fromFactory = HDAccount.factory(account)
expect(account).toEqual(fromFactory)