From 608bd10509889f90106a004886d083904a39a1a8 Mon Sep 17 00:00:00 2001 From: Theophile Sandoz Date: Thu, 14 Mar 2024 17:09:38 +0100 Subject: [PATCH] Test serialized payload against saved data --- .../proposals/SerializedChannelPayouts.bin | Bin 0 -> 3569 bytes packages/ui/test/common/utils/crypto.test.ts | 33 +++++++++++++----- 2 files changed, 25 insertions(+), 8 deletions(-) create mode 100644 packages/ui/test/_mocks/proposals/SerializedChannelPayouts.bin diff --git a/packages/ui/test/_mocks/proposals/SerializedChannelPayouts.bin b/packages/ui/test/_mocks/proposals/SerializedChannelPayouts.bin new file mode 100644 index 0000000000000000000000000000000000000000..c78b30e9991da9b8117240d35742a0c6730fdccd GIT binary patch literal 3569 zcmds4JB}Pj4AttpT3E)8LIF+`8%D9n`Y==A6Zi(KB6$QK#*q(Uz?tEjIC194H}Gq< zASaV)17Qo8>Ed)xP1WP$dz`!9Z(jfV=E=u#`SrX$`R)#r_usy`!R7MDrwu_= zcMQbOaJl?)ykj7Kj?3lt#mDf7kJ;ttm!IQfcKQ3&=lGaietP{mzCGeUj~~at_m4Z5 zAHMk*9`SMKa{p-Z$^Z;n;G?`SN&wKY@fs?yfL!*f-RIo$>xUgSg^QYxzkSvOPN zyB6B#DzdicnA%#@996k**-SyTZ>mad(-;!9PD*RLd2Vd5W-4y*@^h8SeWX|{tYDPQ zwhzK~gL)m?u~?O%WVQIB2=)+JmKl`_yVd)f_a7$P!cUeOTnkRM7gw>SqA@xaa?U+8 zw9FDA#*5cVo{T-l@LmtJpI$**n~iAGnrvffd-D?QRt+a=u1id3EoBs^rZgE-Ydwn= z6{a5dPaY%ML^z~tc-B}m7+kXw1v#ro1d^=Yvu)(eLxsA4OKItgwe>;eK>OJhv=?t@ zQE%#D?%B(-L9-=yNs1+Vfb46Oux8|4C6?17^$MzLJNY!ca+|c;RE`}qY+zrzx@zHB zYT-`3*S00nd4ZtGt28GsIeYFiXI~GurJ2@Rv*|9o=-OrFY(7!gG*)!XV;U;jR+&jm zC0S5}P=+jh@`3jAD`@xL3kx2AV)2R6#11J1 z@><6fFGY&DBn)8>!I(8-w+^GpGxHCj+Yg~6FR!3Ie9AOf(QdMcAqWLN3UB}=^oin7 zj?N6x@HLuMZwqDQhgogN&w0YfhW0}!$*U`917%*d5}2*MLx~iE8x0Zy)!$xia8VQA zFyg)2@yUU)Zgb z6Q{?9_7^M3UpKd}ub>^#p{S9jgJG*@~ literal 0 HcmV?d00001 diff --git a/packages/ui/test/common/utils/crypto.test.ts b/packages/ui/test/common/utils/crypto.test.ts index 2908a350bf..01d306cb2e 100644 --- a/packages/ui/test/common/utils/crypto.test.ts +++ b/packages/ui/test/common/utils/crypto.test.ts @@ -1,3 +1,5 @@ +import { readFileSync } from 'fs' + import { generateJsonPayloadFromPayoutsVector, generateSerializedPayload } from '@joystream/js/content' import { blake3 } from '@noble/hashes/blake3' import { createTestKeyring } from '@polkadot/keyring' @@ -9,17 +11,28 @@ import { accountsMap } from '../../../dev/node-mocks/data/addresses' import ChannelPayoutsVector from '../../_mocks/proposals/ChannelPayoutsVector.json' describe('Utils: Crypto', () => { + const expectSerializedPayload = new Uint8Array(readFileSync('test/_mocks/proposals/SerializedChannelPayouts.bin')) + const expectedCommitment = '0xbefab4c53ab253d6d5b160ee75856304d67442f8bcd84dc7cbedd0ed613d750f' + const [commitment, channelPayouts] = generateJsonPayloadFromPayoutsVector(ChannelPayoutsVector) - const serializedPayload = generateSerializedPayload(channelPayouts) - const file = new Blob([serializedPayload]) + const generatedSerializedPayload = generateSerializedPayload(channelPayouts) + const fileFromExpectedPayload = new Blob([expectSerializedPayload]) + const fileFromGeneratedPayload = new Blob([generatedSerializedPayload]) it('Merkle root from binary file', async () => { - expect(await merkleRootFromBinary(file)).toBe(commitment) + expect(await merkleRootFromBinary(fileFromGeneratedPayload)).toBe(commitment) + expect(await merkleRootFromBinary(fileFromExpectedPayload)).toBe(commitment) + expect(expectedCommitment).toBe(commitment) + }) + + it('serializedPayload', () => { + expect(stringifyU8A(generatedSerializedPayload)).toBe(stringifyU8A(expectSerializedPayload)) }) it('File hash', async () => { expect(await hashFile(new Blob(['foo']))).toBe('gVwzhfDKQjym61HfkEEQr1tZtNH6Lwk52eziQLVdmRriit') - expect(await hashFile(file)).toBe('gW22Sg9hMpHzog1XGwPAM7pz4As1NHDKuRoQvUpDybR6W5') + expect(await hashFile(fileFromExpectedPayload)).toBe('gW22Sg9hMpHzog1XGwPAM7pz4As1NHDKuRoQvUpDybR6W5') + expect(await hashFile(fileFromGeneratedPayload)).toBe('gW22Sg9hMpHzog1XGwPAM7pz4As1NHDKuRoQvUpDybR6W5') }) it('Blake3', () => { @@ -37,14 +50,14 @@ describe('Utils: Crypto', () => { }) it('Polkadot signature', () => { - const savedSignature = - 'e4 ae d1 a8 24 b1 fb f2 4f 36 4b 04 99 8d 53 e3 fb d1 6f 12 13 c7 89 a4 53 67 25 9c 45 a2 59 07 0e 32 1a ee 46 d5 4a f0 f2 30 db 64 4a b3 cb e4 cc c5 2c 37 eb 16 ad 80 58 bc a1 2e 13 d5 49 81' - const saved = new Uint8Array(savedSignature.split(' ').map((byte) => parseInt(byte, 16))) - const keyring = createTestKeyring() const keyPair = keyring.getPair(accountsMap.alice) const generated = keyPair.sign('foo') + const saved = parseU8A( + 'e4 ae d1 a8 24 b1 fb f2 4f 36 4b 04 99 8d 53 e3 fb d1 6f 12 13 c7 89 a4 53 67 25 9c 45 a2 59 07 0e 32 1a ee 46 d5 4a f0 f2 30 db 64 4a b3 cb e4 cc c5 2c 37 eb 16 ad 80 58 bc a1 2e 13 d5 49 81' + ) + expect(keyPair.verify('foo', generated, keyPair.publicKey)).toBe(true) expect(keyPair.verify('bar', saved, keyPair.publicKey)).toBe(true) }) @@ -55,3 +68,7 @@ function stringifyU8A(bytes: Uint8Array): string { .map((byte) => byte.toString(16).padStart(2, '0')) .join(' ') } + +function parseU8A(str: string): Uint8Array { + return new Uint8Array(str.split(' ').map((byte) => parseInt(byte, 16))) +}