Browse Source

Condense sign parts work directly

psbt-tx-getters
junderw 6 years ago
parent
commit
d814c21466
No known key found for this signature in database GPG Key ID: B256185D3A971908
  1. 38
      src/transaction_builder.js
  2. 64
      ts_src/transaction_builder.ts

38
src/transaction_builder.js

@ -153,24 +153,17 @@ class TransactionBuilder {
witnessValue, witnessValue,
witnessScript, witnessScript,
) { ) {
const data = getSigningData(
this,
signParams,
keyPair,
redeemScript,
hashType,
witnessValue,
witnessScript,
);
const { input, ourPubKey, signatureHash } = data;
({ keyPair, hashType } = data);
trySign( trySign(
input, getSigningData(
ourPubKey, this,
keyPair, signParams,
signatureHash, keyPair,
hashType, redeemScript,
this.__USE_LOW_R, hashType,
witnessValue,
witnessScript,
this.__USE_LOW_R,
),
); );
} }
__addInputUnsafe(txHash, vout, options) { __addInputUnsafe(txHash, vout, options) {
@ -934,7 +927,14 @@ function checkSignArgs(txb, signParams) {
break; break;
} }
} }
function trySign(input, ourPubKey, keyPair, signatureHash, hashType, useLowR) { function trySign({
input,
ourPubKey,
keyPair,
signatureHash,
hashType,
useLowR,
}) {
// enforce in order signing of public keys // enforce in order signing of public keys
let signed = false; let signed = false;
for (const [i, pubKey] of input.pubkeys.entries()) { for (const [i, pubKey] of input.pubkeys.entries()) {
@ -960,6 +960,7 @@ function getSigningData(
hashType, hashType,
witnessValue, witnessValue,
witnessScript, witnessScript,
useLowR,
) { ) {
let vin; let vin;
if (typeof signParams === 'number') { if (typeof signParams === 'number') {
@ -1045,5 +1046,6 @@ function getSigningData(
keyPair, keyPair,
signatureHash, signatureHash,
hashType, hashType,
useLowR: !!useLowR,
}; };
} }

64
ts_src/transaction_builder.ts

@ -243,26 +243,17 @@ export class TransactionBuilder {
witnessValue?: number, witnessValue?: number,
witnessScript?: Buffer, witnessScript?: Buffer,
): void { ): void {
const data = getSigningData(
this,
signParams,
keyPair,
redeemScript,
hashType,
witnessValue,
witnessScript,
);
const { input, ourPubKey, signatureHash } = data;
({ keyPair, hashType } = data);
trySign( trySign(
input, getSigningData(
ourPubKey, this,
keyPair, signParams,
signatureHash, keyPair,
hashType, redeemScript,
this.__USE_LOW_R, hashType,
witnessValue,
witnessScript,
this.__USE_LOW_R,
),
); );
} }
@ -1161,14 +1152,14 @@ function checkSignArgs(txb: TransactionBuilder, signParams: TxbSignArg): void {
} }
} }
function trySign( function trySign({
input: TxbInput, input,
ourPubKey: Buffer, ourPubKey,
keyPair: ECPairInterface, keyPair,
signatureHash: Buffer, signatureHash,
hashType: number, hashType,
useLowR: boolean, useLowR,
): void { }: SigningData): void {
// enforce in order signing of public keys // enforce in order signing of public keys
let signed = false; let signed = false;
for (const [i, pubKey] of input.pubkeys!.entries()) { for (const [i, pubKey] of input.pubkeys!.entries()) {
@ -1190,6 +1181,15 @@ function trySign(
if (!signed) throw new Error('Key pair cannot sign for this input'); if (!signed) throw new Error('Key pair cannot sign for this input');
} }
interface SigningData {
input: TxbInput;
ourPubKey: Buffer;
keyPair: ECPairInterface;
signatureHash: Buffer;
hashType: number;
useLowR: boolean;
}
function getSigningData( function getSigningData(
txb: TransactionBuilder, txb: TransactionBuilder,
signParams: number | TxbSignArg, signParams: number | TxbSignArg,
@ -1198,13 +1198,8 @@ function getSigningData(
hashType?: number, hashType?: number,
witnessValue?: number, witnessValue?: number,
witnessScript?: Buffer, witnessScript?: Buffer,
): { useLowR?: boolean,
input: TxbInput; ): SigningData {
ourPubKey: Buffer;
keyPair: ECPairInterface;
signatureHash: Buffer;
hashType: number;
} {
let vin: number; let vin: number;
if (typeof signParams === 'number') { if (typeof signParams === 'number') {
console.warn( console.warn(
@ -1302,5 +1297,6 @@ function getSigningData(
keyPair, keyPair,
signatureHash, signatureHash,
hashType, hashType,
useLowR: !!useLowR,
}; };
} }

Loading…
Cancel
Save