@exodus/bip322-js 1.1.0-exodus.6 → 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/dist/Verifier.js CHANGED
@@ -1,239 +1,118 @@
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 helpers_1 = require("./helpers");
32
- const bitcoin = __importStar(require("@exodus/bitcoinjs-lib"));
33
- const bitcoinerlab_secp256k1_1 = __importDefault(require("@exodus/bitcoinerlab-secp256k1"));
34
- const bitcoinMessage = __importStar(require("bitcoinjs-message"));
35
- const bitcoinjs_1 = require("./bitcoinjs");
36
- /**
37
- * Class that handles BIP-322 signature verification.
38
- * Reference: https://github.com/LegReq/bip0322-signatures/blob/master/BIP0322_verification.ipynb
39
- */
1
+ import BIP322 from "./BIP322";
2
+ import { Address, BIP137 } from "./helpers";
3
+ import * as bitcoin from '@exodus/bitcoinjs';
4
+ import ecc from '@exodus/bitcoinerlab-secp256k1';
5
+ import * as bitcoinMessage from 'bitcoinjs-message';
6
+ import { decodeScriptSignature } from './bitcoinjs';
40
7
  class Verifier {
41
- /**
42
- * Verify a BIP-322 signature from P2WPKH, P2SH-P2WPKH, and single-key-spend P2TR address.
43
- * @param signerAddress Address of the signing address
44
- * @param message message_challenge signed by the address
45
- * @param signatureBase64 Signature produced by the signing address
46
- * @returns True if the provided signature is a valid BIP-322 signature for the given message and address, false if otherwise
47
- * @throws If the provided signature fails basic validation, or if unsupported address and signature are provided
48
- */
49
8
  static verifySignature(signerAddress, message, signatureBase64) {
50
- // Handle legacy BIP-137 signature
51
- // For P2PKH address, assume the signature is also a legacy signature
52
- if (helpers_1.Address.isP2PKH(signerAddress) || helpers_1.BIP137.isBIP137Signature(signatureBase64)) {
9
+ if (Address.isP2PKH(signerAddress) || BIP137.isBIP137Signature(signatureBase64)) {
53
10
  return this.verifyBIP137Signature(signerAddress, message, signatureBase64);
54
11
  }
55
- // Convert address into corresponding script pubkey
56
- const scriptPubKey = helpers_1.Address.convertAdressToScriptPubkey(signerAddress);
57
- // Draft corresponding toSpend and toSign transaction using the message and script pubkey
58
- const toSpendTx = BIP322_1.default.buildToSpendTx(message, scriptPubKey);
59
- const toSignTx = BIP322_1.default.buildToSignTx(toSpendTx.getId(), scriptPubKey);
60
- // Add the witness stack into the toSignTx
12
+ const scriptPubKey = Address.convertAdressToScriptPubkey(signerAddress);
13
+ const toSpendTx = BIP322.buildToSpendTx(message, scriptPubKey);
14
+ const toSignTx = BIP322.buildToSignTx(toSpendTx.getId(), scriptPubKey);
61
15
  toSignTx.updateInput(0, {
62
16
  finalScriptWitness: Buffer.from(signatureBase64, 'base64')
63
17
  });
64
- // Obtain the signature within the witness components
65
18
  const witness = toSignTx.extractTransaction().ins[0].witness;
66
19
  const encodedSignature = witness[0];
67
- // Branch depending on whether the signing address is a non-taproot or a taproot address
68
- if (helpers_1.Address.isP2WPKHWitness(witness)) {
69
- // For non-taproot segwit transaciton, public key is included as the second part of the witness data
20
+ if (Address.isP2WPKHWitness(witness)) {
70
21
  const publicKey = witness[1];
71
- const { signature } = (0, bitcoinjs_1.decodeScriptSignature)(encodedSignature);
72
- // Compute OP_HASH160(publicKey)
22
+ const { signature } = decodeScriptSignature(encodedSignature);
73
23
  const hashedPubkey = bitcoin.crypto.hash160(publicKey);
74
- // Common path variable
75
- let hashToSign; // Hash expected to be signed by the signing address
76
- if (helpers_1.Address.isP2SH(signerAddress)) {
77
- // P2SH-P2WPKH verification path
78
- // Compute the hash that correspond to the toSignTx
24
+ let hashToSign;
25
+ if (Address.isP2SH(signerAddress)) {
79
26
  hashToSign = this.getHashForSigP2SHInP2WPKH(toSignTx, hashedPubkey);
80
- // The original locking script for P2SH-P2WPKH is OP_0 <PubKeyHash>
81
27
  const lockingScript = Buffer.concat([Buffer.from([0x00, 0x14]), hashedPubkey]);
82
- // Compute OP_HASH160(lockingScript)
83
28
  const hashedLockingScript = bitcoin.crypto.hash160(lockingScript);
84
- // For nested segwit (P2SH-P2WPKH) address, the hashed locking script is located from the 3rd byte to the last 2nd byte as OP_HASH160 <HASH> OP_EQUAL
85
29
  const hashedLockingScriptInScriptPubKey = scriptPubKey.subarray(2, -1);
86
- // Check if the P2SH locking script OP_HASH160 <HASH> OP_EQUAL is satisified
87
30
  if (Buffer.compare(hashedLockingScript, hashedLockingScriptInScriptPubKey) !== 0) {
88
- return false; // Reject signature if the hashed locking script is different from the hashed locking script in the scriptPubKey
31
+ return false;
89
32
  }
90
33
  }
91
34
  else {
92
- // P2WPKH verification path
93
- // Compute the hash that correspond to the toSignTx
94
35
  hashToSign = this.getHashForSigP2WPKH(toSignTx);
95
- // For native segwit address, the hashed public key is located from the 3rd to the end as OP_0 <HASH>
96
36
  const hashedPubkeyInScriptPubkey = scriptPubKey.subarray(2);
97
- // Check if OP_HASH160(publicKey) === hashedPubkeyInScriptPubkey
98
37
  if (Buffer.compare(hashedPubkey, hashedPubkeyInScriptPubkey) !== 0) {
99
- return false; // Reject signature if the hashed public key did not match
38
+ return false;
100
39
  }
101
40
  }
102
- // Computing OP_CHECKSIG in Javascript
103
- return bitcoinerlab_secp256k1_1.default.verify(hashToSign, publicKey, signature);
41
+ return ecc.verify(hashToSign, publicKey, signature);
104
42
  }
105
- else if (helpers_1.Address.isP2TR(signerAddress)) {
106
- // Check if the witness stack correspond to a single-key-spend P2TR address
107
- if (!helpers_1.Address.isSingleKeyP2TRWitness(witness)) {
43
+ else if (Address.isP2TR(signerAddress)) {
44
+ if (!Address.isSingleKeyP2TRWitness(witness)) {
108
45
  throw new Error('BIP-322 verification from script-spend P2TR is unsupported.');
109
46
  }
110
- // For taproot address, the public key is located starting from the 3rd byte of the script public key
111
47
  const publicKey = scriptPubKey.subarray(2);
112
- // Compute the hash to be signed by the signing address
113
- // Reference: https://github.com/bitcoin/bips/blob/master/bip-0341.mediawiki#user-content-Taproot_key_path_spending_signature_validation
114
48
  let hashToSign;
115
49
  let signature;
116
50
  if (encodedSignature.byteLength === 64) {
117
- // If a BIP-341 signature is 64 bytes, the signature is signed using SIGHASH_DEFAULT 0x00
118
51
  hashToSign = this.getHashForSigP2TR(toSignTx, 0x00);
119
- // And the entirety of the encoded signature is the actual signature
120
52
  signature = encodedSignature;
121
53
  }
122
54
  else if (encodedSignature.byteLength === 65) {
123
- // If a BIP-341 signature is 65 bytes, the signature is signed using SIGHASH included at the last byte of the signature
124
55
  hashToSign = this.getHashForSigP2TR(toSignTx, encodedSignature[64]);
125
- // And encodedSignature[0:64] holds the actual signature
126
56
  signature = encodedSignature.subarray(0, -1);
127
57
  }
128
58
  else {
129
- // Fail validation if the signature is not 64 or 65 bytes
130
59
  throw new Error('Invalid Schnorr signature provided.');
131
60
  }
132
- // Computing OP_CHECKSIG in Javascript
133
- return bitcoinerlab_secp256k1_1.default.verifySchnorr(hashToSign, publicKey, signature);
61
+ return ecc.verifySchnorr(hashToSign, publicKey, signature);
134
62
  }
135
63
  else {
136
64
  throw new Error('Only P2WPKH, P2SH-P2WPKH, and single-key-spend P2TR BIP-322 verification is supported. Unsupported address is provided.');
137
65
  }
138
66
  }
139
- /**
140
- * Verify a legacy BIP-137 signature.
141
- * Note that a signature is considered valid for all types of addresses that can be derived from the recovered public key.
142
- * @param signerAddress Address of the signing address
143
- * @param message message_challenge signed by the address
144
- * @param signatureBase64 Signature produced by the signing address
145
- * @returns True if the provided signature is a valid BIP-137 signature for the given message and address, false if otherwise
146
- * @throws If the provided signature fails basic validation, or if unsupported address and signature are provided
147
- */
148
67
  static verifyBIP137Signature(signerAddress, message, signatureBase64) {
149
- if (helpers_1.Address.isP2PKH(signerAddress)) {
68
+ if (Address.isP2PKH(signerAddress)) {
150
69
  return bitcoinMessage.verify(message, signerAddress, signatureBase64);
151
70
  }
152
71
  else {
153
- // Recover the public key associated with the signature
154
- const publicKeySigned = helpers_1.BIP137.derivePubKey(message, signatureBase64);
155
- // Set the equivalent legacy address to prepare for validation from bitcoinjs-message
156
- const legacySigningAddress = helpers_1.Address.convertPubKeyIntoAddress(publicKeySigned, 'p2pkh').mainnet;
157
- // Make sure that public key recovered corresponds to the claimed signing address
158
- if (helpers_1.Address.isP2SH(signerAddress)) {
159
- // Assume it is a P2SH-P2WPKH address, derive a P2SH-P2WPKH address based on the public key recovered
160
- const p2shAddressDerived = helpers_1.Address.convertPubKeyIntoAddress(publicKeySigned, 'p2sh-p2wpkh');
161
- // Assert that the derived address is identical to the claimed signing address
72
+ const publicKeySigned = BIP137.derivePubKey(message, signatureBase64);
73
+ const legacySigningAddress = Address.convertPubKeyIntoAddress(publicKeySigned, 'p2pkh').mainnet;
74
+ if (Address.isP2SH(signerAddress)) {
75
+ const p2shAddressDerived = Address.convertPubKeyIntoAddress(publicKeySigned, 'p2sh-p2wpkh');
162
76
  if (p2shAddressDerived.mainnet !== signerAddress && p2shAddressDerived.testnet !== signerAddress) {
163
- return false; // Derived address did not match with the claimed signing address
77
+ return false;
164
78
  }
165
79
  }
166
- else if (helpers_1.Address.isP2WPKH(signerAddress)) {
167
- // Assume it is a P2WPKH address, derive a P2WPKH address based on the public key recovered
168
- const p2wpkhAddressDerived = helpers_1.Address.convertPubKeyIntoAddress(publicKeySigned, 'p2wpkh');
169
- // Assert that the derived address is identical to the claimed signing address
80
+ else if (Address.isP2WPKH(signerAddress)) {
81
+ const p2wpkhAddressDerived = Address.convertPubKeyIntoAddress(publicKeySigned, 'p2wpkh');
170
82
  if (p2wpkhAddressDerived.mainnet !== signerAddress && p2wpkhAddressDerived.testnet !== signerAddress) {
171
- return false; // Derived address did not match with the claimed signing address
83
+ return false;
172
84
  }
173
85
  }
174
- else if (helpers_1.Address.isP2TR(signerAddress)) {
175
- // Assume it is a P2TR address, derive a P2TR address based on the public key recovered
176
- const p2trAddressDerived = helpers_1.Address.convertPubKeyIntoAddress(publicKeySigned, 'p2tr');
177
- // Assert that the derived address is identical to the claimed signing address
86
+ else if (Address.isP2TR(signerAddress)) {
87
+ const p2trAddressDerived = Address.convertPubKeyIntoAddress(publicKeySigned, 'p2tr');
178
88
  if (p2trAddressDerived.mainnet !== signerAddress && p2trAddressDerived.testnet !== signerAddress) {
179
- return false; // Derived address did not match with the claimed signing address
89
+ return false;
180
90
  }
181
91
  }
182
92
  else {
183
- return false; // Unsupported address type
93
+ return false;
184
94
  }
185
- // Validate the signature using bitcoinjs-message if address assertion succeeded
186
95
  return bitcoinMessage.verify(message, legacySigningAddress, signatureBase64);
187
96
  }
188
97
  }
189
- /**
190
- * Compute the hash to be signed for a given P2WPKH BIP-322 toSign transaction.
191
- * @param toSignTx PSBT instance of the toSign transaction
192
- * @returns Computed transaction hash that requires signing
193
- */
194
98
  static getHashForSigP2WPKH(toSignTx) {
195
- // Create a signing script to unlock the P2WPKH output based on the P2PKH template
196
- // Reference: https://github.com/bitcoinjs/bitcoinjs-lib/blob/1a9119b53bcea4b83a6aa8b948f0e6370209b1b4/ts_src/psbt.ts#L1654
197
99
  const signingScript = bitcoin.payments.p2pkh({
198
100
  hash: toSignTx.data.inputs[0].witnessUtxo.script.subarray(2)
199
101
  }).output;
200
- // Return computed transaction hash to be signed
201
102
  return toSignTx.extractTransaction().hashForWitnessV0(0, signingScript, 0, bitcoin.Transaction.SIGHASH_ALL);
202
103
  }
203
- /**
204
- * Compute the hash to be signed for a given P2SH-P2WPKH BIP-322 toSign transaction.
205
- * @param toSignTx PSBT instance of the toSign transaction
206
- * @param hashedPubkey Hashed public key of the signing address
207
- * @returns Computed transaction hash that requires signing
208
- */
209
104
  static getHashForSigP2SHInP2WPKH(toSignTx, hashedPubkey) {
210
- // Create a signing script to unlock the P2WPKH output based on the P2PKH template
211
- // Reference: https://github.com/bitcoinjs/bitcoinjs-lib/blob/1a9119b53bcea4b83a6aa8b948f0e6370209b1b4/ts_src/psbt.ts#L1654
212
- // Like P2WPKH, the hash for deriving the meaningfulScript for a P2SH-P2WPKH transaction is its public key hash
213
- // It can be derived by hashing the provided public key in the witness stack
214
105
  const signingScript = bitcoin.payments.p2pkh({
215
106
  hash: hashedPubkey
216
107
  }).output;
217
- // Return computed transaction hash to be signed
218
108
  return toSignTx.extractTransaction().hashForWitnessV0(0, signingScript, 0, bitcoin.Transaction.SIGHASH_ALL);
219
109
  }
220
- /**
221
- * Compute the hash to be signed for a given P2TR BIP-322 toSign transaction.
222
- * @param toSignTx PSBT instance of the toSign transaction
223
- * @param hashType Hash type used to sign the toSign transaction, must be either 0x00 or 0x01
224
- * @returns Computed transaction hash that requires signing
225
- * @throws Error if hashType is anything other than 0x00 or 0x01
226
- */
227
110
  static getHashForSigP2TR(toSignTx, hashType) {
228
- // BIP-322 states that 'all signatures must use the SIGHASH_ALL flag'
229
- // But, in BIP-341, SIGHASH_DEFAULT (0x00) is equivalent to SIGHASH_ALL (0x01) so both should be allowed
230
111
  if (hashType !== bitcoin.Transaction.SIGHASH_DEFAULT && hashType !== bitcoin.Transaction.SIGHASH_ALL) {
231
- // Throw error if hashType is neither SIGHASH_DEFAULT or SIGHASH_ALL
232
112
  throw new Error('Invalid SIGHASH used in signature. Must be either SIGHASH_ALL or SIGHASH_DEFAULT.');
233
113
  }
234
- // Return computed transaction hash to be signed
235
114
  return toSignTx.extractTransaction().hashForWitnessV1(0, [toSignTx.data.inputs[0].witnessUtxo.script], [0], hashType);
236
115
  }
237
116
  }
238
- exports.default = Verifier;
117
+ export default Verifier;
239
118
  //# sourceMappingURL=Verifier.js.map
@@ -1,4 +1,3 @@
1
- /// <reference types="node" />
2
1
  interface ScriptSignature {
3
2
  signature: Buffer;
4
3
  hashType: number;
@@ -1,8 +1,4 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.decodeScriptSignature = void 0;
4
- // Taken from https://github.com/bitcoinjs/bitcoinjs-lib/blob/5d2ff1c61165932e2814d5f37630e6720168561c/ts_src/script_signature.ts#L29
5
- function decodeScriptSignature(buffer) {
1
+ export function decodeScriptSignature(buffer) {
6
2
  const hashType = buffer.readUInt8(buffer.length - 1);
7
3
  const hashTypeMod = hashType & ~0x80;
8
4
  if (hashTypeMod <= 0 || hashTypeMod >= 4)
@@ -13,7 +9,6 @@ function decodeScriptSignature(buffer) {
13
9
  const signature = Buffer.concat([r, s], 64);
14
10
  return { signature, hashType };
15
11
  }
16
- exports.decodeScriptSignature = decodeScriptSignature;
17
12
  function fromDER(x) {
18
13
  if (x[0] === 0x00)
19
14
  x = x.slice(1);
@@ -53,7 +48,6 @@ function decode2(buffer) {
53
48
  throw new Error('S value is negative');
54
49
  if (lenS > 1 && buffer[lenR + 6] === 0x00 && !(buffer[lenR + 7] & 0x80))
55
50
  throw new Error('S value excessively padded');
56
- // non-BIP66 - extract R, S values
57
51
  return {
58
52
  r: buffer.slice(4, 4 + lenR),
59
53
  s: buffer.slice(6 + lenR),
@@ -1,6 +1,3 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.decodeScriptSignature = void 0;
4
- const DecodeScriptSignature_1 = require("./DecodeScriptSignature");
5
- Object.defineProperty(exports, "decodeScriptSignature", { enumerable: true, get: function () { return DecodeScriptSignature_1.decodeScriptSignature; } });
1
+ import { decodeScriptSignature } from "./DecodeScriptSignature";
2
+ export { decodeScriptSignature };
6
3
  //# sourceMappingURL=index.js.map
@@ -1,58 +1,11 @@
1
- /// <reference types="node" />
2
- /**
3
- * Class that implement address-related utility functions.
4
- */
5
1
  declare class Address {
6
- /**
7
- * Check if a given Bitcoin address is a pay-to-public-key-hash (p2pkh) address.
8
- * @param address Bitcoin address to be checked
9
- * @returns True if the provided address correspond to a valid P2PKH address, false if otherwise
10
- */
11
2
  static isP2PKH(address: string): boolean;
12
- /**
13
- * Check if a given Bitcoin address is a pay-to-script-hash (P2SH) address.
14
- * @param address Bitcoin address to be checked
15
- * @returns True if the provided address correspond to a valid P2SH address, false if otherwise
16
- */
17
3
  static isP2SH(address: string): boolean;
18
- /**
19
- * Check if a given Bitcoin address is a pay-to-witness-public-key-hash (P2WPKH) address.
20
- * @param address Bitcoin address to be checked
21
- * @returns True if the provided address correspond to a valid P2WPKH address, false if otherwise
22
- */
23
4
  static isP2WPKH(address: string): boolean;
24
- /**
25
- * Check if a given Bitcoin address is a taproot address.
26
- * @param address Bitcoin address to be checked
27
- * @returns True if the provided address is a taproot address, false if otherwise
28
- */
29
5
  static isP2TR(address: string): boolean;
30
- /**
31
- * Check if a given witness stack corresponds to a P2WPKH address.
32
- * @param witness Witness data associated with the toSign BIP-322 transaction
33
- * @returns True if the provided witness stack correspond to a valid P2WPKH address, false if otherwise
34
- */
35
6
  static isP2WPKHWitness(witness: Buffer[]): boolean;
36
- /**
37
- * Check if a given witness stack corresponds to a single-key-spend P2TR address.
38
- * @param witness Witness data associated with the toSign BIP-322 transaction
39
- * @returns True if the provided address and witness stack correspond to a valid single-key-spend P2TR address, false if otherwise
40
- */
41
7
  static isSingleKeyP2TRWitness(witness: Buffer[]): boolean;
42
- /**
43
- * Convert a given Bitcoin address into its corresponding script public key.
44
- * Reference: https://github.com/buidl-bitcoin/buidl-python/blob/d79e9808e8ca60975d315be41293cb40d968626d/buidl/script.py#L607
45
- * @param address Bitcoin address
46
- * @returns Script public key of the given Bitcoin address
47
- * @throws Error when the provided address is not a valid Bitcoin address
48
- */
49
8
  static convertAdressToScriptPubkey(address: string): Buffer;
50
- /**
51
- * Convert a given public key into a corresponding Bitcoin address.
52
- * @param publicKey Public key for deriving the address, or internal public key for deriving taproot address
53
- * @param addressType Bitcoin address type to be derived, must be either 'p2pkh', 'p2sh-p2wpkh', 'p2wpkh', or 'p2tr'
54
- * @returns Bitcoin address that correspond to the given public key in both mainnet and testnet
55
- */
56
9
  static convertPubKeyIntoAddress(publicKey: Buffer, addressType: 'p2pkh' | 'p2sh-p2wpkh' | 'p2wpkh' | 'p2tr'): {
57
10
  mainnet: string;
58
11
  testnet: string;
@@ -1,106 +1,44 @@
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
- Object.defineProperty(exports, "__esModule", { value: true });
26
- // Import dependency
27
- const bitcoin = __importStar(require("@exodus/bitcoinjs-lib"));
28
- /**
29
- * Class that implement address-related utility functions.
30
- */
1
+ import * as bitcoin from '@exodus/bitcoinjs';
31
2
  class Address {
32
- /**
33
- * Check if a given Bitcoin address is a pay-to-public-key-hash (p2pkh) address.
34
- * @param address Bitcoin address to be checked
35
- * @returns True if the provided address correspond to a valid P2PKH address, false if otherwise
36
- */
37
3
  static isP2PKH(address) {
38
- // Check if the provided address is a P2PKH address
39
4
  if (address[0] === '1' || address[0] === 'm' || address[0] === 'n') {
40
- return true; // P2PKH address
5
+ return true;
41
6
  }
42
7
  else {
43
8
  return false;
44
9
  }
45
10
  }
46
- /**
47
- * Check if a given Bitcoin address is a pay-to-script-hash (P2SH) address.
48
- * @param address Bitcoin address to be checked
49
- * @returns True if the provided address correspond to a valid P2SH address, false if otherwise
50
- */
51
11
  static isP2SH(address) {
52
- // Check if the provided address is a P2SH address
53
12
  if (address[0] === '3' || address[0] === '2') {
54
- return true; // P2SH address
13
+ return true;
55
14
  }
56
15
  else {
57
16
  return false;
58
17
  }
59
18
  }
60
- /**
61
- * Check if a given Bitcoin address is a pay-to-witness-public-key-hash (P2WPKH) address.
62
- * @param address Bitcoin address to be checked
63
- * @returns True if the provided address correspond to a valid P2WPKH address, false if otherwise
64
- */
65
19
  static isP2WPKH(address) {
66
- // Check if the provided address is a P2WPKH/P2WSH address
67
20
  if (address.slice(0, 4) === 'bc1q' || address.slice(0, 4) === 'tb1q') {
68
- // Either a P2WPKH / P2WSH address
69
- // Convert the address into a scriptPubKey
70
21
  const scriptPubKey = this.convertAdressToScriptPubkey(address);
71
- // Check if the scriptPubKey is exactly 22 bytes since P2WPKH scriptPubKey should be 0014<20-BYTE-PUBKEY-HASH>
72
22
  if (scriptPubKey.byteLength === 22) {
73
- return true; // P2WPKH
23
+ return true;
74
24
  }
75
25
  else {
76
- return false; // Not P2WPKH, probably P2WSH
26
+ return false;
77
27
  }
78
28
  }
79
29
  else {
80
30
  return false;
81
31
  }
82
32
  }
83
- /**
84
- * Check if a given Bitcoin address is a taproot address.
85
- * @param address Bitcoin address to be checked
86
- * @returns True if the provided address is a taproot address, false if otherwise
87
- */
88
33
  static isP2TR(address) {
89
34
  if (address.slice(0, 4) === 'bc1p' || address.slice(0, 4) === 'tb1p') {
90
- return true; // P2TR address
35
+ return true;
91
36
  }
92
37
  else {
93
38
  return false;
94
39
  }
95
40
  }
96
- /**
97
- * Check if a given witness stack corresponds to a P2WPKH address.
98
- * @param witness Witness data associated with the toSign BIP-322 transaction
99
- * @returns True if the provided witness stack correspond to a valid P2WPKH address, false if otherwise
100
- */
101
41
  static isP2WPKHWitness(witness) {
102
- // Check whether the witness stack is as expected for a P2WPKH address
103
- // It should contain exactly two items, with the second item being a public key with 33 bytes, and the first byte must be either 0x02/0x03
104
42
  if (witness.length === 2 && witness[1].byteLength === 33 && (witness[1][0] === 0x02 || witness[1][0] === 0x03)) {
105
43
  return true;
106
44
  }
@@ -108,14 +46,7 @@ class Address {
108
46
  return false;
109
47
  }
110
48
  }
111
- /**
112
- * Check if a given witness stack corresponds to a single-key-spend P2TR address.
113
- * @param witness Witness data associated with the toSign BIP-322 transaction
114
- * @returns True if the provided address and witness stack correspond to a valid single-key-spend P2TR address, false if otherwise
115
- */
116
49
  static isSingleKeyP2TRWitness(witness) {
117
- // Check whether the witness stack is as expected for a single-key-spend taproot address
118
- // It should contain exactly one items which is the signature for the transaction
119
50
  if (witness.length === 1) {
120
51
  return true;
121
52
  }
@@ -123,39 +54,27 @@ class Address {
123
54
  return false;
124
55
  }
125
56
  }
126
- /**
127
- * Convert a given Bitcoin address into its corresponding script public key.
128
- * Reference: https://github.com/buidl-bitcoin/buidl-python/blob/d79e9808e8ca60975d315be41293cb40d968626d/buidl/script.py#L607
129
- * @param address Bitcoin address
130
- * @returns Script public key of the given Bitcoin address
131
- * @throws Error when the provided address is not a valid Bitcoin address
132
- */
133
57
  static convertAdressToScriptPubkey(address) {
134
58
  if (address[0] === '1' || address[0] === 'm' || address[0] === 'n') {
135
- // P2PKH address
136
59
  return bitcoin.payments.p2pkh({
137
60
  address: address,
138
61
  network: (address[0] === '1') ? bitcoin.networks.bitcoin : bitcoin.networks.testnet
139
62
  }).output;
140
63
  }
141
64
  else if (address[0] === '3' || address[0] === '2') {
142
- // P2SH address
143
65
  return bitcoin.payments.p2sh({
144
66
  address: address,
145
67
  network: (address[0] === '3') ? bitcoin.networks.bitcoin : bitcoin.networks.testnet
146
68
  }).output;
147
69
  }
148
70
  else if (address.slice(0, 4) === 'bc1q' || address.slice(0, 4) === 'tb1q') {
149
- // P2WPKH or P2WSH address
150
71
  if (address.length === 42) {
151
- // P2WPKH address
152
72
  return bitcoin.payments.p2wpkh({
153
73
  address: address,
154
74
  network: (address.slice(0, 4) === 'bc1q') ? bitcoin.networks.bitcoin : bitcoin.networks.testnet
155
75
  }).output;
156
76
  }
157
77
  else if (address.length === 62) {
158
- // P2WSH address
159
78
  return bitcoin.payments.p2wsh({
160
79
  address: address,
161
80
  network: (address.slice(0, 4) === 'bc1q') ? bitcoin.networks.bitcoin : bitcoin.networks.testnet
@@ -164,7 +83,6 @@ class Address {
164
83
  }
165
84
  else if (address.slice(0, 4) === 'bc1p' || address.slice(0, 4) === 'tb1p') {
166
85
  if (address.length === 62) {
167
- // P2TR address
168
86
  return bitcoin.payments.p2tr({
169
87
  address: address,
170
88
  network: (address.slice(0, 4) === 'bc1p') ? bitcoin.networks.bitcoin : bitcoin.networks.testnet
@@ -173,12 +91,6 @@ class Address {
173
91
  }
174
92
  throw new Error("Unknown address type");
175
93
  }
176
- /**
177
- * Convert a given public key into a corresponding Bitcoin address.
178
- * @param publicKey Public key for deriving the address, or internal public key for deriving taproot address
179
- * @param addressType Bitcoin address type to be derived, must be either 'p2pkh', 'p2sh-p2wpkh', 'p2wpkh', or 'p2tr'
180
- * @returns Bitcoin address that correspond to the given public key in both mainnet and testnet
181
- */
182
94
  static convertPubKeyIntoAddress(publicKey, addressType) {
183
95
  switch (addressType) {
184
96
  case 'p2pkh':
@@ -187,7 +99,6 @@ class Address {
187
99
  testnet: bitcoin.payments.p2pkh({ pubkey: publicKey, network: bitcoin.networks.testnet }).address
188
100
  };
189
101
  case 'p2sh-p2wpkh':
190
- // Reference: https://github.com/bitcoinjs/bitcoinjs-lib/blob/1a9119b53bcea4b83a6aa8b948f0e6370209b1b4/test/integration/addresses.spec.ts#L70
191
102
  return {
192
103
  mainnet: bitcoin.payments.p2sh({
193
104
  redeem: bitcoin.payments.p2wpkh({ pubkey: publicKey, network: bitcoin.networks.bitcoin }),
@@ -204,7 +115,6 @@ class Address {
204
115
  testnet: bitcoin.payments.p2wpkh({ pubkey: publicKey, network: bitcoin.networks.testnet }).address
205
116
  };
206
117
  case 'p2tr':
207
- // Convert full-length public key into internal public key if necessary
208
118
  const internalPubkey = publicKey.byteLength === 33 ? publicKey.subarray(1, 33) : publicKey;
209
119
  return {
210
120
  mainnet: bitcoin.payments.p2tr({ internalPubkey: internalPubkey, network: bitcoin.networks.bitcoin }).address,
@@ -215,5 +125,5 @@ class Address {
215
125
  }
216
126
  }
217
127
  }
218
- exports.default = Address;
128
+ export default Address;
219
129
  //# sourceMappingURL=Address.js.map
@@ -1,27 +1,6 @@
1
- /// <reference types="node" />
2
- /**
3
- * Class that implement BIP137-related utility functions.
4
- */
5
1
  declare class BIP137 {
6
- /**
7
- * Check if a given signature satisified the format of a BIP-137 signature.
8
- * @param signature Base64-encoded signature to be checked
9
- * @returns True if the provided signature correspond to a valid BIP-137 signature, false if otherwise
10
- */
11
2
  static isBIP137Signature(signature: string): boolean;
12
- /**
13
- * Derive the public key that signed a valid BIP-137 signature.
14
- * @param message Message signed by the signature
15
- * @param signature Base-64 encoded signature to be decoded
16
- * @returns Public key that signs the provided signature
17
- */
18
3
  static derivePubKey(message: string, signature: string): Buffer;
19
- /**
20
- * Decode a BIP-137 signature.
21
- * Function copied from bitcoinjs-message library.
22
- * @param signature BIP-137 signature to be decoded
23
- * @returns Decoded BIP-137 signature
24
- */
25
4
  private static decodeSignature;
26
5
  }
27
6
  export default BIP137;