From 3e72ef401f7f7e3ca7c6fa3294c4b92e5df59cdc Mon Sep 17 00:00:00 2001 From: Felicio Mununga Date: Wed, 8 Jun 2022 17:08:03 +0200 Subject: [PATCH] add test for public key recovery --- .../recover-public-key-from-metadata.test.ts | 39 +++++++++++++++++-- .../utils/recover-public-key-from-metadata.ts | 5 +-- 2 files changed, 38 insertions(+), 6 deletions(-) diff --git a/packages/status-js/src/utils/recover-public-key-from-metadata.test.ts b/packages/status-js/src/utils/recover-public-key-from-metadata.test.ts index e53edd2..fff5edf 100644 --- a/packages/status-js/src/utils/recover-public-key-from-metadata.test.ts +++ b/packages/status-js/src/utils/recover-public-key-from-metadata.test.ts @@ -1,10 +1,43 @@ import { recoverPublicKeyFromMetadata } from './recover-public-key-from-metadata' -import type { ApplicationMetadataMessage } from '~/protos/application-metadata-message' +import type { ApplicationMetadataMessage } from '../../protos/application-metadata-message' describe('TODO: recoverPublicKeyFromMetadata', () => { it('should recover public key', async () => { - const metadata: ApplicationMetadataMessage = {} - expect(recoverPublicKeyFromMetadata(metadata)).toEqual({}) + const metadataFixture = { + signature: new Uint8Array([ + 250, 132, 234, 119, 159, 124, 98, 93, 197, 108, 99, 52, 186, 234, 142, + 101, 147, 180, 50, 190, 102, 61, 219, 189, 95, 124, 29, 74, 43, 46, 106, + 108, 102, 234, 77, 209, 130, 140, 87, 96, 210, 34, 11, 115, 56, 98, 223, + 154, 30, 239, 23, 197, 243, 196, 248, 63, 162, 20, 108, 84, 250, 150, + 230, 129, 0, + ]), + payload: new Uint8Array([ + 8, 138, 245, 146, 158, 148, 48, 18, 104, 48, 120, 48, 50, 57, 102, 49, + 57, 54, 98, 98, 102, 101, 102, 52, 102, 97, 54, 97, 53, 101, 98, 56, 49, + 100, 100, 56, 48, 50, 49, 51, 51, 97, 54, 51, 52, 57, 56, 51, 50, 53, + 52, 52, 53, 99, 97, 49, 97, 102, 49, 100, 49, 53, 52, 98, 49, 98, 98, + 52, 53, 52, 50, 57, 53, 53, 49, 51, 51, 51, 48, 56, 48, 52, 101, 97, 55, + 45, 98, 100, 54, 54, 45, 52, 100, 53, 100, 45, 57, 49, 101, 98, 45, 98, + 50, 100, 99, 102, 101, 50, 53, 49, 53, 98, 51, 26, 66, 48, 120, 53, 97, + 57, 49, 99, 52, 54, 48, 97, 97, 100, 101, 99, 51, 99, 55, 54, 100, 48, + 56, 48, 98, 54, 99, 55, 50, 97, 50, 48, 101, 49, 53, 97, 51, 51, 55, + 102, 55, 99, 48, 98, 55, 55, 97, 55, 99, 48, 97, 53, 101, 98, 97, 53, + 102, 97, 57, 100, 52, 100, 57, 49, 98, 97, 56, 32, 5, 40, 2, + ]), + type: 'TYPE_EMOJI_REACTION', + } as unknown as ApplicationMetadataMessage + + const publicKeySnapshot = new Uint8Array([ + 4, 172, 65, 157, 172, 154, 139, 187, 88, 130, 90, 60, 222, 96, 238, 240, + 238, 113, 184, 207, 108, 99, 223, 97, 30, 238, 252, 142, 122, 172, 124, + 121, 181, 89, 84, 182, 121, 210, 76, 245, 236, 130, 218, 126, 217, 33, + 202, 242, 64, 98, 138, 155, 251, 52, 80, 197, 17, 26, 156, 255, 229, 78, + 99, 24, 17, + ]) + + const result = recoverPublicKeyFromMetadata(metadataFixture) + + expect(result).toEqual(publicKeySnapshot) }) }) diff --git a/packages/status-js/src/utils/recover-public-key-from-metadata.ts b/packages/status-js/src/utils/recover-public-key-from-metadata.ts index feff41f..ac860a1 100644 --- a/packages/status-js/src/utils/recover-public-key-from-metadata.ts +++ b/packages/status-js/src/utils/recover-public-key-from-metadata.ts @@ -1,6 +1,5 @@ import { keccak256 } from 'ethereum-cryptography/keccak' import { recoverPublicKey } from 'ethereum-cryptography/secp256k1' -import { bytesToHex } from 'ethereum-cryptography/utils' import type { ApplicationMetadataMessage } from '../../protos/application-metadata-message' @@ -11,7 +10,7 @@ import type { ApplicationMetadataMessage } from '../../protos/application-metada */ export function recoverPublicKeyFromMetadata( metadata: ApplicationMetadataMessage -): string { +): Uint8Array { const signature = metadata.signature.slice(0, 64) const recoveryId = metadata.signature.slice(-1) @@ -21,5 +20,5 @@ export function recoverPublicKeyFromMetadata( Number(recoveryId) ) - return bytesToHex(pk) + return pk }