From bcb69c76d88ebc5d121a43fbf08afbad2acf4c46 Mon Sep 17 00:00:00 2001 From: Reza Ebrahimi Date: Tue, 31 May 2022 12:06:59 -0400 Subject: [PATCH 1/4] modification for mongo client bug added console log error inside loadWallet removed spaces modification for mongo client big in bitcore-client --- packages/bitcore-client/src/storage.ts | 6 +- packages/bitcore-client/src/storage/mongo.ts | 133 +++++++++++-------- 2 files changed, 81 insertions(+), 58 deletions(-) diff --git a/packages/bitcore-client/src/storage.ts b/packages/bitcore-client/src/storage.ts index 9c911109036..c732fe5943b 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); } } } diff --git a/packages/bitcore-client/src/storage/mongo.ts b/packages/bitcore-client/src/storage/mongo.ts index 8dc7bd2ed7b..73c95a2513c 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,13 +25,14 @@ 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) { @@ -45,7 +47,12 @@ export class Mongo { this.collection = this.db.collection(this.addressCollectionName); } await this.collection.createIndex({ name: 1 }); - } catch (error) {} + if (this.db) { + this.initialized = true; + } + } catch (error) { + console.error(error); + } } async close() { @@ -54,82 +61,94 @@ 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(); - } - }); - 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; + 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; + } } 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 +156,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); From 30955fe59800ae90a1a6d924f21b80862a7db1b9 Mon Sep 17 00:00:00 2001 From: Reza Ebrahimi Date: Mon, 6 Jun 2022 17:47:19 -0400 Subject: [PATCH 2/4] changes made for pipe method's error --- packages/bitcore-client/src/storage.ts | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/packages/bitcore-client/src/storage.ts b/packages/bitcore-client/src/storage.ts index c732fe5943b..8e2925a77af 100644 --- a/packages/bitcore-client/src/storage.ts +++ b/packages/bitcore-client/src/storage.ts @@ -77,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; } @@ -87,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; } From 0bcd836e3d0460aa25934eca1db06402e9818811 Mon Sep 17 00:00:00 2001 From: Reza Ebrahimi Date: Thu, 16 Jun 2022 11:11:49 -0400 Subject: [PATCH 3/4] new changes on mongo client connection function --- packages/bitcore-client/src/storage/mongo.ts | 66 +++++++++++--------- 1 file changed, 36 insertions(+), 30 deletions(-) diff --git a/packages/bitcore-client/src/storage/mongo.ts b/packages/bitcore-client/src/storage/mongo.ts index 73c95a2513c..2b4bffc60a7 100644 --- a/packages/bitcore-client/src/storage/mongo.ts +++ b/packages/bitcore-client/src/storage/mongo.ts @@ -36,23 +36,25 @@ export class Mongo { } 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); - } - await this.collection.createIndex({ name: 1 }); - if (this.db) { - this.initialized = true; + 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); } - } catch (error) { - console.error(error); - } + },2000); } async close() { @@ -60,20 +62,24 @@ export class Mongo { } async listWallets() { - 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; + 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; + } + } catch (e) { + console.error(e); } } From b15480c3653e3310c056ef0f3735ab1bb08510bc Mon Sep 17 00:00:00 2001 From: Reza Ebrahimi Date: Thu, 16 Jun 2022 11:15:58 -0400 Subject: [PATCH 4/4] added settimeout to init function --- packages/bitcore-client/src/storage/mongo.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/bitcore-client/src/storage/mongo.ts b/packages/bitcore-client/src/storage/mongo.ts index 2b4bffc60a7..547cddfa314 100644 --- a/packages/bitcore-client/src/storage/mongo.ts +++ b/packages/bitcore-client/src/storage/mongo.ts @@ -36,7 +36,7 @@ export class Mongo { } async init(params) { - setTimeout( async () => { + setTimeout(async () => { try { const { wallet, addresses } = params; this.client = new MongoClient(this.path, { useNewUrlParser: true, useUnifiedTopology: true }); @@ -54,7 +54,7 @@ export class Mongo { } catch (e) { console.error(e); } - },2000); + }, 2000); } async close() {