@opcat-labs/opcat 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (138) hide show
  1. package/.mocharc.yaml +3 -0
  2. package/index.d.ts +1541 -0
  3. package/index.js +74 -0
  4. package/lib/address.js +478 -0
  5. package/lib/block/block.js +277 -0
  6. package/lib/block/blockheader.js +295 -0
  7. package/lib/block/index.js +4 -0
  8. package/lib/block/merkleblock.js +323 -0
  9. package/lib/bn.js +3423 -0
  10. package/lib/crypto/bn.js +278 -0
  11. package/lib/crypto/ecdsa.js +339 -0
  12. package/lib/crypto/hash.browser.js +171 -0
  13. package/lib/crypto/hash.js +2 -0
  14. package/lib/crypto/hash.node.js +171 -0
  15. package/lib/crypto/point.js +221 -0
  16. package/lib/crypto/random.browser.js +28 -0
  17. package/lib/crypto/random.js +2 -0
  18. package/lib/crypto/random.node.js +11 -0
  19. package/lib/crypto/signature.js +325 -0
  20. package/lib/encoding/base58.js +111 -0
  21. package/lib/encoding/base58check.js +121 -0
  22. package/lib/encoding/bufferreader.js +212 -0
  23. package/lib/encoding/bufferwriter.js +140 -0
  24. package/lib/encoding/decode-asm.js +24 -0
  25. package/lib/encoding/decode-hex.js +32 -0
  26. package/lib/encoding/decode-script-chunks.js +43 -0
  27. package/lib/encoding/encode-hex.js +284 -0
  28. package/lib/encoding/is-hex.js +7 -0
  29. package/lib/encoding/varint.js +75 -0
  30. package/lib/errors/index.js +54 -0
  31. package/lib/errors/spec.js +314 -0
  32. package/lib/hash-cache.js +50 -0
  33. package/lib/hdprivatekey.js +678 -0
  34. package/lib/hdpublickey.js +525 -0
  35. package/lib/message/message.js +191 -0
  36. package/lib/mnemonic/mnemonic.js +303 -0
  37. package/lib/mnemonic/pbkdf2.browser.js +68 -0
  38. package/lib/mnemonic/pbkdf2.js +2 -0
  39. package/lib/mnemonic/pbkdf2.node.js +68 -0
  40. package/lib/mnemonic/words/chinese.js +2054 -0
  41. package/lib/mnemonic/words/english.js +2054 -0
  42. package/lib/mnemonic/words/french.js +2054 -0
  43. package/lib/mnemonic/words/index.js +8 -0
  44. package/lib/mnemonic/words/italian.js +2054 -0
  45. package/lib/mnemonic/words/japanese.js +2054 -0
  46. package/lib/mnemonic/words/spanish.js +2054 -0
  47. package/lib/networks.js +379 -0
  48. package/lib/opcode.js +255 -0
  49. package/lib/privatekey.js +374 -0
  50. package/lib/publickey.js +386 -0
  51. package/lib/script/index.js +5 -0
  52. package/lib/script/interpreter.js +1834 -0
  53. package/lib/script/script.js +1074 -0
  54. package/lib/script/stack.js +109 -0
  55. package/lib/script/write-i32-le.js +17 -0
  56. package/lib/script/write-push-data.js +35 -0
  57. package/lib/script/write-u16-le.js +12 -0
  58. package/lib/script/write-u32-le.js +16 -0
  59. package/lib/script/write-u64-le.js +24 -0
  60. package/lib/script/write-u8-le.js +8 -0
  61. package/lib/script/write-varint.js +46 -0
  62. package/lib/transaction/index.js +7 -0
  63. package/lib/transaction/input/index.js +5 -0
  64. package/lib/transaction/input/input.js +354 -0
  65. package/lib/transaction/input/multisig.js +242 -0
  66. package/lib/transaction/input/publickey.js +100 -0
  67. package/lib/transaction/input/publickeyhash.js +118 -0
  68. package/lib/transaction/output.js +231 -0
  69. package/lib/transaction/sighash.js +167 -0
  70. package/lib/transaction/signature.js +97 -0
  71. package/lib/transaction/transaction.js +1639 -0
  72. package/lib/transaction/unspentoutput.js +113 -0
  73. package/lib/util/_.js +47 -0
  74. package/lib/util/js.js +90 -0
  75. package/lib/util/preconditions.js +33 -0
  76. package/package.json +26 -0
  77. package/test/address.js +509 -0
  78. package/test/block/block.js +251 -0
  79. package/test/block/blockheader.js +275 -0
  80. package/test/block/merklebloack.js +211 -0
  81. package/test/crypto/bn.js +177 -0
  82. package/test/crypto/ecdsa.js +391 -0
  83. package/test/crypto/hash.browser.js +135 -0
  84. package/test/crypto/hash.js +136 -0
  85. package/test/crypto/point.js +224 -0
  86. package/test/crypto/random.js +32 -0
  87. package/test/crypto/signature.js +409 -0
  88. package/test/data/bip69.json +215 -0
  89. package/test/data/bitcoind/base58_keys_invalid.json +52 -0
  90. package/test/data/bitcoind/base58_keys_valid.json +335 -0
  91. package/test/data/bitcoind/blocks.json +22 -0
  92. package/test/data/bitcoind/script_tests.json +3822 -0
  93. package/test/data/bitcoind/sig_canonical.json +7 -0
  94. package/test/data/bitcoind/sig_noncanonical.json +36 -0
  95. package/test/data/bitcoind/tx_invalid.json +445 -0
  96. package/test/data/bitcoind/tx_valid.json +44 -0
  97. package/test/data/blk86756-testnet.dat +0 -0
  98. package/test/data/blk86756-testnet.js +14 -0
  99. package/test/data/blk86756-testnet.json +684 -0
  100. package/test/data/block.hex +1 -0
  101. package/test/data/ecdsa.json +230 -0
  102. package/test/data/merkleblocks.js +488 -0
  103. package/test/data/messages.json +22 -0
  104. package/test/data/sighash.json +12 -0
  105. package/test/data/tx_creation.json +95 -0
  106. package/test/encoding/base58.js +131 -0
  107. package/test/encoding/base58check.js +136 -0
  108. package/test/encoding/bufferreader.js +337 -0
  109. package/test/encoding/bufferwriter.js +172 -0
  110. package/test/encoding/varint.js +104 -0
  111. package/test/hashCache.js +67 -0
  112. package/test/hdkeys.js +445 -0
  113. package/test/hdprivatekey.js +332 -0
  114. package/test/hdpublickey.js +304 -0
  115. package/test/index.js +16 -0
  116. package/test/message/message.js +204 -0
  117. package/test/mnemonic/data/fixtures.json +300 -0
  118. package/test/mnemonic/mnemonic.js +259 -0
  119. package/test/mnemonic/mocha.opts +1 -0
  120. package/test/mnemonic/pbkdf2.test.js +59 -0
  121. package/test/networks.js +159 -0
  122. package/test/opcode.js +161 -0
  123. package/test/privatekey.js +439 -0
  124. package/test/publickey.js +554 -0
  125. package/test/script/interpreter.js +734 -0
  126. package/test/script/script.js +1437 -0
  127. package/test/transaction/deserialize.js +34 -0
  128. package/test/transaction/input/input.js +90 -0
  129. package/test/transaction/input/multisig.js +90 -0
  130. package/test/transaction/input/publickey.js +68 -0
  131. package/test/transaction/input/publickeyhash.js +51 -0
  132. package/test/transaction/output.js +185 -0
  133. package/test/transaction/sighash.js +65 -0
  134. package/test/transaction/signature.js +114 -0
  135. package/test/transaction/transaction.js +1109 -0
  136. package/test/transaction/unspentoutput.js +110 -0
  137. package/test/util/js.js +76 -0
  138. package/test/util/preconditions.js +79 -0
