Skip to content

Commit

Permalink
fix: psbt bare utxo signing
Browse files Browse the repository at this point in the history
  • Loading branch information
fbwoolf committed Jul 31, 2023
1 parent e085927 commit ec7ae7b
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 35 deletions.
55 changes: 27 additions & 28 deletions src/shared/crypto/bitcoin/bitcoin.utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { HDKey, Versions } from '@scure/bip32';
import * as btc from '@scure/btc-signer';

import { BitcoinNetworkModes, NetworkModes } from '@shared/constants';
import { logger } from '@shared/logger';
import { whenNetwork } from '@shared/utils';

import { DerivationPathDepth } from '../derivation-path.utils';
Expand Down Expand Up @@ -64,35 +65,33 @@ export function decodeBitcoinTx(tx: string) {
export function getAddressFromOutScript(script: Uint8Array, bitcoinNetwork: BtcSignerNetwork) {
const outputScript = btc.OutScript.decode(script);

if (outputScript.type === 'pk' || outputScript.type === 'tr') {
return btc.Address(bitcoinNetwork).encode({
type: outputScript.type,
pubkey: outputScript.pubkey,
});
switch (outputScript.type) {
case 'pkh' || 'sh' || 'wpkh' || 'wsh':
return btc.Address(bitcoinNetwork).encode({
type: outputScript.type,
hash: outputScript.hash,
});
case 'tr':
return btc.Address(bitcoinNetwork).encode({
type: outputScript.type,
pubkey: outputScript.pubkey,
});
case 'ms':
return btc.p2ms(outputScript.m, outputScript.pubkeys).address ?? '';
case 'pk':
return btc.p2pk(outputScript.pubkey, bitcoinNetwork).address ?? '';
case 'tr_ms' || 'tr_ns':
logger.error(`Cannot currently handle script type ${outputScript.type}`);
return '';
case 'unknown':
return btc.Address(bitcoinNetwork).encode({
type: outputScript.type,
script,
});
default:
logger.error(`Unknown address type=${outputScript.type}`);
return '';
}
if (outputScript.type === 'ms' || outputScript.type === 'tr_ms') {
return btc.Address(bitcoinNetwork).encode({
type: outputScript.type,
pubkeys: outputScript.pubkeys,
m: outputScript.m,
});
}
if (outputScript.type === 'tr_ns') {
return btc.Address(bitcoinNetwork).encode({
type: outputScript.type,
pubkeys: outputScript.pubkeys,
});
}
if (outputScript.type === 'unknown') {
return btc.Address(bitcoinNetwork).encode({
type: outputScript.type,
script,
});
}
return btc.Address(bitcoinNetwork).encode({
type: outputScript.type,
hash: outputScript.hash,
});
}

type BtcSignerLibPaymentTypeIdentifers = 'wpkh' | 'wsh' | 'tr' | 'pkh' | 'sh';
Expand Down
14 changes: 7 additions & 7 deletions test-app/src/components/bitcoin.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,13 @@ function getTaprootPayment(publicKey: Uint8Array) {
return btc.p2tr(ecdsaPublicKeyToSchnorr(publicKey), undefined, bitcoinTestnet);
}

let bareMultiSigLockingScript =
'512102dc054e58b755f233295d2a8759a3e4cbf678619d8e75379e7989046dbce16be32102932b35a45d21395ac8bb54b8f9dae3fd2dbc309c24e550cf2211fe6aa897e5ca2102020202020202020202020202020202020202020202020202020202020202020253ae';

function buildTestNativeSegwitPsbtRequest(pubKey: Uint8Array): PsbtRequestOptions {
const p2wpkh = btc.p2wpkh(pubKey, bitcoinTestnet);

const tx = new btc.Transaction();
const tx = new btc.Transaction({ disableScriptCheck: true });

tx.addInput({
index: 0,
Expand All @@ -64,16 +67,13 @@ function buildTestNativeSegwitPsbtRequest(pubKey: Uint8Array): PsbtRequestOption
script: p2wpkh.script,
},
});
tx.addOutput({
amount: BigInt(5000),
script: p2wpkh.script,
});
tx.addOutput({ amount: BigInt(796), script: bareMultiSigLockingScript });

const psbt = tx.toPSBT();

// For testing mainnet
return { hex: tempHex };
// return { hex: bytesToHex(psbt) };
// return { hex: tempHex };
return { hex: bytesToHex(psbt) };
}

function buildTestNativeSegwitPsbtRequestWithIndexes(pubKey: Uint8Array): PsbtRequestOptions {
Expand Down

0 comments on commit ec7ae7b

Please sign in to comment.