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

64
ts_src/transaction_builder.ts

@ -243,26 +243,17 @@ export class TransactionBuilder {
witnessValue?: number,
witnessScript?: Buffer,
): void {
const data = getSigningData(
this,
signParams,
keyPair,
redeemScript,
hashType,
witnessValue,
witnessScript,
);
const { input, ourPubKey, signatureHash } = data;
({ keyPair, hashType } = data);
trySign(
input,
ourPubKey,
keyPair,
signatureHash,
hashType,
this.__USE_LOW_R,
getSigningData(
this,
signParams,
keyPair,
redeemScript,
hashType,
witnessValue,
witnessScript,
this.__USE_LOW_R,
),
);
}
@ -1161,14 +1152,14 @@ function checkSignArgs(txb: TransactionBuilder, signParams: TxbSignArg): void {
}
}
function trySign(
input: TxbInput,
ourPubKey: Buffer,
keyPair: ECPairInterface,
signatureHash: Buffer,
hashType: number,
useLowR: boolean,
): void {
function trySign({
input,
ourPubKey,
keyPair,
signatureHash,
hashType,
useLowR,
}: SigningData): void {
// enforce in order signing of public keys
let signed = false;
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');
}
interface SigningData {
input: TxbInput;
ourPubKey: Buffer;
keyPair: ECPairInterface;
signatureHash: Buffer;
hashType: number;
useLowR: boolean;
}
function getSigningData(
txb: TransactionBuilder,
signParams: number | TxbSignArg,
@ -1198,13 +1198,8 @@ function getSigningData(
hashType?: number,
witnessValue?: number,
witnessScript?: Buffer,
): {
input: TxbInput;
ourPubKey: Buffer;
keyPair: ECPairInterface;
signatureHash: Buffer;
hashType: number;
} {
useLowR?: boolean,
): SigningData {
let vin: number;
if (typeof signParams === 'number') {
console.warn(
@ -1302,5 +1297,6 @@ function getSigningData(
keyPair,
signatureHash,
hashType,
useLowR: !!useLowR,
};
}

Loading…
Cancel
Save