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 publicKey: string
chatKey: string chatKey: string
username: string username: string
membership: MembershipStatus = 'none' membership: MembershipStatus
constructor(client: Client, initialPrivateKey?: string) { constructor(client: Client, initialAccount?: Account) {
this.#client = client this.#client = client
const privateKey = initialPrivateKey const privateKey = initialAccount
? hexToBytes(initialPrivateKey) ? hexToBytes(initialAccount.privateKey)
: utils.randomPrivateKey() : utils.randomPrivateKey()
const publicKey = getPublicKey(privateKey) const publicKey = getPublicKey(privateKey)
@ -35,6 +35,8 @@ export class Account {
this.publicKey = bytesToHex(publicKey) this.publicKey = bytesToHex(publicKey)
this.chatKey = '0x' + compressPublicKey(this.publicKey) this.chatKey = '0x' + compressPublicKey(this.publicKey)
this.username = generateUsername('0x' + 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. // 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) this.community = new Community(this, options.publicKey)
// Restore account if exists // Restore account if exists
const privateKey = this.storage.getItem<string>( const account = this.storage.getItem<Account>(THROWAWAY_ACCOUNT_STORAGE_KEY)
THROWAWAY_ACCOUNT_STORAGE_KEY if (account) {
) this.#account = new Account(this, account)
if (privateKey) {
this.#account = new Account(this, privateKey)
} }
} }
@ -143,10 +141,7 @@ class Client {
set account(account: Account | undefined) { set account(account: Account | undefined) {
this.#account = account this.#account = account
this.storage.setItem( this.storage.setItem(THROWAWAY_ACCOUNT_STORAGE_KEY, this.#account)
THROWAWAY_ACCOUNT_STORAGE_KEY,
this.#account?.privateKey
)
for (const callback of this.#accountCallbacks) { for (const callback of this.#accountCallbacks) {
callback(this.#account) callback(this.#account)