From ff3caa02fe23a949bad78b23c29a25d5da04d2e1 Mon Sep 17 00:00:00 2001 From: Luke Childs Date: Wed, 26 Jun 2019 16:29:00 +0700 Subject: [PATCH] Add BIP174 pseudo code for signing checks --- src/psbt.js | 21 +++++++++++++++++++++ ts_src/psbt.ts | 21 +++++++++++++++++++++ 2 files changed, 42 insertions(+) diff --git a/src/psbt.js b/src/psbt.js index 887c814..5bdb176 100644 --- a/src/psbt.js +++ b/src/psbt.js @@ -8,6 +8,27 @@ class Psbt extends bip174_1.Psbt { signInput(inputIndex, keyPair) { // TODO: Implement BIP174 pre-sign checks: // https://github.com/bitcoin/bips/blob/master/bip-0174.mediawiki#signer + // + // if non_witness_utxo.exists: + // assert(sha256d(non_witness_utxo) == psbt.tx.innput[i].prevout.hash) + // if redeemScript.exists: + // assert(non_witness_utxo.vout[psbt.tx.input[i].prevout.n].scriptPubKey == P2SH(redeemScript)) + // sign_non_witness(redeemScript) + // else: + // sign_non_witness(non_witness_utxo.vout[psbt.tx.input[i].prevout.n].scriptPubKey) + // else if witness_utxo.exists: + // if redeemScript.exists: + // assert(witness_utxo.scriptPubKey == P2SH(redeemScript)) + // script = redeemScript + // else: + // script = witness_utxo.scriptPubKey + // if IsP2WPKH(script): + // sign_witness(P2PKH(script[2:22])) + // else if IsP2WSH(script): + // assert(script == P2WSH(witnessScript)) + // sign_witness(witnessScript) + // else: + // assert False // TODO: Get hash to sign const hash = Buffer.alloc(32); const partialSig = { diff --git a/ts_src/psbt.ts b/ts_src/psbt.ts index 8fec780..73308a2 100644 --- a/ts_src/psbt.ts +++ b/ts_src/psbt.ts @@ -9,6 +9,27 @@ export class Psbt extends PsbtBase { signInput(inputIndex: number, keyPair: Signer): Psbt { // TODO: Implement BIP174 pre-sign checks: // https://github.com/bitcoin/bips/blob/master/bip-0174.mediawiki#signer + // + // if non_witness_utxo.exists: + // assert(sha256d(non_witness_utxo) == psbt.tx.innput[i].prevout.hash) + // if redeemScript.exists: + // assert(non_witness_utxo.vout[psbt.tx.input[i].prevout.n].scriptPubKey == P2SH(redeemScript)) + // sign_non_witness(redeemScript) + // else: + // sign_non_witness(non_witness_utxo.vout[psbt.tx.input[i].prevout.n].scriptPubKey) + // else if witness_utxo.exists: + // if redeemScript.exists: + // assert(witness_utxo.scriptPubKey == P2SH(redeemScript)) + // script = redeemScript + // else: + // script = witness_utxo.scriptPubKey + // if IsP2WPKH(script): + // sign_witness(P2PKH(script[2:22])) + // else if IsP2WSH(script): + // assert(script == P2WSH(witnessScript)) + // sign_witness(witnessScript) + // else: + // assert False // TODO: Get hash to sign const hash = Buffer.alloc(32);