bsv2 2.0.13
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.
- package/.babelrc.test +10 -0
- package/.editorconfig +4 -0
- package/.env.defaults +1 -0
- package/.env.schema +1 -0
- package/.mocharc.yml +4 -0
- package/.nvmrc +1 -0
- package/.travis.yml +7 -0
- package/LICENSE +39 -0
- package/README.md +53 -0
- package/benchmark.js +50 -0
- package/bin/bsv.js +11 -0
- package/bin/network.js +92 -0
- package/build/bsv.js +3 -0
- package/build/index.html +20 -0
- package/dist/bsv.bundle.js +3 -0
- package/dist/bsv.bundle.js.LICENSE.txt +10 -0
- package/dist/bsv.bundle.js.map +1 -0
- package/dist/bsv.cjs.js +3 -0
- package/dist/bsv.cjs.js.LICENSE.txt +1 -0
- package/dist/bsv.cjs.js.map +1 -0
- package/dist/bsv.min.js +2 -0
- package/dist/bsv.module.js +3 -0
- package/dist/bsv.module.js.LICENSE.txt +1 -0
- package/dist/bsv.module.js.map +1 -0
- package/doc/assets/anchor.js +350 -0
- package/doc/assets/bass-addons.css +12 -0
- package/doc/assets/bass.css +544 -0
- package/doc/assets/fonts/EOT/SourceCodePro-Bold.eot +0 -0
- package/doc/assets/fonts/EOT/SourceCodePro-Regular.eot +0 -0
- package/doc/assets/fonts/LICENSE.txt +93 -0
- package/doc/assets/fonts/OTF/SourceCodePro-Bold.otf +0 -0
- package/doc/assets/fonts/OTF/SourceCodePro-Regular.otf +0 -0
- package/doc/assets/fonts/TTF/SourceCodePro-Bold.ttf +0 -0
- package/doc/assets/fonts/TTF/SourceCodePro-Regular.ttf +0 -0
- package/doc/assets/fonts/WOFF/OTF/SourceCodePro-Bold.otf.woff +0 -0
- package/doc/assets/fonts/WOFF/OTF/SourceCodePro-Regular.otf.woff +0 -0
- package/doc/assets/fonts/WOFF/TTF/SourceCodePro-Bold.ttf.woff +0 -0
- package/doc/assets/fonts/WOFF/TTF/SourceCodePro-Regular.ttf.woff +0 -0
- package/doc/assets/fonts/WOFF2/OTF/SourceCodePro-Bold.otf.woff2 +0 -0
- package/doc/assets/fonts/WOFF2/OTF/SourceCodePro-Regular.otf.woff2 +0 -0
- package/doc/assets/fonts/WOFF2/TTF/SourceCodePro-Bold.ttf.woff2 +0 -0
- package/doc/assets/fonts/WOFF2/TTF/SourceCodePro-Regular.ttf.woff2 +0 -0
- package/doc/assets/fonts/source-code-pro.css +23 -0
- package/doc/assets/github.css +123 -0
- package/doc/assets/site.js +168 -0
- package/doc/assets/split.css +15 -0
- package/doc/assets/split.js +784 -0
- package/doc/assets/style.css +147 -0
- package/doc/index.html +9387 -0
- package/docs/block.html +36 -0
- package/docs/ecies.html +81 -0
- package/docs/index.html +84 -0
- package/docs/networks.html +43 -0
- package/docs/script.html +101 -0
- package/docs/transaction.html +150 -0
- package/docs/unspentoutput.html +32 -0
- package/entry.js +82 -0
- package/index.js +102 -0
- package/jsdoc.json +5 -0
- package/karma.conf.js +78 -0
- package/lib/ach.js +59 -0
- package/lib/address.js +264 -0
- package/lib/aes.js +57 -0
- package/lib/aescbc.js +38 -0
- package/lib/base-58-check.js +83 -0
- package/lib/base-58.js +59 -0
- package/lib/bip-32.js +472 -0
- package/lib/bip-39-en-wordlist.js +2054 -0
- package/lib/bip-39-en.js +10 -0
- package/lib/bip-39-es-wordlist.js +2055 -0
- package/lib/bip-39-es.js +10 -0
- package/lib/bip-39-jp-wordlist.js +2053 -0
- package/lib/bip-39-jp.js +10 -0
- package/lib/bip-39-words.js +5 -0
- package/lib/bip-39.js +275 -0
- package/lib/block-header.js +79 -0
- package/lib/block.js +132 -0
- package/lib/bn.js +467 -0
- package/lib/br.js +172 -0
- package/lib/bsm.js +127 -0
- package/lib/bw.js +188 -0
- package/lib/cbc.js +140 -0
- package/lib/cmp.js +37 -0
- package/lib/config.js +50 -0
- package/lib/constants.js +150 -0
- package/lib/ecdsa.js +495 -0
- package/lib/ecies.js +151 -0
- package/lib/hash-cache.js +41 -0
- package/lib/hash.js +191 -0
- package/lib/interp.js +1853 -0
- package/lib/inv.js +49 -0
- package/lib/key-pair.js +133 -0
- package/lib/mainnet-address.js +5 -0
- package/lib/mainnet-bip-32.js +5 -0
- package/lib/mainnet-key-pair.js +5 -0
- package/lib/mainnet-priv-key.js +5 -0
- package/lib/merkle.js +92 -0
- package/lib/msg.js +184 -0
- package/lib/op-code.js +398 -0
- package/lib/point.js +134 -0
- package/lib/priv-key.js +157 -0
- package/lib/pub-key.js +230 -0
- package/lib/rabin.js +300 -0
- package/lib/random.js +28 -0
- package/lib/script.js +826 -0
- package/lib/sig-operations.js +120 -0
- package/lib/sig.js +399 -0
- package/lib/struct.js +389 -0
- package/lib/testnet-address.js +5 -0
- package/lib/testnet-bip-32.js +5 -0
- package/lib/testnet-key-pair.js +5 -0
- package/lib/testnet-priv-key.js +5 -0
- package/lib/tx-builder.js +593 -0
- package/lib/tx-in.js +172 -0
- package/lib/tx-out-map.js +57 -0
- package/lib/tx-out.js +73 -0
- package/lib/tx-verifier.js +209 -0
- package/lib/tx.js +429 -0
- package/lib/var-int.js +73 -0
- package/lib/version.js +92 -0
- package/lib/workers-result.js +65 -0
- package/lib/workers.js +77 -0
- package/load-babel.js +3 -0
- package/mkdocs.yml +37 -0
- package/package.json +138 -0
- package/test/ach.js +172 -0
- package/test/address.js +464 -0
- package/test/aes.js +96 -0
- package/test/aescbc.js +65 -0
- package/test/base-58-check.js +128 -0
- package/test/base-58.js +113 -0
- package/test/bip-32.js +721 -0
- package/test/bip-39.js +282 -0
- package/test/block-header.js +152 -0
- package/test/block.js +243 -0
- package/test/bn.js +643 -0
- package/test/br.js +353 -0
- package/test/bsm.js +141 -0
- package/test/bw.js +292 -0
- package/test/cbc.js +335 -0
- package/test/cmp.js +56 -0
- package/test/data/bip69.json +215 -0
- package/test/data/bitcoind/base58_keys_invalid.json +152 -0
- package/test/data/bitcoind/base58_keys_valid.json +452 -0
- package/test/data/bitcoind/blocks.json +27 -0
- package/test/data/bitcoind/script_tests.json +2244 -0
- package/test/data/bitcoind/sig_canonical.json +7 -0
- package/test/data/bitcoind/sig_noncanonical.json +22 -0
- package/test/data/bitcoind/tx_invalid.json +177 -0
- package/test/data/bitcoind/tx_valid.json +224 -0
- package/test/data/blk86756-testnet.dat +0 -0
- package/test/data/blk86756-testnet.js +12 -0
- package/test/data/blk86756-testnet.json +684 -0
- package/test/data/ecdsa.json +230 -0
- package/test/data/messages.json +22 -0
- package/test/data/sighash.json +1004 -0
- package/test/data/tx_creation.json +85 -0
- package/test/ecdsa.js +748 -0
- package/test/ecies.js +238 -0
- package/test/global.js +13 -0
- package/test/hash-cache.js +52 -0
- package/test/hash.js +354 -0
- package/test/index.js +10 -0
- package/test/interp.js +512 -0
- package/test/inv.js +65 -0
- package/test/key-pair.js +243 -0
- package/test/merkle.js +179 -0
- package/test/msg.js +295 -0
- package/test/op-code.js +79 -0
- package/test/point.js +375 -0
- package/test/priv-key.js +264 -0
- package/test/pub-key.js +549 -0
- package/test/random.js +31 -0
- package/test/script.js +1340 -0
- package/test/sig-operations.js +149 -0
- package/test/sig.js +662 -0
- package/test/struct.js +552 -0
- package/test/tx-builder.js +2104 -0
- package/test/tx-in.js +198 -0
- package/test/tx-out-map.js +104 -0
- package/test/tx-out.js +162 -0
- package/test/tx-verifier.js +246 -0
- package/test/tx.js +539 -0
- package/test/var-int.js +132 -0
- package/test/vectors/aes.json +10392 -0
- package/test/vectors/aescbc.json +620 -0
- package/test/vectors/bip39.json +502 -0
- package/test/vectors/bip69.json +215 -0
- package/test/vectors/bitcoin-abc/script_tests.json +48 -0
- package/test/vectors/bitcoin-abc/sighash.json +7004 -0
- package/test/vectors/bitcoin-sv/script_tests.json +248 -0
- package/test/vectors/bitcoind/script_invalid.json +1151 -0
- package/test/vectors/bitcoind/script_valid.json +1462 -0
- package/test/vectors/bitcoind/sighash.json +3503 -0
- package/test/vectors/bitcoind/tx_invalid.json +883 -0
- package/test/vectors/bitcoind/tx_valid.json +1150 -0
- package/test/vectors/coolest-tx-ever-sent.json +8 -0
- package/test/vectors/ecdsa.json +300 -0
- package/test/vectors/hash.json +6133 -0
- package/test/vectors/kdf.json +47 -0
- package/test/vectors/largesttx.json +5 -0
- package/test/vectors/largesttxblock.json +6 -0
- package/test/vectors/sig.json +79 -0
- package/test/vectors/sighash-single-bug.json +8 -0
- package/test/version.js +81 -0
- package/test/workers-result.js +89 -0
- package/test/workers.js +91 -0
- package/test-index.js +3 -0
- package/test1.html +88 -0
- package/webpack.config.js +60 -0
- package/webpack.config.js_entry +57 -0
- package/yarn-error.log +6199 -0
package/lib/op-code.js
ADDED
|
@@ -0,0 +1,398 @@
|
|
|
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
ADDED
|
@@ -0,0 +1,134 @@
|
|
|
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
ADDED
|
@@ -0,0 +1,157 @@
|
|
|
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 }
|