Skip to content

Commit

Permalink
Split hash and sign
Browse files Browse the repository at this point in the history
  • Loading branch information
Agusx1211 committed Sep 11, 2023
1 parent 74e5c94 commit 9a0dab9
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 2 deletions.
13 changes: 11 additions & 2 deletions packages/waas/src/payloads/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,16 @@ export type Payload<T extends BasePacket> = {
signatures: Signature[]
}

export function hashPacket(packet: Payload<BasePacket> | BasePacket): ethers.Bytes {
if ('version' in packet) {
packet = packet.packet
}

const encoded = ethers.utils.toUtf8Bytes(JSON.stringify(packet, null, 0))
return ethers.utils.arrayify(ethers.utils.keccak256(encoded))
}

export function signPacket(signer: ethers.Signer, packed: BasePacket): Promise<string> {
const encoded = ethers.utils.toUtf8Bytes(JSON.stringify(packed, null, 0))
return signer.signMessage(encoded)
const hash = hashPacket(packed)
return signer.signMessage(hash)
}
39 changes: 39 additions & 0 deletions packages/waas/tests/payloads.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import * as chai from 'chai'
import { ethers } from 'ethers'

import { Payload, hashPacket, signPacket } from '../src/payloads'
import { TransactionsPacket } from '../src/payloads/wallet'

const { expect } = chai

describe('Payloads', () => {
it('Should sign a transactions payload', async () => {
const transactionsPacket: TransactionsPacket = {
code: 'sendTransactions',
wallet: '0xD67FC48b298B09Ed3D03403d930769C527186c4e',
chainId: 1,
transactions: [{
type: 'erc20send',
token: ethers.constants.AddressZero,
to: '0x0dc9603d4da53841C1C83f3B550C6143e60e0425',
value: '0'
}]
}

const sessionSigner = new ethers.Wallet('0xecd39e2cdadc2427255042ca7e0f86368bd7aa6e3c99470444b7d073840c1b51')
const sessionSignature = await signPacket(sessionSigner, transactionsPacket)

const payloads: Payload<TransactionsPacket> = {
version: '1',
packet: transactionsPacket,
signatures: [{
session: sessionSigner.address,
signature: sessionSignature
}]
}

const hash = hashPacket(payloads)
expect(ethers.utils.hexlify(hash)).to.equal('0xe54e41eca96c1c047ad6a31f80dd3e61ba5f8a6e0a8a83b95e58515afe1780da')
expect(sessionSignature).to.equal('0xd22e5853e09ea29812774fd658811aa6007781d3a7fa23bf5ab69d58cd18cd6467cf1a7969b982695e35db5514b90313c53a9eb941a986d572b35c5bd1d0a46f1b')
})
})

0 comments on commit 9a0dab9

Please sign in to comment.