@btc-vision/bitcoin 7.0.0-alpha.1 → 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 +5 -1
- package/browser/address.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/ecc/context.d.ts +22 -21
- 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 +6 -6
- package/browser/index.d.ts.map +1 -1
- package/browser/index.js +2602 -11786
- 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/p2tr.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/types.d.ts +4 -70
- 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 +7 -0
- package/browser/workers/WorkerSigningPool.d.ts.map +1 -1
- package/browser/workers/WorkerSigningPool.node.d.ts +7 -0
- 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 +5 -1
- package/build/address.d.ts.map +1 -1
- package/build/address.js +29 -17
- package/build/address.js.map +1 -1
- package/build/bech32utils.js.map +1 -1
- 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/ecc/context.d.ts +22 -21
- package/build/ecc/context.d.ts.map +1 -1
- package/build/ecc/context.js +23 -95
- 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 +7 -6
- package/build/index.d.ts.map +1 -1
- package/build/index.js +7 -5
- package/build/index.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.js.map +1 -1
- package/build/payments/embed.js.map +1 -1
- package/build/payments/p2ms.js.map +1 -1
- package/build/payments/p2pk.js.map +1 -1
- package/build/payments/p2pkh.js.map +1 -1
- package/build/payments/p2sh.js.map +1 -1
- package/build/payments/p2tr.d.ts.map +1 -1
- package/build/payments/p2tr.js +2 -3
- package/build/payments/p2tr.js.map +1 -1
- package/build/payments/p2wpkh.js.map +1 -1
- package/build/payments/p2wsh.js.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/types.d.ts +4 -70
- 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.d.ts +26 -40
- package/build/psbt.d.ts.map +1 -1
- package/build/psbt.js +177 -799
- package/build/psbt.js.map +1 -1
- package/build/script.d.ts.map +1 -1
- package/build/script.js +2 -2
- 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 +5 -4
- 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 +11 -16
- package/build/types.js.map +1 -1
- package/build/workers/WorkerSigningPool.d.ts +7 -0
- package/build/workers/WorkerSigningPool.d.ts.map +1 -1
- package/build/workers/WorkerSigningPool.js +12 -1
- package/build/workers/WorkerSigningPool.js.map +1 -1
- package/build/workers/WorkerSigningPool.node.d.ts +7 -0
- package/build/workers/WorkerSigningPool.node.d.ts.map +1 -1
- package/build/workers/WorkerSigningPool.node.js +37 -5
- 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/package.json +46 -8
- package/src/address.ts +41 -18
- package/src/bech32utils.ts +3 -3
- package/src/block.ts +2 -2
- package/src/branded.ts +15 -13
- package/src/ecc/context.ts +30 -133
- package/src/ecc/index.ts +2 -2
- package/src/ecc/types.ts +7 -138
- package/src/env.ts +239 -0
- package/src/index.ts +45 -9
- 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 +3 -3
- package/src/payments/embed.ts +1 -1
- package/src/payments/p2ms.ts +2 -2
- package/src/payments/p2pk.ts +2 -2
- package/src/payments/p2pkh.ts +3 -3
- package/src/payments/p2sh.ts +4 -4
- package/src/payments/p2tr.ts +9 -9
- package/src/payments/p2wpkh.ts +5 -5
- package/src/payments/p2wsh.ts +2 -2
- 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/types.ts +4 -86
- package/src/psbt/validation.ts +1 -1
- package/src/psbt.ts +349 -1198
- package/src/script.ts +2 -2
- package/src/transaction.ts +10 -9
- package/src/types.ts +18 -28
- package/src/workers/WorkerSigningPool.node.ts +41 -5
- package/src/workers/WorkerSigningPool.sequential.ts +191 -0
- package/src/workers/WorkerSigningPool.ts +14 -1
- 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 +7 -7
- package/src/workers/types.ts +21 -0
- 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 +11 -1
- 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/types.ts
CHANGED
|
@@ -6,7 +6,7 @@
|
|
|
6
6
|
import type { Psbt as PsbtBase, PsbtGlobal, PsbtInput, PsbtOutput } from 'bip174';
|
|
7
7
|
import type { Network } from '../networks.js';
|
|
8
8
|
import type { TaprootHashCache, Transaction } from '../transaction.js';
|
|
9
|
-
import type { Bytes32, PublicKey, Satoshi,
|
|
9
|
+
import type { Bytes32, MessageHash, PublicKey, Satoshi, Script } from '../types.js';
|
|
10
10
|
|
|
11
11
|
/**
|
|
12
12
|
* Transaction input interface for PSBT.
|
|
@@ -45,7 +45,7 @@ export interface PsbtTxOutput extends TransactionOutput {
|
|
|
45
45
|
*/
|
|
46
46
|
export type ValidateSigFunction = (
|
|
47
47
|
pubkey: PublicKey,
|
|
48
|
-
msghash:
|
|
48
|
+
msghash: MessageHash,
|
|
49
49
|
signature: Uint8Array,
|
|
50
50
|
) => boolean;
|
|
51
51
|
|
|
@@ -102,89 +102,7 @@ export interface PsbtOutputExtendedScript extends PsbtOutput {
|
|
|
102
102
|
readonly value: Satoshi;
|
|
103
103
|
}
|
|
104
104
|
|
|
105
|
-
|
|
106
|
-
* Base interface for HD signers.
|
|
107
|
-
*/
|
|
108
|
-
interface HDSignerBase {
|
|
109
|
-
/**
|
|
110
|
-
* DER format compressed publicKey Uint8Array
|
|
111
|
-
*/
|
|
112
|
-
readonly publicKey: PublicKey;
|
|
113
|
-
/**
|
|
114
|
-
* The first 4 bytes of the sha256-ripemd160 of the publicKey
|
|
115
|
-
*/
|
|
116
|
-
readonly fingerprint: Uint8Array;
|
|
117
|
-
}
|
|
118
|
-
|
|
119
|
-
/**
|
|
120
|
-
* HD signer interface for synchronous signing.
|
|
121
|
-
*/
|
|
122
|
-
export interface HDSigner extends HDSignerBase {
|
|
123
|
-
/**
|
|
124
|
-
* The path string must match /^m(\/\d+'?)+$/
|
|
125
|
-
* ex. m/44'/0'/0'/1/23 levels with ' must be hard derivations
|
|
126
|
-
*/
|
|
127
|
-
derivePath(path: string): HDSigner;
|
|
128
|
-
|
|
129
|
-
/**
|
|
130
|
-
* Input hash (the "message digest") for the signature algorithm
|
|
131
|
-
* Return a 64 byte signature (32 byte r and 32 byte s in that order)
|
|
132
|
-
*/
|
|
133
|
-
sign(hash: Bytes32): Uint8Array;
|
|
134
|
-
}
|
|
135
|
-
|
|
136
|
-
/**
|
|
137
|
-
* HD signer interface for asynchronous signing.
|
|
138
|
-
*/
|
|
139
|
-
export interface HDSignerAsync extends HDSignerBase {
|
|
140
|
-
derivePath(path: string): HDSignerAsync;
|
|
141
|
-
|
|
142
|
-
sign(hash: Bytes32): Promise<Uint8Array>;
|
|
143
|
-
}
|
|
144
|
-
|
|
145
|
-
/**
|
|
146
|
-
* Alternative signer interface with lowR support.
|
|
147
|
-
*/
|
|
148
|
-
export interface SignerAlternative {
|
|
149
|
-
readonly publicKey: PublicKey;
|
|
150
|
-
readonly lowR: boolean;
|
|
151
|
-
|
|
152
|
-
sign(hash: Bytes32, lowR?: boolean): Signature;
|
|
153
|
-
|
|
154
|
-
verify(hash: Bytes32, signature: Signature): boolean;
|
|
155
|
-
|
|
156
|
-
signSchnorr(hash: Bytes32): SchnorrSignature;
|
|
157
|
-
|
|
158
|
-
verifySchnorr(hash: Bytes32, signature: SchnorrSignature): boolean;
|
|
159
|
-
}
|
|
160
|
-
|
|
161
|
-
/**
|
|
162
|
-
* Basic signer interface for synchronous signing.
|
|
163
|
-
*/
|
|
164
|
-
export interface Signer {
|
|
165
|
-
readonly publicKey: PublicKey;
|
|
166
|
-
readonly network?: Network | undefined;
|
|
167
|
-
|
|
168
|
-
sign(hash: Bytes32, lowR?: boolean): Signature;
|
|
169
|
-
|
|
170
|
-
signSchnorr?(hash: Bytes32): SchnorrSignature;
|
|
171
|
-
|
|
172
|
-
getPublicKey?(): PublicKey;
|
|
173
|
-
}
|
|
174
|
-
|
|
175
|
-
/**
|
|
176
|
-
* Basic signer interface for asynchronous signing.
|
|
177
|
-
*/
|
|
178
|
-
export interface SignerAsync {
|
|
179
|
-
readonly publicKey: PublicKey;
|
|
180
|
-
readonly network?: Network | undefined;
|
|
181
|
-
|
|
182
|
-
sign(hash: Bytes32, lowR?: boolean): Promise<Signature>;
|
|
183
|
-
|
|
184
|
-
signSchnorr?(hash: Bytes32): Promise<SchnorrSignature>;
|
|
185
|
-
|
|
186
|
-
getPublicKey?(): PublicKey;
|
|
187
|
-
}
|
|
105
|
+
export type { Signer, SignerAsync, HDSigner, HDSignerAsync } from '@btc-vision/ecpair';
|
|
188
106
|
|
|
189
107
|
/**
|
|
190
108
|
* Minimal key pair interface for checking Taproot hashes.
|
|
@@ -199,7 +117,7 @@ export interface TaprootHashCheckSigner {
|
|
|
199
117
|
/**
|
|
200
118
|
* Internal PSBT cache for computed values.
|
|
201
119
|
*/
|
|
202
|
-
export interface
|
|
120
|
+
export interface PsbtCacheInterface {
|
|
203
121
|
nonWitnessUtxoTxCache: Transaction[];
|
|
204
122
|
nonWitnessUtxoBufCache: Uint8Array[];
|
|
205
123
|
txInCache: TxInCacheMap;
|
package/src/psbt/validation.ts
CHANGED
|
@@ -11,7 +11,7 @@ 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
|
/**
|