Browse Source

Merge pull request #3 from junderw/fixes3

Fix NBitcoin stuff
master
Andrew Camilleri 5 years ago
committed by GitHub
parent
commit
f321b26327
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
  1. 36
      src/index.js
  2. 37
      ts_src/index.ts

36
src/index.js

@ -134,17 +134,16 @@ function checkInputSanity(input, txInput) {
errors.push('final witness script present but no witness utxo'); errors.push('final witness script present but no witness utxo');
} }
if (input.nonWitnessUtxo) { if (input.nonWitnessUtxo) {
// TODO: get hash const prevTx = bitcoinjs_lib_1.Transaction.fromBuffer(input.nonWitnessUtxo);
const prevOutTxId = input.nonWitnessUtxo; const prevOutTxId = prevTx.getHash();
let validOutpoint = true; let validOutpoint = true;
if (txInput.hash !== prevOutTxId) { if (!txInput.hash.equals(prevOutTxId)) {
errors.push( errors.push(
'non_witness_utxo does not match the transaction id referenced by the global transaction sign', 'non_witness_utxo does not match the transaction id referenced by the global transaction sign',
); );
validOutpoint = false; validOutpoint = false;
} }
// @ts-ignore if (txInput.index >= prevTx.outs.length) {
if (txInput.index >= input.nonWitnessUtxo.Outputs.length) {
errors.push( errors.push(
'Global transaction referencing an out of bound output in non_witness_utxo', 'Global transaction referencing an out of bound output in non_witness_utxo',
); );
@ -152,10 +151,9 @@ function checkInputSanity(input, txInput) {
} }
if (input.redeemScript && validOutpoint) { if (input.redeemScript && validOutpoint) {
if ( if (
// @ts-ignore !redeemScriptToScriptPubkey(input.redeemScript).equals(
input.redeemScript.Hash.ScriptPubKey !== prevTx.outs[txInput.index].script,
// @ts-ignore )
input.nonWitnessUtxo.Outputs[txInput.index].ScriptPubKey
) )
errors.push( errors.push(
'The redeem_script is not coherent with the scriptPubKey of the non_witness_utxo', 'The redeem_script is not coherent with the scriptPubKey of the non_witness_utxo',
@ -165,8 +163,9 @@ function checkInputSanity(input, txInput) {
if (input.witnessUtxo) { if (input.witnessUtxo) {
if (input.redeemScript) { if (input.redeemScript) {
if ( if (
// @ts-ignore !redeemScriptToScriptPubkey(input.redeemScript).equals(
input.redeemScript.Hash.ScriptPubKey !== input.witnessUtxo.ScriptPubKey input.witnessUtxo.script,
)
) )
errors.push( errors.push(
'The redeem_script is not coherent with the scriptPubKey of the witness_utxo', 'The redeem_script is not coherent with the scriptPubKey of the witness_utxo',
@ -174,10 +173,9 @@ function checkInputSanity(input, txInput) {
if ( if (
input.witnessScript && input.witnessScript &&
input.redeemScript && input.redeemScript &&
// @ts-ignore !input.redeemScript.equals(
PayToWitScriptHashTemplate.Instance.ExtractScriptPubKeyParameters( witnessScriptToScriptPubkey(input.witnessScript),
input.redeemScript, )
) !== input.witnessScript.WitHash
) )
errors.push( errors.push(
'witnessScript with witness UTXO does not match the redeemScript', 'witnessScript with witness UTXO does not match the redeemScript',
@ -195,6 +193,14 @@ function checkInputSanity(input, txInput) {
// } // }
return errors; return errors;
} }
function redeemScriptToScriptPubkey(redeemScript) {
return bitcoinjs_lib_1.payments.p2sh({ redeem: { output: redeemScript } })
.output;
}
function witnessScriptToScriptPubkey(witnessScript) {
return bitcoinjs_lib_1.payments.p2wsh({ redeem: { output: witnessScript } })
.output;
}
function hasKeypathInformationSet(items) { function hasKeypathInformationSet(items) {
return ( return (
items.filter( items.filter(

37
ts_src/index.ts

@ -158,18 +158,17 @@ function checkInputSanity(input: PsbtInput, txInput: TxInput): string[] {
} }
if (input.nonWitnessUtxo) { if (input.nonWitnessUtxo) {
const nonWitnessUtxoTransaction = Transaction.fromBuffer(input.nonWitnessUtxo); const prevTx = Transaction.fromBuffer(input.nonWitnessUtxo);
const prevOutTxId = nonWitnessUtxoTransaction.getHash(); const prevOutTxId = prevTx.getHash();
let validOutpoint = true; let validOutpoint = true;
if (txInput.hash !== prevOutTxId) { if (!txInput.hash.equals(prevOutTxId)) {
errors.push( errors.push(
'non_witness_utxo does not match the transaction id referenced by the global transaction sign', 'non_witness_utxo does not match the transaction id referenced by the global transaction sign',
); );
validOutpoint = false; validOutpoint = false;
} }
// @ts-ignore if (txInput.index >= prevTx.outs.length) {
if (txInput.index >= nonWitnessUtxoTransaction.outs.length) {
errors.push( errors.push(
'Global transaction referencing an out of bound output in non_witness_utxo', 'Global transaction referencing an out of bound output in non_witness_utxo',
); );
@ -177,10 +176,9 @@ function checkInputSanity(input: PsbtInput, txInput: TxInput): string[] {
} }
if (input.redeemScript && validOutpoint) { if (input.redeemScript && validOutpoint) {
if ( if (
// @ts-ignore !redeemScriptToScriptPubkey(input.redeemScript).equals(
input.redeemScript.Hash.ScriptPubKey !== prevTx.outs[txInput.index].script,
// @ts-ignore )
input.nonWitnessUtxo.Outputs[txInput.index].ScriptPubKey
) )
errors.push( errors.push(
'The redeem_script is not coherent with the scriptPubKey of the non_witness_utxo', 'The redeem_script is not coherent with the scriptPubKey of the non_witness_utxo',
@ -191,8 +189,9 @@ function checkInputSanity(input: PsbtInput, txInput: TxInput): string[] {
if (input.witnessUtxo) { if (input.witnessUtxo) {
if (input.redeemScript) { if (input.redeemScript) {
if ( if (
// @ts-ignore !redeemScriptToScriptPubkey(input.redeemScript).equals(
input.redeemScript.Hash.ScriptPubKey !== input.witnessUtxo.ScriptPubKey input.witnessUtxo.script,
)
) )
errors.push( errors.push(
'The redeem_script is not coherent with the scriptPubKey of the witness_utxo', 'The redeem_script is not coherent with the scriptPubKey of the witness_utxo',
@ -200,11 +199,9 @@ function checkInputSanity(input: PsbtInput, txInput: TxInput): string[] {
if ( if (
input.witnessScript && input.witnessScript &&
input.redeemScript && input.redeemScript &&
// @ts-ignore !input.redeemScript.equals(
PayToWitScriptHashTemplate.Instance.ExtractScriptPubKeyParameters( witnessScriptToScriptPubkey(input.witnessScript),
input.redeemScript, )
// @ts-ignore
) !== input.witnessScript.WitHash
) )
errors.push( errors.push(
'witnessScript with witness UTXO does not match the redeemScript', 'witnessScript with witness UTXO does not match the redeemScript',
@ -225,6 +222,14 @@ function checkInputSanity(input: PsbtInput, txInput: TxInput): string[] {
return errors; return errors;
} }
function redeemScriptToScriptPubkey(redeemScript: Buffer): Buffer {
return payments.p2sh({ redeem: { output: redeemScript } }).output!;
}
function witnessScriptToScriptPubkey(witnessScript: Buffer): Buffer {
return payments.p2wsh({ redeem: { output: witnessScript } }).output!;
}
function hasKeypathInformationSet( function hasKeypathInformationSet(
items: { bip32Derivation?: Bip32Derivation[] }[], items: { bip32Derivation?: Bip32Derivation[] }[],
): boolean { ): boolean {

Loading…
Cancel
Save