@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/dist/Verifier.js CHANGED
@@ -1,239 +1,124 @@
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.js';
2
+ import { Address, BIP137 } from './helpers/index.js';
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/index.js';
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
- finalScriptWitness: Buffer.from(signatureBase64, 'base64')
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
+ 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);
134
- }
135
- else {
136
- throw new Error('Only P2WPKH, P2SH-P2WPKH, and single-key-spend P2TR BIP-322 verification is supported. Unsupported address is provided.');
61
+ return ecc.verifySchnorr(hashToSign, publicKey, signature);
137
62
  }
63
+ throw new Error('Only P2WPKH, P2SH-P2WPKH, and single-key-spend P2TR BIP-322 verification is supported. Unsupported address is provided.');
138
64
  }
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
65
  static verifyBIP137Signature(signerAddress, message, signatureBase64) {
149
- if (helpers_1.Address.isP2PKH(signerAddress)) {
66
+ if (Address.isP2PKH(signerAddress)) {
150
67
  return bitcoinMessage.verify(message, signerAddress, signatureBase64);
151
68
  }
152
- 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
162
- if (p2shAddressDerived.mainnet !== signerAddress && p2shAddressDerived.testnet !== signerAddress) {
163
- return false; // Derived address did not match with the claimed signing address
164
- }
165
- }
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
170
- if (p2wpkhAddressDerived.mainnet !== signerAddress && p2wpkhAddressDerived.testnet !== signerAddress) {
171
- return false; // Derived address did not match with the claimed signing address
172
- }
69
+ const publicKeySigned = BIP137.derivePubKey(message, signatureBase64);
70
+ const legacySigningAddress = Address.convertPubKeyIntoAddress(publicKeySigned, 'p2pkh').mainnet;
71
+ if (Address.isP2SH(signerAddress)) {
72
+ const p2shAddressDerived = Address.convertPubKeyIntoAddress(publicKeySigned, 'p2sh-p2wpkh');
73
+ if (p2shAddressDerived.mainnet !== signerAddress &&
74
+ p2shAddressDerived.testnet !== signerAddress) {
75
+ return false;
173
76
  }
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
178
- if (p2trAddressDerived.mainnet !== signerAddress && p2trAddressDerived.testnet !== signerAddress) {
179
- return false; // Derived address did not match with the claimed signing address
180
- }
77
+ }
78
+ else if (Address.isP2WPKH(signerAddress)) {
79
+ const p2wpkhAddressDerived = Address.convertPubKeyIntoAddress(publicKeySigned, 'p2wpkh');
80
+ if (p2wpkhAddressDerived.mainnet !== signerAddress &&
81
+ p2wpkhAddressDerived.testnet !== signerAddress) {
82
+ return false;
181
83
  }
182
- else {
183
- return false; // Unsupported address type
84
+ }
85
+ else if (Address.isP2TR(signerAddress)) {
86
+ const p2trAddressDerived = Address.convertPubKeyIntoAddress(publicKeySigned, 'p2tr');
87
+ if (p2trAddressDerived.mainnet !== signerAddress &&
88
+ p2trAddressDerived.testnet !== signerAddress) {
89
+ return false;
184
90
  }
185
- // Validate the signature using bitcoinjs-message if address assertion succeeded
186
- return bitcoinMessage.verify(message, legacySigningAddress, signatureBase64);
187
91
  }
92
+ else {
93
+ return false;
94
+ }
95
+ return bitcoinMessage.verify(message, legacySigningAddress, signatureBase64);
188
96
  }
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
97
  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
98
  const signingScript = bitcoin.payments.p2pkh({
198
- hash: toSignTx.data.inputs[0].witnessUtxo.script.subarray(2)
99
+ hash: toSignTx.data.inputs[0].witnessUtxo.script.subarray(2),
199
100
  }).output;
200
- // Return computed transaction hash to be signed
201
- return toSignTx.extractTransaction().hashForWitnessV0(0, signingScript, 0, bitcoin.Transaction.SIGHASH_ALL);
101
+ return toSignTx
102
+ .extractTransaction()
103
+ .hashForWitnessV0(0, signingScript, 0, bitcoin.Transaction.SIGHASH_ALL);
202
104
  }
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
105
  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
106
  const signingScript = bitcoin.payments.p2pkh({
215
- hash: hashedPubkey
107
+ hash: hashedPubkey,
216
108
  }).output;
217
- // Return computed transaction hash to be signed
218
- return toSignTx.extractTransaction().hashForWitnessV0(0, signingScript, 0, bitcoin.Transaction.SIGHASH_ALL);
109
+ return toSignTx
110
+ .extractTransaction()
111
+ .hashForWitnessV0(0, signingScript, 0, bitcoin.Transaction.SIGHASH_ALL);
219
112
  }
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
113
  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
