You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
101 lines
3.3 KiB
101 lines
3.3 KiB
import { Psbt as PsbtBase } from 'bip174';
|
|
import { Signer } from './ecpair';
|
|
import * as payments from './payments';
|
|
import * as script from './script';
|
|
import { Transaction } from './transaction';
|
|
|
|
const checkRedeemScript = (
|
|
inputIndex: number,
|
|
scriptPubKey: Buffer,
|
|
redeemScript: Buffer,
|
|
): void => {
|
|
const redeemScriptOutput = payments.p2sh({
|
|
redeem: { output: redeemScript },
|
|
}).output as Buffer;
|
|
|
|
if (!scriptPubKey.equals(redeemScriptOutput)) {
|
|
throw new Error(
|
|
`Redeem script for input #${inputIndex} doesn't match the scriptPubKey in the prevout`,
|
|
);
|
|
}
|
|
};
|
|
|
|
export class Psbt extends PsbtBase {
|
|
constructor() {
|
|
super();
|
|
}
|
|
|
|
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
|
|
|
|
const input = this.inputs[inputIndex];
|
|
if (input === undefined) throw new Error(`No input #${inputIndex}`);
|
|
|
|
if (input.nonWitnessUtxo) {
|
|
const unsignedTx = Transaction.fromBuffer(this.globalMap.unsignedTx!);
|
|
const nonWitnessUtxoTx = Transaction.fromBuffer(input.nonWitnessUtxo);
|
|
|
|
const prevoutHash = unsignedTx.ins[inputIndex].hash;
|
|
const utxoHash = nonWitnessUtxoTx.getHash();
|
|
|
|
// If a non-witness UTXO is provided, its hash must match the hash specified in the prevout
|
|
if (!prevoutHash.equals(utxoHash)) {
|
|
throw new Error(
|
|
`Non-witness UTXO hash for input #${inputIndex} doesn't match the hash specified in the prevout`,
|
|
);
|
|
}
|
|
|
|
if (input.redeemScript) {
|
|
const prevoutIndex = unsignedTx.ins[inputIndex].index;
|
|
const prevout = nonWitnessUtxoTx.outs[prevoutIndex];
|
|
|
|
// If a redeemScript is provided, the scriptPubKey must be for that redeemScript
|
|
checkRedeemScript(inputIndex, prevout.script, input.redeemScript);
|
|
}
|
|
} else if (input.witnessUtxo) {
|
|
if (input.redeemScript) {
|
|
// If a redeemScript is provided, the scriptPubKey must be for that redeemScript
|
|
checkRedeemScript(
|
|
inputIndex,
|
|
input.witnessUtxo.script,
|
|
input.redeemScript,
|
|
);
|
|
}
|
|
}
|
|
|
|
// TODO: Get hash to sign
|
|
const hash = Buffer.alloc(32);
|
|
|
|
const partialSig = {
|
|
pubkey: keyPair.publicKey,
|
|
signature: script.signature.encode(
|
|
keyPair.sign(hash),
|
|
input.sighashType || 0x01,
|
|
),
|
|
};
|
|
|
|
return this.addPartialSigToInput(inputIndex, partialSig);
|
|
}
|
|
}
|
|
|