Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
ukstv committed Nov 23, 2023
1 parent 11502a2 commit b3a813b
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 9 deletions.
25 changes: 22 additions & 3 deletions packages/varsig/src/__tests__/canonicalization.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,23 @@
import { test } from '@jest/globals'
import { expect, test } from '@jest/globals'
import { BytesTape } from '../bytes-tape.js'
import { CanonicalizationDecoder, CanonicalizationKind } from '../canonicalization.js'
import { concat, toString } from 'uint8arrays'
import { encode } from 'varintes/encode'

test.todo('EIP712')
test.todo('EIP191')
test('EIP712', () => {
const bytes = concat([encode(0xe712)[0]])
const tape = new BytesTape(bytes)
const result = CanonicalizationDecoder.read(tape)
expect(result.kind).toEqual(CanonicalizationKind.EIP191)
// const canonicalized = toString(result('Hello'), 'hex')
// expect(canonicalized).toEqual('19457468657265756d205369676e6564204d6573736167653a0a3548656c6c6f')
})

test('EIP191', () => {
const bytes = concat([encode(0xe191)[0]])
const tape = new BytesTape(bytes)
const result = CanonicalizationDecoder.read(tape)
expect(result.kind).toEqual(CanonicalizationKind.EIP191)
const canonicalized = toString(result('Hello'), 'hex')
expect(canonicalized).toEqual('19457468657265756d205369676e6564204d6573736167653a0a3548656c6c6f')
})
11 changes: 6 additions & 5 deletions packages/varsig/src/canonicalization.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { BytesTape } from './bytes-tape.js'
import * as uint8arrays from 'uint8arrays'
import { keccak_256 } from '@noble/hashes/sha3'
import { UnreacheableCaseError } from './unreachable-case-error.js'
import { hashTypedData } from 'viem'
import { CompressedDomain, decompressDomain, decompressTypes } from './encoding/eip712.js'
Expand All @@ -25,6 +24,10 @@ export type CanonicalizationAlgo = CanonicalizationEIP191 | CanonicalizationEIP7
export class CanonicalizationDecoder {
constructor(private readonly tape: BytesTape) {}

static read(tape: BytesTape) {
return new CanonicalizationDecoder(tape).read()
}

read(): CanonicalizationAlgo {
const sigil = this.tape.readVarint<CanonicalizationKind>()
switch (sigil) {
Expand All @@ -47,10 +50,8 @@ export class CanonicalizationDecoder {
}
case CanonicalizationKind.EIP191: {
const fn = (message: string) => {
return keccak_256(
uint8arrays.fromString(
`\x19Ethereum Signed Message:\n` + String(message.length) + message
)
return uint8arrays.fromString(
`\x19Ethereum Signed Message:\n` + String(message.length) + message
)
}
fn.kind = CanonicalizationKind.EIP191
Expand Down
3 changes: 2 additions & 1 deletion packages/varsig/test/parse-eip191.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { BytesTape } from '../src/bytes-tape.js'
import { SigningDecoder } from '../src/signing.js'
import { HashingDecoder } from '../src/hashing.js'
import { CanonicalizationDecoder, CanonicalizationKind } from '../src/canonicalization.js'
import { keccak_256 } from '@noble/hashes/sha3'

class Decoder {
#tape: BytesTape
Expand Down Expand Up @@ -70,5 +71,5 @@ test('validate eip191', async () => {
if (decoder.signing.recoveryBit) {
signature = signature.addRecoveryBit(decoder.signing.recoveryBit - 27)
}
console.log(signature.recoverPublicKey(input).toHex(false))
console.log(signature.recoverPublicKey(keccak_256(input)).toHex(false))
})

0 comments on commit b3a813b

Please sign in to comment.