paulmillr / scure-btc-signer

Audited & minimal library for creating, signing & decoding Bitcoin transactions.
https://paulmillr.com/noble/#scure
MIT License
134 stars 33 forks source link

allowUnkowOutput not passed through fromRaw when calling addInput #59

Closed radicleart closed 10 months ago

radicleart commented 10 months ago

Adding the third output of a tx with an unknown (op_return) output and unable because the tx options {allowUnknowOutput:true} are not passed through this line;

const PSBTInputCoder = P.validate(PSBTKeyMap(PSBTInput), (i) => {
   ..
   ..
    const tx = Transaction.fromRaw(RawTx.encode(i.nonWitnessUtxo));
   ..
   ..

causes

Uncaught (in promise) Error: Transaction/output: unknown output script type, there is a chance that input is unspendable. Pass allowUnkownScript=true, if you sure
    at _Transaction.normalizeOutput (index.ts:1984:13)
    at _Transaction.addOutput (index.ts:1994:28)
    at _Transaction.fromRaw (index.ts:1673:40)

Adding third output of dae5bf18ef1bbbfe153276335e3718f9ee91a91fc3f902e75b657a442d471b10

const txFromUtxo = btc.Transaction.fromRaw(hex.decode(utxo.tx.hex), {allowUnknowInput:true, allowUnknowOutput: true})
const outputToSpend = txFromUtxo.getOutput(utxo.vout)
const spendScr = btc.OutScript.decode(outputToSpend.script)
if (spendScr.type === 'wpkh') {
    const nextI:btc.TransactionInput = {
        txid: hex.decode(utxo.txid),
        index: utxo.vout,
        nonWitnessUtxo: utxo.tx.hex
    }
    transaction.addInput(nextI);
}
radicleart commented 10 months ago

Ah sorry - think this is my mistake as should be adding the input with witnessUTXO data.

const wpkhAlice = btc.p2wpkh(hex.decode(userPaymentPubKey));
const nextI:btc.TransactionInput = {
    txid: hex.decode(utxo.txid),
    index: utxo.vout,
    ...wpkhAlice,
    witnessUtxo: {
        script: wpkhAlice.script,
        amount: BigInt(utxo.value),
    },
}
transaction.addInput(nextI);
radicleart commented 10 months ago

Closing this as user error.