Once you've instantiated your wallet in an unlocked state using one of the previously discussed methods, you can sign a message with wallet.sign_message
. Below is a full example of how to sign and recover a message.
import { WalletUnlocked, hashMessage, Signer} from 'fuels';
const wallet = WalletUnlocked.generate();
const message = 'doc-test-message';
const signedMessage = await wallet.signMessage(message);
const hashedMessage = hashMessage(message);
const recoveredAddress = Signer.recoverAddress(hashedMessage, signedMessage);
expect(wallet.privateKey).toBeTruthy();
expect(wallet.publicKey).toBeTruthy();
expect(wallet.address).toEqual(recoveredAddress);
You can also sign a transaction by using wallet.sign_transaction
. Below is a full example of how to sign and recover a transaction.
import { WalletUnlocked, hashMessage, Signer} from 'fuels';
const wallet = new WalletUnlocked(signTransactionTest.privateKey);
const transactionRequest = signTransactionTest.transaction;
const signedTransaction = await wallet.signTransaction(transactionRequest);
const chainId = (await wallet.provider.getChain()).consensusParameters.chainId.toNumber();
const verifiedAddress = Signer.recoverAddress(
hashTransaction(transactionRequest, chainId),
signedTransaction
);
expect(signedTransaction).toEqual(signTransactionTest.signedTransaction);
expect(verifiedAddress).toEqual(wallet.address);