Skip to content

Commit

Permalink
feat: add support for raw sign method on viem account (#98)
Browse files Browse the repository at this point in the history
Ahead of Viem making `sign` mandatory in v3

#https://github.com/wevm/viem/blob/64a21b080ab30eebb9991444a5fafd9d3c15a801/src/accounts/types.ts#L30
and necessary to support zkSync elastic chains signing.

---------

Co-authored-by: Jean Regisser <[email protected]>
  • Loading branch information
jamalavedra and jeanregisser authored Feb 4, 2025
1 parent bdf263a commit 80a732f
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 0 deletions.
24 changes: 24 additions & 0 deletions src/gcpHsmToAccount.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
recoverTransactionAddress,
recoverTypedDataAddress,
toHex,
verifyHash,
verifyMessage,
} from 'viem'
import { privateKeyToAccount } from 'viem/accounts'
Expand Down Expand Up @@ -87,6 +88,7 @@ describe('gcpHsmToAccount', () => {
address: ACCOUNT1.address,
publicKey: ACCOUNT1.publicKey,
signMessage: expect.any(Function),
sign: expect.any(Function),
signTransaction: expect.any(Function),
signTypedData: expect.any(Function),
source: 'gcpHsm',
Expand Down Expand Up @@ -124,6 +126,28 @@ describe('gcpHsmToAccount', () => {
).resolves.toBeTruthy()
})

it('signs a hash', async () => {
const gcpHsmAccount = (await gcpHsmToAccount({
hsmKeyVersion: MOCK_GCP_HSM_KEY_NAME,
kmsClient: mockKmsClient,
})) as { sign: (params: { hash: Hex }) => Promise<Hex> }

const hash =
'0xd9eba16ed0ecae432b71fe008c98cc872bb4cc214d3220a36f365326cf807d68'
const signature = await gcpHsmAccount.sign({ hash })

expect(signature).toBe(
'0x08c183d08a952dcd603148842de1d7844a1a6d72a3761840ebe10a570240821e3348c9296af823c8f4de5258f997fa35ee4ad8fce79cda929021f6976d0c10431c',
)
await expect(
verifyHash({
address: ACCOUNT1.address,
hash: hash,
signature: signature,
}),
).resolves.toBeTruthy()
})

it('signs a transaction', async () => {
const gcpHsmAccount = await gcpHsmToAccount({
hsmKeyVersion: MOCK_GCP_HSM_KEY_NAME,
Expand Down
4 changes: 4 additions & 0 deletions src/gcpHsmToAccount.ts
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,10 @@ export async function gcpHsmToAccount({
)
return signatureToHex(signature)
},
async sign({ hash }) {
const signature = await sign(kmsClient, hsmKeyVersion, publicKey, hash)
return signatureToHex(signature)
},
async signTransaction(
transaction,
{ serializer = serializeTransaction } = {},
Expand Down

0 comments on commit 80a732f

Please sign in to comment.