@exodus/bip322-js 1.1.0 → 1.2.1

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.
Files changed (39) hide show
  1. package/CHANGELOG.md +61 -0
  2. package/README.md +13 -13
  3. package/dist/BIP322.d.ts +2 -33
  4. package/dist/BIP322.js +23 -103
  5. package/dist/BIP322.js.map +1 -0
  6. package/dist/Signer.d.ts +1 -20
  7. package/dist/Signer.js +32 -97
  8. package/dist/Signer.js.map +1 -0
  9. package/dist/Verifier.d.ts +0 -39
  10. package/dist/Verifier.js +60 -175
  11. package/dist/Verifier.js.map +1 -0
  12. package/dist/bitcoinjs/DecodeScriptSignature.d.ts +0 -1
  13. package/dist/bitcoinjs/DecodeScriptSignature.js +1 -7
  14. package/dist/bitcoinjs/DecodeScriptSignature.js.map +1 -0
  15. package/dist/bitcoinjs/index.d.ts +1 -2
  16. package/dist/bitcoinjs/index.js +1 -5
  17. package/dist/bitcoinjs/index.js.map +1 -0
  18. package/dist/helpers/Address.d.ts +2 -49
  19. package/dist/helpers/Address.js +57 -156
  20. package/dist/helpers/Address.js.map +1 -0
  21. package/dist/helpers/BIP137.d.ts +0 -21
  22. package/dist/helpers/BIP137.js +6 -61
  23. package/dist/helpers/BIP137.js.map +1 -0
  24. package/dist/helpers/VarInt.d.ts +0 -17
  25. package/dist/helpers/VarInt.js +11 -40
  26. package/dist/helpers/VarInt.js.map +1 -0
  27. package/dist/helpers/VarStr.d.ts +0 -17
  28. package/dist/helpers/VarStr.js +5 -32
  29. package/dist/helpers/VarStr.js.map +1 -0
  30. package/dist/helpers/Witness.d.ts +0 -18
  31. package/dist/helpers/Witness.js +10 -47
  32. package/dist/helpers/Witness.js.map +1 -0
  33. package/dist/helpers/index.d.ts +5 -6
  34. package/dist/helpers/index.js +5 -16
  35. package/dist/helpers/index.js.map +1 -0
  36. package/dist/index.d.ts +5 -5
  37. package/dist/index.js +5 -17
  38. package/dist/index.js.map +1 -0
  39. package/package.json +53 -44
package/CHANGELOG.md ADDED
@@ -0,0 +1,61 @@
1
+ # Change Log
2
+
3
+ All notable changes to this project will be documented in this file.
4
+ See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.
5
+
6
+ ## [1.2.1](https://github.com/ExodusMovement/exodus-hydra/compare/@exodus/bip322-js@1.2.0...@exodus/bip322-js@1.2.1) (2024-10-22)
7
+
8
+ **Note:** Version bump only for package @exodus/bip322-js
9
+
10
+ ## [1.1.0-rc.0](https://github.com/ExodusMovement/exodus-hydra.git/compare/@exodus/bip322-js@1.1.0-exodus.6...@exodus/bip322-js@1.1.0-rc.0) (2024-10-11)
11
+
12
+ **Note:** Version bump only for package @exodus/bip322-js
13
+
14
+ # Changelog
15
+
16
+ All notable changes to this project will be documented in this file.
17
+
18
+ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
19
+ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
20
+
21
+ ## [Current]
22
+
23
+ ### Changed
24
+
25
+ - Bumped `@exodus/bitcoinjs-lib` fork
26
+ - Removed ecc initialization of `@exodus/bitcoinjs-lib`
27
+
28
+ ## [1.1.0-exodus.6] - 2024-07-22
29
+
30
+ ### Changed
31
+
32
+ - Swap out with `@exodus/bitcoinjs-lib` fork
33
+ - Swap out with `@exodus/secp256k1` fork
34
+
35
+ ## [1.1.0-exodus.4] - 2023-10-31
36
+
37
+ ### Changed
38
+
39
+ - Sign with `SIGHASH_DEFAULT` instead of `SIGHASH_ALL` for non-spec compliant compatibility.
40
+
41
+ ## [1.1.0] - 2023-08-20
42
+
43
+ ### Added
44
+
45
+ - Added support for BIP-137 legacy signature verification against P2SH-P2WPKH, P2WPKH, and single-key-spend P2TR addresses.
46
+
47
+ ## [1.0.3] - 2023-06-29
48
+
49
+ ### Fixed
50
+
51
+ - Fixed ECC library uninitialized error during taproot signature verification.
52
+
53
+ ## [1.0.2] - 2023-06-28
54
+
55
+ Initial release.
56
+
57
+ ### Added
58
+
59
+ - Generate raw toSpend and toSign BIP-322 transactions via the BIP322 class.
60
+ - Sign a BIP-322 signature using a private key via the Signer class.
61
+ - Verify a simple BIP-322 signature via the Verifier class.
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,40 +1,9 @@
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
- */
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,155 +1,75 @@
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
- */
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
- // Compute the message hash - SHA256(SHA256(tag) || SHA256(tag) || message)
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 = (0, create_hash_1.default)('sha256');
9
+ const messageHasher = createHash('sha256');
50
10
  messageHasher.update(tagHash);
