bitcoinjs-lib/ts_src/psbt.ts

1828 lines
51 KiB
TypeScript
Raw Permalink 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 {
2020-04-29 07:39:50 +02:00
Bip32Derivation,
2019-07-11 10:17:49 +02:00
KeyValue,
2019-07-04 04:26:23 +02:00
PartialSig,
2019-07-18 04:43:24 +02:00
PsbtGlobalUpdate,
2019-07-04 04:26:23 +02:00
PsbtInput,
2019-07-18 04:43:24 +02:00
PsbtInputUpdate,
2019-08-07 10:37:52 +02:00
PsbtOutput,
2019-07-18 04:43:24 +02:00
PsbtOutputUpdate,
Transaction as ITransaction,
TransactionFromBuffer,
2019-07-04 04:26:23 +02:00
} from 'bip174/src/lib/interfaces';
import { checkForInput, checkForOutput } from 'bip174/src/lib/utils';
2020-04-26 13:05:18 +02:00
import { fromOutputScript, toOutputScript } from './address';
import { cloneBuffer, 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';
2020-04-26 13:15:06 +02:00
import { Output, Transaction } from './transaction';
2019-06-24 12:20:15 +02:00
export interface TransactionInput {
hash: string | Buffer;
index: number;
sequence?: number;
}
2020-04-27 11:46:07 +02:00
export interface PsbtTxInput extends TransactionInput {
hash: Buffer;
}
export interface TransactionOutput {
script: Buffer;
value: number;
}
2020-09-11 17:35:57 +02:00
export interface PsbtTxOutput extends TransactionOutput {
address: string | undefined;
2020-04-27 11:46:07 +02:00
}
/**
* These are the default arguments for a Psbt instance.
*/
2019-07-05 11:26:52 +02:00
const DEFAULT_OPTS: PsbtOpts = {
/**
* A bitcoinjs Network object. This is only used if you pass an `address`
* parameter to addOutput. Otherwise it is not needed and can be left default.
*/
2019-07-05 11:26:52 +02:00
network: btcNetwork,
/**
* When extractTransaction is called, the fee rate is checked.
* THIS IS NOT TO BE RELIED ON.
* It is only here as a last ditch effort to prevent sending a 500 BTC fee etc.
*/
2019-07-05 11:26:52 +02:00
maximumFeeRate: 5000, // satoshi per byte
};
/**
* Psbt class can parse and generate a PSBT binary based off of the BIP174.
* There are 6 roles that this class fulfills. (Explained in BIP174)
*
* Creator: This can be done with `new Psbt()`
* Updater: This can be done with `psbt.addInput(input)`, `psbt.addInputs(inputs)`,
* `psbt.addOutput(output)`, `psbt.addOutputs(outputs)` when you are looking to
* add new inputs and outputs to the PSBT, and `psbt.updateGlobal(itemObject)`,
* `psbt.updateInput(itemObject)`, `psbt.updateOutput(itemObject)`
* addInput requires hash: Buffer | string; and index: number; as attributes
* and can also include any attributes that are used in updateInput method.
* addOutput requires script: Buffer; and value: number; and likewise can include
* data for updateOutput.
* For a list of what attributes should be what types. Check the bip174 library.
* Also, check the integration tests for some examples of usage.
2019-07-19 10:21:31 +02:00
* Signer: There are a few methods. signAllInputs and signAllInputsAsync, which will search all input
* information for your pubkey or pubkeyhash, and only sign inputs where it finds
* your info. Or you can explicitly sign a specific input with signInput and
* signInputAsync. For the async methods you can create a SignerAsync object
* and use something like a hardware wallet to sign with. (You must implement this)
* Combiner: psbts can be combined easily with `psbt.combine(psbt2, psbt3, psbt4 ...)`
* the psbt calling combine will always have precedence when a conflict occurs.
* Combine checks if the internal bitcoin transaction is the same, so be sure that
* all sequences, version, locktime, etc. are the same before combining.
* Input Finalizer: This role is fairly important. Not only does it need to construct
* the input scriptSigs and witnesses, but it SHOULD verify the signatures etc.
2019-07-19 07:53:54 +02:00
* Before running `psbt.finalizeAllInputs()` please run `psbt.validateSignaturesOfAllInputs()`
* Running any finalize method will delete any data in the input(s) that are no longer
* needed due to the finalized scripts containing the information.
* Transaction Extractor: This role will perform some checks before returning a
* Transaction object. Such as fee rate not being larger than maximumFeeRate etc.
*/
2019-07-11 10:17:49 +02:00
export class Psbt {
static fromBase64(data: string, opts: PsbtOptsOptional = {}): Psbt {
const buffer = Buffer.from(data, 'base64');
return this.fromBuffer(buffer, opts);
}
static fromHex(data: string, opts: PsbtOptsOptional = {}): Psbt {
const buffer = Buffer.from(data, 'hex');
return this.fromBuffer(buffer, opts);
}
static fromBuffer(buffer: Buffer, opts: PsbtOptsOptional = {}): Psbt {
2019-07-18 04:43:24 +02:00
const psbtBase = PsbtBase.fromBuffer(buffer, transactionFromBuffer);
2019-07-11 10:17:49 +02:00
const psbt = new Psbt(opts, psbtBase);
2019-07-19 08:51:38 +02:00
checkTxForDupeIns(psbt.__CACHE.__TX, psbt.__CACHE);
2019-07-11 10:17:49 +02:00
return psbt;
}
2019-07-19 08:51:38 +02:00
private __CACHE: PsbtCache;
2019-07-04 04:26:23 +02:00
private opts: PsbtOpts;
2019-07-11 10:17:49 +02:00
constructor(
opts: PsbtOptsOptional = {},
2019-07-18 04:43:24 +02:00
readonly data: PsbtBase = new PsbtBase(new PsbtTransaction()),
2019-07-11 10:17:49 +02:00
) {
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);
2019-07-19 08:51:38 +02:00
this.__CACHE = {
__NON_WITNESS_UTXO_TX_CACHE: [],
__NON_WITNESS_UTXO_BUF_CACHE: [],
__TX_IN_CACHE: {},
__TX: (this.data.globalMap.unsignedTx as PsbtTransaction).tx,
// Old TransactionBuilder behavior was to not confirm input values
// before signing. Even though we highly encourage people to get
// the full parent transaction to verify values, the ability to
// sign non-segwit inputs without the full transaction was often
// requested. So the only way to activate is to use @ts-ignore.
// We will disable exporting the Psbt when unsafe sign is active.
// because it is not BIP174 compliant.
__UNSAFE_SIGN_NONSEGWIT: false,
2019-07-19 08:51:38 +02:00
};
2019-07-11 10:17:49 +02:00
if (this.data.inputs.length === 0) this.setVersion(2);
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 {
2019-07-11 10:17:49 +02:00
return this.data.inputs.length;
}
2020-04-26 10:34:11 +02:00
get version(): number {
return this.__CACHE.__TX.version;
}
2020-04-26 12:43:58 +02:00
set version(version: number) {
this.setVersion(version);
}
2020-04-26 10:34:11 +02:00
get locktime(): number {
return this.__CACHE.__TX.locktime;
}
2020-04-26 12:43:58 +02:00
set locktime(locktime: number) {
this.setLocktime(locktime);
}
2020-04-27 11:46:07 +02:00
get txInputs(): PsbtTxInput[] {
2020-04-26 10:37:57 +02:00
return this.__CACHE.__TX.ins.map(input => ({
hash: cloneBuffer(input.hash),
index: input.index,
sequence: input.sequence,
}));
}
2020-09-11 17:35:57 +02:00
get txOutputs(): PsbtTxOutput[] {
return this.__CACHE.__TX.outs.map(output => {
let address;
try {
address = fromOutputScript(output.script, this.opts.network);
} catch (_) {}
return {
script: cloneBuffer(output.script),
value: output.value,
address,
};
});
}
2019-07-11 10:17:49 +02:00
combine(...those: Psbt[]): this {
this.data.combine(...those.map(o => o.data));
return this;
}
2019-07-09 11:03:15 +02:00
clone(): Psbt {
// TODO: more efficient cloning
2019-07-11 10:17:49 +02:00
const res = Psbt.fromBuffer(this.data.toBuffer());
2019-07-09 11:03:15 +02:00
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);
2019-07-11 10:17:49 +02:00
checkInputsForPartialSig(this.data.inputs, 'setVersion');
2019-07-09 04:51:28 +02:00
const c = this.__CACHE;
c.__TX.version = version;
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);
2019-07-11 10:17:49 +02:00
checkInputsForPartialSig(this.data.inputs, 'setLocktime');
2019-07-09 04:51:28 +02:00
const c = this.__CACHE;
c.__TX.locktime = locktime;
c.__EXTRACTED_TX = undefined;
2019-07-04 06:42:34 +02:00
return this;
}
2019-07-19 07:53:54 +02:00
setInputSequence(inputIndex: number, sequence: number): this {
2019-07-04 10:35:39 +02:00
check32Bit(sequence);
2019-07-19 07:53:54 +02:00
checkInputsForPartialSig(this.data.inputs, 'setInputSequence');
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.__EXTRACTED_TX = undefined;
2019-07-04 10:35:39 +02:00
return this;
}
2019-08-07 10:37:52 +02:00
addInputs(inputDatas: PsbtInputExtended[]): this {
2019-07-09 11:03:15 +02:00
inputDatas.forEach(inputData => this.addInput(inputData));
return this;
}
2019-08-07 10:37:52 +02:00
addInput(inputData: PsbtInputExtended): this {
2019-10-01 07:54:57 +02:00
if (
arguments.length > 1 ||
!inputData ||
inputData.hash === undefined ||
inputData.index === undefined
) {
throw new Error(
`Invalid arguments for Psbt.addInput. ` +
`Requires single object with at least [hash] and [index]`,
);
}
2019-07-11 10:17:49 +02:00
checkInputsForPartialSig(this.data.inputs, 'addInput');
2020-04-29 04:05:33 +02:00
if (inputData.witnessScript) checkInvalidP2WSH(inputData.witnessScript);
2019-07-09 04:57:50 +02:00
const c = this.__CACHE;
2019-07-18 04:43:24 +02:00
this.data.addInput(inputData);
const txIn = c.__TX.ins[c.__TX.ins.length - 1];
checkTxInputCache(c, txIn);
2019-07-11 10:17:49 +02:00
const inputIndex = this.data.inputs.length - 1;
const input = this.data.inputs[inputIndex];
if (input.nonWitnessUtxo) {
addNonWitnessTxCache(this.__CACHE, input, inputIndex);
}
2019-08-26 12:15:05 +02:00
c.__FEE = undefined;
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-08-07 10:37:52 +02:00
addOutputs(outputDatas: PsbtOutputExtended[]): this {
2019-07-09 11:03:15 +02:00
outputDatas.forEach(outputData => this.addOutput(outputData));
return this;
}
2019-08-07 10:37:52 +02:00
addOutput(outputData: PsbtOutputExtended): this {
2019-10-01 07:54:57 +02:00
if (
arguments.length > 1 ||
!outputData ||
outputData.value === undefined ||
((outputData as any).address === undefined &&
(outputData as any).script === undefined)
) {
throw new Error(
`Invalid arguments for Psbt.addOutput. ` +
`Requires single object with at least [script or address] and [value]`,
);
}
2019-07-11 10:17:49 +02:00
checkInputsForPartialSig(this.data.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;
2019-07-18 04:43:24 +02:00
this.data.addOutput(outputData);
2019-08-26 12:15:05 +02:00
c.__FEE = undefined;
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
extractTransaction(disableFeeCheck?: boolean): Transaction {
2019-07-11 10:17:49 +02:00
if (!this.data.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();
2019-07-11 10:17:49 +02:00
inputFinalizeGetAmts(this.data.inputs, tx, c, true);
2019-07-03 08:13:36 +02:00
return tx;
}
2019-07-05 05:28:04 +02:00
getFeeRate(): number {
2019-08-26 12:15:05 +02:00
return getTxCacheValue(
'__FEE_RATE',
'fee rate',
this.data.inputs,
this.__CACHE,
)!;
}
getFee(): number {
return getTxCacheValue('__FEE', 'fee', this.data.inputs, this.__CACHE)!;
}
2019-07-09 08:45:56 +02:00
finalizeAllInputs(): this {
2019-07-11 10:17:49 +02:00
checkForInput(this.data.inputs, 0); // making sure we have at least one
range(this.data.inputs.length).forEach(idx => this.finalizeInput(idx));
2019-07-09 08:45:56 +02:00
return this;
2019-07-03 08:13:36 +02:00
}
finalizeInput(
inputIndex: number,
2019-10-28 03:40:56 +01:00
finalScriptsFunc: FinalScriptsFunc = getFinalScripts,
): this {
2019-07-11 10:17:49 +02:00
const input = checkForInput(this.data.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
checkPartialSigSighashes(input);
2019-10-28 03:40:56 +01:00
const { finalScriptSig, finalScriptWitness } = finalScriptsFunc(
inputIndex,
input,
2019-07-03 08:13:36 +02:00
script,
2019-07-03 08:34:18 +02:00
isSegwit,
isP2SH,
isP2WSH,
2019-07-03 08:13:36 +02:00
);
2019-07-18 04:43:24 +02:00
if (finalScriptSig) this.data.updateInput(inputIndex, { finalScriptSig });
2019-07-03 08:13:36 +02:00
if (finalScriptWitness)
2019-07-18 04:43:24 +02:00
this.data.updateInput(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
2019-07-11 10:17:49 +02:00
this.data.clearFinalizedInput(inputIndex);
2019-07-09 08:45:56 +02:00
return this;
}
2020-04-29 04:05:33 +02:00
getInputType(inputIndex: number): AllScriptType {
const input = checkForInput(this.data.inputs, inputIndex);
const script = getScriptFromUtxo(inputIndex, input, this.__CACHE);
const result = getMeaningfulScript(
script,
inputIndex,
'input',
input.redeemScript || redeemFromFinalScriptSig(input.finalScriptSig),
input.witnessScript ||
redeemFromFinalWitnessScript(input.finalScriptWitness),
2020-04-29 04:05:33 +02:00
);
const type = result.type === 'raw' ? '' : result.type + '-';
const mainType = classifyScript(result.meaningfulScript);
return (type + mainType) as AllScriptType;
}
inputHasPubkey(inputIndex: number, pubkey: Buffer): boolean {
const input = checkForInput(this.data.inputs, inputIndex);
return pubkeyInInput(pubkey, input, inputIndex, this.__CACHE);
}
2020-04-29 07:39:50 +02:00
inputHasHDKey(inputIndex: number, root: HDSigner): boolean {
const input = checkForInput(this.data.inputs, inputIndex);
const derivationIsMine = bip32DerivationIsMine(root);
return (
!!input.bip32Derivation && input.bip32Derivation.some(derivationIsMine)
);
}
outputHasPubkey(outputIndex: number, pubkey: Buffer): boolean {
const output = checkForOutput(this.data.outputs, outputIndex);
return pubkeyInOutput(pubkey, output, outputIndex, this.__CACHE);
}
2020-04-29 07:39:50 +02:00
outputHasHDKey(outputIndex: number, root: HDSigner): boolean {
const output = checkForOutput(this.data.outputs, outputIndex);
const derivationIsMine = bip32DerivationIsMine(root);
return (
!!output.bip32Derivation && output.bip32Derivation.some(derivationIsMine)
);
}
2019-07-19 07:53:54 +02:00
validateSignaturesOfAllInputs(): boolean {
2019-07-11 10:17:49 +02:00
checkForInput(this.data.inputs, 0); // making sure we have at least one
const results = range(this.data.inputs.length).map(idx =>
2019-07-19 07:53:54 +02:00
this.validateSignaturesOfInput(idx),
2019-07-09 08:45:56 +02:00
);
return results.reduce((final, res) => res === true && final, true);
}
2019-07-19 07:53:54 +02:00
validateSignaturesOfInput(inputIndex: number, pubkey?: Buffer): boolean {
2019-07-11 10:17:49 +02:00
const input = this.data.inputs[inputIndex];
2019-07-08 08:46:06 +02:00
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,
true,
2019-07-08 08:46:06 +02:00
)
: { 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-19 07:53:54 +02:00
signAllInputsHD(
2019-07-19 04:42:45 +02:00
hdKeyPair: HDSigner,
sighashTypes: number[] = [Transaction.SIGHASH_ALL],
): this {
if (!hdKeyPair || !hdKeyPair.publicKey || !hdKeyPair.fingerprint) {
throw new Error('Need HDSigner to sign input');
}
const results: boolean[] = [];
for (const i of range(this.data.inputs.length)) {
try {
this.signInputHD(i, hdKeyPair, sighashTypes);
results.push(true);
} catch (err) {
results.push(false);
}
}
if (results.every(v => v === false)) {
throw new Error('No inputs were signed');
}
return this;
}
2019-07-19 10:21:31 +02:00
signAllInputsHDAsync(
2019-07-19 04:42:45 +02:00
hdKeyPair: HDSigner | HDSignerAsync,
sighashTypes: number[] = [Transaction.SIGHASH_ALL],
): Promise<void> {
return new Promise(
(resolve, reject): any => {
if (!hdKeyPair || !hdKeyPair.publicKey || !hdKeyPair.fingerprint) {
return reject(new Error('Need HDSigner to sign input'));
}
const results: boolean[] = [];
const promises: Array<Promise<void>> = [];
for (const i of range(this.data.inputs.length)) {
promises.push(
this.signInputHDAsync(i, hdKeyPair, sighashTypes).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();
});
},
);
}
signInputHD(
inputIndex: number,
hdKeyPair: HDSigner,
sighashTypes: number[] = [Transaction.SIGHASH_ALL],
): this {
if (!hdKeyPair || !hdKeyPair.publicKey || !hdKeyPair.fingerprint) {
throw new Error('Need HDSigner to sign input');
}
const signers = getSignersFromHD(
inputIndex,
this.data.inputs,
hdKeyPair,
) as Signer[];
signers.forEach(signer => this.signInput(inputIndex, signer, sighashTypes));
return this;
}
signInputHDAsync(
inputIndex: number,
hdKeyPair: HDSigner | HDSignerAsync,
sighashTypes: number[] = [Transaction.SIGHASH_ALL],
): Promise<void> {
return new Promise(
(resolve, reject): any => {
if (!hdKeyPair || !hdKeyPair.publicKey || !hdKeyPair.fingerprint) {
return reject(new Error('Need HDSigner to sign input'));
}
const signers = getSignersFromHD(
inputIndex,
this.data.inputs,
hdKeyPair,
);
const promises = signers.map(signer =>
this.signInputAsync(inputIndex, signer, sighashTypes),
);
return Promise.all(promises)
.then(() => {
resolve();
})
.catch(reject);
},
);
}
2019-07-19 07:53:54 +02:00
signAllInputs(
2019-07-10 04:15:12 +02:00
keyPair: Signer,
sighashTypes: number[] = [Transaction.SIGHASH_ALL],
): this {
2019-07-08 09:30:59 +02:00
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-11 10:17:49 +02:00
for (const i of range(this.data.inputs.length)) {
2019-07-08 09:30:59 +02:00
try {
2019-07-10 04:15:12 +02:00
this.signInput(i, keyPair, sighashTypes);
2019-07-08 09:30:59 +02:00
results.push(true);
} catch (err) {
results.push(false);
}
}
if (results.every(v => v === false)) {
throw new Error('No inputs were signed');
}
return this;
}
2019-07-19 10:21:31 +02:00
signAllInputsAsync(
keyPair: Signer | SignerAsync,
2019-07-10 04:15:12 +02:00
sighashTypes: number[] = [Transaction.SIGHASH_ALL],
): Promise<void> {
2019-07-08 09:30:59 +02:00
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>> = [];
2019-07-11 10:17:49 +02:00
for (const [i] of this.data.inputs.entries()) {
2019-07-08 09:30:59 +02:00
promises.push(
2019-07-10 04:15:12 +02:00
this.signInputAsync(i, keyPair, sighashTypes).then(
2019-07-08 09:30:59 +02:00
() => {
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();
});
},
);
}
2019-07-10 04:15:12 +02:00
signInput(
inputIndex: number,
keyPair: Signer,
sighashTypes: number[] = [Transaction.SIGHASH_ALL],
): 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(
2019-07-11 10:17:49 +02:00
this.data.inputs,
inputIndex,
2019-07-03 08:34:18 +02:00
keyPair.publicKey,
this.__CACHE,
2019-07-10 04:15:12 +02:00
sighashTypes,
);
2019-07-18 04:43:24 +02:00
const partialSig = [
{
pubkey: keyPair.publicKey,
signature: bscript.signature.encode(keyPair.sign(hash), sighashType),
},
];
2019-07-18 04:43:24 +02:00
this.data.updateInput(inputIndex, { partialSig });
2019-07-11 10:17:49 +02:00
return this;
}
2019-07-03 08:34:18 +02:00
2019-07-10 04:15:12 +02:00
signInputAsync(
inputIndex: number,
keyPair: Signer | SignerAsync,
2019-07-10 04:15:12 +02:00
sighashTypes: number[] = [Transaction.SIGHASH_ALL],
): Promise<void> {
return Promise.resolve().then(() => {
if (!keyPair || !keyPair.publicKey)
throw new Error('Need Signer to sign input');
const { hash, sighashType } = getHashAndSighashType(
this.data.inputs,
inputIndex,
keyPair.publicKey,
this.__CACHE,
sighashTypes,
);
2019-07-03 08:34:18 +02:00
return Promise.resolve(keyPair.sign(hash)).then(signature => {
const partialSig = [
{
pubkey: keyPair.publicKey,
signature: bscript.signature.encode(signature, sighashType),
},
];
2019-07-18 04:43:24 +02:00
this.data.updateInput(inputIndex, { partialSig });
});
});
2019-07-03 08:34:18 +02:00
}
2019-07-11 10:17:49 +02:00
toBuffer(): Buffer {
checkCache(this.__CACHE);
2019-07-11 10:17:49 +02:00
return this.data.toBuffer();
}
toHex(): string {
checkCache(this.__CACHE);
2019-07-11 10:17:49 +02:00
return this.data.toHex();
}
toBase64(): string {
checkCache(this.__CACHE);
2019-07-11 10:17:49 +02:00
return this.data.toBase64();
}
2019-07-18 04:43:24 +02:00
updateGlobal(updateData: PsbtGlobalUpdate): this {
this.data.updateGlobal(updateData);
2019-07-11 10:17:49 +02:00
return this;
}
2019-07-18 04:43:24 +02:00
updateInput(inputIndex: number, updateData: PsbtInputUpdate): this {
2020-04-29 04:05:33 +02:00
if (updateData.witnessScript) checkInvalidP2WSH(updateData.witnessScript);
2019-07-18 04:43:24 +02:00
this.data.updateInput(inputIndex, updateData);
if (updateData.nonWitnessUtxo) {
addNonWitnessTxCache(
this.__CACHE,
this.data.inputs[inputIndex],
inputIndex,
);
}
2019-07-11 10:17:49 +02:00
return this;
}
2019-07-18 04:43:24 +02:00
updateOutput(outputIndex: number, updateData: PsbtOutputUpdate): this {
this.data.updateOutput(outputIndex, updateData);
2019-07-11 10:17:49 +02:00
return this;
}
addUnknownKeyValToGlobal(keyVal: KeyValue): this {
this.data.addUnknownKeyValToGlobal(keyVal);
return this;
}
addUnknownKeyValToInput(inputIndex: number, keyVal: KeyValue): this {
this.data.addUnknownKeyValToInput(inputIndex, keyVal);
return this;
}
addUnknownKeyValToOutput(outputIndex: number, keyVal: KeyValue): this {
this.data.addUnknownKeyValToOutput(outputIndex, keyVal);
return this;
}
clearFinalizedInput(inputIndex: number): this {
this.data.clearFinalizedInput(inputIndex);
return this;
}
}
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;
__FEE_RATE?: number;
2019-08-26 12:15:05 +02:00
__FEE?: number;
2019-07-09 04:51:28 +02:00
__EXTRACTED_TX?: Transaction;
__UNSAFE_SIGN_NONSEGWIT: boolean;
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
}
2019-08-07 10:37:52 +02:00
interface PsbtInputExtended extends PsbtInput, TransactionInput {}
2019-09-02 11:41:31 +02:00
type PsbtOutputExtended = PsbtOutputExtendedAddress | PsbtOutputExtendedScript;
interface PsbtOutputExtendedAddress extends PsbtOutput {
address: string;
value: number;
}
interface PsbtOutputExtendedScript extends PsbtOutput {
script: Buffer;
value: number;
}
2019-08-07 10:37:52 +02:00
2019-07-19 04:42:45 +02:00
interface HDSignerBase {
/**
* DER format compressed publicKey buffer
*/
publicKey: Buffer;
/**
* The first 4 bytes of the sha256-ripemd160 of the publicKey
*/
fingerprint: Buffer;
}
interface HDSigner extends HDSignerBase {
/**
* The path string must match /^m(\/\d+'?)+$/
* ex. m/44'/0'/0'/1/23 levels with ' must be hard derivations
*/
derivePath(path: string): HDSigner;
/**
* Input hash (the "message digest") for the signature algorithm
* Return a 64 byte signature (32 byte r and 32 byte s in that order)
*/
sign(hash: Buffer): Buffer;
}
/**
* Same as above but with async sign method
*/
interface HDSignerAsync extends HDSignerBase {
derivePath(path: string): HDSignerAsync;
sign(hash: Buffer): Promise<Buffer>;
}
/**
* This function is needed to pass to the bip174 base class's fromBuffer.
* It takes the "transaction buffer" portion of the psbt buffer and returns a
* Transaction (From the bip174 library) interface.
*/
2019-07-18 04:43:24 +02:00
const transactionFromBuffer: TransactionFromBuffer = (
buffer: Buffer,
): ITransaction => new PsbtTransaction(buffer);
/**
* This class implements the Transaction interface from bip174 library.
* It contains a bitcoinjs-lib Transaction object.
*/
2019-07-18 04:43:24 +02:00
class PsbtTransaction implements ITransaction {
tx: Transaction;
constructor(buffer: Buffer = Buffer.from([2, 0, 0, 0, 0, 0, 0, 0, 0, 0])) {
this.tx = Transaction.fromBuffer(buffer);
checkTxEmpty(this.tx);
2019-07-18 04:43:24 +02:00
Object.defineProperty(this, 'tx', {
enumerable: false,
writable: true,
});
}
getInputOutputCounts(): {
inputCount: number;
outputCount: number;
} {
return {
inputCount: this.tx.ins.length,
outputCount: this.tx.outs.length,
};
}
addInput(input: any): void {
if (
(input as any).hash === undefined ||
(input as any).index === undefined ||
(!Buffer.isBuffer((input as any).hash) &&
typeof (input as any).hash !== 'string') ||
typeof (input as any).index !== 'number'
) {
throw new Error('Error adding input.');
}
const hash =
typeof input.hash === 'string'
? reverseBuffer(Buffer.from(input.hash, 'hex'))
: input.hash;
this.tx.addInput(hash, input.index, input.sequence);
}
addOutput(output: any): void {
if (
(output as any).script === undefined ||
(output as any).value === undefined ||
!Buffer.isBuffer((output as any).script) ||
typeof (output as any).value !== 'number'
) {
throw new Error('Error adding output.');
}
this.tx.addOutput(output.script, output.value);
}
toBuffer(): Buffer {
return this.tx.toBuffer();
}
}
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, p2ms.pubkeys);
default:
return false;
}
}
2019-07-05 05:28:04 +02:00
function checkCache(cache: PsbtCache): void {
if (cache.__UNSAFE_SIGN_NONSEGWIT !== false) {
throw new Error('Not BIP174 compliant, can not export');
}
}
function hasSigs(
neededSigs: number,
partialSig?: any[],
pubkeys?: Buffer[],
): boolean {
if (!partialSig) return false;
let sigs: any;
if (pubkeys) {
sigs = pubkeys
.map(pkey => {
const pubkey = ecPairFromPublicKey(pkey, { compressed: true })
.publicKey;
return partialSig.find(pSig => pSig.pubkey.equals(pubkey));
})
.filter(v => !!v);
} else {
sigs = partialSig;
}
if (sigs.length > neededSigs) throw new Error('Too many signatures');
return sigs.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);
const isP2WSHScript = isPaymentFactory(payments.p2wsh);
const isP2SHScript = isPaymentFactory(payments.p2sh);
2020-04-29 07:39:50 +02:00
function bip32DerivationIsMine(
root: HDSigner,
): (d: Bip32Derivation) => boolean {
return (d: Bip32Derivation): boolean => {
if (!d.masterFingerprint.equals(root.fingerprint)) return false;
if (!root.derivePath(d.path).publicKey.equals(d.pubkey)) return false;
return true;
};
}
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;
let pSigs: PartialSig[] = [];
if ((input.partialSig || []).length === 0) {
if (!input.finalScriptSig && !input.finalScriptWitness) return;
pSigs = getPsigsFromInputFinalScripts(input);
} else {
pSigs = input.partialSig!;
}
pSigs.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');
2019-07-19 07:53:54 +02:00
whitelist.push('setInputSequence');
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 checkPartialSigSighashes(input: PsbtInput): void {
if (!input.sighashType || !input.partialSig) return;
const { partialSig, sighashType } = input;
partialSig.forEach(pSig => {
const { hashType } = bscript.signature.decode(pSig.signature);
if (sighashType !== hashType) {
throw new Error('Signature sighash does not match input sighash type');
}
});
}
function checkScriptForPubkey(
pubkey: Buffer,
script: Buffer,
action: string,
): void {
if (!pubkeyInScript(pubkey, script)) {
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,
2020-04-28 11:50:00 +02:00
): (idx: number, spk: Buffer, rs: Buffer, ioType: 'input' | 'output') => void {
return (
inputIndex: number,
scriptPubKey: Buffer,
redeemScript: Buffer,
2020-04-28 11:50:00 +02:00
ioType: 'input' | 'output',
): 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(
2020-04-28 11:50:00 +02:00
`${paymentScriptName} for ${ioType} #${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
2019-08-27 03:06:43 +02:00
type TxCacheNumberKey = '__FEE_RATE' | '__FEE';
2019-08-26 12:15:05 +02:00
function getTxCacheValue(
key: TxCacheNumberKey,
name: string,
inputs: PsbtInput[],
c: PsbtCache,
): number | undefined {
if (!inputs.every(isFinalized))
throw new Error(`PSBT must be finalized to calculate ${name}`);
if (key === '__FEE_RATE' && c.__FEE_RATE) return c.__FEE_RATE;
if (key === '__FEE' && c.__FEE) return c.__FEE;
let tx: Transaction;
let mustFinalize = true;
if (c.__EXTRACTED_TX) {
tx = c.__EXTRACTED_TX;
mustFinalize = false;
} else {
tx = c.__TX.clone();
}
inputFinalizeGetAmts(inputs, tx, c, mustFinalize);
if (key === '__FEE_RATE') return c.__FEE_RATE!;
else if (key === '__FEE') return c.__FEE!;
}
2019-10-28 03:40:56 +01:00
/**
* This function must do two things:
* 1. Check if the `input` can be finalized. If it can not be finalized, throw.
* ie. `Can not finalize input #${inputIndex}`
* 2. Create the finalScriptSig and finalScriptWitness Buffers.
*/
type FinalScriptsFunc = (
inputIndex: number, // Which input is it?
input: PsbtInput, // The PSBT input contents
script: Buffer, // The "meaningful" locking script Buffer (redeemScript for P2SH etc.)
isSegwit: boolean, // Is it segwit?
isP2SH: boolean, // Is it P2SH?
isP2WSH: boolean, // Is it P2WSH?
) => {
finalScriptSig: Buffer | undefined;
finalScriptWitness: Buffer | undefined;
};
2019-07-03 08:34:18 +02:00
function getFinalScripts(
2019-10-28 03:40:56 +01:00
inputIndex: number,
input: PsbtInput,
script: Buffer,
isSegwit: boolean,
isP2SH: boolean,
isP2WSH: boolean,
): {
finalScriptSig: Buffer | undefined;
finalScriptWitness: Buffer | undefined;
} {
const scriptType = classifyScript(script);
if (!canFinalize(input, script, scriptType))
throw new Error(`Can not finalize input #${inputIndex}`);
return prepareFinalScripts(
script,
scriptType,
input.partialSig!,
isSegwit,
isP2SH,
isP2WSH,
);
}
function prepareFinalScripts(
2019-07-03 08:34:18 +02:00
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,
2019-07-10 04:15:12 +02:00
sighashTypes: number[],
): {
hash: Buffer;
sighashType: number;
} {
const input = checkForInput(inputs, inputIndex);
2019-07-10 04:15:12 +02:00
const { hash, sighashType, script } = getHashForSig(
inputIndex,
input,
cache,
false,
2019-07-10 04:15:12 +02:00
sighashTypes,
);
checkScriptForPubkey(pubkey, script, 'sign');
return {
hash,
sighashType,
};
}
2019-07-09 03:57:41 +02:00
function getHashForSig(
inputIndex: number,
input: PsbtInput,
cache: PsbtCache,
forValidate: boolean,
2019-07-10 04:15:12 +02:00
sighashTypes?: number[],
): {
script: Buffer;
hash: Buffer;
sighashType: number;
} {
const unsignedTx = cache.__TX;
const sighashType = input.sighashType || Transaction.SIGHASH_ALL;
2019-07-10 04:15:12 +02:00
if (sighashTypes && sighashTypes.indexOf(sighashType) < 0) {
const str = sighashTypeToString(sighashType);
throw new Error(
`Sighash type is not allowed. Retry the sign method passing the ` +
`sighashTypes array of whitelisted types. Sighash type: ${str}`,
);
}
let hash: Buffer;
2020-04-28 11:50:00 +02:00
let prevout: Output;
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;
2020-04-28 11:50:00 +02:00
prevout = nonWitnessUtxoTx.outs[prevoutIndex] as Output;
} else if (input.witnessUtxo) {
prevout = input.witnessUtxo;
} else {
throw new Error('Need a Utxo input item for signing');
}
2020-04-28 11:50:00 +02:00
const { meaningfulScript, type } = getMeaningfulScript(
prevout.script,
inputIndex,
'input',
input.redeemScript,
input.witnessScript,
);
2020-04-29 04:05:33 +02:00
if (['p2sh-p2wsh', 'p2wsh'].indexOf(type) >= 0) {
2020-04-28 11:50:00 +02:00
hash = unsignedTx.hashForWitnessV0(
inputIndex,
meaningfulScript,
prevout.value,
sighashType,
);
} else if (isP2WPKH(meaningfulScript)) {
// P2WPKH uses the P2PKH template for prevoutScript when signing
const signingScript = payments.p2pkh({ hash: meaningfulScript.slice(2) })
.output!;
hash = unsignedTx.hashForWitnessV0(
inputIndex,
signingScript,
prevout.value,
sighashType,
);
} else {
// non-segwit
if (
input.nonWitnessUtxo === undefined &&
cache.__UNSAFE_SIGN_NONSEGWIT === false
)
throw new Error(
`Input #${inputIndex} has witnessUtxo but non-segwit script: ` +
2020-04-28 11:50:00 +02:00
`${meaningfulScript.toString('hex')}`,
);
if (!forValidate && cache.__UNSAFE_SIGN_NONSEGWIT !== false)
console.warn(
'Warning: Signing non-segwit inputs without the full parent transaction ' +
'means there is a chance that a miner could feed you incorrect information ' +
'to trick you into paying large fees. This behavior is the same as the old ' +
'TransactionBuilder class when signing non-segwit scripts. You are not ' +
'able to export this Psbt with toBuffer|toBase64|toHex since it is not ' +
'BIP174 compliant.\n*********************\nPROCEED WITH CAUTION!\n' +
'*********************',
);
2020-04-28 11:50:00 +02:00
hash = unsignedTx.hashForSignature(
inputIndex,
meaningfulScript,
sighashType,
);
}
2020-04-28 11:50:00 +02:00
return {
2020-04-28 11:50:00 +02:00
script: meaningfulScript,
sighashType,
hash,
};
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
function getPsigsFromInputFinalScripts(input: PsbtInput): PartialSig[] {
const scriptItems = !input.finalScriptSig
? []
: bscript.decompile(input.finalScriptSig) || [];
const witnessItems = !input.finalScriptWitness
? []
: bscript.decompile(input.finalScriptWitness) || [];
return scriptItems
.concat(witnessItems)
.filter(item => {
return Buffer.isBuffer(item) && bscript.isCanonicalScriptSignature(item);
})
.map(sig => ({ signature: sig })) as PartialSig[];
}
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-08-23 05:52:04 +02:00
res.isP2SH = !!input.redeemScript;
res.isP2WSH = !!input.witnessScript;
if (input.witnessScript) {
res.script = input.witnessScript;
} else if (input.redeemScript) {
res.script = input.redeemScript;
} else {
if (input.nonWitnessUtxo) {
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-08-23 05:52:04 +02:00
} else if (input.witnessUtxo) {
res.script = input.witnessUtxo.script;
2019-07-01 12:57:35 +02:00
}
2019-08-23 05:52:04 +02:00
}
if (input.witnessScript || isP2WPKH(res.script!)) {
2019-07-03 08:13:36 +02:00
res.isSegwit = true;
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
2019-07-19 04:42:45 +02:00
function getSignersFromHD(
inputIndex: number,
inputs: PsbtInput[],
hdKeyPair: HDSigner | HDSignerAsync,
): Array<Signer | SignerAsync> {
const input = checkForInput(inputs, inputIndex);
if (!input.bip32Derivation || input.bip32Derivation.length === 0) {
throw new Error('Need bip32Derivation to sign with HD');
}
const myDerivations = input.bip32Derivation
.map(bipDv => {
if (bipDv.masterFingerprint.equals(hdKeyPair.fingerprint)) {
return bipDv;
} else {
return;
}
})
.filter(v => !!v);
if (myDerivations.length === 0) {
throw new Error(
'Need one bip32Derivation masterFingerprint to match the HDSigner fingerprint',
);
}
const signers: Array<Signer | SignerAsync> = myDerivations.map(bipDv => {
const node = hdKeyPair.derivePath(bipDv!.path);
if (!bipDv!.pubkey.equals(node.publicKey)) {
throw new Error('pubkey did not match bip32Derivation');
}
return node;
});
return signers;
}
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();
}
2019-07-10 04:15:12 +02:00
function sighashTypeToString(sighashType: number): string {
let text =
sighashType & Transaction.SIGHASH_ANYONECANPAY
? 'SIGHASH_ANYONECANPAY | '
: '';
const sigMod = sighashType & 0x1f;
switch (sigMod) {
case Transaction.SIGHASH_ALL:
text += 'SIGHASH_ALL';
break;
case Transaction.SIGHASH_SINGLE:
text += 'SIGHASH_SINGLE';
break;
case Transaction.SIGHASH_NONE:
text += 'SIGHASH_NONE';
break;
}
return text;
}
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();
2019-08-26 12:15:05 +02:00
cache.__FEE = fee;
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];
}
2020-04-29 04:05:33 +02:00
function getScriptFromUtxo(
inputIndex: number,
2020-04-29 04:05:33 +02:00
input: PsbtInput,
cache: PsbtCache,
2020-04-29 04:05:33 +02:00
): Buffer {
if (input.witnessUtxo !== undefined) {
2020-04-29 04:05:33 +02:00
return input.witnessUtxo.script;
} else if (input.nonWitnessUtxo !== undefined) {
const nonWitnessUtxoTx = nonWitnessUtxoTxFromCache(
cache,
input,
inputIndex,
);
2020-04-29 04:05:33 +02:00
return nonWitnessUtxoTx.outs[cache.__TX.ins[inputIndex].index].script;
} else {
throw new Error("Can't find pubkey in input without Utxo data");
}
2020-04-29 04:05:33 +02:00
}
function pubkeyInInput(
pubkey: Buffer,
input: PsbtInput,
inputIndex: number,
cache: PsbtCache,
): boolean {
const script = getScriptFromUtxo(inputIndex, input, cache);
2020-04-28 11:50:00 +02:00
const { meaningfulScript } = getMeaningfulScript(
script,
2020-04-28 11:50:00 +02:00
inputIndex,
'input',
input.redeemScript,
input.witnessScript,
);
return pubkeyInScript(pubkey, meaningfulScript);
}
function pubkeyInOutput(
pubkey: Buffer,
output: PsbtOutput,
outputIndex: number,
cache: PsbtCache,
): boolean {
const script = cache.__TX.outs[outputIndex].script;
2020-04-28 11:50:00 +02:00
const { meaningfulScript } = getMeaningfulScript(
script,
2020-04-28 11:50:00 +02:00
outputIndex,
'output',
output.redeemScript,
output.witnessScript,
);
return pubkeyInScript(pubkey, meaningfulScript);
}
function redeemFromFinalScriptSig(
finalScript: Buffer | undefined,
): Buffer | undefined {
if (!finalScript) return;
const decomp = bscript.decompile(finalScript);
if (!decomp) return;
const lastItem = decomp[decomp.length - 1];
if (
!Buffer.isBuffer(lastItem) ||
isPubkeyLike(lastItem) ||
isSigLike(lastItem)
)
return;
const sDecomp = bscript.decompile(lastItem);
if (!sDecomp) return;
return lastItem;
}
function redeemFromFinalWitnessScript(
finalScript: Buffer | undefined,
): Buffer | undefined {
if (!finalScript) return;
const decomp = scriptWitnessToWitnessStack(finalScript);
const lastItem = decomp[decomp.length - 1];
if (isPubkeyLike(lastItem)) return;
const sDecomp = bscript.decompile(lastItem);
if (!sDecomp) return;
return lastItem;
}
function isPubkeyLike(buf: Buffer): boolean {
return buf.length === 33 && bscript.isCanonicalPubKey(buf);
}
function isSigLike(buf: Buffer): boolean {
return bscript.isCanonicalScriptSignature(buf);
}
2020-04-28 07:41:48 +02:00
function getMeaningfulScript(
script: Buffer,
2020-04-28 11:50:00 +02:00
index: number,
ioType: 'input' | 'output',
redeemScript?: Buffer,
witnessScript?: Buffer,
2020-04-28 11:50:00 +02:00
): {
meaningfulScript: Buffer;
2020-04-29 04:05:33 +02:00
type: 'p2sh' | 'p2wsh' | 'p2sh-p2wsh' | 'raw';
2020-04-28 11:50:00 +02:00
} {
2020-04-28 07:41:48 +02:00
const isP2SH = isP2SHScript(script);
const isP2SHP2WSH = isP2SH && redeemScript && isP2WSHScript(redeemScript);
const isP2WSH = isP2WSHScript(script);
if (isP2SH && redeemScript === undefined)
throw new Error('scriptPubkey is P2SH but redeemScript missing');
if ((isP2WSH || isP2SHP2WSH) && witnessScript === undefined)
throw new Error(
'scriptPubkey or redeemScript is P2WSH but witnessScript missing',
);
let meaningfulScript: Buffer;
if (isP2SHP2WSH) {
meaningfulScript = witnessScript!;
2020-04-28 11:50:00 +02:00
checkRedeemScript(index, script, redeemScript!, ioType);
checkWitnessScript(index, redeemScript!, witnessScript!, ioType);
2020-04-29 04:05:33 +02:00
checkInvalidP2WSH(meaningfulScript);
2020-04-28 07:41:48 +02:00
} else if (isP2WSH) {
meaningfulScript = witnessScript!;
2020-04-28 11:50:00 +02:00
checkWitnessScript(index, script, witnessScript!, ioType);
2020-04-29 04:05:33 +02:00
checkInvalidP2WSH(meaningfulScript);
2020-04-28 07:41:48 +02:00
} else if (isP2SH) {
meaningfulScript = redeemScript!;
2020-04-28 11:50:00 +02:00
checkRedeemScript(index, script, redeemScript!, ioType);
2020-04-28 07:41:48 +02:00
} else {
meaningfulScript = script;
}
2020-04-28 11:50:00 +02:00
return {
meaningfulScript,
type: isP2SHP2WSH
2020-04-29 04:05:33 +02:00
? 'p2sh-p2wsh'
2020-04-28 11:50:00 +02:00
: isP2SH
? 'p2sh'
: isP2WSH
? 'p2wsh'
: 'raw',
};
}
2020-04-29 04:05:33 +02:00
function checkInvalidP2WSH(script: Buffer): void {
if (isP2WPKH(script) || isP2SHScript(script)) {
throw new Error('P2WPKH or P2SH can not be contained within P2WSH');
}
}
function pubkeyInScript(pubkey: Buffer, script: Buffer): boolean {
const pubkeyHash = hash160(pubkey);
const decompiled = bscript.decompile(script);
if (decompiled === null) throw new Error('Unknown script error');
return decompiled.some(element => {
if (typeof element === 'number') return false;
return element.equals(pubkey) || element.equals(pubkeyHash);
});
}
2020-04-29 04:05:33 +02:00
type AllScriptType =
| 'witnesspubkeyhash'
| 'pubkeyhash'
| 'multisig'
| 'pubkey'
| 'nonstandard'
| 'p2sh-witnesspubkeyhash'
| 'p2sh-pubkeyhash'
| 'p2sh-multisig'
| 'p2sh-pubkey'
| 'p2sh-nonstandard'
| 'p2wsh-pubkeyhash'
| 'p2wsh-multisig'
| 'p2wsh-pubkey'
| 'p2wsh-nonstandard'
| 'p2sh-p2wsh-pubkeyhash'
| 'p2sh-p2wsh-multisig'
| 'p2sh-p2wsh-pubkey'
| 'p2sh-p2wsh-nonstandard';
type ScriptType =
| 'witnesspubkeyhash'
| 'pubkeyhash'
| 'multisig'
| 'pubkey'
| 'nonstandard';
function classifyScript(script: Buffer): ScriptType {
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
}