@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
|
@@ -1,11 +1,11 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
-
var _ = require('../util/_');
|
|
4
|
-
var $ = require('../util/preconditions');
|
|
5
|
-
var JSUtil = require('../util/js');
|
|
3
|
+
var _ = require('../util/_.cjs');
|
|
4
|
+
var $ = require('../util/preconditions.cjs');
|
|
5
|
+
var JSUtil = require('../util/js.cjs');
|
|
6
6
|
|
|
7
|
-
var Script = require('../script');
|
|
8
|
-
var Address = require('../address');
|
|
7
|
+
var Script = require('../script/index.cjs');
|
|
8
|
+
var Address = require('../address.cjs');
|
|
9
9
|
|
|
10
10
|
/**
|
|
11
11
|
* Represents an unspent output information: its script, associated amount and address,
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
|
|
2
|
+
var _ = require('./_.cjs');
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Represents a derivation function or class (purpose to be determined based on implementation).
|
|
6
|
+
* @constructor
|
|
7
|
+
*/
|
|
8
|
+
function Derivation() {
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
Derivation.RootElementAlias = ['m', 'M', "m'", "M'"]
|
|
12
|
+
Derivation.Hardened = 0x80000000;
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* function that splits a string path into a derivation index array.
|
|
16
|
+
* It will return null if the string path is malformed.
|
|
17
|
+
* It does not validate if indexes are in bounds.
|
|
18
|
+
*
|
|
19
|
+
* @param {string} path
|
|
20
|
+
* @return {Array}
|
|
21
|
+
*/
|
|
22
|
+
Derivation.getDerivationIndexes = function getDerivationIndexes(path) {
|
|
23
|
+
var steps = path.split('/');
|
|
24
|
+
|
|
25
|
+
// Special cases:
|
|
26
|
+
if (_.includes(Derivation.RootElementAlias, path)) {
|
|
27
|
+
return [];
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
if (!_.includes(Derivation.RootElementAlias, steps[0])) {
|
|
31
|
+
return null;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
var indexes = steps.slice(1).map(function (step) {
|
|
35
|
+
var isHardened = step.slice(-1) === "'";
|
|
36
|
+
if (isHardened) {
|
|
37
|
+
step = step.slice(0, -1);
|
|
38
|
+
}
|
|
39
|
+
if (!step || step[0] === '-') {
|
|
40
|
+
return NaN;
|
|
41
|
+
}
|
|
42
|
+
var index = +step; // cast to number
|
|
43
|
+
if (isHardened) {
|
|
44
|
+
index += Derivation.Hardened;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
return index;
|
|
48
|
+
});
|
|
49
|
+
|
|
50
|
+
return _.some(indexes, isNaN) ? null : indexes;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
module.exports = Derivation;
|
package/cjs/util/js.cjs
ADDED
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var _ = require('./_.cjs');
|
|
4
|
+
var $ = require('./preconditions.cjs');
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Utility functions for JavaScript operations.
|
|
8
|
+
* @constructor
|
|
9
|
+
*/
|
|
10
|
+
function JSUtil() {
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* Determines whether a string contains only hexadecimal values
|
|
15
|
+
*
|
|
16
|
+
* @name JSUtil.isHexa
|
|
17
|
+
* @param {string} value
|
|
18
|
+
* @return {boolean} true if the string is the hexa representation of a number
|
|
19
|
+
*/
|
|
20
|
+
JSUtil.isHexa = function isHexa(value) {
|
|
21
|
+
if (!_.isString(value)) {
|
|
22
|
+
return false;
|
|
23
|
+
}
|
|
24
|
+
return /^[0-9a-fA-F]+$/.test(value);
|
|
25
|
+
};
|
|
26
|
+
|
|
27
|
+
JSUtil.isHexaString = JSUtil.isHexa;
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* Checks that a value is a natural number, a positive integer or zero.
|
|
31
|
+
*
|
|
32
|
+
* @param {*} value
|
|
33
|
+
* @return {Boolean}
|
|
34
|
+
*/
|
|
35
|
+
JSUtil.isNaturalNumber = function isNaturalNumber(value) {
|
|
36
|
+
return (
|
|
37
|
+
typeof value === 'number' && isFinite(value) && Math.floor(value) === value && value >= 0
|
|
38
|
+
);
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* Transform a 4-byte integer (unsigned value) into a Buffer of length 4 (Big Endian Byte Order)
|
|
43
|
+
*
|
|
44
|
+
* @param {number} integer
|
|
45
|
+
* @return {Buffer}
|
|
46
|
+
*/
|
|
47
|
+
JSUtil.integerAsBuffer = function integerAsBuffer(integer) {
|
|
48
|
+
$.checkArgumentType(integer, 'number', 'integer');
|
|
49
|
+
const buf = Buffer.allocUnsafe(4);
|
|
50
|
+
buf.writeUInt32BE(integer, 0);
|
|
51
|
+
return buf;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* Test if an argument is a valid JSON object. If it is, returns a truthy
|
|
56
|
+
* value (the json object decoded), so no double JSON.parse call is necessary
|
|
57
|
+
*
|
|
58
|
+
* @param {string} arg
|
|
59
|
+
* @return {Object|boolean} false if the argument is not a JSON string.
|
|
60
|
+
*/
|
|
61
|
+
JSUtil.isValidJSON = function isValidJSON(arg) {
|
|
62
|
+
var parsed;
|
|
63
|
+
if (!_.isString(arg)) {
|
|
64
|
+
return false;
|
|
65
|
+
}
|
|
66
|
+
try {
|
|
67
|
+
parsed = JSON.parse(arg);
|
|
68
|
+
} catch (e) {
|
|
69
|
+
return false;
|
|
70
|
+
}
|
|
71
|
+
if (typeof parsed === 'object') {
|
|
72
|
+
return true;
|
|
73
|
+
}
|
|
74
|
+
return false;
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
/**
|
|
78
|
+
* Define immutable properties on a target object
|
|
79
|
+
*
|
|
80
|
+
* @param {Object} target - An object to be extended
|
|
81
|
+
* @param {Object} values - An object of properties
|
|
82
|
+
* @return {Object} The target object
|
|
83
|
+
*/
|
|
84
|
+
JSUtil.defineImmutable = function defineImmutable(target, values) {
|
|
85
|
+
Object.keys(values).forEach(function (key) {
|
|
86
|
+
Object.defineProperty(target, key, {
|
|
87
|
+
configurable: false,
|
|
88
|
+
enumerable: true,
|
|
89
|
+
value: values[key],
|
|
90
|
+
});
|
|
91
|
+
});
|
|
92
|
+
return target;
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
module.exports = JSUtil;
|
package/esm/address.js
ADDED
|
@@ -0,0 +1,483 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
import _ from './util/_.js';
|
|
4
|
+
import $ from './util/preconditions.js';
|
|
5
|
+
import Base58Check from './encoding/base58check.js';
|
|
6
|
+
import Networks from './networks.js';
|
|
7
|
+
import Hash from './crypto/hash.js';
|
|
8
|
+
import JSUtil from './util/js.js';
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* Instantiate an address from an address String or Buffer, a public key hash Buffer,
|
|
12
|
+
* or a {@link PublicKey} Buffer.
|
|
13
|
+
*
|
|
14
|
+
* This is an immutable class, and if the first parameter provided to this constructor is an
|
|
15
|
+
* `Address` instance, the same argument will be returned.
|
|
16
|
+
*
|
|
17
|
+
* An address has two key properties: `network` and `type`. The type is either
|
|
18
|
+
* `Address.PayToPublicKeyHash` (value is the `'pubkeyhash'` string).
|
|
19
|
+
* The network is an instance of {@link Network}.
|
|
20
|
+
* You can quickly check whether an address is of a given kind by using the methods
|
|
21
|
+
* `isPayToPublicKeyHash`
|
|
22
|
+
*
|
|
23
|
+
* @example
|
|
24
|
+
* ```javascript
|
|
25
|
+
* // validate that an input field is valid
|
|
26
|
+
* var error = Address.getValidationError(input, 'testnet');
|
|
27
|
+
* if (!error) {
|
|
28
|
+
* var address = Address(input, 'testnet');
|
|
29
|
+
* } else {
|
|
30
|
+
* // invalid network or checksum (typo?)
|
|
31
|
+
* var message = error.messsage;
|
|
32
|
+
* }
|
|
33
|
+
*
|
|
34
|
+
* // get an address from a public key
|
|
35
|
+
* var address = Address(publicKey, 'testnet').toString();
|
|
36
|
+
* ```
|
|
37
|
+
*
|
|
38
|
+
* @param {*} data - The encoded data in various formats
|
|
39
|
+
* @param {Network|String|number} [network] - The network: 'livenet' or 'testnet'
|
|
40
|
+
* @param {string} [type] - The type of address: 'pubkey'
|
|
41
|
+
* @returns {Address} A new valid and frozen instance of an Address
|
|
42
|
+
* @constructor
|
|
43
|
+
*/
|
|
44
|
+
function Address(data, network, type) {
|
|
45
|
+
if (!(this instanceof Address)) {
|
|
46
|
+
return new Address(data, network, type);
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
if (data instanceof Address) {
|
|
50
|
+
// Immutable instance
|
|
51
|
+
return data;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
$.checkArgument(
|
|
55
|
+
data,
|
|
56
|
+
'First argument is required, please include address data.',
|
|
57
|
+
'guide/address.html',
|
|
58
|
+
);
|
|
59
|
+
|
|
60
|
+
if (network && !Networks.get(network)) {
|
|
61
|
+
throw new TypeError('Second argument must be "livenet", "testnet", or "regtest".');
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
if (type && type !== Address.PayToPublicKeyHash) {
|
|
65
|
+
throw new TypeError('Third argument must be "pubkeyhash".');
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
var info = this._classifyArguments(data, network, type);
|
|
69
|
+
|
|
70
|
+
// set defaults if not set
|
|
71
|
+
info.network = info.network || Networks.get(network) || Networks.defaultNetwork;
|
|
72
|
+
info.type = info.type || type || Address.PayToPublicKeyHash;
|
|
73
|
+
|
|
74
|
+
JSUtil.defineImmutable(this, {
|
|
75
|
+
hashBuffer: info.hashBuffer,
|
|
76
|
+
network: info.network,
|
|
77
|
+
type: info.type,
|
|
78
|
+
});
|
|
79
|
+
|
|
80
|
+
return this;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
/**
|
|
84
|
+
* Gets the hash buffer of the Address instance.
|
|
85
|
+
* @memberof Address.prototype
|
|
86
|
+
* @type {Buffer}
|
|
87
|
+
* @readonly
|
|
88
|
+
*/
|
|
89
|
+
Object.defineProperty(Address.prototype, 'hashBuffer', {
|
|
90
|
+
enumerable: true,
|
|
91
|
+
configurable: false,
|
|
92
|
+
get: function () {
|
|
93
|
+
return this.hashBuffer;
|
|
94
|
+
},
|
|
95
|
+
});
|
|
96
|
+
|
|
97
|
+
/**
|
|
98
|
+
* Gets or sets the network associated with this Address instance.
|
|
99
|
+
* @memberof Address.prototype
|
|
100
|
+
* @type {Network}
|
|
101
|
+
* @readonly
|
|
102
|
+
*/
|
|
103
|
+
Object.defineProperty(Address.prototype, 'network', {
|
|
104
|
+
enumerable: true,
|
|
105
|
+
configurable: false,
|
|
106
|
+
get: function () {
|
|
107
|
+
return this.network;
|
|
108
|
+
},
|
|
109
|
+
});
|
|
110
|
+
|
|
111
|
+
/**
|
|
112
|
+
* Gets the address type (e.g. 'pubkeyhash').
|
|
113
|
+
* @memberof Address.prototype
|
|
114
|
+
* @type {string}
|
|
115
|
+
* @readonly
|
|
116
|
+
*/
|
|
117
|
+
Object.defineProperty(Address.prototype, 'type', {
|
|
118
|
+
enumerable: true,
|
|
119
|
+
configurable: false,
|
|
120
|
+
get: function () {
|
|
121
|
+
return this.type;
|
|
122
|
+
},
|
|
123
|
+
});
|
|
124
|
+
|
|
125
|
+
/**
|
|
126
|
+
* Internal function used to split different kinds of arguments of the constructor
|
|
127
|
+
* @param {Buffer|Uint8Array|string|Object} data - The encoded data in various formats
|
|
128
|
+
* @param {Network|string} [network] - The network: 'livenet' or 'testnet'
|
|
129
|
+
* @param {string} [type] - The type of address: 'pubkey'
|
|
130
|
+
* @returns {Object} An "info" object with "type", "network", and "hashBuffer"
|
|
131
|
+
* @private
|
|
132
|
+
*/
|
|
133
|
+
Address.prototype._classifyArguments = function (data, network, type) {
|
|
134
|
+
// transform and validate input data
|
|
135
|
+
if ((data instanceof Buffer || data instanceof Uint8Array) && data.length === 20) {
|
|
136
|
+
return Address._transformHash(data);
|
|
137
|
+
} else if ((data instanceof Buffer || data instanceof Uint8Array) && data.length === 21) {
|
|
138
|
+
return Address._transformBuffer(data, network, type);
|
|
139
|
+
} else if ((data instanceof Buffer || data instanceof Uint8Array) && (data.length === 33 || data.length === 65)) {
|
|
140
|
+
return Address._transformPublicKey(data);
|
|
141
|
+
} else if (typeof data === 'string') {
|
|
142
|
+
return Address._transformString(data, network, type);
|
|
143
|
+
} else if (_.isObject(data)) {
|
|
144
|
+
return Address._transformObject(data);
|
|
145
|
+
} else {
|
|
146
|
+
throw new TypeError('First argument is an unrecognized data format.');
|
|
147
|
+
}
|
|
148
|
+
};
|
|
149
|
+
|
|
150
|
+
/**
|
|
151
|
+
* PayToPublicKeyHash address type identifier.
|
|
152
|
+
* @type {string}
|
|
153
|
+
* @static
|
|
154
|
+
*/
|
|
155
|
+
Address.PayToPublicKeyHash = 'pubkeyhash';
|
|
156
|
+
|
|
157
|
+
/**
|
|
158
|
+
* @param {Buffer} hash - An instance of a hash Buffer
|
|
159
|
+
* @returns {Object} An object with keys: hashBuffer
|
|
160
|
+
* @private
|
|
161
|
+
*/
|
|
162
|
+
Address._transformHash = function (hash) {
|
|
163
|
+
var info = {};
|
|
164
|
+
if (!(hash instanceof Buffer) && !(hash instanceof Uint8Array)) {
|
|
165
|
+
throw new TypeError('Address supplied is not a buffer.');
|
|
166
|
+
}
|
|
167
|
+
if (hash.length !== 20) {
|
|
168
|
+
throw new TypeError('Address hashbuffers must be exactly 20 bytes.');
|
|
169
|
+
}
|
|
170
|
+
info.hashBuffer = hash;
|
|
171
|
+
return info;
|
|
172
|
+
};
|
|
173
|
+
|
|
174
|
+
/**
|
|
175
|
+
* Deserializes an address serialized through `Address#toObject()`
|
|
176
|
+
* @param {Object} data
|
|
177
|
+
* @param {string} data.hash - the hash that this address encodes
|
|
178
|
+
* @param {string} data.type - either 'pubkeyhash' or 'scripthash'
|
|
179
|
+
* @param {Network=} data.network - the name of the network associated
|
|
180
|
+
* @return {Address}
|
|
181
|
+
* @private
|
|
182
|
+
*/
|
|
183
|
+
Address._transformObject = function (data) {
|
|
184
|
+
$.checkArgument(data.hash || data.hashBuffer, 'Must provide a `hash` or `hashBuffer` property');
|
|
185
|
+
$.checkArgument(data.type, 'Must provide a `type` property');
|
|
186
|
+
return {
|
|
187
|
+
hashBuffer: data.hash ? Buffer.from(data.hash, 'hex') : data.hashBuffer,
|
|
188
|
+
network: Networks.get(data.network) || Networks.defaultNetwork,
|
|
189
|
+
type: data.type,
|
|
190
|
+
};
|
|
191
|
+
};
|
|
192
|
+
|
|
193
|
+
/**
|
|
194
|
+
* Internal function to discover the network and type based on the first data byte
|
|
195
|
+
*
|
|
196
|
+
* @param {Buffer} buffer - An instance of a hex encoded address Buffer
|
|
197
|
+
* @returns {Object} An object with keys: network and type
|
|
198
|
+
* @private
|
|
199
|
+
*/
|
|
200
|
+
Address._classifyFromVersion = function (buffer) {
|
|
201
|
+
var version = {};
|
|
202
|
+
|
|
203
|
+
var pubkeyhashNetwork = Networks.get(buffer[0], 'pubkeyhash');
|
|
204
|
+
|
|
205
|
+
if (pubkeyhashNetwork) {
|
|
206
|
+
version.network = pubkeyhashNetwork;
|
|
207
|
+
version.type = Address.PayToPublicKeyHash;
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
return version;
|
|
211
|
+
};
|
|
212
|
+
|
|
213
|
+
/**
|
|
214
|
+
* Internal function to transform a bitcoin address buffer
|
|
215
|
+
*
|
|
216
|
+
* @param {Buffer} buffer - An instance of a hex encoded address Buffer
|
|
217
|
+
* @param {string=} network - The network: 'livenet' or 'testnet'
|
|
218
|
+
* @param {string=} type - The type: 'pubkeyhash' or 'scripthash'
|
|
219
|
+
* @returns {Object} An object with keys: hashBuffer, network and type
|
|
220
|
+
* @private
|
|
221
|
+
*/
|
|
222
|
+
Address._transformBuffer = function (buffer, network, type) {
|
|
223
|
+
var info = {};
|
|
224
|
+
if (!(buffer instanceof Buffer) && !(buffer instanceof Uint8Array)) {
|
|
225
|
+
throw new TypeError('Address supplied is not a buffer.');
|
|
226
|
+
}
|
|
227
|
+
if (buffer.length !== 1 + 20) {
|
|
228
|
+
throw new TypeError('Address buffers must be exactly 21 bytes.');
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
var networkObj = Networks.get(network);
|
|
232
|
+
var bufferVersion = Address._classifyFromVersion(buffer);
|
|
233
|
+
|
|
234
|
+
if (network && !networkObj) {
|
|
235
|
+
throw new TypeError('Unknown network');
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
if (!bufferVersion.network || (networkObj && networkObj !== bufferVersion.network)) {
|
|
239
|
+
// console.log(bufferVersion)
|
|
240
|
+
throw new TypeError('Address has mismatched network type.');
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
if (!bufferVersion.type || (type && type !== bufferVersion.type)) {
|
|
244
|
+
throw new TypeError('Address has mismatched type.');
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
info.hashBuffer = buffer.slice(1);
|
|
248
|
+
info.network = bufferVersion.network;
|
|
249
|
+
info.type = bufferVersion.type;
|
|
250
|
+
return info;
|
|
251
|
+
};
|
|
252
|
+
|
|
253
|
+
/**
|
|
254
|
+
* Internal function to transform a {@link PublicKey}
|
|
255
|
+
*
|
|
256
|
+
* @param {PublicKey} pubkey - An instance of PublicKey
|
|
257
|
+
* @returns {Object} An object with keys: hashBuffer, type
|
|
258
|
+
* @private
|
|
259
|
+
*/
|
|
260
|
+
Address._transformPublicKey = function (pubkey) {
|
|
261
|
+
var info = {};
|
|
262
|
+
if (!(pubkey instanceof Buffer) && !(pubkey instanceof Uint8Array) || (pubkey.length !== 33 && pubkey.length !== 65)) {
|
|
263
|
+
throw new TypeError('Pubkey supplied is not a buffer with 33 or 65 bytes.');
|
|
264
|
+
}
|
|
265
|
+
info.hashBuffer = Hash.sha256ripemd160(pubkey);
|
|
266
|
+
info.type = Address.PayToPublicKeyHash;
|
|
267
|
+
return info;
|
|
268
|
+
};
|
|
269
|
+
|
|
270
|
+
/**
|
|
271
|
+
* Internal function to transform a bitcoin cash address string
|
|
272
|
+
*
|
|
273
|
+
* @param {string} data
|
|
274
|
+
* @param {String|Network=} network - either a Network instance, 'livenet', or 'testnet'
|
|
275
|
+
* @param {string=} type - The type: 'pubkeyhash'
|
|
276
|
+
* @returns {Object} An object with keys: hashBuffer, network and type
|
|
277
|
+
* @private
|
|
278
|
+
*/
|
|
279
|
+
Address._transformString = function (data, network, type) {
|
|
280
|
+
if (typeof data !== 'string') {
|
|
281
|
+
throw new TypeError('data parameter supplied is not a string.');
|
|
282
|
+
}
|
|
283
|
+
if (data.length < 27) {
|
|
284
|
+
throw new Error('Invalid Address string provided');
|
|
285
|
+
}
|
|
286
|
+
data = data.trim();
|
|
287
|
+
var networkObj = Networks.get(network);
|
|
288
|
+
|
|
289
|
+
if (network && !networkObj) {
|
|
290
|
+
throw new TypeError('Unknown network');
|
|
291
|
+
}
|
|
292
|
+
|
|
293
|
+
var addressBuffer = Base58Check.decode(data);
|
|
294
|
+
return Address._transformBuffer(addressBuffer, network, type);
|
|
295
|
+
};
|
|
296
|
+
|
|
297
|
+
/**
|
|
298
|
+
* Instantiate an address from a PublicKey buffer
|
|
299
|
+
*
|
|
300
|
+
* @param {Buffer} data - A buffer of the public key
|
|
301
|
+
* @param {String|Network} network - either a Network instance, 'livenet', or 'testnet'
|
|
302
|
+
* @returns {Address} A new valid and frozen instance of an Address
|
|
303
|
+
*/
|
|
304
|
+
Address.fromPublicKey = function (data, network) {
|
|
305
|
+
var info = Address._transformPublicKey(data);
|
|
306
|
+
network = network || Networks.defaultNetwork;
|
|
307
|
+
return new Address(info.hashBuffer, network, info.type);
|
|
308
|
+
};
|
|
309
|
+
|
|
310
|
+
|
|
311
|
+
/**
|
|
312
|
+
* Instantiate an address from a ripemd160 public key hash
|
|
313
|
+
*
|
|
314
|
+
* @param {Buffer} hash - An instance of buffer of the hash
|
|
315
|
+
* @param {String|Network} network - either a Network instance, 'livenet', or 'testnet'
|
|
316
|
+
* @returns {Address} A new valid and frozen instance of an Address
|
|
317
|
+
*/
|
|
318
|
+
Address.fromPublicKeyHash = function (hash, network) {
|
|
319
|
+
var info = Address._transformHash(hash);
|
|
320
|
+
return new Address(info.hashBuffer, network, Address.PayToPublicKeyHash);
|
|
321
|
+
};
|
|
322
|
+
|
|
323
|
+
|
|
324
|
+
/**
|
|
325
|
+
* Instantiate an address from a bitcoin address buffer
|
|
326
|
+
*
|
|
327
|
+
* @param {Buffer} buffer - An instance of buffer of the address
|
|
328
|
+
* @param {String|Network=} network - either a Network instance, 'livenet', or 'testnet'
|
|
329
|
+
* @param {string=} type - The type of address: 'pubkey'
|
|
330
|
+
* @returns {Address} A new valid and frozen instance of an Address
|
|
331
|
+
*/
|
|
332
|
+
Address.fromBuffer = function (buffer, network, type) {
|
|
333
|
+
var info = Address._transformBuffer(buffer, network, type);
|
|
334
|
+
return new Address(info.hashBuffer, info.network, info.type);
|
|
335
|
+
};
|
|
336
|
+
|
|
337
|
+
/**
|
|
338
|
+
* Creates an Address instance from a hex string.
|
|
339
|
+
* @param {string} hex - The hex string representation of the address.
|
|
340
|
+
* @param {Network} network - The network type (e.g., 'mainnet', 'testnet').
|
|
341
|
+
* @param {AddressType} [type] - Optional address type.
|
|
342
|
+
* @returns {Address} The Address instance created from the hex string.
|
|
343
|
+
*/
|
|
344
|
+
Address.fromHex = function (hex, network, type) {
|
|
345
|
+
return Address.fromBuffer(Buffer.from(hex, 'hex'), network, type);
|
|
346
|
+
};
|
|
347
|
+
|
|
348
|
+
/**
|
|
349
|
+
* Instantiate an address from an address string
|
|
350
|
+
*
|
|
351
|
+
* @param {string} str - An string of the bitcoin address
|
|
352
|
+
* @param {String|Network=} network - either a Network instance, 'livenet', or 'testnet'
|
|
353
|
+
* @param {string=} type - The type of address: 'pubkey'
|
|
354
|
+
* @returns {Address} A new valid and frozen instance of an Address
|
|
355
|
+
*/
|
|
356
|
+
Address.fromString = function (str, network, type) {
|
|
357
|
+
var info = Address._transformString(str, network, type);
|
|
358
|
+
return new Address(info.hashBuffer, info.network, info.type);
|
|
359
|
+
};
|
|
360
|
+
|
|
361
|
+
/**
|
|
362
|
+
* Instantiate an address from an Object
|
|
363
|
+
*
|
|
364
|
+
* @param {string} json - An JSON string or Object with keys: hash, network and type
|
|
365
|
+
* @returns {Address} A new valid instance of an Address
|
|
366
|
+
*/
|
|
367
|
+
Address.fromObject = function fromObject(obj) {
|
|
368
|
+
$.checkState(
|
|
369
|
+
JSUtil.isHexa(obj.hash),
|
|
370
|
+
'Unexpected hash property, "' + obj.hash + '", expected to be hex.',
|
|
371
|
+
);
|
|
372
|
+
var hashBuffer = Buffer.from(obj.hash, 'hex');
|
|
373
|
+
return new Address(hashBuffer, obj.network, obj.type);
|
|
374
|
+
};
|
|
375
|
+
|
|
376
|
+
/**
|
|
377
|
+
* Will return a validation error if exists
|
|
378
|
+
*
|
|
379
|
+
* @example
|
|
380
|
+
* ```javascript
|
|
381
|
+
* // a network mismatch error
|
|
382
|
+
* var error = Address.getValidationError('15vkcKf7gB23wLAnZLmbVuMiiVDc1Nm4a2', 'testnet');
|
|
383
|
+
* ```
|
|
384
|
+
*
|
|
385
|
+
* @param {string} data - The encoded data
|
|
386
|
+
* @param {String|Network} network - either a Network instance, 'livenet', or 'testnet'
|
|
387
|
+
* @param {string} type - The type of address: 'pubkey'
|
|
388
|
+
* @returns {null|Error} The corresponding error message
|
|
389
|
+
*/
|
|
390
|
+
Address.getValidationError = function (data, network, type) {
|
|
391
|
+
var error;
|
|
392
|
+
try {
|
|
393
|
+
new Address(data, network, type);
|
|
394
|
+
} catch (e) {
|
|
395
|
+
error = e;
|
|
396
|
+
}
|
|
397
|
+
return error;
|
|
398
|
+
};
|
|
399
|
+
|
|
400
|
+
/**
|
|
401
|
+
* Will return a boolean if an address is valid
|
|
402
|
+
*
|
|
403
|
+
* @example
|
|
404
|
+
* ```javascript
|
|
405
|
+
* assert(Address.isValid('15vkcKf7gB23wLAnZLmbVuMiiVDc1Nm4a2', 'livenet'));
|
|
406
|
+
* ```
|
|
407
|
+
*
|
|
408
|
+
* @param {string} data - The encoded data
|
|
409
|
+
* @param {String|Network} network - either a Network instance, 'livenet', or 'testnet'
|
|
410
|
+
* @param {string} type - The type of address: 'pubkey'
|
|
411
|
+
* @returns {boolean} The corresponding error message
|
|
412
|
+
*/
|
|
413
|
+
Address.isValid = function (data, network, type) {
|
|
414
|
+
return !Address.getValidationError(data, network, type);
|
|
415
|
+
};
|
|
416
|
+
|
|
417
|
+
/**
|
|
418
|
+
* Returns true if an address is of pay to public key hash type
|
|
419
|
+
* @return boolean
|
|
420
|
+
*/
|
|
421
|
+
Address.prototype.isPayToPublicKeyHash = function () {
|
|
422
|
+
return this.type === Address.PayToPublicKeyHash;
|
|
423
|
+
};
|
|
424
|
+
|
|
425
|
+
/**
|
|
426
|
+
* Will return a buffer representation of the address
|
|
427
|
+
*
|
|
428
|
+
* @returns {Buffer} Bitcoin address buffer
|
|
429
|
+
*/
|
|
430
|
+
Address.prototype.toBuffer = function () {
|
|
431
|
+
var version = Buffer.from([this.network[this.type]]);
|
|
432
|
+
var buf = Buffer.concat([version, this.hashBuffer]);
|
|
433
|
+
return buf;
|
|
434
|
+
};
|
|
435
|
+
|
|
436
|
+
/**
|
|
437
|
+
* Converts the address to a hexadecimal string representation.
|
|
438
|
+
* @returns {string} The hexadecimal string representation of the address.
|
|
439
|
+
*/
|
|
440
|
+
Address.prototype.toHex = function () {
|
|
441
|
+
return this.toBuffer().toString('hex');
|
|
442
|
+
};
|
|
443
|
+
|
|
444
|
+
/**
|
|
445
|
+
* Converts the address to a publickey hash string representation.
|
|
446
|
+
* @returns {string} The hexadecimal string of the publickey hash buffer.
|
|
447
|
+
*/
|
|
448
|
+
Address.prototype.toPublickeyHash = function () {
|
|
449
|
+
return this.hashBuffer.toString('hex');
|
|
450
|
+
};
|
|
451
|
+
|
|
452
|
+
/**
|
|
453
|
+
* @returns {Object} A plain object with the address information
|
|
454
|
+
*/
|
|
455
|
+
Address.prototype.toObject = Address.prototype.toJSON = function toObject() {
|
|
456
|
+
return {
|
|
457
|
+
hash: this.hashBuffer.toString('hex'),
|
|
458
|
+
type: this.type,
|
|
459
|
+
network: this.network.toString(),
|
|
460
|
+
};
|
|
461
|
+
};
|
|
462
|
+
|
|
463
|
+
/**
|
|
464
|
+
* Will return a string formatted for the console
|
|
465
|
+
*
|
|
466
|
+
* @returns {string} Bitcoin address
|
|
467
|
+
*/
|
|
468
|
+
Address.prototype.inspect = function () {
|
|
469
|
+
return (
|
|
470
|
+
'<Address: ' + this.toString() + ', type: ' + this.type + ', network: ' + this.network + '>'
|
|
471
|
+
);
|
|
472
|
+
};
|
|
473
|
+
|
|
474
|
+
/**
|
|
475
|
+
* Will return a the base58 string representation of the address
|
|
476
|
+
*
|
|
477
|
+
* @returns {string} Bitcoin address
|
|
478
|
+
*/
|
|
479
|
+
Address.prototype.toString = function () {
|
|
480
|
+
return Base58Check.encode(this.toBuffer());
|
|
481
|
+
};
|
|
482
|
+
|
|
483
|
+
export default Address;
|