add Account
This commit is contained in:
parent
fd0dde5cd5
commit
2832e0f8f6
|
@ -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()
|
||||
})
|
||||
})
|
|
@ -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)
|
||||
}
|
||||
}
|
|
@ -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<string, Date>
|
||||
|
||||
|
@ -135,7 +134,10 @@ export class Client {
|
|||
}
|
||||
}
|
||||
|
||||
async sendMessage(message: any) {}
|
||||
createAccount = async (): Promise<Account> => {
|
||||
this.account = new Account()
|
||||
return this.account
|
||||
}
|
||||
}
|
||||
|
||||
export const createClient = async (options: ClientOptions) => {
|
||||
|
|
Loading…
Reference in New Issue