MCPcopy Create free account
hub / github.com/bitpay/bitcore / load

Method load

packages/bitcore-cli/src/wallet.ts:231–300  ·  view source on GitHub ↗
(opts?: { doNotComplete?: boolean; allowCache?: boolean })

Source from the content-addressed store, hash-verified

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()) ||

Callers 3

getClientMethod · 0.95
createMethod · 0.95
createFromTssMethod · 0.95

Calls 11

saveMethod · 0.95
getPasswordFunction · 0.90
decryptWithPasswordMethod · 0.80
dieMethod · 0.80
upgradeCredentialsV1Method · 0.80
fromStringMethod · 0.80
openWalletMethod · 0.80
loadMethod · 0.65
isCompleteMethod · 0.65
toStringMethod · 0.45
fromObjMethod · 0.45

Tested by

no test coverage detected