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,4 +1,6 @@
|
|
|
1
|
-
import { BN } from './bn
|
|
1
|
+
import { BN } from './bn';
|
|
2
|
+
import { BufferUtil } from '../util';
|
|
3
|
+
import { isSchnorrSignature } from './sigtype';
|
|
2
4
|
export class Signature {
|
|
3
5
|
r;
|
|
4
6
|
s;
|
|
@@ -12,6 +14,8 @@ export class Signature {
|
|
|
12
14
|
static SIGHASH_FORKID = 0x40;
|
|
13
15
|
static SIGHASH_LOTUS = 0x60;
|
|
14
16
|
static SIGHASH_ANYONECANPAY = 0x80;
|
|
17
|
+
static SIGHASH_RESERVED = 0x20;
|
|
18
|
+
static SIGHASH_ALGORITHM_MASK = 0x60;
|
|
15
19
|
constructor(r, s, isSchnorr) {
|
|
16
20
|
if (r instanceof BN) {
|
|
17
21
|
this.set({
|
|
@@ -38,18 +42,18 @@ export class Signature {
|
|
|
38
42
|
return this;
|
|
39
43
|
}
|
|
40
44
|
static fromCompact(buf) {
|
|
41
|
-
if (!
|
|
45
|
+
if (!BufferUtil.isBuffer(buf)) {
|
|
42
46
|
throw new Error('Argument is expected to be a Buffer');
|
|
43
47
|
}
|
|
44
48
|
const sig = new Signature(new BN(0), new BN(0));
|
|
45
49
|
let compressed = true;
|
|
46
|
-
let i = buf.
|
|
50
|
+
let i = buf.slice(0, 1)[0] - 27 - 4;
|
|
47
51
|
if (i < 0) {
|
|
48
52
|
compressed = false;
|
|
49
53
|
i = i + 4;
|
|
50
54
|
}
|
|
51
|
-
const b2 = buf.
|
|
52
|
-
const b3 = buf.
|
|
55
|
+
const b2 = buf.slice(1, 33);
|
|
56
|
+
const b3 = buf.slice(33, 65);
|
|
53
57
|
if (!(i === 0 || i === 1 || i === 2 || i === 3)) {
|
|
54
58
|
throw new Error('i must be 0, 1, 2, or 3');
|
|
55
59
|
}
|
|
@@ -61,12 +65,13 @@ export class Signature {
|
|
|
61
65
|
}
|
|
62
66
|
sig.compressed = compressed;
|
|
63
67
|
sig.i = i;
|
|
64
|
-
sig.r =
|
|
65
|
-
sig.s =
|
|
68
|
+
sig.r = BN.fromBuffer(b2);
|
|
69
|
+
sig.s = BN.fromBuffer(b3);
|
|
66
70
|
return sig;
|
|
67
71
|
}
|
|
68
72
|
static fromDER(buf, strict = true) {
|
|
69
|
-
|
|
73
|
+
const rawSig = buf.length === 65 && buf[0] !== 0x30 ? buf.slice(0, 64) : buf;
|
|
74
|
+
if (isSchnorrSignature(rawSig) && buf[0] !== 0x30) {
|
|
70
75
|
const obj = Signature.parseSchnorrEncodedSig(buf);
|
|
71
76
|
const sig = new Signature(new BN(0), new BN(0));
|
|
72
77
|
sig.r = obj.r;
|
|
@@ -74,7 +79,7 @@ export class Signature {
|
|
|
74
79
|
sig.isSchnorr = true;
|
|
75
80
|
return sig;
|
|
76
81
|
}
|
|
77
|
-
if (buf
|
|
82
|
+
if (isSchnorrSignature(buf) && buf[0] === 0x30) {
|
|
78
83
|
throw new Error('64 DER (ecdsa) signatures not allowed');
|
|
79
84
|
}
|
|
80
85
|
const obj = Signature.parseDER(buf, strict);
|
|
@@ -88,17 +93,17 @@ export class Signature {
|
|
|
88
93
|
}
|
|
89
94
|
static fromTxFormat(buf) {
|
|
90
95
|
const nhashtype = buf.readUInt8(buf.length - 1);
|
|
91
|
-
const derbuf = buf.
|
|
96
|
+
const derbuf = buf.slice(0, buf.length - 1);
|
|
92
97
|
const sig = Signature.fromDER(derbuf, false);
|
|
93
98
|
sig.nhashtype = nhashtype;
|
|
94
99
|
return sig;
|
|
95
100
|
}
|
|
96
101
|
static fromDataFormat(buf) {
|
|
97
|
-
const derbuf = buf.
|
|
102
|
+
const derbuf = buf.slice(0, buf.length);
|
|
98
103
|
return Signature.fromDER(derbuf, false);
|
|
99
104
|
}
|
|
100
105
|
static fromString(str) {
|
|
101
|
-
const buf =
|
|
106
|
+
const buf = BufferUtil.from(str, 'hex');
|
|
102
107
|
return Signature.fromDER(buf);
|
|
103
108
|
}
|
|
104
109
|
static fromSchnorr(buf) {
|
|
@@ -114,20 +119,20 @@ export class Signature {
|
|
|
114
119
|
if (buf.length !== 64 && buf.length !== 65) {
|
|
115
120
|
throw new Error(`Invalid Schnorr signature length: ${buf.length} bytes (expected 64 or 65)`);
|
|
116
121
|
}
|
|
117
|
-
const r = buf.
|
|
118
|
-
const s = buf.
|
|
122
|
+
const r = buf.slice(0, 32);
|
|
123
|
+
const s = buf.slice(32, 64);
|
|
119
124
|
let hashtype;
|
|
120
125
|
if (buf.length === 65) {
|
|
121
|
-
hashtype = buf.
|
|
126
|
+
hashtype = buf.slice(64, 65);
|
|
122
127
|
}
|
|
123
128
|
return {
|
|
124
|
-
r:
|
|
125
|
-
s:
|
|
129
|
+
r: BN.fromBuffer(r),
|
|
130
|
+
s: BN.fromBuffer(s),
|
|
126
131
|
nhashtype: hashtype,
|
|
127
132
|
};
|
|
128
133
|
}
|
|
129
134
|
static parseDER(buf, strict = true) {
|
|
130
|
-
if (!
|
|
135
|
+
if (!BufferUtil.isBuffer(buf)) {
|
|
131
136
|
throw new Error('DER formatted signature should be a buffer');
|
|
132
137
|
}
|
|
133
138
|
const header = buf[0];
|
|
@@ -135,7 +140,7 @@ export class Signature {
|
|
|
135
140
|
throw new Error('Header byte should be 0x30');
|
|
136
141
|
}
|
|
137
142
|
let length = buf[1];
|
|
138
|
-
const buflength = buf.
|
|
143
|
+
const buflength = buf.slice(2).length;
|
|
139
144
|
if (strict && length !== buflength) {
|
|
140
145
|
throw new Error('Length byte should length of what follows');
|
|
141
146
|
}
|
|
@@ -145,7 +150,7 @@ export class Signature {
|
|
|
145
150
|
throw new Error('Integer byte for r should be 0x02');
|
|
146
151
|
}
|
|
147
152
|
const rlength = buf[2 + 1];
|
|
148
|
-
const rbuf = buf.
|
|
153
|
+
const rbuf = buf.slice(2 + 2, 2 + 2 + rlength);
|
|
149
154
|
const r = new BN(rbuf, 'be');
|
|
150
155
|
if (rlength !== rbuf.length) {
|
|
151
156
|
throw new Error('Length of r incorrect');
|
|
@@ -155,7 +160,7 @@ export class Signature {
|
|
|
155
160
|
throw new Error('Integer byte for s should be 0x02');
|
|
156
161
|
}
|
|
157
162
|
const slength = buf[2 + 2 + rlength + 1];
|
|
158
|
-
const sbuf = buf.
|
|
163
|
+
const sbuf = buf.slice(2 + 2 + rlength + 2, 2 + 2 + rlength + 2 + slength);
|
|
159
164
|
const s = new BN(sbuf, 'be');
|
|
160
165
|
if (slength !== sbuf.length) {
|
|
161
166
|
throw new Error('Length of s incorrect');
|
|
@@ -179,34 +184,38 @@ export class Signature {
|
|
|
179
184
|
if (isCompressed === false) {
|
|
180
185
|
val = val - 4;
|
|
181
186
|
}
|
|
182
|
-
const b1 =
|
|
183
|
-
const b2 = this.r.
|
|
184
|
-
const b3 = this.s.
|
|
185
|
-
return
|
|
187
|
+
const b1 = BufferUtil.from([val]);
|
|
188
|
+
const b2 = this.r.toBuffer({ size: 32 });
|
|
189
|
+
const b3 = this.s.toBuffer({ size: 32 });
|
|
190
|
+
return BufferUtil.concat([b1, b2, b3]);
|
|
186
191
|
}
|
|
187
192
|
toDER(signingMethod = 'ecdsa') {
|
|
188
193
|
if (signingMethod === 'schnorr') {
|
|
189
|
-
return
|
|
190
|
-
this.r.
|
|
191
|
-
this.s.
|
|
194
|
+
return BufferUtil.concat([
|
|
195
|
+
this.r.toBuffer({ size: 32 }),
|
|
196
|
+
this.s.toBuffer({ size: 32 }),
|
|
192
197
|
]);
|
|
193
198
|
}
|
|
194
|
-
const rnbuf = this.r.
|
|
195
|
-
const snbuf = this.s.
|
|
199
|
+
const rnbuf = this.r.toBuffer();
|
|
200
|
+
const snbuf = this.s.toBuffer();
|
|
196
201
|
const rneg = (rnbuf[0] & 0x80) !== 0;
|
|
197
202
|
const sneg = (snbuf[0] & 0x80) !== 0;
|
|
198
|
-
const rbuf = rneg
|
|
199
|
-
|
|
203
|
+
const rbuf = rneg
|
|
204
|
+
? BufferUtil.concat([BufferUtil.from([0x00]), rnbuf])
|
|
205
|
+
: rnbuf;
|
|
206
|
+
const sbuf = sneg
|
|
207
|
+
? BufferUtil.concat([BufferUtil.from([0x00]), snbuf])
|
|
208
|
+
: snbuf;
|
|
200
209
|
const rlength = rbuf.length;
|
|
201
210
|
const slength = sbuf.length;
|
|
202
211
|
const length = 2 + rlength + 2 + slength;
|
|
203
212
|
const rheader = 0x02;
|
|
204
213
|
const sheader = 0x02;
|
|
205
214
|
const header = 0x30;
|
|
206
|
-
return
|
|
207
|
-
|
|
215
|
+
return BufferUtil.concat([
|
|
216
|
+
BufferUtil.from([header, length, rheader, rlength]),
|
|
208
217
|
rbuf,
|
|
209
|
-
|
|
218
|
+
BufferUtil.from([sheader, slength]),
|
|
210
219
|
sbuf,
|
|
211
220
|
]);
|
|
212
221
|
}
|
|
@@ -221,9 +230,12 @@ export class Signature {
|
|
|
221
230
|
}
|
|
222
231
|
toTxFormat(signingMethod) {
|
|
223
232
|
const derbuf = this.toDER(signingMethod);
|
|
224
|
-
const buf =
|
|
225
|
-
|
|
226
|
-
|
|
233
|
+
const buf = BufferUtil.alloc(1);
|
|
234
|
+
const defaultSighash = this.isSchnorr
|
|
235
|
+
? Signature.SIGHASH_ALL | Signature.SIGHASH_LOTUS
|
|
236
|
+
: 0;
|
|
237
|
+
buf.writeUInt8(this.nhashtype ?? defaultSighash, 0);
|
|
238
|
+
return BufferUtil.concat([derbuf, buf]);
|
|
227
239
|
}
|
|
228
240
|
static isDER(buf) {
|
|
229
241
|
if (buf.length < 8 || buf.length > 72) {
|
|
@@ -271,8 +283,8 @@ export class Signature {
|
|
|
271
283
|
return true;
|
|
272
284
|
}
|
|
273
285
|
hasLowS() {
|
|
274
|
-
const lowSThreshold =
|
|
275
|
-
if (this.s.lt(
|
|
286
|
+
const lowSThreshold = BN.fromString('7FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF5D576E7357A4501DDFE92F46681B20A0', 'hex');
|
|
287
|
+
if (this.s.lt(BN.One) || this.s.gt(lowSThreshold)) {
|
|
276
288
|
return false;
|
|
277
289
|
}
|
|
278
290
|
return true;
|
|
@@ -281,20 +293,20 @@ export class Signature {
|
|
|
281
293
|
if (typeof this.nhashtype !== 'number') {
|
|
282
294
|
return false;
|
|
283
295
|
}
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
return
|
|
288
|
-
baseType <= Signature.SIGHASH_SINGLE);
|
|
296
|
+
const baseType = this.nhashtype & 0x1f;
|
|
297
|
+
if (baseType < Signature.SIGHASH_ALL ||
|
|
298
|
+
baseType > Signature.SIGHASH_SINGLE) {
|
|
299
|
+
return false;
|
|
289
300
|
}
|
|
290
|
-
const
|
|
291
|
-
|
|
292
|
-
|
|
301
|
+
const algorithm = this.nhashtype & Signature.SIGHASH_ALGORITHM_MASK;
|
|
302
|
+
if (algorithm !== 0x00 &&
|
|
303
|
+
algorithm !== Signature.SIGHASH_FORKID &&
|
|
304
|
+
algorithm !== Signature.SIGHASH_LOTUS) {
|
|
293
305
|
return false;
|
|
294
306
|
}
|
|
295
307
|
return true;
|
|
296
308
|
}
|
|
297
309
|
static isTxDER(buf) {
|
|
298
|
-
return Signature.isDER(buf.
|
|
310
|
+
return Signature.isDER(buf.slice(0, buf.length - 1));
|
|
299
311
|
}
|
|
300
312
|
}
|
|
@@ -1,9 +1,10 @@
|
|
|
1
1
|
import bs58 from 'bs58';
|
|
2
|
+
import { BufferUtil } from '../util';
|
|
2
3
|
const ALPHABET = '123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz'.split('');
|
|
3
4
|
export class Base58 {
|
|
4
5
|
buf;
|
|
5
6
|
constructor(obj) {
|
|
6
|
-
if (
|
|
7
|
+
if (BufferUtil.isBuffer(obj)) {
|
|
7
8
|
const buf = obj;
|
|
8
9
|
this.fromBuffer(buf);
|
|
9
10
|
}
|
|
@@ -16,7 +17,7 @@ export class Base58 {
|
|
|
16
17
|
}
|
|
17
18
|
}
|
|
18
19
|
static validCharacters(chars) {
|
|
19
|
-
if (
|
|
20
|
+
if (BufferUtil.isBuffer(chars)) {
|
|
20
21
|
chars = chars.toString();
|
|
21
22
|
}
|
|
22
23
|
return Array.from(chars).every(char => ALPHABET.includes(char));
|
|
@@ -26,7 +27,7 @@ export class Base58 {
|
|
|
26
27
|
return this;
|
|
27
28
|
}
|
|
28
29
|
static encode(buf) {
|
|
29
|
-
if (!
|
|
30
|
+
if (!BufferUtil.isBuffer(buf)) {
|
|
30
31
|
throw new Error('Input should be a buffer');
|
|
31
32
|
}
|
|
32
33
|
return bs58.encode(buf);
|
|
@@ -35,7 +36,7 @@ export class Base58 {
|
|
|
35
36
|
if (typeof str !== 'string') {
|
|
36
37
|
throw new Error('Input should be a string');
|
|
37
38
|
}
|
|
38
|
-
return
|
|
39
|
+
return BufferUtil.from(bs58.decode(str));
|
|
39
40
|
}
|
|
40
41
|
fromBuffer(buf) {
|
|
41
42
|
this.buf = buf;
|
|
@@ -1,82 +1,36 @@
|
|
|
1
|
-
import { sha256 } from '@noble/hashes/
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
for (let i = 0; i < ALPHABET.length; i++) {
|
|
5
|
-
ALPHABET_MAP[ALPHABET[i]] = i;
|
|
6
|
-
}
|
|
1
|
+
import { sha256 } from '@noble/hashes/sha2';
|
|
2
|
+
import bs58 from 'bs58';
|
|
3
|
+
import { BufferUtil } from '../util';
|
|
7
4
|
export class Base58Check {
|
|
8
5
|
static encode(buf) {
|
|
9
|
-
if (!
|
|
6
|
+
if (!BufferUtil.isBuffer(buf)) {
|
|
10
7
|
throw new Error('Input must be a Buffer');
|
|
11
8
|
}
|
|
12
9
|
const checksum = this.checksum(buf);
|
|
13
|
-
const payload =
|
|
14
|
-
return
|
|
10
|
+
const payload = BufferUtil.concat([buf, checksum]);
|
|
11
|
+
return bs58.encode(payload);
|
|
15
12
|
}
|
|
16
13
|
static decode(str) {
|
|
17
14
|
if (typeof str !== 'string') {
|
|
18
15
|
throw new Error('Input must be a string');
|
|
19
16
|
}
|
|
20
|
-
const buf =
|
|
17
|
+
const buf = BufferUtil.from(bs58.decode(str));
|
|
21
18
|
if (buf.length < 4) {
|
|
22
19
|
throw new Error('Input string too short');
|
|
23
20
|
}
|
|
24
|
-
const data = buf.
|
|
25
|
-
const checksum = buf.
|
|
21
|
+
const data = buf.slice(0, -4);
|
|
22
|
+
const checksum = buf.slice(-4);
|
|
26
23
|
if (!this.validChecksum(data, checksum)) {
|
|
27
24
|
throw new Error('Invalid checksum');
|
|
28
25
|
}
|
|
29
26
|
return data;
|
|
30
27
|
}
|
|
31
28
|
static checksum(data) {
|
|
32
|
-
const
|
|
33
|
-
|
|
34
|
-
return Buffer.from(hash2).subarray(0, 4);
|
|
29
|
+
const hash = sha256(sha256(data));
|
|
30
|
+
return BufferUtil.from(hash).slice(0, 4);
|
|
35
31
|
}
|
|
36
32
|
static validChecksum(data, checksum) {
|
|
37
33
|
const calculatedChecksum = this.checksum(data);
|
|
38
34
|
return calculatedChecksum.equals(checksum);
|
|
39
35
|
}
|
|
40
|
-
static base58Encode(buf) {
|
|
41
|
-
if (buf.length === 0)
|
|
42
|
-
return '';
|
|
43
|
-
let num = 0n;
|
|
44
|
-
for (let i = 0; i < buf.length; i++) {
|
|
45
|
-
num = num * 256n + BigInt(buf[i]);
|
|
46
|
-
}
|
|
47
|
-
let result = '';
|
|
48
|
-
while (num > 0n) {
|
|
49
|
-
result = ALPHABET[Number(num % 58n)] + result;
|
|
50
|
-
num = num / 58n;
|
|
51
|
-
}
|
|
52
|
-
for (let i = 0; i < buf.length && buf[i] === 0; i++) {
|
|
53
|
-
result = '1' + result;
|
|
54
|
-
}
|
|
55
|
-
return result;
|
|
56
|
-
}
|
|
57
|
-
static base58Decode(str) {
|
|
58
|
-
if (str.length === 0)
|
|
59
|
-
return Buffer.alloc(0);
|
|
60
|
-
let num = 0n;
|
|
61
|
-
let leadingZeros = 0;
|
|
62
|
-
for (let i = 0; i < str.length; i++) {
|
|
63
|
-
const char = str[i];
|
|
64
|
-
if (ALPHABET_MAP[char] === undefined) {
|
|
65
|
-
throw new Error(`Invalid character: ${char}`);
|
|
66
|
-
}
|
|
67
|
-
num = num * 58n + BigInt(ALPHABET_MAP[char]);
|
|
68
|
-
}
|
|
69
|
-
for (let i = 0; i < str.length && str[i] === '1'; i++) {
|
|
70
|
-
leadingZeros++;
|
|
71
|
-
}
|
|
72
|
-
const bytes = [];
|
|
73
|
-
while (num > 0n) {
|
|
74
|
-
bytes.unshift(Number(num % 256n));
|
|
75
|
-
num = num / 256n;
|
|
76
|
-
}
|
|
77
|
-
for (let i = 0; i < leadingZeros; i++) {
|
|
78
|
-
bytes.unshift(0);
|
|
79
|
-
}
|
|
80
|
-
return Buffer.from(bytes);
|
|
81
|
-
}
|
|
82
36
|
}
|
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import { Preconditions } from '../util/preconditions
|
|
2
|
-
import { BN } from '../crypto/bn
|
|
3
|
-
import { BufferUtil } from '../util/buffer
|
|
1
|
+
import { Preconditions } from '../util/preconditions';
|
|
2
|
+
import { BN } from '../crypto/bn';
|
|
3
|
+
import { BufferUtil } from '../util/buffer';
|
|
4
4
|
export class BufferReader {
|
|
5
5
|
buf;
|
|
6
6
|
pos = 0;
|
|
@@ -8,13 +8,13 @@ export class BufferReader {
|
|
|
8
8
|
if (buf === undefined) {
|
|
9
9
|
return;
|
|
10
10
|
}
|
|
11
|
-
if (
|
|
11
|
+
if (BufferUtil.isBuffer(buf)) {
|
|
12
12
|
this.set({
|
|
13
13
|
buf: buf,
|
|
14
14
|
});
|
|
15
15
|
}
|
|
16
16
|
else if (typeof buf === 'string') {
|
|
17
|
-
const b =
|
|
17
|
+
const b = BufferUtil.from(buf, 'hex');
|
|
18
18
|
if (b.length * 2 !== buf.length) {
|
|
19
19
|
throw new TypeError('Invalid hex string');
|
|
20
20
|
}
|
|
@@ -112,7 +112,7 @@ export class BufferReader {
|
|
|
112
112
|
}
|
|
113
113
|
else {
|
|
114
114
|
const data = Array.prototype.slice.call(this.buf, this.pos, this.pos + 8);
|
|
115
|
-
bn = new BN(
|
|
115
|
+
bn = new BN(BufferUtil.from(data), 'le');
|
|
116
116
|
}
|
|
117
117
|
this.pos = this.pos + 8;
|
|
118
118
|
return bn;
|
|
@@ -180,7 +180,7 @@ export class BufferReader {
|
|
|
180
180
|
}
|
|
181
181
|
}
|
|
182
182
|
reverse() {
|
|
183
|
-
const buf =
|
|
183
|
+
const buf = BufferUtil.alloc(this.buf.length);
|
|
184
184
|
for (let i = 0; i < buf.length; i++) {
|
|
185
185
|
buf[i] = this.buf[this.buf.length - 1 - i];
|
|
186
186
|
}
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { BufferUtil } from '../util/buffer
|
|
2
|
-
import { BN } from '../crypto/bn
|
|
1
|
+
import { BufferUtil } from '../util/buffer';
|
|
2
|
+
import { BN } from '../crypto/bn';
|
|
3
3
|
export class BufferWriter {
|
|
4
4
|
bufs = [];
|
|
5
5
|
bufLen = 0;
|
|
@@ -24,7 +24,7 @@ export class BufferWriter {
|
|
|
24
24
|
return this.concat();
|
|
25
25
|
}
|
|
26
26
|
concat() {
|
|
27
|
-
return
|
|
27
|
+
return BufferUtil.concat(this.bufs, this.bufLen);
|
|
28
28
|
}
|
|
29
29
|
write(buf) {
|
|
30
30
|
if (!BufferUtil.isBuffer(buf)) {
|
|
@@ -43,49 +43,49 @@ export class BufferWriter {
|
|
|
43
43
|
return this;
|
|
44
44
|
}
|
|
45
45
|
writeUInt8(n) {
|
|
46
|
-
const buf =
|
|
46
|
+
const buf = BufferUtil.alloc(1);
|
|
47
47
|
buf.writeUInt8(n, 0);
|
|
48
48
|
this.write(buf);
|
|
49
49
|
return this;
|
|
50
50
|
}
|
|
51
51
|
writeUInt16BE(n) {
|
|
52
|
-
const buf =
|
|
52
|
+
const buf = BufferUtil.alloc(2);
|
|
53
53
|
buf.writeUInt16BE(n, 0);
|
|
54
54
|
this.write(buf);
|
|
55
55
|
return this;
|
|
56
56
|
}
|
|
57
57
|
writeUInt16LE(n) {
|
|
58
|
-
const buf =
|
|
58
|
+
const buf = BufferUtil.alloc(2);
|
|
59
59
|
buf.writeUInt16LE(n, 0);
|
|
60
60
|
this.write(buf);
|
|
61
61
|
return this;
|
|
62
62
|
}
|
|
63
63
|
writeUInt32BE(n) {
|
|
64
|
-
const buf =
|
|
64
|
+
const buf = BufferUtil.alloc(4);
|
|
65
65
|
buf.writeUInt32BE(n, 0);
|
|
66
66
|
this.write(buf);
|
|
67
67
|
return this;
|
|
68
68
|
}
|
|
69
69
|
writeInt32LE(n) {
|
|
70
|
-
const buf =
|
|
70
|
+
const buf = BufferUtil.alloc(4);
|
|
71
71
|
buf.writeInt32LE(n, 0);
|
|
72
72
|
this.write(buf);
|
|
73
73
|
return this;
|
|
74
74
|
}
|
|
75
75
|
writeUInt32LE(n) {
|
|
76
|
-
const buf =
|
|
76
|
+
const buf = BufferUtil.alloc(4);
|
|
77
77
|
buf.writeUInt32LE(n, 0);
|
|
78
78
|
this.write(buf);
|
|
79
79
|
return this;
|
|
80
80
|
}
|
|
81
81
|
writeUInt48LE(n) {
|
|
82
|
-
const buf =
|
|
82
|
+
const buf = BufferUtil.alloc(6);
|
|
83
83
|
buf.writeUIntLE(n, 0, 6);
|
|
84
84
|
this.write(buf);
|
|
85
85
|
return this;
|
|
86
86
|
}
|
|
87
87
|
writeUInt56LE(n) {
|
|
88
|
-
const buf =
|
|
88
|
+
const buf = BufferUtil.alloc(7);
|
|
89
89
|
buf.writeUIntLE(n, 0, 7);
|
|
90
90
|
this.write(buf);
|
|
91
91
|
return this;
|
|
@@ -136,21 +136,21 @@ export class BufferWriter {
|
|
|
136
136
|
static varintBufNum(n) {
|
|
137
137
|
let buf;
|
|
138
138
|
if (n < 253) {
|
|
139
|
-
buf =
|
|
139
|
+
buf = BufferUtil.alloc(1);
|
|
140
140
|
buf.writeUInt8(n, 0);
|
|
141
141
|
}
|
|
142
142
|
else if (n < 0x10000) {
|
|
143
|
-
buf =
|
|
143
|
+
buf = BufferUtil.alloc(1 + 2);
|
|
144
144
|
buf.writeUInt8(253, 0);
|
|
145
145
|
buf.writeUInt16LE(n, 1);
|
|
146
146
|
}
|
|
147
147
|
else if (n < 0x100000000) {
|
|
148
|
-
buf =
|
|
148
|
+
buf = BufferUtil.alloc(1 + 4);
|
|
149
149
|
buf.writeUInt8(254, 0);
|
|
150
150
|
buf.writeUInt32LE(n, 1);
|
|
151
151
|
}
|
|
152
152
|
else {
|
|
153
|
-
buf =
|
|
153
|
+
buf = BufferUtil.alloc(1 + 8);
|
|
154
154
|
buf.writeUInt8(255, 0);
|
|
155
155
|
buf.writeInt32LE(n & -1, 1);
|
|
156
156
|
buf.writeUInt32LE(Math.floor(n / 0x100000000), 5);
|
|
@@ -160,18 +160,18 @@ export class BufferWriter {
|
|
|
160
160
|
static varintBufBN(bn) {
|
|
161
161
|
const n = bn.toNumber();
|
|
162
162
|
if (n < 253) {
|
|
163
|
-
const buf =
|
|
163
|
+
const buf = BufferUtil.alloc(1);
|
|
164
164
|
buf.writeUInt8(n, 0);
|
|
165
165
|
return buf;
|
|
166
166
|
}
|
|
167
167
|
else if (n < 0x10000) {
|
|
168
|
-
const buf =
|
|
168
|
+
const buf = BufferUtil.alloc(1 + 2);
|
|
169
169
|
buf.writeUInt8(253, 0);
|
|
170
170
|
buf.writeUInt16LE(n, 1);
|
|
171
171
|
return buf;
|
|
172
172
|
}
|
|
173
173
|
else if (n < 0x100000000) {
|
|
174
|
-
const buf =
|
|
174
|
+
const buf = BufferUtil.alloc(1 + 4);
|
|
175
175
|
buf.writeUInt8(254, 0);
|
|
176
176
|
buf.writeUInt32LE(n, 1);
|
|
177
177
|
return buf;
|
|
@@ -184,6 +184,3 @@ export class BufferWriter {
|
|
|
184
184
|
}
|
|
185
185
|
}
|
|
186
186
|
}
|
|
187
|
-
export function BufferWriterFactory(obj) {
|
|
188
|
-
return new BufferWriter(obj);
|
|
189
|
-
}
|
|
@@ -1,10 +1,11 @@
|
|
|
1
|
-
import { BufferWriter } from './bufferwriter
|
|
2
|
-
import { BufferReader } from './bufferreader
|
|
3
|
-
import { BN } from '../crypto/bn
|
|
1
|
+
import { BufferWriter } from './bufferwriter';
|
|
2
|
+
import { BufferReader } from './bufferreader';
|
|
3
|
+
import { BN } from '../crypto/bn';
|
|
4
|
+
import { BufferUtil } from '../util';
|
|
4
5
|
export class Varint {
|
|
5
|
-
buf =
|
|
6
|
+
buf = BufferUtil.alloc(0);
|
|
6
7
|
constructor(buf) {
|
|
7
|
-
if (
|
|
8
|
+
if (BufferUtil.isBuffer(buf)) {
|
|
8
9
|
this.buf = buf;
|
|
9
10
|
}
|
|
10
11
|
else if (typeof buf === 'number') {
|
|
@@ -26,7 +27,7 @@ export class Varint {
|
|
|
26
27
|
}
|
|
27
28
|
fromString(str) {
|
|
28
29
|
this.set({
|
|
29
|
-
buf:
|
|
30
|
+
buf: BufferUtil.from(str, 'hex'),
|
|
30
31
|
});
|
|
31
32
|
return this;
|
|
32
33
|
}
|