@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.
- package/.mocharc.yaml +3 -0
- package/index.d.ts +1541 -0
- package/index.js +74 -0
- package/lib/address.js +478 -0
- package/lib/block/block.js +277 -0
- package/lib/block/blockheader.js +295 -0
- package/lib/block/index.js +4 -0
- package/lib/block/merkleblock.js +323 -0
- package/lib/bn.js +3423 -0
- package/lib/crypto/bn.js +278 -0
- package/lib/crypto/ecdsa.js +339 -0
- package/lib/crypto/hash.browser.js +171 -0
- package/lib/crypto/hash.js +2 -0
- package/lib/crypto/hash.node.js +171 -0
- package/lib/crypto/point.js +221 -0
- package/lib/crypto/random.browser.js +28 -0
- package/lib/crypto/random.js +2 -0
- package/lib/crypto/random.node.js +11 -0
- package/lib/crypto/signature.js +325 -0
- package/lib/encoding/base58.js +111 -0
- package/lib/encoding/base58check.js +121 -0
- package/lib/encoding/bufferreader.js +212 -0
- package/lib/encoding/bufferwriter.js +140 -0
- package/lib/encoding/decode-asm.js +24 -0
- package/lib/encoding/decode-hex.js +32 -0
- package/lib/encoding/decode-script-chunks.js +43 -0
- package/lib/encoding/encode-hex.js +284 -0
- package/lib/encoding/is-hex.js +7 -0
- package/lib/encoding/varint.js +75 -0
- package/lib/errors/index.js +54 -0
- package/lib/errors/spec.js +314 -0
- package/lib/hash-cache.js +50 -0
- package/lib/hdprivatekey.js +678 -0
- package/lib/hdpublickey.js +525 -0
- package/lib/message/message.js +191 -0
- package/lib/mnemonic/mnemonic.js +303 -0
- package/lib/mnemonic/pbkdf2.browser.js +68 -0
- package/lib/mnemonic/pbkdf2.js +2 -0
- package/lib/mnemonic/pbkdf2.node.js +68 -0
- package/lib/mnemonic/words/chinese.js +2054 -0
- package/lib/mnemonic/words/english.js +2054 -0
- package/lib/mnemonic/words/french.js +2054 -0
- package/lib/mnemonic/words/index.js +8 -0
- package/lib/mnemonic/words/italian.js +2054 -0
- package/lib/mnemonic/words/japanese.js +2054 -0
- package/lib/mnemonic/words/spanish.js +2054 -0
- package/lib/networks.js +379 -0
- package/lib/opcode.js +255 -0
- package/lib/privatekey.js +374 -0
- package/lib/publickey.js +386 -0
- package/lib/script/index.js +5 -0
- package/lib/script/interpreter.js +1834 -0
- package/lib/script/script.js +1074 -0
- package/lib/script/stack.js +109 -0
- package/lib/script/write-i32-le.js +17 -0
- package/lib/script/write-push-data.js +35 -0
- package/lib/script/write-u16-le.js +12 -0
- package/lib/script/write-u32-le.js +16 -0
- package/lib/script/write-u64-le.js +24 -0
- package/lib/script/write-u8-le.js +8 -0
- package/lib/script/write-varint.js +46 -0
- package/lib/transaction/index.js +7 -0
- package/lib/transaction/input/index.js +5 -0
- package/lib/transaction/input/input.js +354 -0
- package/lib/transaction/input/multisig.js +242 -0
- package/lib/transaction/input/publickey.js +100 -0
- package/lib/transaction/input/publickeyhash.js +118 -0
- package/lib/transaction/output.js +231 -0
- package/lib/transaction/sighash.js +167 -0
- package/lib/transaction/signature.js +97 -0
- package/lib/transaction/transaction.js +1639 -0
- package/lib/transaction/unspentoutput.js +113 -0
- package/lib/util/_.js +47 -0
- package/lib/util/js.js +90 -0
- package/lib/util/preconditions.js +33 -0
- package/package.json +26 -0
- package/test/address.js +509 -0
- package/test/block/block.js +251 -0
- package/test/block/blockheader.js +275 -0
- package/test/block/merklebloack.js +211 -0
- package/test/crypto/bn.js +177 -0
- package/test/crypto/ecdsa.js +391 -0
- package/test/crypto/hash.browser.js +135 -0
- package/test/crypto/hash.js +136 -0
- package/test/crypto/point.js +224 -0
- package/test/crypto/random.js +32 -0
- package/test/crypto/signature.js +409 -0
- package/test/data/bip69.json +215 -0
- package/test/data/bitcoind/base58_keys_invalid.json +52 -0
- package/test/data/bitcoind/base58_keys_valid.json +335 -0
- package/test/data/bitcoind/blocks.json +22 -0
- package/test/data/bitcoind/script_tests.json +3822 -0
- package/test/data/bitcoind/sig_canonical.json +7 -0
- package/test/data/bitcoind/sig_noncanonical.json +36 -0
- package/test/data/bitcoind/tx_invalid.json +445 -0
- package/test/data/bitcoind/tx_valid.json +44 -0
- package/test/data/blk86756-testnet.dat +0 -0
- package/test/data/blk86756-testnet.js +14 -0
- package/test/data/blk86756-testnet.json +684 -0
- package/test/data/block.hex +1 -0
- package/test/data/ecdsa.json +230 -0
- package/test/data/merkleblocks.js +488 -0
- package/test/data/messages.json +22 -0
- package/test/data/sighash.json +12 -0
- package/test/data/tx_creation.json +95 -0
- package/test/encoding/base58.js +131 -0
- package/test/encoding/base58check.js +136 -0
- package/test/encoding/bufferreader.js +337 -0
- package/test/encoding/bufferwriter.js +172 -0
- package/test/encoding/varint.js +104 -0
- package/test/hashCache.js +67 -0
- package/test/hdkeys.js +445 -0
- package/test/hdprivatekey.js +332 -0
- package/test/hdpublickey.js +304 -0
- package/test/index.js +16 -0
- package/test/message/message.js +204 -0
- package/test/mnemonic/data/fixtures.json +300 -0
- package/test/mnemonic/mnemonic.js +259 -0
- package/test/mnemonic/mocha.opts +1 -0
- package/test/mnemonic/pbkdf2.test.js +59 -0
- package/test/networks.js +159 -0
- package/test/opcode.js +161 -0
- package/test/privatekey.js +439 -0
- package/test/publickey.js +554 -0
- package/test/script/interpreter.js +734 -0
- package/test/script/script.js +1437 -0
- package/test/transaction/deserialize.js +34 -0
- package/test/transaction/input/input.js +90 -0
- package/test/transaction/input/multisig.js +90 -0
- package/test/transaction/input/publickey.js +68 -0
- package/test/transaction/input/publickeyhash.js +51 -0
- package/test/transaction/output.js +185 -0
- package/test/transaction/sighash.js +65 -0
- package/test/transaction/signature.js +114 -0
- package/test/transaction/transaction.js +1109 -0
- package/test/transaction/unspentoutput.js +110 -0
- package/test/util/js.js +76 -0
- package/test/util/preconditions.js +79 -0
|
@@ -0,0 +1,1074 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var Address = require('../address');
|
|
4
|
+
var BufferWriter = require('../encoding/bufferwriter');
|
|
5
|
+
var Hash = require('../crypto/hash');
|
|
6
|
+
var Opcode = require('../opcode');
|
|
7
|
+
var PublicKey = require('../publickey');
|
|
8
|
+
var Signature = require('../crypto/signature');
|
|
9
|
+
var Networks = require('../networks');
|
|
10
|
+
var $ = require('../util/preconditions');
|
|
11
|
+
var _ = require('../util/_');
|
|
12
|
+
var errors = require('../errors');
|
|
13
|
+
var JSUtil = require('../util/js');
|
|
14
|
+
const decodeScriptChunks = require('../encoding/decode-script-chunks');
|
|
15
|
+
const decodeASM = require('../encoding/decode-asm');
|
|
16
|
+
const encodeHex = require('../encoding/encode-hex');
|
|
17
|
+
|
|
18
|
+
// These WeakMap caches allow the objects themselves to maintain their immutability
|
|
19
|
+
const SCRIPT_TO_CHUNKS_CACHE = new WeakMap();
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* A bitcoin transaction script. Each transaction's inputs and outputs
|
|
23
|
+
* has a script that is evaluated to validate it's spending.
|
|
24
|
+
*
|
|
25
|
+
* See https://en.bitcoin.it/wiki/Script
|
|
26
|
+
*
|
|
27
|
+
* @constructor
|
|
28
|
+
* @param {Object|string|Buffer=} from optional data to populate script
|
|
29
|
+
*/
|
|
30
|
+
var Script = function Script(from) {
|
|
31
|
+
if (!(this instanceof Script)) {
|
|
32
|
+
return new Script(from);
|
|
33
|
+
}
|
|
34
|
+
this.buffer = Buffer.from([]);
|
|
35
|
+
|
|
36
|
+
if (Buffer.isBuffer(from)) {
|
|
37
|
+
return Script.fromBuffer(from);
|
|
38
|
+
} else if (from instanceof Address) {
|
|
39
|
+
return Script.fromAddress(from);
|
|
40
|
+
} else if (from instanceof Script) {
|
|
41
|
+
return Script.fromBuffer(from.toBuffer());
|
|
42
|
+
} else if (_.isString(from)) {
|
|
43
|
+
return Script.fromString(from);
|
|
44
|
+
} else if (_.isObject(from) && _.isArray(from.chunks)) {
|
|
45
|
+
return Script.fromChunks(from.chunks);
|
|
46
|
+
} else if (_.isObject(from) && Buffer.isBuffer(from.buffer)) {
|
|
47
|
+
return Script.fromBuffer(from.buffer);
|
|
48
|
+
}
|
|
49
|
+
};
|
|
50
|
+
|
|
51
|
+
Script.prototype.set = function (obj) {
|
|
52
|
+
$.checkArgument(_.isObject(obj));
|
|
53
|
+
if (obj.chunks && _.isArray(obj.chunks)) {
|
|
54
|
+
var s = Script.fromChunks(obj.chunks);
|
|
55
|
+
this.buffer = s.buffer;
|
|
56
|
+
return this;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
$.checkArgument(Buffer.isBuffer(obj.buffer));
|
|
60
|
+
this.buffer = obj.buffer;
|
|
61
|
+
return this;
|
|
62
|
+
};
|
|
63
|
+
|
|
64
|
+
Script.fromBuffer = function (buffer) {
|
|
65
|
+
$.checkArgument(Buffer.isBuffer(buffer));
|
|
66
|
+
var script = new Script();
|
|
67
|
+
script.buffer = buffer;
|
|
68
|
+
return script;
|
|
69
|
+
};
|
|
70
|
+
|
|
71
|
+
Script.fromChunks = function (chunks) {
|
|
72
|
+
var script = new Script();
|
|
73
|
+
|
|
74
|
+
const bw = new BufferWriter();
|
|
75
|
+
|
|
76
|
+
for (let index = 0; index < chunks.length; index++) {
|
|
77
|
+
const chunk = chunks[index];
|
|
78
|
+
bw.writeUInt8(chunk.opcodenum);
|
|
79
|
+
if (chunk.buf) {
|
|
80
|
+
if (chunk.opcodenum < Opcode.OP_PUSHDATA1) {
|
|
81
|
+
bw.write(chunk.buf);
|
|
82
|
+
} else if (chunk.opcodenum === Opcode.OP_PUSHDATA1) {
|
|
83
|
+
bw.writeUInt8(chunk.len);
|
|
84
|
+
bw.write(chunk.buf);
|
|
85
|
+
} else if (chunk.opcodenum === Opcode.OP_PUSHDATA2) {
|
|
86
|
+
bw.writeUInt16LE(chunk.len);
|
|
87
|
+
bw.write(chunk.buf);
|
|
88
|
+
} else if (chunk.opcodenum === Opcode.OP_PUSHDATA4) {
|
|
89
|
+
bw.writeUInt32LE(chunk.len);
|
|
90
|
+
bw.write(chunk.buf);
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
script.buffer = bw.toBuffer();
|
|
96
|
+
return script;
|
|
97
|
+
};
|
|
98
|
+
|
|
99
|
+
Script.prototype.toBuffer = function () {
|
|
100
|
+
return this.buffer;
|
|
101
|
+
};
|
|
102
|
+
|
|
103
|
+
Script.fromASM = function (str) {
|
|
104
|
+
return Script.fromBuffer(decodeASM(str));
|
|
105
|
+
};
|
|
106
|
+
|
|
107
|
+
Script.fromHex = function (str) {
|
|
108
|
+
return new Script(Buffer.from(str, 'hex'));
|
|
109
|
+
};
|
|
110
|
+
|
|
111
|
+
Script.fromString = function (str) {
|
|
112
|
+
if (JSUtil.isHexa(str) || str.length === 0) {
|
|
113
|
+
return new Script(Buffer.from(str, 'hex'));
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
var chunks = [];
|
|
117
|
+
|
|
118
|
+
var tokens = str.split(' ');
|
|
119
|
+
var i = 0;
|
|
120
|
+
while (i < tokens.length) {
|
|
121
|
+
var token = tokens[i];
|
|
122
|
+
var opcode = Opcode(token);
|
|
123
|
+
var opcodenum = opcode.toNumber();
|
|
124
|
+
|
|
125
|
+
if (_.isUndefined(opcodenum)) {
|
|
126
|
+
opcodenum = parseInt(token);
|
|
127
|
+
if (opcodenum > 0 && opcodenum < Opcode.OP_PUSHDATA1) {
|
|
128
|
+
var buf = Buffer.from(tokens[i + 1].slice(2), 'hex');
|
|
129
|
+
if (buf.length !== opcodenum) {
|
|
130
|
+
throw new Error('Invalid script buf len: ' + JSON.stringify(str));
|
|
131
|
+
}
|
|
132
|
+
chunks.push({
|
|
133
|
+
buf: Buffer.from(tokens[i + 1].slice(2), 'hex'),
|
|
134
|
+
len: opcodenum,
|
|
135
|
+
opcodenum: opcodenum,
|
|
136
|
+
});
|
|
137
|
+
i = i + 2;
|
|
138
|
+
} else {
|
|
139
|
+
throw new Error('Invalid script: ' + JSON.stringify(str));
|
|
140
|
+
}
|
|
141
|
+
} else if (
|
|
142
|
+
opcodenum === Opcode.OP_PUSHDATA1 ||
|
|
143
|
+
opcodenum === Opcode.OP_PUSHDATA2 ||
|
|
144
|
+
opcodenum === Opcode.OP_PUSHDATA4
|
|
145
|
+
) {
|
|
146
|
+
if (tokens[i + 2].slice(0, 2) !== '0x') {
|
|
147
|
+
throw new Error('Pushdata data must start with 0x');
|
|
148
|
+
}
|
|
149
|
+
chunks.push({
|
|
150
|
+
buf: Buffer.from(tokens[i + 2].slice(2), 'hex'),
|
|
151
|
+
len: parseInt(tokens[i + 1]),
|
|
152
|
+
opcodenum: opcodenum,
|
|
153
|
+
});
|
|
154
|
+
i = i + 3;
|
|
155
|
+
} else {
|
|
156
|
+
chunks.push({
|
|
157
|
+
opcodenum: opcodenum,
|
|
158
|
+
});
|
|
159
|
+
i = i + 1;
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
return Script.fromChunks(chunks);
|
|
163
|
+
};
|
|
164
|
+
|
|
165
|
+
Script.prototype.slice = function (start, end) {
|
|
166
|
+
return this.buffer.slice(start, end);
|
|
167
|
+
};
|
|
168
|
+
|
|
169
|
+
Object.defineProperty(Script.prototype, 'chunks', {
|
|
170
|
+
get() {
|
|
171
|
+
if (SCRIPT_TO_CHUNKS_CACHE.has(this)) return SCRIPT_TO_CHUNKS_CACHE.get(this);
|
|
172
|
+
const chunks = decodeScriptChunks(this.buffer);
|
|
173
|
+
SCRIPT_TO_CHUNKS_CACHE.set(this, chunks);
|
|
174
|
+
return chunks;
|
|
175
|
+
},
|
|
176
|
+
});
|
|
177
|
+
|
|
178
|
+
Object.defineProperty(Script.prototype, 'length', {
|
|
179
|
+
get() {
|
|
180
|
+
return this.buffer.length;
|
|
181
|
+
},
|
|
182
|
+
});
|
|
183
|
+
|
|
184
|
+
Script.prototype._chunkToString = function (chunk, type) {
|
|
185
|
+
var opcodenum = chunk.opcodenum;
|
|
186
|
+
var asm = type === 'asm';
|
|
187
|
+
var str = '';
|
|
188
|
+
if (!chunk.buf) {
|
|
189
|
+
// no data chunk
|
|
190
|
+
if (typeof Opcode.reverseMap[opcodenum] !== 'undefined') {
|
|
191
|
+
if (asm) {
|
|
192
|
+
// A few cases where the opcode name differs from reverseMap
|
|
193
|
+
// aside from 1 to 16 data pushes.
|
|
194
|
+
if (opcodenum === 0) {
|
|
195
|
+
// OP_0 -> 0
|
|
196
|
+
str = str + ' 0';
|
|
197
|
+
} else if (opcodenum === 79) {
|
|
198
|
+
// OP_1NEGATE -> 1
|
|
199
|
+
str = str + ' -1';
|
|
200
|
+
} else {
|
|
201
|
+
str = str + ' ' + Opcode(opcodenum).toString();
|
|
202
|
+
}
|
|
203
|
+
} else {
|
|
204
|
+
str = str + ' ' + Opcode(opcodenum).toString();
|
|
205
|
+
}
|
|
206
|
+
} else {
|
|
207
|
+
var numstr = opcodenum.toString(16);
|
|
208
|
+
if (numstr.length % 2 !== 0) {
|
|
209
|
+
numstr = '0' + numstr;
|
|
210
|
+
}
|
|
211
|
+
if (asm) {
|
|
212
|
+
str = str + ' ' + numstr;
|
|
213
|
+
} else {
|
|
214
|
+
str = str + ' ' + '0x' + numstr;
|
|
215
|
+
}
|
|
216
|
+
}
|
|
217
|
+
} else {
|
|
218
|
+
// data chunk
|
|
219
|
+
if (
|
|
220
|
+
!asm &&
|
|
221
|
+
(opcodenum === Opcode.OP_PUSHDATA1 ||
|
|
222
|
+
opcodenum === Opcode.OP_PUSHDATA2 ||
|
|
223
|
+
opcodenum === Opcode.OP_PUSHDATA4)
|
|
224
|
+
) {
|
|
225
|
+
str = str + ' ' + Opcode(opcodenum).toString();
|
|
226
|
+
}
|
|
227
|
+
if (chunk.len > 0) {
|
|
228
|
+
if (asm) {
|
|
229
|
+
str = str + ' ' + chunk.buf.toString('hex');
|
|
230
|
+
} else {
|
|
231
|
+
str = str + ' ' + chunk.len + ' ' + '0x' + chunk.buf.toString('hex');
|
|
232
|
+
}
|
|
233
|
+
}
|
|
234
|
+
}
|
|
235
|
+
return str;
|
|
236
|
+
};
|
|
237
|
+
|
|
238
|
+
Script.prototype.toASM = function () {
|
|
239
|
+
var str = '';
|
|
240
|
+
var chunks = this.chunks;
|
|
241
|
+
for (var i = 0; i < chunks.length; i++) {
|
|
242
|
+
var chunk = this.chunks[i];
|
|
243
|
+
str += this._chunkToString(chunk, 'asm');
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
return str.substr(1);
|
|
247
|
+
};
|
|
248
|
+
|
|
249
|
+
Script.prototype.toString = function () {
|
|
250
|
+
var str = '';
|
|
251
|
+
for (var i = 0; i < this.chunks.length; i++) {
|
|
252
|
+
var chunk = this.chunks[i];
|
|
253
|
+
str += this._chunkToString(chunk);
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
return str.substr(1);
|
|
257
|
+
};
|
|
258
|
+
|
|
259
|
+
Script.prototype.toHex = function () {
|
|
260
|
+
return encodeHex(this.buffer);
|
|
261
|
+
};
|
|
262
|
+
|
|
263
|
+
Script.prototype.inspect = function () {
|
|
264
|
+
return '<Script: ' + this.toString() + '>';
|
|
265
|
+
};
|
|
266
|
+
|
|
267
|
+
// script classification methods
|
|
268
|
+
|
|
269
|
+
/**
|
|
270
|
+
* @returns {boolean} if this is a pay to pubkey hash output script
|
|
271
|
+
*/
|
|
272
|
+
Script.prototype.isPublicKeyHashOut = function () {
|
|
273
|
+
return !!(
|
|
274
|
+
this.chunks.length === 5 &&
|
|
275
|
+
this.chunks[0].opcodenum === Opcode.OP_DUP &&
|
|
276
|
+
this.chunks[1].opcodenum === Opcode.OP_HASH160 &&
|
|
277
|
+
this.chunks[2].buf &&
|
|
278
|
+
this.chunks[2].buf.length === 20 &&
|
|
279
|
+
this.chunks[3].opcodenum === Opcode.OP_EQUALVERIFY &&
|
|
280
|
+
this.chunks[4].opcodenum === Opcode.OP_CHECKSIG
|
|
281
|
+
);
|
|
282
|
+
};
|
|
283
|
+
|
|
284
|
+
/**
|
|
285
|
+
* @returns {boolean} if this is a pay to public key hash input script
|
|
286
|
+
*/
|
|
287
|
+
Script.prototype.isPublicKeyHashIn = function () {
|
|
288
|
+
if (this.chunks.length === 2) {
|
|
289
|
+
var signatureBuf = this.chunks[0].buf;
|
|
290
|
+
var pubkeyBuf = this.chunks[1].buf;
|
|
291
|
+
if (
|
|
292
|
+
signatureBuf &&
|
|
293
|
+
signatureBuf.length &&
|
|
294
|
+
signatureBuf[0] === 0x30 &&
|
|
295
|
+
pubkeyBuf &&
|
|
296
|
+
pubkeyBuf.length
|
|
297
|
+
) {
|
|
298
|
+
var version = pubkeyBuf[0];
|
|
299
|
+
if ((version === 0x04 || version === 0x06 || version === 0x07) && pubkeyBuf.length === 65) {
|
|
300
|
+
return true;
|
|
301
|
+
} else if ((version === 0x03 || version === 0x02) && pubkeyBuf.length === 33) {
|
|
302
|
+
return true;
|
|
303
|
+
}
|
|
304
|
+
}
|
|
305
|
+
}
|
|
306
|
+
return false;
|
|
307
|
+
};
|
|
308
|
+
|
|
309
|
+
Script.prototype.getPublicKey = function () {
|
|
310
|
+
$.checkState(this.isPublicKeyOut(), "Can't retrieve PublicKey from a non-PK output");
|
|
311
|
+
return this.chunks[0].buf;
|
|
312
|
+
};
|
|
313
|
+
|
|
314
|
+
Script.prototype.getPublicKeyHash = function () {
|
|
315
|
+
$.checkState(this.isPublicKeyHashOut(), "Can't retrieve PublicKeyHash from a non-PKH output");
|
|
316
|
+
return this.chunks[2].buf;
|
|
317
|
+
};
|
|
318
|
+
|
|
319
|
+
/**
|
|
320
|
+
* @returns {boolean} if this is a public key output script
|
|
321
|
+
*/
|
|
322
|
+
Script.prototype.isPublicKeyOut = function () {
|
|
323
|
+
if (
|
|
324
|
+
this.chunks.length === 2 &&
|
|
325
|
+
this.chunks[0].buf &&
|
|
326
|
+
this.chunks[0].buf.length &&
|
|
327
|
+
this.chunks[1].opcodenum === Opcode.OP_CHECKSIG
|
|
328
|
+
) {
|
|
329
|
+
var pubkeyBuf = this.chunks[0].buf;
|
|
330
|
+
var version = pubkeyBuf[0];
|
|
331
|
+
var isVersion = false;
|
|
332
|
+
if ((version === 0x04 || version === 0x06 || version === 0x07) && pubkeyBuf.length === 65) {
|
|
333
|
+
isVersion = true;
|
|
334
|
+
} else if ((version === 0x03 || version === 0x02) && pubkeyBuf.length === 33) {
|
|
335
|
+
isVersion = true;
|
|
336
|
+
}
|
|
337
|
+
if (isVersion) {
|
|
338
|
+
return PublicKey.isValid(pubkeyBuf);
|
|
339
|
+
}
|
|
340
|
+
}
|
|
341
|
+
return false;
|
|
342
|
+
};
|
|
343
|
+
|
|
344
|
+
/**
|
|
345
|
+
* @returns {boolean} if this is a pay to public key input script
|
|
346
|
+
*/
|
|
347
|
+
Script.prototype.isPublicKeyIn = function () {
|
|
348
|
+
if (this.chunks.length === 1) {
|
|
349
|
+
var signatureBuf = this.chunks[0].buf;
|
|
350
|
+
if (signatureBuf && signatureBuf.length && signatureBuf[0] === 0x30) {
|
|
351
|
+
return true;
|
|
352
|
+
}
|
|
353
|
+
}
|
|
354
|
+
return false;
|
|
355
|
+
};
|
|
356
|
+
|
|
357
|
+
/**
|
|
358
|
+
* @returns {boolean} if this is a mutlsig output script
|
|
359
|
+
*/
|
|
360
|
+
Script.prototype.isMultisigOut = function () {
|
|
361
|
+
return (
|
|
362
|
+
this.chunks.length > 3 &&
|
|
363
|
+
Opcode.isSmallIntOp(this.chunks[0].opcodenum) &&
|
|
364
|
+
this.chunks.slice(1, this.chunks.length - 2).every(function (obj) {
|
|
365
|
+
return obj.buf && Buffer.isBuffer(obj.buf);
|
|
366
|
+
}) &&
|
|
367
|
+
Opcode.isSmallIntOp(this.chunks[this.chunks.length - 2].opcodenum) &&
|
|
368
|
+
this.chunks[this.chunks.length - 1].opcodenum === Opcode.OP_CHECKMULTISIG
|
|
369
|
+
);
|
|
370
|
+
};
|
|
371
|
+
|
|
372
|
+
|
|
373
|
+
/**
|
|
374
|
+
* Decodes a multisig output script into its components.
|
|
375
|
+
* @returns {Object} An object containing:
|
|
376
|
+
* - m {number} The required number of signatures (m-of-n)
|
|
377
|
+
* - n {number} The total number of public keys
|
|
378
|
+
* - pubkeys {Buffer[]} Array of public keys involved in the multisig
|
|
379
|
+
*/
|
|
380
|
+
Script.prototype.decodeMultisigOut = function () {
|
|
381
|
+
$.checkState(this.isMultisigOut(), "Can't decode a non-multisig output script");
|
|
382
|
+
const OP_INT_BASE = Opcode.OP_RESERVED; // OP_1 - 1
|
|
383
|
+
const m = this.chunks[0].opcodenum - OP_INT_BASE
|
|
384
|
+
const n = (this.chunks[0][this.chunks[0].length - 2]) - OP_INT_BASE;
|
|
385
|
+
const pubkeys = this.chunks.slice(1, -2).map((chunk) => chunk.buf);
|
|
386
|
+
|
|
387
|
+
return {
|
|
388
|
+
m,
|
|
389
|
+
n,
|
|
390
|
+
pubkeys
|
|
391
|
+
};
|
|
392
|
+
};
|
|
393
|
+
|
|
394
|
+
/**
|
|
395
|
+
* @returns {boolean} if this is a multisig input script
|
|
396
|
+
*/
|
|
397
|
+
Script.prototype.isMultisigIn = function () {
|
|
398
|
+
return (
|
|
399
|
+
this.chunks.length >= 2 &&
|
|
400
|
+
this.chunks[0].opcodenum === 0 &&
|
|
401
|
+
this.chunks.slice(1, this.chunks.length).every(function (obj) {
|
|
402
|
+
return obj.buf && Buffer.isBuffer(obj.buf) && Signature.isTxDER(obj.buf);
|
|
403
|
+
})
|
|
404
|
+
);
|
|
405
|
+
};
|
|
406
|
+
|
|
407
|
+
/**
|
|
408
|
+
* @returns {boolean} true if this is a valid standard OP_RETURN output
|
|
409
|
+
*/
|
|
410
|
+
Script.prototype.isDataOut = function () {
|
|
411
|
+
var step1 = this.buffer.length >= 1 && this.buffer[0] === Opcode.OP_RETURN;
|
|
412
|
+
if (!step1) return false;
|
|
413
|
+
var buffer = this.buffer.slice(1);
|
|
414
|
+
var script2 = new Script({ buffer: buffer });
|
|
415
|
+
return script2.isPushOnly();
|
|
416
|
+
};
|
|
417
|
+
|
|
418
|
+
Script.prototype.isSafeDataOut = function () {
|
|
419
|
+
if (this.buffer.length < 2) {
|
|
420
|
+
return false;
|
|
421
|
+
}
|
|
422
|
+
if (this.buffer[0] !== Opcode.OP_FALSE) {
|
|
423
|
+
return false;
|
|
424
|
+
}
|
|
425
|
+
var buffer = this.buffer.slice(1);
|
|
426
|
+
var script2 = new Script({ buffer });
|
|
427
|
+
return script2.isDataOut();
|
|
428
|
+
};
|
|
429
|
+
|
|
430
|
+
/**
|
|
431
|
+
* Retrieve the associated data for this script.
|
|
432
|
+
* In the case of a pay to public key hash, return the hash.
|
|
433
|
+
* In the case of safe OP_RETURN data, return an array of buffers
|
|
434
|
+
* In the case of a standard deprecated OP_RETURN, return the data
|
|
435
|
+
* @returns {Buffer}
|
|
436
|
+
*/
|
|
437
|
+
Script.prototype.getData = function () {
|
|
438
|
+
if (this.isSafeDataOut()) {
|
|
439
|
+
var chunks = this.chunks.slice(2);
|
|
440
|
+
var buffers = chunks.map((chunk) => chunk.buf);
|
|
441
|
+
return buffers;
|
|
442
|
+
}
|
|
443
|
+
if (this.isDataOut()) {
|
|
444
|
+
if (_.isUndefined(this.chunks[1])) {
|
|
445
|
+
return Buffer.alloc(0);
|
|
446
|
+
} else {
|
|
447
|
+
return Buffer.from(this.chunks[1].buf);
|
|
448
|
+
}
|
|
449
|
+
}
|
|
450
|
+
if (this.isPublicKeyHashOut()) {
|
|
451
|
+
return Buffer.from(this.chunks[2].buf);
|
|
452
|
+
}
|
|
453
|
+
throw new Error('Unrecognized script type to get data from');
|
|
454
|
+
};
|
|
455
|
+
|
|
456
|
+
/**
|
|
457
|
+
* @returns {boolean} if the script is only composed of data pushing
|
|
458
|
+
* opcodes or small int opcodes (OP_0, OP_1, ..., OP_16)
|
|
459
|
+
*/
|
|
460
|
+
Script.prototype.isPushOnly = function () {
|
|
461
|
+
return _.every(this.chunks, function (chunk) {
|
|
462
|
+
return (
|
|
463
|
+
chunk.opcodenum <= Opcode.OP_16 ||
|
|
464
|
+
chunk.opcodenum === Opcode.OP_PUSHDATA1 ||
|
|
465
|
+
chunk.opcodenum === Opcode.OP_PUSHDATA2 ||
|
|
466
|
+
chunk.opcodenum === Opcode.OP_PUSHDATA4
|
|
467
|
+
);
|
|
468
|
+
});
|
|
469
|
+
};
|
|
470
|
+
|
|
471
|
+
Script.types = {};
|
|
472
|
+
Script.types.UNKNOWN = 'Unknown';
|
|
473
|
+
Script.types.PUBKEY_OUT = 'Pay to public key';
|
|
474
|
+
Script.types.PUBKEY_IN = 'Spend from public key';
|
|
475
|
+
Script.types.PUBKEYHASH_OUT = 'Pay to public key hash';
|
|
476
|
+
Script.types.PUBKEYHASH_IN = 'Spend from public key hash';
|
|
477
|
+
Script.types.SCRIPTHASH_OUT = 'Pay to script hash';
|
|
478
|
+
Script.types.SCRIPTHASH_IN = 'Spend from script hash';
|
|
479
|
+
Script.types.MULTISIG_OUT = 'Pay to multisig';
|
|
480
|
+
Script.types.MULTISIG_IN = 'Spend from multisig';
|
|
481
|
+
Script.types.DATA_OUT = 'Data push';
|
|
482
|
+
Script.types.SAFE_DATA_OUT = 'Safe data push';
|
|
483
|
+
|
|
484
|
+
Script.OP_RETURN_STANDARD_SIZE = 220;
|
|
485
|
+
|
|
486
|
+
/**
|
|
487
|
+
* @returns {object} The Script type if it is a known form,
|
|
488
|
+
* or Script.UNKNOWN if it isn't
|
|
489
|
+
*/
|
|
490
|
+
Script.prototype.classify = function () {
|
|
491
|
+
if (this._isInput) {
|
|
492
|
+
return this.classifyInput();
|
|
493
|
+
} else if (this._isOutput) {
|
|
494
|
+
return this.classifyOutput();
|
|
495
|
+
} else {
|
|
496
|
+
var outputType = this.classifyOutput();
|
|
497
|
+
return outputType !== Script.types.UNKNOWN ? outputType : this.classifyInput();
|
|
498
|
+
}
|
|
499
|
+
};
|
|
500
|
+
|
|
501
|
+
Script.outputIdentifiers = {};
|
|
502
|
+
Script.outputIdentifiers.PUBKEY_OUT = Script.prototype.isPublicKeyOut;
|
|
503
|
+
Script.outputIdentifiers.PUBKEYHASH_OUT = Script.prototype.isPublicKeyHashOut;
|
|
504
|
+
Script.outputIdentifiers.MULTISIG_OUT = Script.prototype.isMultisigOut;
|
|
505
|
+
Script.outputIdentifiers.DATA_OUT = Script.prototype.isDataOut;
|
|
506
|
+
Script.outputIdentifiers.SAFE_DATA_OUT = Script.prototype.isSafeDataOut;
|
|
507
|
+
|
|
508
|
+
/**
|
|
509
|
+
* @returns {object} The Script type if it is a known form,
|
|
510
|
+
* or Script.UNKNOWN if it isn't
|
|
511
|
+
*/
|
|
512
|
+
Script.prototype.classifyOutput = function () {
|
|
513
|
+
for (var type in Script.outputIdentifiers) {
|
|
514
|
+
if (Script.outputIdentifiers[type].bind(this)()) {
|
|
515
|
+
return Script.types[type];
|
|
516
|
+
}
|
|
517
|
+
}
|
|
518
|
+
return Script.types.UNKNOWN;
|
|
519
|
+
};
|
|
520
|
+
|
|
521
|
+
Script.inputIdentifiers = {};
|
|
522
|
+
Script.inputIdentifiers.PUBKEY_IN = Script.prototype.isPublicKeyIn;
|
|
523
|
+
Script.inputIdentifiers.PUBKEYHASH_IN = Script.prototype.isPublicKeyHashIn;
|
|
524
|
+
Script.inputIdentifiers.MULTISIG_IN = Script.prototype.isMultisigIn;
|
|
525
|
+
|
|
526
|
+
/**
|
|
527
|
+
* @returns {object} The Script type if it is a known form,
|
|
528
|
+
* or Script.UNKNOWN if it isn't
|
|
529
|
+
*/
|
|
530
|
+
Script.prototype.classifyInput = function () {
|
|
531
|
+
for (var type in Script.inputIdentifiers) {
|
|
532
|
+
if (Script.inputIdentifiers[type].bind(this)()) {
|
|
533
|
+
return Script.types[type];
|
|
534
|
+
}
|
|
535
|
+
}
|
|
536
|
+
return Script.types.UNKNOWN;
|
|
537
|
+
};
|
|
538
|
+
|
|
539
|
+
/**
|
|
540
|
+
* @returns {boolean} if script is one of the known types
|
|
541
|
+
*/
|
|
542
|
+
Script.prototype.isStandard = function () {
|
|
543
|
+
// TODO: Add BIP62 compliance
|
|
544
|
+
return this.classify() !== Script.types.UNKNOWN;
|
|
545
|
+
};
|
|
546
|
+
|
|
547
|
+
// Script construction methods
|
|
548
|
+
|
|
549
|
+
/**
|
|
550
|
+
* Adds a script element at the start of the script.
|
|
551
|
+
* @param {*} obj a string, number, Opcode, Buffer, or object to add
|
|
552
|
+
* @returns {Script} this script instance
|
|
553
|
+
*/
|
|
554
|
+
Script.prototype.prepend = function (obj) {
|
|
555
|
+
this._addByType(obj, true);
|
|
556
|
+
return this;
|
|
557
|
+
};
|
|
558
|
+
|
|
559
|
+
/**
|
|
560
|
+
* Compares a script with another script
|
|
561
|
+
*/
|
|
562
|
+
Script.prototype.equals = function (script) {
|
|
563
|
+
$.checkState(script instanceof Script, 'Must provide another script');
|
|
564
|
+
if (this.buffer.length !== script.buffer.length) {
|
|
565
|
+
return false;
|
|
566
|
+
}
|
|
567
|
+
var i;
|
|
568
|
+
for (i = 0; i < this.buffer.length; i++) {
|
|
569
|
+
if (this.buffer[i] !== script.buffer[i]) {
|
|
570
|
+
return false;
|
|
571
|
+
}
|
|
572
|
+
}
|
|
573
|
+
return true;
|
|
574
|
+
};
|
|
575
|
+
|
|
576
|
+
/**
|
|
577
|
+
* Adds a script element to the end of the script.
|
|
578
|
+
*
|
|
579
|
+
* @param {*} obj a string, number, Opcode, Buffer, or object to add
|
|
580
|
+
* @returns {Script} this script instance
|
|
581
|
+
*
|
|
582
|
+
*/
|
|
583
|
+
Script.prototype.add = function (obj) {
|
|
584
|
+
this._addByType(obj, false);
|
|
585
|
+
return this;
|
|
586
|
+
};
|
|
587
|
+
|
|
588
|
+
Script.prototype._addByType = function (obj, prepend) {
|
|
589
|
+
if (typeof obj === 'string') {
|
|
590
|
+
this._addOpcode(obj, prepend);
|
|
591
|
+
} else if (typeof obj === 'number') {
|
|
592
|
+
this._addOpcode(obj, prepend);
|
|
593
|
+
} else if (obj instanceof Opcode) {
|
|
594
|
+
this._addOpcode(obj, prepend);
|
|
595
|
+
} else if (Buffer.isBuffer(obj)) {
|
|
596
|
+
this._addBuffer(obj, prepend);
|
|
597
|
+
} else if (obj instanceof Script) {
|
|
598
|
+
this._insertAtPosition(obj.buffer, prepend);
|
|
599
|
+
} else if (typeof obj === 'object') {
|
|
600
|
+
var s = Script.fromChunks([obj]);
|
|
601
|
+
this._insertAtPosition(s.toBuffer(), prepend);
|
|
602
|
+
} else {
|
|
603
|
+
throw new Error('Invalid script chunk');
|
|
604
|
+
}
|
|
605
|
+
};
|
|
606
|
+
|
|
607
|
+
Script.prototype._insertAtPosition = function (buf, prepend) {
|
|
608
|
+
var bw = new BufferWriter();
|
|
609
|
+
|
|
610
|
+
if (prepend) {
|
|
611
|
+
bw.write(buf);
|
|
612
|
+
bw.write(this.buffer);
|
|
613
|
+
} else {
|
|
614
|
+
bw.write(this.buffer);
|
|
615
|
+
bw.write(buf);
|
|
616
|
+
}
|
|
617
|
+
this.buffer = bw.toBuffer();
|
|
618
|
+
};
|
|
619
|
+
|
|
620
|
+
Script.prototype._addOpcode = function (opcode, prepend) {
|
|
621
|
+
var op;
|
|
622
|
+
if (typeof opcode === 'number') {
|
|
623
|
+
op = opcode;
|
|
624
|
+
} else if (opcode instanceof Opcode) {
|
|
625
|
+
op = opcode.toNumber();
|
|
626
|
+
} else {
|
|
627
|
+
op = Opcode(opcode).toNumber();
|
|
628
|
+
}
|
|
629
|
+
|
|
630
|
+
// OP_INVALIDOPCODE
|
|
631
|
+
if (op > 255) {
|
|
632
|
+
throw new errors.Script.InvalidOpcode(op);
|
|
633
|
+
}
|
|
634
|
+
this._insertAtPosition(
|
|
635
|
+
Script.fromChunks([
|
|
636
|
+
{
|
|
637
|
+
opcodenum: op,
|
|
638
|
+
},
|
|
639
|
+
]).toBuffer(),
|
|
640
|
+
prepend,
|
|
641
|
+
);
|
|
642
|
+
return this;
|
|
643
|
+
};
|
|
644
|
+
|
|
645
|
+
Script.prototype._addBuffer = function (buf, prepend) {
|
|
646
|
+
var bw = new BufferWriter();
|
|
647
|
+
var opcodenum;
|
|
648
|
+
var len = buf.length;
|
|
649
|
+
if (len === 0) {
|
|
650
|
+
opcodenum = 0;
|
|
651
|
+
bw.writeUInt8(opcodenum);
|
|
652
|
+
} else if (len > 0 && len < Opcode.OP_PUSHDATA1) {
|
|
653
|
+
opcodenum = len;
|
|
654
|
+
bw.writeUInt8(opcodenum);
|
|
655
|
+
bw.write(buf);
|
|
656
|
+
} else if (len < Math.pow(2, 8)) {
|
|
657
|
+
opcodenum = Opcode.OP_PUSHDATA1;
|
|
658
|
+
bw.writeUInt8(opcodenum);
|
|
659
|
+
bw.writeUInt8(len);
|
|
660
|
+
bw.write(buf);
|
|
661
|
+
} else if (len < Math.pow(2, 16)) {
|
|
662
|
+
opcodenum = Opcode.OP_PUSHDATA2;
|
|
663
|
+
bw.writeUInt8(opcodenum);
|
|
664
|
+
bw.writeUInt16LE(len);
|
|
665
|
+
bw.write(buf);
|
|
666
|
+
} else if (len < Math.pow(2, 32)) {
|
|
667
|
+
opcodenum = Opcode.OP_PUSHDATA4;
|
|
668
|
+
bw.writeUInt8(opcodenum);
|
|
669
|
+
bw.writeUInt32LE(len);
|
|
670
|
+
bw.write(buf);
|
|
671
|
+
} else {
|
|
672
|
+
throw new Error("You can't push that much data");
|
|
673
|
+
}
|
|
674
|
+
|
|
675
|
+
this._insertAtPosition(bw.toBuffer(), prepend);
|
|
676
|
+
return this;
|
|
677
|
+
};
|
|
678
|
+
|
|
679
|
+
Script.prototype.clone = function () {
|
|
680
|
+
return Script.fromBuffer(this.buffer.slice());
|
|
681
|
+
};
|
|
682
|
+
|
|
683
|
+
Script.prototype.removeCodeseparators = function () {
|
|
684
|
+
var chunks = [];
|
|
685
|
+
for (var i = 0; i < this.chunks.length; i++) {
|
|
686
|
+
if (this.chunks[i].opcodenum !== Opcode.OP_CODESEPARATOR) {
|
|
687
|
+
chunks.push(this.chunks[i]);
|
|
688
|
+
}
|
|
689
|
+
}
|
|
690
|
+
SCRIPT_TO_CHUNKS_CACHE.delete(this);
|
|
691
|
+
|
|
692
|
+
this.buffer = Script.fromChunks(chunks).toBuffer();
|
|
693
|
+
return this;
|
|
694
|
+
};
|
|
695
|
+
|
|
696
|
+
/**
|
|
697
|
+
* If the script does not contain any OP_CODESEPARATOR, Return all scripts
|
|
698
|
+
* If the script contains any OP_CODESEPARATOR, the scriptCode is the script but removing everything up to and including the last executed OP_CODESEPARATOR before the signature checking opcode being executed
|
|
699
|
+
* @param {n} The {n}th codeseparator in the script
|
|
700
|
+
*
|
|
701
|
+
* @returns {Script} Subset of script starting at the {n}th codeseparator
|
|
702
|
+
*/
|
|
703
|
+
Script.prototype.subScript = function (n) {
|
|
704
|
+
var idx = 0;
|
|
705
|
+
|
|
706
|
+
for (var i = 0; i < this.chunks.length; i++) {
|
|
707
|
+
if (this.chunks[i].opcodenum === Opcode.OP_CODESEPARATOR) {
|
|
708
|
+
if (idx === n) {
|
|
709
|
+
return Script.fromChunks(this.chunks.slice(i + 1));
|
|
710
|
+
} else {
|
|
711
|
+
idx++;
|
|
712
|
+
}
|
|
713
|
+
}
|
|
714
|
+
}
|
|
715
|
+
|
|
716
|
+
return this;
|
|
717
|
+
};
|
|
718
|
+
|
|
719
|
+
// high level script builder methods
|
|
720
|
+
|
|
721
|
+
/**
|
|
722
|
+
* @returns {Script} a new Multisig output script for given public keys,
|
|
723
|
+
* requiring m of those public keys to spend
|
|
724
|
+
* @param {PublicKey[]} publicKeys - list of all public keys controlling the output
|
|
725
|
+
* @param {number} threshold - amount of required signatures to spend the output
|
|
726
|
+
* @param {Object=} opts - Several options:
|
|
727
|
+
* - noSorting: defaults to false, if true, don't sort the given
|
|
728
|
+
* public keys before creating the script
|
|
729
|
+
*/
|
|
730
|
+
Script.buildMultisigOut = function (publicKeys, threshold, opts) {
|
|
731
|
+
$.checkArgument(
|
|
732
|
+
threshold <= publicKeys.length,
|
|
733
|
+
'Number of required signatures must be less than or equal to the number of public keys',
|
|
734
|
+
);
|
|
735
|
+
opts = opts || {};
|
|
736
|
+
var script = new Script();
|
|
737
|
+
script.add(Opcode.smallInt(threshold));
|
|
738
|
+
publicKeys = _.map(publicKeys, PublicKey);
|
|
739
|
+
var sorted = publicKeys;
|
|
740
|
+
if (!opts.noSorting) {
|
|
741
|
+
sorted = publicKeys
|
|
742
|
+
.map((k) => k.toString('hex'))
|
|
743
|
+
.sort()
|
|
744
|
+
.map((k) => new PublicKey(k));
|
|
745
|
+
}
|
|
746
|
+
for (var i = 0; i < sorted.length; i++) {
|
|
747
|
+
var publicKey = sorted[i];
|
|
748
|
+
script.add(publicKey.toBuffer());
|
|
749
|
+
}
|
|
750
|
+
script.add(Opcode.smallInt(publicKeys.length));
|
|
751
|
+
script.add(Opcode.OP_CHECKMULTISIG);
|
|
752
|
+
return script;
|
|
753
|
+
};
|
|
754
|
+
|
|
755
|
+
/**
|
|
756
|
+
* A new Multisig input script for the given public keys, requiring m of those public keys to spend
|
|
757
|
+
*
|
|
758
|
+
* @param {PublicKey[]} pubkeys list of all public keys controlling the output
|
|
759
|
+
* @param {number} threshold amount of required signatures to spend the output
|
|
760
|
+
* @param {Array} signatures and array of signature buffers to append to the script
|
|
761
|
+
* @param {Object=} opts
|
|
762
|
+
* @param {boolean=} opts.noSorting don't sort the given public keys before creating the script (false by default)
|
|
763
|
+
* @param {Script=} opts.cachedMultisig don't recalculate the redeemScript
|
|
764
|
+
*
|
|
765
|
+
* @returns {Script}
|
|
766
|
+
*/
|
|
767
|
+
Script.buildMultisigIn = function (pubkeys, threshold, signatures, opts) {
|
|
768
|
+
$.checkArgument(_.isArray(pubkeys));
|
|
769
|
+
$.checkArgument(_.isNumber(threshold));
|
|
770
|
+
$.checkArgument(_.isArray(signatures));
|
|
771
|
+
opts = opts || {};
|
|
772
|
+
var s = new Script();
|
|
773
|
+
s.add(Opcode.OP_0);
|
|
774
|
+
_.each(signatures, function (signature) {
|
|
775
|
+
$.checkArgument(Buffer.isBuffer(signature), 'Signatures must be an array of Buffers');
|
|
776
|
+
// TODO: allow signatures to be an array of Signature objects
|
|
777
|
+
s.add(signature);
|
|
778
|
+
});
|
|
779
|
+
return s;
|
|
780
|
+
};
|
|
781
|
+
|
|
782
|
+
/**
|
|
783
|
+
* @returns {Script} a new pay to public key hash output for the given
|
|
784
|
+
* address or public key
|
|
785
|
+
* @param {(Address|PublicKey)} to - destination address or public key
|
|
786
|
+
*/
|
|
787
|
+
Script.buildPublicKeyHashOut = function (to) {
|
|
788
|
+
$.checkArgument(!_.isUndefined(to));
|
|
789
|
+
$.checkArgument(to instanceof PublicKey || to instanceof Address || _.isString(to));
|
|
790
|
+
if (to instanceof PublicKey) {
|
|
791
|
+
to = to.toAddress();
|
|
792
|
+
} else if (_.isString(to)) {
|
|
793
|
+
to = new Address(to);
|
|
794
|
+
}
|
|
795
|
+
var s = new Script();
|
|
796
|
+
s.add(Opcode.OP_DUP)
|
|
797
|
+
.add(Opcode.OP_HASH160)
|
|
798
|
+
.add(to.hashBuffer)
|
|
799
|
+
.add(Opcode.OP_EQUALVERIFY)
|
|
800
|
+
.add(Opcode.OP_CHECKSIG);
|
|
801
|
+
s._network = to.network;
|
|
802
|
+
return s;
|
|
803
|
+
};
|
|
804
|
+
|
|
805
|
+
/**
|
|
806
|
+
* @returns {Script} a new pay to public key output for the given
|
|
807
|
+
* public key
|
|
808
|
+
*/
|
|
809
|
+
Script.buildPublicKeyOut = function (pubkey) {
|
|
810
|
+
$.checkArgument(pubkey instanceof PublicKey);
|
|
811
|
+
var s = new Script();
|
|
812
|
+
s.add(pubkey.toBuffer()).add(Opcode.OP_CHECKSIG);
|
|
813
|
+
return s;
|
|
814
|
+
};
|
|
815
|
+
|
|
816
|
+
/**
|
|
817
|
+
* @returns {Script} a new OP_RETURN script with data
|
|
818
|
+
* @param {(string|Buffer|Array)} data - the data to embed in the output - it is a string, buffer, or array of strings or buffers
|
|
819
|
+
* @param {(string)} encoding - the type of encoding of the string(s)
|
|
820
|
+
*/
|
|
821
|
+
Script.buildDataOut = function (data, encoding) {
|
|
822
|
+
$.checkArgument(
|
|
823
|
+
_.isUndefined(data) || _.isString(data) || _.isArray(data) || Buffer.isBuffer(data),
|
|
824
|
+
);
|
|
825
|
+
var datas = data;
|
|
826
|
+
if (!_.isArray(datas)) {
|
|
827
|
+
datas = [data];
|
|
828
|
+
}
|
|
829
|
+
var s = new Script();
|
|
830
|
+
s.add(Opcode.OP_RETURN);
|
|
831
|
+
for (let data of datas) {
|
|
832
|
+
$.checkArgument(_.isUndefined(data) || _.isString(data) || Buffer.isBuffer(data));
|
|
833
|
+
if (_.isString(data)) {
|
|
834
|
+
data = Buffer.from(data, encoding);
|
|
835
|
+
}
|
|
836
|
+
if (!_.isUndefined(data)) {
|
|
837
|
+
s.add(data);
|
|
838
|
+
}
|
|
839
|
+
}
|
|
840
|
+
return s;
|
|
841
|
+
};
|
|
842
|
+
|
|
843
|
+
/**
|
|
844
|
+
* @returns {Script} a new OP_RETURN script with data
|
|
845
|
+
* @param {(string|Buffer|Array)} data - the data to embed in the output - it is a string, buffer, or array of strings or buffers
|
|
846
|
+
* @param {(string)} encoding - the type of encoding of the string(s)
|
|
847
|
+
*/
|
|
848
|
+
Script.buildSafeDataOut = function (data, encoding) {
|
|
849
|
+
var s2 = Script.buildDataOut(data, encoding);
|
|
850
|
+
var s1 = new Script();
|
|
851
|
+
s1.add(Opcode.OP_FALSE);
|
|
852
|
+
s1.add(s2);
|
|
853
|
+
return s1;
|
|
854
|
+
};
|
|
855
|
+
|
|
856
|
+
|
|
857
|
+
/**
|
|
858
|
+
* Builds a scriptSig (a script for an input) that signs a public key output script.
|
|
859
|
+
*
|
|
860
|
+
* @param {Signature|Buffer} signature - a Signature object, or the signature in DER canonical encoding
|
|
861
|
+
* @param {number=} sigtype - the type of the signature (defaults to SIGHASH_ALL)
|
|
862
|
+
*/
|
|
863
|
+
Script.buildPublicKeyIn = function (signature, sigtype) {
|
|
864
|
+
$.checkArgument(signature instanceof Signature || Buffer.isBuffer(signature));
|
|
865
|
+
$.checkArgument(_.isUndefined(sigtype) || _.isNumber(sigtype));
|
|
866
|
+
if (signature instanceof Signature) {
|
|
867
|
+
signature = signature.toBuffer();
|
|
868
|
+
}
|
|
869
|
+
var script = new Script();
|
|
870
|
+
script.add(Buffer.concat([signature, Buffer.from([(sigtype || Signature.SIGHASH_ALL) & 0xff])]));
|
|
871
|
+
return script;
|
|
872
|
+
};
|
|
873
|
+
|
|
874
|
+
/**
|
|
875
|
+
* Builds a scriptSig (a script for an input) that signs a public key hash
|
|
876
|
+
* output script.
|
|
877
|
+
*
|
|
878
|
+
* @param {Buffer|string|PublicKey} publicKey
|
|
879
|
+
* @param {Signature|Buffer} signature - a Signature object, or the signature in DER canonical encoding
|
|
880
|
+
* @param {number=} sigtype - the type of the signature (defaults to SIGHASH_ALL)
|
|
881
|
+
*/
|
|
882
|
+
Script.buildPublicKeyHashIn = function (publicKey, signature, sigtype) {
|
|
883
|
+
$.checkArgument(signature instanceof Signature || Buffer.isBuffer(signature));
|
|
884
|
+
$.checkArgument(_.isUndefined(sigtype) || _.isNumber(sigtype));
|
|
885
|
+
if (signature instanceof Signature) {
|
|
886
|
+
signature = signature.toBuffer();
|
|
887
|
+
}
|
|
888
|
+
var script = new Script()
|
|
889
|
+
.add(Buffer.concat([signature, Buffer.from([(sigtype || Signature.SIGHASH_ALL) & 0xff])]))
|
|
890
|
+
.add(new PublicKey(publicKey).toBuffer());
|
|
891
|
+
return script;
|
|
892
|
+
};
|
|
893
|
+
|
|
894
|
+
/**
|
|
895
|
+
* @returns {Script} an empty script
|
|
896
|
+
*/
|
|
897
|
+
Script.empty = function () {
|
|
898
|
+
return new Script();
|
|
899
|
+
};
|
|
900
|
+
|
|
901
|
+
/**
|
|
902
|
+
* @return {Script} an output script built from the address
|
|
903
|
+
*/
|
|
904
|
+
Script.fromAddress = function (address) {
|
|
905
|
+
address = Address(address);
|
|
906
|
+
if (address.isPayToPublicKeyHash()) {
|
|
907
|
+
return Script.buildPublicKeyHashOut(address);
|
|
908
|
+
}
|
|
909
|
+
throw new errors.Script.UnrecognizedAddress(address);
|
|
910
|
+
};
|
|
911
|
+
|
|
912
|
+
/**
|
|
913
|
+
* Will return the associated address information object
|
|
914
|
+
* @return {Address|boolean}
|
|
915
|
+
*/
|
|
916
|
+
Script.prototype.getAddressInfo = function (opts) {
|
|
917
|
+
if (this._isInput) {
|
|
918
|
+
return this._getInputAddressInfo();
|
|
919
|
+
} else if (this._isOutput) {
|
|
920
|
+
return this._getOutputAddressInfo();
|
|
921
|
+
} else {
|
|
922
|
+
var info = this._getOutputAddressInfo();
|
|
923
|
+
if (!info) {
|
|
924
|
+
return this._getInputAddressInfo();
|
|
925
|
+
}
|
|
926
|
+
return info;
|
|
927
|
+
}
|
|
928
|
+
};
|
|
929
|
+
|
|
930
|
+
/**
|
|
931
|
+
* Will return the associated output scriptPubKey address information object
|
|
932
|
+
* @return {Address|boolean}
|
|
933
|
+
* @private
|
|
934
|
+
*/
|
|
935
|
+
Script.prototype._getOutputAddressInfo = function () {
|
|
936
|
+
var info = {};
|
|
937
|
+
if (this.isPublicKeyHashOut()) {
|
|
938
|
+
info.hashBuffer = this.getData();
|
|
939
|
+
info.type = Address.PayToPublicKeyHash;
|
|
940
|
+
} else {
|
|
941
|
+
return false;
|
|
942
|
+
}
|
|
943
|
+
return info;
|
|
944
|
+
};
|
|
945
|
+
|
|
946
|
+
/**
|
|
947
|
+
* Will return the associated input scriptSig address information object
|
|
948
|
+
* @return {Address|boolean}
|
|
949
|
+
* @private
|
|
950
|
+
*/
|
|
951
|
+
Script.prototype._getInputAddressInfo = function () {
|
|
952
|
+
var info = {};
|
|
953
|
+
if (this.isPublicKeyHashIn()) {
|
|
954
|
+
// hash the publickey found in the scriptSig
|
|
955
|
+
info.hashBuffer = Hash.sha256ripemd160(this.chunks[1].buf);
|
|
956
|
+
info.type = Address.PayToPublicKeyHash;
|
|
957
|
+
} else {
|
|
958
|
+
return false;
|
|
959
|
+
}
|
|
960
|
+
return info;
|
|
961
|
+
};
|
|
962
|
+
|
|
963
|
+
/**
|
|
964
|
+
* @param {Network=} network
|
|
965
|
+
* @return {Address|boolean} the associated address for this script if possible, or false
|
|
966
|
+
*/
|
|
967
|
+
Script.prototype.toAddress = function (network) {
|
|
968
|
+
var info = this.getAddressInfo();
|
|
969
|
+
if (!info) {
|
|
970
|
+
return false;
|
|
971
|
+
}
|
|
972
|
+
info.network = Networks.get(network) || this._network || Networks.defaultNetwork;
|
|
973
|
+
return new Address(info);
|
|
974
|
+
};
|
|
975
|
+
|
|
976
|
+
/**
|
|
977
|
+
* Analogous to bitcoind's FindAndDelete. Find and delete equivalent chunks,
|
|
978
|
+
* typically used with push data chunks. Note that this will find and delete
|
|
979
|
+
* not just the same data, but the same data with the same push data op as
|
|
980
|
+
* produced by default. i.e., if a pushdata in a tx does not use the minimal
|
|
981
|
+
* pushdata op, then when you try to remove the data it is pushing, it will not
|
|
982
|
+
* be removed, because they do not use the same pushdata op.
|
|
983
|
+
*/
|
|
984
|
+
Script.prototype.findAndDelete = function (script) {
|
|
985
|
+
var buf = script.toBuffer();
|
|
986
|
+
var hex = buf.toString('hex');
|
|
987
|
+
var chunks = this.chunks;
|
|
988
|
+
for (var i = 0; i < chunks.length; i++) {
|
|
989
|
+
var script2 = Script.fromChunks([chunks[i]]);
|
|
990
|
+
var buf2 = script2.toBuffer();
|
|
991
|
+
var hex2 = buf2.toString('hex');
|
|
992
|
+
if (hex === hex2) {
|
|
993
|
+
chunks.splice(i, 1);
|
|
994
|
+
this.buffer = Script.fromChunks(chunks).toBuffer();
|
|
995
|
+
}
|
|
996
|
+
}
|
|
997
|
+
return this;
|
|
998
|
+
};
|
|
999
|
+
|
|
1000
|
+
/**
|
|
1001
|
+
* Comes from bitcoind's script interpreter CheckMinimalPush function
|
|
1002
|
+
* @returns {boolean} if the chunk {i} is the smallest way to push that particular data.
|
|
1003
|
+
*/
|
|
1004
|
+
Script.prototype.checkMinimalPush = function (i) {
|
|
1005
|
+
var chunk = this.chunks[i];
|
|
1006
|
+
var buf = chunk.buf;
|
|
1007
|
+
var opcodenum = chunk.opcodenum;
|
|
1008
|
+
if (!buf) {
|
|
1009
|
+
return true;
|
|
1010
|
+
}
|
|
1011
|
+
if (buf.length === 0) {
|
|
1012
|
+
// Could have used OP_0.
|
|
1013
|
+
return opcodenum === Opcode.OP_0;
|
|
1014
|
+
} else if (buf.length === 1 && buf[0] >= 1 && buf[0] <= 16) {
|
|
1015
|
+
// Could have used OP_1 .. OP_16.
|
|
1016
|
+
return opcodenum === Opcode.OP_1 + (buf[0] - 1);
|
|
1017
|
+
} else if (buf.length === 1 && buf[0] === 0x81) {
|
|
1018
|
+
// Could have used OP_1NEGATE
|
|
1019
|
+
return opcodenum === Opcode.OP_1NEGATE;
|
|
1020
|
+
} else if (buf.length <= 75) {
|
|
1021
|
+
// Could have used a direct push (opcode indicating number of bytes pushed + those bytes).
|
|
1022
|
+
return opcodenum === buf.length;
|
|
1023
|
+
} else if (buf.length <= 255) {
|
|
1024
|
+
// Could have used OP_PUSHDATA.
|
|
1025
|
+
return opcodenum === Opcode.OP_PUSHDATA1;
|
|
1026
|
+
} else if (buf.length <= 65535) {
|
|
1027
|
+
// Could have used OP_PUSHDATA2.
|
|
1028
|
+
return opcodenum === Opcode.OP_PUSHDATA2;
|
|
1029
|
+
}
|
|
1030
|
+
return true;
|
|
1031
|
+
};
|
|
1032
|
+
|
|
1033
|
+
/**
|
|
1034
|
+
* Comes from bitcoind's script DecodeOP_N function
|
|
1035
|
+
* @param {number} opcode
|
|
1036
|
+
* @returns {number} numeric value in range of 0 to 16
|
|
1037
|
+
*/
|
|
1038
|
+
Script.prototype._decodeOP_N = function (opcode) {
|
|
1039
|
+
if (opcode === Opcode.OP_0) {
|
|
1040
|
+
return 0;
|
|
1041
|
+
} else if (opcode >= Opcode.OP_1 && opcode <= Opcode.OP_16) {
|
|
1042
|
+
return opcode - (Opcode.OP_1 - 1);
|
|
1043
|
+
} else {
|
|
1044
|
+
throw new Error('Invalid opcode: ' + JSON.stringify(opcode));
|
|
1045
|
+
}
|
|
1046
|
+
};
|
|
1047
|
+
|
|
1048
|
+
/**
|
|
1049
|
+
* Comes from bitcoind's script GetSigOpCount(boolean) function
|
|
1050
|
+
* @param {boolean} use current (true) or pre-version-0.6 (false) logic
|
|
1051
|
+
* @returns {number} number of signature operations required by this script
|
|
1052
|
+
*/
|
|
1053
|
+
Script.prototype.getSignatureOperationsCount = function (accurate) {
|
|
1054
|
+
accurate = _.isUndefined(accurate) ? true : accurate;
|
|
1055
|
+
var self = this;
|
|
1056
|
+
var n = 0;
|
|
1057
|
+
var lastOpcode = Opcode.OP_INVALIDOPCODE;
|
|
1058
|
+
_.each(self.chunks, function getChunk(chunk) {
|
|
1059
|
+
var opcode = chunk.opcodenum;
|
|
1060
|
+
if (opcode === Opcode.OP_CHECKSIG || opcode === Opcode.OP_CHECKSIGVERIFY) {
|
|
1061
|
+
n++;
|
|
1062
|
+
} else if (opcode === Opcode.OP_CHECKMULTISIG || opcode === Opcode.OP_CHECKMULTISIGVERIFY) {
|
|
1063
|
+
if (accurate && lastOpcode >= Opcode.OP_1 && lastOpcode <= Opcode.OP_16) {
|
|
1064
|
+
n += self._decodeOP_N(lastOpcode);
|
|
1065
|
+
} else {
|
|
1066
|
+
n += 20;
|
|
1067
|
+
}
|
|
1068
|
+
}
|
|
1069
|
+
lastOpcode = opcode;
|
|
1070
|
+
});
|
|
1071
|
+
return n;
|
|
1072
|
+
};
|
|
1073
|
+
|
|
1074
|
+
module.exports = Script;
|