bsv2 2.0.24 → 2.1.1

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 (170) hide show
  1. package/dist/bsv2.mjs +36061 -4
  2. package/package.json +35 -39
  3. package/.babelrc.test +0 -10
  4. package/.editorconfig +0 -4
  5. package/.env.defaults +0 -1
  6. package/.env.schema +0 -1
  7. package/.husky/pre-commit +0 -1
  8. package/.mocharc.yml +0 -4
  9. package/.nvmrc +0 -1
  10. package/.travis.yml +0 -7
  11. package/benchmark.js +0 -50
  12. package/bin/bsv.js +0 -11
  13. package/bin/network.js +0 -92
  14. package/build/index.html +0 -20
  15. package/dist/bsv2.bundle.js +0 -8
  16. package/dist/bsv2.cjs.js +0 -2
  17. package/dist/bsv2.cjs.js.LICENSE.txt +0 -1
  18. package/dist/bsv2.module.js +0 -2
  19. package/entry.js +0 -82
  20. package/index.js +0 -102
  21. package/karma.conf.js +0 -78
  22. package/lib/ach.js +0 -59
  23. package/lib/address.js +0 -264
  24. package/lib/aes.js +0 -57
  25. package/lib/aescbc.js +0 -38
  26. package/lib/base-58-check.js +0 -83
  27. package/lib/base-58.js +0 -59
  28. package/lib/bip-32.js +0 -472
  29. package/lib/bip-39-en-wordlist.js +0 -2054
  30. package/lib/bip-39-en.js +0 -10
  31. package/lib/bip-39-es-wordlist.js +0 -2055
  32. package/lib/bip-39-es.js +0 -10
  33. package/lib/bip-39-jp-wordlist.js +0 -2053
  34. package/lib/bip-39-jp.js +0 -10
  35. package/lib/bip-39-words.js +0 -5
  36. package/lib/bip-39.js +0 -285
  37. package/lib/block-header.js +0 -79
  38. package/lib/block.js +0 -132
  39. package/lib/bn.js +0 -467
  40. package/lib/br.js +0 -172
  41. package/lib/bsm.js +0 -127
  42. package/lib/bw.js +0 -188
  43. package/lib/cbc.js +0 -140
  44. package/lib/cmp.js +0 -37
  45. package/lib/config.js +0 -50
  46. package/lib/constants.js +0 -150
  47. package/lib/ecdsa.js +0 -495
  48. package/lib/ecies.js +0 -151
  49. package/lib/hash-cache.js +0 -41
  50. package/lib/hash.js +0 -191
  51. package/lib/interp.js +0 -2013
  52. package/lib/inv.js +0 -49
  53. package/lib/key-pair.js +0 -133
  54. package/lib/mainnet-address.js +0 -5
  55. package/lib/mainnet-bip-32.js +0 -5
  56. package/lib/mainnet-key-pair.js +0 -5
  57. package/lib/mainnet-priv-key.js +0 -5
  58. package/lib/merkle.js +0 -92
  59. package/lib/msg.js +0 -184
  60. package/lib/op-code.js +0 -398
  61. package/lib/point.js +0 -134
  62. package/lib/priv-key.js +0 -157
  63. package/lib/pub-key.js +0 -230
  64. package/lib/random.js +0 -28
  65. package/lib/script.js +0 -826
  66. package/lib/sig-operations.js +0 -120
  67. package/lib/sig.js +0 -399
  68. package/lib/struct.js +0 -389
  69. package/lib/testnet-address.js +0 -5
  70. package/lib/testnet-bip-32.js +0 -5
  71. package/lib/testnet-key-pair.js +0 -5
  72. package/lib/testnet-priv-key.js +0 -5
  73. package/lib/tx-builder.js +0 -593
  74. package/lib/tx-in.js +0 -172
  75. package/lib/tx-out-map.js +0 -57
  76. package/lib/tx-out.js +0 -73
  77. package/lib/tx-verifier.js +0 -209
  78. package/lib/tx.js +0 -430
  79. package/lib/var-int.js +0 -73
  80. package/lib/version.js +0 -92
  81. package/lib/workers-result.js +0 -65
  82. package/lib/workers.js +0 -77
  83. package/load-babel.js +0 -3
  84. package/mtest/test.html +0 -15
  85. package/mtest/test.js +0 -2
  86. package/mtest/test.mjs +0 -2
  87. package/test/ach.js +0 -172
  88. package/test/address.js +0 -464
  89. package/test/aes.js +0 -96
  90. package/test/aescbc.js +0 -65
  91. package/test/base-58-check.js +0 -128
  92. package/test/base-58.js +0 -113
  93. package/test/bip-32.js +0 -721
  94. package/test/bip-39.js +0 -282
  95. package/test/block-header.js +0 -152
  96. package/test/block.js +0 -243
  97. package/test/bn.js +0 -643
  98. package/test/br.js +0 -353
  99. package/test/bsm.js +0 -141
  100. package/test/bw.js +0 -292
  101. package/test/cbc.js +0 -335
  102. package/test/cmp.js +0 -56
  103. package/test/data/bip69.json +0 -215
  104. package/test/data/bitcoind/base58_keys_invalid.json +0 -152
  105. package/test/data/bitcoind/base58_keys_valid.json +0 -452
  106. package/test/data/bitcoind/blocks.json +0 -27
  107. package/test/data/bitcoind/script_tests.json +0 -2244
  108. package/test/data/bitcoind/sig_canonical.json +0 -7
  109. package/test/data/bitcoind/sig_noncanonical.json +0 -22
  110. package/test/data/bitcoind/tx_invalid.json +0 -177
  111. package/test/data/bitcoind/tx_valid.json +0 -224
  112. package/test/data/blk86756-testnet.dat +0 -0
  113. package/test/data/blk86756-testnet.js +0 -12
  114. package/test/data/blk86756-testnet.json +0 -684
  115. package/test/data/ecdsa.json +0 -230
  116. package/test/data/messages.json +0 -22
  117. package/test/data/sighash.json +0 -1004
  118. package/test/data/tx_creation.json +0 -85
  119. package/test/ecdsa.js +0 -748
  120. package/test/ecies.js +0 -238
  121. package/test/global.js +0 -13
  122. package/test/hash-cache.js +0 -52
  123. package/test/hash.js +0 -354
  124. package/test/index.js +0 -10
  125. package/test/interp.js +0 -512
  126. package/test/inv.js +0 -65
  127. package/test/key-pair.js +0 -243
  128. package/test/merkle.js +0 -179
  129. package/test/msg.js +0 -295
  130. package/test/op-code.js +0 -79
  131. package/test/point.js +0 -375
  132. package/test/priv-key.js +0 -264
  133. package/test/pub-key.js +0 -549
  134. package/test/random.js +0 -31
  135. package/test/script.js +0 -1340
  136. package/test/sig-operations.js +0 -149
  137. package/test/sig.js +0 -662
  138. package/test/struct.js +0 -552
  139. package/test/tx-builder.js +0 -2104
  140. package/test/tx-in.js +0 -198
  141. package/test/tx-out-map.js +0 -104
  142. package/test/tx-out.js +0 -162
  143. package/test/tx-verifier.js +0 -246
  144. package/test/tx.js +0 -539
  145. package/test/var-int.js +0 -132
  146. package/test/vectors/aes.json +0 -10392
  147. package/test/vectors/aescbc.json +0 -620
  148. package/test/vectors/bip39.json +0 -502
  149. package/test/vectors/bip69.json +0 -215
  150. package/test/vectors/bitcoin-abc/script_tests.json +0 -48
  151. package/test/vectors/bitcoin-abc/sighash.json +0 -7004
  152. package/test/vectors/bitcoin-sv/script_tests.json +0 -248
  153. package/test/vectors/bitcoind/script_invalid.json +0 -1151
  154. package/test/vectors/bitcoind/script_valid.json +0 -1462
  155. package/test/vectors/bitcoind/sighash.json +0 -3503
  156. package/test/vectors/bitcoind/tx_invalid.json +0 -883
  157. package/test/vectors/bitcoind/tx_valid.json +0 -1150
  158. package/test/vectors/coolest-tx-ever-sent.json +0 -8
  159. package/test/vectors/ecdsa.json +0 -300
  160. package/test/vectors/hash.json +0 -6133
  161. package/test/vectors/kdf.json +0 -47
  162. package/test/vectors/largesttx.json +0 -5
  163. package/test/vectors/largesttxblock.json +0 -6
  164. package/test/vectors/sig.json +0 -79
  165. package/test/vectors/sighash-single-bug.json +0 -8
  166. package/test/version.js +0 -81
  167. package/test/workers-result.js +0 -89
  168. package/test/workers.js +0 -91
  169. package/test-index.js +0 -3
  170. package/webpack.config.js +0 -115
