add test for public key recovery

This commit is contained in:
Felicio Mununga 2022-06-08 17:08:03 +02:00
parent 4c1413cadf
commit d4c597fa0f
No known key found for this signature in database
GPG Key ID: 0EB8D75C775AB6F1
2 changed files with 38 additions and 6 deletions

View File

@ -1,10 +1,43 @@
import { recoverPublicKeyFromMetadata } from './recover-public-key-from-metadata' 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', () => { describe('TODO: recoverPublicKeyFromMetadata', () => {
it('should recover public key', async () => { it('should recover public key', async () => {
const metadata: ApplicationMetadataMessage = {} const metadataFixture = {
expect(recoverPublicKeyFromMetadata(metadata)).toEqual({}) 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)
}) })
}) })

View File

@ -1,6 +1,5 @@
import { keccak256 } from 'ethereum-cryptography/keccak' import { keccak256 } from 'ethereum-cryptography/keccak'
import { recoverPublicKey } from 'ethereum-cryptography/secp256k1' import { recoverPublicKey } from 'ethereum-cryptography/secp256k1'
import { bytesToHex } from 'ethereum-cryptography/utils'
import type { ApplicationMetadataMessage } from '../../protos/application-metadata-message' import type { ApplicationMetadataMessage } from '../../protos/application-metadata-message'
@ -11,7 +10,7 @@ import type { ApplicationMetadataMessage } from '../../protos/application-metada
*/ */
export function recoverPublicKeyFromMetadata( export function recoverPublicKeyFromMetadata(
metadata: ApplicationMetadataMessage metadata: ApplicationMetadataMessage
): string { ): Uint8Array {
const signature = metadata.signature.slice(0, 64) const signature = metadata.signature.slice(0, 64)
const recoveryId = metadata.signature.slice(-1) const recoveryId = metadata.signature.slice(-1)
@ -21,5 +20,5 @@ export function recoverPublicKeyFromMetadata(
Number(recoveryId) Number(recoveryId)
) )
return bytesToHex(pk) return pk
} }