Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: hashMessage bytes conversion #475

Merged
merged 4 commits into from
May 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/polite-carrots-vanish.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"viem": patch
---

Fixed \`hashMessage\` string conversion for messages that have same format as hex bytes.
18 changes: 2 additions & 16 deletions src/actions/public/verifyHash.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,22 +20,10 @@ describe('verifyHash', async () => {
'0xefd5fb29a274ea6682673d8b3caa9263e936d48d486e5df68893003e0a76496439594d12245008c6fba1c8e3ef28241cffe1bef27ff6bca487b167f261f329251c',
expectedResult: true,
},
{
_name: 'deployed, supports ERC1271, valid signature, hex message',
address: smartAccountConfig.address,
hash: hashMessage(
'0x5468697320697320612074657374206d65737361676520666f72207669656d21',
),
signature:
'0xefd5fb29a274ea6682673d8b3caa9263e936d48d486e5df68893003e0a76496439594d12245008c6fba1c8e3ef28241cffe1bef27ff6bca487b167f261f329251c',
expectedResult: true,
},
{
_name: 'deployed, supports ERC1271, invalid signature',
address: smartAccountConfig.address,
hash: hashMessage(
'0x5468697320697320612074657374206d65737361676520666f72207669656d21',
),
hash: hashMessage('This is a test message for viem!'),
signature: '0xdead',
expectedResult: false,
},
Expand Down Expand Up @@ -91,9 +79,7 @@ describe('verifyHash', async () => {
expect(
await verifyHash(publicClient, {
address: smartAccountConfig.address,
hash: hashMessage(
'0x5468697320697320612074657374206d65737361676520666f72207669656d21',
),
hash: hashMessage('This is a test message for viem!'),
signature: toBytes(
'0xefd5fb29a274ea6682673d8b3caa9263e936d48d486e5df68893003e0a76496439594d12245008c6fba1c8e3ef28241cffe1bef27ff6bca487b167f261f329251c',
),
Expand Down
9 changes: 3 additions & 6 deletions src/actions/public/verifyMessage.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@ describe('verifyMessage', async () => {
expect(
await verifyMessage(publicClientMainnet, {
address: smartAccountConfig.address,
message:
'0x5468697320697320612074657374206d65737361676520666f72207669656d21',
message: 'This is a test message for viem!',
signature:
'0xefd5fb29a274ea6682673d8b3caa9263e936d48d486e5df68893003e0a76496439594d12245008c6fba1c8e3ef28241cffe1bef27ff6bca487b167f261f329251c',
}),
Expand All @@ -22,8 +21,7 @@ describe('verifyMessage', async () => {
expect(
await verifyMessage(publicClientMainnet, {
address: smartAccountConfig.address,
message:
'0x5468697320697320612074657374206d65737361676520666f72207669656d21',
message: 'This is a test message for viem!',
signature: '0xdead',
}),
).toBe(false)
Expand All @@ -33,8 +31,7 @@ describe('verifyMessage', async () => {
expect(
await verifyMessage(publicClientMainnet, {
address: address.notDeployed,
message:
'0x5468697320697320612074657374206d65737361676520666f72207669656d21',
message: 'This is a test message for viem!',
signature:
'0xefd5fb29a274ea6682673d8b3caa9263e936d48d486e5df68893003e0a76496439594d12245008c6fba1c8e3ef28241cffe1bef27ff6bca487b167f261f329251c',
}),
Expand Down
41 changes: 41 additions & 0 deletions src/utils/signature/hashMessage.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ test('to hex', () => {
expect(hashMessage('🤗')).toMatchInlineSnapshot(
'"0x716ce69c5d2d629c168bc02e24a961456bdc5a362d366119305aea73978a0332"',
)
expect(hashMessage('0xdeadbeef')).toMatchInlineSnapshot(
'"0xefedd0a9a0294228c3977d7fbb68c7d40279f8b408cf3e24ef1823b179709e58"',
)
})

test('to bytes', () => {
Expand Down Expand Up @@ -88,4 +91,42 @@ test('to bytes', () => {
]
`,
)
expect(hashMessage('0xdeadbeef', 'bytes')).toMatchInlineSnapshot(
`
Uint8Array [
239,
237,
208,
169,
160,
41,
66,
40,
195,
151,
125,
127,
187,
104,
199,
212,
2,
121,
248,
180,
8,
207,
62,
36,
239,
24,
35,
177,
121,
112,
158,
88,
]
`,
)
})
6 changes: 3 additions & 3 deletions src/utils/signature/hashMessage.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { ByteArray, Hex } from '../../types/misc.js'
import { concat } from '../data/concat.js'
import { toBytes } from '../encoding/toBytes.js'
import { stringToBytes } from '../encoding/toBytes.js'
import { keccak256 } from '../hash/keccak256.js'

type To = 'hex' | 'bytes'
Expand All @@ -13,8 +13,8 @@ export function hashMessage<TTo extends To = 'hex'>(
message: string,
to_?: TTo,
): HashMessage<TTo> {
const messageBytes = toBytes(message)
const prefixBytes = toBytes(
const messageBytes = stringToBytes(message)
const prefixBytes = stringToBytes(
`\x19Ethereum Signed Message:\n${messageBytes.length}`,
)
return keccak256(concat([prefixBytes, messageBytes]), to_)
Expand Down