Skip to content

Commit

Permalink
use EIP2098
Browse files Browse the repository at this point in the history
  • Loading branch information
0age committed Dec 9, 2024
1 parent 9cad3c6 commit 285b35c
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 2 deletions.
16 changes: 14 additions & 2 deletions src/crypto.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
import { type Hex } from 'viem';
import {
type Hex,
signatureToCompactSignature,
serializeCompactSignature,
parseSignature,
} from 'viem';
import {
hashTypedData,
keccak256,
Expand Down Expand Up @@ -137,10 +142,17 @@ export async function generateClaimHash(

export async function signCompact(hash: Hex, _chainId: bigint): Promise<Hex> {
// 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 {
Expand Down
18 changes: 18 additions & 0 deletions src/types/viem.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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' {
Expand Down

0 comments on commit 285b35c

Please sign in to comment.