@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,212 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var _ = require('../util/_');
|
|
4
|
+
var $ = require('../util/preconditions');
|
|
5
|
+
var BN = require('../crypto/bn');
|
|
6
|
+
|
|
7
|
+
var BufferReader = function BufferReader(buf) {
|
|
8
|
+
if (!(this instanceof BufferReader)) {
|
|
9
|
+
return new BufferReader(buf);
|
|
10
|
+
}
|
|
11
|
+
if (_.isUndefined(buf)) {
|
|
12
|
+
return;
|
|
13
|
+
}
|
|
14
|
+
if (Buffer.isBuffer(buf)) {
|
|
15
|
+
this.set({
|
|
16
|
+
buf: buf,
|
|
17
|
+
});
|
|
18
|
+
} else if (_.isString(buf)) {
|
|
19
|
+
var b = Buffer.from(buf, 'hex');
|
|
20
|
+
if (b.length * 2 !== buf.length) {
|
|
21
|
+
throw new TypeError('Invalid hex string');
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
this.set({
|
|
25
|
+
buf: b,
|
|
26
|
+
});
|
|
27
|
+
} else if (_.isObject(buf)) {
|
|
28
|
+
var obj = buf;
|
|
29
|
+
this.set(obj);
|
|
30
|
+
} else {
|
|
31
|
+
throw new TypeError('Unrecognized argument for BufferReader');
|
|
32
|
+
}
|
|
33
|
+
};
|
|
34
|
+
|
|
35
|
+
BufferReader.prototype.set = function (obj) {
|
|
36
|
+
this.buf = obj.buf || this.buf || undefined;
|
|
37
|
+
this.pos = obj.pos || this.pos || 0;
|
|
38
|
+
return this;
|
|
39
|
+
};
|
|
40
|
+
|
|
41
|
+
BufferReader.prototype.eof = function () {
|
|
42
|
+
return this.pos >= this.buf.length;
|
|
43
|
+
};
|
|
44
|
+
|
|
45
|
+
BufferReader.prototype.finished = BufferReader.prototype.eof;
|
|
46
|
+
|
|
47
|
+
BufferReader.prototype.read = function (len) {
|
|
48
|
+
$.checkArgument(!_.isUndefined(len), 'Must specify a length');
|
|
49
|
+
var buf = this.buf.slice(this.pos, this.pos + len);
|
|
50
|
+
this.pos = this.pos + len;
|
|
51
|
+
return buf;
|
|
52
|
+
};
|
|
53
|
+
|
|
54
|
+
BufferReader.prototype.readAll = function () {
|
|
55
|
+
var buf = this.buf.slice(this.pos, this.buf.length);
|
|
56
|
+
this.pos = this.buf.length;
|
|
57
|
+
return buf;
|
|
58
|
+
};
|
|
59
|
+
|
|
60
|
+
BufferReader.prototype.readUInt8 = function () {
|
|
61
|
+
var val = this.buf.readUInt8(this.pos);
|
|
62
|
+
this.pos = this.pos + 1;
|
|
63
|
+
return val;
|
|
64
|
+
};
|
|
65
|
+
|
|
66
|
+
BufferReader.prototype.readUInt16BE = function () {
|
|
67
|
+
var val = this.buf.readUInt16BE(this.pos);
|
|
68
|
+
this.pos = this.pos + 2;
|
|
69
|
+
return val;
|
|
70
|
+
};
|
|
71
|
+
|
|
72
|
+
BufferReader.prototype.readUInt16LE = function () {
|
|
73
|
+
var val = this.buf.readUInt16LE(this.pos);
|
|
74
|
+
this.pos = this.pos + 2;
|
|
75
|
+
return val;
|
|
76
|
+
};
|
|
77
|
+
|
|
78
|
+
BufferReader.prototype.readUInt32BE = function () {
|
|
79
|
+
var val = this.buf.readUInt32BE(this.pos);
|
|
80
|
+
this.pos = this.pos + 4;
|
|
81
|
+
return val;
|
|
82
|
+
};
|
|
83
|
+
|
|
84
|
+
BufferReader.prototype.readUInt32LE = function () {
|
|
85
|
+
var val = this.buf.readUInt32LE(this.pos);
|
|
86
|
+
this.pos = this.pos + 4;
|
|
87
|
+
return val;
|
|
88
|
+
};
|
|
89
|
+
|
|
90
|
+
BufferReader.prototype.readInt32LE = function () {
|
|
91
|
+
var val = this.buf.readInt32LE(this.pos);
|
|
92
|
+
this.pos = this.pos + 4;
|
|
93
|
+
return val;
|
|
94
|
+
};
|
|
95
|
+
|
|
96
|
+
BufferReader.prototype.readUInt64BEBN = function () {
|
|
97
|
+
var buf = this.buf.slice(this.pos, this.pos + 8);
|
|
98
|
+
var bn = BN.fromBuffer(buf);
|
|
99
|
+
this.pos = this.pos + 8;
|
|
100
|
+
return bn;
|
|
101
|
+
};
|
|
102
|
+
|
|
103
|
+
BufferReader.prototype.readUInt64LEBN = function () {
|
|
104
|
+
var second = this.buf.readUInt32LE(this.pos);
|
|
105
|
+
var first = this.buf.readUInt32LE(this.pos + 4);
|
|
106
|
+
var combined = first * 0x100000000 + second;
|
|
107
|
+
// Instantiating an instance of BN with a number is faster than with an
|
|
108
|
+
// array or string. However, the maximum safe number for a double precision
|
|
109
|
+
// floating point is 2 ^ 52 - 1 (0x1fffffffffffff), thus we can safely use
|
|
110
|
+
// non-floating point numbers less than this amount (52 bits). And in the case
|
|
111
|
+
// that the number is larger, we can instatiate an instance of BN by passing
|
|
112
|
+
// an array from the buffer (slower) and specifying the endianness.
|
|
113
|
+
var bn;
|
|
114
|
+
if (combined <= 0x1fffffffffffff) {
|
|
115
|
+
bn = new BN(combined);
|
|
116
|
+
} else {
|
|
117
|
+
var data = Array.prototype.slice.call(this.buf, this.pos, this.pos + 8);
|
|
118
|
+
bn = new BN(data, 10, 'le');
|
|
119
|
+
}
|
|
120
|
+
this.pos = this.pos + 8;
|
|
121
|
+
return bn;
|
|
122
|
+
};
|
|
123
|
+
|
|
124
|
+
BufferReader.prototype.readVarintNum = function () {
|
|
125
|
+
var first = this.readUInt8();
|
|
126
|
+
switch (first) {
|
|
127
|
+
case 0xfd:
|
|
128
|
+
return this.readUInt16LE();
|
|
129
|
+
case 0xfe:
|
|
130
|
+
return this.readUInt32LE();
|
|
131
|
+
case 0xff:
|
|
132
|
+
var bn = this.readUInt64LEBN();
|
|
133
|
+
var n = bn.toNumber();
|
|
134
|
+
if (n <= Math.pow(2, 53)) {
|
|
135
|
+
return n;
|
|
136
|
+
} else {
|
|
137
|
+
throw new Error('number too large to retain precision - use readVarintBN');
|
|
138
|
+
}
|
|
139
|
+
// break // unreachable
|
|
140
|
+
default:
|
|
141
|
+
return first;
|
|
142
|
+
}
|
|
143
|
+
};
|
|
144
|
+
|
|
145
|
+
/**
|
|
146
|
+
* reads a length prepended buffer
|
|
147
|
+
*/
|
|
148
|
+
BufferReader.prototype.readVarLengthBuffer = function () {
|
|
149
|
+
var len = this.readVarintNum();
|
|
150
|
+
var buf = this.read(len);
|
|
151
|
+
$.checkState(
|
|
152
|
+
buf.length === len,
|
|
153
|
+
'Invalid length while reading varlength buffer. ' +
|
|
154
|
+
'Expected to read: ' +
|
|
155
|
+
len +
|
|
156
|
+
' and read ' +
|
|
157
|
+
buf.length,
|
|
158
|
+
);
|
|
159
|
+
return buf;
|
|
160
|
+
};
|
|
161
|
+
|
|
162
|
+
BufferReader.prototype.readVarintBuf = function () {
|
|
163
|
+
var first = this.buf.readUInt8(this.pos);
|
|
164
|
+
switch (first) {
|
|
165
|
+
case 0xfd:
|
|
166
|
+
return this.read(1 + 2);
|
|
167
|
+
case 0xfe:
|
|
168
|
+
return this.read(1 + 4);
|
|
169
|
+
case 0xff:
|
|
170
|
+
return this.read(1 + 8);
|
|
171
|
+
default:
|
|
172
|
+
return this.read(1);
|
|
173
|
+
}
|
|
174
|
+
};
|
|
175
|
+
|
|
176
|
+
BufferReader.prototype.readVarintBN = function () {
|
|
177
|
+
var first = this.readUInt8();
|
|
178
|
+
switch (first) {
|
|
179
|
+
case 0xfd:
|
|
180
|
+
return new BN(this.readUInt16LE());
|
|
181
|
+
case 0xfe:
|
|
182
|
+
return new BN(this.readUInt32LE());
|
|
183
|
+
case 0xff:
|
|
184
|
+
return this.readUInt64LEBN();
|
|
185
|
+
default:
|
|
186
|
+
return new BN(first);
|
|
187
|
+
}
|
|
188
|
+
};
|
|
189
|
+
|
|
190
|
+
BufferReader.prototype.reverse = function () {
|
|
191
|
+
var buf = Buffer.alloc(this.buf.length);
|
|
192
|
+
for (var i = 0; i < buf.length; i++) {
|
|
193
|
+
buf[i] = this.buf[this.buf.length - 1 - i];
|
|
194
|
+
}
|
|
195
|
+
this.buf = buf;
|
|
196
|
+
return this;
|
|
197
|
+
};
|
|
198
|
+
|
|
199
|
+
BufferReader.prototype.readReverse = function (len) {
|
|
200
|
+
if (_.isUndefined(len)) {
|
|
201
|
+
len = this.buf.length;
|
|
202
|
+
}
|
|
203
|
+
var buf = this.buf.slice(this.pos, this.pos + len);
|
|
204
|
+
this.pos = this.pos + len;
|
|
205
|
+
return Buffer.from(buf).reverse();
|
|
206
|
+
};
|
|
207
|
+
|
|
208
|
+
BufferReader.prototype.remaining = function () {
|
|
209
|
+
return this.buf.length - this.pos;
|
|
210
|
+
};
|
|
211
|
+
|
|
212
|
+
module.exports = BufferReader;
|
|
@@ -0,0 +1,140 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var assert = require('assert');
|
|
4
|
+
|
|
5
|
+
const writeU8LE = require('../script/write-u8-le');
|
|
6
|
+
const writeU16LE = require('../script/write-u16-le');
|
|
7
|
+
const writeU32LE = require('../script/write-u32-le');
|
|
8
|
+
const writeI32LE = require('../script/write-i32-le');
|
|
9
|
+
const writeVarint = require('../script/write-varint');
|
|
10
|
+
|
|
11
|
+
class BufferWriter {
|
|
12
|
+
constructor(obj) {
|
|
13
|
+
if (obj) {
|
|
14
|
+
this.set(obj);
|
|
15
|
+
} else {
|
|
16
|
+
this.buffers = [];
|
|
17
|
+
this.length = 0;
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
write(buffer) {
|
|
22
|
+
this.buffers.push(buffer);
|
|
23
|
+
this.length += buffer.length;
|
|
24
|
+
return this;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
set(obj) {
|
|
28
|
+
this.buffers = obj.buffers || obj.bufs || this.buffers || [];
|
|
29
|
+
this.length = this.buffers.reduce(function (prev, buf) {
|
|
30
|
+
return prev + buf.length;
|
|
31
|
+
}, 0);
|
|
32
|
+
return this;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
concat() {
|
|
36
|
+
return this.toBuffer();
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
toBuffer() {
|
|
40
|
+
if (this.buffers.length === 1) {
|
|
41
|
+
return Buffer.from(this.buffers[0]);
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
const whole = new Uint8Array(this.length);
|
|
45
|
+
|
|
46
|
+
let offset = 0;
|
|
47
|
+
this.buffers.forEach((part) => {
|
|
48
|
+
whole.set(part, offset);
|
|
49
|
+
offset += part.length;
|
|
50
|
+
});
|
|
51
|
+
|
|
52
|
+
return Buffer.from(whole);
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
writeReverse(buf) {
|
|
56
|
+
assert(Buffer.isBuffer(buf));
|
|
57
|
+
this.write(Buffer.from(buf).reverse());
|
|
58
|
+
return this;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
writeUInt16LE(n) {
|
|
62
|
+
writeU16LE(this, n);
|
|
63
|
+
return this;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
writeUInt16BE(n) {
|
|
67
|
+
var bw = new BufferWriter();
|
|
68
|
+
bw.writeUInt16LE(n);
|
|
69
|
+
this.writeReverse(bw.toBuffer());
|
|
70
|
+
return this;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
writeUInt32LE(n) {
|
|
74
|
+
writeU32LE(this, n);
|
|
75
|
+
return this;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
writeUInt32BE(n) {
|
|
79
|
+
var bw = new BufferWriter();
|
|
80
|
+
bw.writeUInt32LE(n);
|
|
81
|
+
this.writeReverse(bw.toBuffer());
|
|
82
|
+
return this;
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
writeUInt8(n) {
|
|
86
|
+
writeU8LE(this, n);
|
|
87
|
+
return this;
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
writeUInt64LEBN(bn) {
|
|
91
|
+
var buf = bn.toBuffer({ size: 8 });
|
|
92
|
+
this.writeReverse(buf);
|
|
93
|
+
return this;
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
writeUInt64BEBN(bn) {
|
|
97
|
+
var bw = new BufferWriter();
|
|
98
|
+
bw.writeUInt64LEBN(bn);
|
|
99
|
+
this.writeReverse(bw.toBuffer());
|
|
100
|
+
return this;
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
writeVarintNum(n) {
|
|
104
|
+
writeVarint(this, n);
|
|
105
|
+
return this;
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
writeInt32LE(n) {
|
|
109
|
+
writeI32LE(this, n);
|
|
110
|
+
return this;
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
static varintBufNum(n) {
|
|
114
|
+
var bw = new BufferWriter();
|
|
115
|
+
bw.writeVarintNum(n);
|
|
116
|
+
return bw.toBuffer();
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
writeVarintBN(bn) {
|
|
120
|
+
var n = bn.toNumber();
|
|
121
|
+
if (n < 253) {
|
|
122
|
+
writeU8LE(this, n);
|
|
123
|
+
} else if (n < 0x10000) {
|
|
124
|
+
writeU8LE(this, 253);
|
|
125
|
+
writeU16LE(this, n);
|
|
126
|
+
} else if (n < 0x100000000) {
|
|
127
|
+
writeU8LE(this, 254);
|
|
128
|
+
writeU32LE(this, n);
|
|
129
|
+
} else {
|
|
130
|
+
var bw = new BufferWriter();
|
|
131
|
+
bw.writeUInt8(255);
|
|
132
|
+
bw.writeUInt64LEBN(bn);
|
|
133
|
+
var buf = bw.toBuffer();
|
|
134
|
+
this.write(buf);
|
|
135
|
+
}
|
|
136
|
+
return this;
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
module.exports = BufferWriter;
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
const decodeHex = require('./decode-hex');
|
|
2
|
+
const opcodes = require('../opcode');
|
|
3
|
+
const BufferWriter = require('./bufferwriter');
|
|
4
|
+
const writePushData = require('../script/write-push-data');
|
|
5
|
+
|
|
6
|
+
function decodeASM(script) {
|
|
7
|
+
const parts = script.split(' ');
|
|
8
|
+
const writer = new BufferWriter();
|
|
9
|
+
parts.forEach((part) => {
|
|
10
|
+
if (part in opcodes) {
|
|
11
|
+
writer.write([opcodes[part]]);
|
|
12
|
+
} else if (part === '0') {
|
|
13
|
+
writer.write([opcodes.OP_0]);
|
|
14
|
+
} else if (part === '-1') {
|
|
15
|
+
writer.write([opcodes.OP_1NEGATE]);
|
|
16
|
+
} else {
|
|
17
|
+
const buf = decodeHex(part);
|
|
18
|
+
writePushData(writer, buf);
|
|
19
|
+
}
|
|
20
|
+
});
|
|
21
|
+
return writer.toBuffer();
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
module.exports = decodeASM;
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
/* global VARIANT */
|
|
2
|
+
|
|
3
|
+
const isHex = require('./is-hex');
|
|
4
|
+
|
|
5
|
+
// Prefer our implementation of decodeHex over Buffer when we don't know the VARIANT
|
|
6
|
+
// to avoid accidentally importing the Buffer shim in the browser.
|
|
7
|
+
|
|
8
|
+
function decodeHex(hex) {
|
|
9
|
+
if (typeof hex !== 'string') throw new Error('not a string');
|
|
10
|
+
|
|
11
|
+
if (hex.startsWith('0x')) hex = hex.slice(2);
|
|
12
|
+
|
|
13
|
+
if (hex.length % 2 === 1) hex = '0' + hex;
|
|
14
|
+
|
|
15
|
+
if (!isHex(hex)) throw new Error('invalid hex string in script');
|
|
16
|
+
|
|
17
|
+
if (typeof VARIANT === 'undefined' || VARIANT === 'browser') {
|
|
18
|
+
const length = hex.length / 2;
|
|
19
|
+
const arr = new Uint8Array(length);
|
|
20
|
+
const isNaN = (x) => x !== x;
|
|
21
|
+
for (let i = 0; i < length; ++i) {
|
|
22
|
+
const byte = parseInt(hex.substr(i * 2, 2), 16);
|
|
23
|
+
if (isNaN(byte)) throw new Error('bad hex char');
|
|
24
|
+
arr[i] = byte;
|
|
25
|
+
}
|
|
26
|
+
return arr;
|
|
27
|
+
} else {
|
|
28
|
+
return Buffer.from(hex, 'hex');
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
module.exports = decodeHex;
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
function decodeScriptChunks(script) {
|
|
2
|
+
const chunks = [];
|
|
3
|
+
let i = 0;
|
|
4
|
+
let len = 0;
|
|
5
|
+
while (i < script.length) {
|
|
6
|
+
const opcodenum = script[i];
|
|
7
|
+
i += 1;
|
|
8
|
+
if (opcodenum === 0) {
|
|
9
|
+
len = opcodenum;
|
|
10
|
+
chunks.push({ opcodenum: opcodenum, len });
|
|
11
|
+
} else if (opcodenum < 76) {
|
|
12
|
+
// OP_PUSHDATA1
|
|
13
|
+
len = opcodenum;
|
|
14
|
+
chunks.push({ opcodenum: opcodenum, buf: script.slice(i, i + opcodenum), len });
|
|
15
|
+
i += opcodenum;
|
|
16
|
+
} else if (opcodenum === 76) {
|
|
17
|
+
// OP_PUSHDATA1
|
|
18
|
+
len = script[i];
|
|
19
|
+
i += 1;
|
|
20
|
+
chunks.push({ opcodenum: opcodenum, buf: script.slice(i, i + len), len });
|
|
21
|
+
i += len;
|
|
22
|
+
} else if (opcodenum === 77) {
|
|
23
|
+
// OP_PUSHDATA2
|
|
24
|
+
len = script[i] | (script[i + 1] << 8);
|
|
25
|
+
i += 2;
|
|
26
|
+
chunks.push({ opcodenum: opcodenum, buf: script.slice(i, i + len), len });
|
|
27
|
+
i += len;
|
|
28
|
+
} else if (opcodenum === 78) {
|
|
29
|
+
// OP_PUSHDATA4
|
|
30
|
+
len =
|
|
31
|
+
script[i] + script[i + 1] * 0x0100 + script[i + 2] * 0x010000 + script[i + 3] * 0x01000000;
|
|
32
|
+
i += 4;
|
|
33
|
+
chunks.push({ opcodenum: opcodenum, buf: script.slice(i, i + len), len });
|
|
34
|
+
i += len;
|
|
35
|
+
} else {
|
|
36
|
+
chunks.push({ opcodenum: opcodenum });
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
// if (i !== script.length) throw new Error('bad script')
|
|
40
|
+
return chunks;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
module.exports = decodeScriptChunks;
|