fix account restoration

This commit is contained in:
Pavel Prichodko 2022-10-09 18:26:11 -05:00
parent 11e6831201
commit 27ff048970
No known key found for this signature in database
GPG Key ID: 8E4C82D464215E83
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)