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'); throw new Error('Inconsistent redeemScript');
} }
const ourPubKey = keyPair.publicKey || keyPair.getPublicKey(); const ourPubKey =
keyPair.publicKey || (keyPair.getPublicKey && keyPair.getPublicKey());
if (!canSign(input)) { if (!canSign(input)) {
if (witnessValue !== undefined) { if (witnessValue !== undefined) {
if (input.value !== undefined && input.value !== witnessValue) if (input.value !== undefined && input.value !== witnessValue)

View file

@ -19,15 +19,26 @@ interface ECPairOptions {
rng?(arg0: number): Buffer; 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; compressed: boolean;
network: Network; network: Network;
publicKey: Buffer;
privateKey?: Buffer; privateKey?: Buffer;
toWIF(): string; toWIF(): string;
sign(hash: Buffer, lowR?: boolean): Buffer;
verify(hash: Buffer, signature: Buffer): boolean; verify(hash: Buffer, signature: Buffer): boolean;
getPublicKey?(): Buffer;
} }
class ECPair implements ECPairInterface { class ECPair implements ECPairInterface {

View file

@ -14,7 +14,7 @@ export { Transaction } from './transaction';
export { TransactionBuilder } from './transaction_builder'; export { TransactionBuilder } from './transaction_builder';
export { BIP32Interface } from 'bip32'; export { BIP32Interface } from 'bip32';
export { ECPairInterface } from './ecpair'; export { ECPairInterface, Signer, SignerAsync } from './ecpair';
export { Network } from './networks'; export { Network } from './networks';
export { Payment, PaymentOpts, Stack, StackElement } from './payments'; export { Payment, PaymentOpts, Stack, StackElement } from './payments';
export { OpCode } from './script'; export { OpCode } from './script';

View file

@ -2,7 +2,7 @@ import * as baddress from './address';
import { reverseBuffer } from './bufferutils'; import { reverseBuffer } from './bufferutils';
import * as classify from './classify'; import * as classify from './classify';
import * as bcrypto from './crypto'; import * as bcrypto from './crypto';
import { ECPairInterface } from './ecpair'; import { Signer } from './ecpair';
import * as ECPair from './ecpair'; import * as ECPair from './ecpair';
import { Network } from './networks'; import { Network } from './networks';
import * as networks from './networks'; import * as networks from './networks';
@ -74,7 +74,7 @@ interface TxbOutput {
interface TxbSignArg { interface TxbSignArg {
prevOutScriptType: string; prevOutScriptType: string;
vin: number; vin: number;
keyPair: ECPairInterface; keyPair: Signer;
redeemScript?: Buffer; redeemScript?: Buffer;
hashType?: number; hashType?: number;
witnessValue?: number; witnessValue?: number;
@ -237,7 +237,7 @@ export class TransactionBuilder {
sign( sign(
signParams: number | TxbSignArg, signParams: number | TxbSignArg,
keyPair?: ECPairInterface, keyPair?: Signer,
redeemScript?: Buffer, redeemScript?: Buffer,
hashType?: number, hashType?: number,
witnessValue?: number, witnessValue?: number,
@ -1186,7 +1186,7 @@ function trySign({
interface SigningData { interface SigningData {
input: TxbInput; input: TxbInput;
ourPubKey: Buffer; ourPubKey: Buffer;
keyPair: ECPairInterface; keyPair: Signer;
signatureHash: Buffer; signatureHash: Buffer;
hashType: number; hashType: number;
useLowR: boolean; useLowR: boolean;
@ -1200,7 +1200,7 @@ function getSigningData(
needsOutputs: HashTypeCheck, needsOutputs: HashTypeCheck,
tx: Transaction, tx: Transaction,
signParams: number | TxbSignArg, signParams: number | TxbSignArg,
keyPair?: ECPairInterface, keyPair?: Signer,
redeemScript?: Buffer, redeemScript?: Buffer,
hashType?: number, hashType?: number,
witnessValue?: number, witnessValue?: number,
@ -1251,7 +1251,8 @@ function getSigningData(
throw new Error('Inconsistent redeemScript'); throw new Error('Inconsistent redeemScript');
} }
const ourPubKey = keyPair.publicKey || keyPair.getPublicKey!(); const ourPubKey =
keyPair.publicKey || (keyPair.getPublicKey && keyPair.getPublicKey());
if (!canSign(input)) { if (!canSign(input)) {
if (witnessValue !== undefined) { if (witnessValue !== undefined) {
if (input.value !== undefined && input.value !== witnessValue) if (input.value !== undefined && input.value !== witnessValue)

17
types/ecpair.d.ts vendored
View file

@ -5,15 +5,24 @@ interface ECPairOptions {
network?: Network; network?: Network;
rng?(arg0: number): Buffer; 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; compressed: boolean;
network: Network; network: Network;
publicKey: Buffer;
privateKey?: Buffer; privateKey?: Buffer;
toWIF(): string; toWIF(): string;
sign(hash: Buffer, lowR?: boolean): Buffer;
verify(hash: Buffer, signature: Buffer): boolean; verify(hash: Buffer, signature: Buffer): boolean;
getPublicKey?(): Buffer;
} }
declare class ECPair implements ECPairInterface { declare class ECPair implements ECPairInterface {
private __D?; 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 { Transaction } from './transaction';
export { TransactionBuilder } from './transaction_builder'; export { TransactionBuilder } from './transaction_builder';
export { BIP32Interface } from 'bip32'; export { BIP32Interface } from 'bip32';
export { ECPairInterface } from './ecpair'; export { ECPairInterface, Signer, SignerAsync } from './ecpair';
export { Network } from './networks'; export { Network } from './networks';
export { Payment, PaymentOpts, Stack, StackElement } from './payments'; export { Payment, PaymentOpts, Stack, StackElement } from './payments';
export { OpCode } from './script'; export { OpCode } from './script';

View file

@ -1,11 +1,11 @@
/// <reference types="node" /> /// <reference types="node" />
import { ECPairInterface } from './ecpair'; import { Signer } from './ecpair';
import { Network } from './networks'; import { Network } from './networks';
import { Transaction } from './transaction'; import { Transaction } from './transaction';
interface TxbSignArg { interface TxbSignArg {
prevOutScriptType: string; prevOutScriptType: string;
vin: number; vin: number;
keyPair: ECPairInterface; keyPair: Signer;
redeemScript?: Buffer; redeemScript?: Buffer;
hashType?: number; hashType?: number;
witnessValue?: number; witnessValue?: number;
@ -27,7 +27,7 @@ export declare class TransactionBuilder {
addOutput(scriptPubKey: string | Buffer, value: number): number; addOutput(scriptPubKey: string | Buffer, value: number): number;
build(): Transaction; build(): Transaction;
buildIncomplete(): 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 __addInputUnsafe;
private __build; private __build;
private __canModifyInputs; private __canModifyInputs;