Skip to content

Commit 982e2ca

Browse files
committed
fix: resolves #3593
1 parent 4b96010 commit 982e2ca

File tree

4 files changed

+25
-1
lines changed

4 files changed

+25
-1
lines changed

.changeset/lazy-snakes-yawn.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"viem": patch
3+
---
4+
5+
Added assertion for signature length in signature validation.

src/actions/public/verifyHash.test.ts

+17
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,12 @@ import { http } from '../../clients/transports/http.js'
2525
import { signMessage as signMessageErc1271 } from '../../experimental/erc7739/actions/signMessage.js'
2626
import type { Hex } from '../../types/misc.js'
2727
import {
28+
concat,
2829
encodeFunctionData,
2930
hashMessage,
3031
pad,
3132
serializeErc6492Signature,
33+
slice,
3234
toBytes,
3335
} from '../../utils/index.js'
3436
import { parseSignature } from '../../utils/signature/parseSignature.js'
@@ -471,3 +473,18 @@ test('https://github.com/wevm/viem/issues/2484', async () => {
471473
}),
472474
).resolves.toBe(true)
473475
})
476+
477+
test('https://github.com/wevm/viem/issues/3593', async () => {
478+
const signature = await signMessage(client, {
479+
account: localAccount,
480+
message: 'hello world',
481+
})
482+
483+
expect(
484+
verifyHash(client, {
485+
address: localAccount.address,
486+
hash: hashMessage('hello world'),
487+
signature: concat([slice(signature, 0, 64), '0x001b']),
488+
}),
489+
).resolves.toBe(false)
490+
})

src/utils/signature/recoverAddress.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,5 +19,5 @@ export async function recoverAddress({
1919
hash,
2020
signature,
2121
}: RecoverAddressParameters): Promise<RecoverAddressReturnType> {
22-
return publicKeyToAddress(await recoverPublicKey({ hash: hash, signature }))
22+
return publicKeyToAddress(await recoverPublicKey({ hash, signature }))
2323
}

src/utils/signature/recoverPublicKey.ts

+2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import type { ErrorType } from '../../errors/utils.js'
22
import type { ByteArray, Hex, Signature } from '../../types/misc.js'
33
import { type IsHexErrorType, isHex } from '../data/isHex.js'
4+
import { size } from '../data/size.js'
45
import {
56
type HexToNumberErrorType,
67
hexToBigInt,
@@ -41,6 +42,7 @@ export async function recoverPublicKey({
4142

4243
// typeof signature: `Hex | ByteArray`
4344
const signatureHex = isHex(signature) ? signature : toHex(signature)
45+
if (size(signatureHex) !== 65) throw new Error('invalid signature length')
4446
const yParityOrV = hexToNumber(`0x${signatureHex.slice(130)}`)
4547
const recoveryBit = toRecoveryBit(yParityOrV)
4648
return secp256k1.Signature.fromCompact(

0 commit comments

Comments
 (0)