Skip to content

Commit

Permalink
refactor: change sign methods to be async (#552)
Browse files Browse the repository at this point in the history
  • Loading branch information
luizstacio authored Oct 27, 2022
1 parent 2cf77e8 commit fcacb06
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 19 deletions.
6 changes: 6 additions & 0 deletions .changeset/soft-elephants-drum.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"@fuel-ts/wallet": minor
"@fuel-ts/wallet-manager": minor
---

Change sign methods to be async
2 changes: 1 addition & 1 deletion packages/wallet-manager/src/wallet-manager.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
20 changes: 11 additions & 9 deletions packages/wallet/src/wallet.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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);
});
Expand All @@ -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],
});
Expand Down Expand Up @@ -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);

Expand All @@ -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);

Expand Down
21 changes: 12 additions & 9 deletions packages/wallet/src/wallet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string> - Signature a ECDSA 64 bytes
*/
signMessage(message: string): string {
async signMessage(message: string): Promise<string> {
return this.signer().sign(hashMessage(message));
}

Expand All @@ -87,20 +87,20 @@ 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<string> {
const transactionRequest = transactionRequestify(transactionRequestLike);
const hashedTransaction = hashTransaction(transactionRequest);
const signature = this.signer().sign(hashedTransaction);

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);
}

Expand Down Expand Up @@ -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)
);
}

Expand All @@ -278,9 +278,12 @@ export default class Wallet extends AbstractWallet {
async simulateTransaction(transactionRequestLike: TransactionRequestLike): Promise<CallResult> {
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(
Expand Down

0 comments on commit fcacb06

Please sign in to comment.