Browse Source

Promise fixes for async

psbt
junderw 6 years ago
parent
commit
b98761a283
No known key found for this signature in database GPG Key ID: B256185D3A971908
  1. 31
      src/psbt.js
  2. 35
      ts_src/psbt.ts

31
src/psbt.js

@ -175,20 +175,23 @@ class Psbt extends bip174_1.Psbt {
return this.addPartialSigToInput(inputIndex, partialSig); return this.addPartialSigToInput(inputIndex, partialSig);
} }
signInputAsync(inputIndex, keyPair) { signInputAsync(inputIndex, keyPair) {
if (!keyPair || !keyPair.publicKey) return new Promise((resolve, reject) => {
throw new Error('Need Signer to sign input'); if (!keyPair || !keyPair.publicKey)
const { hash, sighashType } = getHashAndSighashType( return reject(new Error('Need Signer to sign input'));
this.inputs, const { hash, sighashType } = getHashAndSighashType(
inputIndex, this.inputs,
keyPair.publicKey, inputIndex,
this.globalMap.unsignedTx, keyPair.publicKey,
); this.globalMap.unsignedTx,
return keyPair.sign(hash).then(signature => { );
const partialSig = { Promise.resolve(keyPair.sign(hash)).then(signature => {
pubkey: keyPair.publicKey, const partialSig = {
signature: bscript.signature.encode(signature, sighashType), pubkey: keyPair.publicKey,
}; signature: bscript.signature.encode(signature, sighashType),
this.addPartialSigToInput(inputIndex, partialSig); };
this.addPartialSigToInput(inputIndex, partialSig);
resolve();
});
}); });
} }
} }

35
ts_src/psbt.ts

@ -214,23 +214,28 @@ export class Psbt extends PsbtBase {
} }
signInputAsync(inputIndex: number, keyPair: SignerAsync): Promise<void> { signInputAsync(inputIndex: number, keyPair: SignerAsync): Promise<void> {
if (!keyPair || !keyPair.publicKey) return new Promise(
throw new Error('Need Signer to sign input'); (resolve, reject): void => {
const { hash, sighashType } = getHashAndSighashType( if (!keyPair || !keyPair.publicKey)
this.inputs, return reject(new Error('Need Signer to sign input'));
inputIndex, const { hash, sighashType } = getHashAndSighashType(
keyPair.publicKey, this.inputs,
this.globalMap.unsignedTx!, inputIndex,
); keyPair.publicKey,
this.globalMap.unsignedTx!,
);
return keyPair.sign(hash).then(signature => { Promise.resolve(keyPair.sign(hash)).then(signature => {
const partialSig = { const partialSig = {
pubkey: keyPair.publicKey, pubkey: keyPair.publicKey,
signature: bscript.signature.encode(signature, sighashType), signature: bscript.signature.encode(signature, sighashType),
}; };
this.addPartialSigToInput(inputIndex, partialSig); this.addPartialSigToInput(inputIndex, partialSig);
}); resolve();
});
},
);
} }
} }

Loading…
Cancel
Save