diff --git a/src/shared/crypto/bitcoin/bitcoin.utils.ts b/src/shared/crypto/bitcoin/bitcoin.utils.ts index e2c10ec812a..0cffd60ecb4 100644 --- a/src/shared/crypto/bitcoin/bitcoin.utils.ts +++ b/src/shared/crypto/bitcoin/bitcoin.utils.ts @@ -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'; @@ -64,24 +65,19 @@ 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') { + if (outputScript.type === 'ms') + return btc.p2ms(outputScript.m, outputScript.pubkeys).address ?? ''; + if (outputScript.type === 'pk') + return btc.p2pk(outputScript.pubkey, bitcoinNetwork).address ?? ''; + if (outputScript.type === 'tr') { return btc.Address(bitcoinNetwork).encode({ type: outputScript.type, pubkey: outputScript.pubkey, }); } - 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 === 'tr_ms' || outputScript.type === 'tr_ns') { + logger.error(`Cannot currently handle script type ${outputScript.type}`); + return ''; } if (outputScript.type === 'unknown') { return btc.Address(bitcoinNetwork).encode({ diff --git a/test-app/src/components/bitcoin.tsx b/test-app/src/components/bitcoin.tsx index 72b645edba7..cfe08d29bd4 100644 --- a/test-app/src/components/bitcoin.tsx +++ b/test-app/src/components/bitcoin.tsx @@ -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, @@ -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 {