From 7d3781e19f139bcfe0350c7033f0acdc0c901dca Mon Sep 17 00:00:00 2001 From: Trent Nelson Date: Thu, 23 Apr 2020 15:37:49 -0600 Subject: [PATCH] feat: expose `Transaction._getSignData()` as `Transaction.signData` --- web3.js/module.d.ts | 1 + web3.js/module.flow.js | 1 + web3.js/src/transaction.js | 12 ++++++------ 3 files changed, 8 insertions(+), 6 deletions(-) diff --git a/web3.js/module.d.ts b/web3.js/module.d.ts index 6fa45bbb73..94d54a376d 100644 --- a/web3.js/module.d.ts +++ b/web3.js/module.d.ts @@ -403,6 +403,7 @@ declare module '@solana/web3.js' { Transaction | TransactionInstruction | TransactionInstructionCtorFields > ): Transaction; + signData: Buffer; sign(...signers: Array): void; signPartial(...partialSigners: Array): void; addSigner(signer: Account): void; diff --git a/web3.js/module.flow.js b/web3.js/module.flow.js index a7bccc7477..fc7d90e229 100644 --- a/web3.js/module.flow.js +++ b/web3.js/module.flow.js @@ -411,6 +411,7 @@ declare module '@solana/web3.js' { Transaction | TransactionInstruction | TransactionInstructionCtorFields, > ): Transaction; + signData: Buffer; sign(...signers: Array): void; signPartial(...partialSigners: Array): void; addSigner(signer: Account): void; diff --git a/web3.js/src/transaction.js b/web3.js/src/transaction.js index 0cc09b4e6a..a1ffeb1250 100644 --- a/web3.js/src/transaction.js +++ b/web3.js/src/transaction.js @@ -180,9 +180,9 @@ export class Transaction { } /** - * @private + * Get a buffer of the Transaction data that need to be covered by signatures */ - _getSignData(): Buffer { + get signData(): Buffer { const {nonceInfo} = this; if (nonceInfo && this.instructions[0] != nonceInfo.nonceInstruction) { this.recentBlockhash = nonceInfo.nonce; @@ -377,7 +377,7 @@ export class Transaction { }, ); this.signatures = signatures; - const signData = this._getSignData(); + const signData = this.signData; partialSigners.forEach((accountOrPublicKey, index) => { if (accountOrPublicKey instanceof PublicKey) { @@ -405,7 +405,7 @@ export class Transaction { throw new Error(`Unknown signer: ${signer.publicKey.toString()}`); } - const signData = this._getSignData(); + const signData = this.signData; const signature = nacl.sign.detached(signData, signer.secretKey); invariant(signature.length === 64); this.signatures[index].signature = Buffer.from(signature); @@ -416,7 +416,7 @@ export class Transaction { */ verifySignatures(): boolean { let verified = true; - const signData = this._getSignData(); + const signData = this.signData; for (const {signature, publicKey} of this.signatures) { if ( !nacl.sign.detached.verify(signData, signature, publicKey.toBuffer()) @@ -438,7 +438,7 @@ export class Transaction { throw new Error('Transaction has not been signed'); } - const signData = this._getSignData(); + const signData = this.signData; const signatureCount = []; shortvec.encodeLength(signatureCount, signatures.length); const transactionLength =