51
11
  messageHasher.update(tagHash);
52
12
  messageHasher.update(Buffer.from(message));
53
- const messageHash = messageHasher.digest();
54
- return messageHash;
13
+ return messageHasher.digest();
55
14
  }
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
15
  static buildToSpendTx(message, scriptPublicKey) {
63
- // Create PSBT object for constructing the transaction
64
16
  const psbt = new bitcoin.Psbt();
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)
17
+ psbt.setVersion(0);
18
+ psbt.setLocktime(0);
69
19
  const messageHash = this.hashMessage(message);
70
- // Construct the scriptSig - OP_0 PUSH32[ message_hash ]
71
- const scriptSigPartOne = new Uint8Array([0x00, 0x20]); // OP_0 PUSH32
20
+ const scriptSigPartOne = new Uint8Array([0x00, 0x20]);
72
21
  const scriptSig = new Uint8Array(scriptSigPartOne.length + messageHash.length);
73
22
  scriptSig.set(scriptSigPartOne);
74
23
  scriptSig.set(messageHash, scriptSigPartOne.length);
75
- // Set the input
76
24
  psbt.addInput({
77
25
  hash: '0'.repeat(64),
78
- index: 0xFFFFFFFF,
26
+ index: 0xff_ff_ff_ff,
79
27
  sequence: 0,
80
28
  finalScriptSig: Buffer.from(scriptSig),
81
- witnessScript: Buffer.from([]) // vin[0].scriptWitness = []
29
+ witnessScript: Buffer.from([]),
82
30
  });
83
- // Set the output
84
31
  psbt.addOutput({
85
32
  value: 0,
86
- script: scriptPublicKey // vout[0].scriptPubKey = message_challenge
33
+ script: scriptPublicKey,
87
34
  });
88
- // Return transaction
89
35
  return psbt.extractTransaction();
