@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,554 @@
1
+ 'use strict';
2
+
3
+ var should = require('chai').should();
4
+ var expect = require('chai').expect;
5
+
6
+ var opcat = require('..');
7
+ var Point = opcat.crypto.Point;
8
+ var BN = opcat.crypto.BN;
9
+ var PublicKey = opcat.PublicKey;
10
+ var PrivateKey = opcat.PrivateKey;
11
+ var Address = opcat.Address;
12
+ var Networks = opcat.Networks;
13
+
14
+ describe('PublicKey', function () {
15
+ var invalidPoint =
16
+ '0400000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000';
17
+
18
+ describe('validating errors on creation', function () {
19
+ it('errors if data is missing', function () {
20
+ (function () {
21
+ return new PublicKey();
22
+ }).should.throw('First argument is required, please include public key data.');
23
+ });
24
+
25
+ it('errors if an invalid point is provided', function () {
26
+ (function () {
27
+ return new PublicKey(invalidPoint);
28
+ }).should.throw('Point does not lie on the curve');
29
+ });
30
+
31
+ it('errors if a point not on the secp256k1 curve is provided', function () {
32
+ (function () {
33
+ return new PublicKey(new Point(1000, 1000));
34
+ }).should.throw('Point does not lie on the curve');
35
+ });
36
+
37
+ it('errors if the argument is of an unrecognized type', function () {
38
+ (function () {
39
+ return new PublicKey(new Error());
40
+ }).should.throw('First argument is an unrecognized data format.');
41
+ });
42
+ });
43
+
44
+ describe('instantiation', function () {
45
+ it('from a private key', function () {
46
+ var privhex = '906977a061af29276e40bf377042ffbde414e496ae2260bbf1fa9d085637bfff';
47
+ var pubhex = '02a1633cafcc01ebfb6d78e39f687a1f0995c62fc95f51ead10a02ee0be551b5dc';
48
+ var privkey = new PrivateKey(new BN(Buffer.from(privhex, 'hex')));
49
+ var pk = new PublicKey(privkey);
50
+ pk.toString().should.equal(pubhex);
51
+ });
52
+
53
+ it('problematic secp256k1 public keys', function () {
54
+ var knownKeys = [
55
+ {
56
+ wif: 'KzsjKq2FVqVuQv2ueHVFuB65A9uEZ6S1L6F8NuokCrE3V3kE3Ack',
57
+ priv: '6d1229a6b24c2e775c062870ad26bc261051e0198c67203167273c7c62538846',
58
+ pub: '03d6106302d2698d6a41e9c9a114269e7be7c6a0081317de444bb2980bf9265a01',
59
+ pubx: 'd6106302d2698d6a41e9c9a114269e7be7c6a0081317de444bb2980bf9265a01',
60
+ puby: 'e05fb262e64b108991a29979809fcef9d3e70cafceb3248c922c17d83d66bc9d',
61
+ },
62
+ {
63
+ wif: 'L5MgSwNB2R76xBGorofRSTuQFd1bm3hQMFVf3u2CneFom8u1Yt7G',
64
+ priv: 'f2cc9d2b008927db94b89e04e2f6e70c180e547b3e5e564b06b8215d1c264b53',
65
+ pub: '03e275faa35bd1e88f5df6e8f9f6edb93bdf1d65f4915efc79fd7a726ec0c21700',
66
+ pubx: 'e275faa35bd1e88f5df6e8f9f6edb93bdf1d65f4915efc79fd7a726ec0c21700',
67
+ puby: '367216cb35b086e6686d69dddd822a8f4d52eb82ac5d9de18fdcd9bf44fa7df7',
68
+ },
69
+ ];
70
+
71
+ for (var i = 0; i < knownKeys.length; i++) {
72
+ var privkey = new PrivateKey(knownKeys[i].wif);
73
+ var pubkey = privkey.toPublicKey();
74
+ pubkey.toString().should.equal(knownKeys[i].pub);
75
+ pubkey.point.x.toString('hex').should.equal(knownKeys[i].pubx);
76
+ pubkey.point.y.toString('hex').should.equal(knownKeys[i].puby);
77
+ }
78
+ });
79
+
80
+ it('from a compressed public key', function () {
81
+ var publicKeyHex = '031ff0fe0f7b15ffaa85ff9f4744d539139c252a49710fb053bb9f2b933173ff9a';
82
+ var publicKey = new PublicKey(publicKeyHex);
83
+ publicKey.toString().should.equal(publicKeyHex);
84
+ });
85
+
86
+ it('from another publicKey', function () {
87
+ var publicKeyHex = '031ff0fe0f7b15ffaa85ff9f4744d539139c252a49710fb053bb9f2b933173ff9a';
88
+ var publicKey = new PublicKey(publicKeyHex);
89
+ var publicKey2 = new PublicKey(publicKey);
90
+ publicKey.should.equal(publicKey2);
91
+ });
92
+
93
+ it('sets the network to defaultNetwork if none provided', function () {
94
+ var publicKeyHex = '031ff0fe0f7b15ffaa85ff9f4744d539139c252a49710fb053bb9f2b933173ff9a';
95
+ var publicKey = new PublicKey(publicKeyHex);
96
+ publicKey.network.should.equal(Networks.defaultNetwork);
97
+ });
98
+
99
+ it('from a hex encoded DER string', function () {
100
+ var pk = new PublicKey(
101
+ '041ff0fe0f7b15ffaa85ff9f4744d539139c252a49710fb053bb9f2b933173ff9a7baad41d04514751e6851f5304fd243751703bed21b914f6be218c0fa354a341',
102
+ );
103
+ should.exist(pk.point);
104
+ pk.point
105
+ .getX()
106
+ .toString(16)
107
+ .should.equal('1ff0fe0f7b15ffaa85ff9f4744d539139c252a49710fb053bb9f2b933173ff9a');
108
+ });
109
+
110
+ it('from a hex encoded DER buffer', function () {
111
+ var pk = new PublicKey(
112
+ Buffer.from(
113
+ '041ff0fe0f7b15ffaa85ff9f4744d539139c252a49710fb053bb9f2b933173ff9a7baad41d04514751e6851f5304fd243751703bed21b914f6be218c0fa354a341',
114
+ 'hex',
115
+ ),
116
+ );
117
+ should.exist(pk.point);
118
+ pk.point
119
+ .getX()
120
+ .toString(16)
121
+ .should.equal('1ff0fe0f7b15ffaa85ff9f4744d539139c252a49710fb053bb9f2b933173ff9a');
122
+ });
123
+
124
+ it('from a point', function () {
125
+ var p = new Point(
126
+ '86a80a5a2bfc48dddde2b0bd88bd56b0b6ddc4e6811445b175b90268924d7d48',
127
+ '3b402dfc89712cfe50963e670a0598e6b152b3cd94735001cdac6794975d3afd',
128
+ );
129
+ var a = new PublicKey(p);
130
+ should.exist(a.point);
131
+ a.point.toString().should.equal(p.toString());
132
+ var c = new PublicKey(p);
133
+ should.exist(c.point);
134
+ c.point.toString().should.equal(p.toString());
135
+ });
136
+ });
137
+
138
+ describe('#getValidationError', function () {
139
+ it('should recieve an invalid point error', function () {
140
+ var error = PublicKey.getValidationError(invalidPoint);
141
+ should.exist(error);
142
+ error.message.should.equal('Point does not lie on the curve');
143
+ });
144
+
145
+ it('should recieve a boolean as false', function () {
146
+ var valid = PublicKey.isValid(invalidPoint);
147
+ valid.should.equal(false);
148
+ });
149
+
150
+ it('should recieve a boolean as true for uncompressed', function () {
151
+ var valid = PublicKey.isValid(
152
+ '041ff0fe0f7b15ffaa85ff9f4744d539139c252a49710fb053bb9f2b933173ff9a7baad41d04514751e6851f5304fd243751703bed21b914f6be218c0fa354a341',
153
+ );
154
+ valid.should.equal(true);
155
+ });
156
+
157
+ it('should recieve a boolean as true for compressed', function () {
158
+ var valid = PublicKey.isValid(
159
+ '031ff0fe0f7b15ffaa85ff9f4744d539139c252a49710fb053bb9f2b933173ff9a',
160
+ );
161
+ valid.should.equal(true);
162
+ });
163
+ });
164
+
165
+ describe('#fromPoint', function () {
166
+ it('should instantiate from a point', function () {
167
+ var p = new Point(
168
+ '86a80a5a2bfc48dddde2b0bd88bd56b0b6ddc4e6811445b175b90268924d7d48',
169
+ '3b402dfc89712cfe50963e670a0598e6b152b3cd94735001cdac6794975d3afd',
170
+ );
171
+ var b = PublicKey.fromPoint(p);
172
+ should.exist(b.point);
173
+ b.point.toString().should.equal(p.toString());
174
+ });
175
+
176
+ it('should error because paramater is not a point', function () {
177
+ (function () {
178
+ PublicKey.fromPoint(new Error());
179
+ }).should.throw('First argument must be an instance of Point.');
180
+ });
181
+ });
182
+
183
+ describe('#json/object', function () {
184
+ it('should input/ouput json', function () {
185
+ var json = JSON.stringify({
186
+ x: '1ff0fe0f7b15ffaa85ff9f4744d539139c252a49710fb053bb9f2b933173ff9a',
187
+ y: '7baad41d04514751e6851f5304fd243751703bed21b914f6be218c0fa354a341',
188
+ compressed: false,
189
+ });
190
+ var pubkey = new PublicKey(JSON.parse(json));
191
+ JSON.stringify(pubkey).should.deep.equal(json);
192
+ });
193
+
194
+ it('fails if "y" is not provided', function () {
195
+ expect(function () {
196
+ return new PublicKey({
197
+ x: '1ff0fe0f7b15ffaa85ff9f4744d539139c252a49710fb053bb9f2b933173ff9a',
198
+ });
199
+ }).to.throw();
200
+ });
201
+
202
+ it('fails if invalid JSON is provided', function () {
203
+ expect(function () {
204
+ return PublicKey._transformJSON('¹');
205
+ }).to.throw();
206
+ });
207
+
208
+ it('works for X starting with 0x00', function () {
209
+ var a = new PublicKey('030589ee559348bd6a7325994f9c8eff12bd5d73cc683142bd0dd1a17abc99b0dc');
210
+ var b = new PublicKey('03' + a.toObject().x);
211
+ b.toString().should.equal(a.toString());
212
+ });
213
+ });
214
+
215
+ describe('#fromPrivateKey', function () {
216
+ it('should make a public key from a privkey', function () {
217
+ should.exist(PublicKey.fromPrivateKey(PrivateKey.fromRandom()));
218
+ });
219
+
220
+ it('should error because not an instance of privkey', function () {
221
+ (function () {
222
+ PublicKey.fromPrivateKey(new Error());
223
+ }).should.throw('Must be an instance of PrivateKey');
224
+ });
225
+ });
226
+
227
+ describe('#fromBuffer', function () {
228
+ it('should parse this uncompressed public key', function () {
229
+ var pk = PublicKey.fromBuffer(
230
+ Buffer.from(
231
+ '041ff0fe0f7b15ffaa85ff9f4744d539139c252a49710fb053bb9f2b933173ff9a7baad41d04514751e6851f5304fd243751703bed21b914f6be218c0fa354a341',
232
+ 'hex',
233
+ ),
234
+ );
235
+ pk.point
236
+ .getX()
237
+ .toString(16)
238
+ .should.equal('1ff0fe0f7b15ffaa85ff9f4744d539139c252a49710fb053bb9f2b933173ff9a');
239
+ pk.point
240
+ .getY()
241
+ .toString(16)
242
+ .should.equal('7baad41d04514751e6851f5304fd243751703bed21b914f6be218c0fa354a341');
243
+ });
244
+
245
+ it('should parse this compressed public key', function () {
246
+ var pk = PublicKey.fromBuffer(
247
+ Buffer.from('031ff0fe0f7b15ffaa85ff9f4744d539139c252a49710fb053bb9f2b933173ff9a', 'hex'),
248
+ );
249
+ pk.point
250
+ .getX()
251
+ .toString(16)
252
+ .should.equal('1ff0fe0f7b15ffaa85ff9f4744d539139c252a49710fb053bb9f2b933173ff9a');
253
+ pk.point
254
+ .getY()
255
+ .toString(16)
256
+ .should.equal('7baad41d04514751e6851f5304fd243751703bed21b914f6be218c0fa354a341');
257
+ });
258
+
259
+ it('should throw an error on this invalid public key', function () {
260
+ (function () {
261
+ PublicKey.fromBuffer(
262
+ Buffer.from('091ff0fe0f7b15ffaa85ff9f4744d539139c252a49710fb053bb9f2b933173ff9a', 'hex'),
263
+ );
264
+ }).should.throw();
265
+ });
266
+
267
+ it('should throw error because not a buffer', function () {
268
+ (function () {
269
+ PublicKey.fromBuffer('091ff0fe0f7b15ffaa85ff9f4744d539139c252a49710fb053bb9f2b933173ff9a');
270
+ }).should.throw('Must be a buffer of DER encoded public key');
271
+ });
272
+
273
+ it('should throw error because buffer is the incorrect length', function () {
274
+ (function () {
275
+ PublicKey.fromBuffer(
276
+ Buffer.from(
277
+ '041ff0fe0f7b15ffaa85ff9f4744d539139c252a49710fb053bb9f2b933173ff9a7baad41d04514751e6851f5304fd243751703bed21b914f6be218c0fa354a34112',
278
+ 'hex',
279
+ ),
280
+ );
281
+ }).should.throw('Length of x and y must be 32 bytes');
282
+ });
283
+ });
284
+
285
+ describe('#fromDER', function () {
286
+ it('should parse this uncompressed public key', function () {
287
+ var pk = PublicKey.fromDER(
288
+ Buffer.from(
289
+ '041ff0fe0f7b15ffaa85ff9f4744d539139c252a49710fb053bb9f2b933173ff9a7baad41d04514751e6851f5304fd243751703bed21b914f6be218c0fa354a341',
290
+ 'hex',
291
+ ),
292
+ );
293
+ pk.point
294
+ .getX()
295
+ .toString(16)
296
+ .should.equal('1ff0fe0f7b15ffaa85ff9f4744d539139c252a49710fb053bb9f2b933173ff9a');
297
+ pk.point
298
+ .getY()
299
+ .toString(16)
300
+ .should.equal('7baad41d04514751e6851f5304fd243751703bed21b914f6be218c0fa354a341');
301
+ });
302
+
303
+ it('should parse this compressed public key', function () {
304
+ var pk = PublicKey.fromDER(
305
+ Buffer.from('031ff0fe0f7b15ffaa85ff9f4744d539139c252a49710fb053bb9f2b933173ff9a', 'hex'),
306
+ );
307
+ pk.point
308
+ .getX()
309
+ .toString(16)
310
+ .should.equal('1ff0fe0f7b15ffaa85ff9f4744d539139c252a49710fb053bb9f2b933173ff9a');
311
+ pk.point
312
+ .getY()
313
+ .toString(16)
314
+ .should.equal('7baad41d04514751e6851f5304fd243751703bed21b914f6be218c0fa354a341');
315
+ });
316
+
317
+ it('should throw an error on this invalid public key', function () {
318
+ (function () {
319
+ PublicKey.fromDER(
320
+ Buffer.from('091ff0fe0f7b15ffaa85ff9f4744d539139c252a49710fb053bb9f2b933173ff9a', 'hex'),
321
+ );
322
+ }).should.throw();
323
+ });
324
+ });
325
+
326
+ describe('#fromString', function () {
327
+ it('should parse this known valid public key', function () {
328
+ var pk = PublicKey.fromString(
329
+ '041ff0fe0f7b15ffaa85ff9f4744d539139c252a49710fb053bb9f2b933173ff9a7baad41d04514751e6851f5304fd243751703bed21b914f6be218c0fa354a341',
330
+ );
331
+ pk.point
332
+ .getX()
333
+ .toString(16)
334
+ .should.equal('1ff0fe0f7b15ffaa85ff9f4744d539139c252a49710fb053bb9f2b933173ff9a');
335
+ pk.point
336
+ .getY()
337
+ .toString(16)
338
+ .should.equal('7baad41d04514751e6851f5304fd243751703bed21b914f6be218c0fa354a341');
339
+ });
340
+ });
341
+
342
+ describe('#fromHex', function () {
343
+ it('should parse this known valid public key', function () {
344
+ var pk = PublicKey.fromHex(
345
+ '041ff0fe0f7b15ffaa85ff9f4744d539139c252a49710fb053bb9f2b933173ff9a7baad41d04514751e6851f5304fd243751703bed21b914f6be218c0fa354a341',
346
+ );
347
+ pk.point
348
+ .getX()
349
+ .toString(16)
350
+ .should.equal('1ff0fe0f7b15ffaa85ff9f4744d539139c252a49710fb053bb9f2b933173ff9a');
351
+ pk.point
352
+ .getY()
353
+ .toString(16)
354
+ .should.equal('7baad41d04514751e6851f5304fd243751703bed21b914f6be218c0fa354a341');
355
+ });
356
+ });
357
+
358
+ describe('#fromX', function () {
359
+ it('should create this known public key', function () {
360
+ var x = BN.fromBuffer(
361
+ Buffer.from('1ff0fe0f7b15ffaa85ff9f4744d539139c252a49710fb053bb9f2b933173ff9a', 'hex'),
362
+ );
363
+ var pk = PublicKey.fromX(true, x);
364
+ pk.point
365
+ .getX()
366
+ .toString(16)
367
+ .should.equal('1ff0fe0f7b15ffaa85ff9f4744d539139c252a49710fb053bb9f2b933173ff9a');
368
+ pk.point
369
+ .getY()
370
+ .toString(16)
371
+ .should.equal('7baad41d04514751e6851f5304fd243751703bed21b914f6be218c0fa354a341');
372
+ });
373
+
374
+ it('should error because odd was not included as a param', function () {
375
+ var x = BN.fromBuffer(
376
+ Buffer.from('1ff0fe0f7b15ffaa85ff9f4744d539139c252a49710fb053bb9f2b933173ff9a', 'hex'),
377
+ );
378
+ (function () {
379
+ return PublicKey.fromX(null, x);
380
+ }).should.throw('Must specify whether y is odd or not (true or false)');
381
+ });
382
+ });
383
+
384
+ describe('#toBuffer', function () {
385
+ it('should return this compressed DER format', function () {
386
+ var x = BN.fromBuffer(
387
+ Buffer.from('1ff0fe0f7b15ffaa85ff9f4744d539139c252a49710fb053bb9f2b933173ff9a', 'hex'),
388
+ );
389
+ var pk = PublicKey.fromX(true, x);
390
+ pk.toBuffer()
391
+ .toString('hex')
392
+ .should.equal('031ff0fe0f7b15ffaa85ff9f4744d539139c252a49710fb053bb9f2b933173ff9a');
393
+ });
394
+
395
+ it('should return this uncompressed DER format', function () {
396
+ var x = BN.fromBuffer(
397
+ Buffer.from('1ff0fe0f7b15ffaa85ff9f4744d539139c252a49710fb053bb9f2b933173ff9a', 'hex'),
398
+ );
399
+ var pk = PublicKey.fromX(true, x);
400
+ pk.toBuffer()
401
+ .toString('hex')
402
+ .should.equal('031ff0fe0f7b15ffaa85ff9f4744d539139c252a49710fb053bb9f2b933173ff9a');
403
+ });
404
+ });
405
+
406
+ describe('#toDER', function () {
407
+ it('should return this compressed DER format', function () {
408
+ var x = BN.fromBuffer(
409
+ Buffer.from('1ff0fe0f7b15ffaa85ff9f4744d539139c252a49710fb053bb9f2b933173ff9a', 'hex'),
410
+ );
411
+ var pk = PublicKey.fromX(true, x);
412
+ pk.toDER()
413
+ .toString('hex')
414
+ .should.equal('031ff0fe0f7b15ffaa85ff9f4744d539139c252a49710fb053bb9f2b933173ff9a');
415
+ });
416
+
417
+ it('should return this uncompressed DER format', function () {
418
+ var pk = PublicKey.fromString(
419
+ '041ff0fe0f7b15ffaa85ff9f4744d539139c252a49710fb053bb9f2b933173ff9a7baad41d04514751e6851f5304fd243751703bed21b914f6be218c0fa354a341',
420
+ );
421
+ pk.toDER()
422
+ .toString('hex')
423
+ .should.equal(
424
+ '041ff0fe0f7b15ffaa85ff9f4744d539139c252a49710fb053bb9f2b933173ff9a7baad41d04514751e6851f5304fd243751703bed21b914f6be218c0fa354a341',
425
+ );
426
+ });
427
+ });
428
+
429
+ describe('#toAddress', function () {
430
+ it('should output this known mainnet address correctly', function () {
431
+ var pk = new PublicKey('03c87bd0e162f26969da8509cafcb7b8c8d202af30b928c582e263dd13ee9a9781');
432
+ var address = pk.toAddress('livenet');
433
+ address.toString().should.equal('1A6ut1tWnUq1SEQLMr4ttDh24wcbJ5o9TT');
434
+ });
435
+
436
+ it('should output this known testnet address correctly', function () {
437
+ var pk = new PublicKey('0293126ccc927c111b88a0fe09baa0eca719e2a3e087e8a5d1059163f5c566feef');
438
+ var address = pk.toAddress('testnet');
439
+ address.toString().should.equal('mtX8nPZZdJ8d3QNLRJ1oJTiEi26Sj6LQXS');
440
+ });
441
+ });
442
+
443
+ describe('hashes', function () {
444
+ it('should pass this test', function () {
445
+ // wif private key, address
446
+ // see: https://github.com/bitcoin/bitcoin/blob/master/src/test/key_tests.cpp#L20
447
+ var data = [
448
+ [
449
+ '5HxWvvfubhXpYYpS3tJkw6fq9jE9j18THftkZjHHfmFiWtmAbrj',
450
+ '1QFqqMUD55ZV3PJEJZtaKCsQmjLT6JkjvJ',
451
+ ],
452
+ [
453
+ '5KC4ejrDjv152FGwP386VD1i2NYc5KkfSMyv1nGy1VGDxGHqVY3',
454
+ '1F5y5E5FMc5YzdJtB9hLaUe43GDxEKXENJ',
455
+ ],
456
+ [
457
+ 'Kwr371tjA9u2rFSMZjTNun2PXXP3WPZu2afRHTcta6KxEUdm1vEw',
458
+ '1NoJrossxPBKfCHuJXT4HadJrXRE9Fxiqs',
459
+ ],
460
+ [
461
+ 'L3Hq7a8FEQwJkW1M2GNKDW28546Vp5miewcCzSqUD9kCAXrJdS3g',
462
+ '1CRj2HyM1CXWzHAXLQtiGLyggNT9WQqsDs',
463
+ ],
464
+ ];
465
+
466
+ data.forEach(function (d) {
467
+ var publicKey = PrivateKey.fromWIF(d[0]).toPublicKey();
468
+ var address = Address.fromString(d[1]);
469
+ address.hashBuffer.should.deep.equal(publicKey._getID());
470
+ });
471
+ });
472
+ });
473
+
474
+ describe('#toString', function () {
475
+ it('should print this known public key', function () {
476
+ var hex = '031ff0fe0f7b15ffaa85ff9f4744d539139c252a49710fb053bb9f2b933173ff9a';
477
+ var pk = PublicKey.fromString(hex);
478
+ pk.toString().should.equal(hex);
479
+ });
480
+ });
481
+
482
+ describe('#toHex', function () {
483
+ it('should print this known public key', function () {
484
+ var hex = '031ff0fe0f7b15ffaa85ff9f4744d539139c252a49710fb053bb9f2b933173ff9a';
485
+ var pk = PublicKey.fromHex(hex);
486
+ pk.toHex().should.equal(hex);
487
+ });
488
+ });
489
+
490
+ describe('#inspect', function () {
491
+ it('should output known uncompressed pubkey for console', function () {
492
+ var pubkey = PublicKey.fromString(
493
+ '041ff0fe0f7b15ffaa85ff9f4744d539139c252a49710fb053bb9f2b933173ff9a7baad41d04514751e6851f5304fd243751703bed21b914f6be218c0fa354a341',
494
+ );
495
+ pubkey
496
+ .inspect()
497
+ .should.equal(
498
+ '<PublicKey: 041ff0fe0f7b15ffaa85ff9f4744d539139c252a49710fb053bb9f2b933173ff9a7baad41d04514751e6851f5304fd243751703bed21b914f6be218c0fa354a341, uncompressed>',
499
+ );
500
+ });
501
+
502
+ it('should output known compressed pubkey for console', function () {
503
+ var pubkey = PublicKey.fromString(
504
+ '031ff0fe0f7b15ffaa85ff9f4744d539139c252a49710fb053bb9f2b933173ff9a',
505
+ );
506
+ pubkey
507
+ .inspect()
508
+ .should.equal(
509
+ '<PublicKey: 031ff0fe0f7b15ffaa85ff9f4744d539139c252a49710fb053bb9f2b933173ff9a>',
510
+ );
511
+ });
512
+
513
+ it('should output known compressed pubkey with network for console', function () {
514
+ var privkey = PrivateKey.fromWIF('L3T1s1TYP9oyhHpXgkyLoJFGniEgkv2Jhi138d7R2yJ9F4QdDU2m');
515
+ var pubkey = new PublicKey(privkey);
516
+ pubkey
517
+ .inspect()
518
+ .should.equal(
519
+ '<PublicKey: 03c87bd0e162f26969da8509cafcb7b8c8d202af30b928c582e263dd13ee9a9781>',
520
+ );
521
+ });
522
+ });
523
+
524
+ describe('#validate', function () {
525
+ it('should not have an error if pubkey is valid', function () {
526
+ var hex = '031ff0fe0f7b15ffaa85ff9f4744d539139c252a49710fb053bb9f2b933173ff9a';
527
+ expect(function () {
528
+ return PublicKey.fromString(hex);
529
+ }).to.not.throw();
530
+ });
531
+
532
+ it('should throw an error if pubkey is invalid', function () {
533
+ var hex =
534
+ '041ff0fe0f7b15ffaa85ff9f4744d539139c252a49710fb053bb9f2b933173ff9a0000000000000000000000000000000000000000000000000000000000000000';
535
+ (function () {
536
+ return PublicKey.fromString(hex);
537
+ }).should.throw('Invalid y value for curve.');
538
+ });
539
+
540
+ it('should throw an error if pubkey is invalid', function () {
541
+ var hex =
542
+ '041ff0fe0f7b15ffaa85ff9f4744d539139c252a49710fb053bb9f2b933173ff9a00000000000000000000000000000000000000000000000000000000000000FF';
543
+ (function () {
544
+ return PublicKey.fromString(hex);
545
+ }).should.throw('Invalid y value for curve.');
546
+ });
547
+
548
+ it('should throw an error if pubkey is infinity', function () {
549
+ (function () {
550
+ return new PublicKey(Point.getG().mul(Point.getN()));
551
+ }).should.throw('Point cannot be equal to Infinity');
552
+ });
553
+ });
554
+ });