@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,325 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var BN = require('./bn');
|
|
4
|
+
var _ = require('../util/_');
|
|
5
|
+
var $ = require('../util/preconditions');
|
|
6
|
+
var JSUtil = require('../util/js');
|
|
7
|
+
|
|
8
|
+
var Signature = function Signature(r, s) {
|
|
9
|
+
if (!(this instanceof Signature)) {
|
|
10
|
+
return new Signature(r, s);
|
|
11
|
+
}
|
|
12
|
+
if (r instanceof BN) {
|
|
13
|
+
this.set({
|
|
14
|
+
r: r,
|
|
15
|
+
s: s,
|
|
16
|
+
});
|
|
17
|
+
} else if (r) {
|
|
18
|
+
var obj = r;
|
|
19
|
+
this.set(obj);
|
|
20
|
+
}
|
|
21
|
+
};
|
|
22
|
+
|
|
23
|
+
Signature.prototype.set = function (obj) {
|
|
24
|
+
this.r = obj.r || this.r || undefined;
|
|
25
|
+
this.s = obj.s || this.s || undefined;
|
|
26
|
+
|
|
27
|
+
this.i = typeof obj.i !== 'undefined' ? obj.i : this.i; // public key recovery parameter in range [0, 3]
|
|
28
|
+
this.compressed = typeof obj.compressed !== 'undefined' ? obj.compressed : this.compressed; // whether the recovered pubkey is compressed
|
|
29
|
+
this.nhashtype = obj.nhashtype || this.nhashtype || undefined;
|
|
30
|
+
return this;
|
|
31
|
+
};
|
|
32
|
+
|
|
33
|
+
Signature.fromCompact = function (buf) {
|
|
34
|
+
$.checkArgument(Buffer.isBuffer(buf), 'Argument is expected to be a Buffer');
|
|
35
|
+
|
|
36
|
+
var sig = new Signature();
|
|
37
|
+
|
|
38
|
+
var compressed = true;
|
|
39
|
+
var i = buf.slice(0, 1)[0] - 27 - 4;
|
|
40
|
+
if (i < 0) {
|
|
41
|
+
compressed = false;
|
|
42
|
+
i = i + 4;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
var b2 = buf.slice(1, 33);
|
|
46
|
+
var b3 = buf.slice(33, 65);
|
|
47
|
+
|
|
48
|
+
$.checkArgument(i === 0 || i === 1 || i === 2 || i === 3, new Error('i must be 0, 1, 2, or 3'));
|
|
49
|
+
$.checkArgument(b2.length === 32, new Error('r must be 32 bytes'));
|
|
50
|
+
$.checkArgument(b3.length === 32, new Error('s must be 32 bytes'));
|
|
51
|
+
|
|
52
|
+
sig.compressed = compressed;
|
|
53
|
+
sig.i = i;
|
|
54
|
+
sig.r = BN.fromBuffer(b2);
|
|
55
|
+
sig.s = BN.fromBuffer(b3);
|
|
56
|
+
|
|
57
|
+
return sig;
|
|
58
|
+
};
|
|
59
|
+
|
|
60
|
+
Signature.fromDER = Signature.fromBuffer = function (buf, strict) {
|
|
61
|
+
var obj = Signature.parseDER(buf, strict);
|
|
62
|
+
var sig = new Signature();
|
|
63
|
+
|
|
64
|
+
sig.r = obj.r;
|
|
65
|
+
sig.s = obj.s;
|
|
66
|
+
|
|
67
|
+
return sig;
|
|
68
|
+
};
|
|
69
|
+
|
|
70
|
+
// The format used in a tx
|
|
71
|
+
Signature.fromTxFormat = function (buf) {
|
|
72
|
+
var nhashtype = buf.readUInt8(buf.length - 1);
|
|
73
|
+
var derbuf = buf.slice(0, buf.length - 1);
|
|
74
|
+
var sig = Signature.fromDER(derbuf, false);
|
|
75
|
+
sig.nhashtype = nhashtype;
|
|
76
|
+
return sig;
|
|
77
|
+
};
|
|
78
|
+
|
|
79
|
+
Signature.fromString = function (str) {
|
|
80
|
+
var buf = Buffer.from(str, 'hex');
|
|
81
|
+
return Signature.fromDER(buf);
|
|
82
|
+
};
|
|
83
|
+
|
|
84
|
+
/**
|
|
85
|
+
* In order to mimic the non-strict DER encoding of OpenSSL, set strict = false.
|
|
86
|
+
*/
|
|
87
|
+
Signature.parseDER = function (buf, strict) {
|
|
88
|
+
$.checkArgument(Buffer.isBuffer(buf), new Error('DER formatted signature should be a buffer'));
|
|
89
|
+
if (_.isUndefined(strict)) {
|
|
90
|
+
strict = true;
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
var header = buf[0];
|
|
94
|
+
$.checkArgument(header === 0x30, new Error('Header byte should be 0x30'));
|
|
95
|
+
|
|
96
|
+
var length = buf[1];
|
|
97
|
+
var buflength = buf.slice(2).length;
|
|
98
|
+
$.checkArgument(
|
|
99
|
+
!strict || length === buflength,
|
|
100
|
+
new Error('Length byte should length of what follows'),
|
|
101
|
+
);
|
|
102
|
+
|
|
103
|
+
length = length < buflength ? length : buflength;
|
|
104
|
+
|
|
105
|
+
var rheader = buf[2 + 0];
|
|
106
|
+
$.checkArgument(rheader === 0x02, new Error('Integer byte for r should be 0x02'));
|
|
107
|
+
|
|
108
|
+
var rlength = buf[2 + 1];
|
|
109
|
+
var rbuf = buf.slice(2 + 2, 2 + 2 + rlength);
|
|
110
|
+
var r = BN.fromBuffer(rbuf);
|
|
111
|
+
var rneg = buf[2 + 1 + 1] === 0x00;
|
|
112
|
+
$.checkArgument(rlength === rbuf.length, new Error('Length of r incorrect'));
|
|
113
|
+
|
|
114
|
+
var sheader = buf[2 + 2 + rlength + 0];
|
|
115
|
+
$.checkArgument(sheader === 0x02, new Error('Integer byte for s should be 0x02'));
|
|
116
|
+
|
|
117
|
+
var slength = buf[2 + 2 + rlength + 1];
|
|
118
|
+
var sbuf = buf.slice(2 + 2 + rlength + 2, 2 + 2 + rlength + 2 + slength);
|
|
119
|
+
var s = BN.fromBuffer(sbuf);
|
|
120
|
+
var sneg = buf[2 + 2 + rlength + 2 + 2] === 0x00;
|
|
121
|
+
$.checkArgument(slength === sbuf.length, new Error('Length of s incorrect'));
|
|
122
|
+
|
|
123
|
+
var sumlength = 2 + 2 + rlength + 2 + slength;
|
|
124
|
+
$.checkArgument(length === sumlength - 2, new Error('Length of signature incorrect'));
|
|
125
|
+
|
|
126
|
+
var obj = {
|
|
127
|
+
header: header,
|
|
128
|
+
length: length,
|
|
129
|
+
rheader: rheader,
|
|
130
|
+
rlength: rlength,
|
|
131
|
+
rneg: rneg,
|
|
132
|
+
rbuf: rbuf,
|
|
133
|
+
r: r,
|
|
134
|
+
sheader: sheader,
|
|
135
|
+
slength: slength,
|
|
136
|
+
sneg: sneg,
|
|
137
|
+
sbuf: sbuf,
|
|
138
|
+
s: s,
|
|
139
|
+
};
|
|
140
|
+
|
|
141
|
+
return obj;
|
|
142
|
+
};
|
|
143
|
+
|
|
144
|
+
Signature.prototype.toCompact = function (i, compressed) {
|
|
145
|
+
i = typeof i === 'number' ? i : this.i;
|
|
146
|
+
compressed = typeof compressed === 'boolean' ? compressed : this.compressed;
|
|
147
|
+
|
|
148
|
+
if (!(i === 0 || i === 1 || i === 2 || i === 3)) {
|
|
149
|
+
throw new Error('i must be equal to 0, 1, 2, or 3');
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
var val = i + 27 + 4;
|
|
153
|
+
if (compressed === false) {
|
|
154
|
+
val = val - 4;
|
|
155
|
+
}
|
|
156
|
+
var b1 = Buffer.from([val]);
|
|
157
|
+
var b2 = this.r.toBuffer({
|
|
158
|
+
size: 32,
|
|
159
|
+
});
|
|
160
|
+
var b3 = this.s.toBuffer({
|
|
161
|
+
size: 32,
|
|
162
|
+
});
|
|
163
|
+
return Buffer.concat([b1, b2, b3]);
|
|
164
|
+
};
|
|
165
|
+
|
|
166
|
+
Signature.prototype.toBuffer = Signature.prototype.toDER = function () {
|
|
167
|
+
var rnbuf = this.r.toBuffer();
|
|
168
|
+
var snbuf = this.s.toBuffer();
|
|
169
|
+
|
|
170
|
+
var rneg = !!(rnbuf[0] & 0x80);
|
|
171
|
+
var sneg = !!(snbuf[0] & 0x80);
|
|
172
|
+
|
|
173
|
+
var rbuf = rneg ? Buffer.concat([Buffer.from([0x00]), rnbuf]) : rnbuf;
|
|
174
|
+
var sbuf = sneg ? Buffer.concat([Buffer.from([0x00]), snbuf]) : snbuf;
|
|
175
|
+
|
|
176
|
+
var rlength = rbuf.length;
|
|
177
|
+
var slength = sbuf.length;
|
|
178
|
+
var length = 2 + rlength + 2 + slength;
|
|
179
|
+
var rheader = 0x02;
|
|
180
|
+
var sheader = 0x02;
|
|
181
|
+
var header = 0x30;
|
|
182
|
+
|
|
183
|
+
var der = Buffer.concat([
|
|
184
|
+
Buffer.from([header, length, rheader, rlength]),
|
|
185
|
+
rbuf,
|
|
186
|
+
Buffer.from([sheader, slength]),
|
|
187
|
+
sbuf,
|
|
188
|
+
]);
|
|
189
|
+
return der;
|
|
190
|
+
};
|
|
191
|
+
|
|
192
|
+
Signature.prototype.toString = function () {
|
|
193
|
+
var buf = this.toDER();
|
|
194
|
+
return buf.toString('hex');
|
|
195
|
+
};
|
|
196
|
+
|
|
197
|
+
/**
|
|
198
|
+
* This function is translated from bitcoind's IsDERSignature and is used in
|
|
199
|
+
* the script interpreter. This "DER" format actually includes an extra byte,
|
|
200
|
+
* the nhashtype, at the end. It is really the tx format, not DER format.
|
|
201
|
+
*
|
|
202
|
+
* A canonical signature exists of: [30] [total len] [02] [len R] [R] [02] [len S] [S] [hashtype]
|
|
203
|
+
* Where R and S are not negative (their first byte has its highest bit not set), and not
|
|
204
|
+
* excessively padded (do not start with a 0 byte, unless an otherwise negative number follows,
|
|
205
|
+
* in which case a single 0 byte is necessary and even required).
|
|
206
|
+
*
|
|
207
|
+
* See https://bitcointalk.org/index.php?topic=8392.msg127623#msg127623
|
|
208
|
+
*/
|
|
209
|
+
Signature.isTxDER = function (buf) {
|
|
210
|
+
if (buf.length < 9) {
|
|
211
|
+
// Non-canonical signature: too short
|
|
212
|
+
return false;
|
|
213
|
+
}
|
|
214
|
+
if (buf.length > 73) {
|
|
215
|
+
// Non-canonical signature: too long
|
|
216
|
+
return false;
|
|
217
|
+
}
|
|
218
|
+
if (buf[0] !== 0x30) {
|
|
219
|
+
// Non-canonical signature: wrong type
|
|
220
|
+
return false;
|
|
221
|
+
}
|
|
222
|
+
if (buf[1] !== buf.length - 3) {
|
|
223
|
+
// Non-canonical signature: wrong length marker
|
|
224
|
+
return false;
|
|
225
|
+
}
|
|
226
|
+
var nLenR = buf[3];
|
|
227
|
+
if (5 + nLenR >= buf.length) {
|
|
228
|
+
// Non-canonical signature: S length misplaced
|
|
229
|
+
return false;
|
|
230
|
+
}
|
|
231
|
+
var nLenS = buf[5 + nLenR];
|
|
232
|
+
if (nLenR + nLenS + 7 !== buf.length) {
|
|
233
|
+
// Non-canonical signature: R+S length mismatch
|
|
234
|
+
return false;
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
var R = buf.slice(4);
|
|
238
|
+
if (buf[4 - 2] !== 0x02) {
|
|
239
|
+
// Non-canonical signature: R value type mismatch
|
|
240
|
+
return false;
|
|
241
|
+
}
|
|
242
|
+
if (nLenR === 0) {
|
|
243
|
+
// Non-canonical signature: R length is zero
|
|
244
|
+
return false;
|
|
245
|
+
}
|
|
246
|
+
if (R[0] & 0x80) {
|
|
247
|
+
// Non-canonical signature: R value negative
|
|
248
|
+
return false;
|
|
249
|
+
}
|
|
250
|
+
if (nLenR > 1 && R[0] === 0x00 && !(R[1] & 0x80)) {
|
|
251
|
+
// Non-canonical signature: R value excessively padded
|
|
252
|
+
return false;
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
var S = buf.slice(6 + nLenR);
|
|
256
|
+
if (buf[6 + nLenR - 2] !== 0x02) {
|
|
257
|
+
// Non-canonical signature: S value type mismatch
|
|
258
|
+
return false;
|
|
259
|
+
}
|
|
260
|
+
if (nLenS === 0) {
|
|
261
|
+
// Non-canonical signature: S length is zero
|
|
262
|
+
return false;
|
|
263
|
+
}
|
|
264
|
+
if (S[0] & 0x80) {
|
|
265
|
+
// Non-canonical signature: S value negative
|
|
266
|
+
return false;
|
|
267
|
+
}
|
|
268
|
+
if (nLenS > 1 && S[0] === 0x00 && !(S[1] & 0x80)) {
|
|
269
|
+
// Non-canonical signature: S value excessively padded
|
|
270
|
+
return false;
|
|
271
|
+
}
|
|
272
|
+
return true;
|
|
273
|
+
};
|
|
274
|
+
|
|
275
|
+
/**
|
|
276
|
+
* Compares to bitcoind's IsLowDERSignature
|
|
277
|
+
* See also ECDSA signature algorithm which enforces this.
|
|
278
|
+
* See also BIP 62, "low S values in signatures"
|
|
279
|
+
*/
|
|
280
|
+
Signature.prototype.hasLowS = function () {
|
|
281
|
+
if (
|
|
282
|
+
this.s.lt(new BN(1)) ||
|
|
283
|
+
this.s.gt(new BN('7FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF5D576E7357A4501DDFE92F46681B20A0', 'hex'))
|
|
284
|
+
) {
|
|
285
|
+
return false;
|
|
286
|
+
}
|
|
287
|
+
return true;
|
|
288
|
+
};
|
|
289
|
+
|
|
290
|
+
/**
|
|
291
|
+
* @returns true if the nhashtype is exactly equal to one of the standard options or combinations thereof.
|
|
292
|
+
* Translated from bitcoind's IsDefinedHashtypeSignature
|
|
293
|
+
*/
|
|
294
|
+
Signature.prototype.hasDefinedHashtype = function () {
|
|
295
|
+
if (!JSUtil.isNaturalNumber(this.nhashtype)) {
|
|
296
|
+
return false;
|
|
297
|
+
}
|
|
298
|
+
// accept with or without Signature.SIGHASH_ANYONECANPAY by ignoring the bit
|
|
299
|
+
var temp = this.nhashtype & 0x1f;
|
|
300
|
+
if (temp < Signature.SIGHASH_ALL || temp > Signature.SIGHASH_SINGLE) {
|
|
301
|
+
return false;
|
|
302
|
+
}
|
|
303
|
+
return true;
|
|
304
|
+
};
|
|
305
|
+
|
|
306
|
+
Signature.prototype.toTxFormat = function () {
|
|
307
|
+
var derbuf = this.toDER();
|
|
308
|
+
var buf = Buffer.alloc(1);
|
|
309
|
+
buf.writeUInt8(this.nhashtype, 0);
|
|
310
|
+
return Buffer.concat([derbuf, buf]);
|
|
311
|
+
};
|
|
312
|
+
|
|
313
|
+
Signature.SIGHASH_ALL = 0x01;
|
|
314
|
+
Signature.SIGHASH_NONE = 0x02;
|
|
315
|
+
Signature.SIGHASH_SINGLE = 0x03;
|
|
316
|
+
Signature.SIGHASH_ANYONECANPAY = 0x80;
|
|
317
|
+
|
|
318
|
+
Signature.ALL = Signature.SIGHASH_ALL
|
|
319
|
+
Signature.NONE = Signature.SIGHASH_NONE
|
|
320
|
+
Signature.SINGLE = Signature.SIGHASH_SINGLE
|
|
321
|
+
Signature.ANYONECANPAY_ALL = Signature.SIGHASH_ALL | Signature.SIGHASH_ANYONECANPAY
|
|
322
|
+
Signature.ANYONECANPAY_NONE = Signature.SIGHASH_NONE | Signature.SIGHASH_ANYONECANPAY
|
|
323
|
+
Signature.ANYONECANPAY_SINGLE = Signature.SIGHASH_SINGLE | Signature.SIGHASH_ANYONECANPAY
|
|
324
|
+
|
|
325
|
+
module.exports = Signature;
|
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var _ = require('../util/_');
|
|
4
|
+
var bs58 = require('bs58');
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* The alphabet for the Bitcoin-specific Base 58 encoding distinguishes between
|
|
8
|
+
* lower case L and upper case i - neither of those characters are allowed to
|
|
9
|
+
* prevent accidentaly miscopying of letters.
|
|
10
|
+
*/
|
|
11
|
+
var ALPHABET = '123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz'.split('');
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* A Base58 object can encode/decoded Base 58, which is used primarily for
|
|
15
|
+
* string-formatted Bitcoin addresses and private keys. Addresses and private
|
|
16
|
+
* keys actually use an additional checksum, and so they actually use the
|
|
17
|
+
* Base58Check class.
|
|
18
|
+
*
|
|
19
|
+
* @param {object} obj Can be a string or buffer.
|
|
20
|
+
*/
|
|
21
|
+
var Base58 = function Base58(obj) {
|
|
22
|
+
if (!(this instanceof Base58)) {
|
|
23
|
+
return new Base58(obj);
|
|
24
|
+
}
|
|
25
|
+
if (Buffer.isBuffer(obj)) {
|
|
26
|
+
var buf = obj;
|
|
27
|
+
this.fromBuffer(buf);
|
|
28
|
+
} else if (typeof obj === 'string') {
|
|
29
|
+
var str = obj;
|
|
30
|
+
this.fromString(str);
|
|
31
|
+
}
|
|
32
|
+
};
|
|
33
|
+
|
|
34
|
+
Base58.validCharacters = function validCharacters(chars) {
|
|
35
|
+
if (Buffer.isBuffer(chars)) {
|
|
36
|
+
chars = chars.toString();
|
|
37
|
+
}
|
|
38
|
+
return _.every(
|
|
39
|
+
_.map(chars, function (char) {
|
|
40
|
+
return _.includes(ALPHABET, char);
|
|
41
|
+
}),
|
|
42
|
+
);
|
|
43
|
+
};
|
|
44
|
+
|
|
45
|
+
Base58.prototype.set = function (obj) {
|
|
46
|
+
this.buf = obj.buf || this.buf || undefined;
|
|
47
|
+
return this;
|
|
48
|
+
};
|
|
49
|
+
|
|
50
|
+
/**
|
|
51
|
+
* Encode a buffer to Bsae 58.
|
|
52
|
+
*
|
|
53
|
+
* @param {Buffer} buf Any buffer to be encoded.
|
|
54
|
+
* @returns {string} A Base 58 encoded string.
|
|
55
|
+
*/
|
|
56
|
+
Base58.encode = function (buf) {
|
|
57
|
+
if (!Buffer.isBuffer(buf)) {
|
|
58
|
+
throw new Error('Input should be a buffer');
|
|
59
|
+
}
|
|
60
|
+
return bs58.encode(buf);
|
|
61
|
+
};
|
|
62
|
+
|
|
63
|
+
/**
|
|
64
|
+
* Decode a Base 58 string to a buffer.
|
|
65
|
+
*
|
|
66
|
+
* @param {string} str A Base 58 encoded string.
|
|
67
|
+
* @returns {Buffer} The decoded buffer.
|
|
68
|
+
*/
|
|
69
|
+
Base58.decode = function (str) {
|
|
70
|
+
if (typeof str !== 'string') {
|
|
71
|
+
throw new Error('Input should be a string');
|
|
72
|
+
}
|
|
73
|
+
return Buffer.from(bs58.decode(str));
|
|
74
|
+
};
|
|
75
|
+
|
|
76
|
+
Base58.prototype.fromBuffer = function (buf) {
|
|
77
|
+
this.buf = buf;
|
|
78
|
+
return this;
|
|
79
|
+
};
|
|
80
|
+
|
|
81
|
+
Base58.fromBuffer = function (buf) {
|
|
82
|
+
return new Base58().fromBuffer(buf);
|
|
83
|
+
};
|
|
84
|
+
|
|
85
|
+
Base58.fromHex = function (hex) {
|
|
86
|
+
return Base58.fromBuffer(Buffer.from(hex, 'hex'));
|
|
87
|
+
};
|
|
88
|
+
|
|
89
|
+
Base58.prototype.fromString = function (str) {
|
|
90
|
+
var buf = Base58.decode(str);
|
|
91
|
+
this.buf = buf;
|
|
92
|
+
return this;
|
|
93
|
+
};
|
|
94
|
+
|
|
95
|
+
Base58.fromString = function (str) {
|
|
96
|
+
return new Base58().fromString(str);
|
|
97
|
+
};
|
|
98
|
+
|
|
99
|
+
Base58.prototype.toBuffer = function () {
|
|
100
|
+
return this.buf;
|
|
101
|
+
};
|
|
102
|
+
|
|
103
|
+
Base58.prototype.toHex = function () {
|
|
104
|
+
return this.toBuffer().toString('hex');
|
|
105
|
+
};
|
|
106
|
+
|
|
107
|
+
Base58.prototype.toString = function () {
|
|
108
|
+
return Base58.encode(this.buf);
|
|
109
|
+
};
|
|
110
|
+
|
|
111
|
+
module.exports = Base58;
|
|
@@ -0,0 +1,121 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var _ = require('../util/_');
|
|
4
|
+
var Base58 = require('./base58');
|
|
5
|
+
var sha256sha256 = require('../crypto/hash').sha256sha256;
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* A Base58check object can encode/decodd Base 58, which is used primarily for
|
|
9
|
+
* string-formatted Bitcoin addresses and private keys. This is the same as
|
|
10
|
+
* Base58, except that it includes a checksum to prevent accidental mistypings.
|
|
11
|
+
*
|
|
12
|
+
* @param {object} obj Can be a string or buffer.
|
|
13
|
+
*/
|
|
14
|
+
var Base58Check = function Base58Check(obj) {
|
|
15
|
+
if (!(this instanceof Base58Check)) {
|
|
16
|
+
return new Base58Check(obj);
|
|
17
|
+
}
|
|
18
|
+
if (Buffer.isBuffer(obj)) {
|
|
19
|
+
var buf = obj;
|
|
20
|
+
this.fromBuffer(buf);
|
|
21
|
+
} else if (typeof obj === 'string') {
|
|
22
|
+
var str = obj;
|
|
23
|
+
this.fromString(str);
|
|
24
|
+
}
|
|
25
|
+
};
|
|
26
|
+
|
|
27
|
+
Base58Check.prototype.set = function (obj) {
|
|
28
|
+
this.buf = obj.buf || this.buf || undefined;
|
|
29
|
+
return this;
|
|
30
|
+
};
|
|
31
|
+
|
|
32
|
+
Base58Check.validChecksum = function validChecksum(data, checksum) {
|
|
33
|
+
if (_.isString(data)) {
|
|
34
|
+
data = Buffer.from(Base58.decode(data));
|
|
35
|
+
}
|
|
36
|
+
if (_.isString(checksum)) {
|
|
37
|
+
checksum = Buffer.from(Base58.decode(checksum));
|
|
38
|
+
}
|
|
39
|
+
if (!checksum) {
|
|
40
|
+
checksum = data.slice(-4);
|
|
41
|
+
data = data.slice(0, -4);
|
|
42
|
+
}
|
|
43
|
+
return Base58Check.checksum(data).toString('hex') === checksum.toString('hex');
|
|
44
|
+
};
|
|
45
|
+
|
|
46
|
+
Base58Check.decode = function (s) {
|
|
47
|
+
if (typeof s !== 'string') {
|
|
48
|
+
throw new Error('Input must be a string');
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
var buf = Buffer.from(Base58.decode(s));
|
|
52
|
+
|
|
53
|
+
if (buf.length < 4) {
|
|
54
|
+
throw new Error('Input string too short');
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
var data = buf.slice(0, -4);
|
|
58
|
+
var csum = buf.slice(-4);
|
|
59
|
+
|
|
60
|
+
var hash = sha256sha256(data);
|
|
61
|
+
var hash4 = hash.slice(0, 4);
|
|
62
|
+
|
|
63
|
+
if (csum.toString('hex') !== hash4.toString('hex')) {
|
|
64
|
+
throw new Error('Checksum mismatch');
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
return data;
|
|
68
|
+
};
|
|
69
|
+
|
|
70
|
+
Base58Check.checksum = function (buffer) {
|
|
71
|
+
return sha256sha256(buffer).slice(0, 4);
|
|
72
|
+
};
|
|
73
|
+
|
|
74
|
+
Base58Check.encode = function (buf) {
|
|
75
|
+
if (!Buffer.isBuffer(buf)) {
|
|
76
|
+
throw new Error('Input must be a buffer');
|
|
77
|
+
}
|
|
78
|
+
var checkedBuf = Buffer.alloc(buf.length + 4);
|
|
79
|
+
var hash = Base58Check.checksum(buf);
|
|
80
|
+
buf.copy(checkedBuf);
|
|
81
|
+
hash.copy(checkedBuf, buf.length);
|
|
82
|
+
return Base58.encode(checkedBuf);
|
|
83
|
+
};
|
|
84
|
+
|
|
85
|
+
Base58Check.prototype.fromBuffer = function (buf) {
|
|
86
|
+
this.buf = buf;
|
|
87
|
+
return this;
|
|
88
|
+
};
|
|
89
|
+
|
|
90
|
+
Base58Check.fromBuffer = function (buf) {
|
|
91
|
+
return new Base58Check().fromBuffer(buf);
|
|
92
|
+
};
|
|
93
|
+
|
|
94
|
+
Base58Check.fromHex = function (hex) {
|
|
95
|
+
return Base58Check.fromBuffer(Buffer.from(hex, 'hex'));
|
|
96
|
+
};
|
|
97
|
+
|
|
98
|
+
Base58Check.prototype.fromString = function (str) {
|
|
99
|
+
var buf = Base58Check.decode(str);
|
|
100
|
+
this.buf = buf;
|
|
101
|
+
return this;
|
|
102
|
+
};
|
|
103
|
+
|
|
104
|
+
Base58Check.fromString = function (str) {
|
|
105
|
+
var buf = Base58Check.decode(str);
|
|
106
|
+
return new Base58(buf);
|
|
107
|
+
};
|
|
108
|
+
|
|
109
|
+
Base58Check.prototype.toBuffer = function () {
|
|
110
|
+
return this.buf;
|
|
111
|
+
};
|
|
112
|
+
|
|
113
|
+
Base58Check.prototype.toHex = function () {
|
|
114
|
+
return this.toBuffer().toString('hex');
|
|
115
|
+
};
|
|
116
|
+
|
|
117
|
+
Base58Check.prototype.toString = function () {
|
|
118
|
+
return Base58Check.encode(this.buf);
|
|
119
|
+
};
|
|
120
|
+
|
|
121
|
+
module.exports = Base58Check;
|