1 | 'use strict'
|
2 |
|
3 | var chai = require('chai')
|
4 | var should = chai.should()
|
5 | var expect = chai.expect
|
6 |
|
7 | var bsv = require('..')
|
8 | var PublicKey = bsv.PublicKey
|
9 | var PrivateKey = bsv.PrivateKey
|
10 | var Address = bsv.Address
|
11 | var Script = bsv.Script
|
12 | var Networks = bsv.Networks
|
13 |
|
14 | var validbase58 = require('./data/bitcoind/base58_keys_valid.json')
|
15 | var invalidbase58 = require('./data/bitcoind/base58_keys_invalid.json')
|
16 |
|
17 | describe('Address', function () {
|
18 | var pubkeyhash = Buffer.from('3c3fa3d4adcaf8f52d5b1843975e122548269937', 'hex')
|
19 | var buf = Buffer.concat([Buffer.from([0]), pubkeyhash])
|
20 | var str = '16VZnHwRhwrExfeHFHGjwrgEMq8VcYPs9r'
|
21 |
|
22 | it('can\'t build without data', function () {
|
23 | (function () {
|
24 | return new Address()
|
25 | }).should.throw('First argument is required, please include address data.')
|
26 | })
|
27 |
|
28 | it('should throw an error because of bad network param', function () {
|
29 | (function () {
|
30 | return new Address(PKHLivenet[0], 'main', 'pubkeyhash')
|
31 | }).should.throw('Second argument must be "livenet", "testnet", or "regtest".')
|
32 | })
|
33 |
|
34 | it('should throw an error because of bad type param', function () {
|
35 | (function () {
|
36 | return new Address(PKHLivenet[0], 'livenet', 'pubkey')
|
37 | }).should.throw('Third argument must be "pubkeyhash" or "scripthash"')
|
38 | })
|
39 |
|
40 | describe('bitcoind compliance', function () {
|
41 | validbase58.map(function (d) {
|
42 | if (!d[2].isPrivkey) {
|
43 | it('should describe address ' + d[0] + ' as valid', function () {
|
44 | var type
|
45 | if (d[2].addrType === 'script') {
|
46 | type = 'scripthash'
|
47 | } else if (d[2].addrType === 'pubkey') {
|
48 | type = 'pubkeyhash'
|
49 | }
|
50 | var network = 'livenet'
|
51 | if (d[2].isTestnet) {
|
52 | network = 'testnet'
|
53 | }
|
54 | return new Address(d[0], network, type)
|
55 | })
|
56 | }
|
57 | })
|
58 | invalidbase58.map(function (d) {
|
59 | it('should describe input ' + d[0].slice(0, 10) + '... as invalid', function () {
|
60 | expect(function () {
|
61 | return new Address(d[0])
|
62 | }).to.throw(Error)
|
63 | })
|
64 | })
|
65 | })
|
66 |
|
67 | describe('generic tests', function () {
|
68 | it('should pass these tests', function () {
|
69 | var str = '13k3vneZ3yvZnc9dNWYH2RJRFsagTfAERv'
|
70 | var address = Address.fromString(str)
|
71 | address.toString().should.equal(str)
|
72 | })
|
73 | })
|
74 |
|
75 |
|
76 | var PKHLivenet = [
|
77 | '15vkcKf7gB23wLAnZLmbVuMiiVDc1Nm4a2',
|
78 | '1A6ut1tWnUq1SEQLMr4ttDh24wcbJ5o9TT',
|
79 | '1BpbpfLdY7oBS9gK7aDXgvMgr1DPvNhEB2',
|
80 | '1Jz2yCRd5ST1p2gUqFB5wsSQfdm3jaFfg7',
|
81 | ' 1Jz2yCRd5ST1p2gUqFB5wsSQfdm3jaFfg7 \t\n'
|
82 | ]
|
83 |
|
84 |
|
85 | var P2SHLivenet = [
|
86 | '342ftSRCvFHfCeFFBuz4xwbeqnDw6BGUey',
|
87 | '33vt8ViH5jsr115AGkW6cEmEz9MpvJSwDk',
|
88 | '37Sp6Rv3y4kVd1nQ1JV5pfqXccHNyZm1x3',
|
89 | '3QjYXhTkvuj8qPaXHTTWb5wjXhdsLAAWVy',
|
90 | '\t3QjYXhTkvuj8qPaXHTTWb5wjXhdsLAAWVy \n \r'
|
91 | ]
|
92 |
|
93 |
|
94 | var P2SHTestnet = [
|
95 | '2N7FuwuUuoTBrDFdrAZ9KxBmtqMLxce9i1C',
|
96 | '2NEWDzHWwY5ZZp8CQWbB7ouNMLqCia6YRda',
|
97 | '2MxgPqX1iThW3oZVk9KoFcE5M4JpiETssVN',
|
98 | '2NB72XtkjpnATMggui83aEtPawyyKvnbX2o'
|
99 | ]
|
100 |
|
101 |
|
102 | var badChecksums = [
|
103 | '15vkcKf7gB23wLAnZLmbVuMiiVDc3nq4a2',
|
104 | '1A6ut1tWnUq1SEQLMr4ttDh24wcbj4w2TT',
|
105 | '1BpbpfLdY7oBS9gK7aDXgvMgr1DpvNH3B2',
|
106 | '1Jz2yCRd5ST1p2gUqFB5wsSQfdmEJaffg7'
|
107 | ]
|
108 |
|
109 |
|
110 | var nonBase58 = [
|
111 | '15vkcKf7g#23wLAnZLmb$uMiiVDc3nq4a2',
|
112 | '1A601ttWnUq1SEQLMr4ttDh24wcbj4w2TT',
|
113 | '1BpbpfLdY7oBS9gK7aIXgvMgr1DpvNH3B2',
|
114 | '1Jz2yCRdOST1p2gUqFB5wsSQfdmEJaffg7'
|
115 | ]
|
116 |
|
117 |
|
118 | var PKHTestnet = [
|
119 | 'n28S35tqEMbt6vNad7A5K3mZ7vdn8dZ86X',
|
120 | 'n45x3R2w2jaSC62BMa9MeJCd3TXxgvDEmm',
|
121 | 'mursDVxqNQmmwWHACpM9VHwVVSfTddGsEM',
|
122 | 'mtX8nPZZdJ8d3QNLRJ1oJTiEi26Sj6LQXS'
|
123 | ]
|
124 |
|
125 | describe('validation', function () {
|
126 | it('getValidationError detects network mismatchs', function () {
|
127 | var error = Address.getValidationError('HC1hAdrx7APHg1DkE4bVLsZhY1SE5Dik1r', 'testnet')
|
128 | should.exist(error)
|
129 | })
|
130 |
|
131 | it('isValid returns true on a valid livenet address', function () {
|
132 | Address.isValid('37BahqRsFrAd3qLiNNwLNV3AWMRD7itxTo', 'livenet').should.equal(true)
|
133 | })
|
134 |
|
135 | it('isValid returns false on network mismatch', function () {
|
136 | Address.isValid('37BahqRsFrAd3qLiNNwLNV3AWMRD7itxTo', 'testnet').should.equal(false)
|
137 | Address.isValid('37BahqRsFrAd3qLiNNwLNV3AWMRD7itxTo', 'regtest').should.equal(false)
|
138 | })
|
139 |
|
140 | it('validates correctly the P2PKH test vector', function () {
|
141 | for (var i = 0; i < PKHLivenet.length; i++) {
|
142 | var error = Address.getValidationError(PKHLivenet[i])
|
143 | should.not.exist(error)
|
144 | }
|
145 | })
|
146 |
|
147 | it('validates correctly the P2SH test vector', function () {
|
148 | for (var i = 0; i < P2SHLivenet.length; i++) {
|
149 | var error = Address.getValidationError(P2SHLivenet[i])
|
150 | should.not.exist(error)
|
151 | }
|
152 | })
|
153 |
|
154 | it('validates correctly the P2SH testnet test vector', function () {
|
155 | for (var i = 0; i < P2SHTestnet.length; i++) {
|
156 | var error = Address.getValidationError(P2SHTestnet[i], 'testnet')
|
157 | should.not.exist(error)
|
158 | }
|
159 | })
|
160 |
|
161 | it('rejects correctly the P2PKH livenet test vector with "testnet" parameter', function () {
|
162 | for (var i = 0; i < PKHLivenet.length; i++) {
|
163 | var error = Address.getValidationError(PKHLivenet[i], 'testnet')
|
164 | should.exist(error)
|
165 | }
|
166 | })
|
167 |
|
168 | it('validates correctly the P2PKH livenet test vector with "livenet" parameter', function () {
|
169 | for (var i = 0; i < PKHLivenet.length; i++) {
|
170 | var error = Address.getValidationError(PKHLivenet[i], 'livenet')
|
171 | should.not.exist(error)
|
172 | }
|
173 | })
|
174 |
|
175 | it('should not validate if checksum is invalid', function () {
|
176 | for (var i = 0; i < badChecksums.length; i++) {
|
177 | var error = Address.getValidationError(badChecksums[i], 'livenet', 'pubkeyhash')
|
178 | should.exist(error)
|
179 | error.message.should.match(/Checksum mismatch/)
|
180 | }
|
181 | })
|
182 |
|
183 | it('should not validate on a network mismatch', function () {
|
184 | var error,
|
185 | i
|
186 | for (i = 0; i < PKHLivenet.length; i++) {
|
187 | error = Address.getValidationError(PKHLivenet[i], 'testnet', 'pubkeyhash')
|
188 | should.exist(error)
|
189 | error.message.should.equal('Address has mismatched network type.')
|
190 | }
|
191 | for (i = 0; i < PKHTestnet.length; i++) {
|
192 | error = Address.getValidationError(PKHTestnet[i], 'livenet', 'pubkeyhash')
|
193 | should.exist(error)
|
194 | error.message.should.equal('Address has mismatched network type.')
|
195 | }
|
196 | })
|
197 |
|
198 | it('should not validate on a type mismatch', function () {
|
199 | for (var i = 0; i < PKHLivenet.length; i++) {
|
200 | var error = Address.getValidationError(PKHLivenet[i], 'livenet', 'scripthash')
|
201 | should.exist(error)
|
202 | error.message.should.equal('Address has mismatched type.')
|
203 | }
|
204 | })
|
205 |
|
206 | it('should not validate on non-base58 characters', function () {
|
207 | for (var i = 0; i < nonBase58.length; i++) {
|
208 | var error = Address.getValidationError(nonBase58[i], 'livenet', 'pubkeyhash')
|
209 | should.exist(error)
|
210 | error.message.should.match(/Non-base58/)
|
211 | }
|
212 | })
|
213 |
|
214 | it('testnet addresses are validated correctly', function () {
|
215 | for (var i = 0; i < PKHTestnet.length; i++) {
|
216 | var error = Address.getValidationError(PKHTestnet[i], 'testnet')
|
217 | should.not.exist(error)
|
218 | }
|
219 | })
|
220 |
|
221 | it('addresses with whitespace are validated correctly', function () {
|
222 | var ws = ' \r \t \n 1A6ut1tWnUq1SEQLMr4ttDh24wcbJ5o9TT \t \n \r'
|
223 | var error = Address.getValidationError(ws)
|
224 | should.not.exist(error)
|
225 | Address.fromString(ws).toString().should.equal('1A6ut1tWnUq1SEQLMr4ttDh24wcbJ5o9TT')
|
226 | })
|
227 | })
|
228 |
|
229 | describe('instantiation', function () {
|
230 | it('can be instantiated from another address', function () {
|
231 | var address = Address.fromBuffer(buf)
|
232 | var address2 = new Address({
|
233 | hashBuffer: address.hashBuffer,
|
234 | network: address.network,
|
235 | type: address.type
|
236 | })
|
237 | address.toString().should.equal(address2.toString())
|
238 | })
|
239 | })
|
240 |
|
241 | describe('@fromBuffer', function () {
|
242 | it('can be instantiated from another address', function () {
|
243 | var address = Address.fromBuffer(buf)
|
244 | var address2 = new Address({
|
245 | hashBuffer: address.hashBuffer,
|
246 | network: address.network,
|
247 | type: address.type
|
248 | })
|
249 | address.toString().should.equal(address2.toString())
|
250 | })
|
251 | })
|
252 |
|
253 | describe('@fromHex', function () {
|
254 | it('can be instantiated from another address', function () {
|
255 | var address = Address.fromHex(buf.toString('hex'))
|
256 | var address2 = new Address({
|
257 | hashBuffer: address.hashBuffer,
|
258 | network: address.network,
|
259 | type: address.type
|
260 | })
|
261 | address.toString().should.equal(address2.toString())
|
262 | })
|
263 | })
|
264 |
|
265 | describe('encodings', function () {
|
266 | it('should make an address from a buffer', function () {
|
267 | Address.fromBuffer(buf).toString().should.equal(str)
|
268 | new Address(buf).toString().should.equal(str)
|
269 | new Address(buf).toString().should.equal(str)
|
270 | })
|
271 |
|
272 | it('should make an address from a string', function () {
|
273 | Address.fromString(str).toString().should.equal(str)
|
274 | new Address(str).toString().should.equal(str)
|
275 | })
|
276 |
|
277 | it('should make an address using a non-string network', function () {
|
278 | Address.fromString(str, Networks.livenet).toString().should.equal(str)
|
279 | })
|
280 |
|
281 | it('should throw with bad network param', function () {
|
282 | (function () {
|
283 | Address.fromString(str, 'somenet')
|
284 | }).should.throw('Unknown network')
|
285 | })
|
286 |
|
287 | it('should error because of unrecognized data format', function () {
|
288 | (function () {
|
289 | return new Address(new Error())
|
290 | }).should.throw(bsv.errors.InvalidArgument)
|
291 | })
|
292 |
|
293 | it('should error because of incorrect format for pubkey hash', function () {
|
294 | (function () {
|
295 | return new Address.fromPublicKeyHash('notahash')
|
296 | }).should.throw('Address supplied is not a buffer.')
|
297 | })
|
298 |
|
299 | it('should error because of incorrect format for script hash', function () {
|
300 | (function () {
|
301 | return new Address.fromScriptHash('notascript')
|
302 | }).should.throw('Address supplied is not a buffer.')
|
303 | })
|
304 |
|
305 | it('should error because of incorrect type for transform buffer', function () {
|
306 | (function () {
|
307 | return Address._transformBuffer('notabuffer')
|
308 | }).should.throw('Address supplied is not a buffer.')
|
309 | })
|
310 |
|
311 | it('should error because of incorrect length buffer for transform buffer', function () {
|
312 | (function () {
|
313 | return Address._transformBuffer(Buffer.alloc(20))
|
314 | }).should.throw('Address buffers must be exactly 21 bytes.')
|
315 | })
|
316 |
|
317 | it('should error because of incorrect type for pubkey transform', function () {
|
318 | (function () {
|
319 | return Address._transformPublicKey(Buffer.alloc(20))
|
320 | }).should.throw('Address must be an instance of PublicKey.')
|
321 | })
|
322 |
|
323 | it('should error because of incorrect type for script transform', function () {
|
324 | (function () {
|
325 | return Address._transformScript(Buffer.alloc(20))
|
326 | }).should.throw('Invalid Argument: script must be a Script instance')
|
327 | })
|
328 |
|
329 | it('should error because of incorrect type for string transform', function () {
|
330 | (function () {
|
331 | return Address._transformString(Buffer.alloc(20))
|
332 | }).should.throw('data parameter supplied is not a string.')
|
333 | })
|
334 |
|
335 | it('should make an address from a pubkey hash buffer', function () {
|
336 | var hash = pubkeyhash
|
337 | var a = Address.fromPublicKeyHash(hash, 'livenet')
|
338 | a.network.should.equal(Networks.livenet)
|
339 | a.toString().should.equal(str)
|
340 | var b = Address.fromPublicKeyHash(hash, 'testnet')
|
341 | b.network.should.equal(Networks.testnet)
|
342 | b.type.should.equal('pubkeyhash')
|
343 | new Address(hash, 'livenet').toString().should.equal(str)
|
344 | })
|
345 |
|
346 | it('should make an address using the default network', function () {
|
347 | var hash = pubkeyhash
|
348 | var network = Networks.defaultNetwork
|
349 | Networks.defaultNetwork = Networks.livenet
|
350 | var a = Address.fromPublicKeyHash(hash)
|
351 | a.network.should.equal(Networks.livenet)
|
352 |
|
353 | Networks.defaultNetwork = Networks.testnet
|
354 | var b = Address.fromPublicKeyHash(hash)
|
355 | b.network.should.equal(Networks.testnet)
|
356 |
|
357 | Networks.defaultNetwork = network
|
358 | })
|
359 |
|
360 | it('should throw an error for invalid length hashBuffer', function () {
|
361 | (function () {
|
362 | return Address.fromPublicKeyHash(buf)
|
363 | }).should.throw('Address hashbuffers must be exactly 20 bytes.')
|
364 | })
|
365 |
|
366 | it('should make this address from a compressed pubkey', function () {
|
367 | var pubkey = new PublicKey('0285e9737a74c30a873f74df05124f2aa6f53042c2fc0a130d6cbd7d16b944b004')
|
368 | var address = Address.fromPublicKey(pubkey, 'livenet')
|
369 | address.toString().should.equal('19gH5uhqY6DKrtkU66PsZPUZdzTd11Y7ke')
|
370 | })
|
371 |
|
372 | it('should use the default network for pubkey', function () {
|
373 | var pubkey = new PublicKey('0285e9737a74c30a873f74df05124f2aa6f53042c2fc0a130d6cbd7d16b944b004')
|
374 | var address = Address.fromPublicKey(pubkey)
|
375 | address.network.should.equal(Networks.defaultNetwork)
|
376 | })
|
377 |
|
378 | it('should make this address from an uncompressed pubkey', function () {
|
379 | var pubkey = new PublicKey('0485e9737a74c30a873f74df05124f2aa6f53042c2fc0a130d6cbd7d16b944b00' +
|
380 | '4833fef26c8be4c4823754869ff4e46755b85d851077771c220e2610496a29d98')
|
381 | var a = Address.fromPublicKey(pubkey, 'livenet')
|
382 | a.toString().should.equal('16JXnhxjJUhxfyx4y6H4sFcxrgt8kQ8ewX')
|
383 | var b = new Address(pubkey, 'livenet', 'pubkeyhash')
|
384 | b.toString().should.equal('16JXnhxjJUhxfyx4y6H4sFcxrgt8kQ8ewX')
|
385 | })
|
386 |
|
387 | it('should classify from a custom network', function () {
|
388 | var custom = {
|
389 | name: 'customnetwork',
|
390 | pubkeyhash: 10,
|
391 | privatekey: 0x1e,
|
392 | scripthash: 15,
|
393 | xpubkey: 0x02e8de8f,
|
394 | xprivkey: 0x02e8da54,
|
395 | networkMagic: 0x0c110907,
|
396 | port: 7333
|
397 | }
|
398 | Networks.add(custom)
|
399 | var addressString = '57gZdnwcQHLirKLwDHcFiWLq9jTZwRaxaE'
|
400 | var network = Networks.get('customnetwork')
|
401 | var address = Address.fromString(addressString)
|
402 | address.type.should.equal(Address.PayToPublicKeyHash)
|
403 | address.network.should.equal(network)
|
404 | Networks.remove(network)
|
405 | })
|
406 |
|
407 | describe('from a script', function () {
|
408 | it('should fail to build address from a non p2sh,p2pkh script', function () {
|
409 | var s = new Script('OP_CHECKMULTISIG');
|
410 | (function () {
|
411 | return new Address(s)
|
412 | }).should.throw('needs to be p2pkh in, p2pkh out, p2sh in, or p2sh out')
|
413 | })
|
414 | it('should make this address from a p2pkh output script', function () {
|
415 | var s = new Script('OP_DUP OP_HASH160 20 ' +
|
416 | '0xc8e11b0eb0d2ad5362d894f048908341fa61b6e1 OP_EQUALVERIFY OP_CHECKSIG')
|
417 | var a = Address.fromScript(s, 'livenet')
|
418 | a.toString().should.equal('1KK9oz4bFH8c1t6LmighHaoSEGx3P3FEmc')
|
419 | var b = new Address(s, 'livenet')
|
420 | b.toString().should.equal('1KK9oz4bFH8c1t6LmighHaoSEGx3P3FEmc')
|
421 | })
|
422 |
|
423 | it('should make this address from a p2sh input script', function () {
|
424 | var s = Script.fromString('OP_HASH160 20 0xa6ed4af315271e657ee307828f54a4365fa5d20f OP_EQUAL')
|
425 | var a = Address.fromScript(s, 'livenet')
|
426 | a.toString().should.equal('3GueMn6ruWVfQTN4XKBGEbCbGLwRSUhfnS')
|
427 | var b = new Address(s, 'livenet')
|
428 | b.toString().should.equal('3GueMn6ruWVfQTN4XKBGEbCbGLwRSUhfnS')
|
429 | })
|
430 |
|
431 | it('returns the same address if the script is a pay to public key hash out', function () {
|
432 | var address = '16JXnhxjJUhxfyx4y6H4sFcxrgt8kQ8ewX'
|
433 | var script = Script.buildPublicKeyHashOut(new Address(address))
|
434 | Address(script, Networks.livenet).toString().should.equal(address)
|
435 | })
|
436 | it('returns the same address if the script is a pay to script hash out', function () {
|
437 | var address = '3BYmEwgV2vANrmfRymr1mFnHXgLjD6gAWm'
|
438 | var script = Script.buildScriptHashOut(new Address(address))
|
439 | Address(script, Networks.livenet).toString().should.equal(address)
|
440 | })
|
441 | })
|
442 |
|
443 | it('should derive from this known address string livenet', function () {
|
444 | var address = new Address(str)
|
445 | var buffer = address.toBuffer()
|
446 | var slice = buffer.slice(1)
|
447 | var sliceString = slice.toString('hex')
|
448 | sliceString.should.equal(pubkeyhash.toString('hex'))
|
449 | })
|
450 |
|
451 | it('should derive from this known address string testnet', function () {
|
452 | var a = new Address(PKHTestnet[0], 'testnet')
|
453 | var b = new Address(a.toString())
|
454 | b.toString().should.equal(PKHTestnet[0])
|
455 | b.network.should.equal(Networks.testnet)
|
456 | })
|
457 |
|
458 | it('should derive from this known address string livenet scripthash', function () {
|
459 | var a = new Address(P2SHLivenet[0], 'livenet', 'scripthash')
|
460 | var b = new Address(a.toString())
|
461 | b.toString().should.equal(P2SHLivenet[0])
|
462 | })
|
463 |
|
464 | it('should derive from this known address string testnet scripthash', function () {
|
465 | var address = new Address(P2SHTestnet[0], 'testnet', 'scripthash')
|
466 | address = new Address(address.toString())
|
467 | address.toString().should.equal(P2SHTestnet[0])
|
468 | })
|
469 | })
|
470 |
|
471 | describe('#toBuffer', function () {
|
472 | it('3c3fa3d4adcaf8f52d5b1843975e122548269937 corresponds to hash 16VZnHwRhwrExfeHFHGjwrgEMq8VcYPs9r', function () {
|
473 | var address = new Address(str)
|
474 | address.toBuffer().slice(1).toString('hex').should.equal(pubkeyhash.toString('hex'))
|
475 | })
|
476 | })
|
477 |
|
478 | describe('#toHex', function () {
|
479 | it('3c3fa3d4adcaf8f52d5b1843975e122548269937 corresponds to hash 16VZnHwRhwrExfeHFHGjwrgEMq8VcYPs9r', function () {
|
480 | var address = new Address(str)
|
481 | address.toHex().slice(2).should.equal(pubkeyhash.toString('hex'))
|
482 | })
|
483 | })
|
484 |
|
485 | describe('#object', function () {
|
486 | it('roundtrip to-from-to', function () {
|
487 | var obj = new Address(str).toObject()
|
488 | var address = Address.fromObject(obj)
|
489 | address.toString().should.equal(str)
|
490 | })
|
491 |
|
492 | it('will fail with invalid state', function () {
|
493 | expect(function () {
|
494 | return Address.fromObject('¹')
|
495 | }).to.throw(bsv.errors.InvalidState)
|
496 | })
|
497 | })
|
498 |
|
499 | describe('#toString', function () {
|
500 | it('livenet pubkeyhash address', function () {
|
501 | var address = new Address(str)
|
502 | address.toString().should.equal(str)
|
503 | })
|
504 |
|
505 | it('scripthash address', function () {
|
506 | var address = new Address(P2SHLivenet[0])
|
507 | address.toString().should.equal(P2SHLivenet[0])
|
508 | })
|
509 |
|
510 | it('testnet scripthash address', function () {
|
511 | var address = new Address(P2SHTestnet[0])
|
512 | address.toString().should.equal(P2SHTestnet[0])
|
513 | })
|
514 |
|
515 | it('testnet pubkeyhash address', function () {
|
516 | var address = new Address(PKHTestnet[0])
|
517 | address.toString().should.equal(PKHTestnet[0])
|
518 | })
|
519 | })
|
520 |
|
521 | describe('#inspect', function () {
|
522 | it('should output formatted output correctly', function () {
|
523 | var address = new Address(str)
|
524 | var output = '<Address: 16VZnHwRhwrExfeHFHGjwrgEMq8VcYPs9r, type: pubkeyhash, network: livenet>'
|
525 | address.inspect().should.equal(output)
|
526 | })
|
527 | })
|
528 |
|
529 | describe('questions about the address', function () {
|
530 | it('should detect a P2SH address', function () {
|
531 | new Address(P2SHLivenet[0]).isPayToScriptHash().should.equal(true)
|
532 | new Address(P2SHLivenet[0]).isPayToPublicKeyHash().should.equal(false)
|
533 | new Address(P2SHTestnet[0]).isPayToScriptHash().should.equal(true)
|
534 | new Address(P2SHTestnet[0]).isPayToPublicKeyHash().should.equal(false)
|
535 | })
|
536 | it('should detect a Pay To PubkeyHash address', function () {
|
537 | new Address(PKHLivenet[0]).isPayToPublicKeyHash().should.equal(true)
|
538 | new Address(PKHLivenet[0]).isPayToScriptHash().should.equal(false)
|
539 | new Address(PKHTestnet[0]).isPayToPublicKeyHash().should.equal(true)
|
540 | new Address(PKHTestnet[0]).isPayToScriptHash().should.equal(false)
|
541 | })
|
542 | })
|
543 |
|
544 | it('throws an error if it couldn\'t instantiate', function () {
|
545 | expect(function () {
|
546 | return new Address(1)
|
547 | }).to.throw(TypeError)
|
548 | })
|
549 | it('can roundtrip from/to a object', function () {
|
550 | var address = new Address(P2SHLivenet[0])
|
551 | expect(new Address(address.toObject()).toString()).to.equal(P2SHLivenet[0])
|
552 | })
|
553 |
|
554 | it('will use the default network for an object', function () {
|
555 | var obj = {
|
556 | hash: '19a7d869032368fd1f1e26e5e73a4ad0e474960e',
|
557 | type: 'scripthash'
|
558 | }
|
559 | var address = new Address(obj)
|
560 | address.network.should.equal(Networks.defaultNetwork)
|
561 | })
|
562 |
|
563 | describe('creating a P2SH address from public keys', function () {
|
564 | var public1 = '02da5798ed0c055e31339eb9b5cef0d3c0ccdec84a62e2e255eb5c006d4f3e7f5b'
|
565 | var public2 = '0272073bf0287c4469a2a011567361d42529cd1a72ab0d86aa104ecc89342ffeb0'
|
566 | var public3 = '02738a516a78355db138e8119e58934864ce222c553a5407cf92b9c1527e03c1a2'
|
567 | var publics = [public1, public2, public3]
|
568 |
|
569 | it('can create an address from a set of public keys', function () {
|
570 | var address = Address.createMultisig(publics, 2, Networks.livenet)
|
571 | address.toString().should.equal('3FtqPRirhPvrf7mVUSkygyZ5UuoAYrTW3y')
|
572 | address = new Address(publics, 2, Networks.livenet)
|
573 | address.toString().should.equal('3FtqPRirhPvrf7mVUSkygyZ5UuoAYrTW3y')
|
574 | })
|
575 |
|
576 | it('works on testnet also', function () {
|
577 | var address = Address.createMultisig(publics, 2, Networks.testnet)
|
578 | address.toString().should.equal('2N7T3TAetJrSCruQ39aNrJvYLhG1LJosujf')
|
579 | })
|
580 |
|
581 | it('can also be created by Address.createMultisig', function () {
|
582 | var address = Address.createMultisig(publics, 2)
|
583 | var address2 = Address.createMultisig(publics, 2)
|
584 | address.toString().should.equal(address2.toString())
|
585 | })
|
586 |
|
587 | it('fails if invalid array is provided', function () {
|
588 | expect(function () {
|
589 | return Address.createMultisig([], 3, 'testnet')
|
590 | }).to.throw('Number of required signatures must be less than or equal to the number of public keys')
|
591 | })
|
592 | })
|
593 |
|
594 | describe('#fromPublicKey', function () {
|
595 | it('should derive from public key', function () {
|
596 | let privateKey = PrivateKey.fromRandom()
|
597 | let publicKey = PublicKey.fromPrivateKey(privateKey)
|
598 | let address = Address.fromPublicKey(publicKey)
|
599 | address.toString()[0].should.equal('1')
|
600 | })
|
601 |
|
602 | it('should derive from public key testnet', function () {
|
603 | let privateKey = PrivateKey.fromRandom('testnet')
|
604 | let publicKey = PublicKey.fromPrivateKey(privateKey)
|
605 | let address = Address.fromPublicKey(publicKey, 'testnet')
|
606 | ;(address.toString()[0] === 'm' || address.toString()[0] === 'n').should.equal(true)
|
607 | })
|
608 | })
|
609 |
|
610 | describe('#fromPrivateKey', function () {
|
611 | it('should derive from public key', function () {
|
612 | let privateKey = PrivateKey.fromRandom()
|
613 | let address = Address.fromPrivateKey(privateKey)
|
614 | address.toString()[0].should.equal('1')
|
615 | })
|
616 |
|
617 | it('should derive from public key testnet', function () {
|
618 | let privateKey = PrivateKey.fromRandom('testnet')
|
619 | let address = Address.fromPrivateKey(privateKey, 'testnet')
|
620 | ;(address.toString()[0] === 'm' || address.toString()[0] === 'n').should.equal(true)
|
621 | })
|
622 |
|
623 | it('should derive from public key testnet', function () {
|
624 | let privateKey = PrivateKey.fromRandom('testnet')
|
625 | let address = Address.fromPrivateKey(privateKey)
|
626 | ;(address.toString()[0] === 'm' || address.toString()[0] === 'n').should.equal(true)
|
627 | })
|
628 | })
|
629 | })
|