From 0699775423f7d0c6a1ac8cce0d714a696b2430b9 Mon Sep 17 00:00:00 2001 From: Pavel Prichodko <14926950+prichodko@users.noreply.github.com> Date: Tue, 7 Jun 2022 16:32:46 +0200 Subject: [PATCH] add recoverPublicKeyFromMetadata util --- packages/status-js/src/client.ts | 8 ++++++ .../recover-public-key-from-metadata.test.ts | 10 ++++++++ .../utils/recover-public-key-from-metadata.ts | 25 +++++++++++++++++++ 3 files changed, 43 insertions(+) create mode 100644 packages/status-js/src/utils/recover-public-key-from-metadata.test.ts create mode 100644 packages/status-js/src/utils/recover-public-key-from-metadata.ts diff --git a/packages/status-js/src/client.ts b/packages/status-js/src/client.ts index f09f2dc6..99de26f6 100644 --- a/packages/status-js/src/client.ts +++ b/packages/status-js/src/client.ts @@ -22,6 +22,7 @@ import { Account } from './account' import { idToContentTopic } from './contentTopic' import { createSymKeyFromPassword } from './encryption' import { payloadToId } from './utils/payload-to-id' +import { recoverPublicKeyFromMetadata } from './utils/recover-public-key-from-metadata' import { CommunityDescription } from './wire/community_description' import type { WakuMessage } from 'js-waku' @@ -246,6 +247,13 @@ class Community { return } + try { + const pk = recoverPublicKeyFromMetadata(decodedMetadata) + console.log('pk', pk) + } catch (err) { + console.error(err) + } + console.log('MESSAGE: DECODED METADATA') let shouldUpdate = false 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 new file mode 100644 index 00000000..e53edd2b --- /dev/null +++ b/packages/status-js/src/utils/recover-public-key-from-metadata.test.ts @@ -0,0 +1,10 @@ +import { recoverPublicKeyFromMetadata } from './recover-public-key-from-metadata' + +import type { ApplicationMetadataMessage } from '~/protos/application-metadata-message' + +describe('TODO: recoverPublicKeyFromMetadata', () => { + it('should recover public key', async () => { + const metadata: ApplicationMetadataMessage = {} + expect(recoverPublicKeyFromMetadata(metadata)).toEqual({}) + }) +}) 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 new file mode 100644 index 00000000..feff41fa --- /dev/null +++ b/packages/status-js/src/utils/recover-public-key-from-metadata.ts @@ -0,0 +1,25 @@ +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' + +/** + * returns the public key of the signer + * msg must be the 32-byte keccak hash of the message to be signed. + * sig must be a 65-byte compact ECDSA signature containing the recovery id as the last element. + */ +export function recoverPublicKeyFromMetadata( + metadata: ApplicationMetadataMessage +): string { + const signature = metadata.signature.slice(0, 64) + const recoveryId = metadata.signature.slice(-1) + + const pk = recoverPublicKey( + keccak256(metadata.payload), + signature, + Number(recoveryId) + ) + + return bytesToHex(pk) +}