From 285b35c0bf1c2042b410c682f2d458467a329bab Mon Sep 17 00:00:00 2001 From: 0age <37939117+0age@users.noreply.github.com> Date: Mon, 9 Dec 2024 03:48:58 -0800 Subject: [PATCH] use EIP2098 --- src/crypto.ts | 16 ++++++++++++++-- src/types/viem.d.ts | 18 ++++++++++++++++++ 2 files changed, 32 insertions(+), 2 deletions(-) diff --git a/src/crypto.ts b/src/crypto.ts index 19502ed..04347cc 100644 --- a/src/crypto.ts +++ b/src/crypto.ts @@ -1,4 +1,9 @@ -import { type Hex } from 'viem'; +import { + type Hex, + signatureToCompactSignature, + serializeCompactSignature, + parseSignature, +} from 'viem'; import { hashTypedData, keccak256, @@ -137,10 +142,17 @@ export async function generateClaimHash( export async function signCompact(hash: Hex, _chainId: bigint): Promise { // Sign the hash directly using the private key - return await signMessage({ + const signature = await signMessage({ message: { raw: hash }, privateKey, }); + + // Parse the signature into its components + const parsedSignature = parseSignature(signature); + + // Convert to EIP2098 compact signature format and serialize + const compactSig = signatureToCompactSignature(parsedSignature); + return serializeCompactSignature(compactSig); } export function getSigningAddress(): string { diff --git a/src/types/viem.d.ts b/src/types/viem.d.ts index 10c608b..cd88882 100644 --- a/src/types/viem.d.ts +++ b/src/types/viem.d.ts @@ -6,6 +6,17 @@ declare module 'viem' { export type Bytes = Hex; export type SignableMessage = string | Bytes; + export type Signature = { + r: Hex; + s: Hex; + yParity: 0 | 1; + }; + + export type CompactSignature = { + r: Hex; + yParityAndS: Hex; + }; + export type AbiParameter = { name: string; type: string; @@ -36,6 +47,13 @@ declare module 'viem' { values: readonly any[] ): Hex; export function concat(values: readonly Hex[]): Hex; + export function signatureToCompactSignature( + signature: Signature + ): CompactSignature; + export function serializeCompactSignature( + compactSignature: CompactSignature + ): Hex; + export function parseSignature(signatureHex: Hex): Signature; } declare module 'viem/accounts' {