xpi-ts 0.2.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/LICENSE +21 -0
- package/README.md +516 -0
- package/dist/index.d.ts +9 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +8 -0
- package/dist/lib/bitcore/address.d.ts +66 -0
- package/dist/lib/bitcore/address.d.ts.map +1 -0
- package/dist/lib/bitcore/address.js +407 -0
- package/dist/lib/bitcore/block/block.d.ts +57 -0
- package/dist/lib/bitcore/block/block.d.ts.map +1 -0
- package/dist/lib/bitcore/block/block.js +233 -0
- package/dist/lib/bitcore/block/blockheader.d.ts +82 -0
- package/dist/lib/bitcore/block/blockheader.d.ts.map +1 -0
- package/dist/lib/bitcore/block/blockheader.js +323 -0
- package/dist/lib/bitcore/block/index.d.ts +5 -0
- package/dist/lib/bitcore/block/index.d.ts.map +1 -0
- package/dist/lib/bitcore/block/index.js +2 -0
- package/dist/lib/bitcore/chunk.d.ts +22 -0
- package/dist/lib/bitcore/chunk.d.ts.map +1 -0
- package/dist/lib/bitcore/chunk.js +46 -0
- package/dist/lib/bitcore/crypto/bn.d.ts +53 -0
- package/dist/lib/bitcore/crypto/bn.d.ts.map +1 -0
- package/dist/lib/bitcore/crypto/bn.js +238 -0
- package/dist/lib/bitcore/crypto/ecdsa.d.ts +46 -0
- package/dist/lib/bitcore/crypto/ecdsa.d.ts.map +1 -0
- package/dist/lib/bitcore/crypto/ecdsa.js +247 -0
- package/dist/lib/bitcore/crypto/hash.d.ts +16 -0
- package/dist/lib/bitcore/crypto/hash.d.ts.map +1 -0
- package/dist/lib/bitcore/crypto/hash.js +87 -0
- package/dist/lib/bitcore/crypto/index.d.ts +9 -0
- package/dist/lib/bitcore/crypto/index.d.ts.map +1 -0
- package/dist/lib/bitcore/crypto/index.js +8 -0
- package/dist/lib/bitcore/crypto/musig2.d.ts +40 -0
- package/dist/lib/bitcore/crypto/musig2.d.ts.map +1 -0
- package/dist/lib/bitcore/crypto/musig2.js +236 -0
- package/dist/lib/bitcore/crypto/point.d.ts +20 -0
- package/dist/lib/bitcore/crypto/point.d.ts.map +1 -0
- package/dist/lib/bitcore/crypto/point.js +133 -0
- package/dist/lib/bitcore/crypto/random.d.ts +7 -0
- package/dist/lib/bitcore/crypto/random.d.ts.map +1 -0
- package/dist/lib/bitcore/crypto/random.js +30 -0
- package/dist/lib/bitcore/crypto/schnorr.d.ts +40 -0
- package/dist/lib/bitcore/crypto/schnorr.d.ts.map +1 -0
- package/dist/lib/bitcore/crypto/schnorr.js +185 -0
- package/dist/lib/bitcore/crypto/signature.d.ts +53 -0
- package/dist/lib/bitcore/crypto/signature.d.ts.map +1 -0
- package/dist/lib/bitcore/crypto/signature.js +300 -0
- package/dist/lib/bitcore/crypto/sigtype.d.ts +5 -0
- package/dist/lib/bitcore/crypto/sigtype.d.ts.map +1 -0
- package/dist/lib/bitcore/crypto/sigtype.js +18 -0
- package/dist/lib/bitcore/encoding/base58.d.ts +16 -0
- package/dist/lib/bitcore/encoding/base58.d.ts.map +1 -0
- package/dist/lib/bitcore/encoding/base58.js +55 -0
- package/dist/lib/bitcore/encoding/base58check.d.ts +9 -0
- package/dist/lib/bitcore/encoding/base58check.d.ts.map +1 -0
- package/dist/lib/bitcore/encoding/base58check.js +82 -0
- package/dist/lib/bitcore/encoding/bufferreader.d.ts +34 -0
- package/dist/lib/bitcore/encoding/bufferreader.d.ts.map +1 -0
- package/dist/lib/bitcore/encoding/bufferreader.js +198 -0
- package/dist/lib/bitcore/encoding/bufferwriter.d.ts +36 -0
- package/dist/lib/bitcore/encoding/bufferwriter.d.ts.map +1 -0
- package/dist/lib/bitcore/encoding/bufferwriter.js +189 -0
- package/dist/lib/bitcore/encoding/varint.d.ts +20 -0
- package/dist/lib/bitcore/encoding/varint.d.ts.map +1 -0
- package/dist/lib/bitcore/encoding/varint.js +61 -0
- package/dist/lib/bitcore/errors.d.ts +28 -0
- package/dist/lib/bitcore/errors.d.ts.map +1 -0
- package/dist/lib/bitcore/errors.js +325 -0
- package/dist/lib/bitcore/hdprivatekey.d.ts +78 -0
- package/dist/lib/bitcore/hdprivatekey.d.ts.map +1 -0
- package/dist/lib/bitcore/hdprivatekey.js +381 -0
- package/dist/lib/bitcore/hdpublickey.d.ts +98 -0
- package/dist/lib/bitcore/hdpublickey.d.ts.map +1 -0
- package/dist/lib/bitcore/hdpublickey.js +416 -0
- package/dist/lib/bitcore/index.d.ts +60 -0
- package/dist/lib/bitcore/index.d.ts.map +1 -0
- package/dist/lib/bitcore/index.js +44 -0
- package/dist/lib/bitcore/message.d.ts +23 -0
- package/dist/lib/bitcore/message.d.ts.map +1 -0
- package/dist/lib/bitcore/message.js +112 -0
- package/dist/lib/bitcore/mnemonic/errors.d.ts +7 -0
- package/dist/lib/bitcore/mnemonic/errors.d.ts.map +1 -0
- package/dist/lib/bitcore/mnemonic/errors.js +20 -0
- package/dist/lib/bitcore/mnemonic/index.d.ts +5 -0
- package/dist/lib/bitcore/mnemonic/index.d.ts.map +1 -0
- package/dist/lib/bitcore/mnemonic/index.js +4 -0
- package/dist/lib/bitcore/mnemonic/mnemonic.d.ts +23 -0
- package/dist/lib/bitcore/mnemonic/mnemonic.d.ts.map +1 -0
- package/dist/lib/bitcore/mnemonic/mnemonic.js +164 -0
- package/dist/lib/bitcore/mnemonic/pbkdf2.d.ts +2 -0
- package/dist/lib/bitcore/mnemonic/pbkdf2.d.ts.map +1 -0
- package/dist/lib/bitcore/mnemonic/pbkdf2.js +25 -0
- package/dist/lib/bitcore/mnemonic/words/english.d.ts +2 -0
- package/dist/lib/bitcore/mnemonic/words/english.d.ts.map +1 -0
- package/dist/lib/bitcore/mnemonic/words/english.js +2050 -0
- package/dist/lib/bitcore/mnemonic/words/index.d.ts +4 -0
- package/dist/lib/bitcore/mnemonic/words/index.d.ts.map +1 -0
- package/dist/lib/bitcore/mnemonic/words/index.js +4 -0
- package/dist/lib/bitcore/musig2/index.d.ts +3 -0
- package/dist/lib/bitcore/musig2/index.d.ts.map +1 -0
- package/dist/lib/bitcore/musig2/index.js +2 -0
- package/dist/lib/bitcore/musig2/session.d.ts +79 -0
- package/dist/lib/bitcore/musig2/session.d.ts.map +1 -0
- package/dist/lib/bitcore/musig2/session.js +346 -0
- package/dist/lib/bitcore/musig2/signer.d.ts +61 -0
- package/dist/lib/bitcore/musig2/signer.d.ts.map +1 -0
- package/dist/lib/bitcore/musig2/signer.js +146 -0
- package/dist/lib/bitcore/networks.d.ts +53 -0
- package/dist/lib/bitcore/networks.d.ts.map +1 -0
- package/dist/lib/bitcore/networks.js +150 -0
- package/dist/lib/bitcore/opcode.d.ts +250 -0
- package/dist/lib/bitcore/opcode.d.ts.map +1 -0
- package/dist/lib/bitcore/opcode.js +270 -0
- package/dist/lib/bitcore/privatekey.d.ts +56 -0
- package/dist/lib/bitcore/privatekey.d.ts.map +1 -0
- package/dist/lib/bitcore/privatekey.js +237 -0
- package/dist/lib/bitcore/publickey.d.ts +59 -0
- package/dist/lib/bitcore/publickey.d.ts.map +1 -0
- package/dist/lib/bitcore/publickey.js +263 -0
- package/dist/lib/bitcore/script/interpreter.d.ts +98 -0
- package/dist/lib/bitcore/script/interpreter.d.ts.map +1 -0
- package/dist/lib/bitcore/script/interpreter.js +1704 -0
- package/dist/lib/bitcore/script.d.ts +111 -0
- package/dist/lib/bitcore/script.d.ts.map +1 -0
- package/dist/lib/bitcore/script.js +1112 -0
- package/dist/lib/bitcore/taproot/musig2.d.ts +29 -0
- package/dist/lib/bitcore/taproot/musig2.d.ts.map +1 -0
- package/dist/lib/bitcore/taproot/musig2.js +104 -0
- package/dist/lib/bitcore/taproot/nft.d.ts +164 -0
- package/dist/lib/bitcore/taproot/nft.d.ts.map +1 -0
- package/dist/lib/bitcore/taproot/nft.js +407 -0
- package/dist/lib/bitcore/taproot.d.ts +65 -0
- package/dist/lib/bitcore/taproot.d.ts.map +1 -0
- package/dist/lib/bitcore/taproot.js +288 -0
- package/dist/lib/bitcore/transaction/index.d.ts +12 -0
- package/dist/lib/bitcore/transaction/index.d.ts.map +1 -0
- package/dist/lib/bitcore/transaction/index.js +6 -0
- package/dist/lib/bitcore/transaction/input.d.ts +202 -0
- package/dist/lib/bitcore/transaction/input.d.ts.map +1 -0
- package/dist/lib/bitcore/transaction/input.js +911 -0
- package/dist/lib/bitcore/transaction/output.d.ts +48 -0
- package/dist/lib/bitcore/transaction/output.d.ts.map +1 -0
- package/dist/lib/bitcore/transaction/output.js +231 -0
- package/dist/lib/bitcore/transaction/sighash.d.ts +32 -0
- package/dist/lib/bitcore/transaction/sighash.d.ts.map +1 -0
- package/dist/lib/bitcore/transaction/sighash.js +335 -0
- package/dist/lib/bitcore/transaction/signature.d.ts +36 -0
- package/dist/lib/bitcore/transaction/signature.d.ts.map +1 -0
- package/dist/lib/bitcore/transaction/signature.js +130 -0
- package/dist/lib/bitcore/transaction/transaction.d.ts +164 -0
- package/dist/lib/bitcore/transaction/transaction.d.ts.map +1 -0
- package/dist/lib/bitcore/transaction/transaction.js +1016 -0
- package/dist/lib/bitcore/transaction/unspentoutput.d.ts +58 -0
- package/dist/lib/bitcore/transaction/unspentoutput.d.ts.map +1 -0
- package/dist/lib/bitcore/transaction/unspentoutput.js +167 -0
- package/dist/lib/bitcore/unit.d.ts +44 -0
- package/dist/lib/bitcore/unit.d.ts.map +1 -0
- package/dist/lib/bitcore/unit.js +106 -0
- package/dist/lib/bitcore/uri.d.ts +29 -0
- package/dist/lib/bitcore/uri.d.ts.map +1 -0
- package/dist/lib/bitcore/uri.js +163 -0
- package/dist/lib/bitcore/util/base32.d.ts +5 -0
- package/dist/lib/bitcore/util/base32.d.ts.map +1 -0
- package/dist/lib/bitcore/util/base32.js +58 -0
- package/dist/lib/bitcore/util/buffer.d.ts +18 -0
- package/dist/lib/bitcore/util/buffer.d.ts.map +1 -0
- package/dist/lib/bitcore/util/buffer.js +76 -0
- package/dist/lib/bitcore/util/convertBits.d.ts +2 -0
- package/dist/lib/bitcore/util/convertBits.d.ts.map +1 -0
- package/dist/lib/bitcore/util/convertBits.js +26 -0
- package/dist/lib/bitcore/util/js.d.ts +9 -0
- package/dist/lib/bitcore/util/js.d.ts.map +1 -0
- package/dist/lib/bitcore/util/js.js +45 -0
- package/dist/lib/bitcore/util/preconditions.d.ts +6 -0
- package/dist/lib/bitcore/util/preconditions.d.ts.map +1 -0
- package/dist/lib/bitcore/util/preconditions.js +31 -0
- package/dist/lib/bitcore/util.d.ts +14 -0
- package/dist/lib/bitcore/util.d.ts.map +1 -0
- package/dist/lib/bitcore/util.js +13 -0
- package/dist/lib/bitcore/xaddress.d.ts +45 -0
- package/dist/lib/bitcore/xaddress.d.ts.map +1 -0
- package/dist/lib/bitcore/xaddress.js +279 -0
- package/dist/lib/rank/api.d.ts +75 -0
- package/dist/lib/rank/api.d.ts.map +1 -0
- package/dist/lib/rank/api.js +4 -0
- package/dist/lib/rank/index.d.ts +127 -0
- package/dist/lib/rank/index.d.ts.map +1 -0
- package/dist/lib/rank/index.js +421 -0
- package/dist/lib/rank/opcode.d.ts +23 -0
- package/dist/lib/rank/opcode.d.ts.map +1 -0
- package/dist/lib/rank/opcode.js +23 -0
- package/dist/lib/rank/script.d.ts +2 -0
- package/dist/lib/rank/script.d.ts.map +1 -0
- package/dist/lib/rank/script.js +7 -0
- package/dist/lib/rank/transaction.d.ts +3 -0
- package/dist/lib/rank/transaction.d.ts.map +1 -0
- package/dist/lib/rank/transaction.js +12 -0
- package/dist/lib/rpc.d.ts +136 -0
- package/dist/lib/rpc.d.ts.map +1 -0
- package/dist/lib/rpc.js +62 -0
- package/dist/utils/constants.d.ts +18 -0
- package/dist/utils/constants.d.ts.map +1 -0
- package/dist/utils/constants.js +20 -0
- package/dist/utils/env.d.ts +3 -0
- package/dist/utils/env.d.ts.map +1 -0
- package/dist/utils/env.js +8 -0
- package/dist/utils/string.d.ts +11 -0
- package/dist/utils/string.d.ts.map +1 -0
- package/dist/utils/string.js +47 -0
- package/dist/utils/types.d.ts +2 -0
- package/dist/utils/types.d.ts.map +1 -0
- package/dist/utils/types.js +1 -0
- package/dist/utils/wallet.d.ts +12 -0
- package/dist/utils/wallet.d.ts.map +1 -0
- package/dist/utils/wallet.js +28 -0
- package/package.json +91 -0
|
@@ -0,0 +1,238 @@
|
|
|
1
|
+
import BN from 'bn.js';
|
|
2
|
+
class BNWrapper {
|
|
3
|
+
_bn;
|
|
4
|
+
static Zero = new BNWrapper(0);
|
|
5
|
+
static One = new BNWrapper(1);
|
|
6
|
+
static Minus1 = new BNWrapper(-1);
|
|
7
|
+
constructor(n, base) {
|
|
8
|
+
if (n instanceof BN) {
|
|
9
|
+
this._bn = n.clone();
|
|
10
|
+
}
|
|
11
|
+
else if (Buffer.isBuffer(n)) {
|
|
12
|
+
if (base === 'le' || base === 'be') {
|
|
13
|
+
this._bn = new BN(n, undefined, base);
|
|
14
|
+
}
|
|
15
|
+
else {
|
|
16
|
+
this._bn = new BN(n, base || 'hex');
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
else {
|
|
20
|
+
const validBase = base === 'hex' || typeof base === 'number' ? base : undefined;
|
|
21
|
+
this._bn = new BN(n, validBase);
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
static fromString(str, base) {
|
|
25
|
+
return new BNWrapper(str, base);
|
|
26
|
+
}
|
|
27
|
+
static fromBuffer(buf, opts) {
|
|
28
|
+
let buffer = buf;
|
|
29
|
+
if (opts?.size) {
|
|
30
|
+
buffer = Buffer.alloc(opts.size);
|
|
31
|
+
buf.copy(buffer, opts.size - buf.length);
|
|
32
|
+
}
|
|
33
|
+
if (opts?.endian === 'little') {
|
|
34
|
+
buffer = BNWrapper.reversebuf(buffer);
|
|
35
|
+
}
|
|
36
|
+
return new BNWrapper(buffer, 'hex');
|
|
37
|
+
}
|
|
38
|
+
static fromSM(buf, opts) {
|
|
39
|
+
if (buf.length === 0) {
|
|
40
|
+
return BNWrapper.fromBuffer(Buffer.from([0]));
|
|
41
|
+
}
|
|
42
|
+
let endian = 'big';
|
|
43
|
+
if (opts) {
|
|
44
|
+
endian = opts.endian || 'big';
|
|
45
|
+
}
|
|
46
|
+
let buffer = buf;
|
|
47
|
+
if (endian === 'little') {
|
|
48
|
+
buffer = BNWrapper.reversebuf(buf);
|
|
49
|
+
}
|
|
50
|
+
let result;
|
|
51
|
+
if (buffer[0] & 0x80) {
|
|
52
|
+
buffer[0] = buffer[0] & 0x7f;
|
|
53
|
+
result = BNWrapper.fromBuffer(buffer);
|
|
54
|
+
result = result.neg();
|
|
55
|
+
}
|
|
56
|
+
else {
|
|
57
|
+
result = BNWrapper.fromBuffer(buffer);
|
|
58
|
+
}
|
|
59
|
+
return result;
|
|
60
|
+
}
|
|
61
|
+
static fromScriptNumBuffer(buf, fRequireMinimal, size) {
|
|
62
|
+
const nMaxNumSize = size || 4;
|
|
63
|
+
if (buf.length > nMaxNumSize) {
|
|
64
|
+
throw new Error('script number overflow');
|
|
65
|
+
}
|
|
66
|
+
if (fRequireMinimal && buf.length > 0) {
|
|
67
|
+
if ((buf[buf.length - 1] & 0x7f) === 0) {
|
|
68
|
+
if (buf.length <= 1 || (buf[buf.length - 2] & 0x80) === 0) {
|
|
69
|
+
throw new Error('non-minimally encoded script number');
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
return BNWrapper.fromSM(buf, { endian: 'little' });
|
|
74
|
+
}
|
|
75
|
+
static reversebuf(buf) {
|
|
76
|
+
const buf2 = Buffer.alloc(buf.length);
|
|
77
|
+
for (let i = 0; i < buf.length; i++) {
|
|
78
|
+
buf2[i] = buf[buf.length - 1 - i];
|
|
79
|
+
}
|
|
80
|
+
return buf2;
|
|
81
|
+
}
|
|
82
|
+
static trim(buf, natlen) {
|
|
83
|
+
return buf.subarray(natlen - buf.length, buf.length);
|
|
84
|
+
}
|
|
85
|
+
static pad(buf, natlen, size) {
|
|
86
|
+
const rbuf = Buffer.alloc(size);
|
|
87
|
+
for (let i = 0; i < buf.length; i++) {
|
|
88
|
+
rbuf[rbuf.length - 1 - i] = buf[buf.length - 1 - i];
|
|
89
|
+
}
|
|
90
|
+
for (let i = 0; i < size - natlen; i++) {
|
|
91
|
+
rbuf[i] = 0;
|
|
92
|
+
}
|
|
93
|
+
return rbuf;
|
|
94
|
+
}
|
|
95
|
+
static fromNumber(n) {
|
|
96
|
+
return new BNWrapper(n);
|
|
97
|
+
}
|
|
98
|
+
toNumber() {
|
|
99
|
+
return parseInt(this.toString(10), 10);
|
|
100
|
+
}
|
|
101
|
+
add(r) {
|
|
102
|
+
return new BNWrapper(this._bn.add(r._bn));
|
|
103
|
+
}
|
|
104
|
+
mod(r) {
|
|
105
|
+
return new BNWrapper(this._bn.mod(r._bn));
|
|
106
|
+
}
|
|
107
|
+
umod(r) {
|
|
108
|
+
return new BNWrapper(this._bn.umod(r._bn));
|
|
109
|
+
}
|
|
110
|
+
get bn() {
|
|
111
|
+
return this._bn;
|
|
112
|
+
}
|
|
113
|
+
toString(base) {
|
|
114
|
+
return this._bn.toString(base);
|
|
115
|
+
}
|
|
116
|
+
toBuffer(opts) {
|
|
117
|
+
let buf;
|
|
118
|
+
if (opts && opts.size) {
|
|
119
|
+
buf = this._bn.toArrayLike(Buffer, opts.endian === 'little' ? 'le' : 'be');
|
|
120
|
+
if (buf.length === opts.size) {
|
|
121
|
+
}
|
|
122
|
+
else if (buf.length > opts.size) {
|
|
123
|
+
buf = buf.subarray(buf.length - opts.size);
|
|
124
|
+
}
|
|
125
|
+
else if (buf.length < opts.size) {
|
|
126
|
+
const padded = Buffer.alloc(opts.size);
|
|
127
|
+
buf.copy(padded, opts.size - buf.length);
|
|
128
|
+
buf = padded;
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
else {
|
|
132
|
+
buf = this._bn.toArrayLike(Buffer, opts?.endian === 'little' ? 'le' : 'be');
|
|
133
|
+
}
|
|
134
|
+
return buf;
|
|
135
|
+
}
|
|
136
|
+
toArrayLike(ArrayType, endian, length) {
|
|
137
|
+
return this._bn.toArrayLike(ArrayType, endian || 'be', length);
|
|
138
|
+
}
|
|
139
|
+
mul(r) {
|
|
140
|
+
return new BNWrapper(this._bn.mul(r._bn));
|
|
141
|
+
}
|
|
142
|
+
sub(r) {
|
|
143
|
+
return new BNWrapper(this._bn.sub(r._bn));
|
|
144
|
+
}
|
|
145
|
+
pow(r) {
|
|
146
|
+
return new BNWrapper(this._bn.pow(r._bn));
|
|
147
|
+
}
|
|
148
|
+
div(r) {
|
|
149
|
+
return new BNWrapper(this._bn.div(r._bn));
|
|
150
|
+
}
|
|
151
|
+
modPow(exponent, modulus) {
|
|
152
|
+
const red = BN.red(modulus._bn);
|
|
153
|
+
const base = this._bn.toRed(red);
|
|
154
|
+
const result = base.redPow(exponent._bn);
|
|
155
|
+
return new BNWrapper(result.fromRed());
|
|
156
|
+
}
|
|
157
|
+
and(other) {
|
|
158
|
+
const otherBN = other instanceof BNWrapper ? other._bn : new BN(other);
|
|
159
|
+
return new BNWrapper(this._bn.and(otherBN));
|
|
160
|
+
}
|
|
161
|
+
neg() {
|
|
162
|
+
return new BNWrapper(this._bn.neg());
|
|
163
|
+
}
|
|
164
|
+
invm(r) {
|
|
165
|
+
return new BNWrapper(this._bn.invm(r._bn));
|
|
166
|
+
}
|
|
167
|
+
eq(other) {
|
|
168
|
+
return this._bn.eq(other._bn);
|
|
169
|
+
}
|
|
170
|
+
lt(other) {
|
|
171
|
+
return this._bn.lt(other._bn);
|
|
172
|
+
}
|
|
173
|
+
gt(other) {
|
|
174
|
+
return this._bn.gt(other._bn);
|
|
175
|
+
}
|
|
176
|
+
lte(other) {
|
|
177
|
+
return this._bn.lte(other._bn);
|
|
178
|
+
}
|
|
179
|
+
gte(other) {
|
|
180
|
+
return this._bn.gte(other._bn);
|
|
181
|
+
}
|
|
182
|
+
isZero() {
|
|
183
|
+
return this._bn.isZero();
|
|
184
|
+
}
|
|
185
|
+
isNeg() {
|
|
186
|
+
return this._bn.isNeg();
|
|
187
|
+
}
|
|
188
|
+
isOdd() {
|
|
189
|
+
return this._bn.isOdd();
|
|
190
|
+
}
|
|
191
|
+
isEven() {
|
|
192
|
+
return this._bn.isEven();
|
|
193
|
+
}
|
|
194
|
+
toSMBigEndian() {
|
|
195
|
+
let buf;
|
|
196
|
+
if (this.cmp(BNWrapper.Zero) === -1) {
|
|
197
|
+
buf = this.neg().toBuffer();
|
|
198
|
+
if (buf[0] & 0x80) {
|
|
199
|
+
buf = Buffer.concat([Buffer.from([0x80]), buf]);
|
|
200
|
+
}
|
|
201
|
+
else {
|
|
202
|
+
buf[0] = buf[0] | 0x80;
|
|
203
|
+
}
|
|
204
|
+
}
|
|
205
|
+
else {
|
|
206
|
+
buf = this.toBuffer();
|
|
207
|
+
if (buf[0] & 0x80) {
|
|
208
|
+
buf = Buffer.concat([Buffer.from([0x00]), buf]);
|
|
209
|
+
}
|
|
210
|
+
}
|
|
211
|
+
if (buf.length === 1 && buf[0] === 0) {
|
|
212
|
+
buf = Buffer.from([]);
|
|
213
|
+
}
|
|
214
|
+
return buf;
|
|
215
|
+
}
|
|
216
|
+
toSM(opts) {
|
|
217
|
+
const endian = opts ? opts.endian : 'big';
|
|
218
|
+
let buf = this.toSMBigEndian();
|
|
219
|
+
if (endian === 'little') {
|
|
220
|
+
buf = BNWrapper.reversebuf(buf);
|
|
221
|
+
}
|
|
222
|
+
return buf;
|
|
223
|
+
}
|
|
224
|
+
toScriptNumBuffer() {
|
|
225
|
+
return this.toSM({ endian: 'little' });
|
|
226
|
+
}
|
|
227
|
+
cmp(other) {
|
|
228
|
+
return this._bn.cmp(other._bn);
|
|
229
|
+
}
|
|
230
|
+
copy(other) {
|
|
231
|
+
this._bn = other._bn.clone();
|
|
232
|
+
return this;
|
|
233
|
+
}
|
|
234
|
+
clone() {
|
|
235
|
+
return new BNWrapper(this._bn.clone());
|
|
236
|
+
}
|
|
237
|
+
}
|
|
238
|
+
export { BNWrapper as BN };
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import { BN } from './bn.js';
|
|
2
|
+
import { Signature } from './signature.js';
|
|
3
|
+
import { PrivateKey } from '../privatekey.js';
|
|
4
|
+
import { PublicKey } from '../publickey.js';
|
|
5
|
+
export interface ECDSAData {
|
|
6
|
+
hashbuf?: Buffer;
|
|
7
|
+
endian?: 'little' | 'big';
|
|
8
|
+
privkey?: PrivateKey;
|
|
9
|
+
pubkey?: PublicKey;
|
|
10
|
+
sig?: Signature;
|
|
11
|
+
k?: BN;
|
|
12
|
+
verified?: boolean;
|
|
13
|
+
}
|
|
14
|
+
export declare class ECDSA {
|
|
15
|
+
hashbuf: Buffer;
|
|
16
|
+
endian?: 'little' | 'big';
|
|
17
|
+
privkey: PrivateKey;
|
|
18
|
+
pubkey: PublicKey;
|
|
19
|
+
sig: Signature;
|
|
20
|
+
k: BN;
|
|
21
|
+
verified: boolean;
|
|
22
|
+
constructor(obj?: ECDSAData);
|
|
23
|
+
static create(obj?: ECDSAData): ECDSA;
|
|
24
|
+
set(obj: ECDSAData): ECDSA;
|
|
25
|
+
privkey2pubkey(): ECDSA;
|
|
26
|
+
calci(): ECDSA;
|
|
27
|
+
static fromString(str: string): ECDSA;
|
|
28
|
+
randomK(): ECDSA;
|
|
29
|
+
deterministicK(badrs?: number): ECDSA;
|
|
30
|
+
toPublicKey(): PublicKey;
|
|
31
|
+
sigError(): string | false;
|
|
32
|
+
static toLowS(s: BN): BN;
|
|
33
|
+
_findSignature(d: BN, e: BN): {
|
|
34
|
+
s: BN;
|
|
35
|
+
r: BN;
|
|
36
|
+
compressed?: boolean;
|
|
37
|
+
};
|
|
38
|
+
sign(): ECDSA;
|
|
39
|
+
signRandomK(): ECDSA;
|
|
40
|
+
toString(): string;
|
|
41
|
+
verify(): ECDSA;
|
|
42
|
+
static sign(hashbuf: Buffer, privkey: PrivateKey, endian?: 'little' | 'big'): Signature;
|
|
43
|
+
static verify(hashbuf: Buffer, sig: Signature, pubkey: PublicKey, endian?: 'little' | 'big'): boolean;
|
|
44
|
+
private reverseBuffer;
|
|
45
|
+
}
|
|
46
|
+
//# sourceMappingURL=ecdsa.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"ecdsa.d.ts","sourceRoot":"","sources":["../../../../lib/bitcore/crypto/ecdsa.ts"],"names":[],"mappings":"AAwBA,OAAO,EAAE,EAAE,EAAE,MAAM,SAAS,CAAA;AAE5B,OAAO,EAAE,SAAS,EAAE,MAAM,gBAAgB,CAAA;AAG1C,OAAO,EAAE,UAAU,EAAE,MAAM,kBAAkB,CAAA;AAC7C,OAAO,EAAE,SAAS,EAAE,MAAM,iBAAiB,CAAA;AAE3C,MAAM,WAAW,SAAS;IACxB,OAAO,CAAC,EAAE,MAAM,CAAA;IAChB,MAAM,CAAC,EAAE,QAAQ,GAAG,KAAK,CAAA;IACzB,OAAO,CAAC,EAAE,UAAU,CAAA;IACpB,MAAM,CAAC,EAAE,SAAS,CAAA;IAClB,GAAG,CAAC,EAAE,SAAS,CAAA;IACf,CAAC,CAAC,EAAE,EAAE,CAAA;IACN,QAAQ,CAAC,EAAE,OAAO,CAAA;CACnB;AAED,qBAAa,KAAK;IAChB,OAAO,EAAG,MAAM,CAAA;IAChB,MAAM,CAAC,EAAE,QAAQ,GAAG,KAAK,CAAA;IACzB,OAAO,EAAG,UAAU,CAAA;IACpB,MAAM,EAAG,SAAS,CAAA;IAClB,GAAG,EAAG,SAAS,CAAA;IACf,CAAC,EAAG,EAAE,CAAA;IACN,QAAQ,EAAG,OAAO,CAAA;gBAEN,GAAG,CAAC,EAAE,SAAS;IAO3B,MAAM,CAAC,MAAM,CAAC,GAAG,CAAC,EAAE,SAAS,GAAG,KAAK;IAIrC,GAAG,CAAC,GAAG,EAAE,SAAS,GAAG,KAAK;IAe1B,cAAc,IAAI,KAAK;IAQvB,KAAK,IAAI,KAAK;IAwBd,MAAM,CAAC,UAAU,CAAC,GAAG,EAAE,MAAM,GAAG,KAAK;IAQrC,OAAO,IAAI,KAAK;IAahB,cAAc,CAAC,KAAK,GAAE,MAAU,GAAG,KAAK;IA4CxC,WAAW,IAAI,SAAS;IAoCxB,QAAQ,IAAI,MAAM,GAAG,KAAK;IA2C1B,MAAM,CAAC,MAAM,CAAC,CAAC,EAAE,EAAE,GAAG,EAAE;IAexB,cAAc,CAAC,CAAC,EAAE,EAAE,EAAE,CAAC,EAAE,EAAE,GAAG;QAAE,CAAC,EAAE,EAAE,CAAC;QAAC,CAAC,EAAE,EAAE,CAAC;QAAC,UAAU,CAAC,EAAE,OAAO,CAAA;KAAE;IA2BpE,IAAI,IAAI,KAAK;IAuBb,WAAW,IAAI,KAAK;IAQpB,QAAQ,IAAI,MAAM;IAuBlB,MAAM,IAAI,KAAK;IAYf,MAAM,CAAC,IAAI,CACT,OAAO,EAAE,MAAM,EACf,OAAO,EAAE,UAAU,EACnB,MAAM,CAAC,EAAE,QAAQ,GAAG,KAAK,GACxB,SAAS;IAaZ,MAAM,CAAC,MAAM,CACX,OAAO,EAAE,MAAM,EACf,GAAG,EAAE,SAAS,EACd,MAAM,EAAE,SAAS,EACjB,MAAM,CAAC,EAAE,QAAQ,GAAG,KAAK,GACxB,OAAO;IAcV,OAAO,CAAC,aAAa;CAOtB"}
|
|
@@ -0,0 +1,247 @@
|
|
|
1
|
+
import { BN } from './bn.js';
|
|
2
|
+
import { Point } from './point.js';
|
|
3
|
+
import { Signature } from './signature.js';
|
|
4
|
+
import { Hash } from './hash.js';
|
|
5
|
+
import { Random } from './random.js';
|
|
6
|
+
import { PublicKey } from '../publickey.js';
|
|
7
|
+
export class ECDSA {
|
|
8
|
+
hashbuf;
|
|
9
|
+
endian;
|
|
10
|
+
privkey;
|
|
11
|
+
pubkey;
|
|
12
|
+
sig;
|
|
13
|
+
k;
|
|
14
|
+
verified;
|
|
15
|
+
constructor(obj) {
|
|
16
|
+
if (obj) {
|
|
17
|
+
this.set(obj);
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
static create(obj) {
|
|
21
|
+
return new ECDSA(obj);
|
|
22
|
+
}
|
|
23
|
+
set(obj) {
|
|
24
|
+
this.hashbuf = obj.hashbuf || this.hashbuf;
|
|
25
|
+
this.endian = obj.endian || this.endian;
|
|
26
|
+
this.privkey = obj.privkey || this.privkey;
|
|
27
|
+
this.pubkey =
|
|
28
|
+
obj.pubkey || (this.privkey ? this.privkey.toPublicKey() : this.pubkey);
|
|
29
|
+
this.sig = obj.sig || this.sig;
|
|
30
|
+
this.k = obj.k || this.k;
|
|
31
|
+
this.verified = obj.verified || this.verified;
|
|
32
|
+
return this;
|
|
33
|
+
}
|
|
34
|
+
privkey2pubkey() {
|
|
35
|
+
this.pubkey = this.privkey.toPublicKey();
|
|
36
|
+
return this;
|
|
37
|
+
}
|
|
38
|
+
calci() {
|
|
39
|
+
for (let i = 0; i < 4; i++) {
|
|
40
|
+
this.sig.i = i;
|
|
41
|
+
let Qprime;
|
|
42
|
+
try {
|
|
43
|
+
Qprime = this.toPublicKey();
|
|
44
|
+
}
|
|
45
|
+
catch (e) {
|
|
46
|
+
console.error(e);
|
|
47
|
+
continue;
|
|
48
|
+
}
|
|
49
|
+
if (Qprime.point.eq(this.pubkey.point)) {
|
|
50
|
+
this.sig.compressed = this.pubkey.compressed;
|
|
51
|
+
return this;
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
this.sig.i = undefined;
|
|
55
|
+
throw new Error('Unable to find valid recovery factor');
|
|
56
|
+
}
|
|
57
|
+
static fromString(str) {
|
|
58
|
+
const obj = JSON.parse(str);
|
|
59
|
+
return new ECDSA(obj);
|
|
60
|
+
}
|
|
61
|
+
randomK() {
|
|
62
|
+
const N = Point.getN();
|
|
63
|
+
let k;
|
|
64
|
+
do {
|
|
65
|
+
k = new BN(Random.getPseudoRandomBuffer(32), 'be');
|
|
66
|
+
} while (!(k.lt(N) && k.gt(new BN(0))));
|
|
67
|
+
this.k = k;
|
|
68
|
+
return this;
|
|
69
|
+
}
|
|
70
|
+
deterministicK(badrs = 0) {
|
|
71
|
+
let v = Buffer.alloc(32);
|
|
72
|
+
v.fill(0x01);
|
|
73
|
+
let k = Buffer.alloc(32);
|
|
74
|
+
k.fill(0x00);
|
|
75
|
+
const x = this.privkey.toBuffer();
|
|
76
|
+
const hashbuf = this.endian === 'little'
|
|
77
|
+
? this.reverseBuffer(this.hashbuf)
|
|
78
|
+
: this.hashbuf;
|
|
79
|
+
k = Hash.sha256hmac(Buffer.concat([v, Buffer.from([0x00]), x, hashbuf]), k);
|
|
80
|
+
v = Hash.sha256hmac(v, k);
|
|
81
|
+
k = Hash.sha256hmac(Buffer.concat([v, Buffer.from([0x01]), x, hashbuf]), k);
|
|
82
|
+
v = Hash.sha256hmac(v, k);
|
|
83
|
+
v = Hash.sha256hmac(v, k);
|
|
84
|
+
let T = new BN(v, 'be');
|
|
85
|
+
const N = Point.getN();
|
|
86
|
+
for (let i = 0; i < badrs || !(T.lt(N) && T.gt(new BN(0))); i++) {
|
|
87
|
+
k = Hash.sha256hmac(Buffer.concat([v, Buffer.from([0x00])]), k);
|
|
88
|
+
v = Hash.sha256hmac(v, k);
|
|
89
|
+
v = Hash.sha256hmac(v, k);
|
|
90
|
+
T = new BN(v, 'be');
|
|
91
|
+
}
|
|
92
|
+
this.k = T;
|
|
93
|
+
return this;
|
|
94
|
+
}
|
|
95
|
+
toPublicKey() {
|
|
96
|
+
const i = this.sig.i;
|
|
97
|
+
if (!(i === 0 || i === 1 || i === 2 || i === 3)) {
|
|
98
|
+
throw new Error('i must be equal to 0, 1, 2, or 3');
|
|
99
|
+
}
|
|
100
|
+
const e = new BN(this.hashbuf, 'be');
|
|
101
|
+
const r = this.sig.r;
|
|
102
|
+
const s = this.sig.s;
|
|
103
|
+
const isYOdd = (i & 1) !== 0;
|
|
104
|
+
const isSecondKey = i >> 1 !== 0;
|
|
105
|
+
const n = Point.getN();
|
|
106
|
+
const G = Point.getG();
|
|
107
|
+
const x = isSecondKey ? r.add(n) : r;
|
|
108
|
+
const R = Point.fromX(isYOdd, x);
|
|
109
|
+
const nR = R.mul(n);
|
|
110
|
+
if (!nR.isInfinity()) {
|
|
111
|
+
throw new Error('nR is not a valid curve point');
|
|
112
|
+
}
|
|
113
|
+
const eNeg = e.neg().mod(n);
|
|
114
|
+
const rInv = r.invm(n);
|
|
115
|
+
const Q = R.mul(s).add(G.mul(eNeg)).mul(rInv);
|
|
116
|
+
const pubkey = PublicKey.fromPoint(Q, this.sig.compressed);
|
|
117
|
+
return pubkey;
|
|
118
|
+
}
|
|
119
|
+
sigError() {
|
|
120
|
+
if (!Buffer.isBuffer(this.hashbuf) || this.hashbuf.length !== 32) {
|
|
121
|
+
return 'hashbuf must be a 32 byte buffer';
|
|
122
|
+
}
|
|
123
|
+
const r = this.sig.r;
|
|
124
|
+
const s = this.sig.s;
|
|
125
|
+
const N = Point.getN();
|
|
126
|
+
if (!(r.gt(new BN(0)) && r.lt(N)) || !(s.gt(new BN(0)) && s.lt(N))) {
|
|
127
|
+
return 'r and s not in range';
|
|
128
|
+
}
|
|
129
|
+
const e = new BN(this.hashbuf, this.endian === 'little' ? 'le' : 'be');
|
|
130
|
+
const n = Point.getN();
|
|
131
|
+
const sinv = s.invm(n);
|
|
132
|
+
const u1 = sinv.mul(e).mod(n);
|
|
133
|
+
const u2 = sinv.mul(r).mod(n);
|
|
134
|
+
const p = Point.getG().mulAdd(u1, this.pubkey.point, u2);
|
|
135
|
+
if (p.isInfinity()) {
|
|
136
|
+
return 'p is infinity';
|
|
137
|
+
}
|
|
138
|
+
if (p.getX().mod(n).cmp(r) !== 0) {
|
|
139
|
+
return 'Invalid signature';
|
|
140
|
+
}
|
|
141
|
+
else {
|
|
142
|
+
return false;
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
static toLowS(s) {
|
|
146
|
+
const lowSThreshold = new BN('7FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF5D576E7357A4501DDFE92F46681B20A0', 16);
|
|
147
|
+
if (s.gt(lowSThreshold)) {
|
|
148
|
+
s = Point.getN().sub(s);
|
|
149
|
+
}
|
|
150
|
+
return s;
|
|
151
|
+
}
|
|
152
|
+
_findSignature(d, e) {
|
|
153
|
+
const N = Point.getN();
|
|
154
|
+
const G = Point.getG();
|
|
155
|
+
let badrs = 0;
|
|
156
|
+
let k, Q, r, s;
|
|
157
|
+
do {
|
|
158
|
+
if (!this.k || badrs > 0) {
|
|
159
|
+
this.deterministicK(badrs);
|
|
160
|
+
}
|
|
161
|
+
badrs++;
|
|
162
|
+
k = this.k;
|
|
163
|
+
Q = G.mul(k);
|
|
164
|
+
r = Q.getX().mod(N);
|
|
165
|
+
s = k
|
|
166
|
+
.invm(N)
|
|
167
|
+
.mul(e.add(d.mul(r)))
|
|
168
|
+
.mod(N);
|
|
169
|
+
} while (r.cmp(new BN(0)) <= 0 || s.cmp(new BN(0)) <= 0);
|
|
170
|
+
s = ECDSA.toLowS(s);
|
|
171
|
+
return { s, r, compressed: this.pubkey.compressed };
|
|
172
|
+
}
|
|
173
|
+
sign() {
|
|
174
|
+
const hashbuf = this.hashbuf;
|
|
175
|
+
const privkey = this.privkey;
|
|
176
|
+
const d = privkey.bn;
|
|
177
|
+
if (!hashbuf || !privkey || !d) {
|
|
178
|
+
throw new Error('invalid parameters');
|
|
179
|
+
}
|
|
180
|
+
if (!Buffer.isBuffer(hashbuf) || hashbuf.length !== 32) {
|
|
181
|
+
throw new Error('hashbuf must be a 32 byte buffer');
|
|
182
|
+
}
|
|
183
|
+
const e = new BN(hashbuf, this.endian === 'little' ? 'le' : 'be');
|
|
184
|
+
const obj = this._findSignature(d, e);
|
|
185
|
+
obj.compressed = this.pubkey.compressed;
|
|
186
|
+
this.sig = new Signature(obj);
|
|
187
|
+
return this;
|
|
188
|
+
}
|
|
189
|
+
signRandomK() {
|
|
190
|
+
this.randomK();
|
|
191
|
+
return this.sign();
|
|
192
|
+
}
|
|
193
|
+
toString() {
|
|
194
|
+
const obj = {};
|
|
195
|
+
if (this.hashbuf) {
|
|
196
|
+
obj.hashbuf = this.hashbuf.toString('hex');
|
|
197
|
+
}
|
|
198
|
+
if (this.privkey) {
|
|
199
|
+
obj.privkey = this.privkey.toString();
|
|
200
|
+
}
|
|
201
|
+
if (this.pubkey) {
|
|
202
|
+
obj.pubkey = this.pubkey.toString();
|
|
203
|
+
}
|
|
204
|
+
if (this.sig) {
|
|
205
|
+
obj.sig = this.sig.toString();
|
|
206
|
+
}
|
|
207
|
+
if (this.k) {
|
|
208
|
+
obj.k = this.k.toString();
|
|
209
|
+
}
|
|
210
|
+
return JSON.stringify(obj);
|
|
211
|
+
}
|
|
212
|
+
verify() {
|
|
213
|
+
if (!this.sigError()) {
|
|
214
|
+
this.verified = true;
|
|
215
|
+
}
|
|
216
|
+
else {
|
|
217
|
+
this.verified = false;
|
|
218
|
+
}
|
|
219
|
+
return this;
|
|
220
|
+
}
|
|
221
|
+
static sign(hashbuf, privkey, endian) {
|
|
222
|
+
return ECDSA.create()
|
|
223
|
+
.set({
|
|
224
|
+
hashbuf: hashbuf,
|
|
225
|
+
endian: endian,
|
|
226
|
+
privkey: privkey,
|
|
227
|
+
})
|
|
228
|
+
.sign().sig;
|
|
229
|
+
}
|
|
230
|
+
static verify(hashbuf, sig, pubkey, endian) {
|
|
231
|
+
return ECDSA.create()
|
|
232
|
+
.set({
|
|
233
|
+
hashbuf: hashbuf,
|
|
234
|
+
endian: endian,
|
|
235
|
+
sig: sig,
|
|
236
|
+
pubkey: pubkey,
|
|
237
|
+
})
|
|
238
|
+
.verify().verified;
|
|
239
|
+
}
|
|
240
|
+
reverseBuffer(buf) {
|
|
241
|
+
const buf2 = Buffer.alloc(buf.length);
|
|
242
|
+
for (let i = 0; i < buf.length; i++) {
|
|
243
|
+
buf2[i] = buf[buf.length - 1 - i];
|
|
244
|
+
}
|
|
245
|
+
return buf2;
|
|
246
|
+
}
|
|
247
|
+
}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
export interface HashFunction {
|
|
2
|
+
(buf: Buffer): Buffer;
|
|
3
|
+
blocksize: number;
|
|
4
|
+
}
|
|
5
|
+
export declare class Hash {
|
|
6
|
+
static sha1: HashFunction;
|
|
7
|
+
static sha256: HashFunction;
|
|
8
|
+
static sha512: HashFunction;
|
|
9
|
+
static sha256sha256: (buf: Buffer) => Buffer;
|
|
10
|
+
static ripemd160: (buf: Buffer) => Buffer;
|
|
11
|
+
static sha256ripemd160: (buf: Buffer) => Buffer;
|
|
12
|
+
static hmac(hashf: HashFunction, data: Buffer, key: Buffer): Buffer;
|
|
13
|
+
static sha256hmac(data: Buffer, key: Buffer): Buffer;
|
|
14
|
+
static sha512hmac(data: Buffer, key: Buffer): Buffer;
|
|
15
|
+
}
|
|
16
|
+
//# sourceMappingURL=hash.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"hash.d.ts","sourceRoot":"","sources":["../../../../lib/bitcore/crypto/hash.ts"],"names":[],"mappings":"AAYA,MAAM,WAAW,YAAY;IAC3B,CAAC,GAAG,EAAE,MAAM,GAAG,MAAM,CAAA;IACrB,SAAS,EAAE,MAAM,CAAA;CAClB;AA2BD,qBAAa,IAAI;IACf,MAAM,CAAC,IAAI,eAAW;IACtB,MAAM,CAAC,MAAM,eAAa;IAC1B,MAAM,CAAC,MAAM,eAAa;IAE1B,MAAM,CAAC,YAAY,GAAa,KAAK,MAAM,KAAG,MAAM,CAKnD;IAED,MAAM,CAAC,SAAS,GAAa,KAAK,MAAM,KAAG,MAAM,CAKhD;IAED,MAAM,CAAC,eAAe,GAAa,KAAK,MAAM,KAAG,MAAM,CAKtD;IAOD,MAAM,CAAC,IAAI,CAAC,KAAK,EAAE,YAAY,EAAE,IAAI,EAAE,MAAM,EAAE,GAAG,EAAE,MAAM,GAAG,MAAM;IA0CnE,MAAM,CAAC,UAAU,CAAC,IAAI,EAAE,MAAM,EAAE,GAAG,EAAE,MAAM,GAAG,MAAM;IAIpD,MAAM,CAAC,UAAU,CAAC,IAAI,EAAE,MAAM,EAAE,GAAG,EAAE,MAAM,GAAG,MAAM;CAGrD"}
|
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
import { sha1 } from '@noble/hashes/sha1';
|
|
2
|
+
import { sha256 } from '@noble/hashes/sha256';
|
|
3
|
+
import { sha512 } from '@noble/hashes/sha512';
|
|
4
|
+
import { ripemd160 } from '@noble/hashes/ripemd160';
|
|
5
|
+
const sha1Func = (buf) => {
|
|
6
|
+
if (!Buffer.isBuffer(buf)) {
|
|
7
|
+
throw new Error('Argument must be a Buffer');
|
|
8
|
+
}
|
|
9
|
+
return Buffer.from(sha1(buf));
|
|
10
|
+
};
|
|
11
|
+
sha1Func.blocksize = 512;
|
|
12
|
+
const sha256Func = (buf) => {
|
|
13
|
+
if (!Buffer.isBuffer(buf)) {
|
|
14
|
+
throw new Error('Argument must be a Buffer');
|
|
15
|
+
}
|
|
16
|
+
return Buffer.from(sha256(buf));
|
|
17
|
+
};
|
|
18
|
+
sha256Func.blocksize = 512;
|
|
19
|
+
const sha512Func = (buf) => {
|
|
20
|
+
if (!Buffer.isBuffer(buf)) {
|
|
21
|
+
throw new Error('Argument must be a Buffer');
|
|
22
|
+
}
|
|
23
|
+
return Buffer.from(sha512(buf));
|
|
24
|
+
};
|
|
25
|
+
sha512Func.blocksize = 1024;
|
|
26
|
+
export class Hash {
|
|
27
|
+
static sha1 = sha1Func;
|
|
28
|
+
static sha256 = sha256Func;
|
|
29
|
+
static sha512 = sha512Func;
|
|
30
|
+
static sha256sha256 = function (buf) {
|
|
31
|
+
if (!Buffer.isBuffer(buf)) {
|
|
32
|
+
throw new Error('Argument must be a Buffer');
|
|
33
|
+
}
|
|
34
|
+
return Hash.sha256(Hash.sha256(buf));
|
|
35
|
+
};
|
|
36
|
+
static ripemd160 = function (buf) {
|
|
37
|
+
if (!Buffer.isBuffer(buf)) {
|
|
38
|
+
throw new Error('Argument must be a Buffer');
|
|
39
|
+
}
|
|
40
|
+
return Buffer.from(ripemd160(buf));
|
|
41
|
+
};
|
|
42
|
+
static sha256ripemd160 = function (buf) {
|
|
43
|
+
if (!Buffer.isBuffer(buf)) {
|
|
44
|
+
throw new Error('Argument must be a Buffer');
|
|
45
|
+
}
|
|
46
|
+
return Hash.ripemd160(Hash.sha256(buf));
|
|
47
|
+
};
|
|
48
|
+
static hmac(hashf, data, key) {
|
|
49
|
+
if (!Buffer.isBuffer(data)) {
|
|
50
|
+
throw new Error('Data must be a Buffer');
|
|
51
|
+
}
|
|
52
|
+
if (!Buffer.isBuffer(key)) {
|
|
53
|
+
throw new Error('Key must be a Buffer');
|
|
54
|
+
}
|
|
55
|
+
if (!hashf.blocksize) {
|
|
56
|
+
throw new Error('Hash function must have blocksize property');
|
|
57
|
+
}
|
|
58
|
+
const blocksize = hashf.blocksize / 8;
|
|
59
|
+
let processedKey = key;
|
|
60
|
+
if (key.length > blocksize) {
|
|
61
|
+
processedKey = hashf(key);
|
|
62
|
+
}
|
|
63
|
+
else if (key.length < blocksize) {
|
|
64
|
+
const fill = Buffer.alloc(blocksize);
|
|
65
|
+
fill.fill(0);
|
|
66
|
+
key.copy(fill);
|
|
67
|
+
processedKey = fill;
|
|
68
|
+
}
|
|
69
|
+
const o_key = Buffer.alloc(blocksize);
|
|
70
|
+
o_key.fill(0x5c);
|
|
71
|
+
const i_key = Buffer.alloc(blocksize);
|
|
72
|
+
i_key.fill(0x36);
|
|
73
|
+
const o_key_pad = Buffer.alloc(blocksize);
|
|
74
|
+
const i_key_pad = Buffer.alloc(blocksize);
|
|
75
|
+
for (let i = 0; i < blocksize; i++) {
|
|
76
|
+
o_key_pad[i] = o_key[i] ^ processedKey[i];
|
|
77
|
+
i_key_pad[i] = i_key[i] ^ processedKey[i];
|
|
78
|
+
}
|
|
79
|
+
return hashf(Buffer.concat([o_key_pad, hashf(Buffer.concat([i_key_pad, data]))]));
|
|
80
|
+
}
|
|
81
|
+
static sha256hmac(data, key) {
|
|
82
|
+
return Hash.hmac(Hash.sha256, data, key);
|
|
83
|
+
}
|
|
84
|
+
static sha512hmac(data, key) {
|
|
85
|
+
return Hash.hmac(Hash.sha512, data, key);
|
|
86
|
+
}
|
|
87
|
+
}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
export { Hash } from './hash.js';
|
|
2
|
+
export { Random } from './random.js';
|
|
3
|
+
export { Point } from './point.js';
|
|
4
|
+
export { Signature } from './signature.js';
|
|
5
|
+
export { ECDSA } from './ecdsa.js';
|
|
6
|
+
export { Schnorr } from './schnorr.js';
|
|
7
|
+
export { BN } from './bn.js';
|
|
8
|
+
export { isSchnorrSignature, isECDSASignature, getSignatureType, isValidSignatureLength, } from './sigtype.js';
|
|
9
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../../lib/bitcore/crypto/index.ts"],"names":[],"mappings":"AAKA,OAAO,EAAE,IAAI,EAAE,MAAM,WAAW,CAAA;AAChC,OAAO,EAAE,MAAM,EAAE,MAAM,aAAa,CAAA;AACpC,OAAO,EAAE,KAAK,EAAE,MAAM,YAAY,CAAA;AAClC,OAAO,EAAE,SAAS,EAAE,MAAM,gBAAgB,CAAA;AAC1C,OAAO,EAAE,KAAK,EAAE,MAAM,YAAY,CAAA;AAClC,OAAO,EAAE,OAAO,EAAE,MAAM,cAAc,CAAA;AACtC,OAAO,EAAE,EAAE,EAAE,MAAM,SAAS,CAAA;AAC5B,OAAO,EACL,kBAAkB,EAClB,gBAAgB,EAChB,gBAAgB,EAChB,sBAAsB,GACvB,MAAM,cAAc,CAAA"}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
export { Hash } from './hash.js';
|
|
2
|
+
export { Random } from './random.js';
|
|
3
|
+
export { Point } from './point.js';
|
|
4
|
+
export { Signature } from './signature.js';
|
|
5
|
+
export { ECDSA } from './ecdsa.js';
|
|
6
|
+
export { Schnorr } from './schnorr.js';
|
|
7
|
+
export { BN } from './bn.js';
|
|
8
|
+
export { isSchnorrSignature, isECDSASignature, getSignatureType, isValidSignatureLength, } from './sigtype.js';
|