Skip to content

Commit

Permalink
JWS Canon test
Browse files Browse the repository at this point in the history
  • Loading branch information
oed committed Jan 10, 2024
1 parent efb66ec commit 7c9534e
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 2 deletions.
41 changes: 41 additions & 0 deletions packages/varsig/src/__tests__/canons/jws.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import { fromOriginal, prepareCanonicalization } from '../../canons/jws.js'
import { BytesTape } from '../../bytes-tape.js'
import * as uint8arrays from 'uint8arrays'
import { CanonicalizationKind } from '../../canonicalization.js'

//const testJws = 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpYXQiOjE1MTYyMzkwMjIsIm5hbWUiOiJKb2huIERvZSIsInN1YiI6IjEyMzQ1Njc4OTAifQ.SflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV_adQssw5c'
const testJws = 'eyJhbGciOiAiRVMyNTYifQ.eyJpYXQiOjE1MTYyMzkwMjIsIm5hbWUiOiJKb2huIERvZSIsInN1YiI6IjEyMzQ1Njc4OTAifQ.SflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV_adQssw5c'

const expectedHashInput = uint8arrays.fromString(
testJws.slice(0, testJws.lastIndexOf('.'))
)

test('Encode eip712 message', () => {
// @ts-ignore
const node = fromOriginal(testJws)

expect(node._sig.length).toEqual(56)
delete node._sig
expect(node).toEqual({
sub: '1234567890',
name: 'John Doe',
iat: 1516239022
})
})

test('Canonicalize ipld eip712 object', () => {
// @ts-ignore
const node = fromOriginal(testJws)
const tape = new BytesTape(node._sig)
tape.readVarint() // skip sigil
const keyType = tape.readVarint() // skip key type
const hashType = tape.readVarint() // skip hash type
tape.readVarint() // skip canonicalizer codec
const can = prepareCanonicalization(tape, hashType, keyType)
expect(can.kind).toEqual(CanonicalizationKind.JWS)
expect(tape.remainder.length).toEqual(32)
// @ts-ignore
delete node._sig
const sigInput = can(node)
expect(sigInput).toEqual(expectedHashInput)
})
1 change: 1 addition & 0 deletions packages/varsig/src/canonicalization.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import type { SigningKind } from './signing.js'
export enum CanonicalizationKind {
EIP712 = MAGIC.EIP712,
EIP191 = MAGIC.EIP191,
JWS = MAGIC.JOSE,
}

type IpldNode = Record<string, any>
Expand Down
4 changes: 2 additions & 2 deletions packages/varsig/src/canons/jws.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ export function prepareCanonicalization(

const can = (node: IpldNode) => {
// encode node using dag-json from multiformats
const payloadB64u = toB64u(uint8arrays.fromString(JSON.stringify(encode(node))))
const payloadB64u = toB64u(encode(node))
const protectedB64u = toB64u(protectedBytes)
return uint8arrays.fromString(`${protectedB64u}.${payloadB64u}`)
}
Expand Down Expand Up @@ -96,7 +96,7 @@ interface ProtectedHeader {

function findKeyType({ alg, crv }: ProtectedHeader): number {
if (!alg) throw new Error(`Missing alg in protected header`)
const keyType = KEY_TYPE_BY_ALG_CRV[alg][crv || 'default']
const keyType = KEY_TYPE_BY_ALG_CRV[alg]?.[crv || 'default']
if (!keyType) throw new Error(`Unsupported alg: ${alg}, or crv: ${String(crv)}`)
return keyType
}

0 comments on commit 7c9534e

Please sign in to comment.