From f4911bbdeab2c36421c7bbc10cfd7a9247b15e36 Mon Sep 17 00:00:00 2001 From: Joel Thorstensson Date: Thu, 8 Feb 2024 21:52:33 +0000 Subject: [PATCH] All varsig tests passing --- .../varsig/src/__tests__/__vectors__/jws.car | Bin 10776 -> 10845 bytes ...p256k1.test.ts => eip712-fixtures.test.ts} | 0 ...jws-es256.test.ts => jws-fixtures.test.ts} | 48 ++++++++++++++---- 3 files changed, 37 insertions(+), 11 deletions(-) rename packages/varsig/src/__tests__/{eip712-secp256k1.test.ts => eip712-fixtures.test.ts} (100%) rename packages/varsig/src/__tests__/{jws-es256.test.ts => jws-fixtures.test.ts} (85%) diff --git a/packages/varsig/src/__tests__/__vectors__/jws.car b/packages/varsig/src/__tests__/__vectors__/jws.car index ddde7a8197a79ea1fd525f5e94f3ee8222c2212f..c0c44219c1cfe54ab3ca95a34c6d769ee73fa849 100644 GIT binary patch literal 10845 zcmds-dpy(oAIFJGI4LQIB&j$RF?Mn3q%mg0*v2+e9AuwCfX4z@AX8)r)OB028A33WMuq=8NB7b#Rh>9fW_YNngQ+Xo27xqPHZ}fs z15H?F*^15Hju?-)Z`?NBh~Cn4xe1N}i@CLx>G`Y1Y0!_2J9RFzDCUvYtokZ%Zj*)M z@f9s&cXM3*c5p<1P~-{nxL4;BWD8W8u#?RcvO^41G3jFl7f|r4ra9x6S(5!L@Af9H zp=1*8RMwUVoAUFUt+gLpSTDGo*e@%9S`FS4n`!HY0f1RP1H zaX7&=XCd7;Qg)pbNv9&h=wzG_7ZJ{3yWo&szAQ08N)&rZ(J%s846?BlDce;N85|)5 zy?H{=GZu%$Mc_GfUbw`cN{IH4fW@+C93Bus;?c1b*>`fm5SsNuH=2|qfpMfl7h5t3 zL1ht$EErOPqF^Nwrj#Y1#)c7XnaoIm7!`r%!Nph?wYY|0k z9-j>;NP&tTh+&3<>=z-jba{YaCtn5*7y`1GA{jj%Kr9Wdp6{(6ebr|de)F%TM(b3u zt;X7$&p2s5ZOgl|v+O~h87a9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2A;x&;}FilE@&JM#rLE5!&wA6hbF+&cv3H! z#=bV!NxMpW(+{k>+KX-`C7tK>gV%ksQ~!hSJM8m&`+^Ef!({on`}eG7rVy7bYmyqK>m{=FWQ~Wo8;OkG9(`T(J4t+k+9M-`n>DW(oq{EuVG0y6lM#t5DJFusVGE8ab;_A!_W) z0=Ou<(9(qdc7;cZ0-lT?Hmm<Y|%73$DiK4Y-iC$PnAs*{8ISQ@B) z!vO~0u>ihA1OnoJX^$X5oubkfLLK7K7D646&=x{Hn${LVJ>%0BLOo#87D8>@YYU;a zUA2W!dsx~+s67^KA=C}7wh-#xLt6-CdfGxL6VVnzjR9>T)ISu|w*2(~ARb~CpbOQn l92g5=IJvsQ5CFh%ad8*YseA!neJ+m+Du&MH(W7MI{{f;Wv}ynV diff --git a/packages/varsig/src/__tests__/eip712-secp256k1.test.ts b/packages/varsig/src/__tests__/eip712-fixtures.test.ts similarity index 100% rename from packages/varsig/src/__tests__/eip712-secp256k1.test.ts rename to packages/varsig/src/__tests__/eip712-fixtures.test.ts diff --git a/packages/varsig/src/__tests__/jws-es256.test.ts b/packages/varsig/src/__tests__/jws-fixtures.test.ts similarity index 85% rename from packages/varsig/src/__tests__/jws-es256.test.ts rename to packages/varsig/src/__tests__/jws-fixtures.test.ts index e7bce6b..7afd786 100644 --- a/packages/varsig/src/__tests__/jws-es256.test.ts +++ b/packages/varsig/src/__tests__/jws-fixtures.test.ts @@ -22,7 +22,7 @@ describe('jws.car', () => { entries = root.entries as Array }) - // test('GenerateVectors', async () => { + // test.skip('GenerateVectors', async () => { // await createVectors() // }) @@ -77,7 +77,7 @@ describe('jws.car', () => { } }) - test.skip('Recover original from ipld node', async () => { + test('Recover original from ipld node', async () => { for (const entryCID of entries) { // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment const entry = car.get(entryCID) @@ -89,16 +89,22 @@ describe('jws.car', () => { const node = car.get(entry.node) // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment const originalKlone = klona(originalExpected) - console.log('ori', originalKlone) // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment,@typescript-eslint/no-unsafe-argument if (entry.valid) { const originalRecovered = await toOriginal(node) - console.log('rec', originalRecovered) expect(originalRecovered).toEqual(originalKlone) } else { - console.log(entry) - expect(toOriginal(node)).rejects.toThrow() + if (entry.error === 'Invalid hash code') { + expect(toOriginal(node)).rejects.toThrow(/Hash type missmatch/) + } else if (entry.error === 'Missing alg in protected header') { + expect(toOriginal(node)).rejects.toThrow('Missing alg in protected header') + } else if (entry.error === 'Invalid signature') { + const originalRecovered = await toOriginal(node) + expect(originalRecovered).toEqual(originalKlone) + } else { + throw new Error('Unknown error') + } } } }) @@ -132,14 +138,14 @@ describe('jws.car', () => { // signature: ['es256', 'secp256k1', 'ed25519', 'ed448'] // }, { isRoot: true }) // -// await pipeline(car, fs.createWriteStream("./jws.car")); +// await pipeline(car, fs.createWriteStream("./src/__tests__/__vectors__/jws.car")); // // async function gen(name, opt, alg, crv) { // const kp = generateKeyPairSync(name, opt) // const {x, y } = kp.publicKey.export({ format: 'jwk' }) // const verificationKey = y ? // uint8arrays.concat([ -// [0x04], +// new Uint8Array([0x04]), // uint8arrays.fromString(x, 'base64url'), // uint8arrays.fromString(y, 'base64url') // ]) : @@ -162,17 +168,25 @@ describe('jws.car', () => { // // await expect(verify(node, verificationKey)).resolves.toEqual(true) // // console.log('passed one') // +// // contruct valid node // const entry1 = car.put({ // valid: true, // signer: { verificationKey }, // node: car.put(node), // original: car.put(jwt) // }) +// +// // contruct node with incorrect hash kind // const nodeKeccak = klona(node) // let tape = new BytesTape(nodeKeccak._sig) -// tape.read(1); tape.readVarint(); +// tape.read(1); +// let sigKind = tape.readVarint(); +// if (sigKind === MAGIC.SECP256K1) { +// // skip recovery bit for secp256k1 +// tape.read(1) +// } // const hashPosition = tape.position -// nodeKeccak._sig.set([MAGIC.KECCAK_256], hashPosition) // TODO - fix +// nodeKeccak._sig.set([MAGIC.KECCAK_256], hashPosition) // const entry2 = car.put({ // valid: false, // error: 'Invalid hash code', @@ -180,6 +194,8 @@ describe('jws.car', () => { // node: car.put(nodeKeccak), // original: car.put(jwt) // }) +// +// // contruct node with invalid signature // const jwtInvalid = jwt.substring(0, jwt.length - 10) + 'abc' + jwt.substring(jwt.length - 7) // const entry3 = car.put({ // valid: false, @@ -188,13 +204,22 @@ describe('jws.car', () => { // node: car.put(JWS.fromOriginal(jwtInvalid)), // original: car.put(jwtInvalid) // }) +// +// // construct node with missing alg // const invalidProtectedBytes = uint8arrays.fromString(JSON.stringify({})) // const invalidProtected = uint8arrays.toString(invalidProtectedBytes, 'base64url') // const jwtMissingAlg = invalidProtected + jwt.substring(jwt.indexOf('.')) // const nodeMissingAlg = klona(node) // const protectedLength = varintes.encode(invalidProtectedBytes.length)[0] // tape = new BytesTape(nodeKeccak._sig) -// tape.read(1); tape.readVarint(); tape.readVarint(); tape.readVarint(); +// tape.read(1); +// sigKind = tape.readVarint(); +// if (sigKind === MAGIC.SECP256K1) { +// // skip recovery bit for secp256k1 +// tape.read(1) +// } +// // skip hash and canon sigils +// tape.readVarint(); tape.readVarint(); // const protLenPos = tape.position // const klonLength = tape.readVarint() // const signature = nodeMissingAlg._sig.slice(tape.position + klonLength) @@ -204,6 +229,7 @@ describe('jws.car', () => { // invalidProtectedBytes, // signature // ]) +// console.log('nv', newVarsig) // nodeMissingAlg._sig = newVarsig // const entry4 = car.put({ // valid: false,