From 8142aa6c1da89772640f02fb15f577f2b40217cf Mon Sep 17 00:00:00 2001 From: Trent Nelson Date: Thu, 23 Apr 2020 15:44:19 -0600 Subject: [PATCH] feat: allow external signatures on `Transactions` Adds a helper for adding externally created signature/pubkey pairs to `Transactions` --- web3.js/module.d.ts | 1 + web3.js/module.flow.js | 1 + web3.js/src/transaction.js | 22 +++++++++++++++------- 3 files changed, 17 insertions(+), 7 deletions(-) diff --git a/web3.js/module.d.ts b/web3.js/module.d.ts index 94d54a376d..e90c245667 100644 --- a/web3.js/module.d.ts +++ b/web3.js/module.d.ts @@ -406,6 +406,7 @@ declare module '@solana/web3.js' { signData: Buffer; sign(...signers: Array): void; signPartial(...partialSigners: Array): void; + addSignature(pubkey: PublicKey, signature: Buffer): void; addSigner(signer: Account): void; verifySignatures(): boolean; serialize(): Buffer; diff --git a/web3.js/module.flow.js b/web3.js/module.flow.js index fc7d90e229..93e98719f4 100644 --- a/web3.js/module.flow.js +++ b/web3.js/module.flow.js @@ -415,6 +415,7 @@ declare module '@solana/web3.js' { sign(...signers: Array): void; signPartial(...partialSigners: Array): void; addSigner(signer: Account): void; + addSignature(pubkey: PublicKey, signature: Buffer): void; verifySignatures(): boolean; serialize(): Buffer; } diff --git a/web3.js/src/transaction.js b/web3.js/src/transaction.js index a1ffeb1250..bf66d801b4 100644 --- a/web3.js/src/transaction.js +++ b/web3.js/src/transaction.js @@ -398,16 +398,24 @@ export class Transaction { * `signPartial` */ addSigner(signer: Account) { - const index = this.signatures.findIndex(sigpair => - signer.publicKey.equals(sigpair.publicKey), - ); - if (index < 0) { - throw new Error(`Unknown signer: ${signer.publicKey.toString()}`); - } - const signData = this.signData; const signature = nacl.sign.detached(signData, signer.secretKey); + this.addSignature(signer.publicKey, signature); + } + + /** + * Add an externally created signature to a transaction + */ + addSignature(pubkey: PublicKey, signature: Buffer) { invariant(signature.length === 64); + + const index = this.signatures.findIndex(sigpair => + pubkey.equals(sigpair.publicKey), + ); + if (index < 0) { + throw new Error(`Unknown signer: ${pubkey.toString()}`); + } + this.signatures[index].signature = Buffer.from(signature); }