diff --git a/packages/bitcore-client/src/storage.ts b/packages/bitcore-client/src/storage.ts index 9c911109036..8e2925a77af 100644 --- a/packages/bitcore-client/src/storage.ts +++ b/packages/bitcore-client/src/storage.ts @@ -52,7 +52,9 @@ export class Storage { this.storageType = db; break; } - } catch (e) {} + } catch (e) { + console.error(e); + } } if (!wallet) { return; @@ -66,7 +68,7 @@ export class Storage { try { await db.deleteWallet({ name }); } catch (e) { - console.log(e); + console.error(e); } } } @@ -75,8 +77,10 @@ export class Storage { let passThrough = new PassThrough(); for (let db of this.db) { const listWalletStream = await db.listWallets(); - passThrough = listWalletStream.pipe(passThrough, { end: false }); - listWalletStream.once('end', () => this.db.length-- === 0 && passThrough.end()); + if (listWalletStream) { + passThrough = listWalletStream.pipe(passThrough, { end: false }); + listWalletStream.once('end', () => this.db.length-- === 0 && passThrough.end()); + } } return passThrough; } @@ -85,8 +89,10 @@ export class Storage { let passThrough = new PassThrough(); for (let db of this.db) { const listWalletStream = await db.listKeys(); - passThrough = listWalletStream.pipe(passThrough, { end: false }); - listWalletStream.once('end', () => --this.db.length === 0 && passThrough.end()); + if (listWalletStream) { + passThrough = listWalletStream.pipe(passThrough, { end: false }); + listWalletStream.once('end', () => --this.db.length === 0 && passThrough.end()); + } } return passThrough; } diff --git a/packages/bitcore-client/src/storage/mongo.ts b/packages/bitcore-client/src/storage/mongo.ts index 8dc7bd2ed7b..547cddfa314 100644 --- a/packages/bitcore-client/src/storage/mongo.ts +++ b/packages/bitcore-client/src/storage/mongo.ts @@ -14,6 +14,7 @@ export class Mongo { client: MongoClient; port: string; addressCollectionName: string; + initialized: boolean; constructor(params: { path?: string; createIfMissing: boolean; errorIfExists: boolean }) { const { path, createIfMissing, errorIfExists } = params; if (path) { @@ -24,28 +25,36 @@ export class Mongo { } this.databaseName = databasePath.pop(); } else { - this.path = 'mongodb://localhost/bitcoreWallet'; + this.path = 'mongodb://localhost:27017/bitcoreWallet'; this.databaseName = 'bitcoreWallets'; } this.createIfMissing = createIfMissing; this.errorIfExists = errorIfExists; this.collectionName = 'wallets'; this.addressCollectionName = 'walletaddresses'; + this.initialized = false; } async init(params) { - const { wallet, addresses } = params; - try { - this.client = new MongoClient(this.path, { useNewUrlParser: true, useUnifiedTopology: true }); - await this.client.connect(); - this.db = this.client.db(this.databaseName); - if (wallet) { - this.collection = this.db.collection(this.collectionName); - } else if (addresses) { - this.collection = this.db.collection(this.addressCollectionName); + setTimeout(async () => { + try { + const { wallet, addresses } = params; + this.client = new MongoClient(this.path, { useNewUrlParser: true, useUnifiedTopology: true }); + await this.client.connect(); + this.db = this.client.db(this.databaseName); + if (this.db) { + this.initialized = true; + if (wallet) { + this.collection = this.db.collection(this.collectionName); + } else if (addresses) { + this.collection = this.db.collection(this.addressCollectionName); + } + await this.collection.createIndex({ name: 1 }); + } + } catch (e) { + console.error(e); } - await this.collection.createIndex({ name: 1 }); - } catch (error) {} + }, 2000); } async close() { @@ -53,83 +62,99 @@ export class Mongo { } async listWallets() { - await this.init({ wallet: 1 }); - const stream = new Transform({ - objectMode: true, - transform(data, enc, next) { - this.push(JSON.stringify(data)); - next(); + try { + await this.init({ wallet: 1 }); + if (this.initialized) { + const stream = new Transform({ + objectMode: true, + transform(data, enc, next) { + this.push(JSON.stringify(data)); + next(); + } + }); + const cursor = this.collection + .find({ name: { $exists: true } }, { name: 1, chain: 1, network: 1, storageType: 1 }) + .pipe(stream); + stream.on('end', async () => await this.close()); + return cursor; } - }); - const cursor = this.collection - .find({ name: { $exists: true } }, { name: 1, chain: 1, network: 1, storageType: 1 }) - .pipe(stream); - stream.on('end', async () => await this.close()); - return cursor; + } catch (e) { + console.error(e); + } } async listKeys() { await this.init({ addresses: 1 }); - const stream = new Transform({ - objectMode: true, - transform(data, enc, next) { - this.push(JSON.parse(JSON.stringify(data))); - next(); - } - }); - const cursor = this.collection.find({}, { name: 1, key: 1, toStore: 1, storageType: 1 }).pipe(stream); - stream.on('end', async () => await this.close()); - return cursor; + if (this.initialized) { + const stream = new Transform({ + objectMode: true, + transform(data, enc, next) { + this.push(JSON.parse(JSON.stringify(data))); + next(); + } + }); + const cursor = this.collection.find({}, { name: 1, key: 1, toStore: 1, storageType: 1 }).pipe(stream); + stream.on('end', async () => await this.close()); + return cursor; + } } async saveWallet(params) { const { wallet } = params; await this.init({ wallet: 1 }); - if (wallet.lite) { - delete wallet.masterKey; - delete wallet.pubKey; - } - wallet.authKey = wallet.authKey.toString('hex'); - try { - delete wallet.storage; - delete wallet.client; - delete wallet._id; - await this.collection.updateOne({ name: wallet.name }, { $set: wallet }, { upsert: 1 }); - await this.close(); - } catch (error) { - console.error(error); + if (this.initialized) { + if (wallet.lite) { + delete wallet.masterKey; + delete wallet.pubKey; + } + wallet.authKey = wallet.authKey.toString('hex'); + try { + delete wallet.storage; + delete wallet.client; + delete wallet._id; + await this.collection.updateOne({ name: wallet.name }, { $set: wallet }, { upsert: 1 }); + await this.close(); + } catch (error) { + console.error(error); + } + return; } - return; } async loadWallet(params: { name: string }) { await this.init({ wallet: 1 }); - const { name } = params; - const wallet = await this.collection.findOne({ name }); - await this.close(); - if (!wallet) { - return; + if (this.initialized) { + const { name } = params; + const wallet = await this.collection.findOne({ name }); + await this.close(); + if (!wallet) { + return; + } + return JSON.stringify(wallet); } - return JSON.stringify(wallet); } async deleteWallet(params: { name: string }) { await this.init({ wallet: 1 }); - const { name } = params; - await this.collection.deleteOne({ name }); - await this.close(); + if (this.initialized) { + const { name } = params; + await this.collection.deleteOne({ name }); + await this.close(); + } } async getKey(params: { address: string; name: string; keepAlive: boolean; open: boolean }) { if (params.open) { await this.init({ addresses: 1 }); } - const { address, name } = params; - const key = await this.collection.findOne({ name, address }); - if (!params.keepAlive) { - await this.close(); + if (this.initialized) { + const { address, name } = params; + const key = await this.collection.findOne({ name, address }); + if (!params.keepAlive) { + await this.close(); + } + return key.data; } - return key.data; } async addKeys(params: { name: string; key: any; toStore: string; keepAlive: boolean; open: boolean }) { @@ -137,10 +162,12 @@ export class Mongo { if (params.open) { await this.init({ addresses: 1 }); } - const { name, key, toStore } = params; - await this.collection.insertOne({ name, address: key.address, data: toStore }); - if (!params.keepAlive) { - await this.close(); + if (this.initialized) { + const { name, key, toStore } = params; + await this.collection.insertOne({ name, address: key.address, data: toStore }); + if (!params.keepAlive) { + await this.close(); + } } } catch (error) { console.error(error);