From 2832e0f8f64c9d1db12f25c49cc30968c39c89c9 Mon Sep 17 00:00:00 2001 From: Pavel Prichodko <14926950+prichodko@users.noreply.github.com> Date: Thu, 2 Jun 2022 12:11:40 +0200 Subject: [PATCH] add Account --- packages/status-js/src/account.test.ts | 29 ++++++++++++++++++++++++++ packages/status-js/src/account.ts | 25 ++++++++++++++++++++++ packages/status-js/src/client-v2.ts | 10 +++++---- 3 files changed, 60 insertions(+), 4 deletions(-) create mode 100644 packages/status-js/src/account.test.ts create mode 100644 packages/status-js/src/account.ts diff --git a/packages/status-js/src/account.test.ts b/packages/status-js/src/account.test.ts new file mode 100644 index 0000000..32c7bde --- /dev/null +++ b/packages/status-js/src/account.test.ts @@ -0,0 +1,29 @@ +import { keccak256 } from 'ethereum-cryptography/keccak' +import * as secp from 'ethereum-cryptography/secp256k1' +import { utf8ToBytes } from 'ethereum-cryptography/utils' + +import { Account } from './account' + +describe('Account', () => { + it('should verify the signature', async () => { + const account = new Account() + + const message = '123' + const messageHash = keccak256(utf8ToBytes(message)) + + const signature = await account.sign(message) + + expect(secp.verify(signature, messageHash, account.publicKey)).toBeTruthy() + }) + + it('should not verify signature with different message', async () => { + const account = new Account() + + const message = '123' + const messageHash = keccak256(utf8ToBytes(message)) + + const signature = await account.sign('abc') + + expect(secp.verify(signature, messageHash, account.publicKey)).toBeFalsy() + }) +}) diff --git a/packages/status-js/src/account.ts b/packages/status-js/src/account.ts new file mode 100644 index 0000000..70903df --- /dev/null +++ b/packages/status-js/src/account.ts @@ -0,0 +1,25 @@ +import { keccak256 } from 'ethereum-cryptography/keccak' +import { getPublicKey, sign, utils } from 'ethereum-cryptography/secp256k1' +import { bytesToHex, utf8ToBytes } from 'ethereum-cryptography/utils' + +import { privateKeyToAddress } from './utils/private-key-to-address' + +export class Account { + public privateKey: string + public publicKey: string + public address: string + + constructor() { + const privateKey = utils.randomPrivateKey() + const publicKey = getPublicKey(privateKey) + + this.privateKey = bytesToHex(privateKey) + this.publicKey = bytesToHex(publicKey) + this.address = privateKeyToAddress(this.privateKey) + } + + sign = (payload: string) => { + const hash = keccak256(utf8ToBytes(payload)) + return sign(hash, this.privateKey) + } +} diff --git a/packages/status-js/src/client-v2.ts b/packages/status-js/src/client-v2.ts index 34f92ae..cd4162e 100644 --- a/packages/status-js/src/client-v2.ts +++ b/packages/status-js/src/client-v2.ts @@ -3,11 +3,9 @@ import { getPredefinedBootstrapNodes, Waku } from 'js-waku' import { ApplicationMetadataMessage } from '../protos/application-metadata-message' import { ChatMessage } from '../protos/chat-message' import { CommunityChat, CommunityDescription } from '../protos/communities' +import { Account } from './account' import { idToContentTopic } from './contentTopic' import { createSymKeyFromPassword } from './encryption' -import { hexToBuf } from './utils' - -import type { WakuMessage } from 'js-waku' export interface ClientOptions { publicKey: string @@ -20,6 +18,7 @@ export class Client { publicKey: string callback: (message: ChatMessage) => void waku?: Waku + account?: Account communityDescription?: CommunityDescription clocks: Record @@ -135,7 +134,10 @@ export class Client { } } - async sendMessage(message: any) {} + createAccount = async (): Promise => { + this.account = new Account() + return this.account + } } export const createClient = async (options: ClientOptions) => {