@opcat-labs/opcat 1.0.1 → 1.0.3
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/.cjs2esm.json +18 -0
- package/.mocharc.yaml +1 -1
- package/CHANGELOG.md +12 -0
- package/README.md +6 -0
- package/{lib/address.js → cjs/address.cjs} +77 -72
- package/cjs/block/block.cjs +332 -0
- package/{lib/block/blockheader.js → cjs/block/blockheader.cjs} +8 -7
- package/cjs/block/index.cjs +2 -0
- package/{lib/block/merkleblock.js → cjs/block/merkleblock.cjs} +23 -15
- package/cjs/bn.cjs +3411 -0
- package/{lib/crypto/bn.js → cjs/crypto/bn.cjs} +3 -3
- package/{lib/crypto/ecdsa.js → cjs/crypto/ecdsa.cjs} +150 -14
- package/{lib/crypto/hash.node.js → cjs/crypto/hash.cjs} +13 -2
- package/cjs/crypto/index.cjs +16 -0
- package/{lib/crypto/point.js → cjs/crypto/point.cjs} +11 -4
- package/cjs/crypto/random.cjs +18 -0
- package/{lib/crypto/signature.js → cjs/crypto/signature.cjs} +158 -8
- package/{lib/encoding/base58.js → cjs/encoding/base58.cjs} +58 -2
- package/cjs/encoding/base58check.cjs +192 -0
- package/cjs/encoding/bufferreader.cjs +333 -0
- package/cjs/encoding/bufferwriter.cjs +244 -0
- package/{lib/encoding/decode-asm.js → cjs/encoding/decode-asm.cjs} +4 -4
- package/{lib/encoding/decode-hex.js → cjs/encoding/decode-hex.cjs} +1 -1
- package/cjs/encoding/index.cjs +14 -0
- package/cjs/encoding/varint.cjs +116 -0
- package/{lib/errors/index.js → cjs/errors/index.cjs} +9 -9
- package/{lib/errors/spec.js → cjs/errors/spec.cjs} +2 -2
- package/cjs/hash-cache.cjs +98 -0
- package/{lib/hdprivatekey.js → cjs/hdprivatekey.cjs} +232 -140
- package/{lib/hdpublickey.js → cjs/hdpublickey.cjs} +120 -93
- package/cjs/index.cjs +94 -0
- package/cjs/interpreter/index.cjs +2 -0
- package/cjs/interpreter/interpreter.cjs +1988 -0
- package/{lib/script/stack.js → cjs/interpreter/stack.cjs} +9 -2
- package/{lib/message/message.js → cjs/message/message.cjs} +62 -25
- package/cjs/mnemonic/index.cjs +3 -0
- package/{lib/mnemonic/mnemonic.js → cjs/mnemonic/mnemonic.cjs} +44 -13
- package/{lib/mnemonic/pbkdf2.node.js → cjs/mnemonic/pbkdf2.cjs} +9 -2
- package/cjs/mnemonic/words/index.cjs +66 -0
- package/cjs/network.cjs +13 -0
- package/cjs/networks.cjs +321 -0
- package/{lib/opcode.js → cjs/opcode.cjs} +69 -5
- package/cjs/privatekey.cjs +422 -0
- package/{lib/publickey.js → cjs/publickey.cjs} +14 -16
- package/cjs/script/index.cjs +2 -0
- package/{lib/script/script.js → cjs/script/script.cjs} +322 -67
- package/cjs/transaction/index.cjs +5 -0
- package/cjs/transaction/input/index.cjs +34 -0
- package/cjs/transaction/input/input.cjs +396 -0
- package/{lib/transaction/input/multisig.js → cjs/transaction/input/multisig.cjs} +112 -18
- package/{lib/transaction/input/publickey.js → cjs/transaction/input/publickey.cjs} +29 -19
- package/{lib/transaction/input/publickeyhash.js → cjs/transaction/input/publickeyhash.cjs} +25 -17
- package/{lib/transaction/output.js → cjs/transaction/output.cjs} +100 -15
- package/cjs/transaction/sighash.cjs +187 -0
- package/{lib/transaction/signature.js → cjs/transaction/signature.cjs} +30 -6
- package/cjs/transaction/transaction.cjs +2000 -0
- package/{lib/transaction/unspentoutput.js → cjs/transaction/unspentoutput.cjs} +5 -5
- package/cjs/util/derivation.cjs +53 -0
- package/cjs/util/index.cjs +11 -0
- package/cjs/util/js.cjs +95 -0
- package/{lib/util/preconditions.js → cjs/util/preconditions.cjs} +2 -2
- package/esm/address.js +483 -0
- package/{lib → esm}/block/block.js +82 -27
- package/esm/block/blockheader.js +296 -0
- package/esm/block/index.js +2 -0
- package/esm/block/merkleblock.js +331 -0
- package/esm/bn.js +3411 -0
- package/esm/crypto/bn.js +278 -0
- package/esm/crypto/ecdsa.js +475 -0
- package/{lib/crypto/hash.browser.js → esm/crypto/hash.js} +18 -7
- package/esm/crypto/index.js +16 -0
- package/esm/crypto/point.js +228 -0
- package/esm/crypto/random.js +18 -0
- package/esm/crypto/signature.js +475 -0
- package/esm/encoding/base58.js +167 -0
- package/esm/encoding/base58check.js +192 -0
- package/esm/encoding/bufferreader.js +333 -0
- package/esm/encoding/bufferwriter.js +243 -0
- package/esm/encoding/decode-asm.js +24 -0
- package/esm/encoding/decode-hex.js +32 -0
- package/esm/encoding/decode-script-chunks.js +43 -0
- package/esm/encoding/encode-hex.js +284 -0
- package/esm/encoding/index.js +14 -0
- package/esm/encoding/is-hex.js +7 -0
- package/esm/encoding/varint.js +116 -0
- package/esm/errors/index.js +54 -0
- package/esm/errors/spec.js +314 -0
- package/esm/hash-cache.js +98 -0
- package/esm/hdprivatekey.js +768 -0
- package/esm/hdpublickey.js +549 -0
- package/esm/index.js +66 -0
- package/esm/interpreter/index.js +2 -0
- package/{lib/script → esm/interpreter}/interpreter.js +219 -66
- package/esm/interpreter/stack.js +116 -0
- package/esm/message/message.js +228 -0
- package/esm/mnemonic/index.js +3 -0
- package/esm/mnemonic/mnemonic.js +332 -0
- package/{lib/mnemonic/pbkdf2.browser.js → esm/mnemonic/pbkdf2.js} +13 -6
- package/esm/mnemonic/words/chinese.js +2054 -0
- package/esm/mnemonic/words/english.js +2054 -0
- package/esm/mnemonic/words/french.js +2054 -0
- package/esm/mnemonic/words/index.js +66 -0
- package/esm/mnemonic/words/italian.js +2054 -0
- package/esm/mnemonic/words/japanese.js +2054 -0
- package/esm/mnemonic/words/spanish.js +2054 -0
- package/esm/network.js +13 -0
- package/{lib → esm}/networks.js +61 -120
- package/esm/opcode.js +319 -0
- package/{lib → esm}/privatekey.js +76 -28
- package/esm/publickey.js +384 -0
- package/esm/script/index.js +2 -0
- package/esm/script/script.js +1329 -0
- package/esm/script/write-i32-le.js +17 -0
- package/esm/script/write-push-data.js +35 -0
- package/esm/script/write-u16-le.js +12 -0
- package/esm/script/write-u32-le.js +16 -0
- package/esm/script/write-u64-le.js +24 -0
- package/esm/script/write-u8-le.js +8 -0
- package/esm/script/write-varint.js +46 -0
- package/esm/transaction/index.js +5 -0
- package/esm/transaction/input/index.js +33 -0
- package/{lib → esm}/transaction/input/input.js +132 -90
- package/esm/transaction/input/multisig.js +335 -0
- package/esm/transaction/input/publickey.js +108 -0
- package/esm/transaction/input/publickeyhash.js +124 -0
- package/esm/transaction/output.js +316 -0
- package/{lib → esm}/transaction/sighash.js +42 -22
- package/esm/transaction/signature.js +120 -0
- package/{lib → esm}/transaction/transaction.js +522 -163
- package/esm/transaction/unspentoutput.js +112 -0
- package/esm/util/_.js +47 -0
- package/esm/util/derivation.js +53 -0
- package/esm/util/index.js +12 -0
- package/esm/util/js.js +95 -0
- package/esm/util/preconditions.js +33 -0
- package/fixup.cjs +17 -0
- package/package.json +18 -4
- package/test/{address.js → address.cjs} +14 -43
- package/test/block/{block.js → block.cjs} +3 -5
- package/test/block/{blockheader.js → blockheader.cjs} +2 -2
- package/test/block/{merklebloack.js → merklebloack.cjs} +2 -2
- package/test/crypto/{ecdsa.js → ecdsa.cjs} +9 -9
- package/test/crypto/{hash.browser.js → hash.browser.cjs} +2 -1
- package/test/crypto/{signature.js → signature.cjs} +2 -2
- package/test/data/bitcoind/script_tests.json +5 -5
- package/test/{hashCache.js → hashCache.cjs} +2 -1
- package/test/{hdkeys.js → hdkeys.cjs} +4 -2
- package/test/{hdprivatekey.js → hdprivatekey.cjs} +7 -6
- package/test/{hdpublickey.js → hdpublickey.cjs} +2 -7
- package/test/mnemonic/{pbkdf2.test.js → pbkdf2.test.cjs} +2 -2
- package/test/{networks.js → networks.cjs} +12 -31
- package/test/{publickey.js → publickey.cjs} +2 -2
- package/test/script/{interpreter.js → interpreter.cjs} +5 -5
- package/test/script/{script.js → script.cjs} +8 -2
- package/test/transaction/{deserialize.js → deserialize.cjs} +2 -2
- package/test/transaction/input/{input.js → input.cjs} +1 -1
- package/test/transaction/input/{multisig.js → multisig.cjs} +2 -1
- package/test/transaction/input/{publickeyhash.js → publickeyhash.cjs} +1 -1
- package/test/transaction/{sighash.js → sighash.cjs} +1 -1
- package/test/transaction/{transaction.js → transaction.cjs} +2 -2
- package/tsconfig.json +13 -0
- package/types/address.d.cts +252 -0
- package/types/block/block.d.cts +139 -0
- package/types/block/blockheader.d.cts +125 -0
- package/types/block/index.d.cts +2 -0
- package/types/block/merkleblock.d.cts +95 -0
- package/types/bn.d.cts +202 -0
- package/types/crypto/bn.d.cts +2 -0
- package/types/crypto/ecdsa.d.cts +187 -0
- package/types/crypto/hash.d.cts +118 -0
- package/types/crypto/index.d.cts +7 -0
- package/types/crypto/point.d.cts +134 -0
- package/types/crypto/random.d.cts +13 -0
- package/types/crypto/signature.d.cts +160 -0
- package/types/encoding/base58.d.cts +106 -0
- package/types/encoding/base58check.d.cts +107 -0
- package/types/encoding/bufferreader.d.cts +164 -0
- package/types/encoding/bufferwriter.d.cts +126 -0
- package/types/encoding/decode-asm.d.cts +2 -0
- package/types/encoding/decode-hex.d.cts +2 -0
- package/types/encoding/decode-script-chunks.d.cts +14 -0
- package/types/encoding/encode-hex.d.cts +2 -0
- package/types/encoding/index.d.cts +6 -0
- package/types/encoding/is-hex.d.cts +2 -0
- package/types/encoding/varint.d.cts +66 -0
- package/types/errors/index.d.cts +4 -0
- package/types/errors/spec.d.cts +22 -0
- package/types/hash-cache.d.cts +65 -0
- package/types/hdprivatekey.d.cts +281 -0
- package/types/hdpublickey.d.cts +240 -0
- package/types/index.d.cts +26 -0
- package/types/interpreter/index.d.cts +2 -0
- package/types/interpreter/interpreter.d.cts +228 -0
- package/types/interpreter/stack.d.cts +35 -0
- package/types/message/message.d.cts +110 -0
- package/types/mnemonic/index.d.cts +2 -0
- package/types/mnemonic/mnemonic.d.cts +171 -0
- package/types/mnemonic/pbkdf2.d.cts +14 -0
- package/types/mnemonic/words/chinese.d.cts +2 -0
- package/types/mnemonic/words/english.d.cts +2 -0
- package/types/mnemonic/words/french.d.cts +2 -0
- package/types/mnemonic/words/index.d.cts +22 -0
- package/types/mnemonic/words/italian.d.cts +2 -0
- package/types/mnemonic/words/japanese.d.cts +2 -0
- package/types/mnemonic/words/spanish.d.cts +2 -0
- package/types/network.d.cts +11 -0
- package/types/networks.d.cts +76 -0
- package/types/opcode.d.cts +93 -0
- package/types/privatekey.d.cts +169 -0
- package/types/publickey.d.cts +202 -0
- package/types/script/index.d.cts +2 -0
- package/types/script/script.d.cts +449 -0
- package/types/script/write-i32-le.d.cts +2 -0
- package/types/script/write-push-data.d.cts +2 -0
- package/types/script/write-u16-le.d.cts +2 -0
- package/types/script/write-u32-le.d.cts +2 -0
- package/types/script/write-u64-le.d.cts +2 -0
- package/types/script/write-u8-le.d.cts +2 -0
- package/types/script/write-varint.d.cts +2 -0
- package/types/transaction/index.d.cts +2 -0
- package/types/transaction/input/index.d.cts +2 -0
- package/types/transaction/input/input.d.cts +178 -0
- package/types/transaction/input/multisig.d.cts +127 -0
- package/types/transaction/input/publickey.d.cts +44 -0
- package/types/transaction/input/publickeyhash.d.cts +45 -0
- package/types/transaction/output.d.cts +118 -0
- package/types/transaction/sighash.d.cts +61 -0
- package/types/transaction/signature.d.cts +43 -0
- package/types/transaction/transaction.d.cts +716 -0
- package/types/transaction/unspentoutput.d.cts +83 -0
- package/types/util/_.d.cts +26 -0
- package/types/util/derivation.d.cts +21 -0
- package/types/util/index.d.cts +5 -0
- package/types/util/js.d.cts +50 -0
- package/types/util/preconditions.d.cts +3 -0
- package/index.d.ts +0 -1541
- package/index.js +0 -74
- package/lib/block/index.js +0 -4
- package/lib/bn.js +0 -3423
- package/lib/crypto/hash.js +0 -2
- package/lib/crypto/random.browser.js +0 -28
- package/lib/crypto/random.js +0 -2
- package/lib/crypto/random.node.js +0 -11
- package/lib/encoding/base58check.js +0 -121
- package/lib/encoding/bufferreader.js +0 -212
- package/lib/encoding/bufferwriter.js +0 -140
- package/lib/encoding/varint.js +0 -75
- package/lib/hash-cache.js +0 -50
- package/lib/mnemonic/pbkdf2.js +0 -2
- package/lib/mnemonic/words/index.js +0 -8
- package/lib/script/index.js +0 -5
- package/lib/transaction/index.js +0 -7
- package/lib/transaction/input/index.js +0 -5
- package/lib/util/js.js +0 -90
- /package/{lib/encoding/decode-script-chunks.js → cjs/encoding/decode-script-chunks.cjs} +0 -0
- /package/{lib/encoding/encode-hex.js → cjs/encoding/encode-hex.cjs} +0 -0
- /package/{lib/encoding/is-hex.js → cjs/encoding/is-hex.cjs} +0 -0
- /package/{lib/mnemonic/words/chinese.js → cjs/mnemonic/words/chinese.cjs} +0 -0
- /package/{lib/mnemonic/words/english.js → cjs/mnemonic/words/english.cjs} +0 -0
- /package/{lib/mnemonic/words/french.js → cjs/mnemonic/words/french.cjs} +0 -0
- /package/{lib/mnemonic/words/italian.js → cjs/mnemonic/words/italian.cjs} +0 -0
- /package/{lib/mnemonic/words/japanese.js → cjs/mnemonic/words/japanese.cjs} +0 -0
- /package/{lib/mnemonic/words/spanish.js → cjs/mnemonic/words/spanish.cjs} +0 -0
- /package/{lib/script/write-i32-le.js → cjs/script/write-i32-le.cjs} +0 -0
- /package/{lib/script/write-push-data.js → cjs/script/write-push-data.cjs} +0 -0
- /package/{lib/script/write-u16-le.js → cjs/script/write-u16-le.cjs} +0 -0
- /package/{lib/script/write-u32-le.js → cjs/script/write-u32-le.cjs} +0 -0
- /package/{lib/script/write-u64-le.js → cjs/script/write-u64-le.cjs} +0 -0
- /package/{lib/script/write-u8-le.js → cjs/script/write-u8-le.cjs} +0 -0
- /package/{lib/script/write-varint.js → cjs/script/write-varint.cjs} +0 -0
- /package/{lib/util/_.js → cjs/util/_.cjs} +0 -0
- /package/test/crypto/{bn.js → bn.cjs} +0 -0
- /package/test/crypto/{hash.js → hash.cjs} +0 -0
- /package/test/crypto/{point.js → point.cjs} +0 -0
- /package/test/crypto/{random.js → random.cjs} +0 -0
- /package/test/data/{blk86756-testnet.js → blk86756-testnet.cjs} +0 -0
- /package/test/data/{merkleblocks.js → merkleblocks.cjs} +0 -0
- /package/test/encoding/{base58.js → base58.cjs} +0 -0
- /package/test/encoding/{base58check.js → base58check.cjs} +0 -0
- /package/test/encoding/{bufferreader.js → bufferreader.cjs} +0 -0
- /package/test/encoding/{bufferwriter.js → bufferwriter.cjs} +0 -0
- /package/test/encoding/{varint.js → varint.cjs} +0 -0
- /package/test/{index.js → index.cjs} +0 -0
- /package/test/message/{message.js → message.cjs} +0 -0
- /package/test/mnemonic/{mnemonic.js → mnemonic.cjs} +0 -0
- /package/test/{opcode.js → opcode.cjs} +0 -0
- /package/test/{privatekey.js → privatekey.cjs} +0 -0
- /package/test/transaction/input/{publickey.js → publickey.cjs} +0 -0
- /package/test/transaction/{output.js → output.cjs} +0 -0
- /package/test/transaction/{signature.js → signature.cjs} +0 -0
- /package/test/transaction/{unspentoutput.js → unspentoutput.cjs} +0 -0
- /package/test/util/{js.js → js.cjs} +0 -0
- /package/test/util/{preconditions.js → preconditions.cjs} +0 -0
|
@@ -0,0 +1,167 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
import _ from '../util/_.js';
|
|
4
|
+
import bs58 from 'bs58';
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* The alphabet for the Bitcoin-specific Base 58 encoding distinguishes between
|
|
8
|
+
* lower case L and upper case i - neither of those characters are allowed to
|
|
9
|
+
* prevent accidentaly miscopying of letters.
|
|
10
|
+
*/
|
|
11
|
+
var ALPHABET = '123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz'.split('');
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* A Base58 object can encode/decoded Base 58, which is used primarily for
|
|
15
|
+
* string-formatted Bitcoin addresses and private keys. Addresses and private
|
|
16
|
+
* keys actually use an additional checksum, and so they actually use the
|
|
17
|
+
* Base58Check class.
|
|
18
|
+
*
|
|
19
|
+
* @param {object} obj Can be a string or buffer.
|
|
20
|
+
*/
|
|
21
|
+
function Base58(obj) {
|
|
22
|
+
if (!(this instanceof Base58)) {
|
|
23
|
+
return new Base58(obj);
|
|
24
|
+
}
|
|
25
|
+
if (Buffer.isBuffer(obj)) {
|
|
26
|
+
var buf = obj;
|
|
27
|
+
this.fromBuffer(buf);
|
|
28
|
+
} else if (typeof obj === 'string') {
|
|
29
|
+
var str = obj;
|
|
30
|
+
this.fromString(str);
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* Checks if all characters in the input are valid Base58 characters.
|
|
36
|
+
* @param {string|Buffer} chars - The input characters to validate (can be a string or Buffer).
|
|
37
|
+
* @returns {boolean} True if all characters are valid Base58, false otherwise.
|
|
38
|
+
* @static
|
|
39
|
+
*/
|
|
40
|
+
Base58.validCharacters = function validCharacters(chars) {
|
|
41
|
+
if (Buffer.isBuffer(chars)) {
|
|
42
|
+
chars = chars.toString();
|
|
43
|
+
}
|
|
44
|
+
return _.every(
|
|
45
|
+
_.map(chars, function (char) {
|
|
46
|
+
return _.includes(ALPHABET, char);
|
|
47
|
+
}),
|
|
48
|
+
);
|
|
49
|
+
};
|
|
50
|
+
|
|
51
|
+
/**
|
|
52
|
+
* Sets the buffer property from the given object.
|
|
53
|
+
* @param {Object} obj - The object containing the buffer to set.
|
|
54
|
+
* @param {Buffer} [obj.buf] - The buffer to assign. If not provided, retains current buffer or sets to undefined.
|
|
55
|
+
* @returns {Base58} Returns the instance for chaining.
|
|
56
|
+
*/
|
|
57
|
+
Base58.prototype.set = function (obj) {
|
|
58
|
+
this.buf = obj.buf || this.buf || undefined;
|
|
59
|
+
return this;
|
|
60
|
+
};
|
|
61
|
+
|
|
62
|
+
/**
|
|
63
|
+
* Encode a buffer to Bsae 58.
|
|
64
|
+
*
|
|
65
|
+
* @param {Buffer} buf Any buffer to be encoded.
|
|
66
|
+
* @returns {string} A Base 58 encoded string.
|
|
67
|
+
* @throws {Error} If the input is not a buffer.
|
|
68
|
+
* @static
|
|
69
|
+
*/
|
|
70
|
+
Base58.encode = function (buf) {
|
|
71
|
+
if (!Buffer.isBuffer(buf)) {
|
|
72
|
+
throw new Error('Input should be a buffer');
|
|
73
|
+
}
|
|
74
|
+
return bs58.encode(buf);
|
|
75
|
+
};
|
|
76
|
+
|
|
77
|
+
/**
|
|
78
|
+
* Decode a Base 58 string to a buffer.
|
|
79
|
+
*
|
|
80
|
+
* @param {string} str A Base 58 encoded string.
|
|
81
|
+
* @returns {Buffer} The decoded buffer.
|
|
82
|
+
* @throws {Error} If the input is not a string.
|
|
83
|
+
* @static
|
|
84
|
+
*/
|
|
85
|
+
Base58.decode = function (str) {
|
|
86
|
+
if (typeof str !== 'string') {
|
|
87
|
+
throw new Error('Input should be a string');
|
|
88
|
+
}
|
|
89
|
+
return Buffer.from(bs58.decode(str));
|
|
90
|
+
};
|
|
91
|
+
|
|
92
|
+
/**
|
|
93
|
+
* Sets the internal buffer to the provided buffer and returns the instance for chaining.
|
|
94
|
+
* @param {Buffer} buf - The buffer to set as the internal buffer.
|
|
95
|
+
* @returns {Base58} The instance for method chaining.
|
|
96
|
+
*/
|
|
97
|
+
Base58.prototype.fromBuffer = function (buf) {
|
|
98
|
+
this.buf = buf;
|
|
99
|
+
return this;
|
|
100
|
+
};
|
|
101
|
+
|
|
102
|
+
/**
|
|
103
|
+
* Creates a Base58 encoded string from a buffer.
|
|
104
|
+
* @param {Buffer} buf - The input buffer to encode.
|
|
105
|
+
* @returns {Base58} A new Base58 instance containing the encoded string.
|
|
106
|
+
* @static
|
|
107
|
+
*/
|
|
108
|
+
Base58.fromBuffer = function (buf) {
|
|
109
|
+
return new Base58().fromBuffer(buf);
|
|
110
|
+
};
|
|
111
|
+
|
|
112
|
+
/**
|
|
113
|
+
* Converts a hex string to Base58 encoded string.
|
|
114
|
+
* @param {string} hex - The hex string to convert.
|
|
115
|
+
* @returns {string} The Base58 encoded string.
|
|
116
|
+
* @static
|
|
117
|
+
*/
|
|
118
|
+
Base58.fromHex = function (hex) {
|
|
119
|
+
return Base58.fromBuffer(Buffer.from(hex, 'hex'));
|
|
120
|
+
};
|
|
121
|
+
|
|
122
|
+
/**
|
|
123
|
+
* Converts a Base58 encoded string to a buffer and stores it in the instance.
|
|
124
|
+
* @param {string} str - The Base58 encoded string to decode.
|
|
125
|
+
* @returns {Base58} The current instance for chaining.
|
|
126
|
+
*/
|
|
127
|
+
Base58.prototype.fromString = function (str) {
|
|
128
|
+
var buf = Base58.decode(str);
|
|
129
|
+
this.buf = buf;
|
|
130
|
+
return this;
|
|
131
|
+
};
|
|
132
|
+
|
|
133
|
+
/**
|
|
134
|
+
* Creates a Base58 instance from a string input.
|
|
135
|
+
* @param {string} str - The string to convert to Base58.
|
|
136
|
+
* @returns {Base58} A new Base58 instance containing the encoded string.
|
|
137
|
+
* @static
|
|
138
|
+
*/
|
|
139
|
+
Base58.fromString = function (str) {
|
|
140
|
+
return new Base58().fromString(str);
|
|
141
|
+
};
|
|
142
|
+
|
|
143
|
+
/**
|
|
144
|
+
* Returns the internal buffer containing the Base58 encoded data.
|
|
145
|
+
* @returns {Buffer} The raw buffer representation of the Base58 data.
|
|
146
|
+
*/
|
|
147
|
+
Base58.prototype.toBuffer = function () {
|
|
148
|
+
return this.buf;
|
|
149
|
+
};
|
|
150
|
+
|
|
151
|
+
/**
|
|
152
|
+
* Converts the Base58 encoded data to a hexadecimal string.
|
|
153
|
+
* @returns {string} Hexadecimal representation of the Base58 data.
|
|
154
|
+
*/
|
|
155
|
+
Base58.prototype.toHex = function () {
|
|
156
|
+
return this.toBuffer().toString('hex');
|
|
157
|
+
};
|
|
158
|
+
|
|
159
|
+
/**
|
|
160
|
+
* Converts the Base58 instance to its string representation.
|
|
161
|
+
* @returns {string} The Base58 encoded string.
|
|
162
|
+
*/
|
|
163
|
+
Base58.prototype.toString = function () {
|
|
164
|
+
return Base58.encode(this.buf);
|
|
165
|
+
};
|
|
166
|
+
|
|
167
|
+
export default Base58;
|
|
@@ -0,0 +1,192 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
import _ from '../util/_.js';
|
|
4
|
+
import Base58 from './base58.js';
|
|
5
|
+
import Hash from '../crypto/hash.js';
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* A Base58check object can encode/decodd Base 58, which is used primarily for
|
|
9
|
+
* string-formatted Bitcoin addresses and private keys. This is the same as
|
|
10
|
+
* Base58, except that it includes a checksum to prevent accidental mistypings.
|
|
11
|
+
* @constructor
|
|
12
|
+
* @param {Buffer|string} obj Can be a string or buffer.
|
|
13
|
+
*/
|
|
14
|
+
function Base58Check(obj) {
|
|
15
|
+
if (!(this instanceof Base58Check)) {
|
|
16
|
+
return new Base58Check(obj);
|
|
17
|
+
}
|
|
18
|
+
if (Buffer.isBuffer(obj)) {
|
|
19
|
+
var buf = obj;
|
|
20
|
+
this.fromBuffer(buf);
|
|
21
|
+
} else if (typeof obj === 'string') {
|
|
22
|
+
var str = obj;
|
|
23
|
+
this.fromString(str);
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* Sets the buffer property from the given object.
|
|
29
|
+
* @param {Object} obj - The object containing the buffer to set.
|
|
30
|
+
* @returns {Base58Check} Returns the instance for chaining.
|
|
31
|
+
*/
|
|
32
|
+
Base58Check.prototype.set = function (obj) {
|
|
33
|
+
this.buf = obj.buf || this.buf || undefined;
|
|
34
|
+
return this;
|
|
35
|
+
};
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* Validates the checksum of Base58Check encoded data.
|
|
39
|
+
* @param {Buffer|string} data - The data to validate, either as a Buffer or Base58 encoded string.
|
|
40
|
+
* @param {Buffer|string} [checksum] - Optional checksum to validate against, either as a Buffer or Base58 encoded string.
|
|
41
|
+
* If not provided, the last 4 bytes of the data will be used as checksum.
|
|
42
|
+
* @returns {boolean} True if the computed checksum matches the provided/embedded checksum.
|
|
43
|
+
* @static
|
|
44
|
+
*/
|
|
45
|
+
Base58Check.validChecksum = function validChecksum(data, checksum) {
|
|
46
|
+
if (_.isString(data)) {
|
|
47
|
+
data = Buffer.from(Base58.decode(data));
|
|
48
|
+
}
|
|
49
|
+
if (_.isString(checksum)) {
|
|
50
|
+
checksum = Buffer.from(Base58.decode(checksum));
|
|
51
|
+
}
|
|
52
|
+
if (!checksum) {
|
|
53
|
+
checksum = data.slice(-4);
|
|
54
|
+
data = data.slice(0, -4);
|
|
55
|
+
}
|
|
56
|
+
return Base58Check.checksum(data).toString('hex') === checksum.toString('hex');
|
|
57
|
+
};
|
|
58
|
+
|
|
59
|
+
/**
|
|
60
|
+
* Decodes a Base58Check encoded string and verifies its checksum.
|
|
61
|
+
* @param {string} s - The Base58Check encoded string to decode.
|
|
62
|
+
* @returns {Buffer} The decoded data (excluding checksum).
|
|
63
|
+
* @throws {Error} If input is not a string, too short, or checksum mismatch.
|
|
64
|
+
* @static
|
|
65
|
+
*/
|
|
66
|
+
Base58Check.decode = function (s) {
|
|
67
|
+
if (typeof s !== 'string') {
|
|
68
|
+
throw new Error('Input must be a string');
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
var buf = Buffer.from(Base58.decode(s));
|
|
72
|
+
|
|
73
|
+
if (buf.length < 4) {
|
|
74
|
+
throw new Error('Input string too short');
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
var data = buf.slice(0, -4);
|
|
78
|
+
var csum = buf.slice(-4);
|
|
79
|
+
|
|
80
|
+
var hash = Hash.sha256sha256(data);
|
|
81
|
+
var hash4 = hash.slice(0, 4);
|
|
82
|
+
|
|
83
|
+
if (csum.toString('hex') !== hash4.toString('hex')) {
|
|
84
|
+
throw new Error('Checksum mismatch');
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
return data;
|
|
88
|
+
};
|
|
89
|
+
|
|
90
|
+
/**
|
|
91
|
+
* Calculates the checksum for a given buffer using double SHA-256 hash.
|
|
92
|
+
* The checksum is the first 4 bytes of the double-hashed result.
|
|
93
|
+
* @param {Buffer} buffer - The input buffer to calculate checksum for
|
|
94
|
+
* @returns {Buffer} The 4-byte checksum
|
|
95
|
+
* @static
|
|
96
|
+
*/
|
|
97
|
+
Base58Check.checksum = function (buffer) {
|
|
98
|
+
return Hash.sha256sha256(buffer).slice(0, 4);
|
|
99
|
+
};
|
|
100
|
+
|
|
101
|
+
/**
|
|
102
|
+
* Encodes a buffer into Base58Check format.
|
|
103
|
+
* @param {Buffer} buf - The input buffer to encode
|
|
104
|
+
* @returns {string} The Base58Check encoded string
|
|
105
|
+
* @throws {Error} If input is not a Buffer
|
|
106
|
+
* @static
|
|
107
|
+
*/
|
|
108
|
+
Base58Check.encode = function (buf) {
|
|
109
|
+
if (!Buffer.isBuffer(buf)) {
|
|
110
|
+
throw new Error('Input must be a buffer');
|
|
111
|
+
}
|
|
112
|
+
var checkedBuf = Buffer.alloc(buf.length + 4);
|
|
113
|
+
var hash = Base58Check.checksum(buf);
|
|
114
|
+
buf.copy(checkedBuf);
|
|
115
|
+
hash.copy(checkedBuf, buf.length);
|
|
116
|
+
return Base58.encode(checkedBuf);
|
|
117
|
+
};
|
|
118
|
+
|
|
119
|
+
/**
|
|
120
|
+
* Sets the internal buffer to the provided Buffer object.
|
|
121
|
+
* @param {Buffer} buf - The buffer to set as the internal state.
|
|
122
|
+
* @returns {Base58Check} Returns the instance for chaining.
|
|
123
|
+
*/
|
|
124
|
+
Base58Check.prototype.fromBuffer = function (buf) {
|
|
125
|
+
this.buf = buf;
|
|
126
|
+
return this;
|
|
127
|
+
};
|
|
128
|
+
|
|
129
|
+
/**
|
|
130
|
+
* Creates a Base58Check encoded string from a buffer.
|
|
131
|
+
* @param {Buffer} buf - The input buffer to encode.
|
|
132
|
+
* @returns {Base58Check} A new Base58Check instance containing the encoded data.
|
|
133
|
+
*/
|
|
134
|
+
Base58Check.fromBuffer = function (buf) {
|
|
135
|
+
return new Base58Check().fromBuffer(buf);
|
|
136
|
+
};
|
|
137
|
+
|
|
138
|
+
/**
|
|
139
|
+
* Converts a hex string to a Base58Check encoded string.
|
|
140
|
+
* @param {string} hex - The hex string to convert.
|
|
141
|
+
* @returns {string} The Base58Check encoded string.
|
|
142
|
+
*/
|
|
143
|
+
Base58Check.fromHex = function (hex) {
|
|
144
|
+
return Base58Check.fromBuffer(Buffer.from(hex, 'hex'));
|
|
145
|
+
};
|
|
146
|
+
|
|
147
|
+
/**
|
|
148
|
+
* Converts a Base58Check encoded string to a buffer and stores it in the instance.
|
|
149
|
+
* @param {string} str - The Base58Check encoded string to decode.
|
|
150
|
+
* @returns {Base58Check} Returns the instance for chaining.
|
|
151
|
+
*/
|
|
152
|
+
Base58Check.prototype.fromString = function (str) {
|
|
153
|
+
var buf = Base58Check.decode(str);
|
|
154
|
+
this.buf = buf;
|
|
155
|
+
return this;
|
|
156
|
+
};
|
|
157
|
+
|
|
158
|
+
/**
|
|
159
|
+
* Converts a Base58Check-encoded string into a Base58 object.
|
|
160
|
+
* @param {string} str - The Base58Check-encoded string to decode.
|
|
161
|
+
* @returns {Base58} A new Base58 instance containing the decoded data.
|
|
162
|
+
*/
|
|
163
|
+
Base58Check.fromString = function (str) {
|
|
164
|
+
var buf = Base58Check.decode(str);
|
|
165
|
+
return new Base58(buf);
|
|
166
|
+
};
|
|
167
|
+
|
|
168
|
+
/**
|
|
169
|
+
* Returns the internal buffer containing the Base58Check encoded data.
|
|
170
|
+
* @returns {Buffer} The raw buffer representation of the Base58Check data.
|
|
171
|
+
*/
|
|
172
|
+
Base58Check.prototype.toBuffer = function () {
|
|
173
|
+
return this.buf;
|
|
174
|
+
};
|
|
175
|
+
|
|
176
|
+
/**
|
|
177
|
+
* Converts the Base58Check encoded data to a hexadecimal string.
|
|
178
|
+
* @returns {string} Hexadecimal representation of the data.
|
|
179
|
+
*/
|
|
180
|
+
Base58Check.prototype.toHex = function () {
|
|
181
|
+
return this.toBuffer().toString('hex');
|
|
182
|
+
};
|
|
183
|
+
|
|
184
|
+
/**
|
|
185
|
+
* Converts the Base58Check instance to its string representation.
|
|
186
|
+
* @returns {string} The Base58Check encoded string.
|
|
187
|
+
*/
|
|
188
|
+
Base58Check.prototype.toString = function () {
|
|
189
|
+
return Base58Check.encode(this.buf);
|
|
190
|
+
};
|
|
191
|
+
|
|
192
|
+
export default Base58Check;
|
|
@@ -0,0 +1,333 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
import _ from '../util/_.js';
|
|
4
|
+
import $ from '../util/preconditions.js';
|
|
5
|
+
import BN from '../crypto/bn.js';
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* Creates a BufferReader instance to read from various input types.
|
|
9
|
+
* @constructor
|
|
10
|
+
* @param {Buffer|string|Object} buf - Input source (Buffer, hex string, or object with buffer properties)
|
|
11
|
+
* @throws {TypeError} If input is invalid hex string or unrecognized type
|
|
12
|
+
* @example
|
|
13
|
+
* new BufferReader(Buffer.from('abc')) // from Buffer
|
|
14
|
+
* new BufferReader('616263') // from hex string
|
|
15
|
+
* new BufferReader({buf: buffer}) // from object
|
|
16
|
+
*/
|
|
17
|
+
function BufferReader(buf) {
|
|
18
|
+
if (!(this instanceof BufferReader)) {
|
|
19
|
+
return new BufferReader(buf);
|
|
20
|
+
}
|
|
21
|
+
if (_.isUndefined(buf)) {
|
|
22
|
+
return;
|
|
23
|
+
}
|
|
24
|
+
if (Buffer.isBuffer(buf)) {
|
|
25
|
+
this.set({
|
|
26
|
+
buf: buf,
|
|
27
|
+
});
|
|
28
|
+
} else if (_.isString(buf)) {
|
|
29
|
+
var b = Buffer.from(buf, 'hex');
|
|
30
|
+
if (b.length * 2 !== buf.length) {
|
|
31
|
+
throw new TypeError('Invalid hex string');
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
this.set({
|
|
35
|
+
buf: b,
|
|
36
|
+
});
|
|
37
|
+
} else if (_.isObject(buf)) {
|
|
38
|
+
var obj = buf;
|
|
39
|
+
this.set(obj);
|
|
40
|
+
} else {
|
|
41
|
+
throw new TypeError('Unrecognized argument for BufferReader');
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* Updates the buffer and position from the given object.
|
|
47
|
+
* @param {Object} obj - The object containing buffer and position to set.
|
|
48
|
+
* @param {Buffer} [obj.buf] - The buffer to set (optional, keeps current if not provided).
|
|
49
|
+
* @param {number} [obj.pos] - The position to set (optional, keeps current if not provided).
|
|
50
|
+
* @returns {BufferReader} Returns the instance for chaining.
|
|
51
|
+
*/
|
|
52
|
+
BufferReader.prototype.set = function (obj) {
|
|
53
|
+
this.buf = obj.buf || this.buf || undefined;
|
|
54
|
+
this.pos = obj.pos || this.pos || 0;
|
|
55
|
+
return this;
|
|
56
|
+
};
|
|
57
|
+
|
|
58
|
+
/**
|
|
59
|
+
* Checks if the reader has reached the end of the buffer.
|
|
60
|
+
* @returns {boolean} True if the current position is at or beyond the buffer length, false otherwise.
|
|
61
|
+
*/
|
|
62
|
+
BufferReader.prototype.eof = function () {
|
|
63
|
+
return this.pos >= this.buf.length;
|
|
64
|
+
};
|
|
65
|
+
|
|
66
|
+
/**
|
|
67
|
+
* Alias for `eof` method - checks if the buffer reader has reached the end of data.
|
|
68
|
+
* @name BufferReader.prototype.finished
|
|
69
|
+
* @memberof BufferReader
|
|
70
|
+
* @instance
|
|
71
|
+
*/
|
|
72
|
+
BufferReader.prototype.finished = BufferReader.prototype.eof;
|
|
73
|
+
|
|
74
|
+
/**
|
|
75
|
+
* Reads a specified number of bytes from the buffer and advances the position.
|
|
76
|
+
* @param {number} len - The number of bytes to read.
|
|
77
|
+
* @returns {Buffer} The read bytes as a Buffer.
|
|
78
|
+
* @throws {Error} If the length is undefined.
|
|
79
|
+
*/
|
|
80
|
+
BufferReader.prototype.read = function (len) {
|
|
81
|
+
$.checkArgument(!_.isUndefined(len), 'Must specify a length');
|
|
82
|
+
var buf = this.buf.slice(this.pos, this.pos + len);
|
|
83
|
+
this.pos = this.pos + len;
|
|
84
|
+
return buf;
|
|
85
|
+
};
|
|
86
|
+
|
|
87
|
+
/**
|
|
88
|
+
* Reads and returns all remaining bytes from the buffer, advancing the position to the end.
|
|
89
|
+
* @returns {Buffer} The remaining bytes in the buffer.
|
|
90
|
+
*/
|
|
91
|
+
BufferReader.prototype.readAll = function () {
|
|
92
|
+
var buf = this.buf.slice(this.pos, this.buf.length);
|
|
93
|
+
this.pos = this.buf.length;
|
|
94
|
+
return buf;
|
|
95
|
+
};
|
|
96
|
+
|
|
97
|
+
/**
|
|
98
|
+
* Reads an unsigned 8-bit integer from the buffer at the current position.
|
|
99
|
+
* @returns {number} The unsigned 8-bit integer value read.
|
|
100
|
+
* @this {BufferReader}
|
|
101
|
+
*/
|
|
102
|
+
BufferReader.prototype.readUInt8 = function () {
|
|
103
|
+
var val = this.buf.readUInt8(this.pos);
|
|
104
|
+
this.pos = this.pos + 1;
|
|
105
|
+
return val;
|
|
106
|
+
};
|
|
107
|
+
|
|
108
|
+
/**
|
|
109
|
+
* Reads an unsigned 16-bit integer from the buffer in big-endian format.
|
|
110
|
+
* Advances the position by 2 bytes.
|
|
111
|
+
* @returns {number} The read unsigned 16-bit integer value.
|
|
112
|
+
*/
|
|
113
|
+
BufferReader.prototype.readUInt16BE = function () {
|
|
114
|
+
var val = this.buf.readUInt16BE(this.pos);
|
|
115
|
+
this.pos = this.pos + 2;
|
|
116
|
+
return val;
|
|
117
|
+
};
|
|
118
|
+
|
|
119
|
+
/**
|
|
120
|
+
* Reads a 16-bit unsigned integer from the buffer in little-endian format
|
|
121
|
+
* and advances the position by 2 bytes.
|
|
122
|
+
* @returns {number} The read unsigned 16-bit integer value
|
|
123
|
+
*/
|
|
124
|
+
BufferReader.prototype.readUInt16LE = function () {
|
|
125
|
+
var val = this.buf.readUInt16LE(this.pos);
|
|
126
|
+
this.pos = this.pos + 2;
|
|
127
|
+
return val;
|
|
128
|
+
};
|
|
129
|
+
|
|
130
|
+
/**
|
|
131
|
+
* Reads an unsigned 32-bit integer from the buffer in big-endian format.
|
|
132
|
+
* Advances the position by 4 bytes.
|
|
133
|
+
* @returns {number} The read unsigned 32-bit integer value.
|
|
134
|
+
*/
|
|
135
|
+
BufferReader.prototype.readUInt32BE = function () {
|
|
136
|
+
var val = this.buf.readUInt32BE(this.pos);
|
|
137
|
+
this.pos = this.pos + 4;
|
|
138
|
+
return val;
|
|
139
|
+
};
|
|
140
|
+
|
|
141
|
+
/**
|
|
142
|
+
* Reads an unsigned 32-bit integer from the buffer in little-endian format.
|
|
143
|
+
* Advances the position by 4 bytes.
|
|
144
|
+
* @returns {number} The read unsigned 32-bit integer value.
|
|
145
|
+
*/
|
|
146
|
+
BufferReader.prototype.readUInt32LE = function () {
|
|
147
|
+
var val = this.buf.readUInt32LE(this.pos);
|
|
148
|
+
this.pos = this.pos + 4;
|
|
149
|
+
return val;
|
|
150
|
+
};
|
|
151
|
+
|
|
152
|
+
/**
|
|
153
|
+
* Reads a 32-bit signed integer from the buffer in little-endian format.
|
|
154
|
+
* Advances the position by 4 bytes.
|
|
155
|
+
* @returns {number} The read 32-bit signed integer.
|
|
156
|
+
*/
|
|
157
|
+
BufferReader.prototype.readInt32LE = function () {
|
|
158
|
+
var val = this.buf.readInt32LE(this.pos);
|
|
159
|
+
this.pos = this.pos + 4;
|
|
160
|
+
return val;
|
|
161
|
+
};
|
|
162
|
+
|
|
163
|
+
/**
|
|
164
|
+
* Reads an unsigned 64-bit integer in big-endian byte order from the buffer
|
|
165
|
+
* and returns it as a BN (BigNumber) object.
|
|
166
|
+
* Advances the position by 8 bytes.
|
|
167
|
+
* @returns {BN} The parsed 64-bit unsigned integer as a BigNumber
|
|
168
|
+
*/
|
|
169
|
+
BufferReader.prototype.readUInt64BEBN = function () {
|
|
170
|
+
var buf = this.buf.slice(this.pos, this.pos + 8);
|
|
171
|
+
var bn = BN.fromBuffer(buf);
|
|
172
|
+
this.pos = this.pos + 8;
|
|
173
|
+
return bn;
|
|
174
|
+
};
|
|
175
|
+
|
|
176
|
+
/**
|
|
177
|
+
* Reads an unsigned 64-bit integer in little-endian byte order from the buffer and returns it as a BN (BigNumber).
|
|
178
|
+
* Optimizes for numbers <= 52 bits by using numeric constructor, falls back to buffer slice for larger numbers.
|
|
179
|
+
* Advances the buffer position by 8 bytes.
|
|
180
|
+
* @returns {BN} The parsed 64-bit unsigned integer as a BN instance.
|
|
181
|
+
*/
|
|
182
|
+
BufferReader.prototype.readUInt64LEBN = function () {
|
|
183
|
+
var second = this.buf.readUInt32LE(this.pos);
|
|
184
|
+
var first = this.buf.readUInt32LE(this.pos + 4);
|
|
185
|
+
var combined = first * 0x100000000 + second;
|
|
186
|
+
// Instantiating an instance of BN with a number is faster than with an
|
|
187
|
+
// array or string. However, the maximum safe number for a double precision
|
|
188
|
+
// floating point is 2 ^ 52 - 1 (0x1fffffffffffff), thus we can safely use
|
|
189
|
+
// non-floating point numbers less than this amount (52 bits). And in the case
|
|
190
|
+
// that the number is larger, we can instatiate an instance of BN by passing
|
|
191
|
+
// an array from the buffer (slower) and specifying the endianness.
|
|
192
|
+
var bn;
|
|
193
|
+
if (combined <= 0x1fffffffffffff) {
|
|
194
|
+
bn = new BN(combined);
|
|
195
|
+
} else {
|
|
196
|
+
var data = Array.prototype.slice.call(this.buf, this.pos, this.pos + 8);
|
|
197
|
+
bn = new BN(data, 10, 'le');
|
|
198
|
+
}
|
|
199
|
+
this.pos = this.pos + 8;
|
|
200
|
+
return bn;
|
|
201
|
+
};
|
|
202
|
+
|
|
203
|
+
/**
|
|
204
|
+
* Reads a variable-length integer (varint) from the buffer and returns it as a number.
|
|
205
|
+
* Supports varints up to 53 bits (JavaScript's safe integer limit).
|
|
206
|
+
* For larger numbers, throws an error suggesting to use `readVarintBN` instead.
|
|
207
|
+
* @returns {number} The decoded integer value
|
|
208
|
+
* @throws {Error} If the number exceeds 53-bit precision
|
|
209
|
+
*/
|
|
210
|
+
BufferReader.prototype.readVarintNum = function () {
|
|
211
|
+
var first = this.readUInt8();
|
|
212
|
+
switch (first) {
|
|
213
|
+
case 0xfd:
|
|
214
|
+
return this.readUInt16LE();
|
|
215
|
+
case 0xfe:
|
|
216
|
+
return this.readUInt32LE();
|
|
217
|
+
case 0xff:
|
|
218
|
+
var bn = this.readUInt64LEBN();
|
|
219
|
+
var n = bn.toNumber();
|
|
220
|
+
if (n <= Math.pow(2, 53)) {
|
|
221
|
+
return n;
|
|
222
|
+
} else {
|
|
223
|
+
throw new Error('number too large to retain precision - use readVarintBN');
|
|
224
|
+
}
|
|
225
|
+
// break // unreachable
|
|
226
|
+
default:
|
|
227
|
+
return first;
|
|
228
|
+
}
|
|
229
|
+
};
|
|
230
|
+
|
|
231
|
+
|
|
232
|
+
/**
|
|
233
|
+
* Reads a variable-length buffer from the current position.
|
|
234
|
+
* First reads a varint to determine the length, then reads the buffer of that size.
|
|
235
|
+
* @returns {Buffer} The read buffer.
|
|
236
|
+
* @throws {Error} If the actual read length doesn't match the expected length.
|
|
237
|
+
*/
|
|
238
|
+
BufferReader.prototype.readVarLengthBuffer = function () {
|
|
239
|
+
var len = this.readVarintNum();
|
|
240
|
+
var buf = this.read(len);
|
|
241
|
+
$.checkState(
|
|
242
|
+
buf.length === len,
|
|
243
|
+
'Invalid length while reading varlength buffer. ' +
|
|
244
|
+
'Expected to read: ' +
|
|
245
|
+
len +
|
|
246
|
+
' and read ' +
|
|
247
|
+
buf.length,
|
|
248
|
+
);
|
|
249
|
+
return buf;
|
|
250
|
+
};
|
|
251
|
+
|
|
252
|
+
/**
|
|
253
|
+
* Reads a variable-length integer (varint) from the buffer.
|
|
254
|
+
* The first byte determines the length of the varint:
|
|
255
|
+
* - 0xfd: 2-byte varint (plus 1 byte for the prefix)
|
|
256
|
+
* - 0xfe: 4-byte varint (plus 1 byte for the prefix)
|
|
257
|
+
* - 0xff: 8-byte varint (plus 1 byte for the prefix)
|
|
258
|
+
* - Otherwise: 1-byte varint (no prefix)
|
|
259
|
+
* @returns {Buffer} The varint bytes including the prefix (if any)
|
|
260
|
+
*/
|
|
261
|
+
BufferReader.prototype.readVarintBuf = function () {
|
|
262
|
+
var first = this.buf.readUInt8(this.pos);
|
|
263
|
+
switch (first) {
|
|
264
|
+
case 0xfd:
|
|
265
|
+
return this.read(1 + 2);
|
|
266
|
+
case 0xfe:
|
|
267
|
+
return this.read(1 + 4);
|
|
268
|
+
case 0xff:
|
|
269
|
+
return this.read(1 + 8);
|
|
270
|
+
default:
|
|
271
|
+
return this.read(1);
|
|
272
|
+
}
|
|
273
|
+
};
|
|
274
|
+
|
|
275
|
+
/**
|
|
276
|
+
* Reads a variable-length integer (varint) from the buffer and returns it as a BN (BigNumber).
|
|
277
|
+
* Handles different varint sizes (1, 2, 4, or 8 bytes) based on the first byte's value:
|
|
278
|
+
* - 0xfd: reads next 2 bytes as uint16
|
|
279
|
+
* - 0xfe: reads next 4 bytes as uint32
|
|
280
|
+
* - 0xff: reads next 8 bytes as uint64
|
|
281
|
+
* - default: treats first byte as uint8
|
|
282
|
+
* @returns {BN} The parsed varint as a BigNumber
|
|
283
|
+
*/
|
|
284
|
+
BufferReader.prototype.readVarintBN = function () {
|
|
285
|
+
var first = this.readUInt8();
|
|
286
|
+
switch (first) {
|
|
287
|
+
case 0xfd:
|
|
288
|
+
return new BN(this.readUInt16LE());
|
|
289
|
+
case 0xfe:
|
|
290
|
+
return new BN(this.readUInt32LE());
|
|
291
|
+
case 0xff:
|
|
292
|
+
return this.readUInt64LEBN();
|
|
293
|
+
default:
|
|
294
|
+
return new BN(first);
|
|
295
|
+
}
|
|
296
|
+
};
|
|
297
|
+
|
|
298
|
+
/**
|
|
299
|
+
* Reverses the order of bytes in the internal buffer and returns the instance.
|
|
300
|
+
* @returns {BufferReader} The modified BufferReader instance for chaining.
|
|
301
|
+
*/
|
|
302
|
+
BufferReader.prototype.reverse = function () {
|
|
303
|
+
var buf = Buffer.alloc(this.buf.length);
|
|
304
|
+
for (var i = 0; i < buf.length; i++) {
|
|
305
|
+
buf[i] = this.buf[this.buf.length - 1 - i];
|
|
306
|
+
}
|
|
307
|
+
this.buf = buf;
|
|
308
|
+
return this;
|
|
309
|
+
};
|
|
310
|
+
|
|
311
|
+
/**
|
|
312
|
+
* Reads a specified number of bytes from the buffer in reverse order.
|
|
313
|
+
* @param {number} [len] - The number of bytes to read. If omitted, reads the entire buffer.
|
|
314
|
+
* @returns {Buffer} The reversed buffer segment.
|
|
315
|
+
*/
|
|
316
|
+
BufferReader.prototype.readReverse = function (len) {
|
|
317
|
+
if (_.isUndefined(len)) {
|
|
318
|
+
len = this.buf.length;
|
|
319
|
+
}
|
|
320
|
+
var buf = this.buf.slice(this.pos, this.pos + len);
|
|
321
|
+
this.pos = this.pos + len;
|
|
322
|
+
return Buffer.from(buf).reverse();
|
|
323
|
+
};
|
|
324
|
+
|
|
325
|
+
/**
|
|
326
|
+
* Gets the number of bytes remaining to be read in the buffer.
|
|
327
|
+
* @returns {number} The remaining bytes count.
|
|
328
|
+
*/
|
|
329
|
+
BufferReader.prototype.remaining = function () {
|
|
330
|
+
return this.buf.length - this.pos;
|
|
331
|
+
};
|
|
332
|
+
|
|
333
|
+
export default BufferReader;
|