90
36
  }
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
- static buildToSignTx(toSpendTxId, witnessScript, isRedeemScript = false, tapInternalKey = undefined) {
100
- // Create PSBT object for constructing the transaction
37
+ static buildToSignTx(toSpendTxId, witnessScript, isRedeemScript = false, tapInternalKey) {
101
38
  const psbt = new bitcoin.Psbt();
102
- // Set default value for nVersion and nLockTime
103
- psbt.setVersion(0); // nVersion = 0
104
- psbt.setLocktime(0); // nLockTime = 0
105
- // Set the input
39
+ psbt.setVersion(0);
40
+ psbt.setLocktime(0);
106
41
  psbt.addInput({
107
42
  hash: toSpendTxId,
108
43
  index: 0,
109
44
  sequence: 0,
110
45
  witnessUtxo: {
111
46
  script: witnessScript,
112
- value: 0
113
- }
47
+ value: 0,
48
+ },
114
49
  });
115
- // Set redeemScript as witnessScript if isRedeemScript
116
50
  if (isRedeemScript) {
117
51
  psbt.updateInput(0, {
118
- redeemScript: witnessScript
52
+ redeemScript: witnessScript,
119
53
  });
120
54
  }
121
- // Set tapInternalKey if provided
122
55
  if (tapInternalKey) {
123
56
  psbt.updateInput(0, {
124
- tapInternalKey: tapInternalKey
57
+ tapInternalKey,
125
58
  });
126
59
  }
127
- // Set the output
128
60
  psbt.addOutput({
129
61
  value: 0,
130
- script: Buffer.from([0x6a]) // vout[0].scriptPubKey = OP_RETURN
62
+ script: Buffer.from([0x6a]),
131
63
  });
132
64
  return psbt;
133
65
  }
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
66
  static encodeWitness(signedPsbt) {
140
- // Obtain the signed witness data
141
- const witness = signedPsbt.data.inputs[0].finalScriptWitness;
142
- // Check if the witness data is present
67
+ const witness = signedPsbt.data.inputs[0]?.finalScriptWitness;
143
68
  if (witness) {
144
- // Return the base-64 encoded witness stack
145
69
  return witness.toString('base64');
146
70
  }
147
- else {
148
- throw new Error('Cannot encode empty witness stack.');
149
- }
71
+ throw new Error('Cannot encode empty witness stack.');
150
72
  }
151
73
  }
152
- // BIP322 message tag
153
- BIP322.TAG = Buffer.from("BIP0322-signed-message");
154
- exports.default = BIP322;
74
+ export default BIP322;
155
75
  //# sourceMappingURL=BIP322.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"BIP322.js","sourceRoot":"","sources":["../src/BIP322.ts"],"names":[],"mappings":"AACA,OAAO,UAAU,MAAM,aAAa,CAAA;AACpC,OAAO,KAAK,OAAO,MAAM,mBAAmB,CAAA;AAM5C,MAAM,MAAM;IAEV,MAAM,CAAC,GAAG,GAAG,MAAM,CAAC,IAAI,CAAC,wBAAwB,CAAC,CAAA;IAS3C,MAAM,CAAC,WAAW,CAAC,OAAe;QAEvC,MAAM,SAAS,GAAG,UAAU,CAAC,QAAQ,CAAC,CAAA;QACtC,SAAS,CAAC,MAAM,CAAC,IAAI,CAAC,GAAG,CAAC,CAAA;QAC1B,MAAM,OAAO,GAAG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C;;AAGH,eAAe,MAAM,CAAA"}
package/dist/Signer.d.ts CHANGED
@@ -1,25 +1,6 @@
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
- */
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
- "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
- */
1
+ import BIP322 from './BIP322.js';
2
+ import { Address } from './helpers/index.js';
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
- // 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
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
- // 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
14
+ if (Address.isP2PKH(address)) {
61
15
  return bitcoinMessage.sign(message, signer.privateKey, signer.compressed);
62
16
  }
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
17
+ const scriptPubKey = Address.convertAdressToScriptPubkey(address);
18
+ const toSpendTx = BIP322.buildToSpendTx(message, scriptPubKey);
68
19
  let toSignTx;
69
- if (helpers_1.Address.isP2SH(address)) {
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: network
23
+ network,
75
24
  }).output;
76
- toSignTx = BIP322_1.default.buildToSignTx(toSpendTx.getId(), redeemScript, true);
25
+ toSignTx = BIP322.buildToSignTx(toSpendTx.getId(), redeemScript, true);
77
26
  }
