@exodus/bip322-js 1.1.0-rc.0 → 1.1.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 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) // True
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) // bitcoin.Transaction
44
- const toSpendTxId = toSpend.getId()
45
- const toSign = BIP322.buildToSignTx(toSpendTxId, scriptPubKey) // bitcoin.Psbt
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,9 +1,40 @@
1
- import * as bitcoin from '@exodus/bitcoinjs';
1
+ /// <reference types="node" />
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
+ */
2
7
  declare class BIP322 {
3
8
  static TAG: Buffer;
4
- static hashMessage(message: string): 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
+ */
5
23
  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
+ */
6
32
  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
+ */
7
38
  static encodeWitness(signedPsbt: bitcoin.Psbt): string;
8
39
  }
9
40
  export default BIP322;
package/dist/BIP322.js CHANGED
@@ -1,44 +1,108 @@
1
- import createHash from "create-hash";
2
- import * as bitcoin from '@exodus/bitcoinjs';
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
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
+ */
3
36
  class BIP322 {
4
- static TAG = Buffer.from("BIP0322-signed-message");
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
+ */
5
44
  static hashMessage(message) {
6
- const tagHasher = createHash('sha256');
45
+ // Compute the message hash - SHA256(SHA256(tag) || SHA256(tag) || message)
46
+ const tagHasher = (0, create_hash_1.default)('sha256');
7
47
  tagHasher.update(this.TAG);
8
48
  const tagHash = tagHasher.digest();
9
- const messageHasher = createHash('sha256');
49
+ const messageHasher = (0, create_hash_1.default)('sha256');
10
50
  messageHasher.update(tagHash);
11
51
  messageHasher.update(tagHash);
12
52
  messageHasher.update(Buffer.from(message));
13
53
  const messageHash = messageHasher.digest();
14
54
  return messageHash;
15
55
  }
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
+ */
16
62
  static buildToSpendTx(message, scriptPublicKey) {
63
+ // Create PSBT object for constructing the transaction
17
64
  const psbt = new bitcoin.Psbt();
18
- psbt.setVersion(0);
19
- psbt.setLocktime(0);
65
+ // Set default value for nVersion and nLockTime
66
+ psbt.setVersion(0); // nVersion = 0
67
+ psbt.setLocktime(0); // nLockTime = 0
68
+ // Compute the message hash - SHA256(SHA256(tag) || SHA256(tag) || message)
20
69
  const messageHash = this.hashMessage(message);
21
- const scriptSigPartOne = new Uint8Array([0x00, 0x20]);
70
+ // Construct the scriptSig - OP_0 PUSH32[ message_hash ]
71
+ const scriptSigPartOne = new Uint8Array([0x00, 0x20]); // OP_0 PUSH32
22
72
  const scriptSig = new Uint8Array(scriptSigPartOne.length + messageHash.length);
23
73
  scriptSig.set(scriptSigPartOne);
24
74
  scriptSig.set(messageHash, scriptSigPartOne.length);
75
+ // Set the input
25
76
  psbt.addInput({
26
77
  hash: '0'.repeat(64),
27
78
  index: 0xFFFFFFFF,
28
79
  sequence: 0,
29
80
  finalScriptSig: Buffer.from(scriptSig),
30
- witnessScript: Buffer.from([])
81
+ witnessScript: Buffer.from([]) // vin[0].scriptWitness = []
31
82
  });
83
+ // Set the output
32
84
  psbt.addOutput({
33
85
  value: 0,
34
- script: scriptPublicKey
86
+ script: scriptPublicKey // vout[0].scriptPubKey = message_challenge
35
87
  });
88
+ // Return transaction
36
89
  return psbt.extractTransaction();
37
90
  }
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
+ */
38
99
  static buildToSignTx(toSpendTxId, witnessScript, isRedeemScript = false, tapInternalKey = undefined) {
100
+ // Create PSBT object for constructing the transaction
39
101
  const psbt = new bitcoin.Psbt();
40
- psbt.setVersion(0);
41
- psbt.setLocktime(0);
102
+ // Set default value for nVersion and nLockTime
103
+ psbt.setVersion(0); // nVersion = 0
104
+ psbt.setLocktime(0); // nLockTime = 0
105
+ // Set the input
42
106
  psbt.addInput({
43
107
  hash: toSpendTxId,
44
108
  index: 0,
@@ -48,25 +112,36 @@ class BIP322 {
48
112
  value: 0
49
113
  }
50
114
  });
115
+ // Set redeemScript as witnessScript if isRedeemScript
51
116
  if (isRedeemScript) {
52
117
  psbt.updateInput(0, {
53
118
  redeemScript: witnessScript
54
119
  });
55
120
  }
121
+ // Set tapInternalKey if provided
56
122
  if (tapInternalKey) {
57
123
  psbt.updateInput(0, {
58
124
  tapInternalKey: tapInternalKey
59
125
  });
60
126
  }
127
+ // Set the output
61
128
  psbt.addOutput({
62
129
  value: 0,
63
- script: Buffer.from([0x6a])
130
+ script: Buffer.from([0x6a]) // vout[0].scriptPubKey = OP_RETURN
64
131
  });
65
132
  return psbt;
66
133
  }
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
+ */
67
139
  static encodeWitness(signedPsbt) {
140
+ // Obtain the signed witness data
68
141
  const witness = signedPsbt.data.inputs[0].finalScriptWitness;
142
+ // Check if the witness data is present
69
143
  if (witness) {
144
+ // Return the base-64 encoded witness stack
70
145
  return witness.toString('base64');
71
146
  }
72
147
  else {
@@ -74,5 +149,7 @@ class BIP322 {
74
149
  }
75
150
  }
76
151
  }
77
- export default BIP322;
152
+ // BIP322 message tag
153
+ BIP322.TAG = Buffer.from("BIP0322-signed-message");
154
+ exports.default = BIP322;
78
155
  //# sourceMappingURL=BIP322.js.map
package/dist/Signer.d.ts CHANGED
@@ -1,6 +1,25 @@
1
- import * as bitcoin from '@exodus/bitcoinjs';
1
+ /// <reference types="node" />
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
+ */
2
7
  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
+ */
3
16
  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
+ */
4
23
  private static checkPubKeyCorrespondToAddress;
5
24
  }
