Skip to content

Commit

Permalink
Test serialized payload against saved data
Browse files Browse the repository at this point in the history
  • Loading branch information
thesan committed Mar 14, 2024
1 parent 5524347 commit 608bd10
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 8 deletions.
Binary file not shown.
33 changes: 25 additions & 8 deletions packages/ui/test/common/utils/crypto.test.ts
Original file line number Diff line number Diff line change
@@ -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'
Expand All @@ -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', () => {
Expand All @@ -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)
})
Expand All @@ -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)))
}

0 comments on commit 608bd10

Please sign in to comment.