add recoverPublicKeyFromMetadata util
This commit is contained in:
parent
5de124efc7
commit
0699775423
|
@ -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
|
||||||
|
|
|
@ -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({})
|
||||||
|
})
|
||||||
|
})
|
|
@ -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)
|
||||||
|
}
|
Loading…
Reference in New Issue