@@ -0,0 +1,509 @@
1
+ 'use strict';
2
+
3
+ var chai = require('chai');
4
+ var should = chai.should();
5
+ var expect = chai.expect;
6
+
7
+ var opcat = require('..');
8
+ var PublicKey = opcat.PublicKey;
9
+ var PrivateKey = opcat.PrivateKey;
10
+ var Address = opcat.Address;
11
+ var Script = opcat.Script;
12
+ var Networks = opcat.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"');
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
+ // livenet valid
76
+ var PKHLivenet = [
77
+ '15vkcKf7gB23wLAnZLmbVuMiiVDc1Nm4a2',
78
+ '1A6ut1tWnUq1SEQLMr4ttDh24wcbJ5o9TT',
79
+ '1BpbpfLdY7oBS9gK7aDXgvMgr1DPvNhEB2',
80
+ '1Jz2yCRd5ST1p2gUqFB5wsSQfdm3jaFfg7',
81
+ ' 1Jz2yCRd5ST1p2gUqFB5wsSQfdm3jaFfg7 \t\n',
82
+ ];
83
+
84
+
85
+ // livenet bad checksums
86
+ var badChecksums = [
87
+ '15vkcKf7gB23wLAnZLmbVuMiiVDc3nq4a2',
88
+ '1A6ut1tWnUq1SEQLMr4ttDh24wcbj4w2TT',
89
+ '1BpbpfLdY7oBS9gK7aDXgvMgr1DpvNH3B2',
90
+ '1Jz2yCRd5ST1p2gUqFB5wsSQfdmEJaffg7',
91
+ ];
92
+
93
+ // livenet non-base58
94
+ var nonBase58 = [
95
+ '15vkcKf7g#23wLAnZLmb$uMiiVDc3nq4a2',
96
+ '1A601ttWnUq1SEQLMr4ttDh24wcbj4w2TT',
97
+ '1BpbpfLdY7oBS9gK7aIXgvMgr1DpvNH3B2',
98
+ '1Jz2yCRdOST1p2gUqFB5wsSQfdmEJaffg7',
99
+ ];
100
+
101
+ // testnet valid
102
+ var PKHTestnet = [
103
+ 'n28S35tqEMbt6vNad7A5K3mZ7vdn8dZ86X',
104
+ 'n45x3R2w2jaSC62BMa9MeJCd3TXxgvDEmm',
105
+ 'mursDVxqNQmmwWHACpM9VHwVVSfTddGsEM',
106
+ 'mtX8nPZZdJ8d3QNLRJ1oJTiEi26Sj6LQXS',
107
+ ];
108
+
109
+ describe('validation', function () {
110
+ it('getValidationError detects network mismatchs', function () {
111
+ var error = Address.getValidationError('HC1hAdrx7APHg1DkE4bVLsZhY1SE5Dik1r', 'testnet');
112
+ should.exist(error);
113
+ });
114
+
115
+
116
+ it('validates correctly the P2PKH test vector', function () {
117
+ for (var i = 0; i < PKHLivenet.length; i++) {
118
+ var error = Address.getValidationError(PKHLivenet[i]);
119
+ should.not.exist(error);
120
+ }
121
+ });
122
+
123
+ it('rejects correctly the P2PKH livenet test vector with "testnet" parameter', function () {
124
+ for (var i = 0; i < PKHLivenet.length; i++) {
125
+ var error = Address.getValidationError(PKHLivenet[i], 'testnet');
126
+ should.exist(error);
127
+ }
128
+ });
129
+
130
+ it('validates correctly the P2PKH livenet test vector with "livenet" parameter', function () {
131
+ for (var i = 0; i < PKHLivenet.length; i++) {
132
+ var error = Address.getValidationError(PKHLivenet[i], 'livenet');
133
+ should.not.exist(error);
134
+ }
135
+ });
136
+
137
+ it('should not validate if checksum is invalid', function () {
138
+ for (var i = 0; i < badChecksums.length; i++) {
139
+ var error = Address.getValidationError(badChecksums[i], 'livenet', 'pubkeyhash');
140
+ should.exist(error);
141
+ error.message.should.match(/Checksum mismatch/);
142
+ }
143
+ });
144
+
145
+ it('should not validate on a network mismatch', function () {
146
+ var error, i;
147
+ for (i = 0; i < PKHLivenet.length; i++) {
148
+ error = Address.getValidationError(PKHLivenet[i], 'testnet', 'pubkeyhash');
149
+ should.exist(error);
150
+ error.message.should.equal('Address has mismatched network type.');
151
+ }
152
+ for (i = 0; i < PKHTestnet.length; i++) {
153
+ error = Address.getValidationError(PKHTestnet[i], 'livenet', 'pubkeyhash');
154
+ should.exist(error);
155
+ error.message.should.equal('Address has mismatched network type.');
156
+ }
157
+ });
158
+
159
+
160
+ it('should not validate on non-base58 characters', function () {
161
+ for (var i = 0; i < nonBase58.length; i++) {
162
+ var error = Address.getValidationError(nonBase58[i], 'livenet', 'pubkeyhash');
163
+ should.exist(error);
164
+ error.message.should.match(/Non-base58/);
165
+ }
166
+ });
167
+
168
+ it('testnet addresses are validated correctly', function () {
169
+ for (var i = 0; i < PKHTestnet.length; i++) {
170
+ var error = Address.getValidationError(PKHTestnet[i], 'testnet');
171
+ should.not.exist(error);
172
+ }
173
+ });
174
+
175
+ it('addresses with whitespace are validated correctly', function () {
176
+ var ws = ' \r \t \n 1A6ut1tWnUq1SEQLMr4ttDh24wcbJ5o9TT \t \n \r';
177
+ var error = Address.getValidationError(ws);
178
+ should.not.exist(error);
179
+ Address.fromString(ws).toString().should.equal('1A6ut1tWnUq1SEQLMr4ttDh24wcbJ5o9TT');
180
+ });
181
+ });
182
+
183
+ describe('instantiation', function () {
184
+ it('can be instantiated from another address', function () {
185
+ var address = Address.fromBuffer(buf);
186
+ var address2 = new Address({
187
+ hashBuffer: address.hashBuffer,
188
+ network: address.network,
189
+ type: address.type,
190
+ });
191
+ address.toString().should.equal(address2.toString());
192
+ });
193
+ });
194
+
195
+ describe('@fromBuffer', function () {
196
+ it('can be instantiated from another address', function () {
197
+ var address = Address.fromBuffer(buf);
198
+ var address2 = new Address({
199
+ hashBuffer: address.hashBuffer,
200
+ network: address.network,
201
+ type: address.type,
202
+ });
203
+ address.toString().should.equal(address2.toString());
204
+ });
205
+ });
206
+
207
+ describe('@fromHex', function () {
208
+ it('can be instantiated from another address', function () {
209
+ var address = Address.fromHex(buf.toString('hex'));
210
+ var address2 = new Address({
211
+ hashBuffer: address.hashBuffer,
212
+ network: address.network,
213
+ type: address.type,
214
+ });
215
+ address.toString().should.equal(address2.toString());
216
+ });
217
+ });
218
+
219
+ describe('encodings', function () {
220
+ it('should make an address from a buffer', function () {
221
+ Address.fromBuffer(buf).toString().should.equal(str);
222
+ new Address(buf).toString().should.equal(str);
223
+ new Address(buf).toString().should.equal(str);
224
+ });
225
+
226
+ it('should make an address from a string', function () {
227
+ Address.fromString(str).toString().should.equal(str);
228
+ new Address(str).toString().should.equal(str);
229
+ });
230
+
231
+ it('should make an address using a non-string network', function () {
232
+ Address.fromString(str, Networks.livenet).toString().should.equal(str);
233
+ });
234
+
235
+ it('should throw with bad network param', function () {
236
+ (function () {
237
+ Address.fromString(str, 'somenet');
238
+ }).should.throw('Unknown network');
239
+ });
240
+
241
+ it('should error because of unrecognized data format', function () {
242
+ (function () {
243
+ return new Address(new Error());
244
+ }).should.throw(opcat.errors.InvalidArgument);
245
+ });
246
+
247
+ it('should error because of incorrect format for pubkey hash', function () {
248
+ (function () {
249
+ return new Address.fromPublicKeyHash('notahash');
250
+ }).should.throw('Address supplied is not a buffer.');
251
+ });
252
+
253
+ it('should error because of incorrect type for transform buffer', function () {
254
+ (function () {
255
+ return Address._transformBuffer('notabuffer');
256
+ }).should.throw('Address supplied is not a buffer.');
257
+ });
258
+
259
+ it('should error because of incorrect length buffer for transform buffer', function () {
260
+ (function () {
261
+ return Address._transformBuffer(Buffer.alloc(20));
262
+ }).should.throw('Address buffers must be exactly 21 bytes.');
263
+ });
264
+
265
+ it('should error because of incorrect type for pubkey transform', function () {
266
+ (function () {
267
+ return Address._transformPublicKey(Buffer.alloc(20));
268
+ }).should.throw('Address must be an instance of PublicKey.');
269
+ });
270
+
271
+ it('should error because of incorrect type for script transform', function () {
272
+ (function () {
273
+ return Address._transformScript(Buffer.alloc(20));
274
+ }).should.throw('Invalid Argument: script must be a Script instance');
275
+ });
276
+
277
+ it('should error because of incorrect type for string transform', function () {
278
+ (function () {
279
+ return Address._transformString(Buffer.alloc(20));
280
+ }).should.throw('data parameter supplied is not a string.');
281
+ });
282
+
283
+ it('should make an address from a pubkey hash buffer', function () {
284
+ var hash = pubkeyhash; // use the same hash
285
+ var a = Address.fromPublicKeyHash(hash, 'livenet');
286
+ a.network.should.equal(Networks.livenet);
287
+ a.toString().should.equal(str);
288
+ var b = Address.fromPublicKeyHash(hash, 'testnet');
289
+ b.network.should.equal(Networks.testnet);
290
+ b.type.should.equal('pubkeyhash');
291
+ new Address(hash, 'livenet').toString().should.equal(str);
292
+ });
293
+
294
+ it('should make an address using the default network', function () {
295
+ var hash = pubkeyhash; // use the same hash
296
+ var network = Networks.defaultNetwork;
297
+ Networks.defaultNetwork = Networks.livenet;
298
+ var a = Address.fromPublicKeyHash(hash);
299
+ a.network.should.equal(Networks.livenet);
300
+ // change the default
301
+ Networks.defaultNetwork = Networks.testnet;
302
+ var b = Address.fromPublicKeyHash(hash);
303
+ b.network.should.equal(Networks.testnet);
304
+ // restore the default
305
+ Networks.defaultNetwork = network;
306
+ });
307
+
308
+ it('should throw an error for invalid length hashBuffer', function () {
309
+ (function () {
310
+ return Address.fromPublicKeyHash(buf);
311
+ }).should.throw('Address hashbuffers must be exactly 20 bytes.');
312
+ });
313
+
314
+ it('should make this address from a compressed pubkey', function () {
315
+ var pubkey = new PublicKey(
316
+ '0285e9737a74c30a873f74df05124f2aa6f53042c2fc0a130d6cbd7d16b944b004',
317
+ );
318
+ var address = Address.fromPublicKey(pubkey, 'livenet');
319
+ address.toString().should.equal('19gH5uhqY6DKrtkU66PsZPUZdzTd11Y7ke');
320
+ });
321
+
322
+ it('should use the default network for pubkey', function () {
323
+ var pubkey = new PublicKey(
324
+ '0285e9737a74c30a873f74df05124f2aa6f53042c2fc0a130d6cbd7d16b944b004',
325
+ );
326
+ var address = Address.fromPublicKey(pubkey);
327
+ address.network.should.equal(Networks.defaultNetwork);
328
+ });
329
+
330
+ it('should make this address from an uncompressed pubkey', function () {
331
+ var pubkey = new PublicKey(
332
+ '0485e9737a74c30a873f74df05124f2aa6f53042c2fc0a130d6cbd7d16b944b00' +
333
+ '4833fef26c8be4c4823754869ff4e46755b85d851077771c220e2610496a29d98',
334
+ );
335
+ var a = Address.fromPublicKey(pubkey, 'livenet');
336
+ a.toString().should.equal('16JXnhxjJUhxfyx4y6H4sFcxrgt8kQ8ewX');
337
+ var b = new Address(pubkey, 'livenet', 'pubkeyhash');
338
+ b.toString().should.equal('16JXnhxjJUhxfyx4y6H4sFcxrgt8kQ8ewX');
339
+ });
340
+
341
+ it('should classify from a custom network', function () {
342
+ var custom = {
343
+ name: 'customnetwork',
344
+ pubkeyhash: 10,
345
+ privatekey: 0x1e,
346
+ scripthash: 15,
347
+ xpubkey: 0x02e8de8f,
348
+ xprivkey: 0x02e8da54,
349
+ networkMagic: 0x0c110907,
350
+ port: 7333,
351
+ };
352
+ Networks.add(custom);
353
+ var addressString = '57gZdnwcQHLirKLwDHcFiWLq9jTZwRaxaE';
354
+ var network = Networks.get('customnetwork');
355
+ var address = Address.fromString(addressString);
356
+ address.type.should.equal(Address.PayToPublicKeyHash);
357
+ address.network.should.equal(network);
358
+ Networks.remove(network);
359
+ });
360
+
361
+ describe('from a script', function () {
362
+ it('should fail to build address from a non p2sh,p2pkh script', function () {
363
+ var s = new Script('OP_CHECKMULTISIG');
364
+ (function () {
365
+ return new Address(s);
366
+ }).should.throw('needs to be p2pkh in, p2pkh out, p2sh in, or p2sh out');
367
+ });
368
+ it('should make this address from a p2pkh output script', function () {
369
+ var s = new Script(
370
+ 'OP_DUP OP_HASH160 20 ' +
371
+ '0xc8e11b0eb0d2ad5362d894f048908341fa61b6e1 OP_EQUALVERIFY OP_CHECKSIG',
372
+ );
373
+ var a = Address.fromScript(s, 'livenet');
374
+ a.toString().should.equal('1KK9oz4bFH8c1t6LmighHaoSEGx3P3FEmc');
375
+ var b = new Address(s, 'livenet');
376
+ b.toString().should.equal('1KK9oz4bFH8c1t6LmighHaoSEGx3P3FEmc');
377
+ });
378
+
379
+ it('returns the same address if the script is a pay to public key hash out', function () {
380
+ var address = '16JXnhxjJUhxfyx4y6H4sFcxrgt8kQ8ewX';
381
+ var script = Script.buildPublicKeyHashOut(new Address(address));
382
+ Address(script, Networks.livenet).toString().should.equal(address);
383
+ });
384
+ });
385
+
386
+ it('should derive from this known address string livenet', function () {
387
+ var address = new Address(str);
388
+ var buffer = address.toBuffer();
389
+ var slice = buffer.slice(1);
390
+ var sliceString = slice.toString('hex');
391
+ sliceString.should.equal(pubkeyhash.toString('hex'));
392
+ });
393
+
394
+ it('should derive from this known address string testnet', function () {
395
+ var a = new Address(PKHTestnet[0], 'testnet');
396
+ var b = new Address(a.toString());
397
+ b.toString().should.equal(PKHTestnet[0]);
398
+ b.network.should.equal(Networks.testnet);
399
+ });
400
+
401
+ });
402
+
403
+ describe('#toBuffer', function () {
404
+ it('3c3fa3d4adcaf8f52d5b1843975e122548269937 corresponds to hash 16VZnHwRhwrExfeHFHGjwrgEMq8VcYPs9r', function () {
405
+ var address = new Address(str);
406
+ address.toBuffer().slice(1).toString('hex').should.equal(pubkeyhash.toString('hex'));
407
+ });
408
+ });
409
+
410
+ describe('#toHex', function () {
411
+ it('3c3fa3d4adcaf8f52d5b1843975e122548269937 corresponds to hash 16VZnHwRhwrExfeHFHGjwrgEMq8VcYPs9r', function () {
412
+ var address = new Address(str);
413
+ address.toHex().slice(2).should.equal(pubkeyhash.toString('hex'));
414
+ });
415
+ });
416
+
417
+ describe('#object', function () {
418
+ it('roundtrip to-from-to', function () {
419
+ var obj = new Address(str).toObject();
420
+ var address = Address.fromObject(obj);
421
+ address.toString().should.equal(str);
422
+ });
423
+
424
+ it('will fail with invalid state', function () {
425
+ expect(function () {
426
+ return Address.fromObject('¹');
427
+ }).to.throw(opcat.errors.InvalidState);
428
+ });
429
+ });
430
+
431
+ describe('#toString', function () {
432
+ it('livenet pubkeyhash address', function () {
433
+ var address = new Address(str);
434
+ address.toString().should.equal(str);
435
+ });
436
+
437
+ it('testnet pubkeyhash address', function () {
438
+ var address = new Address(PKHTestnet[0]);
439
+ address.toString().should.equal(PKHTestnet[0]);
440
+ });
441
+ });
442
+
443
+ describe('#inspect', function () {
444
+ it('should output formatted output correctly', function () {
445
+ var address = new Address(str);
446
+ var output =
447
+ '<Address: 16VZnHwRhwrExfeHFHGjwrgEMq8VcYPs9r, type: pubkeyhash, network: livenet>';
448
+ address.inspect().should.equal(output);
449
+ });
450
+ });
451
+
452
+ describe('questions about the address', function () {
453
+ it('should detect a Pay To PubkeyHash address', function () {
454
+ new Address(PKHLivenet[0]).isPayToPublicKeyHash().should.equal(true);
455
+ new Address(PKHTestnet[0]).isPayToPublicKeyHash().should.equal(true);
456
+ });
457
+ });
458
+
459
+ it("throws an error if it couldn't instantiate", function () {
460
+ expect(function () {
461
+ return new Address(1);
462
+ }).to.throw(TypeError);
463
+ });
464
+
465
+ it('will use the default network for an object', function () {
466
+ var obj = {
467
+ hash: '19a7d869032368fd1f1e26e5e73a4ad0e474960e',
468
+ type: 'scripthash',
469
+ };
470
+ var address = new Address(obj);
471
+ address.network.should.equal(Networks.defaultNetwork);
472
+ });
473
+
474
+ describe('#fromPublicKey', function () {
475
+ it('should derive from public key', function () {
476
+ let privateKey = PrivateKey.fromRandom();
477
+ let publicKey = PublicKey.fromPrivateKey(privateKey);
478
+ let address = Address.fromPublicKey(publicKey);
479
+ address.toString()[0].should.equal('1');
480
+ });
481
+
482
+ it('should derive from public key testnet', function () {
483
+ let privateKey = PrivateKey.fromRandom('testnet');
484
+ let publicKey = PublicKey.fromPrivateKey(privateKey);
485
+ let address = Address.fromPublicKey(publicKey, 'testnet');
486
+ (address.toString()[0] === 'm' || address.toString()[0] === 'n').should.equal(true);
487
+ });
488
+ });
489
+
490
+ describe('#fromPrivateKey', function () {
491
+ it('should derive from public key', function () {
492
+ let privateKey = PrivateKey.fromRandom();
493
+ let address = Address.fromPrivateKey(privateKey);
494
+ address.toString()[0].should.equal('1');
495
+ });
496
+
497
+ it('should derive from public key testnet', function () {
498
+ let privateKey = PrivateKey.fromRandom('testnet');
499
+ let address = Address.fromPrivateKey(privateKey, 'testnet');
500
+ (address.toString()[0] === 'm' || address.toString()[0] === 'n').should.equal(true);
501
+ });
502
+
503
+ it('should derive from public key testnet', function () {
504
+ let privateKey = PrivateKey.fromRandom('testnet');
505
+ let address = Address.fromPrivateKey(privateKey);
506
+ (address.toString()[0] === 'm' || address.toString()[0] === 'n').should.equal(true);
507
+ });
508
+ });
509
+ });