bitcoinjs-lib/ts_src/psbt.ts

1093 lines
30 KiB
TypeScript
Raw Normal View History

2019-06-24 12:20:15 +02:00
import { Psbt as PsbtBase } from 'bip174';
2019-07-09 06:02:34 +02:00
import * as varuint from 'bip174/src/lib/converter/varint';
2019-07-04 04:26:23 +02:00
import {
2019-07-05 05:28:04 +02:00
NonWitnessUtxo,
2019-07-04 04:26:23 +02:00
PartialSig,
PsbtInput,
2019-07-04 06:33:08 +02:00
TransactionInput,
2019-07-04 04:26:23 +02:00
TransactionOutput,
} from 'bip174/src/lib/interfaces';
2019-07-01 12:57:35 +02:00
import { checkForInput } from 'bip174/src/lib/utils';
2019-07-04 04:26:23 +02:00
import { toOutputScript } from './address';
2019-07-04 06:33:08 +02:00
import { reverseBuffer } from './bufferutils';
import { hash160 } from './crypto';
2019-07-08 08:46:06 +02:00
import {
fromPublicKey as ecPairFromPublicKey,
Signer,
SignerAsync,
} from './ecpair';
2019-07-04 04:26:23 +02:00
import { bitcoin as btcNetwork, Network } from './networks';
import * as payments from './payments';
2019-07-01 11:55:18 +02:00
import * as bscript from './script';
2019-07-05 05:28:04 +02:00
import { Output, Transaction } from './transaction';
2019-06-24 12:20:15 +02:00
2019-07-05 11:26:52 +02:00
const DEFAULT_OPTS: PsbtOpts = {
network: btcNetwork,
maximumFeeRate: 5000, // satoshi per byte
};
export class Psbt extends PsbtBase {
static fromTransaction<T extends typeof PsbtBase>(
this: T,
txBuf: Buffer,
): InstanceType<T> {
const tx = Transaction.fromBuffer(txBuf);
2019-07-04 07:45:50 +02:00
checkTxEmpty(tx);
const psbt = new this() as Psbt;
2019-07-09 04:51:28 +02:00
psbt.__CACHE.__TX = tx;
checkTxForDupeIns(tx, psbt.__CACHE);
let inputCount = tx.ins.length;
let outputCount = tx.outs.length;
while (inputCount > 0) {
psbt.inputs.push({
keyVals: [],
});
inputCount--;
}
while (outputCount > 0) {
psbt.outputs.push({
keyVals: [],
});
outputCount--;
}
return psbt as InstanceType<T>;
}
static fromBuffer<T extends typeof PsbtBase>(
this: T,
buffer: Buffer,
): InstanceType<T> {
let tx: Transaction | undefined;
const txCountGetter = (
txBuf: Buffer,
): {
inputCount: number;
outputCount: number;
} => {
tx = Transaction.fromBuffer(txBuf);
2019-07-04 07:45:50 +02:00
checkTxEmpty(tx);
return {
inputCount: tx.ins.length,
outputCount: tx.outs.length,
};
};
const psbt = super.fromBuffer(buffer, txCountGetter) as Psbt;
2019-07-09 04:51:28 +02:00
psbt.__CACHE.__TX = tx!;
checkTxForDupeIns(tx!, psbt.__CACHE);
return psbt as InstanceType<T>;
}
2019-07-09 04:51:28 +02:00
private __CACHE: PsbtCache = {
__NON_WITNESS_UTXO_TX_CACHE: [],
__NON_WITNESS_UTXO_BUF_CACHE: [],
__TX_IN_CACHE: {},
__TX: new Transaction(),
2019-07-05 07:30:08 +02:00
};
2019-07-04 04:26:23 +02:00
private opts: PsbtOpts;
2019-07-04 04:26:23 +02:00
constructor(opts: PsbtOptsOptional = {}) {
super();
2019-07-04 06:33:08 +02:00
// set defaults
2019-07-04 04:26:23 +02:00
this.opts = Object.assign({}, DEFAULT_OPTS, opts);
const c = this.__CACHE;
c.__TX = Transaction.fromBuffer(this.globalMap.unsignedTx!);
2019-07-04 06:42:34 +02:00
this.setVersion(2);
2019-07-04 06:33:08 +02:00
// set cache
delete this.globalMap.unsignedTx;
Object.defineProperty(this.globalMap, 'unsignedTx', {
enumerable: true,
get(): Buffer {
const buf = c.__TX_BUF_CACHE;
if (buf !== undefined) {
return buf;
2019-07-04 06:33:08 +02:00
} else {
c.__TX_BUF_CACHE = c.__TX.toBuffer();
return c.__TX_BUF_CACHE;
2019-07-04 06:33:08 +02:00
}
},
set(data: Buffer): void {
c.__TX_BUF_CACHE = data;
2019-07-04 06:33:08 +02:00
},
});
// Make data hidden when enumerating
const dpew = (
obj: any,
attr: string,
enumerable: boolean,
writable: boolean,
): any =>
Object.defineProperty(obj, attr, {
enumerable,
writable,
});
dpew(this, '__CACHE', false, true);
2019-07-04 06:33:08 +02:00
dpew(this, 'opts', false, true);
}
get inputCount(): number {
return this.inputs.length;
}
2019-07-09 11:03:15 +02:00
clone(): Psbt {
// TODO: more efficient cloning
const res = Psbt.fromBuffer(this.toBuffer());
res.opts = JSON.parse(JSON.stringify(this.opts));
return res;
}
2019-07-05 05:28:04 +02:00
setMaximumFeeRate(satoshiPerByte: number): void {
check32Bit(satoshiPerByte); // 42.9 BTC per byte IS excessive... so throw
this.opts.maximumFeeRate = satoshiPerByte;
}
2019-07-04 06:42:34 +02:00
setVersion(version: number): this {
2019-07-04 10:35:39 +02:00
check32Bit(version);
checkInputsForPartialSig(this.inputs, 'setVersion');
2019-07-09 04:51:28 +02:00
const c = this.__CACHE;
c.__TX.version = version;
c.__TX_BUF_CACHE = undefined;
c.__EXTRACTED_TX = undefined;
2019-07-04 06:42:34 +02:00
return this;
}
setLocktime(locktime: number): this {
2019-07-04 10:35:39 +02:00
check32Bit(locktime);
checkInputsForPartialSig(this.inputs, 'setLocktime');
2019-07-09 04:51:28 +02:00
const c = this.__CACHE;
c.__TX.locktime = locktime;
c.__TX_BUF_CACHE = undefined;
c.__EXTRACTED_TX = undefined;
2019-07-04 06:42:34 +02:00
return this;
}
2019-07-04 10:35:39 +02:00
setSequence(inputIndex: number, sequence: number): this {
check32Bit(sequence);
checkInputsForPartialSig(this.inputs, 'setSequence');
2019-07-09 04:51:28 +02:00
const c = this.__CACHE;
if (c.__TX.ins.length <= inputIndex) {
2019-07-04 10:35:39 +02:00
throw new Error('Input index too high');
}
2019-07-09 04:51:28 +02:00
c.__TX.ins[inputIndex].sequence = sequence;
c.__TX_BUF_CACHE = undefined;
c.__EXTRACTED_TX = undefined;
2019-07-04 10:35:39 +02:00
return this;
}
2019-07-09 11:03:15 +02:00
addInputs(inputDatas: TransactionInput[]): this {
inputDatas.forEach(inputData => this.addInput(inputData));
return this;
}
2019-07-04 06:33:08 +02:00
addInput(inputData: TransactionInput): this {
2019-07-04 10:35:39 +02:00
checkInputsForPartialSig(this.inputs, 'addInput');
2019-07-09 04:57:50 +02:00
const c = this.__CACHE;
const inputAdder = getInputAdder(c);
2019-07-05 05:51:13 +02:00
super.addInput(inputData, inputAdder);
2019-07-09 04:57:50 +02:00
c.__FEE_RATE = undefined;
c.__EXTRACTED_TX = undefined;
2019-07-05 05:51:13 +02:00
return this;
2019-07-04 06:33:08 +02:00
}
2019-07-09 11:03:15 +02:00
addOutputs(outputDatas: TransactionOutput[]): this {
outputDatas.forEach(outputData => this.addOutput(outputData));
return this;
}
2019-07-04 06:33:08 +02:00
addOutput(outputData: TransactionOutput): this {
2019-07-04 10:35:39 +02:00
checkInputsForPartialSig(this.inputs, 'addOutput');
2019-07-04 04:26:23 +02:00
const { address } = outputData as any;
if (typeof address === 'string') {
const { network } = this.opts;
const script = toOutputScript(address, network);
outputData = Object.assign(outputData, { script });
}
2019-07-09 04:57:50 +02:00
const c = this.__CACHE;
const outputAdder = getOutputAdder(c);
2019-07-05 05:51:13 +02:00
super.addOutput(outputData, true, outputAdder);
2019-07-09 04:57:50 +02:00
c.__FEE_RATE = undefined;
c.__EXTRACTED_TX = undefined;
2019-07-05 05:51:13 +02:00
return this;
2019-07-04 04:26:23 +02:00
}
2019-07-05 05:28:04 +02:00
addNonWitnessUtxoToInput(
inputIndex: number,
nonWitnessUtxo: NonWitnessUtxo,
): this {
super.addNonWitnessUtxoToInput(inputIndex, nonWitnessUtxo);
const input = this.inputs[inputIndex];
addNonWitnessTxCache(this.__CACHE, input, inputIndex);
2019-07-05 05:28:04 +02:00
return this;
}
extractTransaction(disableFeeCheck?: boolean): Transaction {
2019-07-03 08:13:36 +02:00
if (!this.inputs.every(isFinalized)) throw new Error('Not finalized');
const c = this.__CACHE;
2019-07-05 05:28:04 +02:00
if (!disableFeeCheck) {
checkFees(this, c, this.opts);
2019-07-05 05:28:04 +02:00
}
if (c.__EXTRACTED_TX) return c.__EXTRACTED_TX;
const tx = c.__TX.clone();
inputFinalizeGetAmts(this.inputs, tx, c, true);
2019-07-03 08:13:36 +02:00
return tx;
}
2019-07-05 05:28:04 +02:00
getFeeRate(): number {
if (!this.inputs.every(isFinalized))
throw new Error('PSBT must be finalized to calculate fee rate');
const c = this.__CACHE;
if (c.__FEE_RATE) return c.__FEE_RATE;
2019-07-05 05:28:04 +02:00
let tx: Transaction;
let mustFinalize = true;
if (c.__EXTRACTED_TX) {
tx = c.__EXTRACTED_TX;
2019-07-05 05:28:04 +02:00
mustFinalize = false;
} else {
tx = c.__TX.clone();
2019-07-05 05:28:04 +02:00
}
inputFinalizeGetAmts(this.inputs, tx, c, mustFinalize);
return c.__FEE_RATE!;
2019-07-05 05:28:04 +02:00
}
2019-07-09 08:45:56 +02:00
finalizeAllInputs(): this {
checkForInput(this.inputs, 0); // making sure we have at least one
range(this.inputs.length).forEach(idx => this.finalizeInput(idx));
return this;
2019-07-03 08:13:36 +02:00
}
2019-07-09 08:45:56 +02:00
finalizeInput(inputIndex: number): this {
const input = checkForInput(this.inputs, inputIndex);
2019-07-03 08:13:36 +02:00
const { script, isP2SH, isP2WSH, isSegwit } = getScriptFromInput(
inputIndex,
input,
this.__CACHE,
);
2019-07-09 08:45:56 +02:00
if (!script) throw new Error(`No script found for input #${inputIndex}`);
2019-07-03 08:13:36 +02:00
const scriptType = classifyScript(script);
2019-07-09 08:45:56 +02:00
if (!canFinalize(input, script, scriptType))
throw new Error(`Can not finalize input #${inputIndex}`);
2019-07-02 08:18:00 +02:00
2019-07-03 08:34:18 +02:00
const { finalScriptSig, finalScriptWitness } = getFinalScripts(
2019-07-03 08:13:36 +02:00
script,
scriptType,
input.partialSig!,
2019-07-03 08:34:18 +02:00
isSegwit,
isP2SH,
isP2WSH,
2019-07-03 08:13:36 +02:00
);
if (finalScriptSig)
this.addFinalScriptSigToInput(inputIndex, finalScriptSig);
if (finalScriptWitness)
this.addFinalScriptWitnessToInput(inputIndex, finalScriptWitness);
2019-07-09 08:45:56 +02:00
if (!finalScriptSig && !finalScriptWitness)
throw new Error(`Unknown error finalizing input #${inputIndex}`);
2019-07-03 08:13:36 +02:00
this.clearFinalizedInput(inputIndex);
2019-07-09 08:45:56 +02:00
return this;
}
validateAllSignatures(): boolean {
checkForInput(this.inputs, 0); // making sure we have at least one
const results = range(this.inputs.length).map(idx =>
this.validateSignatures(idx),
);
return results.reduce((final, res) => res === true && final, true);
}
2019-07-08 08:46:06 +02:00
validateSignatures(inputIndex: number, pubkey?: Buffer): boolean {
const input = this.inputs[inputIndex];
const partialSig = (input || {}).partialSig;
if (!input || !partialSig || partialSig.length < 1)
throw new Error('No signatures to validate');
const mySigs = pubkey
? partialSig.filter(sig => sig.pubkey.equals(pubkey))
: partialSig;
if (mySigs.length < 1) throw new Error('No signatures for this pubkey');
const results: boolean[] = [];
let hashCache: Buffer;
let scriptCache: Buffer;
let sighashCache: number;
for (const pSig of mySigs) {
const sig = bscript.signature.decode(pSig.signature);
const { hash, script } =
sighashCache! !== sig.hashType
? getHashForSig(
inputIndex,
Object.assign({}, input, { sighashType: sig.hashType }),
this.__CACHE,
)
: { hash: hashCache!, script: scriptCache! };
sighashCache = sig.hashType;
hashCache = hash;
scriptCache = script;
checkScriptForPubkey(pSig.pubkey, script, 'verify');
const keypair = ecPairFromPublicKey(pSig.pubkey);
results.push(keypair.verify(hash, sig.signature));
}
return results.every(res => res === true);
}
2019-07-08 09:30:59 +02:00
sign(keyPair: Signer): this {
if (!keyPair || !keyPair.publicKey)
throw new Error('Need Signer to sign input');
// TODO: Add a pubkey/pubkeyhash cache to each input
// as input information is added, then eventually
// optimize this method.
const results: boolean[] = [];
2019-07-09 08:45:56 +02:00
for (const i of range(this.inputs.length)) {
2019-07-08 09:30:59 +02:00
try {
this.signInput(i, keyPair);
results.push(true);
} catch (err) {
results.push(false);
}
}
if (results.every(v => v === false)) {
throw new Error('No inputs were signed');
}
return this;
}
signAsync(keyPair: SignerAsync): Promise<void> {
return new Promise(
(resolve, reject): any => {
if (!keyPair || !keyPair.publicKey)
return reject(new Error('Need Signer to sign input'));
// TODO: Add a pubkey/pubkeyhash cache to each input
// as input information is added, then eventually
// optimize this method.
const results: boolean[] = [];
const promises: Array<Promise<void>> = [];
for (const [i] of this.inputs.entries()) {
promises.push(
this.signInputAsync(i, keyPair).then(
() => {
results.push(true);
},
() => {
results.push(false);
},
),
);
}
return Promise.all(promises).then(() => {
if (results.every(v => v === false)) {
return reject(new Error('No inputs were signed'));
}
resolve();
});
},
);
}
signInput(inputIndex: number, keyPair: Signer): this {
2019-07-03 08:13:36 +02:00
if (!keyPair || !keyPair.publicKey)
throw new Error('Need Signer to sign input');
2019-07-03 08:34:18 +02:00
const { hash, sighashType } = getHashAndSighashType(
this.inputs,
inputIndex,
2019-07-03 08:34:18 +02:00
keyPair.publicKey,
this.__CACHE,
);
const partialSig = {
2019-07-03 08:34:18 +02:00
pubkey: keyPair.publicKey,
signature: bscript.signature.encode(keyPair.sign(hash), sighashType),
};
return this.addPartialSigToInput(inputIndex, partialSig);
}
2019-07-03 08:34:18 +02:00
2019-07-03 08:48:56 +02:00
signInputAsync(inputIndex: number, keyPair: SignerAsync): Promise<void> {
2019-07-04 06:52:48 +02:00
return new Promise(
(resolve, reject): void => {
if (!keyPair || !keyPair.publicKey)
return reject(new Error('Need Signer to sign input'));
const { hash, sighashType } = getHashAndSighashType(
this.inputs,
inputIndex,
keyPair.publicKey,
this.__CACHE,
2019-07-04 06:52:48 +02:00
);
2019-07-03 08:34:18 +02:00
2019-07-04 06:52:48 +02:00
Promise.resolve(keyPair.sign(hash)).then(signature => {
const partialSig = {
pubkey: keyPair.publicKey,
signature: bscript.signature.encode(signature, sighashType),
};
2019-07-03 08:34:18 +02:00
2019-07-04 06:52:48 +02:00
this.addPartialSigToInput(inputIndex, partialSig);
resolve();
});
},
);
2019-07-03 08:34:18 +02:00
}
}
interface PsbtCache {
2019-07-05 07:30:08 +02:00
__NON_WITNESS_UTXO_TX_CACHE: Transaction[];
__NON_WITNESS_UTXO_BUF_CACHE: Buffer[];
__TX_IN_CACHE: { [index: string]: number };
2019-07-09 04:51:28 +02:00
__TX: Transaction;
__TX_BUF_CACHE?: Buffer;
__FEE_RATE?: number;
__EXTRACTED_TX?: Transaction;
2019-07-05 07:30:08 +02:00
}
2019-07-04 04:26:23 +02:00
interface PsbtOptsOptional {
network?: Network;
2019-07-05 05:28:04 +02:00
maximumFeeRate?: number;
2019-07-04 04:26:23 +02:00
}
interface PsbtOpts {
network: Network;
2019-07-05 05:28:04 +02:00
maximumFeeRate: number;
2019-07-04 04:26:23 +02:00
}
function canFinalize(
2019-07-05 05:28:04 +02:00
input: PsbtInput,
script: Buffer,
scriptType: string,
): boolean {
switch (scriptType) {
case 'pubkey':
case 'pubkeyhash':
case 'witnesspubkeyhash':
return hasSigs(1, input.partialSig);
case 'multisig':
const p2ms = payments.p2ms({ output: script });
return hasSigs(p2ms.m!, input.partialSig);
default:
return false;
}
}
2019-07-05 05:28:04 +02:00
function hasSigs(neededSigs: number, partialSig?: any[]): boolean {
if (!partialSig) return false;
if (partialSig.length > neededSigs) throw new Error('Too many signatures');
return partialSig.length === neededSigs;
}
2019-07-05 05:28:04 +02:00
function isFinalized(input: PsbtInput): boolean {
return !!input.finalScriptSig || !!input.finalScriptWitness;
}
function isPaymentFactory(payment: any): (script: Buffer) => boolean {
return (script: Buffer): boolean => {
try {
payment({ output: script });
return true;
} catch (err) {
return false;
}
};
}
const isP2MS = isPaymentFactory(payments.p2ms);
const isP2PK = isPaymentFactory(payments.p2pk);
const isP2PKH = isPaymentFactory(payments.p2pkh);
const isP2WPKH = isPaymentFactory(payments.p2wpkh);
function check32Bit(num: number): void {
if (
typeof num !== 'number' ||
num !== Math.floor(num) ||
num > 0xffffffff ||
num < 0
) {
throw new Error('Invalid 32 bit integer');
}
}
function checkFees(psbt: Psbt, cache: PsbtCache, opts: PsbtOpts): void {
const feeRate = cache.__FEE_RATE || psbt.getFeeRate();
const vsize = cache.__EXTRACTED_TX!.virtualSize();
const satoshis = feeRate * vsize;
if (feeRate >= opts.maximumFeeRate) {
throw new Error(
`Warning: You are paying around ${(satoshis / 1e8).toFixed(8)} in ` +
`fees, which is ${feeRate} satoshi per byte for a transaction ` +
`with a VSize of ${vsize} bytes (segwit counted as 0.25 byte per ` +
`byte). Use setMaximumFeeRate method to raise your threshold, or ` +
`pass true to the first arg of extractTransaction.`,
);
}
}
function checkInputsForPartialSig(inputs: PsbtInput[], action: string): void {
inputs.forEach(input => {
let throws = false;
if ((input.partialSig || []).length === 0) return;
input.partialSig!.forEach(pSig => {
const { hashType } = bscript.signature.decode(pSig.signature);
const whitelist: string[] = [];
const isAnyoneCanPay = hashType & Transaction.SIGHASH_ANYONECANPAY;
if (isAnyoneCanPay) whitelist.push('addInput');
const hashMod = hashType & 0x1f;
switch (hashMod) {
case Transaction.SIGHASH_ALL:
break;
case Transaction.SIGHASH_SINGLE:
case Transaction.SIGHASH_NONE:
whitelist.push('addOutput');
whitelist.push('setSequence');
break;
2019-07-05 05:28:04 +02:00
}
if (whitelist.indexOf(action) === -1) {
throws = true;
}
});
if (throws) {
throw new Error('Can not modify transaction, signatures exist.');
}
2019-07-05 05:28:04 +02:00
});
}
function checkScriptForPubkey(
pubkey: Buffer,
script: Buffer,
action: string,
): void {
const pubkeyHash = hash160(pubkey);
const decompiled = bscript.decompile(script);
if (decompiled === null) throw new Error('Unknown script error');
const hasKey = decompiled.some(element => {
if (typeof element === 'number') return false;
return element.equals(pubkey) || element.equals(pubkeyHash);
});
if (!hasKey) {
throw new Error(
`Can not ${action} for this input with the key ${pubkey.toString('hex')}`,
);
}
}
function checkTxEmpty(tx: Transaction): void {
const isEmpty = tx.ins.every(
input =>
input.script &&
input.script.length === 0 &&
input.witness &&
input.witness.length === 0,
);
if (!isEmpty) {
throw new Error('Format Error: Transaction ScriptSigs are not empty');
}
}
function checkTxForDupeIns(tx: Transaction, cache: PsbtCache): void {
tx.ins.forEach(input => {
checkTxInputCache(cache, input);
});
}
function checkTxInputCache(
cache: PsbtCache,
input: { hash: Buffer; index: number },
): void {
const key =
reverseBuffer(Buffer.from(input.hash)).toString('hex') + ':' + input.index;
if (cache.__TX_IN_CACHE[key]) throw new Error('Duplicate input detected.');
cache.__TX_IN_CACHE[key] = 1;
}
function scriptCheckerFactory(
payment: any,
paymentScriptName: string,
): (idx: number, spk: Buffer, rs: Buffer) => void {
return (
inputIndex: number,
scriptPubKey: Buffer,
redeemScript: Buffer,
): void => {
const redeemScriptOutput = payment({
redeem: { output: redeemScript },
}).output as Buffer;
2019-07-03 08:13:36 +02:00
if (!scriptPubKey.equals(redeemScriptOutput)) {
throw new Error(
`${paymentScriptName} for input #${inputIndex} doesn't match the scriptPubKey in the prevout`,
);
}
2019-07-03 08:34:18 +02:00
};
}
const checkRedeemScript = scriptCheckerFactory(payments.p2sh, 'Redeem script');
const checkWitnessScript = scriptCheckerFactory(
payments.p2wsh,
'Witness script',
);
2019-07-03 08:34:18 +02:00
function getFinalScripts(
script: Buffer,
scriptType: string,
partialSig: PartialSig[],
isSegwit: boolean,
isP2SH: boolean,
isP2WSH: boolean,
): {
finalScriptSig: Buffer | undefined;
finalScriptWitness: Buffer | undefined;
} {
let finalScriptSig: Buffer | undefined;
let finalScriptWitness: Buffer | undefined;
// Wow, the payments API is very handy
const payment: payments.Payment = getPayment(script, scriptType, partialSig);
const p2wsh = !isP2WSH ? null : payments.p2wsh({ redeem: payment });
const p2sh = !isP2SH ? null : payments.p2sh({ redeem: p2wsh || payment });
if (isSegwit) {
if (p2wsh) {
finalScriptWitness = witnessStackToScriptWitness(p2wsh.witness!);
} else {
finalScriptWitness = witnessStackToScriptWitness(payment.witness!);
}
if (p2sh) {
2019-07-03 11:42:31 +02:00
finalScriptSig = p2sh.input;
2019-07-03 08:34:18 +02:00
}
} else {
2019-07-03 11:42:31 +02:00
if (p2sh) {
finalScriptSig = p2sh.input;
} else {
finalScriptSig = payment.input;
}
2019-07-03 08:34:18 +02:00
}
return {
finalScriptSig,
finalScriptWitness,
};
}
function getHashAndSighashType(
inputs: PsbtInput[],
inputIndex: number,
2019-07-08 08:46:06 +02:00
pubkey: Buffer,
cache: PsbtCache,
): {
hash: Buffer;
sighashType: number;
} {
const input = checkForInput(inputs, inputIndex);
const { hash, sighashType, script } = getHashForSig(inputIndex, input, cache);
checkScriptForPubkey(pubkey, script, 'sign');
return {
hash,
sighashType,
};
}
2019-07-09 03:57:41 +02:00
function getHashForSig(
inputIndex: number,
input: PsbtInput,
cache: PsbtCache,
): {
script: Buffer;
hash: Buffer;
sighashType: number;
} {
const unsignedTx = cache.__TX;
const sighashType = input.sighashType || Transaction.SIGHASH_ALL;
let hash: Buffer;
let script: Buffer;
if (input.nonWitnessUtxo) {
2019-07-09 04:29:20 +02:00
const nonWitnessUtxoTx = nonWitnessUtxoTxFromCache(
cache,
input,
inputIndex,
);
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`,
);
}
const prevoutIndex = unsignedTx.ins[inputIndex].index;
const prevout = nonWitnessUtxoTx.outs[prevoutIndex];
if (input.redeemScript) {
// If a redeemScript is provided, the scriptPubKey must be for that redeemScript
checkRedeemScript(inputIndex, prevout.script, input.redeemScript);
script = input.redeemScript;
hash = unsignedTx.hashForSignature(
inputIndex,
input.redeemScript,
sighashType,
);
} else {
script = prevout.script;
hash = unsignedTx.hashForSignature(
inputIndex,
prevout.script,
sighashType,
);
}
} else if (input.witnessUtxo) {
let _script: Buffer; // so we don't shadow the `let script` above
if (input.redeemScript) {
// If a redeemScript is provided, the scriptPubKey must be for that redeemScript
checkRedeemScript(
inputIndex,
input.witnessUtxo.script,
input.redeemScript,
);
_script = input.redeemScript;
} else {
_script = input.witnessUtxo.script;
}
if (isP2WPKH(_script)) {
// P2WPKH uses the P2PKH template for prevoutScript when signing
const signingScript = payments.p2pkh({ hash: _script.slice(2) }).output!;
hash = unsignedTx.hashForWitnessV0(
inputIndex,
signingScript,
input.witnessUtxo.value,
sighashType,
);
script = _script;
} else {
if (!input.witnessScript)
throw new Error('Segwit input needs witnessScript if not P2WPKH');
checkWitnessScript(inputIndex, _script, input.witnessScript);
hash = unsignedTx.hashForWitnessV0(
inputIndex,
2019-07-03 11:42:31 +02:00
input.witnessScript,
input.witnessUtxo.value,
sighashType,
);
// want to make sure the script we return is the actual meaningful script
script = input.witnessScript;
}
} else {
throw new Error('Need a Utxo input item for signing');
}
return {
script,
sighashType,
hash,
};
2019-07-09 03:57:41 +02:00
}
function getInputAdder(
cache: PsbtCache,
): (_inputData: TransactionInput, txBuf: Buffer) => Buffer {
const selfCache = cache;
return (_inputData: TransactionInput, txBuf: Buffer): Buffer => {
if (
!txBuf ||
(_inputData as any).hash === undefined ||
(_inputData as any).index === undefined ||
(!Buffer.isBuffer((_inputData as any).hash) &&
typeof (_inputData as any).hash !== 'string') ||
typeof (_inputData as any).index !== 'number'
) {
throw new Error('Error adding input.');
}
const prevHash = Buffer.isBuffer(_inputData.hash)
? _inputData.hash
: reverseBuffer(Buffer.from(_inputData.hash, 'hex'));
2019-07-01 11:55:18 +02:00
// Check if input already exists in cache.
const input = { hash: prevHash, index: _inputData.index };
checkTxInputCache(selfCache, input);
2019-07-09 03:57:41 +02:00
selfCache.__TX.ins.push({
...input,
script: Buffer.alloc(0),
sequence: _inputData.sequence || Transaction.DEFAULT_SEQUENCE,
witness: [],
});
return selfCache.__TX.toBuffer();
2019-07-09 03:57:41 +02:00
};
}
function getOutputAdder(
cache: PsbtCache,
): (_outputData: TransactionOutput, txBuf: Buffer) => Buffer {
const selfCache = cache;
return (_outputData: TransactionOutput, txBuf: Buffer): Buffer => {
if (
!txBuf ||
(_outputData as any).script === undefined ||
(_outputData as any).value === undefined ||
!Buffer.isBuffer((_outputData as any).script) ||
typeof (_outputData as any).value !== 'number'
) {
throw new Error('Error adding output.');
2019-07-09 03:57:41 +02:00
}
selfCache.__TX.outs.push({
script: (_outputData as any).script!,
value: _outputData.value,
});
return selfCache.__TX.toBuffer();
2019-07-09 03:57:41 +02:00
};
}
function getPayment(
script: Buffer,
scriptType: string,
partialSig: PartialSig[],
): payments.Payment {
let payment: payments.Payment;
switch (scriptType) {
case 'multisig':
const sigs = getSortedSigs(script, partialSig);
payment = payments.p2ms({
output: script,
signatures: sigs,
});
break;
case 'pubkey':
payment = payments.p2pk({
output: script,
signature: partialSig[0].signature,
});
break;
case 'pubkeyhash':
payment = payments.p2pkh({
output: script,
pubkey: partialSig[0].pubkey,
signature: partialSig[0].signature,
});
break;
case 'witnesspubkeyhash':
payment = payments.p2wpkh({
output: script,
pubkey: partialSig[0].pubkey,
signature: partialSig[0].signature,
});
break;
}
return payment!;
2019-07-09 03:57:41 +02:00
}
2019-07-01 11:55:18 +02:00
2019-07-03 08:13:36 +02:00
interface GetScriptReturn {
script: Buffer | null;
isSegwit: boolean;
isP2SH: boolean;
isP2WSH: boolean;
}
2019-07-01 12:57:35 +02:00
function getScriptFromInput(
inputIndex: number,
input: PsbtInput,
cache: PsbtCache,
2019-07-03 08:13:36 +02:00
): GetScriptReturn {
const unsignedTx = cache.__TX;
2019-07-03 08:13:36 +02:00
const res: GetScriptReturn = {
script: null,
isSegwit: false,
isP2SH: false,
isP2WSH: false,
};
2019-07-01 12:57:35 +02:00
if (input.nonWitnessUtxo) {
if (input.redeemScript) {
2019-07-03 08:13:36 +02:00
res.isP2SH = true;
res.script = input.redeemScript;
2019-07-01 12:57:35 +02:00
} else {
2019-07-09 04:29:20 +02:00
const nonWitnessUtxoTx = nonWitnessUtxoTxFromCache(
cache,
input,
inputIndex,
);
2019-07-01 12:57:35 +02:00
const prevoutIndex = unsignedTx.ins[inputIndex].index;
2019-07-03 08:13:36 +02:00
res.script = nonWitnessUtxoTx.outs[prevoutIndex].script;
2019-07-01 12:57:35 +02:00
}
} else if (input.witnessUtxo) {
2019-07-03 08:13:36 +02:00
res.isSegwit = true;
res.isP2SH = !!input.redeemScript;
res.isP2WSH = !!input.witnessScript;
2019-07-01 12:57:35 +02:00
if (input.witnessScript) {
2019-07-03 08:13:36 +02:00
res.script = input.witnessScript;
2019-07-01 12:57:35 +02:00
} else if (input.redeemScript) {
2019-07-03 11:42:31 +02:00
res.script = payments.p2wpkh({
2019-07-03 08:13:36 +02:00
hash: input.redeemScript.slice(2),
}).output!;
2019-07-01 12:57:35 +02:00
} else {
2019-07-03 11:42:31 +02:00
res.script = payments.p2wpkh({
2019-07-03 08:13:36 +02:00
hash: input.witnessUtxo.script.slice(2),
}).output!;
2019-07-01 12:57:35 +02:00
}
}
2019-07-03 08:13:36 +02:00
return res;
2019-07-01 12:57:35 +02:00
}
2019-07-03 08:13:36 +02:00
function getSortedSigs(script: Buffer, partialSig: PartialSig[]): Buffer[] {
const p2ms = payments.p2ms({ output: script });
// for each pubkey in order of p2ms script
return p2ms
.pubkeys!.map(pk => {
// filter partialSig array by pubkey being equal
return (
partialSig.filter(ps => {
return ps.pubkey.equals(pk);
})[0] || {}
).signature;
// Any pubkey without a match will return undefined
// this last filter removes all the undefined items in the array.
})
.filter(v => !!v);
2019-07-03 08:13:36 +02:00
}
2019-07-03 11:42:31 +02:00
function scriptWitnessToWitnessStack(buffer: Buffer): Buffer[] {
let offset = 0;
function readSlice(n: number): Buffer {
offset += n;
return buffer.slice(offset - n, offset);
}
function readVarInt(): number {
const vi = varuint.decode(buffer, offset);
2019-07-09 06:02:34 +02:00
offset += (varuint.decode as any).bytes;
2019-07-03 11:42:31 +02:00
return vi;
}
function readVarSlice(): Buffer {
return readSlice(readVarInt());
}
function readVector(): Buffer[] {
const count = readVarInt();
const vector: Buffer[] = [];
for (let i = 0; i < count; i++) vector.push(readVarSlice());
return vector;
}
return readVector();
}
function witnessStackToScriptWitness(witness: Buffer[]): Buffer {
let buffer = Buffer.allocUnsafe(0);
2019-07-04 07:45:50 +02:00
function writeSlice(slice: Buffer): void {
buffer = Buffer.concat([buffer, Buffer.from(slice)]);
2019-07-04 07:45:50 +02:00
}
2019-07-04 10:35:39 +02:00
function writeVarInt(i: number): void {
const currentLen = buffer.length;
const varintLen = varuint.encodingLength(i);
2019-07-04 10:35:39 +02:00
buffer = Buffer.concat([buffer, Buffer.allocUnsafe(varintLen)]);
varuint.encode(i, buffer, currentLen);
2019-07-09 04:29:20 +02:00
}
function writeVarSlice(slice: Buffer): void {
writeVarInt(slice.length);
writeSlice(slice);
}
2019-07-09 04:51:28 +02:00
function writeVector(vector: Buffer[]): void {
writeVarInt(vector.length);
vector.forEach(writeVarSlice);
}
2019-07-09 04:51:28 +02:00
writeVector(witness);
return buffer;
2019-07-09 04:51:28 +02:00
}
function addNonWitnessTxCache(
2019-07-09 04:57:50 +02:00
cache: PsbtCache,
input: PsbtInput,
inputIndex: number,
): void {
cache.__NON_WITNESS_UTXO_BUF_CACHE[inputIndex] = input.nonWitnessUtxo!;
2019-07-09 04:57:50 +02:00
const tx = Transaction.fromBuffer(input.nonWitnessUtxo!);
cache.__NON_WITNESS_UTXO_TX_CACHE[inputIndex] = tx;
const self = cache;
const selfIndex = inputIndex;
delete input.nonWitnessUtxo;
Object.defineProperty(input, 'nonWitnessUtxo', {
enumerable: true,
get(): Buffer {
const buf = self.__NON_WITNESS_UTXO_BUF_CACHE[selfIndex];
const txCache = self.__NON_WITNESS_UTXO_TX_CACHE[selfIndex];
if (buf !== undefined) {
return buf;
} else {
const newBuf = txCache.toBuffer();
self.__NON_WITNESS_UTXO_BUF_CACHE[selfIndex] = newBuf;
return newBuf;
}
},
set(data: Buffer): void {
self.__NON_WITNESS_UTXO_BUF_CACHE[selfIndex] = data;
},
});
}
function inputFinalizeGetAmts(
inputs: PsbtInput[],
tx: Transaction,
cache: PsbtCache,
mustFinalize: boolean,
): void {
let inputAmount = 0;
inputs.forEach((input, idx) => {
if (mustFinalize && input.finalScriptSig)
tx.ins[idx].script = input.finalScriptSig;
if (mustFinalize && input.finalScriptWitness) {
tx.ins[idx].witness = scriptWitnessToWitnessStack(
input.finalScriptWitness,
);
}
if (input.witnessUtxo) {
inputAmount += input.witnessUtxo.value;
} else if (input.nonWitnessUtxo) {
const nwTx = nonWitnessUtxoTxFromCache(cache, input, idx);
const vout = tx.ins[idx].index;
const out = nwTx.outs[vout] as Output;
inputAmount += out.value;
}
});
const outputAmount = (tx.outs as Output[]).reduce(
(total, o) => total + o.value,
0,
);
const fee = inputAmount - outputAmount;
if (fee < 0) {
throw new Error('Outputs are spending more than Inputs');
}
const bytes = tx.virtualSize();
cache.__EXTRACTED_TX = tx;
cache.__FEE_RATE = Math.floor(fee / bytes);
}
function nonWitnessUtxoTxFromCache(
cache: PsbtCache,
input: PsbtInput,
inputIndex: number,
): Transaction {
const c = cache.__NON_WITNESS_UTXO_TX_CACHE;
if (!c[inputIndex]) {
addNonWitnessTxCache(cache, input, inputIndex);
2019-07-04 10:35:39 +02:00
}
return c[inputIndex];
}
function classifyScript(script: Buffer): string {
if (isP2WPKH(script)) return 'witnesspubkeyhash';
if (isP2PKH(script)) return 'pubkeyhash';
if (isP2MS(script)) return 'multisig';
if (isP2PK(script)) return 'pubkey';
return 'nonstandard';
}
function range(n: number): number[] {
return [...Array(n).keys()];
2019-07-04 10:35:39 +02:00
}