39 lines
1.2 KiB
TypeScript
Raw Normal View History

2022-06-02 12:11:40 +02:00
import { keccak256 } from 'ethereum-cryptography/keccak'
2022-06-07 16:35:01 +02:00
import { getPublicKey, sign } from 'ethereum-cryptography/secp256k1'
import {
bytesToHex,
concatBytes,
hexToBytes,
} from 'ethereum-cryptography/utils'
2022-06-02 12:11:40 +02:00
export class Account {
public privateKey: string
public publicKey: string
2022-06-03 17:44:55 +02:00
public chatKey: string
2022-06-02 12:11:40 +02:00
constructor() {
const privateKey = utils.randomPrivateKey()
const publicKey = getPublicKey(privateKey)
2022-06-03 17:44:55 +02:00
const chatKey = getPublicKey(privateKey, true)
2022-06-02 12:11:40 +02:00
this.privateKey = bytesToHex(privateKey)
2022-06-04 13:09:49 +02:00
// this.publicKey = bytesToHex(publicKey)
this.publicKey =
'0x04ac419dac9a8bbb58825a3cde60eef0ee71b8cf6c63df611eeefc8e7aac7c79b55954b679d24cf5ec82da7ed921caf240628a9bfb3450c5111a9cffe54e631811'
// TODO?: add 0x prefix to public key
2022-06-03 17:44:55 +02:00
this.chatKey = bytesToHex(chatKey)
2022-06-02 12:11:40 +02:00
}
2022-06-07 16:35:01 +02:00
// sig must be a 65-byte compact ECDSA signature containing the recovery id as the last element.
sign = async (payload: Uint8Array) => {
2022-06-03 17:44:55 +02:00
const hash = keccak256(payload)
2022-06-07 16:35:01 +02:00
const [signature, recoverId] = await sign(hash, this.privateKey, {
recovered: true,
der: false,
})
return concatBytes(signature, new Uint8Array([recoverId]))
}
2022-06-02 12:11:40 +02:00
}