Skip to content

Commit 91c095a

Browse files
OttoAllmendingerllm-git
andcommitted
feat(utxo-lib): allow buffer as p2shP2pk key
Accept raw public key buffer in addition to BIP32 key instances when creating p2shP2pk scripts, which allows for reuse by BCH, BTG and other forks. Issue: BTC-2656 Co-authored-by: llm-git <[email protected]>
1 parent 201ae54 commit 91c095a

File tree

2 files changed

+16
-6
lines changed

2 files changed

+16
-6
lines changed

modules/utxo-lib/src/testutil/mock.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,9 +76,12 @@ export function isReplayProtectionUnspent<TNumber extends bigint | number>(
7676
export function mockReplayProtectionUnspent<TNumber extends number | bigint>(
7777
network: Network,
7878
value: TNumber,
79-
{ key = replayProtectionKeyPair, vout = 0 }: { key?: BIP32Interface; vout?: number } = {}
79+
{ key = replayProtectionKeyPair, vout = 0 }: { key?: BIP32Interface | Buffer; vout?: number } = {}
8080
): UnspentWithPrevTx<TNumber> {
81-
const outputScript = createOutputScriptP2shP2pk(key.publicKey).scriptPubKey;
81+
if (!Buffer.isBuffer(key)) {
82+
key = key.publicKey;
83+
}
84+
const outputScript = createOutputScriptP2shP2pk(key).scriptPubKey;
8285
const prevTransaction = mockPrevTx(vout, outputScript, BigInt(value), network);
8386
return { ...fromOutputWithPrevTx(prevTransaction, vout), value };
8487
}

modules/utxo-lib/src/testutil/psbt.ts

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -86,10 +86,14 @@ export function toUnspent(
8686
input: Input,
8787
index: number,
8888
network: Network,
89-
rootWalletKeys: RootWalletKeys
89+
rootWalletKeys: RootWalletKeys,
90+
{ p2shP2pkKey }: { p2shP2pkKey?: Buffer } = {}
9091
): Unspent<bigint> {
9192
if (input.scriptType === 'p2shP2pk') {
92-
return mockReplayProtectionUnspent(network, input.value, { key: rootWalletKeys['user'], vout: index });
93+
return mockReplayProtectionUnspent(network, input.value, {
94+
key: p2shP2pkKey ?? rootWalletKeys['user'],
95+
vout: index,
96+
});
9397
} else {
9498
const chain = getInternalChainCode(input.scriptType === 'taprootKeyPathSpend' ? 'p2trMusig2' : input.scriptType);
9599
return mockWalletUnspent(network, input.value, {
@@ -177,6 +181,7 @@ export function constructPsbt(
177181
rootWalletKeys: RootWalletKeys,
178182
signStage: SignStage,
179183
params?: {
184+
p2shP2pkKey?: Buffer;
180185
signers?: { signerName: KeyName; cosignerName?: KeyName };
181186
deterministic?: boolean;
182187
skipNonWitnessUtxo?: boolean;
@@ -194,14 +199,16 @@ export function constructPsbt(
194199
addXpubsToPsbt(psbt, rootWalletKeys);
195200
}
196201

197-
const unspents = inputs.map((input, i) => toUnspent(input, i, network, rootWalletKeys));
202+
const unspents = inputs.map((input, i) =>
203+
toUnspent(input, i, network, rootWalletKeys, { p2shP2pkKey: params?.p2shP2pkKey })
204+
);
198205

199206
unspents.forEach((u, i) => {
200207
const { signerName, cosignerName } = signers ? signers : getSigners(inputs[i].scriptType);
201208
if (isWalletUnspent(u) && cosignerName) {
202209
addWalletUnspentToPsbt(psbt, u, rootWalletKeys, signerName, cosignerName, { skipNonWitnessUtxo });
203210
} else {
204-
const { redeemScript } = createOutputScriptP2shP2pk(rootWalletKeys.user.publicKey);
211+
const { redeemScript } = createOutputScriptP2shP2pk(params?.p2shP2pkKey ?? rootWalletKeys.user.publicKey);
205212
assert(redeemScript);
206213
addReplayProtectionUnspentToPsbt(psbt, u, redeemScript, { skipNonWitnessUtxo });
207214
}

0 commit comments

Comments
 (0)