update Account
This commit is contained in:
parent
341cd4714a
commit
26948d31d3
|
@ -8,10 +8,10 @@ describe('Account', () => {
|
||||||
it('should verify the signature', async () => {
|
it('should verify the signature', async () => {
|
||||||
const account = new Account()
|
const account = new Account()
|
||||||
|
|
||||||
const message = '123'
|
const message = utf8ToBytes('123')
|
||||||
const messageHash = keccak256(utf8ToBytes(message))
|
const messageHash = keccak256(message)
|
||||||
|
|
||||||
const signature = await account.sign(message)
|
const signature = await account.signMessage(message)
|
||||||
|
|
||||||
expect(secp.verify(signature, messageHash, account.publicKey)).toBeTruthy()
|
expect(secp.verify(signature, messageHash, account.publicKey)).toBeTruthy()
|
||||||
})
|
})
|
||||||
|
@ -19,10 +19,10 @@ describe('Account', () => {
|
||||||
it('should not verify signature with different message', async () => {
|
it('should not verify signature with different message', async () => {
|
||||||
const account = new Account()
|
const account = new Account()
|
||||||
|
|
||||||
const message = '123'
|
const message = utf8ToBytes('123')
|
||||||
const messageHash = keccak256(utf8ToBytes(message))
|
const messageHash = keccak256(message)
|
||||||
|
|
||||||
const signature = await account.sign('abc')
|
const signature = await account.signMessage(utf8ToBytes('abc'))
|
||||||
|
|
||||||
expect(secp.verify(signature, messageHash, account.publicKey)).toBeFalsy()
|
expect(secp.verify(signature, messageHash, account.publicKey)).toBeFalsy()
|
||||||
})
|
})
|
||||||
|
|
|
@ -1,25 +1,24 @@
|
||||||
import { keccak256 } from 'ethereum-cryptography/keccak'
|
import { keccak256 } from 'ethereum-cryptography/keccak'
|
||||||
import { getPublicKey, sign, utils } from 'ethereum-cryptography/secp256k1'
|
import { getPublicKey, sign, utils } from 'ethereum-cryptography/secp256k1'
|
||||||
import { bytesToHex, utf8ToBytes } from 'ethereum-cryptography/utils'
|
import { bytesToHex } from 'ethereum-cryptography/utils'
|
||||||
|
|
||||||
import { privateKeyToAddress } from './utils/private-key-to-address'
|
|
||||||
|
|
||||||
export class Account {
|
export class Account {
|
||||||
public privateKey: string
|
public privateKey: string
|
||||||
public publicKey: string
|
public publicKey: string
|
||||||
public address: string
|
public chatKey: string
|
||||||
|
|
||||||
constructor() {
|
constructor() {
|
||||||
const privateKey = utils.randomPrivateKey()
|
const privateKey = utils.randomPrivateKey()
|
||||||
const publicKey = getPublicKey(privateKey)
|
const publicKey = getPublicKey(privateKey)
|
||||||
|
const chatKey = getPublicKey(privateKey, true)
|
||||||
|
|
||||||
this.privateKey = bytesToHex(privateKey)
|
this.privateKey = bytesToHex(privateKey)
|
||||||
this.publicKey = bytesToHex(publicKey)
|
this.publicKey = bytesToHex(publicKey)
|
||||||
this.address = privateKeyToAddress(this.privateKey)
|
this.chatKey = bytesToHex(chatKey)
|
||||||
}
|
}
|
||||||
|
|
||||||
sign = (payload: string) => {
|
signMessage = (payload: Uint8Array) => {
|
||||||
const hash = keccak256(utf8ToBytes(payload))
|
const hash = keccak256(payload)
|
||||||
return sign(hash, this.privateKey)
|
return sign(hash, this.privateKey)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue