@btc-vision/bitcoin 7.0.0-alpha.0 → 7.0.0-alpha.10
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 +455 -155
- package/browser/address.d.ts +6 -2
- package/browser/address.d.ts.map +1 -1
- package/browser/block.d.ts.map +1 -1
- package/browser/branded.d.ts +3 -14
- package/browser/branded.d.ts.map +1 -1
- package/browser/chunks/psbt-parallel-BBFlkmiv.js +10717 -0
- package/browser/crypto.d.ts +1 -1
- package/browser/ecc/context.d.ts +25 -24
- package/browser/ecc/context.d.ts.map +1 -1
- package/browser/ecc/index.d.ts +1 -1
- package/browser/ecc/index.d.ts.map +1 -1
- package/browser/ecc/types.d.ts +10 -123
- package/browser/ecc/types.d.ts.map +1 -1
- package/browser/env.d.ts +13 -0
- package/browser/env.d.ts.map +1 -0
- package/browser/index.d.ts +7 -7
- package/browser/index.d.ts.map +1 -1
- package/browser/index.js +2497 -11686
- package/browser/io/BinaryReader.d.ts +15 -15
- package/browser/io/BinaryReader.d.ts.map +1 -1
- package/browser/io/BinaryWriter.d.ts +17 -17
- package/browser/io/BinaryWriter.d.ts.map +1 -1
- package/browser/io/hex.d.ts.map +1 -1
- package/browser/io/index.d.ts +0 -1
- package/browser/io/index.d.ts.map +1 -1
- package/browser/opcodes.d.ts +11 -0
- package/browser/opcodes.d.ts.map +1 -1
- package/browser/payments/bip341.d.ts +1 -1
- package/browser/payments/bip341.d.ts.map +1 -1
- package/browser/payments/embed.d.ts +1 -1
- package/browser/payments/embed.d.ts.map +1 -1
- package/browser/payments/p2ms.d.ts.map +1 -1
- package/browser/payments/p2op.d.ts +1 -1
- package/browser/payments/p2op.d.ts.map +1 -1
- package/browser/payments/p2pk.d.ts +1 -1
- package/browser/payments/p2pk.d.ts.map +1 -1
- package/browser/payments/p2pkh.d.ts +1 -1
- package/browser/payments/p2pkh.d.ts.map +1 -1
- package/browser/payments/p2sh.d.ts.map +1 -1
- package/browser/payments/p2tr.d.ts +2 -2
- package/browser/payments/p2tr.d.ts.map +1 -1
- package/browser/payments/p2wpkh.d.ts +1 -1
- package/browser/payments/p2wpkh.d.ts.map +1 -1
- package/browser/payments/p2wsh.d.ts.map +1 -1
- package/browser/payments/types.d.ts +1 -1
- package/browser/payments/types.d.ts.map +1 -1
- package/browser/psbt/PsbtCache.d.ts +54 -0
- package/browser/psbt/PsbtCache.d.ts.map +1 -0
- package/browser/psbt/PsbtFinalizer.d.ts +21 -0
- package/browser/psbt/PsbtFinalizer.d.ts.map +1 -0
- package/browser/psbt/PsbtSigner.d.ts +32 -0
- package/browser/psbt/PsbtSigner.d.ts.map +1 -0
- package/browser/psbt/PsbtTransaction.d.ts +25 -0
- package/browser/psbt/PsbtTransaction.d.ts.map +1 -0
- package/browser/psbt/bip371.d.ts.map +1 -1
- package/browser/psbt/types.d.ts +5 -71
- package/browser/psbt/types.d.ts.map +1 -1
- package/browser/psbt/validation.d.ts +1 -1
- package/browser/psbt/validation.d.ts.map +1 -1
- package/browser/psbt.d.ts +26 -40
- package/browser/psbt.d.ts.map +1 -1
- package/browser/script.d.ts.map +1 -1
- package/browser/transaction.d.ts +4 -4
- package/browser/transaction.d.ts.map +1 -1
- package/browser/types.d.ts +5 -3
- package/browser/types.d.ts.map +1 -1
- package/browser/workers/WorkerSigningPool.d.ts +24 -17
- package/browser/workers/WorkerSigningPool.d.ts.map +1 -1
- package/browser/workers/WorkerSigningPool.node.d.ts +19 -12
- package/browser/workers/WorkerSigningPool.node.d.ts.map +1 -1
- package/browser/workers/WorkerSigningPool.sequential.d.ts +67 -0
- package/browser/workers/WorkerSigningPool.sequential.d.ts.map +1 -0
- package/browser/workers/WorkerSigningPool.worklet.d.ts +64 -0
- package/browser/workers/WorkerSigningPool.worklet.d.ts.map +1 -0
- package/browser/workers/index.browser.d.ts +16 -0
- package/browser/workers/index.browser.d.ts.map +1 -0
- package/browser/workers/index.d.ts +4 -64
- package/browser/workers/index.d.ts.map +1 -1
- package/browser/workers/index.js +28 -0
- package/browser/workers/index.node.d.ts +17 -0
- package/browser/workers/index.node.d.ts.map +1 -0
- package/browser/workers/index.react-native.d.ts +28 -0
- package/browser/workers/index.react-native.d.ts.map +1 -0
- package/browser/workers/index.shared.d.ts +15 -0
- package/browser/workers/index.shared.d.ts.map +1 -0
- package/browser/workers/psbt-parallel.d.ts +2 -3
- package/browser/workers/psbt-parallel.d.ts.map +1 -1
- package/browser/workers/types.d.ts +17 -0
- package/browser/workers/types.d.ts.map +1 -1
- package/build/address.d.ts +6 -2
- package/build/address.d.ts.map +1 -1
- package/build/address.js +32 -19
- package/build/address.js.map +1 -1
- package/build/bech32utils.js.map +1 -1
- package/build/block.d.ts.map +1 -1
- package/build/block.js +2 -4
- package/build/block.js.map +1 -1
- package/build/branded.d.ts +3 -14
- package/build/branded.d.ts.map +1 -1
- package/build/branded.js +0 -5
- package/build/branded.js.map +1 -1
- package/build/crypto.d.ts +1 -1
- package/build/ecc/context.d.ts +25 -24
- package/build/ecc/context.d.ts.map +1 -1
- package/build/ecc/context.js +29 -101
- package/build/ecc/context.js.map +1 -1
- package/build/ecc/index.d.ts +1 -1
- package/build/ecc/index.d.ts.map +1 -1
- package/build/ecc/types.d.ts +7 -126
- package/build/ecc/types.d.ts.map +1 -1
- package/build/ecc/types.js +4 -1
- package/build/ecc/types.js.map +1 -1
- package/build/env.d.ts +13 -0
- package/build/env.d.ts.map +1 -0
- package/build/env.js +198 -0
- package/build/env.js.map +1 -0
- package/build/index.d.ts +8 -7
- package/build/index.d.ts.map +1 -1
- package/build/index.js +9 -7
- package/build/index.js.map +1 -1
- package/build/io/BinaryReader.d.ts +15 -15
- package/build/io/BinaryReader.d.ts.map +1 -1
- package/build/io/BinaryReader.js +17 -17
- package/build/io/BinaryReader.js.map +1 -1
- package/build/io/BinaryWriter.d.ts +17 -17
- package/build/io/BinaryWriter.d.ts.map +1 -1
- package/build/io/BinaryWriter.js +39 -39
- package/build/io/BinaryWriter.js.map +1 -1
- package/build/io/hex.d.ts.map +1 -1
- package/build/io/hex.js +2 -1
- package/build/io/hex.js.map +1 -1
- package/build/io/index.d.ts +0 -1
- package/build/io/index.d.ts.map +1 -1
- package/build/io/index.js +0 -2
- package/build/io/index.js.map +1 -1
- package/build/opcodes.d.ts +11 -0
- package/build/opcodes.d.ts.map +1 -1
- package/build/opcodes.js +19 -4
- package/build/opcodes.js.map +1 -1
- package/build/payments/bip341.d.ts +1 -2
- package/build/payments/bip341.d.ts.map +1 -1
- package/build/payments/bip341.js +1 -2
- package/build/payments/bip341.js.map +1 -1
- package/build/payments/embed.d.ts +1 -1
- package/build/payments/embed.d.ts.map +1 -1
- package/build/payments/embed.js +14 -14
- package/build/payments/embed.js.map +1 -1
- package/build/payments/p2ms.d.ts.map +1 -1
- package/build/payments/p2ms.js +21 -21
- package/build/payments/p2ms.js.map +1 -1
- package/build/payments/p2op.d.ts +1 -1
- package/build/payments/p2op.d.ts.map +1 -1
- package/build/payments/p2op.js +18 -18
- package/build/payments/p2op.js.map +1 -1
- package/build/payments/p2pk.d.ts +1 -1
- package/build/payments/p2pk.d.ts.map +1 -1
- package/build/payments/p2pk.js +17 -17
- package/build/payments/p2pk.js.map +1 -1
- package/build/payments/p2pkh.d.ts +1 -1
- package/build/payments/p2pkh.d.ts.map +1 -1
- package/build/payments/p2pkh.js +20 -20
- package/build/payments/p2pkh.js.map +1 -1
- package/build/payments/p2sh.d.ts.map +1 -1
- package/build/payments/p2sh.js +22 -20
- package/build/payments/p2sh.js.map +1 -1
- package/build/payments/p2tr.d.ts +2 -2
- package/build/payments/p2tr.d.ts.map +1 -1
- package/build/payments/p2tr.js +25 -26
- package/build/payments/p2tr.js.map +1 -1
- package/build/payments/p2wpkh.d.ts +1 -1
- package/build/payments/p2wpkh.d.ts.map +1 -1
- package/build/payments/p2wpkh.js +20 -20
- package/build/payments/p2wpkh.js.map +1 -1
- package/build/payments/p2wsh.d.ts.map +1 -1
- package/build/payments/p2wsh.js +22 -22
- package/build/payments/p2wsh.js.map +1 -1
- package/build/payments/types.d.ts +1 -1
- package/build/payments/types.d.ts.map +1 -1
- package/build/psbt/PsbtCache.d.ts +54 -0
- package/build/psbt/PsbtCache.d.ts.map +1 -0
- package/build/psbt/PsbtCache.js +249 -0
- package/build/psbt/PsbtCache.js.map +1 -0
- package/build/psbt/PsbtFinalizer.d.ts +21 -0
- package/build/psbt/PsbtFinalizer.d.ts.map +1 -0
- package/build/psbt/PsbtFinalizer.js +157 -0
- package/build/psbt/PsbtFinalizer.js.map +1 -0
- package/build/psbt/PsbtSigner.d.ts +32 -0
- package/build/psbt/PsbtSigner.d.ts.map +1 -0
- package/build/psbt/PsbtSigner.js +192 -0
- package/build/psbt/PsbtSigner.js.map +1 -0
- package/build/psbt/PsbtTransaction.d.ts +25 -0
- package/build/psbt/PsbtTransaction.d.ts.map +1 -0
- package/build/psbt/PsbtTransaction.js +61 -0
- package/build/psbt/PsbtTransaction.js.map +1 -0
- package/build/psbt/bip371.d.ts.map +1 -1
- package/build/psbt/bip371.js +6 -2
- package/build/psbt/bip371.js.map +1 -1
- package/build/psbt/psbtutils.js +1 -1
- package/build/psbt/psbtutils.js.map +1 -1
- package/build/psbt/types.d.ts +5 -71
- package/build/psbt/types.d.ts.map +1 -1
- package/build/psbt/validation.d.ts +1 -1
- package/build/psbt/validation.d.ts.map +1 -1
- package/build/psbt/validation.js +1 -1
- package/build/psbt/validation.js.map +1 -1
- package/build/psbt.d.ts +26 -40
- package/build/psbt.d.ts.map +1 -1
- package/build/psbt.js +180 -808
- package/build/psbt.js.map +1 -1
- package/build/script.d.ts.map +1 -1
- package/build/script.js +4 -4
- package/build/script.js.map +1 -1
- package/build/transaction.d.ts +4 -4
- package/build/transaction.d.ts.map +1 -1
- package/build/transaction.js +6 -5
- package/build/transaction.js.map +1 -1
- package/build/tsconfig.build.tsbuildinfo +1 -1
- package/build/types.d.ts +5 -3
- package/build/types.d.ts.map +1 -1
- package/build/types.js +14 -25
- package/build/types.js.map +1 -1
- package/build/workers/WorkerSigningPool.d.ts +24 -17
- package/build/workers/WorkerSigningPool.d.ts.map +1 -1
- package/build/workers/WorkerSigningPool.js +36 -25
- package/build/workers/WorkerSigningPool.js.map +1 -1
- package/build/workers/WorkerSigningPool.node.d.ts +19 -12
- package/build/workers/WorkerSigningPool.node.d.ts.map +1 -1
- package/build/workers/WorkerSigningPool.node.js +60 -28
- package/build/workers/WorkerSigningPool.node.js.map +1 -1
- package/build/workers/WorkerSigningPool.sequential.d.ts +76 -0
- package/build/workers/WorkerSigningPool.sequential.d.ts.map +1 -0
- package/build/workers/WorkerSigningPool.sequential.js +160 -0
- package/build/workers/WorkerSigningPool.sequential.js.map +1 -0
- package/build/workers/WorkerSigningPool.worklet.d.ts +79 -0
- package/build/workers/WorkerSigningPool.worklet.d.ts.map +1 -0
- package/build/workers/WorkerSigningPool.worklet.js +390 -0
- package/build/workers/WorkerSigningPool.worklet.js.map +1 -0
- package/build/workers/index.browser.d.ts +24 -0
- package/build/workers/index.browser.d.ts.map +1 -0
- package/build/workers/index.browser.js +30 -0
- package/build/workers/index.browser.js.map +1 -0
- package/build/workers/index.d.ts +6 -18
- package/build/workers/index.d.ts.map +1 -1
- package/build/workers/index.js +12 -14
- package/build/workers/index.js.map +1 -1
- package/build/workers/index.node.d.ts +38 -0
- package/build/workers/index.node.d.ts.map +1 -0
- package/build/workers/index.node.js +45 -0
- package/build/workers/index.node.js.map +1 -0
- package/build/workers/index.react-native.d.ts +28 -0
- package/build/workers/index.react-native.d.ts.map +1 -0
- package/build/workers/index.react-native.js +67 -0
- package/build/workers/index.react-native.js.map +1 -0
- package/build/workers/index.shared.d.ts +15 -0
- package/build/workers/index.shared.d.ts.map +1 -0
- package/build/workers/index.shared.js +20 -0
- package/build/workers/index.shared.js.map +1 -0
- package/build/workers/psbt-parallel.d.ts +2 -3
- package/build/workers/psbt-parallel.d.ts.map +1 -1
- package/build/workers/psbt-parallel.js +4 -4
- package/build/workers/psbt-parallel.js.map +1 -1
- package/build/workers/types.d.ts +17 -0
- package/build/workers/types.d.ts.map +1 -1
- package/build/workers/types.js.map +1 -1
- package/package.json +48 -9
- package/src/address.ts +53 -21
- package/src/bech32utils.ts +3 -3
- package/src/block.ts +17 -10
- package/src/branded.ts +15 -13
- package/src/crypto.ts +1 -1
- package/src/ecc/context.ts +36 -136
- package/src/ecc/index.ts +2 -2
- package/src/ecc/types.ts +7 -145
- package/src/env.ts +239 -0
- package/src/index.ts +57 -22
- package/src/io/BinaryReader.ts +18 -18
- package/src/io/BinaryWriter.ts +43 -43
- package/src/io/hex.ts +2 -1
- package/src/io/index.ts +0 -3
- package/src/opcodes.ts +21 -4
- package/src/payments/bip341.ts +5 -7
- package/src/payments/embed.ts +19 -19
- package/src/payments/p2ms.ts +34 -27
- package/src/payments/p2op.ts +22 -22
- package/src/payments/p2pk.ts +22 -22
- package/src/payments/p2pkh.ts +28 -28
- package/src/payments/p2sh.ts +33 -30
- package/src/payments/p2tr.ts +40 -40
- package/src/payments/p2wpkh.ts +30 -30
- package/src/payments/p2wsh.ts +29 -29
- package/src/payments/types.ts +1 -1
- package/src/psbt/PsbtCache.ts +325 -0
- package/src/psbt/PsbtFinalizer.ts +213 -0
- package/src/psbt/PsbtSigner.ts +302 -0
- package/src/psbt/PsbtTransaction.ts +82 -0
- package/src/psbt/bip371.ts +7 -3
- package/src/psbt/psbtutils.ts +1 -1
- package/src/psbt/types.ts +5 -94
- package/src/psbt/validation.ts +5 -12
- package/src/psbt.ts +376 -1201
- package/src/script.ts +6 -9
- package/src/transaction.ts +19 -15
- package/src/types.ts +33 -45
- package/src/workers/WorkerSigningPool.node.ts +72 -36
- package/src/workers/WorkerSigningPool.sequential.ts +191 -0
- package/src/workers/WorkerSigningPool.ts +48 -39
- package/src/workers/WorkerSigningPool.worklet.ts +522 -0
- package/src/workers/index.browser.ts +34 -0
- package/src/workers/index.node.ts +50 -0
- package/src/workers/index.react-native.ts +110 -0
- package/src/workers/index.shared.ts +58 -0
- package/src/workers/index.ts +14 -65
- package/src/workers/psbt-parallel.ts +8 -13
- package/src/workers/types.ts +26 -1
- package/test/address.spec.ts +2 -2
- package/test/bitcoin.core.spec.ts +5 -2
- package/test/browser/payments.spec.ts +151 -0
- package/test/browser/psbt.spec.ts +1510 -0
- package/test/browser/script.spec.ts +223 -0
- package/test/browser/setup.ts +13 -0
- package/test/browser/workers-signing.spec.ts +537 -0
- package/test/crypto.spec.ts +2 -2
- package/test/env.spec.ts +418 -0
- package/test/fixtures/core/base58_encode_decode.json +12 -48
- package/test/fixtures/core/base58_keys_invalid.json +50 -150
- package/test/fixtures/core/sighash.json +1 -3
- package/test/fixtures/core/tx_valid.json +133 -501
- package/test/fixtures/embed.json +3 -11
- package/test/fixtures/p2ms.json +21 -91
- package/test/fixtures/p2pk.json +5 -24
- package/test/fixtures/p2pkh.json +7 -36
- package/test/fixtures/p2sh.json +8 -54
- package/test/fixtures/p2tr.json +2 -6
- package/test/fixtures/p2wpkh.json +7 -36
- package/test/fixtures/p2wsh.json +14 -59
- package/test/fixtures/psbt.json +2 -6
- package/test/fixtures/script.json +12 -48
- package/test/integration/addresses.spec.ts +11 -5
- package/test/integration/bip32.spec.ts +1 -1
- package/test/integration/cltv.spec.ts +10 -6
- package/test/integration/csv.spec.ts +10 -9
- package/test/integration/payments.spec.ts +8 -4
- package/test/integration/taproot.spec.ts +26 -6
- package/test/integration/transactions.spec.ts +22 -8
- package/test/payments.spec.ts +1 -1
- package/test/payments.utils.ts +1 -1
- package/test/psbt.spec.ts +250 -64
- package/test/script_signature.spec.ts +1 -1
- package/test/transaction.spec.ts +18 -5
- package/test/tsconfig.json +6 -20
- package/test/workers-pool.spec.ts +65 -23
- package/test/workers-sequential.spec.ts +669 -0
- package/test/workers-signing.spec.ts +7 -3
- package/test/workers-worklet.spec.ts +500 -0
- package/test/workers.spec.ts +6 -7
- package/typedoc.json +39 -0
- package/vite.config.browser.ts +31 -6
- package/vitest.config.browser.ts +68 -0
- package/browser/ecpair.d.ts +0 -99
- package/browser/io/MemoryPool.d.ts +0 -220
- package/browser/io/MemoryPool.d.ts.map +0 -1
- package/build/io/MemoryPool.d.ts +0 -220
- package/build/io/MemoryPool.d.ts.map +0 -1
- package/build/io/MemoryPool.js +0 -309
- package/build/io/MemoryPool.js.map +0 -1
- package/src/ecpair.d.ts +0 -99
- package/src/io/MemoryPool.ts +0 -343
- package/test/taproot-cache.spec.ts +0 -694
|
@@ -0,0 +1,302 @@
|
|
|
1
|
+
import type { Bip32Derivation, PsbtInput } from 'bip174';
|
|
2
|
+
import { checkForInput } from 'bip174';
|
|
3
|
+
import { equals, toHex } from '../io/index.js';
|
|
4
|
+
import * as payments from '../payments/index.js';
|
|
5
|
+
import { tapleafHash } from '../payments/bip341.js';
|
|
6
|
+
import { toXOnly } from '../pubkey.js';
|
|
7
|
+
import { isP2WPKH, pubkeyInScript } from './psbtutils.js';
|
|
8
|
+
import { getMeaningfulScript, sighashTypeToString } from './utils.js';
|
|
9
|
+
import { checkScriptForPubkey } from './validation.js';
|
|
10
|
+
import type { Output } from '../transaction.js';
|
|
11
|
+
import { Transaction } from '../transaction.js';
|
|
12
|
+
import type { Bytes20, Bytes32, MessageHash, PublicKey, Satoshi, Script } from '../types.js';
|
|
13
|
+
import type { PsbtCache } from './PsbtCache.js';
|
|
14
|
+
import type { HDSigner, HDSignerAsync } from './types.js';
|
|
15
|
+
|
|
16
|
+
export interface HashForSig {
|
|
17
|
+
readonly pubkey: PublicKey;
|
|
18
|
+
readonly hash: MessageHash;
|
|
19
|
+
readonly leafHash?: Bytes32;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* Class wrapping all signing-related logic for PSBT.
|
|
24
|
+
*/
|
|
25
|
+
export class PsbtSigner {
|
|
26
|
+
readonly #cache: PsbtCache;
|
|
27
|
+
readonly #txFromBuffer: (buf: Uint8Array) => Transaction;
|
|
28
|
+
|
|
29
|
+
public constructor(cache: PsbtCache, txFromBuffer: (buf: Uint8Array) => Transaction) {
|
|
30
|
+
this.#cache = cache;
|
|
31
|
+
this.#txFromBuffer = txFromBuffer;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
public getHashAndSighashType(
|
|
35
|
+
inputs: PsbtInput[],
|
|
36
|
+
inputIndex: number,
|
|
37
|
+
pubkey: Uint8Array,
|
|
38
|
+
sighashTypes: number[],
|
|
39
|
+
): { hash: MessageHash; sighashType: number } {
|
|
40
|
+
const input = checkForInput(inputs, inputIndex);
|
|
41
|
+
const { hash, sighashType, script } = this.getHashForSig(
|
|
42
|
+
inputIndex,
|
|
43
|
+
input,
|
|
44
|
+
false,
|
|
45
|
+
sighashTypes,
|
|
46
|
+
);
|
|
47
|
+
|
|
48
|
+
checkScriptForPubkey(pubkey as PublicKey, script, 'sign');
|
|
49
|
+
return { hash, sighashType };
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
public getHashForSig(
|
|
53
|
+
inputIndex: number,
|
|
54
|
+
input: PsbtInput,
|
|
55
|
+
forValidate: boolean,
|
|
56
|
+
sighashTypes?: number[],
|
|
57
|
+
): { script: Script; hash: MessageHash; sighashType: number } {
|
|
58
|
+
const unsignedTx = this.#cache.tx;
|
|
59
|
+
const sighashType = input.sighashType || Transaction.SIGHASH_ALL;
|
|
60
|
+
checkSighashTypeAllowed(sighashType, sighashTypes);
|
|
61
|
+
|
|
62
|
+
let hash: MessageHash;
|
|
63
|
+
let prevout: Output;
|
|
64
|
+
|
|
65
|
+
if (input.nonWitnessUtxo) {
|
|
66
|
+
const nonWitnessUtxoTx = this.#cache.getNonWitnessUtxoTx(
|
|
67
|
+
input,
|
|
68
|
+
inputIndex,
|
|
69
|
+
this.#txFromBuffer,
|
|
70
|
+
);
|
|
71
|
+
|
|
72
|
+
const prevoutHash = unsignedTx.ins[inputIndex]!.hash;
|
|
73
|
+
const utxoHash = nonWitnessUtxoTx.getHash();
|
|
74
|
+
|
|
75
|
+
if (!equals(prevoutHash, utxoHash)) {
|
|
76
|
+
throw new Error(
|
|
77
|
+
`Non-witness UTXO hash for input #${inputIndex} doesn't match the hash specified in the prevout`,
|
|
78
|
+
);
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
const prevoutIndex = unsignedTx.ins[inputIndex]!.index;
|
|
82
|
+
prevout = nonWitnessUtxoTx.outs[prevoutIndex]!;
|
|
83
|
+
} else if (input.witnessUtxo) {
|
|
84
|
+
prevout = {
|
|
85
|
+
script: input.witnessUtxo.script as Script,
|
|
86
|
+
value: input.witnessUtxo.value as Satoshi,
|
|
87
|
+
};
|
|
88
|
+
} else {
|
|
89
|
+
throw new Error('Need a Utxo input item for signing');
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
const { meaningfulScript, type } = getMeaningfulScript(
|
|
93
|
+
prevout.script,
|
|
94
|
+
inputIndex,
|
|
95
|
+
'input',
|
|
96
|
+
input.redeemScript,
|
|
97
|
+
input.witnessScript,
|
|
98
|
+
);
|
|
99
|
+
|
|
100
|
+
const script = meaningfulScript as Script;
|
|
101
|
+
|
|
102
|
+
if (['p2sh-p2wsh', 'p2wsh'].includes(type)) {
|
|
103
|
+
hash = unsignedTx.hashForWitnessV0(inputIndex, script, prevout.value, sighashType);
|
|
104
|
+
} else if (isP2WPKH(meaningfulScript)) {
|
|
105
|
+
const p2pkhPayment = payments.p2pkh({
|
|
106
|
+
hash: meaningfulScript.subarray(2) as Bytes20,
|
|
107
|
+
});
|
|
108
|
+
if (!p2pkhPayment.output) throw new Error('Unable to create signing script');
|
|
109
|
+
hash = unsignedTx.hashForWitnessV0(
|
|
110
|
+
inputIndex,
|
|
111
|
+
p2pkhPayment.output,
|
|
112
|
+
prevout.value,
|
|
113
|
+
sighashType,
|
|
114
|
+
);
|
|
115
|
+
} else {
|
|
116
|
+
// non-segwit
|
|
117
|
+
if (input.nonWitnessUtxo === undefined && !this.#cache.unsafeSignNonSegwit)
|
|
118
|
+
throw new Error(
|
|
119
|
+
`Input #${inputIndex} has witnessUtxo but non-segwit script: ` +
|
|
120
|
+
toHex(meaningfulScript),
|
|
121
|
+
);
|
|
122
|
+
if (!forValidate && this.#cache.unsafeSignNonSegwit)
|
|
123
|
+
console.warn(
|
|
124
|
+
'Warning: Signing non-segwit inputs without the full parent transaction ' +
|
|
125
|
+
'means there is a chance that a miner could feed you incorrect information ' +
|
|
126
|
+
"to trick you into paying large fees. This behavior is the same as Psbt's predecessor " +
|
|
127
|
+
'(TransactionBuilder - now removed) when signing non-segwit scripts. You are not ' +
|
|
128
|
+
'able to export this Psbt with toBuffer|toBase64|toHex since it is not ' +
|
|
129
|
+
'BIP174 compliant.\n*********************\nPROCEED WITH CAUTION!\n' +
|
|
130
|
+
'*********************',
|
|
131
|
+
);
|
|
132
|
+
hash = unsignedTx.hashForSignature(inputIndex, script, sighashType);
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
return { script, sighashType, hash };
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
public getTaprootHashesForSig(
|
|
139
|
+
inputIndex: number,
|
|
140
|
+
input: PsbtInput,
|
|
141
|
+
inputs: PsbtInput[],
|
|
142
|
+
pubkey: Uint8Array,
|
|
143
|
+
tapLeafHashToSign?: Uint8Array,
|
|
144
|
+
allowedSighashTypes?: number[],
|
|
145
|
+
): HashForSig[] {
|
|
146
|
+
const unsignedTx = this.#cache.tx;
|
|
147
|
+
const sighashType = input.sighashType || Transaction.SIGHASH_DEFAULT;
|
|
148
|
+
checkSighashTypeAllowed(sighashType, allowedSighashTypes);
|
|
149
|
+
|
|
150
|
+
if (!this.#cache.prevOuts) {
|
|
151
|
+
const prevOuts = inputs.map((i, index) =>
|
|
152
|
+
this.#cache.getScriptAndAmountFromUtxo(index, i, this.#txFromBuffer),
|
|
153
|
+
);
|
|
154
|
+
this.#cache.prevOuts = prevOuts;
|
|
155
|
+
this.#cache.signingScripts = prevOuts.map((o) => o.script);
|
|
156
|
+
this.#cache.values = prevOuts.map((o) => o.value);
|
|
157
|
+
}
|
|
158
|
+
const signingScripts = this.#cache.signingScripts as readonly Script[];
|
|
159
|
+
const values = this.#cache.values as readonly Satoshi[];
|
|
160
|
+
|
|
161
|
+
if (!this.#cache.taprootHashCache) {
|
|
162
|
+
this.#cache.taprootHashCache = unsignedTx.getTaprootHashCache(signingScripts, values);
|
|
163
|
+
}
|
|
164
|
+
const taprootCache = this.#cache.taprootHashCache;
|
|
165
|
+
|
|
166
|
+
const hashes: HashForSig[] = [];
|
|
167
|
+
if (input.tapInternalKey && !tapLeafHashToSign) {
|
|
168
|
+
const outputKey =
|
|
169
|
+
this.#cache.getPrevoutTaprootKey(inputIndex, input, this.#txFromBuffer) ||
|
|
170
|
+
new Uint8Array(0);
|
|
171
|
+
if (equals(toXOnly(pubkey as PublicKey), outputKey)) {
|
|
172
|
+
const tapKeyHash = unsignedTx.hashForWitnessV1(
|
|
173
|
+
inputIndex,
|
|
174
|
+
signingScripts,
|
|
175
|
+
values,
|
|
176
|
+
sighashType,
|
|
177
|
+
undefined,
|
|
178
|
+
undefined,
|
|
179
|
+
taprootCache,
|
|
180
|
+
);
|
|
181
|
+
hashes.push({ pubkey: pubkey as PublicKey, hash: tapKeyHash });
|
|
182
|
+
}
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
const tapLeafHashes = (input.tapLeafScript || [])
|
|
186
|
+
.filter((tapLeaf) => pubkeyInScript(pubkey, tapLeaf.script))
|
|
187
|
+
.map((tapLeaf) => {
|
|
188
|
+
const hash = tapleafHash({
|
|
189
|
+
output: tapLeaf.script,
|
|
190
|
+
version: tapLeaf.leafVersion,
|
|
191
|
+
});
|
|
192
|
+
return Object.assign({ hash }, tapLeaf);
|
|
193
|
+
})
|
|
194
|
+
.filter((tapLeaf) => !tapLeafHashToSign || equals(tapLeafHashToSign, tapLeaf.hash))
|
|
195
|
+
.map((tapLeaf) => {
|
|
196
|
+
const tapScriptHash = unsignedTx.hashForWitnessV1(
|
|
197
|
+
inputIndex,
|
|
198
|
+
signingScripts,
|
|
199
|
+
values,
|
|
200
|
+
sighashType,
|
|
201
|
+
tapLeaf.hash,
|
|
202
|
+
undefined,
|
|
203
|
+
taprootCache,
|
|
204
|
+
);
|
|
205
|
+
|
|
206
|
+
return {
|
|
207
|
+
pubkey: pubkey as PublicKey,
|
|
208
|
+
hash: tapScriptHash,
|
|
209
|
+
leafHash: tapLeaf.hash,
|
|
210
|
+
};
|
|
211
|
+
});
|
|
212
|
+
|
|
213
|
+
return hashes.concat(tapLeafHashes);
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
public getAllTaprootHashesForSig(
|
|
217
|
+
inputIndex: number,
|
|
218
|
+
input: PsbtInput,
|
|
219
|
+
inputs: PsbtInput[],
|
|
220
|
+
): HashForSig[] {
|
|
221
|
+
const allPublicKeys: Uint8Array[] = [];
|
|
222
|
+
if (input.tapInternalKey) {
|
|
223
|
+
const key = this.#cache.getPrevoutTaprootKey(inputIndex, input, this.#txFromBuffer);
|
|
224
|
+
if (key) {
|
|
225
|
+
allPublicKeys.push(key);
|
|
226
|
+
}
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
if (input.tapScriptSig) {
|
|
230
|
+
const tapScriptPubkeys = input.tapScriptSig.map((tss) => tss.pubkey);
|
|
231
|
+
allPublicKeys.push(...tapScriptPubkeys);
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
const allHashes = allPublicKeys.map((pubicKey) =>
|
|
235
|
+
this.getTaprootHashesForSig(inputIndex, input, inputs, pubicKey),
|
|
236
|
+
);
|
|
237
|
+
|
|
238
|
+
return allHashes.flat();
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
public trimTaprootSig(signature: Uint8Array): Uint8Array {
|
|
242
|
+
return signature.length === 64 ? signature : signature.subarray(0, 64);
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
public getSignersFromHD<T extends HDSigner | HDSignerAsync>(
|
|
246
|
+
inputIndex: number,
|
|
247
|
+
inputs: PsbtInput[],
|
|
248
|
+
hdKeyPair: T,
|
|
249
|
+
): T[] {
|
|
250
|
+
const input = checkForInput(inputs, inputIndex);
|
|
251
|
+
if (!input.bip32Derivation || input.bip32Derivation.length === 0) {
|
|
252
|
+
throw new Error('Need bip32Derivation to sign with HD');
|
|
253
|
+
}
|
|
254
|
+
const myDerivations = input.bip32Derivation
|
|
255
|
+
.map((bipDv) => {
|
|
256
|
+
if (equals(bipDv.masterFingerprint, hdKeyPair.fingerprint)) {
|
|
257
|
+
return bipDv;
|
|
258
|
+
} else {
|
|
259
|
+
return;
|
|
260
|
+
}
|
|
261
|
+
})
|
|
262
|
+
.filter((v) => !!v);
|
|
263
|
+
if (myDerivations.length === 0) {
|
|
264
|
+
throw new Error(
|
|
265
|
+
'Need one bip32Derivation masterFingerprint to match the HDSigner fingerprint',
|
|
266
|
+
);
|
|
267
|
+
}
|
|
268
|
+
|
|
269
|
+
return myDerivations.map((bipDv) => {
|
|
270
|
+
const node = hdKeyPair.derivePath(bipDv.path) as T;
|
|
271
|
+
if (!equals(bipDv.pubkey, node.publicKey)) {
|
|
272
|
+
throw new Error('pubkey did not match bip32Derivation');
|
|
273
|
+
}
|
|
274
|
+
return node;
|
|
275
|
+
});
|
|
276
|
+
}
|
|
277
|
+
|
|
278
|
+
public bip32DerivationIsMine(root: HDSigner): (d: Bip32Derivation) => boolean {
|
|
279
|
+
return (d: Bip32Derivation): boolean => {
|
|
280
|
+
const fingerprint =
|
|
281
|
+
root.fingerprint instanceof Uint8Array
|
|
282
|
+
? root.fingerprint
|
|
283
|
+
: new Uint8Array(root.fingerprint);
|
|
284
|
+
if (!equals(d.masterFingerprint, fingerprint)) return false;
|
|
285
|
+
const derivedPubkey = root.derivePath(d.path).publicKey;
|
|
286
|
+
const pubkey =
|
|
287
|
+
derivedPubkey instanceof Uint8Array ? derivedPubkey : new Uint8Array(derivedPubkey);
|
|
288
|
+
if (!equals(pubkey, d.pubkey)) return false;
|
|
289
|
+
return true;
|
|
290
|
+
};
|
|
291
|
+
}
|
|
292
|
+
}
|
|
293
|
+
|
|
294
|
+
function checkSighashTypeAllowed(sighashType: number, sighashTypes?: number[]): void {
|
|
295
|
+
if (sighashTypes && !sighashTypes.includes(sighashType)) {
|
|
296
|
+
const str = sighashTypeToString(sighashType);
|
|
297
|
+
throw new Error(
|
|
298
|
+
`Sighash type is not allowed. Retry the sign method passing the ` +
|
|
299
|
+
`sighashTypes array of whitelisted types. Sighash type: ${str}`,
|
|
300
|
+
);
|
|
301
|
+
}
|
|
302
|
+
}
|
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
import type { Transaction as ITransaction, TransactionFromBuffer } from 'bip174';
|
|
2
|
+
import { fromHex, reverse } from '../io/index.js';
|
|
3
|
+
import { Transaction } from '../transaction.js';
|
|
4
|
+
import type { Bytes32 } from '../types.js';
|
|
5
|
+
import { checkTxEmpty } from './validation.js';
|
|
6
|
+
import type { TransactionInput, TransactionOutput } from './types.js';
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* Empty version-2 transaction with zero inputs and zero outputs.
|
|
10
|
+
* Used as the default buffer when constructing a new PsbtTransaction.
|
|
11
|
+
*
|
|
12
|
+
* Layout: [version(4LE)] [inputCount(varint)] [outputCount(varint)] [locktime(4LE)]
|
|
13
|
+
* [02 00 00 00] [00] [00] [00 00 00 00]
|
|
14
|
+
*/
|
|
15
|
+
const EMPTY_TX_V2 = new Uint8Array([2, 0, 0, 0, 0, 0, 0, 0, 0, 0]);
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* This class implements the Transaction interface from bip174 library.
|
|
19
|
+
* It contains a bitcoinjs-lib Transaction object.
|
|
20
|
+
*/
|
|
21
|
+
export class PsbtTransaction implements ITransaction {
|
|
22
|
+
public tx: Transaction;
|
|
23
|
+
|
|
24
|
+
public constructor(buffer: Uint8Array = EMPTY_TX_V2) {
|
|
25
|
+
this.tx = Transaction.fromBuffer(buffer);
|
|
26
|
+
checkTxEmpty(this.tx);
|
|
27
|
+
Object.defineProperty(this, 'tx', {
|
|
28
|
+
enumerable: false,
|
|
29
|
+
writable: true,
|
|
30
|
+
});
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
public getInputOutputCounts(): {
|
|
34
|
+
inputCount: number;
|
|
35
|
+
outputCount: number;
|
|
36
|
+
} {
|
|
37
|
+
return {
|
|
38
|
+
inputCount: this.tx.ins.length,
|
|
39
|
+
outputCount: this.tx.outs.length,
|
|
40
|
+
};
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
public addInput(input: TransactionInput): void {
|
|
44
|
+
if (
|
|
45
|
+
input.hash === undefined ||
|
|
46
|
+
input.index === undefined ||
|
|
47
|
+
(!(input.hash instanceof Uint8Array) && typeof input.hash !== 'string') ||
|
|
48
|
+
typeof input.index !== 'number'
|
|
49
|
+
) {
|
|
50
|
+
throw new Error('Error adding input.');
|
|
51
|
+
}
|
|
52
|
+
const hash = (
|
|
53
|
+
typeof input.hash === 'string' ? reverse(fromHex(input.hash)) : input.hash
|
|
54
|
+
) as Bytes32;
|
|
55
|
+
|
|
56
|
+
this.tx.addInput(hash, input.index, input.sequence);
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
public addOutput(output: TransactionOutput): void {
|
|
60
|
+
if (
|
|
61
|
+
output.script === undefined ||
|
|
62
|
+
output.value === undefined ||
|
|
63
|
+
!(output.script instanceof Uint8Array) ||
|
|
64
|
+
typeof output.value !== 'bigint'
|
|
65
|
+
) {
|
|
66
|
+
throw new Error('Error adding output.');
|
|
67
|
+
}
|
|
68
|
+
this.tx.addOutput(output.script, output.value);
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
public toBuffer(): Uint8Array {
|
|
72
|
+
return this.tx.toBuffer();
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
/**
|
|
77
|
+
* This function is needed to pass to the bip174 base class's fromBuffer.
|
|
78
|
+
* It takes the "transaction buffer" portion of the psbt buffer and returns a
|
|
79
|
+
* Transaction (From the bip174 library) interface.
|
|
80
|
+
*/
|
|
81
|
+
export const transactionFromBuffer: TransactionFromBuffer = (buffer: Uint8Array): ITransaction =>
|
|
82
|
+
new PsbtTransaction(buffer);
|
package/src/psbt/bip371.ts
CHANGED
|
@@ -16,8 +16,8 @@ import {
|
|
|
16
16
|
} from '../payments/bip341.js';
|
|
17
17
|
import { p2tr } from '../payments/p2tr.js';
|
|
18
18
|
import { Transaction } from '../transaction.js';
|
|
19
|
+
import type { Bytes32, Tapleaf, Taptree, XOnlyPublicKey } from '../types.js';
|
|
19
20
|
import { isTapleaf, isTaptree } from '../types.js';
|
|
20
|
-
import type { Tapleaf, Taptree, XOnlyPublicKey, Bytes32 } from '../types.js';
|
|
21
21
|
import { concat, equals } from '../io/index.js';
|
|
22
22
|
import {
|
|
23
23
|
isP2TR,
|
|
@@ -137,8 +137,12 @@ function getTaprootScripPubkey(tapInternalKey: TapInternalKey, tapTree?: TapTree
|
|
|
137
137
|
|
|
138
138
|
export function tweakInternalPubKey(inputIndex: number, input: PsbtInput): Uint8Array {
|
|
139
139
|
const tapInternalKey = input.tapInternalKey;
|
|
140
|
-
const tapInternalKeyBuf = tapInternalKey
|
|
141
|
-
|
|
140
|
+
const tapInternalKeyBuf = tapInternalKey
|
|
141
|
+
? (new Uint8Array(tapInternalKey) as XOnlyPublicKey)
|
|
142
|
+
: undefined;
|
|
143
|
+
const tapMerkleRootBuf = input.tapMerkleRoot
|
|
144
|
+
? (new Uint8Array(input.tapMerkleRoot) as Bytes32)
|
|
145
|
+
: undefined;
|
|
142
146
|
const outputKey = tapInternalKeyBuf && tweakKey(tapInternalKeyBuf, tapMerkleRootBuf);
|
|
143
147
|
|
|
144
148
|
if (!outputKey)
|
package/src/psbt/psbtutils.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import type { PartialSig, PsbtInput } from 'bip174';
|
|
2
|
-
import {
|
|
2
|
+
import { alloc, concat, equals, varuint } from '../io/index.js';
|
|
3
3
|
import { hash160 } from '../crypto.js';
|
|
4
4
|
import { p2ms } from '../payments/p2ms.js';
|
|
5
5
|
import { p2pk } from '../payments/p2pk.js';
|
package/src/psbt/types.ts
CHANGED
|
@@ -5,15 +5,8 @@
|
|
|
5
5
|
|
|
6
6
|
import type { Psbt as PsbtBase, PsbtGlobal, PsbtInput, PsbtOutput } from 'bip174';
|
|
7
7
|
import type { Network } from '../networks.js';
|
|
8
|
-
import type {
|
|
9
|
-
import type {
|
|
10
|
-
Bytes32,
|
|
11
|
-
PublicKey,
|
|
12
|
-
Satoshi,
|
|
13
|
-
Script,
|
|
14
|
-
Signature,
|
|
15
|
-
SchnorrSignature,
|
|
16
|
-
} from '../types.js';
|
|
8
|
+
import type { TaprootHashCache, Transaction } from '../transaction.js';
|
|
9
|
+
import type { Bytes32, MessageHash, PublicKey, Satoshi, Script } from '../types.js';
|
|
17
10
|
|
|
18
11
|
/**
|
|
19
12
|
* Transaction input interface for PSBT.
|
|
@@ -52,7 +45,7 @@ export interface PsbtTxOutput extends TransactionOutput {
|
|
|
52
45
|
*/
|
|
53
46
|
export type ValidateSigFunction = (
|
|
54
47
|
pubkey: PublicKey,
|
|
55
|
-
msghash:
|
|
48
|
+
msghash: MessageHash,
|
|
56
49
|
signature: Uint8Array,
|
|
57
50
|
) => boolean;
|
|
58
51
|
|
|
@@ -109,89 +102,7 @@ export interface PsbtOutputExtendedScript extends PsbtOutput {
|
|
|
109
102
|
readonly value: Satoshi;
|
|
110
103
|
}
|
|
111
104
|
|
|
112
|
-
|
|
113
|
-
* Base interface for HD signers.
|
|
114
|
-
*/
|
|
115
|
-
interface HDSignerBase {
|
|
116
|
-
/**
|
|
117
|
-
* DER format compressed publicKey Uint8Array
|
|
118
|
-
*/
|
|
119
|
-
readonly publicKey: PublicKey;
|
|
120
|
-
/**
|
|
121
|
-
* The first 4 bytes of the sha256-ripemd160 of the publicKey
|
|
122
|
-
*/
|
|
123
|
-
readonly fingerprint: Uint8Array;
|
|
124
|
-
}
|
|
125
|
-
|
|
126
|
-
/**
|
|
127
|
-
* HD signer interface for synchronous signing.
|
|
128
|
-
*/
|
|
129
|
-
export interface HDSigner extends HDSignerBase {
|
|
130
|
-
/**
|
|
131
|
-
* The path string must match /^m(\/\d+'?)+$/
|
|
132
|
-
* ex. m/44'/0'/0'/1/23 levels with ' must be hard derivations
|
|
133
|
-
*/
|
|
134
|
-
derivePath(path: string): HDSigner;
|
|
135
|
-
|
|
136
|
-
/**
|
|
137
|
-
* Input hash (the "message digest") for the signature algorithm
|
|
138
|
-
* Return a 64 byte signature (32 byte r and 32 byte s in that order)
|
|
139
|
-
*/
|
|
140
|
-
sign(hash: Bytes32): Uint8Array;
|
|
141
|
-
}
|
|
142
|
-
|
|
143
|
-
/**
|
|
144
|
-
* HD signer interface for asynchronous signing.
|
|
145
|
-
*/
|
|
146
|
-
export interface HDSignerAsync extends HDSignerBase {
|
|
147
|
-
derivePath(path: string): HDSignerAsync;
|
|
148
|
-
|
|
149
|
-
sign(hash: Bytes32): Promise<Uint8Array>;
|
|
150
|
-
}
|
|
151
|
-
|
|
152
|
-
/**
|
|
153
|
-
* Alternative signer interface with lowR support.
|
|
154
|
-
*/
|
|
155
|
-
export interface SignerAlternative {
|
|
156
|
-
readonly publicKey: PublicKey;
|
|
157
|
-
readonly lowR: boolean;
|
|
158
|
-
|
|
159
|
-
sign(hash: Bytes32, lowR?: boolean): Signature;
|
|
160
|
-
|
|
161
|
-
verify(hash: Bytes32, signature: Signature): boolean;
|
|
162
|
-
|
|
163
|
-
signSchnorr(hash: Bytes32): SchnorrSignature;
|
|
164
|
-
|
|
165
|
-
verifySchnorr(hash: Bytes32, signature: SchnorrSignature): boolean;
|
|
166
|
-
}
|
|
167
|
-
|
|
168
|
-
/**
|
|
169
|
-
* Basic signer interface for synchronous signing.
|
|
170
|
-
*/
|
|
171
|
-
export interface Signer {
|
|
172
|
-
readonly publicKey: PublicKey;
|
|
173
|
-
readonly network?: Network | undefined;
|
|
174
|
-
|
|
175
|
-
sign(hash: Bytes32, lowR?: boolean): Signature;
|
|
176
|
-
|
|
177
|
-
signSchnorr?(hash: Bytes32): SchnorrSignature;
|
|
178
|
-
|
|
179
|
-
getPublicKey?(): PublicKey;
|
|
180
|
-
}
|
|
181
|
-
|
|
182
|
-
/**
|
|
183
|
-
* Basic signer interface for asynchronous signing.
|
|
184
|
-
*/
|
|
185
|
-
export interface SignerAsync {
|
|
186
|
-
readonly publicKey: PublicKey;
|
|
187
|
-
readonly network?: Network | undefined;
|
|
188
|
-
|
|
189
|
-
sign(hash: Bytes32, lowR?: boolean): Promise<Signature>;
|
|
190
|
-
|
|
191
|
-
signSchnorr?(hash: Bytes32): Promise<SchnorrSignature>;
|
|
192
|
-
|
|
193
|
-
getPublicKey?(): PublicKey;
|
|
194
|
-
}
|
|
105
|
+
export type { Signer, SignerAsync, HDSigner, HDSignerAsync } from '@btc-vision/ecpair';
|
|
195
106
|
|
|
196
107
|
/**
|
|
197
108
|
* Minimal key pair interface for checking Taproot hashes.
|
|
@@ -206,7 +117,7 @@ export interface TaprootHashCheckSigner {
|
|
|
206
117
|
/**
|
|
207
118
|
* Internal PSBT cache for computed values.
|
|
208
119
|
*/
|
|
209
|
-
export interface
|
|
120
|
+
export interface PsbtCacheInterface {
|
|
210
121
|
nonWitnessUtxoTxCache: Transaction[];
|
|
211
122
|
nonWitnessUtxoBufCache: Uint8Array[];
|
|
212
123
|
txInCache: TxInCacheMap;
|
package/src/psbt/validation.ts
CHANGED
|
@@ -4,14 +4,14 @@
|
|
|
4
4
|
*/
|
|
5
5
|
|
|
6
6
|
import type { PsbtInput } from 'bip174';
|
|
7
|
-
import {
|
|
8
|
-
import * as payments from '../payments/index.js';
|
|
7
|
+
import { equals, reverse, toHex } from '../io/index.js';
|
|
9
8
|
import type { P2SHPayment, Payment, PaymentOpts } from '../payments/index.js';
|
|
9
|
+
import * as payments from '../payments/index.js';
|
|
10
10
|
import { checkTaprootInputForSigs, isTaprootInput } from './bip371.js';
|
|
11
11
|
import { checkInputForSig, pubkeyInScript } from './psbtutils.js';
|
|
12
12
|
import * as bscript from '../script.js';
|
|
13
13
|
import type { Transaction } from '../transaction.js';
|
|
14
|
-
import type { PsbtCache } from './types.js';
|
|
14
|
+
import type { PsbtCacheInterface as PsbtCache } from './types.js';
|
|
15
15
|
import type { PublicKey, Script } from '../types.js';
|
|
16
16
|
|
|
17
17
|
/**
|
|
@@ -142,9 +142,7 @@ export function checkPartialSigSighashes(input: PsbtInput): void {
|
|
|
142
142
|
*/
|
|
143
143
|
export function checkScriptForPubkey(pubkey: PublicKey, script: Script, action: string): void {
|
|
144
144
|
if (!pubkeyInScript(pubkey, script)) {
|
|
145
|
-
throw new Error(
|
|
146
|
-
`Can not ${action} for this input with the key ${toHex(pubkey)}`,
|
|
147
|
-
);
|
|
145
|
+
throw new Error(`Can not ${action} for this input with the key ${toHex(pubkey)}`);
|
|
148
146
|
}
|
|
149
147
|
}
|
|
150
148
|
|
|
@@ -157,12 +155,7 @@ export function checkScriptForPubkey(pubkey: PublicKey, script: Script, action:
|
|
|
157
155
|
export function scriptCheckerFactory(
|
|
158
156
|
payment: (a: Omit<Payment, 'name'>, opts?: PaymentOpts) => Payment,
|
|
159
157
|
paymentScriptName: string,
|
|
160
|
-
): (
|
|
161
|
-
idx: number,
|
|
162
|
-
scriptPubKey: Script,
|
|
163
|
-
redeemScript: Script,
|
|
164
|
-
ioType: 'input' | 'output',
|
|
165
|
-
) => void {
|
|
158
|
+
): (idx: number, scriptPubKey: Script, redeemScript: Script, ioType: 'input' | 'output') => void {
|
|
166
159
|
return (
|
|
167
160
|
inputIndex: number,
|
|
168
161
|
scriptPubKey: Script,
|