@exodus/bip322-js 2.0.2 → 2.2.0
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/CHANGELOG.md +12 -0
- package/dist/Signer.d.ts +2 -0
- package/dist/Signer.js +61 -1
- package/package.json +16 -10
package/CHANGELOG.md
CHANGED
|
@@ -3,6 +3,18 @@
|
|
|
3
3
|
All notable changes to this project will be documented in this file.
|
|
4
4
|
See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.
|
|
5
5
|
|
|
6
|
+
## [2.2.0](https://github.com/ExodusMovement/exodus-hydra/compare/@exodus/bip322-js@2.1.0...@exodus/bip322-js@2.2.0) (2025-09-09)
|
|
7
|
+
|
|
8
|
+
### Features
|
|
9
|
+
|
|
10
|
+
- feat: do not use ECPair from bitcoinjs (#13791)
|
|
11
|
+
|
|
12
|
+
## [2.1.0](https://github.com/ExodusMovement/exodus-hydra/compare/@exodus/bip322-js@2.0.2...@exodus/bip322-js@2.1.0) (2025-04-01)
|
|
13
|
+
|
|
14
|
+
### Features
|
|
15
|
+
|
|
16
|
+
- feat: support async signing with external signer (#11913)
|
|
17
|
+
|
|
6
18
|
## [2.0.2](https://github.com/ExodusMovement/exodus-hydra/compare/@exodus/bip322-js@2.0.1...@exodus/bip322-js@2.0.2) (2025-01-07)
|
|
7
19
|
|
|
8
20
|
**Note:** Version bump only for package @exodus/bip322-js
|
package/dist/Signer.d.ts
CHANGED
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
import * as bitcoin from '@exodus/bitcoinjs';
|
|
2
|
+
import type { Signer as AssetSigner } from '@exodus/asset-types/src/signer';
|
|
2
3
|
declare class Signer {
|
|
3
4
|
static sign(privateKeyOrWIF: string | Buffer, address: string, message: string, network?: bitcoin.Network): any;
|
|
5
|
+
static signAsync(signer: string | Buffer | AssetSigner, address: string, message: string, network?: bitcoin.Network): Promise<any>;
|
|
4
6
|
private static checkPubKeyCorrespondToAddress;
|
|
5
7
|
}
|
|
6
8
|
export default Signer;
|
package/dist/Signer.js
CHANGED
|
@@ -2,9 +2,24 @@ import Address from './Address.js';
|
|
|
2
2
|
import BIP322 from './BIP322.js';
|
|
3
3
|
import * as bitcoin from '@exodus/bitcoinjs';
|
|
4
4
|
import * as bitcoinMessage from '@exodus/bitcoinjs/message';
|
|
5
|
+
import { tiny_secp256k1_compat as ecc } from '@exodus/crypto/secp256k1';
|
|
6
|
+
import { ECPairFactory } from 'ecpair';
|
|
7
|
+
import assert from 'minimalistic-assert';
|
|
8
|
+
const ECPair = ECPairFactory(ecc);
|
|
9
|
+
const createAsyncSigner = ({ signer, publicKey, tweak }) => {
|
|
10
|
+
return {
|
|
11
|
+
publicKey,
|
|
12
|
+
getPublicKey: () => publicKey,
|
|
13
|
+
sign: async (data) => signer.sign({ signatureType: 'ecdsa', data, enc: 'sig' }),
|
|
14
|
+
signSchnorr: async (hash) => signer.sign({
|
|
15
|
+
signatureType: 'schnorr',
|
|
16
|
+
data: hash,
|
|
17
|
+
tweak,
|
|
18
|
+
}),
|
|
19
|
+
};
|
|
20
|
+
};
|
|
5
21
|
class Signer {
|
|
6
22
|
static sign(privateKeyOrWIF, address, message, network = bitcoin.networks.bitcoin) {
|
|
7
|
-
const ECPair = bitcoin.ECPair;
|
|
8
23
|
let signer = Buffer.isBuffer(privateKeyOrWIF)
|
|
9
24
|
? ECPair.fromPrivateKey(privateKeyOrWIF)
|
|
10
25
|
: ECPair.fromWIF(privateKeyOrWIF, network);
|
|
@@ -37,6 +52,51 @@ class Signer {
|
|
|
37
52
|
.finalizeAllInputs();
|
|
38
53
|
return BIP322.encodeWitness(toSignTxSigned);
|
|
39
54
|
}
|
|
55
|
+
static async signAsync(signer, address, message, network = bitcoin.networks.bitcoin) {
|
|
56
|
+
if (typeof signer === 'string' || Buffer.isBuffer(signer)) {
|
|
57
|
+
return this.sign(signer, address, message, network);
|
|
58
|
+
}
|
|
59
|
+
const publicKey = await signer.getPublicKey();
|
|
60
|
+
assert(this.checkPubKeyCorrespondToAddress(publicKey, address), `Invalid signer for address "${address}".`);
|
|
61
|
+
if (Address.isP2PKH(address)) {
|
|
62
|
+
const asyncSigner = {
|
|
63
|
+
sign(hash, extraEntropy) {
|
|
64
|
+
return signer.sign({
|
|
65
|
+
signatureType: 'ecdsa',
|
|
66
|
+
data: hash,
|
|
67
|
+
extraEntropy,
|
|
68
|
+
enc: 'sig,rec',
|
|
69
|
+
});
|
|
70
|
+
},
|
|
71
|
+
};
|
|
72
|
+
return bitcoinMessage.signAsync(message, asyncSigner, true);
|
|
73
|
+
}
|
|
74
|
+
const scriptPubKey = Address.convertAdressToScriptPubkey(address);
|
|
75
|
+
const toSpendTx = BIP322.buildToSpendTx(message, scriptPubKey);
|
|
76
|
+
let toSignTx;
|
|
77
|
+
let tweak;
|
|
78
|
+
if (Address.isP2SH(address)) {
|
|
79
|
+
const redeemScript = bitcoin.payments.p2wpkh({
|
|
80
|
+
hash: bitcoin.crypto.hash160(publicKey),
|
|
81
|
+
network,
|
|
82
|
+
}).output;
|
|
83
|
+
toSignTx = BIP322.buildToSignTx(toSpendTx.getId(), redeemScript, true);
|
|
84
|
+
}
|
|
85
|
+
else if (Address.isP2WPKH(address)) {
|
|
86
|
+
toSignTx = BIP322.buildToSignTx(toSpendTx.getId(), scriptPubKey);
|
|
87
|
+
}
|
|
88
|
+
else {
|
|
89
|
+
const internalPublicKey = publicKey.subarray(1, 33);
|
|
90
|
+
tweak = bitcoin.crypto.taggedHash('TapTweak', publicKey.subarray(1, 33));
|
|
91
|
+
toSignTx = BIP322.buildToSignTx(toSpendTx.getId(), scriptPubKey, false, internalPublicKey);
|
|
92
|
+
}
|
|
93
|
+
const asyncSigner = createAsyncSigner({ signer, publicKey, tweak });
|
|
94
|
+
await toSignTx.signAllInputsAsync(asyncSigner, [
|
|
95
|
+
bitcoin.Transaction.SIGHASH_ALL,
|
|
96
|
+
bitcoin.Transaction.SIGHASH_DEFAULT,
|
|
97
|
+
]);
|
|
98
|
+
return BIP322.encodeWitness(toSignTx.finalizeAllInputs());
|
|
99
|
+
}
|
|
40
100
|
static checkPubKeyCorrespondToAddress(publicKey, claimedAddress) {
|
|
41
101
|
let derivedAddresses;
|
|
42
102
|
if (Address.isP2PKH(claimedAddress)) {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@exodus/bip322-js",
|
|
3
|
-
"version": "2.0
|
|
3
|
+
"version": "2.2.0",
|
|
4
4
|
"description": "A Javascript library that provides utility functions related to the BIP-322 signature scheme",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"types": "dist/index.d.ts",
|
|
@@ -10,14 +10,14 @@
|
|
|
10
10
|
},
|
|
11
11
|
"files": [
|
|
12
12
|
"dist",
|
|
13
|
-
"CHANGELOG.md"
|
|
13
|
+
"CHANGELOG.md",
|
|
14
|
+
"!**/__tests__/**"
|
|
14
15
|
],
|
|
15
16
|
"scripts": {
|
|
16
|
-
"build": "tsc
|
|
17
|
+
"build": "run -T tsc -p tsconfig.build.json",
|
|
17
18
|
"lint": "run -T eslint",
|
|
18
19
|
"lint:fix": "yarn lint --fix",
|
|
19
20
|
"doc": "typedoc src/index.ts",
|
|
20
|
-
"prepack": "npm run build",
|
|
21
21
|
"test": "run -T exodus-test --jest --esbuild",
|
|
22
22
|
"prepublishOnly": "yarn run -T build --scope @exodus/bip322-js"
|
|
23
23
|
},
|
|
@@ -35,20 +35,26 @@
|
|
|
35
35
|
},
|
|
36
36
|
"license": "MIT",
|
|
37
37
|
"devDependencies": {
|
|
38
|
-
"@
|
|
39
|
-
"@
|
|
38
|
+
"@noble/secp256k1": "^1.7.1",
|
|
39
|
+
"@types/minimalistic-assert": "^1.0.1",
|
|
40
40
|
"@types/node": "^20.2.5",
|
|
41
|
-
"
|
|
41
|
+
"bech32": "^1.1.3",
|
|
42
|
+
"bs58check": "^3.0.1",
|
|
43
|
+
"buffer-equals": "^1.0.3",
|
|
42
44
|
"typedoc": "^0.24.8",
|
|
43
|
-
"typescript": "^5.1.3"
|
|
45
|
+
"typescript": "^5.1.3",
|
|
46
|
+
"varuint-bitcoin": "^1.0.1"
|
|
44
47
|
},
|
|
45
48
|
"dependencies": {
|
|
49
|
+
"@exodus/asset-types": "^0.3.0",
|
|
46
50
|
"@exodus/bitcoinjs": "^1.4.0",
|
|
47
|
-
"@exodus/crypto": "^1.0.0-rc.14"
|
|
51
|
+
"@exodus/crypto": "^1.0.0-rc.14",
|
|
52
|
+
"ecpair": "^2.0.1",
|
|
53
|
+
"minimalistic-assert": "^1.0.1"
|
|
48
54
|
},
|
|
49
55
|
"homepage": "https://github.com/ExodusMovement/exodus-hydra/tree/master/libraries/bip322-js",
|
|
50
56
|
"bugs": {
|
|
51
57
|
"url": "https://github.com/ExodusMovement/exodus-hydra/issues?q=is%3Aissue+is%3Aopen+label%3Abip322-js"
|
|
52
58
|
},
|
|
53
|
-
"gitHead": "
|
|
59
|
+
"gitHead": "90ec3f786bb430800fae41568e71ec2b67cd9009"
|
|
54
60
|
}
|