Create Signer interface and use it

This commit is contained in:
junderw 2019-06-14 15:25:07 +09:00
parent 1cc7205e9c
commit 691e4d15d1
No known key found for this signature in database
GPG key ID: B256185D3A971908
7 changed files with 42 additions and 20 deletions

View file

@ -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)

View file

@ -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 {

View file

@ -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';

View file

@ -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
View file

@ -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
View file

@ -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';

View file

@ -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;