diff --git a/packages/status-js/src/account.test.ts b/packages/status-js/src/account.test.ts new file mode 100644 index 00000000..32c7bde2 --- /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 00000000..70903dfe --- /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 34f92ae9..cd4162ec 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) => {