UNPKG

7.97 kBTypeScriptView Raw
1import { Psbt as PsbtBase } from 'bip174';
2import { KeyValue, PsbtGlobalUpdate, PsbtInput, PsbtInputUpdate, PsbtOutput, PsbtOutputUpdate, TransactionInput, TransactionOutput } from 'bip174/src/lib/interfaces';
3import { Signer, SignerAsync } from './ecpair';
4import { Network } from './networks';
5import { Transaction } from './transaction';
6export interface PsbtTxInput extends TransactionInput {
7 hash: Buffer;
8}
9export interface PsbtTxOutput extends TransactionOutput {
10 address: string | undefined;
11}
12/**
13 * Psbt class can parse and generate a PSBT binary based off of the BIP174.
14 * There are 6 roles that this class fulfills. (Explained in BIP174)
15 *
16 * Creator: This can be done with `new Psbt()`
17 * Updater: This can be done with `psbt.addInput(input)`, `psbt.addInputs(inputs)`,
18 * `psbt.addOutput(output)`, `psbt.addOutputs(outputs)` when you are looking to
19 * add new inputs and outputs to the PSBT, and `psbt.updateGlobal(itemObject)`,
20 * `psbt.updateInput(itemObject)`, `psbt.updateOutput(itemObject)`
21 * addInput requires hash: Buffer | string; and index: number; as attributes
22 * and can also include any attributes that are used in updateInput method.
23 * addOutput requires script: Buffer; and value: number; and likewise can include
24 * data for updateOutput.
25 * For a list of what attributes should be what types. Check the bip174 library.
26 * Also, check the integration tests for some examples of usage.
27 * Signer: There are a few methods. signAllInputs and signAllInputsAsync, which will search all input
28 * information for your pubkey or pubkeyhash, and only sign inputs where it finds
29 * your info. Or you can explicitly sign a specific input with signInput and
30 * signInputAsync. For the async methods you can create a SignerAsync object
31 * and use something like a hardware wallet to sign with. (You must implement this)
32 * Combiner: psbts can be combined easily with `psbt.combine(psbt2, psbt3, psbt4 ...)`
33 * the psbt calling combine will always have precedence when a conflict occurs.
34 * Combine checks if the internal bitcoin transaction is the same, so be sure that
35 * all sequences, version, locktime, etc. are the same before combining.
36 * Input Finalizer: This role is fairly important. Not only does it need to construct
37 * the input scriptSigs and witnesses, but it SHOULD verify the signatures etc.
38 * Before running `psbt.finalizeAllInputs()` please run `psbt.validateSignaturesOfAllInputs()`
39 * Running any finalize method will delete any data in the input(s) that are no longer
40 * needed due to the finalized scripts containing the information.
41 * Transaction Extractor: This role will perform some checks before returning a
42 * Transaction object. Such as fee rate not being larger than maximumFeeRate etc.
43 */
44export declare class Psbt {
45 readonly data: PsbtBase;
46 static fromBase64(data: string, opts?: PsbtOptsOptional): Psbt;
47 static fromHex(data: string, opts?: PsbtOptsOptional): Psbt;
48 static fromBuffer(buffer: Buffer, opts?: PsbtOptsOptional): Psbt;
49 private __CACHE;
50 private opts;
51 constructor(opts?: PsbtOptsOptional, data?: PsbtBase);
52 readonly inputCount: number;
53 version: number;
54 locktime: number;
55 readonly txInputs: PsbtTxInput[];
56 readonly txOutputs: PsbtTxOutput[];
57 combine(...those: Psbt[]): this;
58 clone(): Psbt;
59 setMaximumFeeRate(satoshiPerByte: number): void;
60 setVersion(version: number): this;
61 setLocktime(locktime: number): this;
62 setInputSequence(inputIndex: number, sequence: number): this;
63 addInputs(inputDatas: PsbtInputExtended[]): this;
64 addInput(inputData: PsbtInputExtended): this;
65 addOutputs(outputDatas: PsbtOutputExtended[]): this;
66 addOutput(outputData: PsbtOutputExtended): this;
67 extractTransaction(disableFeeCheck?: boolean): Transaction;
68 getFeeRate(): number;
69 getFee(): number;
70 finalizeAllInputs(): this;
71 finalizeInput(inputIndex: number, finalScriptsFunc?: FinalScriptsFunc): this;
72 getInputType(inputIndex: number): AllScriptType;
73 inputHasPubkey(inputIndex: number, pubkey: Buffer): boolean;
74 inputHasHDKey(inputIndex: number, root: HDSigner): boolean;
75 outputHasPubkey(outputIndex: number, pubkey: Buffer): boolean;
76 outputHasHDKey(outputIndex: number, root: HDSigner): boolean;
77 validateSignaturesOfAllInputs(): boolean;
78 validateSignaturesOfInput(inputIndex: number, pubkey?: Buffer): boolean;
79 signAllInputsHD(hdKeyPair: HDSigner, sighashTypes?: number[]): this;
80 signAllInputsHDAsync(hdKeyPair: HDSigner | HDSignerAsync, sighashTypes?: number[]): Promise<void>;
81 signInputHD(inputIndex: number, hdKeyPair: HDSigner, sighashTypes?: number[]): this;
82 signInputHDAsync(inputIndex: number, hdKeyPair: HDSigner | HDSignerAsync, sighashTypes?: number[]): Promise<void>;
83 signAllInputs(keyPair: Signer, sighashTypes?: number[]): this;
84 signAllInputsAsync(keyPair: Signer | SignerAsync, sighashTypes?: number[]): Promise<void>;
85 signInput(inputIndex: number, keyPair: Signer, sighashTypes?: number[]): this;
86 signInputAsync(inputIndex: number, keyPair: Signer | SignerAsync, sighashTypes?: number[]): Promise<void>;
87 toBuffer(): Buffer;
88 toHex(): string;
89 toBase64(): string;
90 updateGlobal(updateData: PsbtGlobalUpdate): this;
91 updateInput(inputIndex: number, updateData: PsbtInputUpdate): this;
92 updateOutput(outputIndex: number, updateData: PsbtOutputUpdate): this;
93 addUnknownKeyValToGlobal(keyVal: KeyValue): this;
94 addUnknownKeyValToInput(inputIndex: number, keyVal: KeyValue): this;
95 addUnknownKeyValToOutput(outputIndex: number, keyVal: KeyValue): this;
96 clearFinalizedInput(inputIndex: number): this;
97}
98interface PsbtOptsOptional {
99 network?: Network;
100 maximumFeeRate?: number;
101}
102interface PsbtInputExtended extends PsbtInput, TransactionInput {
103}
104declare type PsbtOutputExtended = PsbtOutputExtendedAddress | PsbtOutputExtendedScript;
105interface PsbtOutputExtendedAddress extends PsbtOutput {
106 address: string;
107 value: number;
108}
109interface PsbtOutputExtendedScript extends PsbtOutput {
110 script: Buffer;
111 value: number;
112}
113interface HDSignerBase {
114 /**
115 * DER format compressed publicKey buffer
116 */
117 publicKey: Buffer;
118 /**
119 * The first 4 bytes of the sha256-ripemd160 of the publicKey
120 */
121 fingerprint: Buffer;
122}
123interface HDSigner extends HDSignerBase {
124 /**
125 * The path string must match /^m(\/\d+'?)+$/
126 * ex. m/44'/0'/0'/1/23 levels with ' must be hard derivations
127 */
128 derivePath(path: string): HDSigner;
129 /**
130 * Input hash (the "message digest") for the signature algorithm
131 * Return a 64 byte signature (32 byte r and 32 byte s in that order)
132 */
133 sign(hash: Buffer): Buffer;
134}
135/**
136 * Same as above but with async sign method
137 */
138interface HDSignerAsync extends HDSignerBase {
139 derivePath(path: string): HDSignerAsync;
140 sign(hash: Buffer): Promise<Buffer>;
141}
142/**
143 * This function must do two things:
144 * 1. Check if the `input` can be finalized. If it can not be finalized, throw.
145 * ie. `Can not finalize input #${inputIndex}`
146 * 2. Create the finalScriptSig and finalScriptWitness Buffers.
147 */
148declare type FinalScriptsFunc = (inputIndex: number, // Which input is it?
149input: PsbtInput, // The PSBT input contents
150script: Buffer, // The "meaningful" locking script Buffer (redeemScript for P2SH etc.)
151isSegwit: boolean, // Is it segwit?
152isP2SH: boolean, // Is it P2SH?
153isP2WSH: boolean) => {
154 finalScriptSig: Buffer | undefined;
155 finalScriptWitness: Buffer | undefined;
156};
157declare 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';
158export {};