Skip to content

Commit

Permalink
feat: add auto prefixing check to account.signMessage
Browse files Browse the repository at this point in the history
  • Loading branch information
VGabriel45 committed Mar 5, 2025
1 parent cfdba9b commit 6bb5db0
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 2 deletions.
36 changes: 34 additions & 2 deletions packages/account/src/account.ts
Original file line number Diff line number Diff line change
Expand Up @@ -788,12 +788,44 @@ export class Account {
return this.relayer(chainId).relay({ ...bootstrapTxs, chainId }, feeQuote)
}

/**
* Signs a message.
*
* This method will sign the message using the account associated with this signer
* and the specified chain ID. The message is already being prefixed with the EIP-191 prefix.
*
* @param message - The message to sign. Can be a string or BytesLike.
* @returns A Promise that resolves to the signature as a hexadecimal string
*
* @example
* ```typescript
* const message = "Hello, Sequence!";
* const signature = await account.signMessage(message);
* console.log(signature);
* // => "0x123abc..." (hexadecimal signature)
*/
signMessage(
message: ethers.BytesLike,
message: ethers.BytesLike | string,
chainId: ethers.BigNumberish,
cantValidateBehavior: 'ignore' | 'eip6492' | 'throw' = 'ignore'
): Promise<string> {
return this.signDigest(ethers.keccak256(message), chainId, true, cantValidateBehavior)
const messageBytes = typeof message === 'string' ? ethers.toUtf8Bytes(message) : message
const eip191prefix = ethers.toUtf8Bytes('\x19Ethereum Signed Message:\n')

const messageHex = ethers.hexlify(messageBytes)
const prefixHex = ethers.hexlify(eip191prefix)

let prefixedMessage: Uint8Array

if (messageHex.startsWith(prefixHex)) {
prefixedMessage = ethers.getBytes(messageBytes)
} else {
prefixedMessage = ethers.getBytes(
ethers.concat([eip191prefix, ethers.toUtf8Bytes(String(messageBytes.length)), messageBytes])
)
}

return this.signDigest(ethers.keccak256(prefixedMessage), chainId, true, cantValidateBehavior)
}

async signTransactions(
Expand Down
18 changes: 18 additions & 0 deletions packages/account/src/signer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,24 @@ export class AccountSigner implements ethers.AbstractSigner<ethers.Provider> {
return this.account.address
}

/**
* Signs a message.
*
* This method will sign the message using the account associated with this signer
* and the specified chain ID. The message is already being prefixed with the EIP-191 prefix.
*
* @param message - The message to sign. Can be a string or BytesLike.
* @returns A Promise that resolves to the signature as a hexadecimal string
*
* @example
* ```typescript
* const signer = account.getSigner(chainId)
*
* const message = "Hello, Sequence!";
* const signature = await signer.signMessage(message);
* console.log(signature);
* // => "0x123abc..." (hexadecimal signature)
*/
signMessage(message: string | ethers.BytesLike): Promise<string> {
return this.account.signMessage(message, this.chainId, this.options?.cantValidateBehavior ?? 'throw')
}
Expand Down

0 comments on commit 6bb5db0

Please sign in to comment.