Skip to content
This repository has been archived by the owner on Feb 1, 2024. It is now read-only.

Commit

Permalink
Rename methods
Browse files Browse the repository at this point in the history
  • Loading branch information
junderw committed Jul 19, 2019
1 parent acf59f1 commit e19bc58
Show file tree
Hide file tree
Showing 6 changed files with 70 additions and 64 deletions.
26 changes: 16 additions & 10 deletions src/psbt.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ const DEFAULT_OPTS = {
* data for updateOutput.
* For a list of what attributes should be what types. Check the bip174 library.
* Also, check the integration tests for some examples of usage.
* Signer: There are a few methods. sign and signAsync, which will search all input
* Signer: There are a few methods. signAllInputs and signAsync, which will search all input
* information for your pubkey or pubkeyhash, and only sign inputs where it finds
* your info. Or you can explicitly sign a specific input with signInput and
* signInputAsync. For the async methods you can create a SignerAsync object
Expand All @@ -53,7 +53,7 @@ const DEFAULT_OPTS = {
* all sequences, version, locktime, etc. are the same before combining.
* Input Finalizer: This role is fairly important. Not only does it need to construct
* the input scriptSigs and witnesses, but it SHOULD verify the signatures etc.
* Before running `psbt.finalizeAllInputs()` please run `psbt.validateAllSignatures()`
* Before running `psbt.finalizeAllInputs()` please run `psbt.validateSignaturesOfAllInputs()`
* Running any finalize method will delete any data in the input(s) that are no longer
* needed due to the finalized scripts containing the information.
* Transaction Extractor: This role will perform some checks before returning a
Expand Down Expand Up @@ -131,9 +131,9 @@ class Psbt {
c.__EXTRACTED_TX = undefined;
return this;
}
setSequence(inputIndex, sequence) {
setInputSequence(inputIndex, sequence) {
check32Bit(sequence);
checkInputsForPartialSig(this.data.inputs, 'setSequence');
checkInputsForPartialSig(this.data.inputs, 'setInputSequence');
const c = this.__CACHE;
if (c.__TX.ins.length <= inputIndex) {
throw new Error('Input index too high');
Expand Down Expand Up @@ -239,14 +239,14 @@ class Psbt {
this.data.clearFinalizedInput(inputIndex);
return this;
}
validateAllSignatures() {
validateSignaturesOfAllInputs() {
utils_1.checkForInput(this.data.inputs, 0); // making sure we have at least one
const results = range(this.data.inputs.length).map(idx =>
this.validateSignatures(idx),
this.validateSignaturesOfInput(idx),
);
return results.reduce((final, res) => res === true && final, true);
}
validateSignatures(inputIndex, pubkey) {
validateSignaturesOfInput(inputIndex, pubkey) {
const input = this.data.inputs[inputIndex];
const partialSig = (input || {}).partialSig;
if (!input || !partialSig || partialSig.length < 1)
Expand Down Expand Up @@ -278,7 +278,10 @@ class Psbt {
}
return results.every(res => res === true);
}
signHD(hdKeyPair, sighashTypes = [transaction_1.Transaction.SIGHASH_ALL]) {
signAllInputsHD(
hdKeyPair,
sighashTypes = [transaction_1.Transaction.SIGHASH_ALL],
) {
if (!hdKeyPair || !hdKeyPair.publicKey || !hdKeyPair.fingerprint) {
throw new Error('Need HDSigner to sign input');
}
Expand Down Expand Up @@ -358,7 +361,10 @@ class Psbt {
.catch(reject);
});
}
sign(keyPair, sighashTypes = [transaction_1.Transaction.SIGHASH_ALL]) {
signAllInputs(
keyPair,
sighashTypes = [transaction_1.Transaction.SIGHASH_ALL],
) {
if (!keyPair || !keyPair.publicKey)
throw new Error('Need Signer to sign input');
// TODO: Add a pubkey/pubkeyhash cache to each input
Expand Down Expand Up @@ -635,7 +641,7 @@ function checkInputsForPartialSig(inputs, action) {
case transaction_1.Transaction.SIGHASH_SINGLE:
case transaction_1.Transaction.SIGHASH_NONE:
whitelist.push('addOutput');
whitelist.push('setSequence');
whitelist.push('setInputSequence');
break;
}
if (whitelist.indexOf(action) === -1) {
Expand Down
2 changes: 1 addition & 1 deletion test/fixtures/psbt.json
Original file line number Diff line number Diff line change
Expand Up @@ -526,7 +526,7 @@
"result": "cHNidP8BAKYCAAAAAlwKQ3suPWwEJ/zQ9sZsIioOcHKU1KoLMxlMNSXVIkEWAAAAAAD/////YYDJMap+mYgbTrCNAdpWHN+EkKvl+XYao/6co/EQfwMAAAAAAP////8CkF8BAAAAAAAWABRnPBAmVHz2HL+8/1U+QG5L2thjmjhKAAAAAAAAIgAg700yfFRyhWzQnPHIUb/XQqsjlpf4A0uw682pCVWuQ8IAAAAAAAEBKzB1AAAAAAAAIgAgth9oE4cDfC5aV58VgkW5CptHsIxppYzJV8C5kT6aTo8BCG4CSDBFAiEAs7TFGm6o/dpWSb4M/KSu2p7p881KA1uWn0r0wDCa0ckCIAdXlm9k3xWLj2f+j0hIXK+0ew9dc8qoHEL2babYeWliASMhA182CcWcQ9L2zg9j8jlt7OlaIATNDgNFMBKB1J14wzTprAABASuAOAEAAAAAACIAILYfaBOHA3wuWlefFYJFuQqbR7CMaaWMyVfAuZE+mk6PAQhuAkgwRQIhALpc3q2mj2ZiVl6PgFdJKHkPUrpogsF9DhYFZdSshLHrAiBVrEVSzPzLn3EnVXixnWpqsdf2ln4wmYspuXZlJp2KygEjIQNfNgnFnEPS9s4PY/I5bezpWiAEzQ4DRTASgdSdeMM06awAAAA="
}
],
"validateSignatures": {
"validateSignaturesOfInput": {
"psbt": "cHNidP8BAJoCAAAAAljoeiG1ba8MI76OcHBFbDNvfLqlyHV5JPVFiHuyq911AAAAAAD/////g40EJ9DsZQpoqka7CwmK6kQiwHGyyng1Kgd5WdB86h0BAAAAAP////8CcKrwCAAAAAAWABTYXCtx0AYLCcmIauuBXlCZHdoSTQDh9QUAAAAAFgAUAK6pouXw+HaliN9VRuh0LR2HAI8AAAAAAAEAuwIAAAABqtc5MQGL0l+ErkALaISL4J23BurCrBgpi6vucatlb4sAAAAASEcwRAIgWPb8fGoz4bMVSNSByCbAFb0wE1qtQs1neQ2rZtKtJDsCIEoc7SYExnNbY5PltBaR3XiwDwxZQvufdRhW+qk4FX26Af7///8CgPD6AgAAAAAXqRQPuUY0IWlrgsgzryQceMF9295JNIfQ8gonAQAAABepFCnKdPigj4GZlCgYXJe12FLkBj9hh2UAAAAiAgKVg785rgpgl0etGZrd1jT6YQhVnWxc05tMIYPxq5bgf0cwRAIgdAGK1BgAl7hzMjwAFXILNoTMgSOJEEjn282bVa1nnJkCIHPTabdA4+tT3O+jOCPIBwUUylWn3ZVE8VfBZ5EyYRGMASICAtq2H/SaFNtq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mXV9Lv9VoTatAsaEsYOLZVbl8bazQoKpS2tQBRCWENkMak8AAACAAAAAgAUAAIAA",
"index": 0,
"pubkey": "Buffer.from('029583bf39ae0a609747ad199addd634fa6108559d6c5cd39b4c2183f1ab96e07f', 'hex')",
Expand Down
34 changes: 17 additions & 17 deletions test/integration/transactions-psbt.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ describe('bitcoinjs-lib (transactions with psbt)', () => {
value: 80000,
});
psbt.signInput(0, alice);
psbt.validateSignatures(0);
psbt.validateSignaturesOfInput(0);
psbt.finalizeAllInputs();
assert.strictEqual(
psbt.extractTransaction().toHex(),
Expand Down Expand Up @@ -128,8 +128,8 @@ describe('bitcoinjs-lib (transactions with psbt)', () => {
// Alice signs each input with the respective private keys
// signInput and signInputAsync are better
// (They take the input index explicitly as the first arg)
signer1.sign(alice1.keys[0]);
signer2.sign(alice2.keys[0]);
signer1.signAllInputs(alice1.keys[0]);
signer2.signAllInputs(alice2.keys[0]);

// If your signer object's sign method returns a promise, use the following
// await signer2.signAsync(alice2.keys[0])
Expand All @@ -147,8 +147,8 @@ describe('bitcoinjs-lib (transactions with psbt)', () => {
// Finalizer wants to check all signatures are valid before finalizing.
// If the finalizer wants to check for specific pubkeys, the second arg
// can be passed. See the first multisig example below.
assert.strictEqual(psbt.validateSignatures(0), true);
assert.strictEqual(psbt.validateSignatures(1), true);
assert.strictEqual(psbt.validateSignaturesOfInput(0), true);
assert.strictEqual(psbt.validateSignaturesOfInput(1), true);

// This step it new. Since we separate the signing operation and
// the creation of the scriptSig and witness stack, we are able to
Expand Down Expand Up @@ -183,7 +183,7 @@ describe('bitcoinjs-lib (transactions with psbt)', () => {
})
.signInput(0, alice1.keys[0]);

assert.strictEqual(psbt.validateSignatures(0), true);
assert.strictEqual(psbt.validateSignaturesOfInput(0), true);
psbt.finalizeAllInputs();

// build and broadcast to the RegTest network
Expand Down Expand Up @@ -215,13 +215,13 @@ describe('bitcoinjs-lib (transactions with psbt)', () => {
.signInput(0, multisig.keys[0])
.signInput(0, multisig.keys[2]);

assert.strictEqual(psbt.validateSignatures(0), true);
assert.strictEqual(psbt.validateSignaturesOfInput(0), true);
assert.strictEqual(
psbt.validateSignatures(0, multisig.keys[0].publicKey),
psbt.validateSignaturesOfInput(0, multisig.keys[0].publicKey),
true,
);
assert.throws(() => {
psbt.validateSignatures(0, multisig.keys[3].publicKey);
psbt.validateSignaturesOfInput(0, multisig.keys[3].publicKey);
}, new RegExp('No signatures for this pubkey'));
psbt.finalizeAllInputs();

Expand Down Expand Up @@ -267,7 +267,7 @@ describe('bitcoinjs-lib (transactions with psbt)', () => {
const tx = new bitcoin.Psbt()
.addInputs([inputData, inputData2])
.addOutputs([outputData, outputData2])
.sign(keyPair)
.signAllInputs(keyPair)
.finalizeAllInputs()
.extractTransaction();

Expand Down Expand Up @@ -300,7 +300,7 @@ describe('bitcoinjs-lib (transactions with psbt)', () => {
})
.signInput(0, p2wpkh.keys[0]);

assert.strictEqual(psbt.validateSignatures(0), true);
assert.strictEqual(psbt.validateSignaturesOfInput(0), true);
psbt.finalizeAllInputs();

const tx = psbt.extractTransaction();
Expand Down Expand Up @@ -340,7 +340,7 @@ describe('bitcoinjs-lib (transactions with psbt)', () => {
})
.signInput(0, p2wsh.keys[0]);

assert.strictEqual(psbt.validateSignatures(0), true);
assert.strictEqual(psbt.validateSignaturesOfInput(0), true);
psbt.finalizeAllInputs();

const tx = psbt.extractTransaction();
Expand Down Expand Up @@ -383,13 +383,13 @@ describe('bitcoinjs-lib (transactions with psbt)', () => {
.signInput(0, p2sh.keys[2])
.signInput(0, p2sh.keys[3]);

assert.strictEqual(psbt.validateSignatures(0), true);
assert.strictEqual(psbt.validateSignaturesOfInput(0), true);
assert.strictEqual(
psbt.validateSignatures(0, p2sh.keys[3].publicKey),
psbt.validateSignaturesOfInput(0, p2sh.keys[3].publicKey),
true,
);
assert.throws(() => {
psbt.validateSignatures(0, p2sh.keys[1].publicKey);
psbt.validateSignaturesOfInput(0, p2sh.keys[1].publicKey);
}, new RegExp('No signatures for this pubkey'));
psbt.finalizeAllInputs();

Expand Down Expand Up @@ -449,8 +449,8 @@ describe('bitcoinjs-lib (transactions with psbt)', () => {
})
.signInputHD(0, hdRoot); // must sign with root!!!

assert.strictEqual(psbt.validateSignatures(0), true);
assert.strictEqual(psbt.validateSignatures(0, childNode.publicKey), true);
assert.strictEqual(psbt.validateSignaturesOfInput(0), true);
assert.strictEqual(psbt.validateSignaturesOfInput(0, childNode.publicKey), true);
psbt.finalizeAllInputs();

const tx = psbt.extractTransaction();
Expand Down
38 changes: 19 additions & 19 deletions test/psbt.js
Original file line number Diff line number Diff line change
Expand Up @@ -249,14 +249,14 @@ describe(`Psbt`, () => {
})
})

describe('sign', () => {
describe('signAllInputs', () => {
fixtures.signInput.checks.forEach(f => {
if (f.description === 'checks the input exists') return
it(f.description, () => {
if (f.shouldSign) {
const psbtThatShouldsign = Psbt.fromBase64(f.shouldSign.psbt)
assert.doesNotThrow(() => {
psbtThatShouldsign.sign(
psbtThatShouldsign.signAllInputs(
ECPair.fromWIF(f.shouldSign.WIF),
f.shouldSign.sighashTypes || undefined,
)
Expand All @@ -266,13 +266,13 @@ describe(`Psbt`, () => {
if (f.shouldThrow) {
const psbtThatShouldThrow = Psbt.fromBase64(f.shouldThrow.psbt)
assert.throws(() => {
psbtThatShouldThrow.sign(
psbtThatShouldThrow.signAllInputs(
ECPair.fromWIF(f.shouldThrow.WIF),
f.shouldThrow.sighashTypes || undefined,
)
}, new RegExp('No inputs were signed'))
assert.throws(() => {
psbtThatShouldThrow.sign()
psbtThatShouldThrow.signAllInputs()
}, new RegExp('Need Signer to sign input'))
}
})
Expand Down Expand Up @@ -374,13 +374,13 @@ describe(`Psbt`, () => {
})
})

describe('signHD', () => {
describe('signAllInputsHD', () => {
fixtures.signInputHD.checks.forEach(f => {
it(f.description, () => {
if (f.shouldSign) {
const psbtThatShouldsign = Psbt.fromBase64(f.shouldSign.psbt)
assert.doesNotThrow(() => {
psbtThatShouldsign.signHD(
psbtThatShouldsign.signAllInputsHD(
bip32.fromBase58(f.shouldSign.xprv),
f.shouldSign.sighashTypes || undefined,
)
Expand All @@ -390,13 +390,13 @@ describe(`Psbt`, () => {
if (f.shouldThrow) {
const psbtThatShouldThrow = Psbt.fromBase64(f.shouldThrow.psbt)
assert.throws(() => {
psbtThatShouldThrow.signHD(
psbtThatShouldThrow.signAllInputsHD(
bip32.fromBase58(f.shouldThrow.xprv),
f.shouldThrow.sighashTypes || undefined,
)
}, new RegExp('No inputs were signed'))
assert.throws(() => {
psbtThatShouldThrow.signHD()
psbtThatShouldThrow.signAllInputsHD()
}, new RegExp('Need HDSigner to sign input'))
}
})
Expand Down Expand Up @@ -505,7 +505,7 @@ describe(`Psbt`, () => {
})
})

describe('setSequence', () => {
describe('setInputSequence', () => {
it('Sets the sequence number for a given input', () => {
const psbt = new Psbt()
psbt.addInput({
Expand All @@ -515,7 +515,7 @@ describe(`Psbt`, () => {

assert.strictEqual(psbt.inputCount, 1)
assert.strictEqual(psbt.__CACHE.__TX.ins[0].sequence, 0xffffffff)
psbt.setSequence(0, 0)
psbt.setInputSequence(0, 0)
assert.strictEqual(psbt.__CACHE.__TX.ins[0].sequence, 0)
})

Expand All @@ -527,7 +527,7 @@ describe(`Psbt`, () => {
});

assert.throws(() => {
psbt.setSequence(1, 0)
psbt.setInputSequence(1, 0)
}, new RegExp('Input index too high'))
})
})
Expand All @@ -539,7 +539,7 @@ describe(`Psbt`, () => {
const notAClone = Object.assign(new Psbt(), psbt) // references still active
const clone = psbt.clone()

assert.strictEqual(psbt.validateAllSignatures(), true)
assert.strictEqual(psbt.validateSignaturesOfAllInputs(), true)

assert.strictEqual(clone.toBase64(), psbt.toBase64())
assert.strictEqual(clone.toBase64(), notAClone.toBase64())
Expand All @@ -561,22 +561,22 @@ describe(`Psbt`, () => {
})
})

describe('validateSignatures', () => {
const f = fixtures.validateSignatures
describe('validateSignaturesOfInput', () => {
const f = fixtures.validateSignaturesOfInput
it('Correctly validates a signature', () => {
const psbt = Psbt.fromBase64(f.psbt)

assert.strictEqual(psbt.validateSignatures(f.index), true)
assert.strictEqual(psbt.validateSignaturesOfInput(f.index), true)
assert.throws(() => {
psbt.validateSignatures(f.nonExistantIndex)
psbt.validateSignaturesOfInput(f.nonExistantIndex)
}, new RegExp('No signatures to validate'))
})

it('Correctly validates a signature against a pubkey', () => {
const psbt = Psbt.fromBase64(f.psbt)
assert.strictEqual(psbt.validateSignatures(f.index, f.pubkey), true)
assert.strictEqual(psbt.validateSignaturesOfInput(f.index, f.pubkey), true)
assert.throws(() => {
psbt.validateSignatures(f.index, f.incorrectPubkey)
psbt.validateSignaturesOfInput(f.index, f.incorrectPubkey)
}, new RegExp('No signatures for this pubkey'))
})
})
Expand Down Expand Up @@ -623,7 +623,7 @@ describe(`Psbt`, () => {
assert.throws(() => {
psbt.setVersion(3)
}, new RegExp('Can not modify transaction, signatures exist.'))
psbt.validateSignatures(0)
psbt.validateSignaturesOfInput(0)
psbt.finalizeAllInputs()
assert.strictEqual(
psbt.extractTransaction().toHex(),
Expand Down
Loading

0 comments on commit e19bc58

Please sign in to comment.