78
- else if (helpers_1.Address.isP2WPKH(address)) {
79
- // P2WPKH signing path
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
- // Draft a toSign transaction that spends toSpend transaction
90
- toSignTx = BIP322_1.default.buildToSignTx(toSpendTx.getId(), scriptPubKey, false, internalPublicKey);
33
+ toSignTx = BIP322.buildToSignTx(toSpendTx.getId(), scriptPubKey, false, internalPublicKey);
91
34
  }
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);
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 (helpers_1.Address.isP2PKH(claimedAddress)) {
107
- derivedAddresses = helpers_1.Address.convertPubKeyIntoAddress(publicKey, 'p2pkh');
42
+ if (Address.isP2PKH(claimedAddress)) {
43
+ derivedAddresses = Address.convertPubKeyIntoAddress(publicKey, 'p2pkh');
108
44
  }
109
- else if (helpers_1.Address.isP2SH(claimedAddress)) {
110
- derivedAddresses = helpers_1.Address.convertPubKeyIntoAddress(publicKey, 'p2sh-p2wpkh');
45
+ else if (Address.isP2SH(claimedAddress)) {
46
+ derivedAddresses = Address.convertPubKeyIntoAddress(publicKey, 'p2sh-p2wpkh');
111
47
  }
112
- else if (helpers_1.Address.isP2WPKH(claimedAddress)) {
113
- derivedAddresses = helpers_1.Address.convertPubKeyIntoAddress(publicKey, 'p2wpkh');
48
+ else if (Address.isP2WPKH(claimedAddress)) {
49
+ derivedAddresses = Address.convertPubKeyIntoAddress(publicKey, 'p2wpkh');
114
50
  }
115
- else if (helpers_1.Address.isP2TR(claimedAddress)) {
116
- derivedAddresses = helpers_1.Address.convertPubKeyIntoAddress(publicKey, 'p2tr');
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.'); // Unsupported address type
55
+ throw new Error('Unable to sign BIP-322 message for unsupported address type.');
120
56
  }
121
- // Check if the derived address correspond to the claimedAddress
122
- return (derivedAddresses.mainnet === claimedAddress) || (derivedAddresses.testnet === claimedAddress);
57
+ return (derivedAddresses.mainnet === claimedAddress || derivedAddresses.testnet === claimedAddress);
123
58
  }
124
59
  }
125
- exports.default = Signer;
60
+ export default Signer;
126
61
  //# sourceMappingURL=Signer.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"Signer.js","sourceRoot":"","sources":["../src/Signer.ts"],"names":[],"mappings":"AACA,OAAO,MAAM,MAAM,aAAa,CAAA;AAChC,OAAO,EAAE,OAAO,EAAE,MAAM,oBAAoB,CAAA;AAC5C,OAAO,KAAK,OAAO,MAAM,mBAAmB,CAAA;AAC5C,OAAO,KAAK,cAAc,MAAM,mBAAmB,CAAA;AAMnD,MAAM,MAAM;IASH,MAAM,CAAC,IAAI,CAChB,eAAgC,EAChC,OAAe,E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CAAC,wBAAwB,CAAC,SAAS,EAAE,QAAQ,CAAC,CAAA;QAC1E,CAAC;aAAM,IAAI,OAAO,CAAC,MAAM,CAAC,cAAc,CAAC,EAAE,CAAC;YAC1C,gBAAgB,GAAG,OAAO,CAAC,wBAAwB,CAAC,SAAS,EAAE,MAAM,CAAE,CAAA;QACzE,CAAC;aAAM,CAAC;YACN,MAAM,IAAI,KAAK,CAAC,8DAA8D,CAAC,CAAA;QACjF,CAAC;QAGD,OAAO,CACL,gBAAgB,CAAC,OAAO,KAAK,cAAc,IAAI,gBAAgB,CAAC,OAAO,KAAK,cAAc,CAC3F,CAAA;IACH,CAAC;CACF;AAED,eAAe,MAAM,CAAA"}
@@ -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;