@opcat-labs/opcat 1.0.0 → 1.0.2
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 +13 -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 +40 -26
- 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,422 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var _ = require('./util/_.cjs');
|
|
4
|
+
var Address = require('./address.cjs');
|
|
5
|
+
var Base58Check = require('./encoding/base58check.cjs');
|
|
6
|
+
var BN = require('./crypto/bn.cjs');
|
|
7
|
+
var JSUtil = require('./util/js.cjs');
|
|
8
|
+
var Networks = require('./networks.cjs');
|
|
9
|
+
var PublicKey = require('./publickey.cjs');
|
|
10
|
+
var Point = require('./crypto/point.cjs');
|
|
11
|
+
var Random = require('./crypto/random.cjs');
|
|
12
|
+
var $ = require('./util/preconditions.cjs');
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* Instantiate a PrivateKey from a BN, Buffer or WIF string.
|
|
16
|
+
*
|
|
17
|
+
* @param {string|BN|Buffer|Object} data - The encoded data in various formats
|
|
18
|
+
* @param {Network|string} [network] - a {@link Network} object, or a string with the network name
|
|
19
|
+
* @returns {PrivateKey} A new valid instance of an PrivateKey
|
|
20
|
+
* @constructor
|
|
21
|
+
*/
|
|
22
|
+
function PrivateKey(data, network) {
|
|
23
|
+
if (!(this instanceof PrivateKey)) {
|
|
24
|
+
return new PrivateKey(data, network);
|
|
25
|
+
}
|
|
26
|
+
if (data instanceof PrivateKey) {
|
|
27
|
+
return data;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
var info = this._classifyArguments(data, network);
|
|
31
|
+
|
|
32
|
+
// validation
|
|
33
|
+
if (!info.bn || info.bn.cmp(new BN(0)) === 0) {
|
|
34
|
+
throw new TypeError('Number can not be equal to zero, undefined, null or false');
|
|
35
|
+
}
|
|
36
|
+
if (!info.bn.lt(Point.getN())) {
|
|
37
|
+
throw new TypeError('Number must be less than N');
|
|
38
|
+
}
|
|
39
|
+
if (typeof info.network === 'undefined') {
|
|
40
|
+
throw new TypeError('Must specify the network ("livenet" or "testnet")');
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
JSUtil.defineImmutable(this, {
|
|
44
|
+
bn: info.bn,
|
|
45
|
+
compressed: info.compressed,
|
|
46
|
+
network: info.network,
|
|
47
|
+
});
|
|
48
|
+
|
|
49
|
+
return this;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/**
|
|
53
|
+
* Gets the public key associated with this private key.
|
|
54
|
+
* @memberof PrivateKey.prototype
|
|
55
|
+
* @type {PublicKey}
|
|
56
|
+
* @readonly
|
|
57
|
+
*/
|
|
58
|
+
Object.defineProperty(PrivateKey.prototype, 'publicKey', {
|
|
59
|
+
configurable: false,
|
|
60
|
+
enumerable: true,
|
|
61
|
+
get: function() {
|
|
62
|
+
return this.toPublicKey()
|
|
63
|
+
},
|
|
64
|
+
});
|
|
65
|
+
|
|
66
|
+
/**
|
|
67
|
+
* Gets the network associated with this private key.
|
|
68
|
+
* @memberof PrivateKey.prototype
|
|
69
|
+
* @type {Network}
|
|
70
|
+
* @readonly
|
|
71
|
+
*/
|
|
72
|
+
Object.defineProperty(PrivateKey.prototype, 'network', {
|
|
73
|
+
configurable: false,
|
|
74
|
+
enumerable: true,
|
|
75
|
+
get: function () {
|
|
76
|
+
return this.network;
|
|
77
|
+
}
|
|
78
|
+
});
|
|
79
|
+
|
|
80
|
+
|
|
81
|
+
/**
|
|
82
|
+
* Indicates whether the private key is in compressed format.
|
|
83
|
+
* @memberof PrivateKey.prototype
|
|
84
|
+
* @type {boolean}
|
|
85
|
+
* @readonly
|
|
86
|
+
*/
|
|
87
|
+
Object.defineProperty(PrivateKey.prototype, 'compressed', {
|
|
88
|
+
configurable: false,
|
|
89
|
+
enumerable: true,
|
|
90
|
+
get: function () {
|
|
91
|
+
return this.compressed;
|
|
92
|
+
}
|
|
93
|
+
});
|
|
94
|
+
|
|
95
|
+
/**
|
|
96
|
+
* Internal helper to instantiate PrivateKey internal `info` object from
|
|
97
|
+
* different kinds of arguments passed to the constructor.
|
|
98
|
+
*
|
|
99
|
+
* @param {string|BN|Buffer|Object} data
|
|
100
|
+
* @param {Network|string} [network] - a {@link Network} object, or a string with the network name
|
|
101
|
+
* @return {Object}
|
|
102
|
+
* @private
|
|
103
|
+
*/
|
|
104
|
+
PrivateKey.prototype._classifyArguments = function (data, network) {
|
|
105
|
+
var info = {
|
|
106
|
+
compressed: true,
|
|
107
|
+
network: network ? Networks.get(network) : Networks.defaultNetwork,
|
|
108
|
+
};
|
|
109
|
+
|
|
110
|
+
// detect type of data
|
|
111
|
+
if (_.isUndefined(data) || _.isNull(data)) {
|
|
112
|
+
info.bn = PrivateKey._getRandomBN();
|
|
113
|
+
} else if (data instanceof BN) {
|
|
114
|
+
info.bn = data;
|
|
115
|
+
} else if (data instanceof Buffer || data instanceof Uint8Array) {
|
|
116
|
+
info = PrivateKey._transformBuffer(data, network);
|
|
117
|
+
} else if (data.bn && data.network) {
|
|
118
|
+
info = PrivateKey._transformObject(data);
|
|
119
|
+
} else if (!network && Networks.get(data)) {
|
|
120
|
+
info.bn = PrivateKey._getRandomBN();
|
|
121
|
+
info.network = Networks.get(data);
|
|
122
|
+
} else if (typeof data === 'string') {
|
|
123
|
+
if (JSUtil.isHexa(data)) {
|
|
124
|
+
info.bn = new BN(Buffer.from(data, 'hex'));
|
|
125
|
+
} else {
|
|
126
|
+
info = PrivateKey._transformWIF(data, network);
|
|
127
|
+
}
|
|
128
|
+
} else {
|
|
129
|
+
throw new TypeError('First argument is an unrecognized data type.');
|
|
130
|
+
}
|
|
131
|
+
return info;
|
|
132
|
+
};
|
|
133
|
+
|
|
134
|
+
/**
|
|
135
|
+
* Internal function to get a random Big Number (BN)
|
|
136
|
+
*
|
|
137
|
+
* @returns {BN} A new randomly generated BN
|
|
138
|
+
* @private
|
|
139
|
+
*/
|
|
140
|
+
PrivateKey._getRandomBN = function () {
|
|
141
|
+
var condition;
|
|
142
|
+
var bn;
|
|
143
|
+
do {
|
|
144
|
+
var privbuf = Random.getRandomBuffer(32);
|
|
145
|
+
bn = BN.fromBuffer(privbuf);
|
|
146
|
+
condition = bn.lt(Point.getN());
|
|
147
|
+
} while (!condition);
|
|
148
|
+
return bn;
|
|
149
|
+
};
|
|
150
|
+
|
|
151
|
+
/**
|
|
152
|
+
* Internal function to transform a WIF Buffer into a private key
|
|
153
|
+
*
|
|
154
|
+
* @param {Buffer} buf - An WIF string
|
|
155
|
+
* @param {Network|string} [network] - a {@link Network} object, or a string with the network name
|
|
156
|
+
* @returns {Object} An object with keys: bn, network and compressed
|
|
157
|
+
* @private
|
|
158
|
+
*/
|
|
159
|
+
PrivateKey._transformBuffer = function (buf, network) {
|
|
160
|
+
var info = {};
|
|
161
|
+
|
|
162
|
+
if (buf.length === 32) {
|
|
163
|
+
return PrivateKey._transformBNBuffer(buf, network);
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
info.network = Networks.get(buf[0], 'privatekey');
|
|
167
|
+
|
|
168
|
+
if (!info.network) {
|
|
169
|
+
throw new Error('Invalid network');
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
if (network && info.network !== Networks.get(network)) {
|
|
173
|
+
throw new TypeError('Private key network mismatch');
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
if (buf.length === 1 + 32 + 1 && buf[1 + 32 + 1 - 1] === 1) {
|
|
177
|
+
info.compressed = true;
|
|
178
|
+
} else if (buf.length === 1 + 32) {
|
|
179
|
+
info.compressed = false;
|
|
180
|
+
} else {
|
|
181
|
+
throw new Error('Length of buffer must be 33 (uncompressed) or 34 (compressed)');
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
info.bn = BN.fromBuffer(buf.slice(1, 32 + 1));
|
|
185
|
+
|
|
186
|
+
return info;
|
|
187
|
+
};
|
|
188
|
+
|
|
189
|
+
/**
|
|
190
|
+
* Internal function to transform a BN buffer into a private key
|
|
191
|
+
*
|
|
192
|
+
* @param {Buffer} buf
|
|
193
|
+
* @param {Network|string=} network - a {@link Network} object, or a string with the network name
|
|
194
|
+
* @returns {object} an Object with keys: bn, network, and compressed
|
|
195
|
+
* @private
|
|
196
|
+
*/
|
|
197
|
+
PrivateKey._transformBNBuffer = function (buf, network) {
|
|
198
|
+
var info = {};
|
|
199
|
+
info.network = Networks.get(network) || Networks.defaultNetwork;
|
|
200
|
+
info.bn = BN.fromBuffer(buf);
|
|
201
|
+
info.compressed = false;
|
|
202
|
+
return info;
|
|
203
|
+
};
|
|
204
|
+
|
|
205
|
+
/**
|
|
206
|
+
* Internal function to transform a WIF string into a private key
|
|
207
|
+
*
|
|
208
|
+
* @param {string} buf - An WIF string
|
|
209
|
+
* @returns {Object} An object with keys: bn, network and compressed
|
|
210
|
+
* @private
|
|
211
|
+
*/
|
|
212
|
+
PrivateKey._transformWIF = function (str, network) {
|
|
213
|
+
return PrivateKey._transformBuffer(Base58Check.decode(str), network);
|
|
214
|
+
};
|
|
215
|
+
|
|
216
|
+
/**
|
|
217
|
+
* Instantiate a PrivateKey from a Buffer with the DER or WIF representation
|
|
218
|
+
*
|
|
219
|
+
* @param {Buffer} buf
|
|
220
|
+
* @param {Network} network
|
|
221
|
+
* @return {PrivateKey}
|
|
222
|
+
*/
|
|
223
|
+
PrivateKey.fromBuffer = function (buf, network) {
|
|
224
|
+
return new PrivateKey(buf, network);
|
|
225
|
+
};
|
|
226
|
+
|
|
227
|
+
/**
|
|
228
|
+
* Creates a PrivateKey instance from a hexadecimal string.
|
|
229
|
+
* @param {string} hex - The hexadecimal string representation of the private key.
|
|
230
|
+
* @param {Network} network - The network associated with the private key.
|
|
231
|
+
* @returns {PrivateKey} A PrivateKey instance.
|
|
232
|
+
*/
|
|
233
|
+
PrivateKey.fromHex = function (hex, network) {
|
|
234
|
+
return PrivateKey.fromBuffer(Buffer.from(hex, 'hex'), network);
|
|
235
|
+
};
|
|
236
|
+
|
|
237
|
+
/**
|
|
238
|
+
* Internal function to transform a JSON string on plain object into a private key
|
|
239
|
+
* return this.
|
|
240
|
+
*
|
|
241
|
+
* @param {string} json - A JSON string or plain object
|
|
242
|
+
* @returns {Object} An object with keys: bn, network and compressed
|
|
243
|
+
* @private
|
|
244
|
+
*/
|
|
245
|
+
PrivateKey._transformObject = function (json) {
|
|
246
|
+
var bn = new BN(json.bn, 'hex');
|
|
247
|
+
var network = Networks.get(json.network);
|
|
248
|
+
return {
|
|
249
|
+
bn: bn,
|
|
250
|
+
network: network,
|
|
251
|
+
compressed: json.compressed,
|
|
252
|
+
};
|
|
253
|
+
};
|
|
254
|
+
|
|
255
|
+
/**
|
|
256
|
+
* Instantiate a PrivateKey from a WIF string
|
|
257
|
+
*
|
|
258
|
+
* @param {string} str - The WIF encoded private key string
|
|
259
|
+
* @returns {PrivateKey} A new valid instance of PrivateKey
|
|
260
|
+
*/
|
|
261
|
+
PrivateKey.fromString = PrivateKey.fromWIF = function (str) {
|
|
262
|
+
$.checkArgument(_.isString(str), 'First argument is expected to be a string.');
|
|
263
|
+
return new PrivateKey(str);
|
|
264
|
+
};
|
|
265
|
+
|
|
266
|
+
/**
|
|
267
|
+
* Instantiate a PrivateKey from a plain JavaScript object
|
|
268
|
+
*
|
|
269
|
+
* @param {Object} obj - The output from privateKey.toObject()
|
|
270
|
+
*/
|
|
271
|
+
PrivateKey.fromObject = PrivateKey.fromJSON = function (obj) {
|
|
272
|
+
$.checkArgument(_.isObject(obj), 'First argument is expected to be an object.');
|
|
273
|
+
return new PrivateKey(obj);
|
|
274
|
+
};
|
|
275
|
+
|
|
276
|
+
/**
|
|
277
|
+
* Instantiate a PrivateKey from random bytes
|
|
278
|
+
*
|
|
279
|
+
* @param {string|Network} [network] - Either "livenet" or "testnet"
|
|
280
|
+
* @returns {PrivateKey} A new valid instance of PrivateKey
|
|
281
|
+
*/
|
|
282
|
+
PrivateKey.fromRandom = function (network) {
|
|
283
|
+
var bn = PrivateKey._getRandomBN();
|
|
284
|
+
return new PrivateKey(bn, network);
|
|
285
|
+
};
|
|
286
|
+
|
|
287
|
+
/**
|
|
288
|
+
* Check if there would be any errors when initializing a PrivateKey
|
|
289
|
+
*
|
|
290
|
+
* @param {string} data - The encoded data in various formats
|
|
291
|
+
* @param {string|Network} [network] - Either "livenet" or "testnet"
|
|
292
|
+
* @returns {null|Error} An error if exists
|
|
293
|
+
*/
|
|
294
|
+
|
|
295
|
+
PrivateKey.getValidationError = function (data, network) {
|
|
296
|
+
var error;
|
|
297
|
+
try {
|
|
298
|
+
new PrivateKey(data, network);
|
|
299
|
+
} catch (e) {
|
|
300
|
+
error = e;
|
|
301
|
+
}
|
|
302
|
+
return error;
|
|
303
|
+
};
|
|
304
|
+
|
|
305
|
+
/**
|
|
306
|
+
* Check if the parameters are valid
|
|
307
|
+
*
|
|
308
|
+
* @param {string} data - The encoded data in various formats
|
|
309
|
+
* @param {string|Network} [network] - Either "livenet" or "testnet"
|
|
310
|
+
* @returns {Boolean} If the private key is would be valid
|
|
311
|
+
*/
|
|
312
|
+
PrivateKey.isValid = function (data, network) {
|
|
313
|
+
if (!data) {
|
|
314
|
+
return false;
|
|
315
|
+
}
|
|
316
|
+
return !PrivateKey.getValidationError(data, network);
|
|
317
|
+
};
|
|
318
|
+
|
|
319
|
+
/**
|
|
320
|
+
* Will output the PrivateKey in WIF
|
|
321
|
+
*
|
|
322
|
+
* @returns {string}
|
|
323
|
+
*/
|
|
324
|
+
PrivateKey.prototype.toString = function () {
|
|
325
|
+
return this.toWIF();
|
|
326
|
+
};
|
|
327
|
+
|
|
328
|
+
/**
|
|
329
|
+
* Will output the PrivateKey to a WIF string
|
|
330
|
+
*
|
|
331
|
+
* @returns {string} A WIP representation of the private key
|
|
332
|
+
*/
|
|
333
|
+
PrivateKey.prototype.toWIF = function () {
|
|
334
|
+
var network = this.network;
|
|
335
|
+
var compressed = this.compressed;
|
|
336
|
+
|
|
337
|
+
var buf;
|
|
338
|
+
if (compressed) {
|
|
339
|
+
buf = Buffer.concat([
|
|
340
|
+
Buffer.from([network.privatekey]),
|
|
341
|
+
this.bn.toBuffer({ size: 32 }),
|
|
342
|
+
Buffer.from([0x01]),
|
|
343
|
+
]);
|
|
344
|
+
} else {
|
|
345
|
+
buf = Buffer.concat([Buffer.from([network.privatekey]), this.bn.toBuffer({ size: 32 })]);
|
|
346
|
+
}
|
|
347
|
+
|
|
348
|
+
return Base58Check.encode(buf);
|
|
349
|
+
};
|
|
350
|
+
|
|
351
|
+
/**
|
|
352
|
+
* Will return the private key as a BN instance
|
|
353
|
+
*
|
|
354
|
+
* @returns {BN} A BN instance of the private key
|
|
355
|
+
*/
|
|
356
|
+
PrivateKey.prototype.toBigNumber = function () {
|
|
357
|
+
return this.bn;
|
|
358
|
+
};
|
|
359
|
+
|
|
360
|
+
/**
|
|
361
|
+
* Will return the private key as a BN buffer
|
|
362
|
+
*
|
|
363
|
+
* @returns {Buffer} A buffer of the private key
|
|
364
|
+
*/
|
|
365
|
+
PrivateKey.prototype.toBuffer = function () {
|
|
366
|
+
return this.bn.toBuffer({ size: 32 });
|
|
367
|
+
};
|
|
368
|
+
|
|
369
|
+
/**
|
|
370
|
+
* Converts the private key to a hexadecimal string representation.
|
|
371
|
+
* @returns {string} Hexadecimal string of the private key.
|
|
372
|
+
*/
|
|
373
|
+
PrivateKey.prototype.toHex = function () {
|
|
374
|
+
return this.toBuffer().toString('hex');
|
|
375
|
+
};
|
|
376
|
+
|
|
377
|
+
/**
|
|
378
|
+
* Will return the corresponding public key
|
|
379
|
+
*
|
|
380
|
+
* @returns {PublicKey} A public key generated from the private key
|
|
381
|
+
*/
|
|
382
|
+
PrivateKey.prototype.toPublicKey = function () {
|
|
383
|
+
if (!this._pubkey) {
|
|
384
|
+
this._pubkey = PublicKey.fromPrivateKey(this);
|
|
385
|
+
}
|
|
386
|
+
return this._pubkey;
|
|
387
|
+
};
|
|
388
|
+
|
|
389
|
+
/**
|
|
390
|
+
* Will return an address for the private key
|
|
391
|
+
* @param {Network|string} [network] - optional parameter specifying
|
|
392
|
+
* the desired network for the address
|
|
393
|
+
*
|
|
394
|
+
* @returns {Address} An address generated from the private key
|
|
395
|
+
*/
|
|
396
|
+
PrivateKey.prototype.toAddress = function (network) {
|
|
397
|
+
var pubkey = this.toPublicKey().toBuffer();
|
|
398
|
+
return Address.fromPublicKey(pubkey, network || this.network);
|
|
399
|
+
};
|
|
400
|
+
|
|
401
|
+
/**
|
|
402
|
+
* @returns {Object} A plain object representation
|
|
403
|
+
*/
|
|
404
|
+
PrivateKey.prototype.toObject = PrivateKey.prototype.toJSON = function toObject() {
|
|
405
|
+
return {
|
|
406
|
+
bn: this.bn.toString('hex'),
|
|
407
|
+
compressed: this.compressed,
|
|
408
|
+
network: this.network.toString(),
|
|
409
|
+
};
|
|
410
|
+
};
|
|
411
|
+
|
|
412
|
+
/**
|
|
413
|
+
* Will return a string formatted for the console
|
|
414
|
+
*
|
|
415
|
+
* @returns {string} Private key
|
|
416
|
+
*/
|
|
417
|
+
PrivateKey.prototype.inspect = function () {
|
|
418
|
+
var uncompressed = !this.compressed ? ', uncompressed' : '';
|
|
419
|
+
return '<PrivateKey: ' + this.toHex() + ', network: ' + this.network + uncompressed + '>';
|
|
420
|
+
};
|
|
421
|
+
|
|
422
|
+
module.exports = PrivateKey;
|
|
@@ -1,13 +1,13 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
-
var BN = require('./crypto/bn');
|
|
4
|
-
var Point = require('./crypto/point');
|
|
5
|
-
var Hash = require('./crypto/hash');
|
|
6
|
-
var JSUtil = require('./util/js');
|
|
7
|
-
var
|
|
8
|
-
var _ = require('./util/_');
|
|
9
|
-
var $ = require('./util/preconditions');
|
|
10
|
-
|
|
3
|
+
var BN = require('./crypto/bn.cjs');
|
|
4
|
+
var Point = require('./crypto/point.cjs');
|
|
5
|
+
var Hash = require('./crypto/hash.cjs');
|
|
6
|
+
var JSUtil = require('./util/js.cjs');
|
|
7
|
+
var Networks = require('./networks.cjs');
|
|
8
|
+
var _ = require('./util/_.cjs');
|
|
9
|
+
var $ = require('./util/preconditions.cjs');
|
|
10
|
+
var Address = require('./address.cjs');
|
|
11
11
|
/**
|
|
12
12
|
* Instantiate a PublicKey from a {@link PrivateKey}, {@link Point}, `string`, or `Buffer`.
|
|
13
13
|
*
|
|
@@ -28,7 +28,7 @@ var $ = require('./util/preconditions');
|
|
|
28
28
|
*
|
|
29
29
|
* @param {string} data - The encoded data in various formats
|
|
30
30
|
* @param {Object} extra - additional options
|
|
31
|
-
* @param {Network
|
|
31
|
+
* @param {Network} extra.network - Which network should the address for this public key be for
|
|
32
32
|
* @param {String=} extra.compressed - If the public key is compressed
|
|
33
33
|
* @returns {PublicKey} A new valid instance of an PublicKey
|
|
34
34
|
* @constructor
|
|
@@ -54,7 +54,7 @@ function PublicKey(data, extra) {
|
|
|
54
54
|
JSUtil.defineImmutable(this, {
|
|
55
55
|
point: info.point,
|
|
56
56
|
compressed: info.compressed,
|
|
57
|
-
network: info.network ||
|
|
57
|
+
network: info.network || Networks.defaultNetwork,
|
|
58
58
|
});
|
|
59
59
|
|
|
60
60
|
return this;
|
|
@@ -85,7 +85,7 @@ PublicKey.prototype._classifyArgs = function (data, extra) {
|
|
|
85
85
|
throw new TypeError('First argument is an unrecognized data format.');
|
|
86
86
|
}
|
|
87
87
|
if (!info.network) {
|
|
88
|
-
info.network = _.isUndefined(extra.network) ? undefined :
|
|
88
|
+
info.network = _.isUndefined(extra.network) ? undefined : Networks.get(extra.network);
|
|
89
89
|
}
|
|
90
90
|
return info;
|
|
91
91
|
};
|
|
@@ -98,8 +98,7 @@ PublicKey.prototype._classifyArgs = function (data, extra) {
|
|
|
98
98
|
* @private
|
|
99
99
|
*/
|
|
100
100
|
PublicKey._isPrivateKey = function (param) {
|
|
101
|
-
|
|
102
|
-
return param instanceof PrivateKey;
|
|
101
|
+
return param && param.toPublicKey;
|
|
103
102
|
};
|
|
104
103
|
|
|
105
104
|
/**
|
|
@@ -357,12 +356,11 @@ PublicKey.prototype._getID = function _getID() {
|
|
|
357
356
|
/**
|
|
358
357
|
* Will return an address for the public key
|
|
359
358
|
*
|
|
360
|
-
* @param {
|
|
359
|
+
* @param {string|Network} [network] - Which network should the address be for
|
|
361
360
|
* @returns {Address} An address generated from the public key
|
|
362
361
|
*/
|
|
363
362
|
PublicKey.prototype.toAddress = function (network) {
|
|
364
|
-
|
|
365
|
-
return Address.fromPublicKey(this, network || this.network);
|
|
363
|
+
return Address.fromPublicKey(this.toBuffer(), network || this.network);
|
|
366
364
|
};
|
|
367
365
|
|
|
368
366
|
/**
|