6
25
  export default Signer;
package/dist/Signer.js CHANGED
@@ -1,61 +1,126 @@
1
- import BIP322 from './BIP322';
2
- import { Address } from './helpers';
3
- import * as bitcoin from '@exodus/bitcoinjs';
4
- import * as bitcoinMessage from 'bitcoinjs-message';
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
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 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
+ */
5
40
  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
+ */
6
49
  static sign(privateKeyOrWIF, address, message, network = bitcoin.networks.bitcoin) {
7
- const ECPair = bitcoin.ECPair;
8
- let signer = Buffer.isBuffer(privateKeyOrWIF)
9
- ? ECPair.fromPrivateKey(privateKeyOrWIF)
10
- : ECPair.fromWIF(privateKeyOrWIF, network);
50
+ // Initialize private key used to sign the transaction
51
+ const ECPair = (0, ecpair_1.default)(bitcoinerlab_secp256k1_1.default);
52
+ let signer = Buffer.isBuffer(privateKeyOrWIF) ? ECPair.fromPrivateKey(privateKeyOrWIF) : ECPair.fromWIF(privateKeyOrWIF, network);
53
+ // Check if the private key can sign message for the given address
11
54
  if (!this.checkPubKeyCorrespondToAddress(signer.publicKey, address)) {
12
55
  throw new Error(`Invalid private key provided for signing message for ${address}.`);
13
56
  }
14
- if (Address.isP2PKH(address)) {
57
+ // Handle legacy P2PKH signature
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
15
61
  return bitcoinMessage.sign(message, signer.privateKey, signer.compressed);
16
62
  }
17
- const scriptPubKey = Address.convertAdressToScriptPubkey(address);
18
- const toSpendTx = BIP322.buildToSpendTx(message, scriptPubKey);
63
+ // Convert address into corresponding script pubkey
64
+ const scriptPubKey = helpers_1.Address.convertAdressToScriptPubkey(address);
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
19
68
  let toSignTx;
20
- if (Address.isP2SH(address)) {
69
+ if (helpers_1.Address.isP2SH(address)) {
70
+ // P2SH-P2WPKH signing path
71
+ // Derive the P2SH-P2WPKH redeemScript from the corresponding hashed public key
21
72
  const redeemScript = bitcoin.payments.p2wpkh({
22
73
  hash: bitcoin.crypto.hash160(signer.publicKey),
23
- network,
74
+ network: network
24
75
  }).output;
25
- toSignTx = BIP322.buildToSignTx(toSpendTx.getId(), redeemScript, true);
76
+ toSignTx = BIP322_1.default.buildToSignTx(toSpendTx.getId(), redeemScript, true);
26
77
  }
27
- else if (Address.isP2WPKH(address)) {
28
- toSignTx = BIP322.buildToSignTx(toSpendTx.getId(), scriptPubKey);
78
+ else if (helpers_1.Address.isP2WPKH(address)) {
79
+ // P2WPKH signing path
80
+ toSignTx = BIP322_1.default.buildToSignTx(toSpendTx.getId(), scriptPubKey);
29
81
  }
30
82
  else {
83
+ // P2TR signing path
84
+ // Extract the taproot internal public key
31
85
  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
32
88
  signer = signer.tweak(bitcoin.crypto.taggedHash('TapTweak', signer.publicKey.subarray(1, 33)));
33
- toSignTx = BIP322.buildToSignTx(toSpendTx.getId(), scriptPubKey, false, internalPublicKey);
89
+ // Draft a toSign transaction that spends toSpend transaction
90
+ toSignTx = BIP322_1.default.buildToSignTx(toSpendTx.getId(), scriptPubKey, false, internalPublicKey);
34
91
  }
35
- const toSignTxSigned = toSignTx
36
- .signAllInputs(signer, [bitcoin.Transaction.SIGHASH_ALL, bitcoin.Transaction.SIGHASH_DEFAULT])
37
- .finalizeAllInputs();
38
- return BIP322.encodeWitness(toSignTxSigned);
92
+ // Sign the toSign transaction
93
+ const toSignTxSigned = toSignTx.signAllInputs(signer, [bitcoin.Transaction.SIGHASH_ALL, bitcoin.Transaction.SIGHASH_DEFAULT]).finalizeAllInputs();
94
+ // Extract and return the signature
95
+ return BIP322_1.default.encodeWitness(toSignTxSigned);
39
96
  }
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
+ */
40
103
  static checkPubKeyCorrespondToAddress(publicKey, claimedAddress) {
104
+ // Derive the same address type from the provided public key
41
105
  let derivedAddresses;
42
- if (Address.isP2PKH(claimedAddress)) {
43
- derivedAddresses = Address.convertPubKeyIntoAddress(publicKey, 'p2pkh');
106
+ if (helpers_1.Address.isP2PKH(claimedAddress)) {
107
+ derivedAddresses = helpers_1.Address.convertPubKeyIntoAddress(publicKey, 'p2pkh');
44
108
  }
45
- else if (Address.isP2SH(claimedAddress)) {
46
- derivedAddresses = Address.convertPubKeyIntoAddress(publicKey, 'p2sh-p2wpkh');
109
+ else if (helpers_1.Address.isP2SH(claimedAddress)) {
110
+ derivedAddresses = helpers_1.Address.convertPubKeyIntoAddress(publicKey, 'p2sh-p2wpkh');
47
111
  }
48
- else if (Address.isP2WPKH(claimedAddress)) {
49
- derivedAddresses = Address.convertPubKeyIntoAddress(publicKey, 'p2wpkh');
112
+ else if (helpers_1.Address.isP2WPKH(claimedAddress)) {
113
+ derivedAddresses = helpers_1.Address.convertPubKeyIntoAddress(publicKey, 'p2wpkh');
50
114
  }
51
- else if (Address.isP2TR(claimedAddress)) {
52
- derivedAddresses = Address.convertPubKeyIntoAddress(publicKey, 'p2tr');
115
+ else if (helpers_1.Address.isP2TR(claimedAddress)) {
116
+ derivedAddresses = helpers_1.Address.convertPubKeyIntoAddress(publicKey, 'p2tr');
53
117
  }
54
118
  else {
55
- throw new Error('Unable to sign BIP-322 message for unsupported address type.');
119
+ throw new Error('Unable to sign BIP-322 message for unsupported address type.'); // Unsupported address type
56
120
  }
57
- return (derivedAddresses.mainnet === claimedAddress || derivedAddresses.testnet === claimedAddress);
121
+ // Check if the derived address correspond to the claimedAddress
122
+ return (derivedAddresses.mainnet === claimedAddress) || (derivedAddresses.testnet === claimedAddress);
58
123
  }
59
124
  }
60
- export default Signer;
125
+ exports.default = Signer;
61
126
  //# sourceMappingURL=Signer.js.map
@@ -1,8 +1,47 @@
1
+ /**
2
+ * Class that handles BIP-322 signature verification.
3
+ * Reference: https://github.com/LegReq/bip0322-signatures/blob/master/BIP0322_verification.ipynb
4
+ */
1
5
  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
+ */
2
14
  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
+ */
3
24
  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
+ */
4
30
  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
+ */
5
37
  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
+ */
6
45
  private static getHashForSigP2TR;
7
46
  }
8
47
  export default Verifier;