Create Signer interface and use it
This commit is contained in:
parent
1cc7205e9c
commit
691e4d15d1
7 changed files with 42 additions and 20 deletions
|
@ -1007,7 +1007,8 @@ function getSigningData(
|
|||
) {
|
||||
throw new Error('Inconsistent redeemScript');
|
||||
}
|
||||
const ourPubKey = keyPair.publicKey || keyPair.getPublicKey();
|
||||
const ourPubKey =
|
||||
keyPair.publicKey || (keyPair.getPublicKey && keyPair.getPublicKey());
|
||||
if (!canSign(input)) {
|
||||
if (witnessValue !== undefined) {
|
||||
if (input.value !== undefined && input.value !== witnessValue)
|
||||
|
|
|
@ -19,15 +19,26 @@ interface ECPairOptions {
|
|||
rng?(arg0: number): Buffer;
|
||||
}
|
||||
|
||||
export interface ECPairInterface {
|
||||
export interface Signer {
|
||||
publicKey: Buffer;
|
||||
network?: Network;
|
||||
sign(hash: Buffer, lowR?: boolean): Buffer;
|
||||
getPublicKey?(): Buffer;
|
||||
}
|
||||
|
||||
export interface SignerAsync {
|
||||
publicKey: Buffer;
|
||||
network?: Network;
|
||||
sign(hash: Buffer, lowR?: boolean): Promise<Buffer>;
|
||||
getPublicKey?(): Buffer;
|
||||
}
|
||||
|
||||
export interface ECPairInterface extends Signer {
|
||||
compressed: boolean;
|
||||
network: Network;
|
||||
publicKey: Buffer;
|
||||
privateKey?: Buffer;
|
||||
toWIF(): string;
|
||||
sign(hash: Buffer, lowR?: boolean): Buffer;
|
||||
verify(hash: Buffer, signature: Buffer): boolean;
|
||||
getPublicKey?(): Buffer;
|
||||
}
|
||||
|
||||
class ECPair implements ECPairInterface {
|
||||
|
|
|
@ -14,7 +14,7 @@ export { Transaction } from './transaction';
|
|||
export { TransactionBuilder } from './transaction_builder';
|
||||
|
||||
export { BIP32Interface } from 'bip32';
|
||||
export { ECPairInterface } from './ecpair';
|
||||
export { ECPairInterface, Signer, SignerAsync } from './ecpair';
|
||||
export { Network } from './networks';
|
||||
export { Payment, PaymentOpts, Stack, StackElement } from './payments';
|
||||
export { OpCode } from './script';
|
||||
|
|
|
@ -2,7 +2,7 @@ import * as baddress from './address';
|
|||
import { reverseBuffer } from './bufferutils';
|
||||
import * as classify from './classify';
|
||||
import * as bcrypto from './crypto';
|
||||
import { ECPairInterface } from './ecpair';
|
||||
import { Signer } from './ecpair';
|
||||
import * as ECPair from './ecpair';
|
||||
import { Network } from './networks';
|
||||
import * as networks from './networks';
|
||||
|
@ -74,7 +74,7 @@ interface TxbOutput {
|
|||
interface TxbSignArg {
|
||||
prevOutScriptType: string;
|
||||
vin: number;
|
||||
keyPair: ECPairInterface;
|
||||
keyPair: Signer;
|
||||
redeemScript?: Buffer;
|
||||
hashType?: number;
|
||||
witnessValue?: number;
|
||||
|
@ -237,7 +237,7 @@ export class TransactionBuilder {
|
|||
|
||||
sign(
|
||||
signParams: number | TxbSignArg,
|
||||
keyPair?: ECPairInterface,
|
||||
keyPair?: Signer,
|
||||
redeemScript?: Buffer,
|
||||
hashType?: number,
|
||||
witnessValue?: number,
|
||||
|
@ -1186,7 +1186,7 @@ function trySign({
|
|||
interface SigningData {
|
||||
input: TxbInput;
|
||||
ourPubKey: Buffer;
|
||||
keyPair: ECPairInterface;
|
||||
keyPair: Signer;
|
||||
signatureHash: Buffer;
|
||||
hashType: number;
|
||||
useLowR: boolean;
|
||||
|
@ -1200,7 +1200,7 @@ function getSigningData(
|
|||
needsOutputs: HashTypeCheck,
|
||||
tx: Transaction,
|
||||
signParams: number | TxbSignArg,
|
||||
keyPair?: ECPairInterface,
|
||||
keyPair?: Signer,
|
||||
redeemScript?: Buffer,
|
||||
hashType?: number,
|
||||
witnessValue?: number,
|
||||
|
@ -1251,7 +1251,8 @@ function getSigningData(
|
|||
throw new Error('Inconsistent redeemScript');
|
||||
}
|
||||
|
||||
const ourPubKey = keyPair.publicKey || keyPair.getPublicKey!();
|
||||
const ourPubKey =
|
||||
keyPair.publicKey || (keyPair.getPublicKey && keyPair.getPublicKey());
|
||||
if (!canSign(input)) {
|
||||
if (witnessValue !== undefined) {
|
||||
if (input.value !== undefined && input.value !== witnessValue)
|
||||
|
|
17
types/ecpair.d.ts
vendored
17
types/ecpair.d.ts
vendored
|
@ -5,15 +5,24 @@ interface ECPairOptions {
|
|||
network?: Network;
|
||||
rng?(arg0: number): Buffer;
|
||||
}
|
||||
export interface ECPairInterface {
|
||||
export interface Signer {
|
||||
publicKey: Buffer;
|
||||
network?: Network;
|
||||
sign(hash: Buffer, lowR?: boolean): Buffer;
|
||||
getPublicKey?(): Buffer;
|
||||
}
|
||||
export interface SignerAsync {
|
||||
publicKey: Buffer;
|
||||
network?: Network;
|
||||
sign(hash: Buffer, lowR?: boolean): Promise<Buffer>;
|
||||
getPublicKey?(): Buffer;
|
||||
}
|
||||
export interface ECPairInterface extends Signer {
|
||||
compressed: boolean;
|
||||
network: Network;
|
||||
publicKey: Buffer;
|
||||
privateKey?: Buffer;
|
||||
toWIF(): string;
|
||||
sign(hash: Buffer, lowR?: boolean): Buffer;
|
||||
verify(hash: Buffer, signature: Buffer): boolean;
|
||||
getPublicKey?(): Buffer;
|
||||
}
|
||||
declare class ECPair implements ECPairInterface {
|
||||
private __D?;
|
||||
|
|
2
types/index.d.ts
vendored
2
types/index.d.ts
vendored
|
@ -11,7 +11,7 @@ export { OPS as opcodes } from './script';
|
|||
export { Transaction } from './transaction';
|
||||
export { TransactionBuilder } from './transaction_builder';
|
||||
export { BIP32Interface } from 'bip32';
|
||||
export { ECPairInterface } from './ecpair';
|
||||
export { ECPairInterface, Signer, SignerAsync } from './ecpair';
|
||||
export { Network } from './networks';
|
||||
export { Payment, PaymentOpts, Stack, StackElement } from './payments';
|
||||
export { OpCode } from './script';
|
||||
|
|
6
types/transaction_builder.d.ts
vendored
6
types/transaction_builder.d.ts
vendored
|
@ -1,11 +1,11 @@
|
|||
/// <reference types="node" />
|
||||
import { ECPairInterface } from './ecpair';
|
||||
import { Signer } from './ecpair';
|
||||
import { Network } from './networks';
|
||||
import { Transaction } from './transaction';
|
||||
interface TxbSignArg {
|
||||
prevOutScriptType: string;
|
||||
vin: number;
|
||||
keyPair: ECPairInterface;
|
||||
keyPair: Signer;
|
||||
redeemScript?: Buffer;
|
||||
hashType?: number;
|
||||
witnessValue?: number;
|
||||
|
@ -27,7 +27,7 @@ export declare class TransactionBuilder {
|
|||
addOutput(scriptPubKey: string | Buffer, value: number): number;
|
||||
build(): Transaction;
|
||||
buildIncomplete(): Transaction;
|
||||
sign(signParams: number | TxbSignArg, keyPair?: ECPairInterface, redeemScript?: Buffer, hashType?: number, witnessValue?: number, witnessScript?: Buffer): void;
|
||||
sign(signParams: number | TxbSignArg, keyPair?: Signer, redeemScript?: Buffer, hashType?: number, witnessValue?: number, witnessScript?: Buffer): void;
|
||||
private __addInputUnsafe;
|
||||
private __build;
|
||||
private __canModifyInputs;
|
||||
|
|
Loading…
Reference in a new issue