From fcacb06a006367381d05c85bb83fa7bf2fa485a5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Luiz=20Est=C3=A1cio=20=7C=20stacio=2Eeth?= Date: Thu, 27 Oct 2022 18:41:06 -0300 Subject: [PATCH] refactor: change sign methods to be async (#552) --- .changeset/soft-elephants-drum.md | 6 ++++++ .../wallet-manager/src/wallet-manager.test.ts | 2 +- packages/wallet/src/wallet.test.ts | 20 ++++++++++-------- packages/wallet/src/wallet.ts | 21 +++++++++++-------- 4 files changed, 30 insertions(+), 19 deletions(-) create mode 100644 .changeset/soft-elephants-drum.md diff --git a/.changeset/soft-elephants-drum.md b/.changeset/soft-elephants-drum.md new file mode 100644 index 00000000000..7aa87b220b5 --- /dev/null +++ b/.changeset/soft-elephants-drum.md @@ -0,0 +1,6 @@ +--- +"@fuel-ts/wallet": minor +"@fuel-ts/wallet-manager": minor +--- + +Change sign methods to be async diff --git a/packages/wallet-manager/src/wallet-manager.test.ts b/packages/wallet-manager/src/wallet-manager.test.ts index 97a3311ac97..ca0e6e33444 100644 --- a/packages/wallet-manager/src/wallet-manager.test.ts +++ b/packages/wallet-manager/src/wallet-manager.test.ts @@ -258,7 +258,7 @@ describe('Wallet Manager', () => { // Get Wallet instance const wallet = walletManager.getWallet(accounts[0].address); // Sign message - const signedMessage = wallet.signMessage('hello'); + const signedMessage = await wallet.signMessage('hello'); // Verify signedMessage is the same from account 0 const address = Signer.recoverAddress(hashMessage('hello'), signedMessage); expect(address).toEqual(accounts[0].address); diff --git a/packages/wallet/src/wallet.test.ts b/packages/wallet/src/wallet.test.ts index c307d9ff7a0..17f1660c2dd 100644 --- a/packages/wallet/src/wallet.test.ts +++ b/packages/wallet/src/wallet.test.ts @@ -19,7 +19,7 @@ describe('Wallet', () => { it('Sign a message using wallet instance', async () => { const wallet = new Wallet(signMessageTest.privateKey); - const signedMessage = wallet.signMessage(signMessageTest.message); + const signedMessage = await wallet.signMessage(signMessageTest.message); const verifiedAddress = Signer.recoverAddress( hashMessage(signMessageTest.message), signedMessage @@ -32,7 +32,7 @@ describe('Wallet', () => { it('Sign a transaction using wallet instance', async () => { const wallet = new Wallet(signTransactionTest.privateKey); const transactionRequest = signTransactionTest.transaction; - const signedTransaction = wallet.signTransaction(transactionRequest); + const signedTransaction = await wallet.signTransaction(transactionRequest); const verifiedAddress = Signer.recoverAddress( hashTransaction(transactionRequest), signedTransaction @@ -45,8 +45,10 @@ describe('Wallet', () => { it('Populate transaction witnesses signature using wallet instance', async () => { const wallet = new Wallet(signTransactionTest.privateKey); const transactionRequest = signTransactionTest.transaction; - const signedTransaction = wallet.signTransaction(transactionRequest); - const populatedTransaction = wallet.populateTransactionWitnessesSignature(transactionRequest); + const signedTransaction = await wallet.signTransaction(transactionRequest); + const populatedTransaction = await wallet.populateTransactionWitnessesSignature( + transactionRequest + ); expect(populatedTransaction.witnesses?.[0]).toBe(signedTransaction); }); @@ -56,9 +58,9 @@ describe('Wallet', () => { const privateKey = randomBytes(32); const otherWallet = new Wallet(privateKey); const transactionRequest = signTransactionTest.transaction; - const signedTransaction = wallet.signTransaction(transactionRequest); - const otherSignedTransaction = otherWallet.signTransaction(transactionRequest); - const populatedTransaction = wallet.populateTransactionWitnessesSignature({ + const signedTransaction = await wallet.signTransaction(transactionRequest); + const otherSignedTransaction = await otherWallet.signTransaction(transactionRequest); + const populatedTransaction = await wallet.populateTransactionWitnessesSignature({ ...transactionRequest, witnesses: [...transactionRequest.witnesses, otherSignedTransaction], }); @@ -93,7 +95,7 @@ describe('Wallet', () => { it('Generate a new random wallet', async () => { const wallet = Wallet.generate(); const message = 'test'; - const signedMessage = wallet.signMessage(message); + const signedMessage = await wallet.signMessage(message); const hashedMessage = hashMessage(message); const recoveredAddress = Signer.recoverAddress(hashedMessage, signedMessage); @@ -107,7 +109,7 @@ describe('Wallet', () => { entropy: randomBytes(32), }); const message = 'test'; - const signedMessage = wallet.signMessage(message); + const signedMessage = await wallet.signMessage(message); const hashedMessage = hashMessage(message); const recoveredAddress = Signer.recoverAddress(hashedMessage, signedMessage); diff --git a/packages/wallet/src/wallet.ts b/packages/wallet/src/wallet.ts index 3e664534f43..9e702e6fc03 100644 --- a/packages/wallet/src/wallet.ts +++ b/packages/wallet/src/wallet.ts @@ -75,9 +75,9 @@ export default class Wallet extends AbstractWallet { * Sign message with wallet instance privateKey * * @param message - Message - * @returns string - Signature a ECDSA 64 bytes + * @returns Promise - Signature a ECDSA 64 bytes */ - signMessage(message: string): string { + async signMessage(message: string): Promise { return this.signer().sign(hashMessage(message)); } @@ -87,7 +87,7 @@ export default class Wallet extends AbstractWallet { * @param transactionRequestLike - TransactionRequestLike * @returns string - Signature a ECDSA 64 bytes */ - signTransaction(transactionRequestLike: TransactionRequestLike): string { + async signTransaction(transactionRequestLike: TransactionRequestLike): Promise { const transactionRequest = transactionRequestify(transactionRequestLike); const hashedTransaction = hashTransaction(transactionRequest); const signature = this.signer().sign(hashedTransaction); @@ -95,12 +95,12 @@ export default class Wallet extends AbstractWallet { return signature; } - populateTransactionWitnessesSignature(transactionRequestLike: TransactionRequestLike) { + async populateTransactionWitnessesSignature(transactionRequestLike: TransactionRequestLike) { const transactionRequest = transactionRequestify(transactionRequestLike); const witnessIndex = transactionRequest.getCoinInputWitnessIndexByOwner(this.address); if (typeof witnessIndex === 'number') { - const signedTransaction = this.signTransaction(transactionRequest); + const signedTransaction = await this.signTransaction(transactionRequest); transactionRequest.updateWitness(witnessIndex, signedTransaction); } @@ -265,7 +265,7 @@ export default class Wallet extends AbstractWallet { const transactionRequest = transactionRequestify(transactionRequestLike); return this.provider.sendTransaction( - this.populateTransactionWitnessesSignature(transactionRequest) + await this.populateTransactionWitnessesSignature(transactionRequest) ); } @@ -278,9 +278,12 @@ export default class Wallet extends AbstractWallet { async simulateTransaction(transactionRequestLike: TransactionRequestLike): Promise { const transactionRequest = transactionRequestify(transactionRequestLike); - return this.provider.call(this.populateTransactionWitnessesSignature(transactionRequest), { - utxoValidation: true, - }); + return this.provider.call( + await this.populateTransactionWitnessesSignature(transactionRequest), + { + utxoValidation: true, + } + ); } async buildPredicateTransaction(