- if (hashType !== bitcoin.Transaction.SIGHASH_DEFAULT && hashType !== bitcoin.Transaction.SIGHASH_ALL) {
231
- // Throw error if hashType is neither SIGHASH_DEFAULT or SIGHASH_ALL
114
+ if (hashType !== bitcoin.Transaction.SIGHASH_DEFAULT &&
115
+ hashType !== bitcoin.Transaction.SIGHASH_ALL) {
232
116
  throw new Error('Invalid SIGHASH used in signature. Must be either SIGHASH_ALL or SIGHASH_DEFAULT.');
233
117
  }
234
- // Return computed transaction hash to be signed
235
- return toSignTx.extractTransaction().hashForWitnessV1(0, [toSignTx.data.inputs[0].witnessUtxo.script], [0], hashType);
118
+ return toSignTx
119
+ .extractTransaction()
120
+ .hashForWitnessV1(0, [toSignTx.data.inputs[0].witnessUtxo.script], [0], hashType);
236
121
  }
237
122
  }
238
- exports.default = Verifier;
123
+ export default Verifier;
239
124
  //# sourceMappingURL=Verifier.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"Verifier.js","sourceRoot":"","sources":["../src/Verifier.ts"],"names":[],"mappings":"AACA,OAAO,MAAM,MAAM,aAAa,CAAA;AAChC,OAAO,EAAE,OAAO,EAAE,MAAM,EAAE,MAAM,oBAAoB,CAAA;AACpD,OAAO,KAAK,OAAO,MAAM,mBAAmB,CAAA;AAC5C,OAAO,GAA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EAAE,OAAO,CAAC,WAAW,CAAC,WAAW,CAAC,CAAA;IAC5E,CAAC;IAQO,MAAM,CAAC,yBAAyB,CAAC,QAAsB,EAAE,YAAoB;QAKnF,MAAM,aAAa,GAAG,OAAO,CAAC,QAAQ,CAAC,KAAK,CAAC;YAC3C,IAAI,EAAE,YAAY;SACnB,CAAC,CAAC,MAAM,CAAA;QAET,OAAO,QAAQ;aACZ,kBAAkB,EAAE;aACpB,gBAAgB,CAAC,CAAC,EAAE,aAAc,EAAE,CAAC,EAAE,OAAO,CAAC,WAAW,CAAC,WAAW,CAAC,CAAA;IAC5E,CAAC;IASO,MAAM,CAAC,iBAAiB,CAAC,QAAsB,EAAE,QAAgB;QAGvE,IACE,QAAQ,KAAK,OAAO,CAAC,WAAW,CAAC,eAAe;YAChD,QAAQ,KAAK,OAAO,CAAC,WAAW,CAAC,WAAW,EAC5C,CAAC;YAED,MAAM,IAAI,KAAK,CACb,mFAAmF,CACpF,CAAA;QACH,CAAC;QAGD,OAAO,QAAQ;aACZ,kBAAkB,EAAE;aACpB,gBAAgB,CAAC,CAAC,EAAE,CAAC,QAAQ,CAAC,IAAI,CAAC,MAAM,CAAC,CAAC,CAAE,CAAC,WAAY,CAAC,MAAM,CAAC,EAAE,CAAC,CAAC,CAAC,EAAE,QAAQ,CAAC,CAAA;IACvF,CAAC;CACF;AAED,eAAe,QAAQ,CAAA"}
@@ -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),
@@ -0,0 +1 @@
1
+ {"version":3,"file":"DecodeScriptSignature.js","sourceRoot":"","sources":["../../src/bitcoinjs/DecodeScriptSignature.ts"],"names":[],"mappings":"AAMA,MAAM,UAAU,qBAAqB,CAAC,MAAc;IAClD,MAAM,QAAQ,GAAG,MAAM,CAAC,SAAS,CAAC,MAAM,CAAC,MAAM,GAAG,CAAC,CAAC,CAAA;IACpD,MAAM,WAAW,GAAG,QAAQ,GAAG,CAAC,IAAI,CAAA;IACpC,IAAI,WAAW,IAAI,CAAC,IAAI,WAAW,IAAI,CAAC;QAAE,MAAM,IAAI,KAAK,CAAC,mBAAmB,GAAG,QAAQ,CAAC,CAAA;IAEzF,MAAM,OAAO,GAAG,OAAO,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,CAAC,CAAA;IAC5C,MAAM,CAAC,GAAG,OAAO,CAAC,OAAO,CAAC,CAAC,CAAC,CAAA;IAC5B,MAAM,CAAC,GAAG,OAAO,CAAC,OAAO,CAAC,CAAC,CAAC,CAAA;IAC5B,MAAM,SAAS,GAAG,MAAM,CAAC,MAAM,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,EAAE,EAAE,CAAC,CAAA;IAE3C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2
- export { decodeScriptSignature };
1
+ export { decodeScriptSignature } from './DecodeScriptSignature.js';
@@ -1,6 +1,2 @@
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
+ export { decodeScriptSignature } from './DecodeScriptSignature.js';
6
2
  //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/bitcoinjs/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,qBAAqB,EAAE,MAAM,4BAA4B,CAAA"}
@@ -1,61 +1,14 @@
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
- mainnet: string;
58
- testnet: string;
10
+ mainnet: string | undefined;
11
+ testnet: string | undefined;
59
12
  };
60
13
  }
61
14
  export default Address;