@bsv/sdk 1.0.0 → 1.0.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.
- package/README.md +25 -3
- package/package.json +9 -5
- package/.github/ISSUE_TEMPLATE/bug_report.md +0 -40
- package/.github/ISSUE_TEMPLATE/discussion.md +0 -24
- package/.github/PULL_REQUEST_TEMPLATE/pull_request_template.md +0 -23
- package/CHANGELOG.md +0 -72
- package/CONTRIBUTING.md +0 -85
- package/ROADMAP.md +0 -3
- package/docs/getting-started/COMMONJS.md +0 -94
- package/docs/getting-started/REACT-TS.md +0 -131
- package/docs/getting-started/TS-NODE.md +0 -106
- package/docs/getting-started/VUE.md +0 -103
- package/jest.config.js +0 -6
- package/mod.ts +0 -8
- package/src/compat/BSM.ts +0 -51
- package/src/compat/ECIES.ts +0 -557
- package/src/compat/HD.ts +0 -348
- package/src/compat/Mnemonic.ts +0 -295
- package/src/compat/__tests/BSM.test.ts +0 -38
- package/src/compat/__tests/ECIES.test.ts +0 -90
- package/src/compat/__tests/HD.test.ts +0 -405
- package/src/compat/__tests/Mnemonic.test.ts +0 -177
- package/src/compat/__tests/Mnemonic.vectors.ts +0 -172
- package/src/compat/bip-39-wordlist-en.ts +0 -2053
- package/src/compat/index.ts +0 -4
- package/src/messages/EncryptedMessage.ts +0 -70
- package/src/messages/SignedMessage.ts +0 -87
- package/src/messages/__tests/EncryptedMessage.test.ts +0 -36
- package/src/messages/__tests/SignedMessage.test.ts +0 -53
- package/src/messages/index.ts +0 -2
- package/src/primitives/AESGCM.ts +0 -479
- package/src/primitives/BasePoint.ts +0 -21
- package/src/primitives/BigNumber.ts +0 -4619
- package/src/primitives/Curve.ts +0 -1163
- package/src/primitives/DRBG.ts +0 -102
- package/src/primitives/ECDSA.ts +0 -164
- package/src/primitives/Hash.ts +0 -1420
- package/src/primitives/JacobianPoint.ts +0 -410
- package/src/primitives/K256.ts +0 -116
- package/src/primitives/Mersenne.ts +0 -123
- package/src/primitives/MontgomoryMethod.ts +0 -160
- package/src/primitives/Point.ts +0 -852
- package/src/primitives/PrivateKey.ts +0 -195
- package/src/primitives/PublicKey.ts +0 -154
- package/src/primitives/Random.ts +0 -55
- package/src/primitives/ReductionContext.ts +0 -528
- package/src/primitives/Signature.ts +0 -235
- package/src/primitives/SymmetricKey.ts +0 -75
- package/src/primitives/TransactionSignature.ts +0 -189
- package/src/primitives/__tests/AESGCM.test.ts +0 -338
- package/src/primitives/__tests/BRC42.private.vectors.ts +0 -33
- package/src/primitives/__tests/BRC42.public.vectors.ts +0 -33
- package/src/primitives/__tests/BigNumber.arithmatic.test.ts +0 -572
- package/src/primitives/__tests/BigNumber.binary.test.ts +0 -203
- package/src/primitives/__tests/BigNumber.constructor.test.ts +0 -176
- package/src/primitives/__tests/BigNumber.dhGroup.test.ts +0 -18
- package/src/primitives/__tests/BigNumber.fixtures.ts +0 -264
- package/src/primitives/__tests/BigNumber.serializers.test.ts +0 -157
- package/src/primitives/__tests/BigNumber.utils.test.ts +0 -347
- package/src/primitives/__tests/Curve.unit.test.ts +0 -192
- package/src/primitives/__tests/DRBG.test.ts +0 -18
- package/src/primitives/__tests/DRBG.vectors.ts +0 -167
- package/src/primitives/__tests/ECDH.test.ts +0 -31
- package/src/primitives/__tests/ECDSA.test.ts +0 -58
- package/src/primitives/__tests/HMAC.test.ts +0 -59
- package/src/primitives/__tests/Hash.test.ts +0 -121
- package/src/primitives/__tests/PBKDF2.vectors.ts +0 -119
- package/src/primitives/__tests/PrivateKey.test.ts +0 -17
- package/src/primitives/__tests/PublicKey.test.ts +0 -66
- package/src/primitives/__tests/Random.test.ts +0 -14
- package/src/primitives/__tests/Reader.test.ts +0 -296
- package/src/primitives/__tests/ReductionContext.test.ts +0 -279
- package/src/primitives/__tests/SymmetricKey.test.ts +0 -58
- package/src/primitives/__tests/SymmetricKey.vectors.ts +0 -40
- package/src/primitives/__tests/Writer.test.ts +0 -198
- package/src/primitives/__tests/sighash.vectors.ts +0 -3503
- package/src/primitives/__tests/utils.test.ts +0 -108
- package/src/primitives/index.ts +0 -8
- package/src/primitives/utils.ts +0 -665
- package/src/script/LockingScript.ts +0 -30
- package/src/script/OP.ts +0 -219
- package/src/script/Script.ts +0 -426
- package/src/script/ScriptChunk.ts +0 -7
- package/src/script/ScriptTemplate.ts +0 -36
- package/src/script/Spend.ts +0 -1379
- package/src/script/UnlockingScript.ts +0 -30
- package/src/script/__tests/Script.test.ts +0 -369
- package/src/script/__tests/Spend.test.ts +0 -248
- package/src/script/__tests/script.invalid.vectors.ts +0 -925
- package/src/script/__tests/script.valid.vectors.ts +0 -1120
- package/src/script/__tests/scriptFromVector.ts +0 -42
- package/src/script/__tests/spend.valid.vectors.ts +0 -2288
- package/src/script/index.ts +0 -7
- package/src/script/templates/P2PKH.ts +0 -109
- package/src/script/templates/RPuzzle.ts +0 -140
- package/src/script/templates/index.ts +0 -2
- package/src/transaction/Broadcaster.ts +0 -42
- package/src/transaction/ChainTracker.ts +0 -22
- package/src/transaction/FeeModel.ts +0 -13
- package/src/transaction/MerklePath.ts +0 -259
- package/src/transaction/Transaction.ts +0 -602
- package/src/transaction/TransactionInput.ts +0 -63
- package/src/transaction/TransactionOutput.ts +0 -37
- package/src/transaction/__tests/MerklePath.test.ts +0 -181
- package/src/transaction/__tests/Transaction.test.ts +0 -413
- package/src/transaction/__tests/bigtx.vectors.ts +0 -4
- package/src/transaction/__tests/bump.invalid.vectors.ts +0 -8
- package/src/transaction/__tests/bump.valid.vectors.ts +0 -4
- package/src/transaction/__tests/tx.invalid.vectors.ts +0 -281
- package/src/transaction/__tests/tx.valid.vectors.ts +0 -364
- package/src/transaction/broadcasters/ARC.ts +0 -106
- package/src/transaction/broadcasters/__tests/ARC.test.ts +0 -115
- package/src/transaction/broadcasters/index.ts +0 -1
- package/src/transaction/fee-models/SatoshisPerKilobyte.ts +0 -71
- package/src/transaction/fee-models/index.ts +0 -1
- package/src/transaction/index.ts +0 -6
- package/ts2md.json +0 -5
- package/tsconfig.base.json +0 -26
- package/tsconfig.cjs.json +0 -11
- package/tsconfig.eslint.json +0 -12
- package/tsconfig.esm.json +0 -9
- package/tsconfig.json +0 -17
- package/tsconfig.types.json +0 -11
package/src/script/OP.ts
DELETED
|
@@ -1,219 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* An object mapping opcode names (such as OP_DUP) to their corresponding numbers (such as 0x76), and vice versa.
|
|
3
|
-
*/
|
|
4
|
-
const OP = {
|
|
5
|
-
// push value
|
|
6
|
-
OP_FALSE: 0x00,
|
|
7
|
-
OP_0: 0x00,
|
|
8
|
-
OP_PUSHDATA1: 0x4c,
|
|
9
|
-
OP_PUSHDATA2: 0x4d,
|
|
10
|
-
OP_PUSHDATA4: 0x4e,
|
|
11
|
-
OP_1NEGATE: 0x4f,
|
|
12
|
-
OP_RESERVED: 0x50,
|
|
13
|
-
OP_TRUE: 0x51,
|
|
14
|
-
OP_1: 0x51,
|
|
15
|
-
OP_2: 0x52,
|
|
16
|
-
OP_3: 0x53,
|
|
17
|
-
OP_4: 0x54,
|
|
18
|
-
OP_5: 0x55,
|
|
19
|
-
OP_6: 0x56,
|
|
20
|
-
OP_7: 0x57,
|
|
21
|
-
OP_8: 0x58,
|
|
22
|
-
OP_9: 0x59,
|
|
23
|
-
OP_10: 0x5a,
|
|
24
|
-
OP_11: 0x5b,
|
|
25
|
-
OP_12: 0x5c,
|
|
26
|
-
OP_13: 0x5d,
|
|
27
|
-
OP_14: 0x5e,
|
|
28
|
-
OP_15: 0x5f,
|
|
29
|
-
OP_16: 0x60,
|
|
30
|
-
|
|
31
|
-
// control
|
|
32
|
-
OP_NOP: 0x61,
|
|
33
|
-
OP_VER: 0x62,
|
|
34
|
-
OP_IF: 0x63,
|
|
35
|
-
OP_NOTIF: 0x64,
|
|
36
|
-
OP_VERIF: 0x65,
|
|
37
|
-
OP_VERNOTIF: 0x66,
|
|
38
|
-
OP_ELSE: 0x67,
|
|
39
|
-
OP_ENDIF: 0x68,
|
|
40
|
-
OP_VERIFY: 0x69,
|
|
41
|
-
OP_RETURN: 0x6a,
|
|
42
|
-
|
|
43
|
-
// stack ops
|
|
44
|
-
OP_TOALTSTACK: 0x6b,
|
|
45
|
-
OP_FROMALTSTACK: 0x6c,
|
|
46
|
-
OP_2DROP: 0x6d,
|
|
47
|
-
OP_2DUP: 0x6e,
|
|
48
|
-
OP_3DUP: 0x6f,
|
|
49
|
-
OP_2OVER: 0x70,
|
|
50
|
-
OP_2ROT: 0x71,
|
|
51
|
-
OP_2SWAP: 0x72,
|
|
52
|
-
OP_IFDUP: 0x73,
|
|
53
|
-
OP_DEPTH: 0x74,
|
|
54
|
-
OP_DROP: 0x75,
|
|
55
|
-
OP_DUP: 0x76,
|
|
56
|
-
OP_NIP: 0x77,
|
|
57
|
-
OP_OVER: 0x78,
|
|
58
|
-
OP_PICK: 0x79,
|
|
59
|
-
OP_ROLL: 0x7a,
|
|
60
|
-
OP_ROT: 0x7b,
|
|
61
|
-
OP_SWAP: 0x7c,
|
|
62
|
-
OP_TUCK: 0x7d,
|
|
63
|
-
|
|
64
|
-
// data manipulation ops
|
|
65
|
-
OP_CAT: 0x7e,
|
|
66
|
-
OP_SUBSTR: 0x7f, // Replaced in BSV
|
|
67
|
-
OP_SPLIT: 0x7f,
|
|
68
|
-
OP_LEFT: 0x80, // Replaced in BSV
|
|
69
|
-
OP_NUM2BIN: 0x80,
|
|
70
|
-
OP_RIGHT: 0x81, // Replaced in BSV
|
|
71
|
-
OP_BIN2NUM: 0x81,
|
|
72
|
-
OP_SIZE: 0x82,
|
|
73
|
-
|
|
74
|
-
// bit logic
|
|
75
|
-
OP_INVERT: 0x83,
|
|
76
|
-
OP_AND: 0x84,
|
|
77
|
-
OP_OR: 0x85,
|
|
78
|
-
OP_XOR: 0x86,
|
|
79
|
-
OP_EQUAL: 0x87,
|
|
80
|
-
OP_EQUALVERIFY: 0x88,
|
|
81
|
-
OP_RESERVED1: 0x89,
|
|
82
|
-
OP_RESERVED2: 0x8a,
|
|
83
|
-
|
|
84
|
-
// numeric
|
|
85
|
-
OP_1ADD: 0x8b,
|
|
86
|
-
OP_1SUB: 0x8c,
|
|
87
|
-
OP_2MUL: 0x8d,
|
|
88
|
-
OP_2DIV: 0x8e,
|
|
89
|
-
OP_NEGATE: 0x8f,
|
|
90
|
-
OP_ABS: 0x90,
|
|
91
|
-
OP_NOT: 0x91,
|
|
92
|
-
OP_0NOTEQUAL: 0x92,
|
|
93
|
-
|
|
94
|
-
OP_ADD: 0x93,
|
|
95
|
-
OP_SUB: 0x94,
|
|
96
|
-
OP_MUL: 0x95,
|
|
97
|
-
OP_DIV: 0x96,
|
|
98
|
-
OP_MOD: 0x97,
|
|
99
|
-
OP_LSHIFT: 0x98,
|
|
100
|
-
OP_RSHIFT: 0x99,
|
|
101
|
-
|
|
102
|
-
OP_BOOLAND: 0x9a,
|
|
103
|
-
OP_BOOLOR: 0x9b,
|
|
104
|
-
OP_NUMEQUAL: 0x9c,
|
|
105
|
-
OP_NUMEQUALVERIFY: 0x9d,
|
|
106
|
-
OP_NUMNOTEQUAL: 0x9e,
|
|
107
|
-
OP_LESSTHAN: 0x9f,
|
|
108
|
-
OP_GREATERTHAN: 0xa0,
|
|
109
|
-
OP_LESSTHANOREQUAL: 0xa1,
|
|
110
|
-
OP_GREATERTHANOREQUAL: 0xa2,
|
|
111
|
-
OP_MIN: 0xa3,
|
|
112
|
-
OP_MAX: 0xa4,
|
|
113
|
-
|
|
114
|
-
OP_WITHIN: 0xa5,
|
|
115
|
-
|
|
116
|
-
// crypto
|
|
117
|
-
OP_RIPEMD160: 0xa6,
|
|
118
|
-
OP_SHA1: 0xa7,
|
|
119
|
-
OP_SHA256: 0xa8,
|
|
120
|
-
OP_HASH160: 0xa9,
|
|
121
|
-
OP_HASH256: 0xaa,
|
|
122
|
-
OP_CODESEPARATOR: 0xab,
|
|
123
|
-
OP_CHECKSIG: 0xac,
|
|
124
|
-
OP_CHECKSIGVERIFY: 0xad,
|
|
125
|
-
OP_CHECKMULTISIG: 0xae,
|
|
126
|
-
OP_CHECKMULTISIGVERIFY: 0xaf,
|
|
127
|
-
|
|
128
|
-
// expansion
|
|
129
|
-
OP_NOP1: 0xb0,
|
|
130
|
-
OP_NOP2: 0xb1,
|
|
131
|
-
OP_NOP3: 0xb2,
|
|
132
|
-
OP_NOP4: 0xb3,
|
|
133
|
-
OP_NOP5: 0xb4,
|
|
134
|
-
OP_NOP6: 0xb5,
|
|
135
|
-
OP_NOP7: 0xb6,
|
|
136
|
-
OP_NOP8: 0xb7,
|
|
137
|
-
OP_NOP9: 0xb8,
|
|
138
|
-
OP_NOP10: 0xb9,
|
|
139
|
-
OP_NOP11: 0xba,
|
|
140
|
-
OP_NOP12: 0xbb,
|
|
141
|
-
OP_NOP13: 0xbc,
|
|
142
|
-
OP_NOP14: 0xbd,
|
|
143
|
-
OP_NOP15: 0xbe,
|
|
144
|
-
OP_NOP16: 0xbf,
|
|
145
|
-
OP_NOP17: 0xc0,
|
|
146
|
-
OP_NOP18: 0xc1,
|
|
147
|
-
OP_NOP19: 0xc2,
|
|
148
|
-
OP_NOP20: 0xc3,
|
|
149
|
-
OP_NOP21: 0xc4,
|
|
150
|
-
OP_NOP22: 0xc5,
|
|
151
|
-
OP_NOP23: 0xc6,
|
|
152
|
-
OP_NOP24: 0xc7,
|
|
153
|
-
OP_NOP25: 0xc8,
|
|
154
|
-
OP_NOP26: 0xc9,
|
|
155
|
-
OP_NOP27: 0xca,
|
|
156
|
-
OP_NOP28: 0xcb,
|
|
157
|
-
OP_NOP29: 0xcc,
|
|
158
|
-
OP_NOP30: 0xcd,
|
|
159
|
-
OP_NOP31: 0xce,
|
|
160
|
-
OP_NOP32: 0xcf,
|
|
161
|
-
OP_NOP33: 0xd0,
|
|
162
|
-
OP_NOP34: 0xd1,
|
|
163
|
-
OP_NOP35: 0xd2,
|
|
164
|
-
OP_NOP36: 0xd3,
|
|
165
|
-
OP_NOP37: 0xd4,
|
|
166
|
-
OP_NOP38: 0xd5,
|
|
167
|
-
OP_NOP39: 0xd6,
|
|
168
|
-
OP_NOP40: 0xd7,
|
|
169
|
-
OP_NOP41: 0xd8,
|
|
170
|
-
OP_NOP42: 0xd9,
|
|
171
|
-
OP_NOP43: 0xda,
|
|
172
|
-
OP_NOP44: 0xdb,
|
|
173
|
-
OP_NOP45: 0xdc,
|
|
174
|
-
OP_NOP46: 0xdd,
|
|
175
|
-
OP_NOP47: 0xde,
|
|
176
|
-
OP_NOP48: 0xdf,
|
|
177
|
-
OP_NOP49: 0xe0,
|
|
178
|
-
OP_NOP50: 0xe1,
|
|
179
|
-
OP_NOP51: 0xe2,
|
|
180
|
-
OP_NOP52: 0xe3,
|
|
181
|
-
OP_NOP53: 0xe4,
|
|
182
|
-
OP_NOP54: 0xe5,
|
|
183
|
-
OP_NOP55: 0xe6,
|
|
184
|
-
OP_NOP56: 0xe7,
|
|
185
|
-
OP_NOP57: 0xe8,
|
|
186
|
-
OP_NOP58: 0xe9,
|
|
187
|
-
OP_NOP59: 0xea,
|
|
188
|
-
OP_NOP60: 0xeb,
|
|
189
|
-
OP_NOP61: 0xec,
|
|
190
|
-
OP_NOP62: 0xed,
|
|
191
|
-
OP_NOP63: 0xee,
|
|
192
|
-
OP_NOP64: 0xef,
|
|
193
|
-
OP_NOP65: 0xf0,
|
|
194
|
-
OP_NOP66: 0xf1,
|
|
195
|
-
OP_NOP67: 0xf2,
|
|
196
|
-
OP_NOP68: 0xf3,
|
|
197
|
-
OP_NOP69: 0xf4,
|
|
198
|
-
OP_NOP70: 0xf5,
|
|
199
|
-
OP_NOP71: 0xf6,
|
|
200
|
-
OP_NOP72: 0xf7,
|
|
201
|
-
OP_NOP73: 0xf8,
|
|
202
|
-
OP_NOP77: 0xfc,
|
|
203
|
-
|
|
204
|
-
// template matching params
|
|
205
|
-
OP_SMALLDATA: 0xf9,
|
|
206
|
-
OP_SMALLINTEGER: 0xfa,
|
|
207
|
-
OP_PUBKEYS: 0xfb,
|
|
208
|
-
OP_PUBKEYHASH: 0xfd,
|
|
209
|
-
OP_PUBKEY: 0xfe,
|
|
210
|
-
|
|
211
|
-
OP_INVALIDOPCODE: 0xff
|
|
212
|
-
}
|
|
213
|
-
|
|
214
|
-
for (const name in OP) {
|
|
215
|
-
OP[OP[name]] = name
|
|
216
|
-
OP[String(OP[name])] = name
|
|
217
|
-
}
|
|
218
|
-
|
|
219
|
-
export default OP
|
package/src/script/Script.ts
DELETED
|
@@ -1,426 +0,0 @@
|
|
|
1
|
-
import ScriptChunk from './ScriptChunk.js'
|
|
2
|
-
import OP from './OP.js'
|
|
3
|
-
import { encode, toHex, Reader, Writer, toArray } from '../primitives/utils.js'
|
|
4
|
-
import BigNumber from '../primitives/BigNumber.js'
|
|
5
|
-
|
|
6
|
-
/**
|
|
7
|
-
* The Script class represents a script in a Bitcoin SV transaction,
|
|
8
|
-
* encapsulating the functionality to construct, parse, and serialize
|
|
9
|
-
* scripts used in both locking (output) and unlocking (input) scripts.
|
|
10
|
-
*
|
|
11
|
-
* @property {ScriptChunk[]} chunks - An array of script chunks that make up the script.
|
|
12
|
-
*/
|
|
13
|
-
export default class Script {
|
|
14
|
-
chunks: ScriptChunk[]
|
|
15
|
-
|
|
16
|
-
/**
|
|
17
|
-
* @method fromASM
|
|
18
|
-
* Static method to construct a Script instance from an ASM (Assembly) formatted string.
|
|
19
|
-
* @param asm - The script in ASM string format.
|
|
20
|
-
* @returns A new Script instance.
|
|
21
|
-
* @example
|
|
22
|
-
* const script = Script.fromASM("OP_DUP OP_HASH160 abcd... OP_EQUALVERIFY OP_CHECKSIG")
|
|
23
|
-
*/
|
|
24
|
-
static fromASM (asm: string): Script {
|
|
25
|
-
const chunks: ScriptChunk[] = []
|
|
26
|
-
const tokens = asm.split(' ')
|
|
27
|
-
let i = 0
|
|
28
|
-
while (i < tokens.length) {
|
|
29
|
-
const token = tokens[i]
|
|
30
|
-
let opCode
|
|
31
|
-
let opCodeNum: number
|
|
32
|
-
if (typeof OP[token] !== 'undefined') {
|
|
33
|
-
opCode = token
|
|
34
|
-
opCodeNum = OP[token]
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
// we start with two special cases, 0 and -1, which are handled specially in
|
|
38
|
-
// toASM. see _chunkToString.
|
|
39
|
-
if (token === '0') {
|
|
40
|
-
opCodeNum = 0
|
|
41
|
-
chunks.push({
|
|
42
|
-
op: opCodeNum
|
|
43
|
-
})
|
|
44
|
-
i = i + 1
|
|
45
|
-
} else if (token === '-1') {
|
|
46
|
-
opCodeNum = OP.OP_1NEGATE
|
|
47
|
-
chunks.push({
|
|
48
|
-
op: opCodeNum
|
|
49
|
-
})
|
|
50
|
-
i = i + 1
|
|
51
|
-
} else if (opCode === undefined) {
|
|
52
|
-
let hex = tokens[i]
|
|
53
|
-
if (hex.length % 2 !== 0) {
|
|
54
|
-
hex = '0' + hex
|
|
55
|
-
}
|
|
56
|
-
const arr = toArray(hex, 'hex')
|
|
57
|
-
if (encode(arr, 'hex') !== hex) {
|
|
58
|
-
throw new Error('invalid hex string in script')
|
|
59
|
-
}
|
|
60
|
-
const len = arr.length
|
|
61
|
-
if (len >= 0 && len < OP.OP_PUSHDATA1) {
|
|
62
|
-
opCodeNum = len
|
|
63
|
-
} else if (len < Math.pow(2, 8)) {
|
|
64
|
-
opCodeNum = OP.OP_PUSHDATA1
|
|
65
|
-
} else if (len < Math.pow(2, 16)) {
|
|
66
|
-
opCodeNum = OP.OP_PUSHDATA2
|
|
67
|
-
} else if (len < Math.pow(2, 32)) {
|
|
68
|
-
opCodeNum = OP.OP_PUSHDATA4
|
|
69
|
-
}
|
|
70
|
-
chunks.push({
|
|
71
|
-
data: arr,
|
|
72
|
-
op: opCodeNum
|
|
73
|
-
})
|
|
74
|
-
i = i + 1
|
|
75
|
-
} else if (
|
|
76
|
-
opCodeNum === OP.OP_PUSHDATA1 ||
|
|
77
|
-
opCodeNum === OP.OP_PUSHDATA2 ||
|
|
78
|
-
opCodeNum === OP.OP_PUSHDATA4
|
|
79
|
-
) {
|
|
80
|
-
chunks.push({
|
|
81
|
-
data: toArray(tokens[i + 2], 'hex'),
|
|
82
|
-
op: opCodeNum
|
|
83
|
-
})
|
|
84
|
-
i = i + 3
|
|
85
|
-
} else {
|
|
86
|
-
chunks.push({
|
|
87
|
-
op: opCodeNum
|
|
88
|
-
})
|
|
89
|
-
i = i + 1
|
|
90
|
-
}
|
|
91
|
-
}
|
|
92
|
-
return new Script(chunks)
|
|
93
|
-
}
|
|
94
|
-
|
|
95
|
-
/**
|
|
96
|
-
* @method fromHex
|
|
97
|
-
* Static method to construct a Script instance from a hexadecimal string.
|
|
98
|
-
* @param hex - The script in hexadecimal format.
|
|
99
|
-
* @returns A new Script instance.
|
|
100
|
-
* @example
|
|
101
|
-
* const script = Script.fromHex("76a9...");
|
|
102
|
-
*/
|
|
103
|
-
static fromHex (hex: string): Script {
|
|
104
|
-
return Script.fromBinary(toArray(hex, 'hex'))
|
|
105
|
-
}
|
|
106
|
-
|
|
107
|
-
/**
|
|
108
|
-
* @method fromBinary
|
|
109
|
-
* Static method to construct a Script instance from a binary array.
|
|
110
|
-
* @param bin - The script in binary array format.
|
|
111
|
-
* @returns A new Script instance.
|
|
112
|
-
* @example
|
|
113
|
-
* const script = Script.fromBinary([0x76, 0xa9, ...])
|
|
114
|
-
*/
|
|
115
|
-
static fromBinary (bin: number[]): Script {
|
|
116
|
-
bin = [...bin]
|
|
117
|
-
const chunks: ScriptChunk[] = []
|
|
118
|
-
|
|
119
|
-
const br = new Reader(bin)
|
|
120
|
-
while (!br.eof()) {
|
|
121
|
-
const op = br.readUInt8()
|
|
122
|
-
|
|
123
|
-
let len = 0
|
|
124
|
-
// eslint-disable-next-line @typescript-eslint/no-shadow
|
|
125
|
-
let data: number[] = []
|
|
126
|
-
if (op > 0 && op < OP.OP_PUSHDATA1) {
|
|
127
|
-
len = op
|
|
128
|
-
chunks.push({
|
|
129
|
-
data: br.read(len),
|
|
130
|
-
op
|
|
131
|
-
})
|
|
132
|
-
} else if (op === OP.OP_PUSHDATA1) {
|
|
133
|
-
try {
|
|
134
|
-
len = br.readUInt8()
|
|
135
|
-
data = br.read(len)
|
|
136
|
-
} catch (err) {
|
|
137
|
-
br.read()
|
|
138
|
-
}
|
|
139
|
-
chunks.push({
|
|
140
|
-
data,
|
|
141
|
-
op
|
|
142
|
-
})
|
|
143
|
-
} else if (op === OP.OP_PUSHDATA2) {
|
|
144
|
-
try {
|
|
145
|
-
len = br.readUInt16LE()
|
|
146
|
-
data = br.read(len)
|
|
147
|
-
} catch (err) {
|
|
148
|
-
br.read()
|
|
149
|
-
}
|
|
150
|
-
chunks.push({
|
|
151
|
-
data,
|
|
152
|
-
op
|
|
153
|
-
})
|
|
154
|
-
} else if (op === OP.OP_PUSHDATA4) {
|
|
155
|
-
try {
|
|
156
|
-
len = br.readUInt32LE()
|
|
157
|
-
data = br.read(len)
|
|
158
|
-
} catch (err) {
|
|
159
|
-
br.read()
|
|
160
|
-
}
|
|
161
|
-
chunks.push({
|
|
162
|
-
data,
|
|
163
|
-
op
|
|
164
|
-
})
|
|
165
|
-
} else {
|
|
166
|
-
chunks.push({
|
|
167
|
-
op
|
|
168
|
-
})
|
|
169
|
-
}
|
|
170
|
-
}
|
|
171
|
-
return new Script(chunks)
|
|
172
|
-
}
|
|
173
|
-
|
|
174
|
-
/**
|
|
175
|
-
* @constructor
|
|
176
|
-
* Constructs a new Script object.
|
|
177
|
-
* @param chunks=[] - An array of script chunks to directly initialize the script.
|
|
178
|
-
*/
|
|
179
|
-
constructor (chunks: ScriptChunk[] = []) {
|
|
180
|
-
this.chunks = chunks
|
|
181
|
-
}
|
|
182
|
-
|
|
183
|
-
/**
|
|
184
|
-
* @method toASM
|
|
185
|
-
* Serializes the script to an ASM formatted string.
|
|
186
|
-
* @returns The script in ASM string format.
|
|
187
|
-
*/
|
|
188
|
-
toASM (): string {
|
|
189
|
-
let str = ''
|
|
190
|
-
for (let i = 0; i < this.chunks.length; i++) {
|
|
191
|
-
const chunk = this.chunks[i]
|
|
192
|
-
str += this._chunkToString(chunk)
|
|
193
|
-
}
|
|
194
|
-
|
|
195
|
-
return str.slice(1)
|
|
196
|
-
}
|
|
197
|
-
|
|
198
|
-
/**
|
|
199
|
-
* @method toHex
|
|
200
|
-
* Serializes the script to a hexadecimal string.
|
|
201
|
-
* @returns The script in hexadecimal format.
|
|
202
|
-
*/
|
|
203
|
-
toHex (): string {
|
|
204
|
-
return encode(this.toBinary(), 'hex') as string
|
|
205
|
-
}
|
|
206
|
-
|
|
207
|
-
/**
|
|
208
|
-
* @method toBinary
|
|
209
|
-
* Serializes the script to a binary array.
|
|
210
|
-
* @returns The script in binary array format.
|
|
211
|
-
*/
|
|
212
|
-
toBinary (): number[] {
|
|
213
|
-
const writer = new Writer()
|
|
214
|
-
|
|
215
|
-
for (let i = 0; i < this.chunks.length; i++) {
|
|
216
|
-
const chunk = this.chunks[i]
|
|
217
|
-
const op = chunk.op
|
|
218
|
-
writer.writeUInt8(op)
|
|
219
|
-
if (chunk.data) {
|
|
220
|
-
if (op < OP.OP_PUSHDATA1) {
|
|
221
|
-
writer.write(chunk.data)
|
|
222
|
-
} else if (op === OP.OP_PUSHDATA1) {
|
|
223
|
-
writer.writeUInt8(chunk.data.length)
|
|
224
|
-
writer.write(chunk.data)
|
|
225
|
-
} else if (op === OP.OP_PUSHDATA2) {
|
|
226
|
-
writer.writeUInt16LE(chunk.data.length)
|
|
227
|
-
writer.write(chunk.data)
|
|
228
|
-
} else if (op === OP.OP_PUSHDATA4) {
|
|
229
|
-
writer.writeUInt32LE(chunk.data.length)
|
|
230
|
-
writer.write(chunk.data)
|
|
231
|
-
}
|
|
232
|
-
}
|
|
233
|
-
}
|
|
234
|
-
|
|
235
|
-
return writer.toArray()
|
|
236
|
-
}
|
|
237
|
-
|
|
238
|
-
/**
|
|
239
|
-
* @method writeScript
|
|
240
|
-
* Appends another script to this script.
|
|
241
|
-
* @param script - The script to append.
|
|
242
|
-
* @returns This script instance for chaining.
|
|
243
|
-
*/
|
|
244
|
-
writeScript (script: Script): Script {
|
|
245
|
-
this.chunks = this.chunks.concat(script.chunks)
|
|
246
|
-
return this
|
|
247
|
-
}
|
|
248
|
-
|
|
249
|
-
/**
|
|
250
|
-
* @method writeOpCode
|
|
251
|
-
* Appends an opcode to the script.
|
|
252
|
-
* @param op - The opcode to append.
|
|
253
|
-
* @returns This script instance for chaining.
|
|
254
|
-
*/
|
|
255
|
-
writeOpCode (op: number): Script {
|
|
256
|
-
this.chunks.push({ op })
|
|
257
|
-
return this
|
|
258
|
-
}
|
|
259
|
-
|
|
260
|
-
/**
|
|
261
|
-
* @method setChunkOpCode
|
|
262
|
-
* Sets the opcode of a specific chunk in the script.
|
|
263
|
-
* @param i - The index of the chunk.
|
|
264
|
-
* @param op - The opcode to set.
|
|
265
|
-
* @returns This script instance for chaining.
|
|
266
|
-
*/
|
|
267
|
-
setChunkOpCode (i: number, op: number): Script {
|
|
268
|
-
this.chunks[i] = { op }
|
|
269
|
-
return this
|
|
270
|
-
}
|
|
271
|
-
|
|
272
|
-
/**
|
|
273
|
-
* @method writeBn
|
|
274
|
-
* Appends a BigNumber to the script as an opcode.
|
|
275
|
-
* @param bn - The BigNumber to append.
|
|
276
|
-
* @returns This script instance for chaining.
|
|
277
|
-
*/
|
|
278
|
-
writeBn (bn: BigNumber): Script {
|
|
279
|
-
if (bn.cmpn(0) === OP.OP_0) {
|
|
280
|
-
this.chunks.push({
|
|
281
|
-
op: OP.OP_0
|
|
282
|
-
})
|
|
283
|
-
} else if (bn.cmpn(-1) === 0) {
|
|
284
|
-
this.chunks.push({
|
|
285
|
-
op: OP.OP_1NEGATE
|
|
286
|
-
})
|
|
287
|
-
} else if (bn.cmpn(1) >= 0 && bn.cmpn(16) <= 0) {
|
|
288
|
-
// see OP_1 - OP_16
|
|
289
|
-
this.chunks.push({
|
|
290
|
-
op: bn.toNumber() + OP.OP_1 - 1
|
|
291
|
-
})
|
|
292
|
-
} else {
|
|
293
|
-
const buf = bn.toSm('little')
|
|
294
|
-
this.writeBin(buf)
|
|
295
|
-
}
|
|
296
|
-
return this
|
|
297
|
-
}
|
|
298
|
-
|
|
299
|
-
/**
|
|
300
|
-
* @method writeBin
|
|
301
|
-
* Appends binary data to the script, determining the appropriate opcode based on length.
|
|
302
|
-
* @param bin - The binary data to append.
|
|
303
|
-
* @returns This script instance for chaining.
|
|
304
|
-
* @throws {Error} Throws an error if the data is too large to be pushed.
|
|
305
|
-
*/
|
|
306
|
-
writeBin (bin: number[]): Script {
|
|
307
|
-
let op
|
|
308
|
-
if (bin.length > 0 && bin.length < OP.OP_PUSHDATA1) {
|
|
309
|
-
op = bin.length
|
|
310
|
-
} else if (bin.length === 0) {
|
|
311
|
-
op = OP.OP_0
|
|
312
|
-
} else if (bin.length < Math.pow(2, 8)) {
|
|
313
|
-
op = OP.OP_PUSHDATA1
|
|
314
|
-
} else if (bin.length < Math.pow(2, 16)) {
|
|
315
|
-
op = OP.OP_PUSHDATA2
|
|
316
|
-
} else if (bin.length < Math.pow(2, 32)) {
|
|
317
|
-
op = OP.OP_PUSHDATA4
|
|
318
|
-
} else {
|
|
319
|
-
throw new Error("You can't push that much data")
|
|
320
|
-
}
|
|
321
|
-
this.chunks.push({
|
|
322
|
-
data: bin,
|
|
323
|
-
op
|
|
324
|
-
})
|
|
325
|
-
return this
|
|
326
|
-
}
|
|
327
|
-
|
|
328
|
-
/**
|
|
329
|
-
* @method writeNumber
|
|
330
|
-
* Appends a number to the script.
|
|
331
|
-
* @param num - The number to append.
|
|
332
|
-
* @returns This script instance for chaining.
|
|
333
|
-
*/
|
|
334
|
-
writeNumber (num: number): Script {
|
|
335
|
-
this.writeBn(new BigNumber(num))
|
|
336
|
-
return this
|
|
337
|
-
}
|
|
338
|
-
|
|
339
|
-
/**
|
|
340
|
-
* @method removeCodeseparators
|
|
341
|
-
* Removes all OP_CODESEPARATOR opcodes from the script.
|
|
342
|
-
* @returns This script instance for chaining.
|
|
343
|
-
*/
|
|
344
|
-
removeCodeseparators (): Script {
|
|
345
|
-
const chunks = []
|
|
346
|
-
for (let i = 0; i < this.chunks.length; i++) {
|
|
347
|
-
if (this.chunks[i].op !== OP.OP_CODESEPARATOR) {
|
|
348
|
-
chunks.push(this.chunks[i])
|
|
349
|
-
}
|
|
350
|
-
}
|
|
351
|
-
this.chunks = chunks
|
|
352
|
-
return this
|
|
353
|
-
}
|
|
354
|
-
|
|
355
|
-
/**
|
|
356
|
-
* Deletes the given item wherever it appears in the current script.
|
|
357
|
-
*
|
|
358
|
-
* @param script - The script containing the item to delete from the current script.
|
|
359
|
-
*
|
|
360
|
-
* @returns This script instance for chaining.
|
|
361
|
-
*/
|
|
362
|
-
findAndDelete (script: Script): Script {
|
|
363
|
-
const buf = script.toHex()
|
|
364
|
-
for (let i = 0; i < this.chunks.length; i++) {
|
|
365
|
-
const script2 = new Script([this.chunks[i]])
|
|
366
|
-
const buf2 = script2.toHex()
|
|
367
|
-
if (buf === buf2) {
|
|
368
|
-
this.chunks.splice(i, 1)
|
|
369
|
-
}
|
|
370
|
-
}
|
|
371
|
-
return this
|
|
372
|
-
}
|
|
373
|
-
|
|
374
|
-
/**
|
|
375
|
-
* @method isPushOnly
|
|
376
|
-
* Checks if the script contains only push data operations.
|
|
377
|
-
* @returns True if the script is push-only, otherwise false.
|
|
378
|
-
*/
|
|
379
|
-
isPushOnly (): boolean {
|
|
380
|
-
for (let i = 0; i < this.chunks.length; i++) {
|
|
381
|
-
const chunk = this.chunks[i]
|
|
382
|
-
const opCodeNum = chunk.op
|
|
383
|
-
if (opCodeNum > OP.OP_16) {
|
|
384
|
-
return false
|
|
385
|
-
}
|
|
386
|
-
}
|
|
387
|
-
return true
|
|
388
|
-
}
|
|
389
|
-
|
|
390
|
-
/**
|
|
391
|
-
* @method isLockingScript
|
|
392
|
-
* Determines if the script is a locking script.
|
|
393
|
-
* @returns True if the script is a locking script, otherwise false.
|
|
394
|
-
*/
|
|
395
|
-
isLockingScript (): boolean {
|
|
396
|
-
throw new Error('Not implemented')
|
|
397
|
-
}
|
|
398
|
-
|
|
399
|
-
/**
|
|
400
|
-
* @method isUnlockingScript
|
|
401
|
-
* Determines if the script is an unlocking script.
|
|
402
|
-
* @returns True if the script is an unlocking script, otherwise false.
|
|
403
|
-
*/
|
|
404
|
-
isUnlockingScript (): boolean {
|
|
405
|
-
throw new Error('Not implemented')
|
|
406
|
-
}
|
|
407
|
-
|
|
408
|
-
/**
|
|
409
|
-
* @private
|
|
410
|
-
* @method _chunkToString
|
|
411
|
-
* Converts a script chunk to its string representation.
|
|
412
|
-
* @param chunk - The script chunk.
|
|
413
|
-
* @returns The string representation of the chunk.
|
|
414
|
-
*/
|
|
415
|
-
private _chunkToString (chunk: ScriptChunk): string {
|
|
416
|
-
const op = chunk.op
|
|
417
|
-
let str = ''
|
|
418
|
-
if (typeof chunk.data === 'undefined') {
|
|
419
|
-
const val = (OP[op] as string)
|
|
420
|
-
str = `${str} ${val}`
|
|
421
|
-
} else {
|
|
422
|
-
str = `${str} ${toHex(chunk.data)}`
|
|
423
|
-
}
|
|
424
|
-
return str
|
|
425
|
-
}
|
|
426
|
-
}
|
|
@@ -1,36 +0,0 @@
|
|
|
1
|
-
import LockingScript from './LockingScript.js'
|
|
2
|
-
import UnlockingScript from './UnlockingScript.js'
|
|
3
|
-
import Transaction from '../transaction/Transaction.js'
|
|
4
|
-
|
|
5
|
-
/**
|
|
6
|
-
* @interface
|
|
7
|
-
*
|
|
8
|
-
* This interface defines the structure for script templates used in Bitcoin transactions.
|
|
9
|
-
* It provides methods for creating locking scripts, unlocking scripts, and estimating the length of unlocking scripts.
|
|
10
|
-
*/
|
|
11
|
-
export default interface ScriptTemplate {
|
|
12
|
-
/**
|
|
13
|
-
* Creates a locking script with the given parameters.
|
|
14
|
-
*
|
|
15
|
-
* @param {...any} params - The parameters required to create the locking script.
|
|
16
|
-
* @returns {LockingScript} - An instance of LockingScript.
|
|
17
|
-
*/
|
|
18
|
-
lock: (...params: any) => LockingScript
|
|
19
|
-
|
|
20
|
-
/**
|
|
21
|
-
* Creates a function that generates an unlocking script along with its signature and length estimation.
|
|
22
|
-
*
|
|
23
|
-
* This method returns an object containing two functions:
|
|
24
|
-
* 1. `sign` - A function that, when called with a transaction and an input index, returns an UnlockingScript instance.
|
|
25
|
-
* 2. `estimateLength` - A function that returns the estimated length of the unlocking script in bytes.
|
|
26
|
-
*
|
|
27
|
-
* @param {...any} params - The parameters required to create the unlocking script.
|
|
28
|
-
* @returns {Object} - An object containing the `sign` and `estimateLength` functions.
|
|
29
|
-
*/
|
|
30
|
-
unlock: (...params: any) =>
|
|
31
|
-
{
|
|
32
|
-
sign: (tx: Transaction, inputIndex: number) =>
|
|
33
|
-
Promise<UnlockingScript>
|
|
34
|
-
estimateLength: (tx: Transaction, inputIndex: number) => Promise<number>
|
|
35
|
-
}
|
|
36
|
-
}
|