xpi-ts 0.2.13 → 0.2.15
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/dist/cjs/lib/bitcore/address.js +75 -74
- package/dist/cjs/lib/bitcore/block/block.js +124 -126
- package/dist/cjs/lib/bitcore/block/blockheader.js +142 -208
- package/dist/cjs/lib/bitcore/block/index.js +3 -1
- package/dist/cjs/lib/bitcore/block/merkleblock.js +301 -0
- package/dist/cjs/lib/bitcore/crypto/bn.js +18 -22
- package/dist/cjs/lib/bitcore/crypto/ecdsa.js +50 -49
- package/dist/cjs/lib/bitcore/crypto/hash.js +25 -22
- package/dist/cjs/lib/bitcore/crypto/musig2.js +86 -92
- package/dist/cjs/lib/bitcore/crypto/point.js +42 -35
- package/dist/cjs/lib/bitcore/crypto/random.js +3 -2
- package/dist/cjs/lib/bitcore/crypto/schnorr.js +46 -55
- package/dist/cjs/lib/bitcore/crypto/signature.js +67 -55
- package/dist/cjs/lib/bitcore/crypto/sigtype.js +1 -1
- package/dist/cjs/lib/bitcore/encoding/base58.js +5 -4
- package/dist/cjs/lib/bitcore/encoding/base58check.js +14 -57
- package/dist/cjs/lib/bitcore/encoding/bufferreader.js +17 -17
- package/dist/cjs/lib/bitcore/encoding/bufferwriter.js +22 -26
- package/dist/cjs/lib/bitcore/encoding/varint.js +12 -11
- package/dist/cjs/lib/bitcore/errors.js +1 -0
- package/dist/cjs/lib/bitcore/hdprivatekey.js +74 -66
- package/dist/cjs/lib/bitcore/hdpublickey.js +96 -116
- package/dist/cjs/lib/bitcore/index.js +165 -172
- package/dist/cjs/lib/bitcore/message.js +36 -35
- package/dist/cjs/lib/bitcore/mnemonic/mnemonic.js +4 -3
- package/dist/cjs/lib/bitcore/mnemonic/pbkdf2.js +6 -5
- package/dist/cjs/lib/bitcore/mnemonic/words/french.js +2053 -0
- package/dist/cjs/lib/bitcore/mnemonic/words/index.js +2 -0
- package/dist/cjs/lib/bitcore/musig2/session.js +26 -25
- package/dist/cjs/lib/bitcore/musig2/signer.js +17 -16
- package/dist/cjs/lib/bitcore/networks.js +4 -3
- package/dist/cjs/lib/bitcore/opcode.js +11 -7
- package/dist/cjs/lib/bitcore/privatekey.js +42 -42
- package/dist/cjs/lib/bitcore/publickey.js +61 -57
- package/dist/cjs/lib/bitcore/script/interpreter/condition-stack.js +41 -0
- package/dist/cjs/lib/bitcore/script/interpreter/index.js +25 -0
- package/dist/cjs/lib/bitcore/script/interpreter/interpreter.js +1362 -0
- package/dist/cjs/lib/bitcore/script/interpreter/script-num.js +200 -0
- package/dist/cjs/lib/bitcore/script/interpreter/types.js +96 -0
- package/dist/cjs/lib/bitcore/{taproot.js → script/taproot.js} +107 -95
- package/dist/cjs/lib/bitcore/script.js +163 -104
- package/dist/cjs/lib/bitcore/taproot/musig2.js +51 -42
- package/dist/cjs/lib/bitcore/transaction/index.js +21 -21
- package/dist/cjs/lib/bitcore/transaction/input/multisig.js +10 -0
- package/dist/cjs/lib/bitcore/transaction/input/multisigscripthash.js +10 -0
- package/dist/cjs/lib/bitcore/transaction/input/publickey.js +10 -0
- package/dist/cjs/lib/bitcore/transaction/input/publickeyhash.js +10 -0
- package/dist/cjs/lib/bitcore/transaction/input/taproot.js +20 -0
- package/dist/cjs/lib/bitcore/transaction/input.js +145 -166
- package/dist/cjs/lib/bitcore/transaction/outpoint.js +89 -0
- package/dist/cjs/lib/bitcore/transaction/output.js +13 -8
- package/dist/cjs/lib/bitcore/transaction/sighash.js +96 -96
- package/dist/cjs/lib/bitcore/transaction/signature.js +39 -40
- package/dist/cjs/lib/bitcore/transaction/transaction.js +130 -118
- package/dist/cjs/lib/bitcore/transaction/unspentoutput.js +27 -23
- package/dist/cjs/lib/bitcore/util/bits.js +33 -0
- package/dist/cjs/lib/bitcore/util/buffer.js +29 -18
- package/dist/cjs/lib/bitcore/util/js.js +0 -3
- package/dist/cjs/lib/bitcore/util/merkle.js +53 -0
- package/dist/cjs/lib/bitcore/util/preconditions.js +8 -7
- package/dist/cjs/lib/bitcore/xaddress.js +27 -23
- package/dist/cjs/lib/{bitcore/taproot/nft.js → nft.js} +98 -29
- package/dist/cjs/lib/rank/index.js +28 -27
- package/dist/cjs/lib/rank/script.js +2 -1
- package/dist/cjs/lib/rank/transaction.js +2 -1
- package/dist/cjs/utils/string.js +6 -5
- package/dist/esm/lib/bitcore/address.js +42 -41
- package/dist/esm/lib/bitcore/block/block.js +126 -128
- package/dist/esm/lib/bitcore/block/blockheader.js +141 -207
- package/dist/esm/lib/bitcore/block/index.js +1 -0
- package/dist/esm/lib/bitcore/block/merkleblock.js +295 -0
- package/dist/esm/lib/bitcore/crypto/bn.js +18 -22
- package/dist/esm/lib/bitcore/crypto/ecdsa.js +25 -24
- package/dist/esm/lib/bitcore/crypto/hash.js +25 -22
- package/dist/esm/lib/bitcore/crypto/musig2.js +60 -66
- package/dist/esm/lib/bitcore/crypto/point.js +22 -15
- package/dist/esm/lib/bitcore/crypto/random.js +3 -2
- package/dist/esm/lib/bitcore/crypto/schnorr.js +35 -44
- package/dist/esm/lib/bitcore/crypto/signature.js +61 -49
- package/dist/esm/lib/bitcore/crypto/sigtype.js +1 -1
- package/dist/esm/lib/bitcore/encoding/base58.js +5 -4
- package/dist/esm/lib/bitcore/encoding/base58check.js +11 -57
- package/dist/esm/lib/bitcore/encoding/bufferreader.js +7 -7
- package/dist/esm/lib/bitcore/encoding/bufferwriter.js +18 -21
- package/dist/esm/lib/bitcore/encoding/varint.js +7 -6
- package/dist/esm/lib/bitcore/errors.js +1 -0
- package/dist/esm/lib/bitcore/hdprivatekey.js +47 -39
- package/dist/esm/lib/bitcore/hdpublickey.js +77 -97
- package/dist/esm/lib/bitcore/index.js +42 -45
- package/dist/esm/lib/bitcore/message.js +16 -15
- package/dist/esm/lib/bitcore/mnemonic/mnemonic.js +4 -3
- package/dist/esm/lib/bitcore/mnemonic/pbkdf2.js +5 -4
- package/dist/esm/lib/bitcore/mnemonic/words/french.js +2050 -0
- package/dist/esm/lib/bitcore/mnemonic/words/index.js +2 -0
- package/dist/esm/lib/bitcore/musig2/session.js +17 -16
- package/dist/esm/lib/bitcore/musig2/signer.js +19 -18
- package/dist/esm/lib/bitcore/networks.js +4 -3
- package/dist/esm/lib/bitcore/opcode.js +9 -5
- package/dist/esm/lib/bitcore/privatekey.js +25 -25
- package/dist/esm/lib/bitcore/publickey.js +46 -42
- package/dist/esm/lib/bitcore/script/interpreter/condition-stack.js +37 -0
- package/dist/esm/lib/bitcore/script/interpreter/index.js +4 -0
- package/dist/esm/lib/bitcore/script/interpreter/interpreter.js +1356 -0
- package/dist/esm/lib/bitcore/script/interpreter/script-num.js +195 -0
- package/dist/esm/lib/bitcore/script/interpreter/types.js +93 -0
- package/dist/esm/lib/bitcore/{taproot.js → script/taproot.js} +92 -81
- package/dist/esm/lib/bitcore/script.js +163 -102
- package/dist/esm/lib/bitcore/taproot/musig2.js +36 -27
- package/dist/esm/lib/bitcore/transaction/index.js +6 -6
- package/dist/esm/lib/bitcore/transaction/input/multisig.js +6 -0
- package/dist/esm/lib/bitcore/transaction/input/multisigscripthash.js +6 -0
- package/dist/esm/lib/bitcore/transaction/input/publickey.js +6 -0
- package/dist/esm/lib/bitcore/transaction/input/publickeyhash.js +6 -0
- package/dist/esm/lib/bitcore/transaction/input/taproot.js +16 -0
- package/dist/esm/lib/bitcore/transaction/input.js +55 -76
- package/dist/esm/lib/bitcore/transaction/outpoint.js +85 -0
- package/dist/esm/lib/bitcore/transaction/output.js +13 -8
- package/dist/esm/lib/bitcore/transaction/sighash.js +22 -22
- package/dist/esm/lib/bitcore/transaction/signature.js +16 -17
- package/dist/esm/lib/bitcore/transaction/transaction.js +58 -46
- package/dist/esm/lib/bitcore/transaction/unspentoutput.js +15 -11
- package/dist/esm/lib/bitcore/util/bits.js +27 -0
- package/dist/esm/lib/bitcore/util/buffer.js +20 -9
- package/dist/esm/lib/bitcore/util/js.js +0 -3
- package/dist/esm/lib/bitcore/util/merkle.js +47 -0
- package/dist/esm/lib/bitcore/util/preconditions.js +3 -2
- package/dist/esm/lib/bitcore/xaddress.js +16 -12
- package/dist/esm/lib/{bitcore/taproot/nft.js → nft.js} +97 -28
- package/dist/esm/lib/rank/index.js +15 -14
- package/dist/esm/lib/rank/script.js +1 -0
- package/dist/esm/lib/rank/transaction.js +1 -0
- package/dist/esm/utils/string.js +1 -0
- package/dist/types/lib/bitcore/address.d.ts +4 -3
- package/dist/types/lib/bitcore/address.d.ts.map +1 -1
- package/dist/types/lib/bitcore/block/block.d.ts +11 -19
- package/dist/types/lib/bitcore/block/block.d.ts.map +1 -1
- package/dist/types/lib/bitcore/block/blockheader.d.ts +30 -32
- package/dist/types/lib/bitcore/block/blockheader.d.ts.map +1 -1
- package/dist/types/lib/bitcore/block/index.d.ts +2 -0
- package/dist/types/lib/bitcore/block/index.d.ts.map +1 -1
- package/dist/types/lib/bitcore/block/merkleblock.d.ts +53 -0
- package/dist/types/lib/bitcore/block/merkleblock.d.ts.map +1 -0
- package/dist/types/lib/bitcore/crypto/bn.d.ts +2 -2
- package/dist/types/lib/bitcore/crypto/bn.d.ts.map +1 -1
- package/dist/types/lib/bitcore/crypto/ecdsa.d.ts +5 -4
- package/dist/types/lib/bitcore/crypto/ecdsa.d.ts.map +1 -1
- package/dist/types/lib/bitcore/crypto/hash.d.ts +3 -1
- package/dist/types/lib/bitcore/crypto/hash.d.ts.map +1 -1
- package/dist/types/lib/bitcore/crypto/musig2.d.ts +22 -30
- package/dist/types/lib/bitcore/crypto/musig2.d.ts.map +1 -1
- package/dist/types/lib/bitcore/crypto/point.d.ts +8 -3
- package/dist/types/lib/bitcore/crypto/point.d.ts.map +1 -1
- package/dist/types/lib/bitcore/crypto/random.d.ts +1 -0
- package/dist/types/lib/bitcore/crypto/random.d.ts.map +1 -1
- package/dist/types/lib/bitcore/crypto/schnorr.d.ts +5 -4
- package/dist/types/lib/bitcore/crypto/schnorr.d.ts.map +1 -1
- package/dist/types/lib/bitcore/crypto/signature.d.ts +4 -1
- package/dist/types/lib/bitcore/crypto/signature.d.ts.map +1 -1
- package/dist/types/lib/bitcore/crypto/sigtype.d.ts +1 -0
- package/dist/types/lib/bitcore/crypto/sigtype.d.ts.map +1 -1
- package/dist/types/lib/bitcore/encoding/base58.d.ts +1 -0
- package/dist/types/lib/bitcore/encoding/base58.d.ts.map +1 -1
- package/dist/types/lib/bitcore/encoding/base58check.d.ts +1 -2
- package/dist/types/lib/bitcore/encoding/base58check.d.ts.map +1 -1
- package/dist/types/lib/bitcore/encoding/bufferreader.d.ts +2 -1
- package/dist/types/lib/bitcore/encoding/bufferreader.d.ts.map +1 -1
- package/dist/types/lib/bitcore/encoding/bufferwriter.d.ts +2 -2
- package/dist/types/lib/bitcore/encoding/bufferwriter.d.ts.map +1 -1
- package/dist/types/lib/bitcore/encoding/varint.d.ts +3 -2
- package/dist/types/lib/bitcore/encoding/varint.d.ts.map +1 -1
- package/dist/types/lib/bitcore/errors.d.ts +3 -0
- package/dist/types/lib/bitcore/errors.d.ts.map +1 -1
- package/dist/types/lib/bitcore/hdprivatekey.d.ts +5 -4
- package/dist/types/lib/bitcore/hdprivatekey.d.ts.map +1 -1
- package/dist/types/lib/bitcore/hdpublickey.d.ts +4 -3
- package/dist/types/lib/bitcore/hdpublickey.d.ts.map +1 -1
- package/dist/types/lib/bitcore/index.d.ts +55 -60
- package/dist/types/lib/bitcore/index.d.ts.map +1 -1
- package/dist/types/lib/bitcore/message.d.ts +7 -4
- package/dist/types/lib/bitcore/message.d.ts.map +1 -1
- package/dist/types/lib/bitcore/mnemonic/mnemonic.d.ts +2 -0
- package/dist/types/lib/bitcore/mnemonic/mnemonic.d.ts.map +1 -1
- package/dist/types/lib/bitcore/mnemonic/pbkdf2.d.ts +1 -0
- package/dist/types/lib/bitcore/mnemonic/pbkdf2.d.ts.map +1 -1
- package/dist/types/lib/bitcore/mnemonic/words/french.d.ts +2 -0
- package/dist/types/lib/bitcore/mnemonic/words/french.d.ts.map +1 -0
- package/dist/types/lib/bitcore/mnemonic/words/index.d.ts +1 -0
- package/dist/types/lib/bitcore/mnemonic/words/index.d.ts.map +1 -1
- package/dist/types/lib/bitcore/musig2/session.d.ts +8 -7
- package/dist/types/lib/bitcore/musig2/session.d.ts.map +1 -1
- package/dist/types/lib/bitcore/musig2/signer.d.ts +10 -9
- package/dist/types/lib/bitcore/musig2/signer.d.ts.map +1 -1
- package/dist/types/lib/bitcore/networks.d.ts +1 -0
- package/dist/types/lib/bitcore/networks.d.ts.map +1 -1
- package/dist/types/lib/bitcore/opcode.d.ts +5 -0
- package/dist/types/lib/bitcore/opcode.d.ts.map +1 -1
- package/dist/types/lib/bitcore/privatekey.d.ts +5 -4
- package/dist/types/lib/bitcore/privatekey.d.ts.map +1 -1
- package/dist/types/lib/bitcore/publickey.d.ts +13 -6
- package/dist/types/lib/bitcore/publickey.d.ts.map +1 -1
- package/dist/types/lib/bitcore/{chunk.d.ts → script/chunk.d.ts} +1 -0
- package/dist/types/lib/bitcore/script/chunk.d.ts.map +1 -0
- package/dist/types/lib/bitcore/script/interpreter/condition-stack.d.ts +11 -0
- package/dist/types/lib/bitcore/script/interpreter/condition-stack.d.ts.map +1 -0
- package/dist/types/lib/bitcore/script/interpreter/index.d.ts +5 -0
- package/dist/types/lib/bitcore/script/interpreter/index.d.ts.map +1 -0
- package/dist/types/lib/bitcore/script/interpreter/interpreter.d.ts +21 -0
- package/dist/types/lib/bitcore/script/interpreter/interpreter.d.ts.map +1 -0
- package/dist/types/lib/bitcore/script/interpreter/script-num.d.ts +35 -0
- package/dist/types/lib/bitcore/script/interpreter/script-num.d.ts.map +1 -0
- package/dist/types/lib/bitcore/script/interpreter/types.d.ts +101 -0
- package/dist/types/lib/bitcore/script/interpreter/types.d.ts.map +1 -0
- package/dist/types/lib/bitcore/{taproot.d.ts → script/taproot.d.ts} +20 -7
- package/dist/types/lib/bitcore/script/taproot.d.ts.map +1 -0
- package/dist/types/lib/bitcore/script.d.ts +12 -11
- package/dist/types/lib/bitcore/script.d.ts.map +1 -1
- package/dist/types/lib/bitcore/taproot/musig2.d.ts +17 -16
- package/dist/types/lib/bitcore/taproot/musig2.d.ts.map +1 -1
- package/dist/types/lib/bitcore/transaction/index.d.ts +11 -11
- package/dist/types/lib/bitcore/transaction/index.d.ts.map +1 -1
- package/dist/types/lib/bitcore/transaction/input/multisig.d.ts +5 -0
- package/dist/types/lib/bitcore/transaction/input/multisig.d.ts.map +1 -0
- package/dist/types/lib/bitcore/transaction/input/multisigscripthash.d.ts +5 -0
- package/dist/types/lib/bitcore/transaction/input/multisigscripthash.d.ts.map +1 -0
- package/dist/types/lib/bitcore/transaction/input/publickey.d.ts +5 -0
- package/dist/types/lib/bitcore/transaction/input/publickey.d.ts.map +1 -0
- package/dist/types/lib/bitcore/transaction/input/publickeyhash.d.ts +6 -0
- package/dist/types/lib/bitcore/transaction/input/publickeyhash.d.ts.map +1 -0
- package/dist/types/lib/bitcore/transaction/input/taproot.d.ts +18 -0
- package/dist/types/lib/bitcore/transaction/input/taproot.d.ts.map +1 -0
- package/dist/types/lib/bitcore/transaction/input.d.ts +27 -35
- package/dist/types/lib/bitcore/transaction/input.d.ts.map +1 -1
- package/dist/types/lib/bitcore/transaction/outpoint.d.ts +32 -0
- package/dist/types/lib/bitcore/transaction/outpoint.d.ts.map +1 -0
- package/dist/types/lib/bitcore/transaction/output.d.ts +3 -1
- package/dist/types/lib/bitcore/transaction/output.d.ts.map +1 -1
- package/dist/types/lib/bitcore/transaction/sighash.d.ts +8 -7
- package/dist/types/lib/bitcore/transaction/sighash.d.ts.map +1 -1
- package/dist/types/lib/bitcore/transaction/signature.d.ts +3 -2
- package/dist/types/lib/bitcore/transaction/signature.d.ts.map +1 -1
- package/dist/types/lib/bitcore/transaction/transaction.d.ts +18 -15
- package/dist/types/lib/bitcore/transaction/transaction.d.ts.map +1 -1
- package/dist/types/lib/bitcore/transaction/unspentoutput.d.ts +9 -7
- package/dist/types/lib/bitcore/transaction/unspentoutput.d.ts.map +1 -1
- package/dist/types/lib/bitcore/unit.d.ts.map +1 -1
- package/dist/types/lib/bitcore/util/base32.d.ts.map +1 -1
- package/dist/types/lib/bitcore/util/bits.d.ts +5 -0
- package/dist/types/lib/bitcore/util/bits.d.ts.map +1 -0
- package/dist/types/lib/bitcore/util/buffer.d.ts +10 -2
- package/dist/types/lib/bitcore/util/buffer.d.ts.map +1 -1
- package/dist/types/lib/bitcore/util/js.d.ts +0 -1
- package/dist/types/lib/bitcore/util/js.d.ts.map +1 -1
- package/dist/types/lib/bitcore/util/merkle.d.ts +6 -0
- package/dist/types/lib/bitcore/util/merkle.d.ts.map +1 -0
- package/dist/types/lib/bitcore/util/preconditions.d.ts.map +1 -1
- package/dist/types/lib/bitcore/xaddress.d.ts +1 -0
- package/dist/types/lib/bitcore/xaddress.d.ts.map +1 -1
- package/dist/types/lib/{bitcore/taproot/nft.d.ts → nft.d.ts} +26 -12
- package/dist/types/lib/nft.d.ts.map +1 -0
- package/dist/types/lib/rank/index.d.ts +1 -0
- package/dist/types/lib/rank/index.d.ts.map +1 -1
- package/dist/types/lib/rank/script.d.ts +1 -0
- package/dist/types/lib/rank/script.d.ts.map +1 -1
- package/dist/types/lib/rank/transaction.d.ts +1 -0
- package/dist/types/lib/rank/transaction.d.ts.map +1 -1
- package/dist/types/utils/string.d.ts +1 -0
- package/dist/types/utils/string.d.ts.map +1 -1
- package/package.json +3 -2
- package/dist/cjs/lib/bitcore/script/interpreter.js +0 -1757
- package/dist/esm/lib/bitcore/script/interpreter.js +0 -1753
- package/dist/types/lib/bitcore/chunk.d.ts.map +0 -1
- package/dist/types/lib/bitcore/script/interpreter.d.ts +0 -101
- package/dist/types/lib/bitcore/script/interpreter.d.ts.map +0 -1
- package/dist/types/lib/bitcore/taproot/nft.d.ts.map +0 -1
- package/dist/types/lib/bitcore/taproot.d.ts.map +0 -1
- /package/dist/cjs/lib/bitcore/{chunk.js → script/chunk.js} +0 -0
- /package/dist/esm/lib/bitcore/{chunk.js → script/chunk.js} +0 -0
|
@@ -1,10 +1,11 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.Schnorr = void 0;
|
|
4
|
-
const
|
|
5
|
-
const
|
|
6
|
-
const
|
|
7
|
-
const
|
|
4
|
+
const bn_1 = require("./bn");
|
|
5
|
+
const point_1 = require("./point");
|
|
6
|
+
const signature_1 = require("./signature");
|
|
7
|
+
const hash_1 = require("./hash");
|
|
8
|
+
const util_1 = require("../util");
|
|
8
9
|
class Schnorr {
|
|
9
10
|
hashbuf;
|
|
10
11
|
endian;
|
|
@@ -41,26 +42,26 @@ class Schnorr {
|
|
|
41
42
|
if (!hashbuf || !privkey || !d) {
|
|
42
43
|
throw new Error('invalid parameters');
|
|
43
44
|
}
|
|
44
|
-
if (!
|
|
45
|
+
if (!util_1.BufferUtil.isBuffer(hashbuf) || hashbuf.length !== 32) {
|
|
45
46
|
throw new Error('hashbuf must be a 32 byte buffer');
|
|
46
47
|
}
|
|
47
|
-
const e =
|
|
48
|
+
const e = bn_1.BN.fromBuffer(hashbuf);
|
|
48
49
|
const obj = this._findSignature(d, e);
|
|
49
50
|
obj.compressed = this.pubkey.compressed;
|
|
50
51
|
obj.isSchnorr = true;
|
|
51
|
-
this.sig = new
|
|
52
|
+
this.sig = new signature_1.Signature(obj);
|
|
52
53
|
return this;
|
|
53
54
|
}
|
|
54
55
|
_findSignature(d, e) {
|
|
55
|
-
const n =
|
|
56
|
-
const G =
|
|
57
|
-
if (d.lte(
|
|
56
|
+
const n = point_1.Point.getN();
|
|
57
|
+
const G = point_1.Point.getG();
|
|
58
|
+
if (d.lte(bn_1.BN.Zero)) {
|
|
58
59
|
throw new Error('privkey out of field of curve');
|
|
59
60
|
}
|
|
60
61
|
if (d.gte(n)) {
|
|
61
62
|
throw new Error('privkey out of field of curve');
|
|
62
63
|
}
|
|
63
|
-
let k = this.nonceFunctionRFC6979(d.
|
|
64
|
+
let k = this.nonceFunctionRFC6979(d.toBuffer({ size: 32 }), e.toBuffer({ size: 32 }));
|
|
64
65
|
const P = G.mul(d);
|
|
65
66
|
const R = G.mul(k);
|
|
66
67
|
if (R.hasSquare()) {
|
|
@@ -68,94 +69,84 @@ class Schnorr {
|
|
|
68
69
|
else {
|
|
69
70
|
k = n.sub(k);
|
|
70
71
|
}
|
|
71
|
-
const r = R.
|
|
72
|
+
const r = R.x;
|
|
72
73
|
const rBuffer = this.getrBuffer(r);
|
|
73
|
-
const e0 =
|
|
74
|
+
const e0 = bn_1.BN.fromBuffer(hash_1.Hash.sha256(util_1.BufferUtil.concat([
|
|
74
75
|
rBuffer,
|
|
75
|
-
|
|
76
|
-
e.
|
|
77
|
-
]))
|
|
76
|
+
point_1.Point.pointToCompressed(P),
|
|
77
|
+
e.toBuffer({ size: 32 }),
|
|
78
|
+
])));
|
|
78
79
|
const s = e0.mul(d).add(k).mod(n);
|
|
79
80
|
return { r, s, compressed: this.pubkey.compressed, isSchnorr: true };
|
|
80
81
|
}
|
|
81
82
|
getrBuffer(r) {
|
|
82
|
-
const
|
|
83
|
-
|
|
84
|
-
return r.toArrayLike(Buffer, 'be', 32);
|
|
85
|
-
}
|
|
86
|
-
return r.toArrayLike(Buffer, 'be');
|
|
83
|
+
const buf = r.toBuffer();
|
|
84
|
+
return buf.length < 32 ? r.toBuffer({ size: 32 }) : buf;
|
|
87
85
|
}
|
|
88
86
|
getsBuffer(s) {
|
|
89
|
-
const
|
|
90
|
-
|
|
91
|
-
return s.toArrayLike(Buffer, 'be', 32);
|
|
92
|
-
}
|
|
93
|
-
return s.toArrayLike(Buffer, 'be');
|
|
87
|
+
const buf = s.toBuffer();
|
|
88
|
+
return buf.length < 32 ? s.toBuffer({ size: 32 }) : buf;
|
|
94
89
|
}
|
|
95
90
|
sigError() {
|
|
96
|
-
if (!
|
|
97
|
-
return true;
|
|
98
|
-
}
|
|
99
|
-
const sigLength = this.getrBuffer(this.sig.r).length + this.getsBuffer(this.sig.s).length;
|
|
100
|
-
if (!(sigLength === 64 || sigLength === 65)) {
|
|
91
|
+
if (!util_1.BufferUtil.isBuffer(this.hashbuf) || this.hashbuf.length !== 32) {
|
|
101
92
|
return true;
|
|
102
93
|
}
|
|
103
94
|
const hashbuf = this.endian === 'little' ? this.reverseBuffer(this.hashbuf) : this.hashbuf;
|
|
104
95
|
const P = this.pubkey.point;
|
|
105
|
-
const G =
|
|
96
|
+
const G = point_1.Point.getG();
|
|
106
97
|
if (P.isInfinity()) {
|
|
107
98
|
return true;
|
|
108
99
|
}
|
|
109
100
|
const r = this.sig.r;
|
|
110
101
|
const s = this.sig.s;
|
|
111
|
-
const p =
|
|
112
|
-
const n =
|
|
102
|
+
const p = bn_1.BN.fromString('FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEFFFFFC2F', 'hex');
|
|
103
|
+
const n = point_1.Point.getN();
|
|
113
104
|
if (r.gte(p) || s.gte(n)) {
|
|
114
105
|
return true;
|
|
115
106
|
}
|
|
116
107
|
const Br = this.getrBuffer(this.sig.r);
|
|
117
|
-
const Bp =
|
|
118
|
-
const hash =
|
|
119
|
-
const e =
|
|
108
|
+
const Bp = point_1.Point.pointToCompressed(P);
|
|
109
|
+
const hash = hash_1.Hash.sha256(util_1.BufferUtil.concat([Br, Bp, hashbuf]));
|
|
110
|
+
const e = bn_1.BN.fromBuffer(hash).mod(n);
|
|
120
111
|
const sG = G.mul(s);
|
|
121
112
|
const eP = P.mul(n.sub(e));
|
|
122
113
|
const R = sG.add(eP);
|
|
123
|
-
if (R.isInfinity() || !R.hasSquare() || !R.
|
|
114
|
+
if (R.isInfinity() || !R.hasSquare() || !R.x.eq(r)) {
|
|
124
115
|
return true;
|
|
125
116
|
}
|
|
126
117
|
return false;
|
|
127
118
|
}
|
|
128
119
|
verify() {
|
|
129
|
-
this.verified =
|
|
120
|
+
this.verified = this.sigError() === false;
|
|
130
121
|
return this;
|
|
131
122
|
}
|
|
132
123
|
nonceFunctionRFC6979(privkey, msgbuf) {
|
|
133
|
-
let V =
|
|
134
|
-
let K =
|
|
135
|
-
const blob =
|
|
124
|
+
let V = util_1.BufferUtil.from('0101010101010101010101010101010101010101010101010101010101010101', 'hex');
|
|
125
|
+
let K = util_1.BufferUtil.from('0000000000000000000000000000000000000000000000000000000000000000', 'hex');
|
|
126
|
+
const blob = util_1.BufferUtil.concat([
|
|
136
127
|
privkey,
|
|
137
128
|
msgbuf,
|
|
138
|
-
|
|
139
|
-
|
|
129
|
+
util_1.BufferUtil.from('', 'ascii'),
|
|
130
|
+
util_1.BufferUtil.from('Schnorr+SHA256 ', 'ascii'),
|
|
140
131
|
]);
|
|
141
|
-
K =
|
|
142
|
-
V =
|
|
143
|
-
K =
|
|
144
|
-
V =
|
|
145
|
-
let k =
|
|
132
|
+
K = hash_1.Hash.sha256hmac(util_1.BufferUtil.concat([V, util_1.BufferUtil.from('00', 'hex'), blob]), K);
|
|
133
|
+
V = hash_1.Hash.sha256hmac(V, K);
|
|
134
|
+
K = hash_1.Hash.sha256hmac(util_1.BufferUtil.concat([V, util_1.BufferUtil.from('01', 'hex'), blob]), K);
|
|
135
|
+
V = hash_1.Hash.sha256hmac(V, K);
|
|
136
|
+
let k = bn_1.BN.Zero;
|
|
146
137
|
let T;
|
|
147
138
|
while (true) {
|
|
148
|
-
V =
|
|
149
|
-
T =
|
|
139
|
+
V = hash_1.Hash.sha256hmac(V, K);
|
|
140
|
+
T = bn_1.BN.fromBuffer(V);
|
|
150
141
|
k = T;
|
|
151
142
|
if (V.length < 32) {
|
|
152
143
|
throw new Error('V length should be >= 32');
|
|
153
144
|
}
|
|
154
|
-
if (k.gt(
|
|
145
|
+
if (k.gt(bn_1.BN.Zero) && k.lt(point_1.Point.getN())) {
|
|
155
146
|
break;
|
|
156
147
|
}
|
|
157
|
-
K =
|
|
158
|
-
V =
|
|
148
|
+
K = hash_1.Hash.sha256hmac(util_1.BufferUtil.concat([V, util_1.BufferUtil.from('00', 'hex')]), K);
|
|
149
|
+
V = hash_1.Hash.sha256hmac(V, K);
|
|
159
150
|
}
|
|
160
151
|
return k;
|
|
161
152
|
}
|
|
@@ -179,7 +170,7 @@ class Schnorr {
|
|
|
179
170
|
.verify().verified;
|
|
180
171
|
}
|
|
181
172
|
reverseBuffer(buf) {
|
|
182
|
-
const buf2 =
|
|
173
|
+
const buf2 = util_1.BufferUtil.alloc(buf.length);
|
|
183
174
|
for (let i = 0; i < buf.length; i++) {
|
|
184
175
|
buf2[i] = buf[buf.length - 1 - i];
|
|
185
176
|
}
|
|
@@ -1,7 +1,9 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.Signature = void 0;
|
|
4
|
-
const
|
|
4
|
+
const bn_1 = require("./bn");
|
|
5
|
+
const util_1 = require("../util");
|
|
6
|
+
const sigtype_1 = require("./sigtype");
|
|
5
7
|
class Signature {
|
|
6
8
|
r;
|
|
7
9
|
s;
|
|
@@ -15,8 +17,10 @@ class Signature {
|
|
|
15
17
|
static SIGHASH_FORKID = 0x40;
|
|
16
18
|
static SIGHASH_LOTUS = 0x60;
|
|
17
19
|
static SIGHASH_ANYONECANPAY = 0x80;
|
|
20
|
+
static SIGHASH_RESERVED = 0x20;
|
|
21
|
+
static SIGHASH_ALGORITHM_MASK = 0x60;
|
|
18
22
|
constructor(r, s, isSchnorr) {
|
|
19
|
-
if (r instanceof
|
|
23
|
+
if (r instanceof bn_1.BN) {
|
|
20
24
|
this.set({
|
|
21
25
|
r: r,
|
|
22
26
|
s: s,
|
|
@@ -41,18 +45,18 @@ class Signature {
|
|
|
41
45
|
return this;
|
|
42
46
|
}
|
|
43
47
|
static fromCompact(buf) {
|
|
44
|
-
if (!
|
|
48
|
+
if (!util_1.BufferUtil.isBuffer(buf)) {
|
|
45
49
|
throw new Error('Argument is expected to be a Buffer');
|
|
46
50
|
}
|
|
47
|
-
const sig = new Signature(new
|
|
51
|
+
const sig = new Signature(new bn_1.BN(0), new bn_1.BN(0));
|
|
48
52
|
let compressed = true;
|
|
49
|
-
let i = buf.
|
|
53
|
+
let i = buf.slice(0, 1)[0] - 27 - 4;
|
|
50
54
|
if (i < 0) {
|
|
51
55
|
compressed = false;
|
|
52
56
|
i = i + 4;
|
|
53
57
|
}
|
|
54
|
-
const b2 = buf.
|
|
55
|
-
const b3 = buf.
|
|
58
|
+
const b2 = buf.slice(1, 33);
|
|
59
|
+
const b3 = buf.slice(33, 65);
|
|
56
60
|
if (!(i === 0 || i === 1 || i === 2 || i === 3)) {
|
|
57
61
|
throw new Error('i must be 0, 1, 2, or 3');
|
|
58
62
|
}
|
|
@@ -64,24 +68,25 @@ class Signature {
|
|
|
64
68
|
}
|
|
65
69
|
sig.compressed = compressed;
|
|
66
70
|
sig.i = i;
|
|
67
|
-
sig.r =
|
|
68
|
-
sig.s =
|
|
71
|
+
sig.r = bn_1.BN.fromBuffer(b2);
|
|
72
|
+
sig.s = bn_1.BN.fromBuffer(b3);
|
|
69
73
|
return sig;
|
|
70
74
|
}
|
|
71
75
|
static fromDER(buf, strict = true) {
|
|
72
|
-
|
|
76
|
+
const rawSig = buf.length === 65 && buf[0] !== 0x30 ? buf.slice(0, 64) : buf;
|
|
77
|
+
if ((0, sigtype_1.isSchnorrSignature)(rawSig) && buf[0] !== 0x30) {
|
|
73
78
|
const obj = Signature.parseSchnorrEncodedSig(buf);
|
|
74
|
-
const sig = new Signature(new
|
|
79
|
+
const sig = new Signature(new bn_1.BN(0), new bn_1.BN(0));
|
|
75
80
|
sig.r = obj.r;
|
|
76
81
|
sig.s = obj.s;
|
|
77
82
|
sig.isSchnorr = true;
|
|
78
83
|
return sig;
|
|
79
84
|
}
|
|
80
|
-
if (buf
|
|
85
|
+
if ((0, sigtype_1.isSchnorrSignature)(buf) && buf[0] === 0x30) {
|
|
81
86
|
throw new Error('64 DER (ecdsa) signatures not allowed');
|
|
82
87
|
}
|
|
83
88
|
const obj = Signature.parseDER(buf, strict);
|
|
84
|
-
const sig = new Signature(new
|
|
89
|
+
const sig = new Signature(new bn_1.BN(0), new bn_1.BN(0));
|
|
85
90
|
sig.r = obj.r;
|
|
86
91
|
sig.s = obj.s;
|
|
87
92
|
return sig;
|
|
@@ -91,17 +96,17 @@ class Signature {
|
|
|
91
96
|
}
|
|
92
97
|
static fromTxFormat(buf) {
|
|
93
98
|
const nhashtype = buf.readUInt8(buf.length - 1);
|
|
94
|
-
const derbuf = buf.
|
|
99
|
+
const derbuf = buf.slice(0, buf.length - 1);
|
|
95
100
|
const sig = Signature.fromDER(derbuf, false);
|
|
96
101
|
sig.nhashtype = nhashtype;
|
|
97
102
|
return sig;
|
|
98
103
|
}
|
|
99
104
|
static fromDataFormat(buf) {
|
|
100
|
-
const derbuf = buf.
|
|
105
|
+
const derbuf = buf.slice(0, buf.length);
|
|
101
106
|
return Signature.fromDER(derbuf, false);
|
|
102
107
|
}
|
|
103
108
|
static fromString(str) {
|
|
104
|
-
const buf =
|
|
109
|
+
const buf = util_1.BufferUtil.from(str, 'hex');
|
|
105
110
|
return Signature.fromDER(buf);
|
|
106
111
|
}
|
|
107
112
|
static fromSchnorr(buf) {
|
|
@@ -117,20 +122,20 @@ class Signature {
|
|
|
117
122
|
if (buf.length !== 64 && buf.length !== 65) {
|
|
118
123
|
throw new Error(`Invalid Schnorr signature length: ${buf.length} bytes (expected 64 or 65)`);
|
|
119
124
|
}
|
|
120
|
-
const r = buf.
|
|
121
|
-
const s = buf.
|
|
125
|
+
const r = buf.slice(0, 32);
|
|
126
|
+
const s = buf.slice(32, 64);
|
|
122
127
|
let hashtype;
|
|
123
128
|
if (buf.length === 65) {
|
|
124
|
-
hashtype = buf.
|
|
129
|
+
hashtype = buf.slice(64, 65);
|
|
125
130
|
}
|
|
126
131
|
return {
|
|
127
|
-
r:
|
|
128
|
-
s:
|
|
132
|
+
r: bn_1.BN.fromBuffer(r),
|
|
133
|
+
s: bn_1.BN.fromBuffer(s),
|
|
129
134
|
nhashtype: hashtype,
|
|
130
135
|
};
|
|
131
136
|
}
|
|
132
137
|
static parseDER(buf, strict = true) {
|
|
133
|
-
if (!
|
|
138
|
+
if (!util_1.BufferUtil.isBuffer(buf)) {
|
|
134
139
|
throw new Error('DER formatted signature should be a buffer');
|
|
135
140
|
}
|
|
136
141
|
const header = buf[0];
|
|
@@ -138,7 +143,7 @@ class Signature {
|
|
|
138
143
|
throw new Error('Header byte should be 0x30');
|
|
139
144
|
}
|
|
140
145
|
let length = buf[1];
|
|
141
|
-
const buflength = buf.
|
|
146
|
+
const buflength = buf.slice(2).length;
|
|
142
147
|
if (strict && length !== buflength) {
|
|
143
148
|
throw new Error('Length byte should length of what follows');
|
|
144
149
|
}
|
|
@@ -148,8 +153,8 @@ class Signature {
|
|
|
148
153
|
throw new Error('Integer byte for r should be 0x02');
|
|
149
154
|
}
|
|
150
155
|
const rlength = buf[2 + 1];
|
|
151
|
-
const rbuf = buf.
|
|
152
|
-
const r = new
|
|
156
|
+
const rbuf = buf.slice(2 + 2, 2 + 2 + rlength);
|
|
157
|
+
const r = new bn_1.BN(rbuf, 'be');
|
|
153
158
|
if (rlength !== rbuf.length) {
|
|
154
159
|
throw new Error('Length of r incorrect');
|
|
155
160
|
}
|
|
@@ -158,8 +163,8 @@ class Signature {
|
|
|
158
163
|
throw new Error('Integer byte for s should be 0x02');
|
|
159
164
|
}
|
|
160
165
|
const slength = buf[2 + 2 + rlength + 1];
|
|
161
|
-
const sbuf = buf.
|
|
162
|
-
const s = new
|
|
166
|
+
const sbuf = buf.slice(2 + 2 + rlength + 2, 2 + 2 + rlength + 2 + slength);
|
|
167
|
+
const s = new bn_1.BN(sbuf, 'be');
|
|
163
168
|
if (slength !== sbuf.length) {
|
|
164
169
|
throw new Error('Length of s incorrect');
|
|
165
170
|
}
|
|
@@ -182,34 +187,38 @@ class Signature {
|
|
|
182
187
|
if (isCompressed === false) {
|
|
183
188
|
val = val - 4;
|
|
184
189
|
}
|
|
185
|
-
const b1 =
|
|
186
|
-
const b2 = this.r.
|
|
187
|
-
const b3 = this.s.
|
|
188
|
-
return
|
|
190
|
+
const b1 = util_1.BufferUtil.from([val]);
|
|
191
|
+
const b2 = this.r.toBuffer({ size: 32 });
|
|
192
|
+
const b3 = this.s.toBuffer({ size: 32 });
|
|
193
|
+
return util_1.BufferUtil.concat([b1, b2, b3]);
|
|
189
194
|
}
|
|
190
195
|
toDER(signingMethod = 'ecdsa') {
|
|
191
196
|
if (signingMethod === 'schnorr') {
|
|
192
|
-
return
|
|
193
|
-
this.r.
|
|
194
|
-
this.s.
|
|
197
|
+
return util_1.BufferUtil.concat([
|
|
198
|
+
this.r.toBuffer({ size: 32 }),
|
|
199
|
+
this.s.toBuffer({ size: 32 }),
|
|
195
200
|
]);
|
|
196
201
|
}
|
|
197
|
-
const rnbuf = this.r.
|
|
198
|
-
const snbuf = this.s.
|
|
202
|
+
const rnbuf = this.r.toBuffer();
|
|
203
|
+
const snbuf = this.s.toBuffer();
|
|
199
204
|
const rneg = (rnbuf[0] & 0x80) !== 0;
|
|
200
205
|
const sneg = (snbuf[0] & 0x80) !== 0;
|
|
201
|
-
const rbuf = rneg
|
|
202
|
-
|
|
206
|
+
const rbuf = rneg
|
|
207
|
+
? util_1.BufferUtil.concat([util_1.BufferUtil.from([0x00]), rnbuf])
|
|
208
|
+
: rnbuf;
|
|
209
|
+
const sbuf = sneg
|
|
210
|
+
? util_1.BufferUtil.concat([util_1.BufferUtil.from([0x00]), snbuf])
|
|
211
|
+
: snbuf;
|
|
203
212
|
const rlength = rbuf.length;
|
|
204
213
|
const slength = sbuf.length;
|
|
205
214
|
const length = 2 + rlength + 2 + slength;
|
|
206
215
|
const rheader = 0x02;
|
|
207
216
|
const sheader = 0x02;
|
|
208
217
|
const header = 0x30;
|
|
209
|
-
return
|
|
210
|
-
|
|
218
|
+
return util_1.BufferUtil.concat([
|
|
219
|
+
util_1.BufferUtil.from([header, length, rheader, rlength]),
|
|
211
220
|
rbuf,
|
|
212
|
-
|
|
221
|
+
util_1.BufferUtil.from([sheader, slength]),
|
|
213
222
|
sbuf,
|
|
214
223
|
]);
|
|
215
224
|
}
|
|
@@ -224,9 +233,12 @@ class Signature {
|
|
|
224
233
|
}
|
|
225
234
|
toTxFormat(signingMethod) {
|
|
226
235
|
const derbuf = this.toDER(signingMethod);
|
|
227
|
-
const buf =
|
|
228
|
-
|
|
229
|
-
|
|
236
|
+
const buf = util_1.BufferUtil.alloc(1);
|
|
237
|
+
const defaultSighash = this.isSchnorr
|
|
238
|
+
? Signature.SIGHASH_ALL | Signature.SIGHASH_LOTUS
|
|
239
|
+
: 0;
|
|
240
|
+
buf.writeUInt8(this.nhashtype ?? defaultSighash, 0);
|
|
241
|
+
return util_1.BufferUtil.concat([derbuf, buf]);
|
|
230
242
|
}
|
|
231
243
|
static isDER(buf) {
|
|
232
244
|
if (buf.length < 8 || buf.length > 72) {
|
|
@@ -274,8 +286,8 @@ class Signature {
|
|
|
274
286
|
return true;
|
|
275
287
|
}
|
|
276
288
|
hasLowS() {
|
|
277
|
-
const lowSThreshold =
|
|
278
|
-
if (this.s.lt(
|
|
289
|
+
const lowSThreshold = bn_1.BN.fromString('7FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF5D576E7357A4501DDFE92F46681B20A0', 'hex');
|
|
290
|
+
if (this.s.lt(bn_1.BN.One) || this.s.gt(lowSThreshold)) {
|
|
279
291
|
return false;
|
|
280
292
|
}
|
|
281
293
|
return true;
|
|
@@ -284,21 +296,21 @@ class Signature {
|
|
|
284
296
|
if (typeof this.nhashtype !== 'number') {
|
|
285
297
|
return false;
|
|
286
298
|
}
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
return
|
|
291
|
-
baseType <= Signature.SIGHASH_SINGLE);
|
|
299
|
+
const baseType = this.nhashtype & 0x1f;
|
|
300
|
+
if (baseType < Signature.SIGHASH_ALL ||
|
|
301
|
+
baseType > Signature.SIGHASH_SINGLE) {
|
|
302
|
+
return false;
|
|
292
303
|
}
|
|
293
|
-
const
|
|
294
|
-
|
|
295
|
-
|
|
304
|
+
const algorithm = this.nhashtype & Signature.SIGHASH_ALGORITHM_MASK;
|
|
305
|
+
if (algorithm !== 0x00 &&
|
|
306
|
+
algorithm !== Signature.SIGHASH_FORKID &&
|
|
307
|
+
algorithm !== Signature.SIGHASH_LOTUS) {
|
|
296
308
|
return false;
|
|
297
309
|
}
|
|
298
310
|
return true;
|
|
299
311
|
}
|
|
300
312
|
static isTxDER(buf) {
|
|
301
|
-
return Signature.isDER(buf.
|
|
313
|
+
return Signature.isDER(buf.slice(0, buf.length - 1));
|
|
302
314
|
}
|
|
303
315
|
}
|
|
304
316
|
exports.Signature = Signature;
|
|
@@ -5,11 +5,12 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
5
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
6
|
exports.Base58 = void 0;
|
|
7
7
|
const bs58_1 = __importDefault(require("bs58"));
|
|
8
|
+
const util_1 = require("../util");
|
|
8
9
|
const ALPHABET = '123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz'.split('');
|
|
9
10
|
class Base58 {
|
|
10
11
|
buf;
|
|
11
12
|
constructor(obj) {
|
|
12
|
-
if (
|
|
13
|
+
if (util_1.BufferUtil.isBuffer(obj)) {
|
|
13
14
|
const buf = obj;
|
|
14
15
|
this.fromBuffer(buf);
|
|
15
16
|
}
|
|
@@ -22,7 +23,7 @@ class Base58 {
|
|
|
22
23
|
}
|
|
23
24
|
}
|
|
24
25
|
static validCharacters(chars) {
|
|
25
|
-
if (
|
|
26
|
+
if (util_1.BufferUtil.isBuffer(chars)) {
|
|
26
27
|
chars = chars.toString();
|
|
27
28
|
}
|
|
28
29
|
return Array.from(chars).every(char => ALPHABET.includes(char));
|
|
@@ -32,7 +33,7 @@ class Base58 {
|
|
|
32
33
|
return this;
|
|
33
34
|
}
|
|
34
35
|
static encode(buf) {
|
|
35
|
-
if (!
|
|
36
|
+
if (!util_1.BufferUtil.isBuffer(buf)) {
|
|
36
37
|
throw new Error('Input should be a buffer');
|
|
37
38
|
}
|
|
38
39
|
return bs58_1.default.encode(buf);
|
|
@@ -41,7 +42,7 @@ class Base58 {
|
|
|
41
42
|
if (typeof str !== 'string') {
|
|
42
43
|
throw new Error('Input should be a string');
|
|
43
44
|
}
|
|
44
|
-
return
|
|
45
|
+
return util_1.BufferUtil.from(bs58_1.default.decode(str));
|
|
45
46
|
}
|
|
46
47
|
fromBuffer(buf) {
|
|
47
48
|
this.buf = buf;
|
|
@@ -1,86 +1,43 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
2
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
6
|
exports.Base58Check = void 0;
|
|
4
|
-
const
|
|
5
|
-
const
|
|
6
|
-
const
|
|
7
|
-
for (let i = 0; i < ALPHABET.length; i++) {
|
|
8
|
-
ALPHABET_MAP[ALPHABET[i]] = i;
|
|
9
|
-
}
|
|
7
|
+
const sha2_1 = require("@noble/hashes/sha2");
|
|
8
|
+
const bs58_1 = __importDefault(require("bs58"));
|
|
9
|
+
const util_1 = require("../util");
|
|
10
10
|
class Base58Check {
|
|
11
11
|
static encode(buf) {
|
|
12
|
-
if (!
|
|
12
|
+
if (!util_1.BufferUtil.isBuffer(buf)) {
|
|
13
13
|
throw new Error('Input must be a Buffer');
|
|
14
14
|
}
|
|
15
15
|
const checksum = this.checksum(buf);
|
|
16
|
-
const payload =
|
|
17
|
-
return
|
|
16
|
+
const payload = util_1.BufferUtil.concat([buf, checksum]);
|
|
17
|
+
return bs58_1.default.encode(payload);
|
|
18
18
|
}
|
|
19
19
|
static decode(str) {
|
|
20
20
|
if (typeof str !== 'string') {
|
|
21
21
|
throw new Error('Input must be a string');
|
|
22
22
|
}
|
|
23
|
-
const buf =
|
|
23
|
+
const buf = util_1.BufferUtil.from(bs58_1.default.decode(str));
|
|
24
24
|
if (buf.length < 4) {
|
|
25
25
|
throw new Error('Input string too short');
|
|
26
26
|
}
|
|
27
|
-
const data = buf.
|
|
28
|
-
const checksum = buf.
|
|
27
|
+
const data = buf.slice(0, -4);
|
|
28
|
+
const checksum = buf.slice(-4);
|
|
29
29
|
if (!this.validChecksum(data, checksum)) {
|
|
30
30
|
throw new Error('Invalid checksum');
|
|
31
31
|
}
|
|
32
32
|
return data;
|
|
33
33
|
}
|
|
34
34
|
static checksum(data) {
|
|
35
|
-
const
|
|
36
|
-
|
|
37
|
-
return Buffer.from(hash2).subarray(0, 4);
|
|
35
|
+
const hash = (0, sha2_1.sha256)((0, sha2_1.sha256)(data));
|
|
36
|
+
return util_1.BufferUtil.from(hash).slice(0, 4);
|
|
38
37
|
}
|
|
39
38
|
static validChecksum(data, checksum) {
|
|
40
39
|
const calculatedChecksum = this.checksum(data);
|
|
41
40
|
return calculatedChecksum.equals(checksum);
|
|
42
41
|
}
|
|
43
|
-
static base58Encode(buf) {
|
|
44
|
-
if (buf.length === 0)
|
|
45
|
-
return '';
|
|
46
|
-
let num = 0n;
|
|
47
|
-
for (let i = 0; i < buf.length; i++) {
|
|
48
|
-
num = num * 256n + BigInt(buf[i]);
|
|
49
|
-
}
|
|
50
|
-
let result = '';
|
|
51
|
-
while (num > 0n) {
|
|
52
|
-
result = ALPHABET[Number(num % 58n)] + result;
|
|
53
|
-
num = num / 58n;
|
|
54
|
-
}
|
|
55
|
-
for (let i = 0; i < buf.length && buf[i] === 0; i++) {
|
|
56
|
-
result = '1' + result;
|
|
57
|
-
}
|
|
58
|
-
return result;
|
|
59
|
-
}
|
|
60
|
-
static base58Decode(str) {
|
|
61
|
-
if (str.length === 0)
|
|
62
|
-
return Buffer.alloc(0);
|
|
63
|
-
let num = 0n;
|
|
64
|
-
let leadingZeros = 0;
|
|
65
|
-
for (let i = 0; i < str.length; i++) {
|
|
66
|
-
const char = str[i];
|
|
67
|
-
if (ALPHABET_MAP[char] === undefined) {
|
|
68
|
-
throw new Error(`Invalid character: ${char}`);
|
|
69
|
-
}
|
|
70
|
-
num = num * 58n + BigInt(ALPHABET_MAP[char]);
|
|
71
|
-
}
|
|
72
|
-
for (let i = 0; i < str.length && str[i] === '1'; i++) {
|
|
73
|
-
leadingZeros++;
|
|
74
|
-
}
|
|
75
|
-
const bytes = [];
|
|
76
|
-
while (num > 0n) {
|
|
77
|
-
bytes.unshift(Number(num % 256n));
|
|
78
|
-
num = num / 256n;
|
|
79
|
-
}
|
|
80
|
-
for (let i = 0; i < leadingZeros; i++) {
|
|
81
|
-
bytes.unshift(0);
|
|
82
|
-
}
|
|
83
|
-
return Buffer.from(bytes);
|
|
84
|
-
}
|
|
85
42
|
}
|
|
86
43
|
exports.Base58Check = Base58Check;
|