add recoverPublicKeyFromMetadata util

This commit is contained in:
Pavel Prichodko 2022-06-07 16:32:46 +02:00 committed by Felicio Mununga
parent 5de124efc7
commit 0699775423
No known key found for this signature in database
GPG Key ID: 0EB8D75C775AB6F1
3 changed files with 43 additions and 0 deletions

View File

@ -22,6 +22,7 @@ import { Account } from './account'
import { idToContentTopic } from './contentTopic' import { idToContentTopic } from './contentTopic'
import { createSymKeyFromPassword } from './encryption' import { createSymKeyFromPassword } from './encryption'
import { payloadToId } from './utils/payload-to-id' import { payloadToId } from './utils/payload-to-id'
import { recoverPublicKeyFromMetadata } from './utils/recover-public-key-from-metadata'
import { CommunityDescription } from './wire/community_description' import { CommunityDescription } from './wire/community_description'
import type { WakuMessage } from 'js-waku' import type { WakuMessage } from 'js-waku'
@ -246,6 +247,13 @@ class Community {
return return
} }
try {
const pk = recoverPublicKeyFromMetadata(decodedMetadata)
console.log('pk', pk)
} catch (err) {
console.error(err)
}
console.log('MESSAGE: DECODED METADATA') console.log('MESSAGE: DECODED METADATA')
let shouldUpdate = false let shouldUpdate = false

View File

@ -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({})
})
})

View File

@ -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)
}