From 7c9534ee238219551bc77bc741484d6351604c60 Mon Sep 17 00:00:00 2001 From: Joel Thorstensson Date: Wed, 10 Jan 2024 17:59:22 +0100 Subject: [PATCH] JWS Canon test --- .../varsig/src/__tests__/canons/jws.test.ts | 41 +++++++++++++++++++ packages/varsig/src/canonicalization.ts | 1 + packages/varsig/src/canons/jws.ts | 4 +- 3 files changed, 44 insertions(+), 2 deletions(-) create mode 100644 packages/varsig/src/__tests__/canons/jws.test.ts diff --git a/packages/varsig/src/__tests__/canons/jws.test.ts b/packages/varsig/src/__tests__/canons/jws.test.ts new file mode 100644 index 00000000..44d64c8c --- /dev/null +++ b/packages/varsig/src/__tests__/canons/jws.test.ts @@ -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) +}) diff --git a/packages/varsig/src/canonicalization.ts b/packages/varsig/src/canonicalization.ts index c391c008..4f4f57f6 100644 --- a/packages/varsig/src/canonicalization.ts +++ b/packages/varsig/src/canonicalization.ts @@ -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 diff --git a/packages/varsig/src/canons/jws.ts b/packages/varsig/src/canons/jws.ts index b09cbf05..aea05143 100644 --- a/packages/varsig/src/canons/jws.ts +++ b/packages/varsig/src/canons/jws.ts @@ -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}`) } @@ -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 }