@exodus/bip322-js 1.1.0-exodus.5 → 1.1.0-rc.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/README.md +13 -13
- package/dist/BIP322.d.ts +2 -33
- package/dist/BIP322.js +14 -91
- package/dist/Signer.d.ts +1 -20
- package/dist/Signer.js +32 -97
- package/dist/Verifier.d.ts +0 -39
- package/dist/Verifier.js +34 -155
- package/dist/bitcoinjs/DecodeScriptSignature.d.ts +0 -1
- package/dist/bitcoinjs/DecodeScriptSignature.js +1 -7
- package/dist/bitcoinjs/index.js +2 -5
- package/dist/helpers/Address.d.ts +0 -47
- package/dist/helpers/Address.js +7 -97
- package/dist/helpers/BIP137.d.ts +0 -21
- package/dist/helpers/BIP137.js +4 -54
- package/dist/helpers/VarInt.d.ts +0 -17
- package/dist/helpers/VarInt.js +3 -28
- package/dist/helpers/VarStr.d.ts +0 -17
- package/dist/helpers/VarStr.js +5 -32
- package/dist/helpers/Witness.d.ts +0 -18
- package/dist/helpers/Witness.js +9 -46
- package/dist/helpers/index.js +6 -16
- package/dist/index.js +5 -44
- package/package.json +46 -45
package/README.md
CHANGED
|
@@ -25,25 +25,25 @@ Available at https://acken2.github.io/bip322-js/
|
|
|
25
25
|
|
|
26
26
|
```js
|
|
27
27
|
// Import modules that are useful to you
|
|
28
|
-
const { BIP322, Signer, Verifier } = require('bip322-js')
|
|
28
|
+
const { BIP322, Signer, Verifier } = require('bip322-js')
|
|
29
29
|
|
|
30
30
|
// Signing a BIP-322 signature with a private key
|
|
31
|
-
const privateKey = 'L3VFeEujGtevx9w18HD1fhRbCH67Az2dpCymeRE1SoPK6XQtaN2k'
|
|
32
|
-
const address = 'bc1q9vza2e8x573nczrlzms0wvx3gsqjx7vavgkx0l'
|
|
33
|
-
const message = 'Hello World'
|
|
34
|
-
const signature = Signer.sign(privateKey, address, message)
|
|
35
|
-
console.log(signature)
|
|
31
|
+
const privateKey = 'L3VFeEujGtevx9w18HD1fhRbCH67Az2dpCymeRE1SoPK6XQtaN2k'
|
|
32
|
+
const address = 'bc1q9vza2e8x573nczrlzms0wvx3gsqjx7vavgkx0l'
|
|
33
|
+
const message = 'Hello World'
|
|
34
|
+
const signature = Signer.sign(privateKey, address, message)
|
|
35
|
+
console.log(signature)
|
|
36
36
|
|
|
37
37
|
// Verifying a simple BIP-322 signature
|
|
38
|
-
const validity = Verifier.verifySignature(address, message, signature)
|
|
39
|
-
console.log(validity)
|
|
38
|
+
const validity = Verifier.verifySignature(address, message, signature)
|
|
39
|
+
console.log(validity) // True
|
|
40
40
|
|
|
41
41
|
// You can also get the raw unsigned BIP-322 toSpend and toSign transaction directly
|
|
42
|
-
const scriptPubKey = Buffer.from('00142b05d564e6a7a33c087f16e0f730d1440123799d', 'hex')
|
|
43
|
-
const toSpend = BIP322.buildToSpendTx(message, scriptPubKey)
|
|
44
|
-
const toSpendTxId = toSpend.getId()
|
|
45
|
-
const toSign = BIP322.buildToSignTx(toSpendTxId, scriptPubKey)
|
|
42
|
+
const scriptPubKey = Buffer.from('00142b05d564e6a7a33c087f16e0f730d1440123799d', 'hex')
|
|
43
|
+
const toSpend = BIP322.buildToSpendTx(message, scriptPubKey) // bitcoin.Transaction
|
|
44
|
+
const toSpendTxId = toSpend.getId()
|
|
45
|
+
const toSign = BIP322.buildToSignTx(toSpendTxId, scriptPubKey) // bitcoin.Psbt
|
|
46
46
|
// Do whatever you want to do with the PSBT
|
|
47
47
|
```
|
|
48
48
|
|
|
49
|
-
More working examples can be found within the unit test for BIP322, Signer, and Verifier.
|
|
49
|
+
More working examples can be found within the unit test for BIP322, Signer, and Verifier.
|
package/dist/BIP322.d.ts
CHANGED
|
@@ -1,40 +1,9 @@
|
|
|
1
|
-
|
|
2
|
-
import * as bitcoin from '@exodus/bitcoinjs-lib';
|
|
3
|
-
/**
|
|
4
|
-
* Class that handles BIP-322 related operations.
|
|
5
|
-
* Reference: https://github.com/LegReq/bip0322-signatures/blob/master/BIP0322_signing.ipynb
|
|
6
|
-
*/
|
|
1
|
+
import * as bitcoin from '@exodus/bitcoinjs';
|
|
7
2
|
declare class BIP322 {
|
|
8
3
|
static TAG: Buffer;
|
|
9
|
-
|
|
10
|
-
* Compute the message hash as specified in the BIP-322.
|
|
11
|
-
* The standard is specified in BIP-340 as:
|
|
12
|
-
* The function hashtag(x) where tag is a UTF-8 encoded tag name and x is a byte array returns the 32-byte hash SHA256(SHA256(tag) || SHA256(tag) || x).
|
|
13
|
-
* @param message Message to be hashed
|
|
14
|
-
* @returns Hashed message
|
|
15
|
-
*/
|
|
16
|
-
static hashMessage(message: string): any;
|
|
17
|
-
/**
|
|
18
|
-
* Build a to_spend transaction using simple signature in accordance to the BIP-322.
|
|
19
|
-
* @param message Message to be signed using BIP-322
|
|
20
|
-
* @param scriptPublicKey The script public key for the signing wallet
|
|
21
|
-
* @returns Bitcoin transaction that correspond to the to_spend transaction
|
|
22
|
-
*/
|
|
4
|
+
static hashMessage(message: string): Buffer;
|
|
23
5
|
static buildToSpendTx(message: string, scriptPublicKey: Buffer): bitcoin.Transaction;
|
|
24
|
-
/**
|
|
25
|
-
* Build a to_sign transaction using simple signature in accordance to the BIP-322.
|
|
26
|
-
* @param toSpendTxId Transaction ID of the to_spend transaction as constructed by buildToSpendTx
|
|
27
|
-
* @param witnessScript The script public key for the signing wallet, or the redeemScript for P2SH-P2WPKH address
|
|
28
|
-
* @param isRedeemScript Set to true if the provided witnessScript is a redeemScript for P2SH-P2WPKH address, default to false
|
|
29
|
-
* @param tapInternalKey Used to set the taproot internal public key of a taproot signing address when provided, default to undefined
|
|
30
|
-
* @returns Ready-to-be-signed bitcoinjs.Psbt transaction
|
|
31
|
-
*/
|
|
32
6
|
static buildToSignTx(toSpendTxId: string, witnessScript: Buffer, isRedeemScript?: boolean, tapInternalKey?: Buffer): bitcoin.Psbt;
|
|
33
|
-
/**
|
|
34
|
-
* Encode witness stack in a signed BIP-322 PSBT into its base-64 encoded format.
|
|
35
|
-
* @param signedPsbt Signed PSBT
|
|
36
|
-
* @returns Base-64 encoded witness data
|
|
37
|
-
*/
|
|
38
7
|
static encodeWitness(signedPsbt: bitcoin.Psbt): string;
|
|
39
8
|
}
|
|
40
9
|
export default BIP322;
|
package/dist/BIP322.js
CHANGED
|
@@ -1,108 +1,44 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
if (k2 === undefined) k2 = k;
|
|
4
|
-
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
-
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
-
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
-
}
|
|
8
|
-
Object.defineProperty(o, k2, desc);
|
|
9
|
-
}) : (function(o, m, k, k2) {
|
|
10
|
-
if (k2 === undefined) k2 = k;
|
|
11
|
-
o[k2] = m[k];
|
|
12
|
-
}));
|
|
13
|
-
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
-
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
-
}) : function(o, v) {
|
|
16
|
-
o["default"] = v;
|
|
17
|
-
});
|
|
18
|
-
var __importStar = (this && this.__importStar) || function (mod) {
|
|
19
|
-
if (mod && mod.__esModule) return mod;
|
|
20
|
-
var result = {};
|
|
21
|
-
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
|
22
|
-
__setModuleDefault(result, mod);
|
|
23
|
-
return result;
|
|
24
|
-
};
|
|
25
|
-
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
26
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
27
|
-
};
|
|
28
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
29
|
-
// Import dependencies
|
|
30
|
-
const create_hash_1 = __importDefault(require("create-hash"));
|
|
31
|
-
const bitcoin = __importStar(require("@exodus/bitcoinjs-lib"));
|
|
32
|
-
/**
|
|
33
|
-
* Class that handles BIP-322 related operations.
|
|
34
|
-
* Reference: https://github.com/LegReq/bip0322-signatures/blob/master/BIP0322_signing.ipynb
|
|
35
|
-
*/
|
|
1
|
+
import createHash from "create-hash";
|
|
2
|
+
import * as bitcoin from '@exodus/bitcoinjs';
|
|
36
3
|
class BIP322 {
|
|
37
|
-
|
|
38
|
-
* Compute the message hash as specified in the BIP-322.
|
|
39
|
-
* The standard is specified in BIP-340 as:
|
|
40
|
-
* The function hashtag(x) where tag is a UTF-8 encoded tag name and x is a byte array returns the 32-byte hash SHA256(SHA256(tag) || SHA256(tag) || x).
|
|
41
|
-
* @param message Message to be hashed
|
|
42
|
-
* @returns Hashed message
|
|
43
|
-
*/
|
|
4
|
+
static TAG = Buffer.from("BIP0322-signed-message");
|
|
44
5
|
static hashMessage(message) {
|
|
45
|
-
|
|
46
|
-
const tagHasher = (0, create_hash_1.default)('sha256');
|
|
6
|
+
const tagHasher = createHash('sha256');
|
|
47
7
|
tagHasher.update(this.TAG);
|
|
48
8
|
const tagHash = tagHasher.digest();
|
|
49
|
-
const messageHasher = (
|
|
9
|
+
const messageHasher = createHash('sha256');
|
|
50
10
|
messageHasher.update(tagHash);
|
|
51
11
|
messageHasher.update(tagHash);
|
|
52
12
|
messageHasher.update(Buffer.from(message));
|
|
53
13
|
const messageHash = messageHasher.digest();
|
|
54
14
|
return messageHash;
|
|
55
15
|
}
|
|
56
|
-
/**
|
|
57
|
-
* Build a to_spend transaction using simple signature in accordance to the BIP-322.
|
|
58
|
-
* @param message Message to be signed using BIP-322
|
|
59
|
-
* @param scriptPublicKey The script public key for the signing wallet
|
|
60
|
-
* @returns Bitcoin transaction that correspond to the to_spend transaction
|
|
61
|
-
*/
|
|
62
16
|
static buildToSpendTx(message, scriptPublicKey) {
|
|
63
|
-
// Create PSBT object for constructing the transaction
|
|
64
17
|
const psbt = new bitcoin.Psbt();
|
|
65
|
-
|
|
66
|
-
psbt.
|
|
67
|
-
psbt.setLocktime(0); // nLockTime = 0
|
|
68
|
-
// Compute the message hash - SHA256(SHA256(tag) || SHA256(tag) || message)
|
|
18
|
+
psbt.setVersion(0);
|
|
19
|
+
psbt.setLocktime(0);
|
|
69
20
|
const messageHash = this.hashMessage(message);
|
|
70
|
-
|
|
71
|
-
const scriptSigPartOne = new Uint8Array([0x00, 0x20]); // OP_0 PUSH32
|
|
21
|
+
const scriptSigPartOne = new Uint8Array([0x00, 0x20]);
|
|
72
22
|
const scriptSig = new Uint8Array(scriptSigPartOne.length + messageHash.length);
|
|
73
23
|
scriptSig.set(scriptSigPartOne);
|
|
74
24
|
scriptSig.set(messageHash, scriptSigPartOne.length);
|
|
75
|
-
// Set the input
|
|
76
25
|
psbt.addInput({
|
|
77
26
|
hash: '0'.repeat(64),
|
|
78
27
|
index: 0xFFFFFFFF,
|
|
79
28
|
sequence: 0,
|
|
80
29
|
finalScriptSig: Buffer.from(scriptSig),
|
|
81
|
-
witnessScript: Buffer.from([])
|
|
30
|
+
witnessScript: Buffer.from([])
|
|
82
31
|
});
|
|
83
|
-
// Set the output
|
|
84
32
|
psbt.addOutput({
|
|
85
33
|
value: 0,
|
|
86
|
-
script: scriptPublicKey
|
|
34
|
+
script: scriptPublicKey
|
|
87
35
|
});
|
|
88
|
-
// Return transaction
|
|
89
36
|
return psbt.extractTransaction();
|
|
90
37
|
}
|
|
91
|
-
/**
|
|
92
|
-
* Build a to_sign transaction using simple signature in accordance to the BIP-322.
|
|
93
|
-
* @param toSpendTxId Transaction ID of the to_spend transaction as constructed by buildToSpendTx
|
|
94
|
-
* @param witnessScript The script public key for the signing wallet, or the redeemScript for P2SH-P2WPKH address
|
|
95
|
-
* @param isRedeemScript Set to true if the provided witnessScript is a redeemScript for P2SH-P2WPKH address, default to false
|
|
96
|
-
* @param tapInternalKey Used to set the taproot internal public key of a taproot signing address when provided, default to undefined
|
|
97
|
-
* @returns Ready-to-be-signed bitcoinjs.Psbt transaction
|
|
98
|
-
*/
|
|
99
38
|
static buildToSignTx(toSpendTxId, witnessScript, isRedeemScript = false, tapInternalKey = undefined) {
|
|
100
|
-
// Create PSBT object for constructing the transaction
|
|
101
39
|
const psbt = new bitcoin.Psbt();
|
|
102
|
-
|
|
103
|
-
psbt.
|
|
104
|
-
psbt.setLocktime(0); // nLockTime = 0
|
|
105
|
-
// Set the input
|
|
40
|
+
psbt.setVersion(0);
|
|
41
|
+
psbt.setLocktime(0);
|
|
106
42
|
psbt.addInput({
|
|
107
43
|
hash: toSpendTxId,
|
|
108
44
|
index: 0,
|
|
@@ -112,36 +48,25 @@ class BIP322 {
|
|
|
112
48
|
value: 0
|
|
113
49
|
}
|
|
114
50
|
});
|
|
115
|
-
// Set redeemScript as witnessScript if isRedeemScript
|
|
116
51
|
if (isRedeemScript) {
|
|
117
52
|
psbt.updateInput(0, {
|
|
118
53
|
redeemScript: witnessScript
|
|
119
54
|
});
|
|
120
55
|
}
|
|
121
|
-
// Set tapInternalKey if provided
|
|
122
56
|
if (tapInternalKey) {
|
|
123
57
|
psbt.updateInput(0, {
|
|
124
58
|
tapInternalKey: tapInternalKey
|
|
125
59
|
});
|
|
126
60
|
}
|
|
127
|
-
// Set the output
|
|
128
61
|
psbt.addOutput({
|
|
129
62
|
value: 0,
|
|
130
|
-
script: Buffer.from([0x6a])
|
|
63
|
+
script: Buffer.from([0x6a])
|
|
131
64
|
});
|
|
132
65
|
return psbt;
|
|
133
66
|
}
|
|
134
|
-
/**
|
|
135
|
-
* Encode witness stack in a signed BIP-322 PSBT into its base-64 encoded format.
|
|
136
|
-
* @param signedPsbt Signed PSBT
|
|
137
|
-
* @returns Base-64 encoded witness data
|
|
138
|
-
*/
|
|
139
67
|
static encodeWitness(signedPsbt) {
|
|
140
|
-
// Obtain the signed witness data
|
|
141
68
|
const witness = signedPsbt.data.inputs[0].finalScriptWitness;
|
|
142
|
-
// Check if the witness data is present
|
|
143
69
|
if (witness) {
|
|
144
|
-
// Return the base-64 encoded witness stack
|
|
145
70
|
return witness.toString('base64');
|
|
146
71
|
}
|
|
147
72
|
else {
|
|
@@ -149,7 +74,5 @@ class BIP322 {
|
|
|
149
74
|
}
|
|
150
75
|
}
|
|
151
76
|
}
|
|
152
|
-
|
|
153
|
-
BIP322.TAG = Buffer.from("BIP0322-signed-message");
|
|
154
|
-
exports.default = BIP322;
|
|
77
|
+
export default BIP322;
|
|
155
78
|
//# sourceMappingURL=BIP322.js.map
|
package/dist/Signer.d.ts
CHANGED
|
@@ -1,25 +1,6 @@
|
|
|
1
|
-
|
|
2
|
-
import * as bitcoin from '@exodus/bitcoinjs-lib';
|
|
3
|
-
/**
|
|
4
|
-
* Class that signs BIP-322 signature using a private key.
|
|
5
|
-
* Reference: https://github.com/LegReq/bip0322-signatures/blob/master/BIP0322_signing.ipynb
|
|
6
|
-
*/
|
|
1
|
+
import * as bitcoin from '@exodus/bitcoinjs';
|
|
7
2
|
declare class Signer {
|
|
8
|
-
/**
|
|
9
|
-
* Sign a BIP-322 signature from P2WPKH, P2SH-P2WPKH, and single-key-spend P2TR address and its corresponding private key.
|
|
10
|
-
* @param privateKeyOrWIF
|
|
11
|
-
* @param address Address to be signing the message
|
|
12
|
-
* @param message message_challenge to be signed by the address
|
|
13
|
-
* @param network Network that the address is located, defaults to the Bitcoin mainnet
|
|
14
|
-
* @returns BIP-322 simple signature, encoded in base-64
|
|
15
|
-
*/
|
|
16
3
|
static sign(privateKeyOrWIF: string | Buffer, address: string, message: string, network?: bitcoin.Network): string | Buffer;
|
|
17
|
-
/**
|
|
18
|
-
* Check if a given public key is the public key for a claimed address.
|
|
19
|
-
* @param publicKey Public key to be tested
|
|
20
|
-
* @param claimedAddress Address claimed to be derived based on the provided public key
|
|
21
|
-
* @returns True if the claimedAddress can be derived by the provided publicKey, false if otherwise
|
|
22
|
-
*/
|
|
23
4
|
private static checkPubKeyCorrespondToAddress;
|
|
24
5
|
}
|
|
25
6
|
export default Signer;
|
package/dist/Signer.js
CHANGED
|
@@ -1,126 +1,61 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
-
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
-
}
|
|
8
|
-
Object.defineProperty(o, k2, desc);
|
|
9
|
-
}) : (function(o, m, k, k2) {
|
|
10
|
-
if (k2 === undefined) k2 = k;
|
|
11
|
-
o[k2] = m[k];
|
|
12
|
-
}));
|
|
13
|
-
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
-
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
-
}) : function(o, v) {
|
|
16
|
-
o["default"] = v;
|
|
17
|
-
});
|
|
18
|
-
var __importStar = (this && this.__importStar) || function (mod) {
|
|
19
|
-
if (mod && mod.__esModule) return mod;
|
|
20
|
-
var result = {};
|
|
21
|
-
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
|
22
|
-
__setModuleDefault(result, mod);
|
|
23
|
-
return result;
|
|
24
|
-
};
|
|
25
|
-
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
26
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
27
|
-
};
|
|
28
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
29
|
-
// Import dependencies
|
|
30
|
-
const BIP322_1 = __importDefault(require("./BIP322"));
|
|
31
|
-
const ecpair_1 = __importDefault(require("ecpair"));
|
|
32
|
-
const helpers_1 = require("./helpers");
|
|
33
|
-
const bitcoin = __importStar(require("@exodus/bitcoinjs-lib"));
|
|
34
|
-
const bitcoinerlab_secp256k1_1 = __importDefault(require("@exodus/bitcoinerlab-secp256k1"));
|
|
35
|
-
const bitcoinMessage = __importStar(require("bitcoinjs-message"));
|
|
36
|
-
/**
|
|
37
|
-
* Class that signs BIP-322 signature using a private key.
|
|
38
|
-
* Reference: https://github.com/LegReq/bip0322-signatures/blob/master/BIP0322_signing.ipynb
|
|
39
|
-
*/
|
|
1
|
+
import BIP322 from './BIP322';
|
|
2
|
+
import { Address } from './helpers';
|
|
3
|
+
import * as bitcoin from '@exodus/bitcoinjs';
|
|
4
|
+
import * as bitcoinMessage from 'bitcoinjs-message';
|
|
40
5
|
class Signer {
|
|
41
|
-
/**
|
|
42
|
-
* Sign a BIP-322 signature from P2WPKH, P2SH-P2WPKH, and single-key-spend P2TR address and its corresponding private key.
|
|
43
|
-
* @param privateKeyOrWIF
|
|
44
|
-
* @param address Address to be signing the message
|
|
45
|
-
* @param message message_challenge to be signed by the address
|
|
46
|
-
* @param network Network that the address is located, defaults to the Bitcoin mainnet
|
|
47
|
-
* @returns BIP-322 simple signature, encoded in base-64
|
|
48
|
-
*/
|
|
49
6
|
static sign(privateKeyOrWIF, address, message, network = bitcoin.networks.bitcoin) {
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
7
|
+
const ECPair = bitcoin.ECPair;
|
|
8
|
+
let signer = Buffer.isBuffer(privateKeyOrWIF)
|
|
9
|
+
? ECPair.fromPrivateKey(privateKeyOrWIF)
|
|
10
|
+
: ECPair.fromWIF(privateKeyOrWIF, network);
|
|
54
11
|
if (!this.checkPubKeyCorrespondToAddress(signer.publicKey, address)) {
|
|
55
12
|
throw new Error(`Invalid private key provided for signing message for ${address}.`);
|
|
56
13
|
}
|
|
57
|
-
|
|
58
|
-
if (helpers_1.Address.isP2PKH(address)) {
|
|
59
|
-
// For P2PKH address, sign a legacy signature
|
|
60
|
-
// Reference: https://github.com/bitcoinjs/bitcoinjs-message/blob/c43430f4c03c292c719e7801e425d887cbdf7464/README.md?plain=1#L21
|
|
14
|
+
if (Address.isP2PKH(address)) {
|
|
61
15
|
return bitcoinMessage.sign(message, signer.privateKey, signer.compressed);
|
|
62
16
|
}
|
|
63
|
-
|
|
64
|
-
const
|
|
65
|
-
// Draft corresponding toSpend using the message and script pubkey
|
|
66
|
-
const toSpendTx = BIP322_1.default.buildToSpendTx(message, scriptPubKey);
|
|
67
|
-
// Draft corresponding toSign transaction based on the address type
|
|
17
|
+
const scriptPubKey = Address.convertAdressToScriptPubkey(address);
|
|
18
|
+
const toSpendTx = BIP322.buildToSpendTx(message, scriptPubKey);
|
|
68
19
|
let toSignTx;
|
|
69
|
-
if (
|
|
70
|
-
// P2SH-P2WPKH signing path
|
|
71
|
-
// Derive the P2SH-P2WPKH redeemScript from the corresponding hashed public key
|
|
20
|
+
if (Address.isP2SH(address)) {
|
|
72
21
|
const redeemScript = bitcoin.payments.p2wpkh({
|
|
73
22
|
hash: bitcoin.crypto.hash160(signer.publicKey),
|
|
74
|
-
network
|
|
23
|
+
network,
|
|
75
24
|
}).output;
|
|
76
|
-
toSignTx =
|
|
25
|
+
toSignTx = BIP322.buildToSignTx(toSpendTx.getId(), redeemScript, true);
|
|
77
26
|
}
|
|
78
|
-
else if (
|
|
79
|
-
|
|
80
|
-
toSignTx = BIP322_1.default.buildToSignTx(toSpendTx.getId(), scriptPubKey);
|
|
27
|
+
else if (Address.isP2WPKH(address)) {
|
|
28
|
+
toSignTx = BIP322.buildToSignTx(toSpendTx.getId(), scriptPubKey);
|
|
81
29
|
}
|
|
82
30
|
else {
|
|
83
|
-
// P2TR signing path
|
|
84
|
-
// Extract the taproot internal public key
|
|
85
31
|
const internalPublicKey = signer.publicKey.subarray(1, 33);
|
|
86
|
-
// Tweak the private key for signing, since the output and address uses tweaked key
|
|
87
|
-
// Reference: https://github.com/bitcoinjs/bitcoinjs-lib/blob/1a9119b53bcea4b83a6aa8b948f0e6370209b1b4/test/integration/taproot.spec.ts#L55
|
|
88
32
|
signer = signer.tweak(bitcoin.crypto.taggedHash('TapTweak', signer.publicKey.subarray(1, 33)));
|
|
89
|
-
|
|
90
|
-
toSignTx = BIP322_1.default.buildToSignTx(toSpendTx.getId(), scriptPubKey, false, internalPublicKey);
|
|
33
|
+
toSignTx = BIP322.buildToSignTx(toSpendTx.getId(), scriptPubKey, false, internalPublicKey);
|
|
91
34
|
}
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
return
|
|
35
|
+
const toSignTxSigned = toSignTx
|
|
36
|
+
.signAllInputs(signer, [bitcoin.Transaction.SIGHASH_ALL, bitcoin.Transaction.SIGHASH_DEFAULT])
|
|
37
|
+
.finalizeAllInputs();
|
|
38
|
+
return BIP322.encodeWitness(toSignTxSigned);
|
|
96
39
|
}
|
|
97
|
-
/**
|
|
98
|
-
* Check if a given public key is the public key for a claimed address.
|
|
99
|
-
* @param publicKey Public key to be tested
|
|
100
|
-
* @param claimedAddress Address claimed to be derived based on the provided public key
|
|
101
|
-
* @returns True if the claimedAddress can be derived by the provided publicKey, false if otherwise
|
|
102
|
-
*/
|
|
103
40
|
static checkPubKeyCorrespondToAddress(publicKey, claimedAddress) {
|
|
104
|
-
// Derive the same address type from the provided public key
|
|
105
41
|
let derivedAddresses;
|
|
106
|
-
if (
|
|
107
|
-
derivedAddresses =
|
|
42
|
+
if (Address.isP2PKH(claimedAddress)) {
|
|
43
|
+
derivedAddresses = Address.convertPubKeyIntoAddress(publicKey, 'p2pkh');
|
|
108
44
|
}
|
|
109
|
-
else if (
|
|
110
|
-
derivedAddresses =
|
|
45
|
+
else if (Address.isP2SH(claimedAddress)) {
|
|
46
|
+
derivedAddresses = Address.convertPubKeyIntoAddress(publicKey, 'p2sh-p2wpkh');
|
|
111
47
|
}
|
|
112
|
-
else if (
|
|
113
|
-
derivedAddresses =
|
|
48
|
+
else if (Address.isP2WPKH(claimedAddress)) {
|
|
49
|
+
derivedAddresses = Address.convertPubKeyIntoAddress(publicKey, 'p2wpkh');
|
|
114
50
|
}
|
|
115
|
-
else if (
|
|
116
|
-
derivedAddresses =
|
|
51
|
+
else if (Address.isP2TR(claimedAddress)) {
|
|
52
|
+
derivedAddresses = Address.convertPubKeyIntoAddress(publicKey, 'p2tr');
|
|
117
53
|
}
|
|
118
54
|
else {
|
|
119
|
-
throw new Error('Unable to sign BIP-322 message for unsupported address type.');
|
|
55
|
+
throw new Error('Unable to sign BIP-322 message for unsupported address type.');
|
|
120
56
|
}
|
|
121
|
-
|
|
122
|
-
return (derivedAddresses.mainnet === claimedAddress) || (derivedAddresses.testnet === claimedAddress);
|
|
57
|
+
return (derivedAddresses.mainnet === claimedAddress || derivedAddresses.testnet === claimedAddress);
|
|
123
58
|
}
|
|
124
59
|
}
|
|
125
|
-
|
|
60
|
+
export default Signer;
|
|
126
61
|
//# sourceMappingURL=Signer.js.map
|
package/dist/Verifier.d.ts
CHANGED
|
@@ -1,47 +1,8 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Class that handles BIP-322 signature verification.
|
|
3
|
-
* Reference: https://github.com/LegReq/bip0322-signatures/blob/master/BIP0322_verification.ipynb
|
|
4
|
-
*/
|
|
5
1
|
declare class Verifier {
|
|
6
|
-
/**
|
|
7
|
-
* Verify a BIP-322 signature from P2WPKH, P2SH-P2WPKH, and single-key-spend P2TR address.
|
|
8
|
-
* @param signerAddress Address of the signing address
|
|
9
|
-
* @param message message_challenge signed by the address
|
|
10
|
-
* @param signatureBase64 Signature produced by the signing address
|
|
11
|
-
* @returns True if the provided signature is a valid BIP-322 signature for the given message and address, false if otherwise
|
|
12
|
-
* @throws If the provided signature fails basic validation, or if unsupported address and signature are provided
|
|
13
|
-
*/
|
|
14
2
|
static verifySignature(signerAddress: string, message: string, signatureBase64: string): boolean;
|
|
15
|
-
/**
|
|
16
|
-
* Verify a legacy BIP-137 signature.
|
|
17
|
-
* Note that a signature is considered valid for all types of addresses that can be derived from the recovered public key.
|
|
18
|
-
* @param signerAddress Address of the signing address
|
|
19
|
-
* @param message message_challenge signed by the address
|
|
20
|
-
* @param signatureBase64 Signature produced by the signing address
|
|
21
|
-
* @returns True if the provided signature is a valid BIP-137 signature for the given message and address, false if otherwise
|
|
22
|
-
* @throws If the provided signature fails basic validation, or if unsupported address and signature are provided
|
|
23
|
-
*/
|
|
24
3
|
private static verifyBIP137Signature;
|
|
25
|
-
/**
|
|
26
|
-
* Compute the hash to be signed for a given P2WPKH BIP-322 toSign transaction.
|
|
27
|
-
* @param toSignTx PSBT instance of the toSign transaction
|
|
28
|
-
* @returns Computed transaction hash that requires signing
|
|
29
|
-
*/
|
|
30
4
|
private static getHashForSigP2WPKH;
|
|
31
|
-
/**
|
|
32
|
-
* Compute the hash to be signed for a given P2SH-P2WPKH BIP-322 toSign transaction.
|
|
33
|
-
* @param toSignTx PSBT instance of the toSign transaction
|
|
34
|
-
* @param hashedPubkey Hashed public key of the signing address
|
|
35
|
-
* @returns Computed transaction hash that requires signing
|
|
36
|
-
*/
|
|
37
5
|
private static getHashForSigP2SHInP2WPKH;
|
|
38
|
-
/**
|
|
39
|
-
* Compute the hash to be signed for a given P2TR BIP-322 toSign transaction.
|
|
40
|
-
* @param toSignTx PSBT instance of the toSign transaction
|
|
41
|
-
* @param hashType Hash type used to sign the toSign transaction, must be either 0x00 or 0x01
|
|
42
|
-
* @returns Computed transaction hash that requires signing
|
|
43
|
-
* @throws Error if hashType is anything other than 0x00 or 0x01
|
|
44
|
-
*/
|
|
45
6
|
private static getHashForSigP2TR;
|
|
46
7
|
}
|
|
47
8
|
export default Verifier;
|