(opts?: { doNotComplete?: boolean; allowCache?: boolean })
| 229 | } |
| 230 | |
| 231 | async load(opts?: { doNotComplete?: boolean; allowCache?: boolean }) { |
| 232 | const { doNotComplete, allowCache } = opts || {}; |
| 233 | |
| 234 | let walletData: WalletData | EncryptionTypes.IEncrypted = allowCache ? this.#walletData : null; |
| 235 | if (!walletData) { |
| 236 | walletData = await this.storage.load(); |
| 237 | } |
| 238 | if ((walletData as EncryptionTypes.IEncrypted).ct) { |
| 239 | const password = await getPassword('Wallet decryption password:', { hidden: true }); |
| 240 | try { |
| 241 | walletData = JSON.parse(Encryption.decryptWithPassword(walletData as EncryptionTypes.IEncrypted, password).toString()); |
| 242 | this.isFullyEncrypted = true; |
| 243 | } catch { |
| 244 | Utils.die('Could not open wallet. Wrong password.'); |
| 245 | } |
| 246 | } |
| 247 | walletData = (walletData as WalletData); |
| 248 | |
| 249 | |
| 250 | const instantiateKey = (walletData: WalletData) => { |
| 251 | if (!walletData.key) { |
| 252 | return undefined; // read-only wallet |
| 253 | } |
| 254 | const obj = walletData.key.toObj ? walletData.key.toObj() : walletData.key; |
| 255 | if ((obj as TssKeyType).metadata) { |
| 256 | return new TssKey.TssKey(obj as TssKeyType); |
| 257 | } else { |
| 258 | obj.version = obj.version ?? 1; |
| 259 | return new Key({ seedType: 'object', seedData: obj }); |
| 260 | } |
| 261 | }; |
| 262 | |
| 263 | let key: KeyType; |
| 264 | try { |
| 265 | const imported = Client.upgradeCredentialsV1(walletData); |
| 266 | this.client.fromString(JSON.stringify(imported.credentials)); |
| 267 | |
| 268 | key = instantiateKey(walletData); |
| 269 | } catch { |
| 270 | try { |
| 271 | this.client.fromObj(walletData.credentials); |
| 272 | key = instantiateKey(walletData); |
| 273 | } catch (e) { |
| 274 | Utils.die('Corrupt wallet file:' + (_verbose && e.stack ? e.stack : e)); |
| 275 | } |
| 276 | } |
| 277 | |
| 278 | this.#walletData = { |
| 279 | key, |
| 280 | credentials: Credentials.fromObj(walletData.credentials) |
| 281 | } as WalletData; |
| 282 | |
| 283 | if (doNotComplete) return key; |
| 284 | |
| 285 | const status = await this.client.openWallet(); |
| 286 | let needsSave = status?.wallet?.status === 'complete'; |
| 287 | if ( |
| 288 | (!this.#walletData.credentials.isComplete() && this.client.credentials.isComplete()) || |
no test coverage detected