fix account restoration

This commit is contained in:
Pavel Prichodko 2022-10-09 18:26:11 -05:00
parent b32fa3a77a
commit 9379dc6078
No known key found for this signature in database
GPG Key ID: 0EB8D75C775AB6F1
2 changed files with 10 additions and 13 deletions

View File

@ -21,13 +21,13 @@ export class Account {
publicKey: string
chatKey: string
username: string
membership: MembershipStatus = 'none'
membership: MembershipStatus
constructor(client: Client, initialPrivateKey?: string) {
constructor(client: Client, initialAccount?: Account) {
this.#client = client
const privateKey = initialPrivateKey
? hexToBytes(initialPrivateKey)
const privateKey = initialAccount
? hexToBytes(initialAccount.privateKey)
: utils.randomPrivateKey()
const publicKey = getPublicKey(privateKey)
@ -35,6 +35,8 @@ export class Account {
this.publicKey = bytesToHex(publicKey)
this.chatKey = '0x' + compressPublicKey(this.publicKey)
this.username = generateUsername('0x' + this.publicKey)
this.membership = initialAccount ? initialAccount.membership : 'none'
}
// sig must be a 65-byte compact ECDSA signature containing the recovery id as the last element.

View File

@ -79,11 +79,9 @@ class Client {
this.community = new Community(this, options.publicKey)
// Restore account if exists
const privateKey = this.storage.getItem<string>(
THROWAWAY_ACCOUNT_STORAGE_KEY
)
if (privateKey) {
this.#account = new Account(this, privateKey)
const account = this.storage.getItem<Account>(THROWAWAY_ACCOUNT_STORAGE_KEY)
if (account) {
this.#account = new Account(this, account)
}
}
@ -143,10 +141,7 @@ class Client {
set account(account: Account | undefined) {
this.#account = account
this.storage.setItem(
THROWAWAY_ACCOUNT_STORAGE_KEY,
this.#account?.privateKey
)
this.storage.setItem(THROWAWAY_ACCOUNT_STORAGE_KEY, this.#account)
for (const callback of this.#accountCallbacks) {
callback(this.#account)