package/lib/op-code.js DELETED
@@ -1,398 +0,0 @@
1
- /*
2
- * OpCode
3
- * ======
4
- *
5
- * An opCode is one of the operations in the bitcoin scripting language. Each
6
- * operation is just a number from 0-255, and it has a corresponding string,
7
- * e.g. "OP_RETURN", which comes from the name of that constant in the bitcoind
8
- * source code. The way you probably want to use this is with
9
- * new OpCode(str).toNumber() or new OpCode(num).toString()
10
- */
11
- 'use strict'
12
-
13
- import { Struct } from './struct'
14
- const noopmap = {
15
- // push value
16
- "FALSE": 0x00,
17
- "0": 0x00,
18
- "PUSHDATA1": 0x4c,
19
- "PUSHDATA2": 0x4d,
20
- "PUSHDATA4": 0x4e,
21
- "1NEGATE": 0x4f,
22
- "RESERVED": 0x50,
23
- "TRUE": 0x51,
24
- "1": 0x51,
25
- "2": 0x52,
26
- "3": 0x53,
27
- "4": 0x54,
28
- "5": 0x55,
29
- "6": 0x56,
30
- "7": 0x57,
31
- "8": 0x58,
32
- "9": 0x59,
33
- "10": 0x5a,
34
- "11": 0x5b,
35
- "12": 0x5c,
36
- "13": 0x5d,
37
- "14": 0x5e,
38
- "15": 0x5f,
39
- "16": 0x60,
40
-
41
- // control
42
- "NOP": 0x61,
43
- "VER": 0x62,
44
- "IF": 0x63,
45
- "NOTIF": 0x64,
46
- "VERIF": 0x65,
47
- "VERNOTIF": 0x66,
48
- "ELSE": 0x67,
49
- "ENDIF": 0x68,
50
- "VERIFY": 0x69,
51
- "RETURN": 0x6a,
52
-
53
- // stack ops
54
- "TOALTSTACK": 0x6b,
55
- "FROMALTSTACK": 0x6c,
56
- "2DROP": 0x6d,
57
- "2DUP": 0x6e,
58
- "3DUP": 0x6f,
59
- "2OVER": 0x70,
60
- "2ROT": 0x71,
61
- "2SWAP": 0x72,
62
- "IFDUP": 0x73,
63
- "DEPTH": 0x74,
64
- "DROP": 0x75,
65
- "DUP": 0x76,
66
- "NIP": 0x77,
67
- "OVER": 0x78,
68
- "PICK": 0x79,
69
- "ROLL": 0x7a,
70
- "ROT": 0x7b,
71
- "SWAP": 0x7c,
72
- "TUCK": 0x7d,
73
-
74
- // data manipulation ops
75
- "CAT": 0x7e,
76
- "SUBSTR": 0x7f, // Replaced in BSV
77
- "SPLIT": 0x7f,
78
- "LEFT": 0x80, // Replaced in BSV
79
- "NUM2BIN": 0x80,
80
- "RIGHT": 0x81, // Replaced in BSV
81
- "BIN2NUM": 0x81,
82
- "SIZE": 0x82,
83
-
84
- // bit logic
85
- "INVERT": 0x83,
86
- "AND": 0x84,
87
- "OR": 0x85,
88
- "XOR": 0x86,
89
- "EQUAL": 0x87,
90
- "EQUALVERIFY": 0x88,
91
- "RESERVED1": 0x89,
92
- "RESERVED2": 0x8a,
93
-
94
- // numeric
95
- "1ADD": 0x8b,
96
- "1SUB": 0x8c,
97
- "2MUL": 0x8d,
98
- "2DIV": 0x8e,
99
- "NEGATE": 0x8f,
100
- "ABS": 0x90,
101
- "NOT": 0x91,
102
- "0NOTEQUAL": 0x92,
103
-
104
- "ADD": 0x93,
105
- "SUB": 0x94,
106
- "MUL": 0x95,
107
- "DIV": 0x96,
108
- "MOD": 0x97,
109
- "LSHIFT": 0x98,
110
- "RSHIFT": 0x99,
111
-
112
- "BOOLAND": 0x9a,
113
- "BOOLOR": 0x9b,
114
- "NUMEQUAL": 0x9c,
115
- "NUMEQUALVERIFY": 0x9d,
116
- "NUMNOTEQUAL": 0x9e,
117
- "LESSTHAN": 0x9f,
118
- "GREATERTHAN": 0xa0,
119
- "LESSTHANOREQUAL": 0xa1,
120
- "GREATERTHANOREQUAL": 0xa2,
121
- "MIN": 0xa3,
122
- "MAX": 0xa4,
123
-
124
- "WITHIN": 0xa5,
125
-
126
- // crypto
127
- "RIPEMD160": 0xa6,
128
- "SHA1": 0xa7,
129
- "SHA256": 0xa8,
130
- "HASH160": 0xa9,
131
- "HASH256": 0xaa,
132
- "CODESEPARATOR": 0xab,
133
- "CHECKSIG": 0xac,
134
- "CHECKSIGVERIFY": 0xad,
135
- "CHECKMULTISIG": 0xae,
136
- "CHECKMULTISIGVERIFY": 0xaf,
137
-
138
- // expansion
139
- "NOP1": 0xb0,
140
- "NOP2": 0xb1,
141
- "CHECKLOCKTIMEVERIFY": 0xb1,
142
- "NOP3": 0xb2,
143
- "CHECKSEQUENCEVERIFY": 0xb2,
144
- "NOP4": 0xb3,
145
- "NOP5": 0xb4,
146
- "NOP6": 0xb5,
147
- "NOP7": 0xb6,
148
- "NOP8": 0xb7,
149
- "NOP9": 0xb8,
150
- "NOP10": 0xb9,
151
-
152
- // template matching params
153
- "SMALLDATA": 0xf9,
154
- "SMALLINTEGER": 0xfa,
155
- "PUBKEYS": 0xfb,
156
- "PUBKEYHASH": 0xfd,
157
- "PUBKEY": 0xfe,
158
-
159
- "INVALIDOPCODE": 0xff
160
- }
161
-
162
- const map = {
163
- // push value
164
- OP_FALSE: 0x00,
165
- OP_0: 0x00,
166
- OP_PUSHDATA1: 0x4c,
167
- OP_PUSHDATA2: 0x4d,
168
- OP_PUSHDATA4: 0x4e,
169
- OP_1NEGATE: 0x4f,
170
- OP_RESERVED: 0x50,
171
- OP_TRUE: 0x51,
172
- OP_1: 0x51,
173
- OP_2: 0x52,
174
- OP_3: 0x53,
175
- OP_4: 0x54,
176
- OP_5: 0x55,
177
- OP_6: 0x56,
178
- OP_7: 0x57,
179
- OP_8: 0x58,
180
- OP_9: 0x59,
181
- OP_10: 0x5a,
182
- OP_11: 0x5b,
183
- OP_12: 0x5c,
184
- OP_13: 0x5d,
185
- OP_14: 0x5e,
186
- OP_15: 0x5f,
187
- OP_16: 0x60,
188
-
189
- // control
190
- OP_NOP: 0x61,
191
- OP_VER: 0x62,
192
- OP_IF: 0x63,
193
- OP_NOTIF: 0x64,
194
- OP_VERIF: 0x65,
195
- OP_VERNOTIF: 0x66,
196
- OP_ELSE: 0x67,
197
- OP_ENDIF: 0x68,
198
- OP_VERIFY: 0x69,
199
- OP_RETURN: 0x6a,
200
-
201
- // stack ops
202
- OP_TOALTSTACK: 0x6b,
203
- OP_FROMALTSTACK: 0x6c,
204
- OP_2DROP: 0x6d,
205
- OP_2DUP: 0x6e,
206
- OP_3DUP: 0x6f,
207
- OP_2OVER: 0x70,
208
- OP_2ROT: 0x71,
209
- OP_2SWAP: 0x72,
210
- OP_IFDUP: 0x73,
211
- OP_DEPTH: 0x74,
212
- OP_DROP: 0x75,
213
- OP_DUP: 0x76,
214
- OP_NIP: 0x77,
215
- OP_OVER: 0x78,
216
- OP_PICK: 0x79,
217
- OP_ROLL: 0x7a,
218
- OP_ROT: 0x7b,
219
- OP_SWAP: 0x7c,
220
- OP_TUCK: 0x7d,
221
-
222
- // data manipulation ops
223
- OP_CAT: 0x7e,
224
- OP_SUBSTR: 0x7f, // Replaced in BSV
225
- OP_SPLIT: 0x7f,
226
- OP_LEFT: 0x80, // Replaced in BSV
227
- OP_NUM2BIN: 0x80,
228
- OP_RIGHT: 0x81, // Replaced in BSV
229
- OP_BIN2NUM: 0x81,
230
- OP_SIZE: 0x82,
231
-
232
- // bit logic
233
- OP_INVERT: 0x83,
234
- OP_AND: 0x84,
235
- OP_OR: 0x85,
236
- OP_XOR: 0x86,
237
- OP_EQUAL: 0x87,
238
- OP_EQUALVERIFY: 0x88,
239
- OP_RESERVED1: 0x89,
240
- OP_RESERVED2: 0x8a,
241
-
242
- // numeric
243
- OP_1ADD: 0x8b,
244
- OP_1SUB: 0x8c,
245
- OP_2MUL: 0x8d,
246
- OP_2DIV: 0x8e,
247
- OP_NEGATE: 0x8f,
248
- OP_ABS: 0x90,
249
- OP_NOT: 0x91,
250
- OP_0NOTEQUAL: 0x92,
251
-
252
- OP_ADD: 0x93,
253
- OP_SUB: 0x94,
254
- OP_MUL: 0x95,
255
- OP_DIV: 0x96,
256
- OP_MOD: 0x97,
257
- OP_LSHIFT: 0x98,
258
- OP_RSHIFT: 0x99,
259
-
260
- OP_BOOLAND: 0x9a,
261
- OP_BOOLOR: 0x9b,
262
- OP_NUMEQUAL: 0x9c,
263
- OP_NUMEQUALVERIFY: 0x9d,
264
- OP_NUMNOTEQUAL: 0x9e,
265
- OP_LESSTHAN: 0x9f,
266
- OP_GREATERTHAN: 0xa0,
267
- OP_LESSTHANOREQUAL: 0xa1,
268
- OP_GREATERTHANOREQUAL: 0xa2,
269
- OP_MIN: 0xa3,
270
- OP_MAX: 0xa4,
271
-
272
- OP_WITHIN: 0xa5,
273
-
274
- // crypto
275
- OP_RIPEMD160: 0xa6,
276
- OP_SHA1: 0xa7,
277
- OP_SHA256: 0xa8,
278
- OP_HASH160: 0xa9,
279
- OP_HASH256: 0xaa,
280
- OP_CODESEPARATOR: 0xab,
281
- OP_CHECKSIG: 0xac,
282
- OP_CHECKSIGVERIFY: 0xad,
283
- OP_CHECKMULTISIG: 0xae,
284
- OP_CHECKMULTISIGVERIFY: 0xaf,
285
-
286
- // expansion
287
- OP_NOP1: 0xb0,
288
- OP_NOP2: 0xb1,
289
- OP_CHECKLOCKTIMEVERIFY: 0xb1,
290
- OP_NOP3: 0xb2,
291
- OP_CHECKSEQUENCEVERIFY: 0xb2,
292
- OP_NOP4: 0xb3,
293
- OP_NOP5: 0xb4,
294
- OP_NOP6: 0xb5,
295
- OP_NOP7: 0xb6,
296
- OP_NOP8: 0xb7,
297
- OP_NOP9: 0xb8,
298
- OP_NOP10: 0xb9,
299
-
300
- // template matching params
301
- OP_SMALLDATA: 0xf9,
302
- OP_SMALLINTEGER: 0xfa,
303
- OP_PUBKEYS: 0xfb,
304
- OP_PUBKEYHASH: 0xfd,
305
- OP_PUBKEY: 0xfe,
306
-
307
- OP_INVALIDOPCODE: 0xff
308
- }
309
-
310
- class OpCode extends Struct {
311
- constructor (num) {
312
- super({ num })
313
- }
314
-
315
- fromNumber (num) {
316
- this.num = num
317
- return this
318
- }
319
-
320
- static fromNumber (num) {
321
- return new this().fromNumber(num)
322
- }
323
-
324
- toNumber () {
325
- return this.num
326
- }
327
-
328
- fromString (str) {
329
- const num = map[str]
330
- if (num === undefined) {
331
- throw new Error('Invalid opCodeStr')
332
- }
333
- this.num = num
334
- return this
335
- }
336
- fromNoOpString (str) {
337
- const num = noopmap[str]
338
- if (num === undefined) {
339
- throw new Error('Invalid opCodeStr')
340
- }
341
- this.num = num
342
- return this
343
- }
344
-
345
- static fromString (str) {
346
- return new this().fromString(str)
347
- }
348
- static fromNoOpString (str) {
349
- return new this().fromNoOpString(str)
350
- }
351
-
352
- toString () {
353
- const str = OpCode.str[this.num]
354
- if (str === undefined) {
355
- if (this.num > 0 && this.num < OpCode.OP_PUSHDATA1) {
356
- return this.num.toString()
357
- }else{
358
- return "OP_"+this.num.toNumber()
359
- }
360
- // throw new Error('OpCode does not have a string representation')
361
- }
362
- return str
363
- }
364
- toNoOpString () {
365
- const str = OpCode.noopstr[this.num]
366
- if (str === undefined) {
367
- if (this.num > 0 && this.num < OpCode.OP_PUSHDATA1) {
368
- return this.num.toString()
369
- }else{
370
- return this.num.toNumber()
371
- }
372
- // throw new Error('OpCode does not have a string representation')
373
- }
374
- return str
375
- }
376
- }
377
-
378
- OpCode.str = {}
379
- OpCode.noopstr = {}
380
-
381
- for (const opCodeStr in map) {
382
- OpCode[opCodeStr] = map[opCodeStr]
383
-
384
- if (Object.prototype.hasOwnProperty.call(map, opCodeStr)) {
385
- OpCode.str[map[opCodeStr]] = opCodeStr
386
- }
387
- }
388
-
389
- for (const CodeStr in noopmap) {
390
- OpCode[CodeStr] = noopmap[CodeStr]
391
-
392
- if (Object.prototype.hasOwnProperty.call(noopmap, CodeStr)) {
393
- OpCode.noopstr[noopmap[CodeStr]] = CodeStr
394
- }
395
- }
396
-
397
-
398
- export { OpCode }
package/lib/point.js DELETED
@@ -1,134 +0,0 @@
1
- /**
2
- * Point (on secp256k1)
3
- * ====================
4
- *
5
- * A point is a point on the secp256k1 curve which is the elliptic curve used
6
- * by bitcoin. This code is a wrapper for Fedor Indutny's Point class from his
7
- * elliptic library. This code adds a few minor conveniences, but is mostly the
8
- * same. Since Fedor's code returns points and big numbers that are instances
9
- * of his point and big number classes, we have to wrap all the methods such as
10
- * getX() to return the Yours Bitcoin point and big number types.
11
- */
12
- 'use strict'
13
-
14
- import { Bn } from './bn'
15
- import elliptic from 'bitcoin-elliptic'
16
-
17
- const ec = elliptic.curves.secp256k1
18
- const _point = ec.curve.point()
19
- const _Point = _point.constructor
20
-
21
- class Point extends _Point {
22
- constructor (x, y, isRed) {
23
- super(ec.curve, x, y, isRed)
24
- }
25
-
26
- static fromX (isOdd, x) {
27
- const _point = ec.curve.pointFromX(x, isOdd)
28
- const point = Object.create(Point.prototype)
29
- return point.copyFrom(_point)
30
- }
31
-
32
- copyFrom (point) {
33
- if (!(point instanceof _Point)) {
34
- throw new Error('point should be an external point')
35
- }
36
- Object.keys(point).forEach(
37
- function (key) {
38
- this[key] = point[key]
39
- }.bind(this)
40
- )
41
- return this
42
- }
43
-
44
- add (p) {
45
- p = _Point.prototype.add.call(this, p)
46
- const point = Object.create(Point.prototype)
47
- return point.copyFrom(p)
48
- }
49
-
50
- mul (bn) {
51
- if (!bn.lt(Point.getN())) {
52
- throw new Error('point mul out of range')
53
- }
54
- const p = _Point.prototype.mul.call(this, bn)
55
- const point = Object.create(Point.prototype)
56
- return point.copyFrom(p)
57
- }
58
-
59
- mulAdd (bn1, point, bn2) {
60
- const p = _Point.prototype.mulAdd.call(this, bn1, point, bn2)
61
- point = Object.create(Point.prototype)
62
- return point.copyFrom(p)
63
- }
64
-
65
- getX () {
66
- const _x = _Point.prototype.getX.call(this)
67
- const x = Object.create(Bn.prototype)
68
- _x.copy(x)
69
- return x
70
- }
71
-
72
- getY () {
73
- const _y = _Point.prototype.getY.call(this)
74
- const y = Object.create(Bn.prototype)
75
- _y.copy(y)
76
- return y
77
- }
78
-
79
- fromX (isOdd, x) {
80
- const point = Point.fromX(isOdd, x)
81
- return this.copyFrom(point)
82
- }
83
-
84
- toJSON () {
85
- return {
86
- x: this.getX().toString(),
87
- y: this.getY().toString()
88
- }
89
- }
90
-
91
- fromJSON (json) {
92
- const x = new Bn().fromString(json.x)
93
- const y = new Bn().fromString(json.y)
94
- const point = new Point(x, y)
95
- return this.copyFrom(point)
96
- }
97
-
98
- toString () {
99
- return JSON.stringify(this.toJSON())
100
- }
101
-
102
- fromString (str) {
103
- const json = JSON.parse(str)
104
- const p = new Point().fromJSON(json)
105
- return this.copyFrom(p)
106
- }
107
-
108
- static getG () {
109
- const _g = ec.curve.g
110
- const g = Object.create(Point.prototype)
111
- return g.copyFrom(_g)
112
- }
113
-
114
- static getN () {
115
- return new Bn(ec.curve.n.toArray())
116
- }
117
-
118
- // https://www.iacr.org/archive/pkc2003/25670211/25670211.pdf
119
- validate () {
120
- const p2 = Point.fromX(this.getY().isOdd(), this.getX())
121
- if (!(p2.getY().cmp(this.getY()) === 0)) {
122
- throw new Error('Invalid y value of public key')
123
- }
124
- if (
125
- !(this.getX().gt(-1) && this.getX().lt(Point.getN())) ||
126
- !(this.getY().gt(-1) && this.getY().lt(Point.getN()))
127
- ) {
128
- throw new Error('Point does not lie on the curve')
129
- }
130
- return this
131
- }
132
- }
133
-
134
- export { Point }
package/lib/priv-key.js DELETED
@@ -1,157 +0,0 @@
1
- /**
2
- * Private Key
3
- * ===========
4
- *
5
- * A private key is used for signing transactions (or messages). The primary
6
- * way to use this is new PrivKey().fromRandom(), or new PrivKey().fromBuffer(buf).
7
- */
8
- 'use strict'
9
-
10
- import { Bn } from './bn'
11
- import { Point } from './point'
12
- import { Constants } from './constants'
13
- import { Base58Check } from './base-58-check'
14
- import { Random } from './random'
15
- import { Struct } from './struct'
16
-
17
- class PrivKey extends Struct {
18
- constructor (bn, compressed, constants = null) {
19
- super({ bn, compressed })
20
- constants = constants || Constants.Default.PrivKey
21
- this.Constants = constants
22
- }
23
-
24
- fromJSON (json) {
25
- this.fromHex(json)
26
- return this
27
- }
28
-
29
- toJSON () {
30
- return this.toHex()
31
- }
32
-
33
- fromRandom () {
34
- let privBuf, bn, condition
35
-
36
- do {
37
- privBuf = Random.getRandomBuffer(32)
38
- bn = new Bn().fromBuffer(privBuf)
39
- condition = bn.lt(Point.getN())
40
- } while (!condition)
41
-
42
- this.fromObject({
43
- bn: bn,
44
- compressed: true
45
- })
46
- return this
47
- }
48
-
49
- static fromRandom () {
50
- return new this().fromRandom()
51
- }
52
-
53
- toBuffer () {
54
- let compressed = this.compressed
55
-
56
- if (compressed === undefined) {
57
- compressed = true
58
- }
59
-
60
- const privBuf = this.bn.toBuffer({ size: 32 })
61
- let buf
62
- if (compressed) {
63
- buf = Buffer.concat([
64
- Buffer.from([this.Constants.versionByteNum]),
65
- privBuf,
66
- Buffer.from([0x01])
67
- ])
68
- } else {
69
- buf = Buffer.concat([Buffer.from([this.Constants.versionByteNum]), privBuf])
70
- }
71
-
72
- return buf
73
- }
74
-
75
- fromBuffer (buf) {
76
- if (buf.length === 1 + 32 + 1 && buf[1 + 32 + 1 - 1] === 1) {
77
- this.compressed = true
78
- } else if (buf.length === 1 + 32) {
79
- this.compressed = false
80
- } else {
81
- throw new Error(
82
- 'Length of privKey buffer must be 33 (uncompressed pubKey) or 34 (compressed pubKey)'
83
- )
84
- }
85
-
86
- if (buf[0] !== this.Constants.versionByteNum) {
87
- throw new Error('Invalid versionByteNum byte')
88
- }
89
-
90
- return this.fromBn(new Bn().fromBuffer(buf.slice(1, 1 + 32)))
91
- }
92
-
93
- toBn () {
94
- return this.bn
95
- }
96
-
97
- fromBn (bn) {
98
- this.bn = bn
99
- return this
100
- }
101
-
102
- static fromBn (bn) {
103
- return new this().fromBn(bn)
104
- }
105
-
106
- validate () {
107
- if (!this.bn.lt(Point.getN())) {
108
- throw new Error('Number must be less than N')
109
- }
110
- if (typeof this.compressed !== 'boolean') {
111
- throw new Error(
112
- 'Must specify whether the corresponding public key is compressed or not (true or false)'
113
- )
114
- }
115
- return this
116
- }
117
-
118
- /**
119
- * Output the private key a Wallet Import Format (Wif) string.
120
- */
121
- toWif () {
122
- return Base58Check.encode(this.toBuffer())
123
- }
124
-
125
- /**
126
- * Input the private key from a Wallet Import Format (Wif) string.
127
- */
128
- fromWif (str) {
129
- return this.fromBuffer(Base58Check.decode(str))
130
- }
131
-
132
- static fromWif (str) {
133
- return new this().fromWif(str)
134
- }
135
-
136
- toString () {
137
- return this.toWif()
138
- }
139
-
140
- fromString (str) {
141
- return this.fromWif(str)
142
- }
143
- }
144
-
145
- PrivKey.Mainnet = class extends PrivKey {
146
- constructor (bn, compressed) {
147
- super(bn, compressed, Constants.Mainnet.PrivKey)
148
- }
149
- }
150
-
151
- PrivKey.Testnet = class extends PrivKey {
152
- constructor (bn, compressed) {
153
- super(bn, compressed, Constants.Testnet.PrivKey)
154
- }
155
- }
156
-
157
- export { PrivKey }