@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,8 +1,8 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
-
var BN = require('../bn');
|
|
4
|
-
var $ = require('../util/preconditions');
|
|
5
|
-
var _ = require('../util/_');
|
|
3
|
+
var BN = require('../bn.cjs');
|
|
4
|
+
var $ = require('../util/preconditions.cjs');
|
|
5
|
+
var _ = require('../util/_.cjs');
|
|
6
6
|
|
|
7
7
|
var reversebuf = function (buf) {
|
|
8
8
|
var buf2 = Buffer.alloc(buf.length);
|
|
@@ -1,15 +1,20 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
-
var BN = require('./bn');
|
|
4
|
-
var Point = require('./point');
|
|
5
|
-
var Signature = require('./signature');
|
|
6
|
-
var PublicKey = require('../publickey');
|
|
7
|
-
var Random = require('./random');
|
|
8
|
-
var Hash = require('./hash');
|
|
9
|
-
var _ = require('../util/_');
|
|
10
|
-
var $ = require('../util/preconditions');
|
|
11
|
-
|
|
12
|
-
|
|
3
|
+
var BN = require('./bn.cjs');
|
|
4
|
+
var Point = require('./point.cjs');
|
|
5
|
+
var Signature = require('./signature.cjs');
|
|
6
|
+
var PublicKey = require('../publickey.cjs');
|
|
7
|
+
var Random = require('./random.cjs');
|
|
8
|
+
var Hash = require('./hash.cjs');
|
|
9
|
+
var _ = require('../util/_.cjs');
|
|
10
|
+
var $ = require('../util/preconditions.cjs');
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* Creates an ECDSA instance.
|
|
14
|
+
* @constructor
|
|
15
|
+
* @param {Object} [obj] - Optional object containing properties to initialize the instance.
|
|
16
|
+
*/
|
|
17
|
+
function ECDSA(obj) {
|
|
13
18
|
if (!(this instanceof ECDSA)) {
|
|
14
19
|
return new ECDSA(obj);
|
|
15
20
|
}
|
|
@@ -18,6 +23,18 @@ var ECDSA = function ECDSA(obj) {
|
|
|
18
23
|
}
|
|
19
24
|
};
|
|
20
25
|
|
|
26
|
+
/**
|
|
27
|
+
* Updates the ECDSA instance properties with provided values.
|
|
28
|
+
* @param {Object} obj - Object containing properties to update
|
|
29
|
+
* @param {Buffer} [obj.hashbuf] - Hash buffer
|
|
30
|
+
* @param {string} [obj.endian] - Endianness of hashbuf
|
|
31
|
+
* @param {PrivateKey} [obj.privkey] - Private key
|
|
32
|
+
* @param {PublicKey} [obj.pubkey] - Public key (derived from privkey if not provided)
|
|
33
|
+
* @param {Signature} [obj.sig] - Signature
|
|
34
|
+
* @param {BigInteger} [obj.k] - Random number k
|
|
35
|
+
* @param {boolean} [obj.verified] - Verification status
|
|
36
|
+
* @returns {ECDSA} Returns the updated ECDSA instance
|
|
37
|
+
*/
|
|
21
38
|
ECDSA.prototype.set = function (obj) {
|
|
22
39
|
this.hashbuf = obj.hashbuf || this.hashbuf;
|
|
23
40
|
this.endian = obj.endian || this.endian; // the endianness of hashbuf
|
|
@@ -29,10 +46,21 @@ ECDSA.prototype.set = function (obj) {
|
|
|
29
46
|
return this;
|
|
30
47
|
};
|
|
31
48
|
|
|
49
|
+
/**
|
|
50
|
+
* Converts the private key to a public key and stores it in the `pubkey` property.
|
|
51
|
+
*/
|
|
32
52
|
ECDSA.prototype.privkey2pubkey = function () {
|
|
33
53
|
this.pubkey = this.privkey.toPublicKey();
|
|
34
54
|
};
|
|
35
55
|
|
|
56
|
+
/**
|
|
57
|
+
* Calculates the recovery factor (i) for ECDSA signature verification.
|
|
58
|
+
* Iterates through possible recovery factors (0-3) to find the one that
|
|
59
|
+
* reconstructs the correct public key from the signature.
|
|
60
|
+
*
|
|
61
|
+
* @returns {ECDSA} Returns the instance with updated signature properties if successful.
|
|
62
|
+
* @throws {Error} Throws if no valid recovery factor is found after all iterations.
|
|
63
|
+
*/
|
|
36
64
|
ECDSA.prototype.calci = function () {
|
|
37
65
|
for (var i = 0; i < 4; i++) {
|
|
38
66
|
this.sig.i = i;
|
|
@@ -54,11 +82,21 @@ ECDSA.prototype.calci = function () {
|
|
|
54
82
|
throw new Error('Unable to find valid recovery factor');
|
|
55
83
|
};
|
|
56
84
|
|
|
85
|
+
/**
|
|
86
|
+
* Creates an ECDSA instance from a JSON string representation.
|
|
87
|
+
* @param {string} str - JSON string containing ECDSA parameters.
|
|
88
|
+
* @returns {ECDSA} New ECDSA instance initialized with parsed data.
|
|
89
|
+
*/
|
|
57
90
|
ECDSA.fromString = function (str) {
|
|
58
91
|
var obj = JSON.parse(str);
|
|
59
92
|
return new ECDSA(obj);
|
|
60
93
|
};
|
|
61
94
|
|
|
95
|
+
/**
|
|
96
|
+
* Generates a random value `k` for ECDSA signing.
|
|
97
|
+
* The value is generated within the range (0, N) where N is the curve order.
|
|
98
|
+
* The generated `k` is stored in the instance and returned for chaining.
|
|
99
|
+
*/
|
|
62
100
|
ECDSA.prototype.randomK = function () {
|
|
63
101
|
var N = Point.getN();
|
|
64
102
|
var k;
|
|
@@ -69,7 +107,15 @@ ECDSA.prototype.randomK = function () {
|
|
|
69
107
|
return this;
|
|
70
108
|
};
|
|
71
109
|
|
|
72
|
-
|
|
110
|
+
|
|
111
|
+
/**
|
|
112
|
+
* Generates a deterministic K value for ECDSA signing as per RFC 6979.
|
|
113
|
+
* See:
|
|
114
|
+
* https://tools.ietf.org/html/rfc6979#section-3.2
|
|
115
|
+
* Handles invalid r/s cases by incrementing badrs counter and regenerating K.
|
|
116
|
+
* @param {number} [badrs=0] - Counter for invalid r/s cases (default: 0)
|
|
117
|
+
* @returns {ECDSA} Returns the ECDSA instance for chaining
|
|
118
|
+
*/
|
|
73
119
|
ECDSA.prototype.deterministicK = function (badrs) {
|
|
74
120
|
// if r or s were invalid when this function was used in signing,
|
|
75
121
|
// we do not want to actually compute r, s here for efficiency, so,
|
|
@@ -105,9 +151,20 @@ ECDSA.prototype.deterministicK = function (badrs) {
|
|
|
105
151
|
return this;
|
|
106
152
|
};
|
|
107
153
|
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
154
|
+
/**
|
|
155
|
+
* Converts an ECDSA signature to its corresponding public key.
|
|
156
|
+
*
|
|
157
|
+
* The method follows the ECDSA public key recovery process:
|
|
158
|
+
* 1. Validates the recovery parameter `i` (must be 0-3)
|
|
159
|
+
* 2. Derives the public key point Q using the formula: Q = r⁻¹(sR - eG)
|
|
160
|
+
* 3. Validates the derived curve point
|
|
161
|
+
*
|
|
162
|
+
* see:
|
|
163
|
+
* https://bitcointalk.org/index.php?topic=6430.0
|
|
164
|
+
* http://stackoverflow.com/questions/19665491/how-do-i-get-an-ecdsa-public-key-from-just-a-bitcoin-signature-sec1-4-1-6-k
|
|
165
|
+
* @returns {PublicKey} The recovered public key
|
|
166
|
+
* @throws {Error} If recovery parameter is invalid or derived point is invalid
|
|
167
|
+
*/
|
|
111
168
|
ECDSA.prototype.toPublicKey = function () {
|
|
112
169
|
var i = this.sig.i;
|
|
113
170
|
$.checkArgument(
|
|
@@ -155,6 +212,14 @@ ECDSA.prototype.toPublicKey = function () {
|
|
|
155
212
|
return pubkey;
|
|
156
213
|
};
|
|
157
214
|
|
|
215
|
+
/**
|
|
216
|
+
* Validates an ECDSA signature and returns an error message if invalid.
|
|
217
|
+
* Checks:
|
|
218
|
+
* - hashbuf is a 32-byte buffer
|
|
219
|
+
* - r and s values are within valid range
|
|
220
|
+
* - Signature verification against public key
|
|
221
|
+
* @returns {string|boolean} Error message if invalid, false if valid
|
|
222
|
+
*/
|
|
158
223
|
ECDSA.prototype.sigError = function () {
|
|
159
224
|
if (!Buffer.isBuffer(this.hashbuf) || this.hashbuf.length !== 32) {
|
|
160
225
|
return 'hashbuf must be a 32 byte buffer';
|
|
@@ -191,6 +256,13 @@ ECDSA.prototype.sigError = function () {
|
|
|
191
256
|
}
|
|
192
257
|
};
|
|
193
258
|
|
|
259
|
+
/**
|
|
260
|
+
* Converts the signature `s` value to its low-S form to comply with BIP 62.
|
|
261
|
+
* This prevents signature malleability by ensuring `s` is not greater than half the curve order.
|
|
262
|
+
* @param {BN} s - The signature `s` value as a big number.
|
|
263
|
+
* @returns {BN} The low-S normalized value.
|
|
264
|
+
* @static
|
|
265
|
+
*/
|
|
194
266
|
ECDSA.toLowS = function (s) {
|
|
195
267
|
// enforce low s
|
|
196
268
|
// see BIP 62, "low S values in signatures"
|
|
@@ -206,6 +278,15 @@ ECDSA.toLowS = function (s) {
|
|
|
206
278
|
return s;
|
|
207
279
|
};
|
|
208
280
|
|
|
281
|
+
/**
|
|
282
|
+
* Finds a valid ECDSA signature (r, s) for the given private key `d` and message hash `e`.
|
|
283
|
+
* Uses deterministic k-value generation if initial attempts fail.
|
|
284
|
+
*
|
|
285
|
+
* @param {BN} d - Private key as a big number.
|
|
286
|
+
* @param {BN} e - Message hash as a big number.
|
|
287
|
+
* @returns {Object} Signature object with properties `r` and `s` (big numbers).
|
|
288
|
+
* @throws Will throw if unable to find valid signature after multiple attempts.
|
|
289
|
+
*/
|
|
209
290
|
ECDSA.prototype._findSignature = function (d, e) {
|
|
210
291
|
var N = Point.getN();
|
|
211
292
|
var G = Point.getG();
|
|
@@ -233,6 +314,14 @@ ECDSA.prototype._findSignature = function (d, e) {
|
|
|
233
314
|
};
|
|
234
315
|
};
|
|
235
316
|
|
|
317
|
+
/**
|
|
318
|
+
* Signs a message using ECDSA.
|
|
319
|
+
*
|
|
320
|
+
* @param {Buffer} hashbuf - 32-byte buffer containing the hash of the message to sign.
|
|
321
|
+
* @param {PrivateKey} privkey - Private key used for signing.
|
|
322
|
+
* @returns {ECDSA} Returns the instance for chaining.
|
|
323
|
+
* @throws {Error} Throws if parameters are invalid or hashbuf is not a 32-byte buffer.
|
|
324
|
+
*/
|
|
236
325
|
ECDSA.prototype.sign = function () {
|
|
237
326
|
var hashbuf = this.hashbuf;
|
|
238
327
|
var privkey = this.privkey;
|
|
@@ -260,11 +349,22 @@ ECDSA.prototype.sign = function () {
|
|
|
260
349
|
return this;
|
|
261
350
|
};
|
|
262
351
|
|
|
352
|
+
/**
|
|
353
|
+
* Signs the message using a randomly generated k value.
|
|
354
|
+
*
|
|
355
|
+
* @returns The signature object containing r and s values.
|
|
356
|
+
*/
|
|
263
357
|
ECDSA.prototype.signRandomK = function () {
|
|
264
358
|
this.randomK();
|
|
265
359
|
return this.sign();
|
|
266
360
|
};
|
|
267
361
|
|
|
362
|
+
/**
|
|
363
|
+
* Converts the ECDSA instance to a JSON string representation.
|
|
364
|
+
* Includes hash buffer, private key, public key, signature, and k value if present.
|
|
365
|
+
* Each property is converted to a string format (hex for hashbuf, toString() for others).
|
|
366
|
+
* @returns {string} JSON string containing the ECDSA instance properties
|
|
367
|
+
*/
|
|
268
368
|
ECDSA.prototype.toString = function () {
|
|
269
369
|
var obj = {};
|
|
270
370
|
if (this.hashbuf) {
|
|
@@ -285,6 +385,10 @@ ECDSA.prototype.toString = function () {
|
|
|
285
385
|
return JSON.stringify(obj);
|
|
286
386
|
};
|
|
287
387
|
|
|
388
|
+
/**
|
|
389
|
+
* Verifies the ECDSA signature and updates the `verified` property.
|
|
390
|
+
* @returns {ECDSA} The current instance for chaining.
|
|
391
|
+
*/
|
|
288
392
|
ECDSA.prototype.verify = function () {
|
|
289
393
|
if (!this.sigError()) {
|
|
290
394
|
this.verified = true;
|
|
@@ -294,6 +398,13 @@ ECDSA.prototype.verify = function () {
|
|
|
294
398
|
return this;
|
|
295
399
|
};
|
|
296
400
|
|
|
401
|
+
/**
|
|
402
|
+
* Signs a message hash using ECDSA with the given private key.
|
|
403
|
+
* @param {Buffer} hashbuf - The hash of the message to sign
|
|
404
|
+
* @param {PrivateKey} privkey - The private key to sign with
|
|
405
|
+
* @param {string} [endian] - Endianness of the input/output (optional)
|
|
406
|
+
* @returns {Signature} The ECDSA signature
|
|
407
|
+
*/
|
|
297
408
|
ECDSA.sign = function (hashbuf, privkey, endian) {
|
|
298
409
|
return ECDSA()
|
|
299
410
|
.set({
|
|
@@ -304,6 +415,14 @@ ECDSA.sign = function (hashbuf, privkey, endian) {
|
|
|
304
415
|
.sign().sig;
|
|
305
416
|
};
|
|
306
417
|
|
|
418
|
+
/**
|
|
419
|
+
* Signs a hash buffer with a private key and calculates the 'i' value.
|
|
420
|
+
* @param {Buffer} hashbuf - The hash buffer to sign.
|
|
421
|
+
* @param {Buffer} privkey - The private key used for signing.
|
|
422
|
+
* @param {string} [endian] - The endianness of the input data (optional).
|
|
423
|
+
* @returns {Buffer} The resulting signature.
|
|
424
|
+
* @static
|
|
425
|
+
*/
|
|
307
426
|
ECDSA.signWithCalcI = function (hashbuf, privkey, endian) {
|
|
308
427
|
return ECDSA()
|
|
309
428
|
.set({
|
|
@@ -315,6 +434,14 @@ ECDSA.signWithCalcI = function (hashbuf, privkey, endian) {
|
|
|
315
434
|
.calci().sig;
|
|
316
435
|
};
|
|
317
436
|
|
|
437
|
+
/**
|
|
438
|
+
* Signs a message hash using ECDSA with a randomly generated K value.
|
|
439
|
+
* @param {Buffer} hashbuf - The message hash to sign.
|
|
440
|
+
* @param {Buffer} privkey - The private key used for signing.
|
|
441
|
+
* @param {string} [endian] - The endianness of the input/output (default: 'big').
|
|
442
|
+
* @returns {Buffer} The generated ECDSA signature.
|
|
443
|
+
* @static
|
|
444
|
+
*/
|
|
318
445
|
ECDSA.signRandomK = function (hashbuf, privkey, endian) {
|
|
319
446
|
return ECDSA()
|
|
320
447
|
.set({
|
|
@@ -325,6 +452,15 @@ ECDSA.signRandomK = function (hashbuf, privkey, endian) {
|
|
|
325
452
|
.signRandomK().sig;
|
|
326
453
|
};
|
|
327
454
|
|
|
455
|
+
/**
|
|
456
|
+
* Verifies an ECDSA signature against a hash and public key.
|
|
457
|
+
* @param {Buffer} hashbuf - The hash buffer to verify against.
|
|
458
|
+
* @param {Signature} sig - The signature to verify.
|
|
459
|
+
* @param {PublicKey} pubkey - The public key to verify with.
|
|
460
|
+
* @param {string} [endian] - The endianness of the input data (optional).
|
|
461
|
+
* @returns {boolean} True if the signature is valid, false otherwise.
|
|
462
|
+
* @static
|
|
463
|
+
*/
|
|
328
464
|
ECDSA.verify = function (hashbuf, sig, pubkey, endian) {
|
|
329
465
|
return ECDSA()
|
|
330
466
|
.set({
|
|
@@ -1,9 +1,17 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
3
|
var crypto = require('crypto');
|
|
4
|
-
var $ = require('../util/preconditions');
|
|
4
|
+
var $ = require('../util/preconditions.cjs');
|
|
5
5
|
|
|
6
|
-
|
|
6
|
+
/**
|
|
7
|
+
* Represents a hash utility class.
|
|
8
|
+
* @constructor
|
|
9
|
+
*/
|
|
10
|
+
function Hash() {
|
|
11
|
+
if (!(this instanceof Hash)) {
|
|
12
|
+
return new Hash();
|
|
13
|
+
}
|
|
14
|
+
};
|
|
7
15
|
|
|
8
16
|
/**
|
|
9
17
|
* A SHA or SHA1 hash, which is always 160 bits or 20 bytes long.
|
|
@@ -169,3 +177,6 @@ Hash.sha256hmac = function (data, key) {
|
|
|
169
177
|
Hash.sha512hmac = function (data, key) {
|
|
170
178
|
return Hash.hmac(Hash.sha512, data, key);
|
|
171
179
|
};
|
|
180
|
+
|
|
181
|
+
|
|
182
|
+
module.exports = Hash
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
var BN = require('./bn.cjs');
|
|
2
|
+
var ECDSA = require('./ecdsa.cjs');
|
|
3
|
+
var Hash = require('./hash.cjs');
|
|
4
|
+
var Random = require('./random.cjs');
|
|
5
|
+
var Point = require('./point.cjs');
|
|
6
|
+
var Signature = require('./signature.cjs');
|
|
7
|
+
|
|
8
|
+
module.exports = {
|
|
9
|
+
BN: BN,
|
|
10
|
+
ECDSA: ECDSA,
|
|
11
|
+
Hash: Hash,
|
|
12
|
+
Random: Random,
|
|
13
|
+
Point: Point,
|
|
14
|
+
Signature: Signature
|
|
15
|
+
}
|
|
16
|
+
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
-
var BN = require('./bn');
|
|
3
|
+
var BN = require('./bn.cjs');
|
|
4
4
|
var elliptic = require('elliptic');
|
|
5
5
|
var EC = elliptic.ec;
|
|
6
6
|
var ec = new EC('secp256k1');
|
|
@@ -20,10 +20,10 @@ var ecPointFromX = ec.curve.pointFromX.bind(ec.curve);
|
|
|
20
20
|
* @returns {Point} An instance of Point
|
|
21
21
|
* @constructor
|
|
22
22
|
*/
|
|
23
|
-
|
|
23
|
+
function Point(x, y, isRed) {
|
|
24
24
|
try {
|
|
25
25
|
var point = ecPoint(x, y, isRed);
|
|
26
|
-
} catch (
|
|
26
|
+
} catch (_) {
|
|
27
27
|
throw new Error('Invalid Point');
|
|
28
28
|
}
|
|
29
29
|
point.validate();
|
|
@@ -41,6 +41,7 @@ Point.prototype = Object.getPrototypeOf(ec.curve.point());
|
|
|
41
41
|
* @param {BN|String} x - The X coordinate
|
|
42
42
|
* @throws {Error} A validation error if exists
|
|
43
43
|
* @returns {Point} An instance of Point
|
|
44
|
+
* @static
|
|
44
45
|
*/
|
|
45
46
|
Point.fromX = function fromX(odd, x) {
|
|
46
47
|
try {
|
|
@@ -58,6 +59,7 @@ Point.fromX = function fromX(odd, x) {
|
|
|
58
59
|
*
|
|
59
60
|
* @link https://en.bitcoin.it/wiki/Secp256k1
|
|
60
61
|
* @returns {Point} An instance of the base point.
|
|
62
|
+
* @static
|
|
61
63
|
*/
|
|
62
64
|
Point.getG = function getG() {
|
|
63
65
|
return ec.curve.g;
|
|
@@ -70,6 +72,7 @@ Point.getG = function getG() {
|
|
|
70
72
|
*
|
|
71
73
|
* @link https://en.bitcoin.it/wiki/Private_key#Range_of_valid_ECDSA_private_keys
|
|
72
74
|
* @returns {BN} A BN instance of the number of points on the curve
|
|
75
|
+
* @static
|
|
73
76
|
*/
|
|
74
77
|
Point.getN = function getN() {
|
|
75
78
|
return new BN(ec.curve.n.toArray());
|
|
@@ -116,7 +119,7 @@ Point.prototype.validate = function validate() {
|
|
|
116
119
|
var p2;
|
|
117
120
|
try {
|
|
118
121
|
p2 = ecPointFromX(this.getX(), this.getY().isOdd());
|
|
119
|
-
} catch (
|
|
122
|
+
} catch (_) {
|
|
120
123
|
throw new Error('Point does not lie on the curve');
|
|
121
124
|
}
|
|
122
125
|
|
|
@@ -140,6 +143,7 @@ Point.prototype.validate = function validate() {
|
|
|
140
143
|
*
|
|
141
144
|
* @param {Point} point An instance of Point.
|
|
142
145
|
* @returns {Buffer} A compressed point in the form of a buffer.
|
|
146
|
+
* @static
|
|
143
147
|
*/
|
|
144
148
|
Point.pointToCompressed = function pointToCompressed(point) {
|
|
145
149
|
var xbuf = point.getX().toBuffer({ size: 32 });
|
|
@@ -160,6 +164,7 @@ Point.pointToCompressed = function pointToCompressed(point) {
|
|
|
160
164
|
*
|
|
161
165
|
* @param {Buffer} buf A compressed point.
|
|
162
166
|
* @returns {Point} A Point.
|
|
167
|
+
* @static
|
|
163
168
|
*/
|
|
164
169
|
Point.pointFromCompressed = function (buf) {
|
|
165
170
|
if (buf.length !== 33) {
|
|
@@ -203,6 +208,7 @@ Point.prototype.toHex = function () {
|
|
|
203
208
|
*
|
|
204
209
|
* @param {Buffer} buf A compressed point.
|
|
205
210
|
* @returns {Point} A Point.
|
|
211
|
+
* @static
|
|
206
212
|
*/
|
|
207
213
|
Point.fromBuffer = function (buf) {
|
|
208
214
|
return Point.pointFromCompressed(buf);
|
|
@@ -213,6 +219,7 @@ Point.fromBuffer = function (buf) {
|
|
|
213
219
|
*
|
|
214
220
|
* @param {Buffer} hex A compressed point as a hex string.
|
|
215
221
|
* @returns {Point} A Point.
|
|
222
|
+
* @static
|
|
216
223
|
*/
|
|
217
224
|
Point.fromHex = function (hex) {
|
|
218
225
|
return Point.fromBuffer(Buffer.from(hex, 'hex'));
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
var crypto = require('crypto');
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* A utility class for generating random values.
|
|
6
|
+
*/
|
|
7
|
+
function Random() { }
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* Generates a cryptographically secure random buffer of the specified size.
|
|
11
|
+
* @param {number} size - The number of bytes to generate.
|
|
12
|
+
* @returns {Buffer} A buffer filled with cryptographically secure random bytes.
|
|
13
|
+
*/
|
|
14
|
+
Random.getRandomBuffer = function (size) {
|
|
15
|
+
return crypto.randomBytes(size);
|
|
16
|
+
};
|
|
17
|
+
|
|
18
|
+
module.exports = Random;
|