Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor PSBT file #2076

Closed
wants to merge 8 commits into from
3 changes: 2 additions & 1 deletion src/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ export { address, crypto, networks, payments, script };
export { Block } from './block';
/** @hidden */
export { TaggedHashPrefix } from './crypto';
export { Psbt, PsbtTxInput, PsbtTxOutput, Signer, SignerAsync, HDSigner, HDSignerAsync, } from './psbt';
export { Psbt } from './psbt';
export { PsbtTxInput, PsbtTxOutput, Signer, SignerAsync, HDSigner, HDSignerAsync, } from './psbt/interfaces';
/** @hidden */
export { OPS as opcodes } from './ops';
export { Transaction } from './transaction';
Expand Down
7 changes: 7 additions & 0 deletions src/payments/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,3 +46,10 @@ export type StackElement = Buffer | number;
export type Stack = StackElement[];
export type StackFunction = () => Stack;
export { embed, p2ms, p2pk, p2pkh, p2sh, p2wpkh, p2wsh, p2tr };
export declare const isP2MS: (script: Buffer) => boolean;
export declare const isP2PK: (script: Buffer) => boolean;
export declare const isP2PKH: (script: Buffer) => boolean;
export declare const isP2WPKH: (script: Buffer) => boolean;
export declare const isP2WSHScript: (script: Buffer) => boolean;
export declare const isP2SHScript: (script: Buffer) => boolean;
export declare const isP2TR: (script: Buffer) => boolean;
26 changes: 25 additions & 1 deletion src/payments/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
'use strict';
Object.defineProperty(exports, '__esModule', { value: true });
exports.p2tr =
exports.isP2TR =
exports.isP2SHScript =
exports.isP2WSHScript =
exports.isP2WPKH =
exports.isP2PKH =
exports.isP2PK =
exports.isP2MS =
exports.p2tr =
exports.p2wsh =
exports.p2wpkh =
exports.p2sh =
Expand Down Expand Up @@ -67,3 +74,20 @@ Object.defineProperty(exports, 'p2tr', {
});
// TODO
// witness commitment
function isPaymentFactory(payment) {
return script => {
try {
payment({ output: script });
return true;
} catch (err) {
return false;
}
};
}
exports.isP2MS = isPaymentFactory(p2ms_1.p2ms);
exports.isP2PK = isPaymentFactory(p2pk_1.p2pk);
exports.isP2PKH = isPaymentFactory(p2pkh_1.p2pkh);
exports.isP2WPKH = isPaymentFactory(p2wpkh_1.p2wpkh);
exports.isP2WSHScript = isPaymentFactory(p2wsh_1.p2wsh);
exports.isP2SHScript = isPaymentFactory(p2sh_1.p2sh);
exports.isP2TR = isPaymentFactory(p2tr_1.p2tr);
100 changes: 2 additions & 98 deletions src/psbt.d.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,8 @@
/// <reference types="node" />
import { Psbt as PsbtBase } from 'bip174';
import { KeyValue, PsbtGlobalUpdate, PsbtInput, PsbtInputUpdate, PsbtOutput, PsbtOutputUpdate } from 'bip174/src/lib/interfaces';
import { Network } from './networks';
import { KeyValue, PsbtGlobalUpdate, PsbtInputUpdate, PsbtOutputUpdate } from 'bip174/src/lib/interfaces';
import { AllScriptType, FinalScriptsFunc, FinalTaprootScriptsFunc, HDSigner, HDSignerAsync, PsbtInputExtended, PsbtOptsOptional, PsbtOutputExtended, PsbtTxInput, PsbtTxOutput, Signer, SignerAsync, ValidateSigFunction } from './psbt/interfaces';
import { Transaction } from './transaction';
export interface TransactionInput {
hash: string | Buffer;
index: number;
sequence?: number;
}
export interface PsbtTxInput extends TransactionInput {
hash: Buffer;
}
export interface TransactionOutput {
script: Buffer;
value: number;
}
export interface PsbtTxOutput extends TransactionOutput {
address: string | undefined;
}
export type ValidateSigFunction = (pubkey: Buffer, msghash: Buffer, signature: Buffer) => boolean;
/**
* 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)
Expand Down Expand Up @@ -124,83 +108,3 @@ export declare class Psbt {
addUnknownKeyValToOutput(outputIndex: number, keyVal: KeyValue): this;
clearFinalizedInput(inputIndex: number): this;
}
interface PsbtOptsOptional {
network?: Network;
maximumFeeRate?: number;
}
interface PsbtInputExtended extends PsbtInput, TransactionInput {
}
type PsbtOutputExtended = PsbtOutputExtendedAddress | PsbtOutputExtendedScript;
interface PsbtOutputExtendedAddress extends PsbtOutput {
address: string;
value: number;
}
interface PsbtOutputExtendedScript extends PsbtOutput {
script: Buffer;
value: number;
}
interface HDSignerBase {
/**
* DER format compressed publicKey buffer
*/
publicKey: Buffer;
/**
* The first 4 bytes of the sha256-ripemd160 of the publicKey
*/
fingerprint: Buffer;
}
export 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
*/
export interface HDSignerAsync extends HDSignerBase {
derivePath(path: string): HDSignerAsync;
sign(hash: Buffer): Promise<Buffer>;
}
export interface Signer {
publicKey: Buffer;
network?: any;
sign(hash: Buffer, lowR?: boolean): Buffer;
signSchnorr?(hash: Buffer): Buffer;
getPublicKey?(): Buffer;
}
export interface SignerAsync {
publicKey: Buffer;
network?: any;
sign(hash: Buffer, lowR?: boolean): Promise<Buffer>;
signSchnorr?(hash: Buffer): Promise<Buffer>;
getPublicKey?(): Buffer;
}
/**
* 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) => {
finalScriptSig: Buffer | undefined;
finalScriptWitness: Buffer | undefined;
};
type FinalTaprootScriptsFunc = (inputIndex: number, // Which input is it?
input: PsbtInput, // The PSBT input contents
tapLeafHashToFinalize?: Buffer) => {
finalScriptWitness: Buffer | undefined;
};
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';
export {};
Loading
Loading