Bitcoin - BitGO - Save all wallet objects into Array of Objects

0 votes

My Setup: With the help of the Mongoose module we handle all Mongo Database operations. For each new user, a wallet is generated and stored inside a Mongo collection.

METHOD: If we use User.find({}, function(err, docs) {. We can get each user object.

User.find({}, function(err, docs) { if (err) {console.log('Could not find Wallets in DB');} else { docs.forEach(function(address) { console.log(address); }); } }); // console.log(address); <== Result: { tokens: [], profile: { gender: '', location: '', website: '', picture: '' }, __v: 0, balance: 0, playerWallet: '2N3qPHp7bNaL4hiztrzZCzCywryKUzb5aLE', password: '$2a$10$JRs/hyiStyhcZzHBwFnhmulKX0vTqlNMTGdxJjDdCSD0Bufe7K6wC', email: '111111111111111111111@11111111111111111.com', username: '111111111111111111111', createdAt: Mon May 02 2016 01:38:11 GMT+0300 (EEST), updatedAt: Mon May 02 2016 01:38:17 GMT+0300 (EEST), _id: 57268553ea2d6bb50c9d4069 } 2N3qPHp7bNaL4hiztrzZCzCywryKUzb5aLE } { tokens: [], profile: { gender: '', location: '', website: '', picture: '' }, __v: 0, balance: 0, playerWallet: '2N2oG4nBJZU19Aks3MLvHqDKMXePmHhbEjC', password: '$2a$10$cCo5gQgqcRpQW9Dl/Q.48uJEQCaOpJOLY8LVpQxoQFZ6nUqQD8lEC', email: '2222222222222222222222222@222222222222222222.com', username: '2222222222222222222222222', createdAt: Mon May 02 2016 01:38:38 GMT+0300 (EEST), updatedAt: Mon May 02 2016 01:38:44 GMT+0300 (EEST), _id: 5726856eea2d6bb50c9d406a } 2N2oG4nBJZU19Aks3MLvHqDKMXePmHhbEjC { tokens: [], profile: { gender: '', location: '', website: '', picture: '' }, __v: 0, balance: 0, playerWallet: '2My1FGBG6erNXfzeGXg5DXnLk2PWxhpk9SL', password: '$2a$10$tL96HqPIAi6q8XnI3z4Bc.dX88fTYQXsTt.UFWJQn4k6ZdDFOZLe2', email: '33333333333333333333333@33333333333333333333333.com', username: '33333333333333333333333', createdAt: Mon May 02 2016 01:39:03 GMT+0300 (EEST), updatedAt: Mon May 02 2016 01:39:09 GMT+0300 (EEST), _id: 57268587ea2d6bb50c9d406b }


HERE LIES THE PROBLEM: Because I use a for loop to iterate all wallet addresses, I get 3 different calls and always get one object at a time. I never get more than 1 User object stored in my .JSON file.

var _24HourGame = ".24HourGame.json"; // Save Path var user = 'Super@User.com'; var loginPassword = 'SuperSecretPassword'; var otp = '0000000'; var bitgo = new BitGoJS.BitGo(); var listWallets = function() { User.find({}, function(err, docs) { if (err) {console.log('Could not find Wallets in DB');} else { docs.forEach(function(address) { console.log(address); var allWallets = []; allWallets.push(address.playerWallet); console.log(allWallets); // ALL ADDRESSEs EACH IN ITS OWN ARRAY //[ '2N3qPHp7bNaL4hiztrzZCzCywryKUzb5aLE' ] //[ '2N2oG4nBJZU19Aks3MLvHqDKMXePmHhbEjC' ] //[ '2My1FGBG6erNXfzeGXg5DXnLk2PWxhpk9SL' ] // Don't know how to use a better method that calls back recursively and sums all 3 obj received into an array.... for( var iteration in allWallets){ console.log(allWallets[iteration]); // '2N3qPHp7bNaL4hiztrzZCzCywryKUzb5aLE' // '2N2oG4nBJZU19Aks3MLvHqDKMXePmHhbEjC' // '2My1FGBG6erNXfzeGXg5DXnLk2PWxhpk9SL' // How to call a Recursive Callbacks or a Promise for bitgo.wallets().gest({}) while iterating multiple address? bitgo.wallets().get({ "id": allWallets[iteration] }, function callback(err, wallet) { if (err) { throw err; } console.log( address._id + ' ' + address.playerWallet + ' ' + 'BlockChain_Balance: ' + wallet.balance() / 1e8.toFixed(4) + ' DB_Balance: ' ); jsonfile.writeFileSync(_24HourGame, wallet, {spaces: 3}); }); } }); } }); }; // Authenticate first bitgo.authenticate({ username: user, password: loginPassword, otp: otp }, function(err, result) { if (err) { console.dir(err); throw new Error("Could not authenticate!"); } console.log("Unlocking account.." ); bitgo.unlock({ otp: otp }, function(err) { if (err) { console.dir(err); throw new Error("Could not unlock!"); } listWallets(); }); });



When I save the result to JSON I only get one object at a time:

{ "_id": "5726858f42929c5c066825b6cc7f9d96", "id": "2My1FGBG6erNXfzeGXg5DXnLk2PWxhpk9SL", "label": "123labeling", "isActive": true, "type": "safehd", "freeze": {}, "adminCount": 1, "disableTransactionNotifications": false, "private": { "keychains": [ { "xpub": "xpub661MyMwAqRbcG8zA12U9YnJU3fSxoCq857uMcFon6ViZdYMSeXbeVUGtGkBCasmE3SGdJVRr4ui1V8TzsmM7K6eKikSi9gZQiJXoVumGTnU", "path": "/0/0", "params": { "pubKey": "031ec65814f7e46e274a78671f6ed2b945c386e378160b19613ce8ba555e793635", "chainCode": "a046f6f6ea9fd06974b333e87a483c9c3cbdeb41c0e56c8f21a6954f9f95e387", "depth": 0, "index": 0, "parentFingerprint": 0 } }, { "xpub": "xpub6GiRC55CRvMEnS2CtwqrMNKDnpsogbX1tukjM2xWyW1PttVLSEVgDiJq7bWh6esAQWeQ9oGNR56RHqwzKLCzzneM6DGq91v8sA9DAJbUbtm", "path": "/0/0", "params": { "pubKey": "027f30812a8f33ffeb44515d0fe9cc99041c2369f407ecc537d492d34da48c29d2", "chainCode": "4f493067187019f78a773e3e559c699b98c0e82a53cbf76cf9ba5db8103062a0", "depth": 5, "index": 58402, "parentFingerprint": 2966462100 } }, { "xpub": "xpub661MyMwAqRbcF6cBQfmTER34dwShTVu6x2h24nxhHxMX3jAHCP9DJYuLcHABvaMeaBcSMdcte8mhAMnCcBCypK5iGe6H33aUH8JWbstSKP9", "path": "/0/0", "params": { "pubKey": "02ec33597217f00f1ad5689b0abbfd01d850c921d4560d1333f52f9f70eea2aafd", "chainCode": "37b746ccdf38587d6a943f8007ef9e3012f52c73079f53c094f446cd43f9bb4f", "depth": 0, "index": 0, "parentFingerprint": 0 } } ] }, "canSendInstant": true, "permissions": "admin,spend,view", "admin": { "users": [ { "user": "56f9e3b2cd8f2e5906530948fe1af2d2", "permissions": "admin,spend,view" } ] }, "tags": [], "approvalsRequired": 1, "spendingAccount": true, "pendingApprovals": [], "balance": 30000000, "instantBalance": 0, "spendableConfirmedBalance": 0, "confirmedBalance": 0, "spendableBalance": 0, "sent": 0, "received": 30000000, "unconfirmedSends": 0, "unconfirmedReceives": 30000000 }


I want to sum all objects into one array of objects...:

[ { "_id": "5726857642929c5c066825819a872925", "id": "2N2oG4nBJZU19Aks3MLvHqDKMXePmHhbEjC", "label": "123labeling", "isActive": true, "type": "safehd", "freeze": {}, "adminCount": 1, "disableTransactionNotifications": false, "private": { "keychains": [ { "xpub": "xpub661MyMwAqRbcH2AHKRsNyEv4euymBzY8P52NzeodZBBvG8zPtpLeEJmjD8xevZRrDLCyCr4TjmavB9TuAw69bDH3TkKNPyEQ2UBVrGg8uVq", "path": "/0/0", "params": { "pubKey": "038c20add130bf5d79fc1debc658e8866f5c69962efc15350941d18cc3b88c025f", "chainCode": "f8e61f519f8e5363b24fe19912a25c90395a9189f59517d5c888c2b2eaefe4ba", "depth": 0, "index": 0, "parentFingerprint": 0 } }, { "xpub": "xpub6GiRC55CRvMEkcMvj2xtGSMhQzAys7EMV4gvqporNz5kSkzuqt3x25cnpvYWyR24y38WYij7eLr3xSQsBo7TBHWR27b5QZTrj5sVq5MMBVa", "path": "/0/0", "params": { "pubKey": "037e26f5594705a05a49e63dbd7eb98e0fb56d0f26b584c8e4fcdb15efd0239d62", "chainCode": "984f19d3ae866a97a1b3a0bad1abf2a9aa77dbf50465180dee0cf8a8b73089c8", "depth": 5, "index": 58401, "parentFingerprint": 2966462100 } }, { "xpub": "xpub661MyMwAqRbcGdVpPbFpvi37Ziteqj4JY3uEoV6XijMAk62Pgs2jK3bRPPVjKg4AtwbZ6xiMMHD56oHCdGQwqtWNZgwSWefw9edex9aJ9KC", "path": "/0/0", "params": { "pubKey": "03b323e0b6ebb0cbb845be13c5a090c4648de18c090382e899e66c389c0f6451fc", "chainCode": "d1a6e364a4b44764190148e725a8c584583743d31df2d06439b70016e19799b5", "depth": 0, "index": 0, "parentFingerprint": 0 } } ] }, "canSendInstant": true, "permissions": "admin,spend,view", "admin": { "users": [ { "user": "56f9e3b2cd8f2e5906530948fe1af2d2", "permissions": "admin,spend,view" } ] }, "tags": [], "approvalsRequired": 1, "spendingAccount": true, "pendingApprovals": [], "balance": 533000000, "instantBalance": 0, "spendableConfirmedBalance": 0, "confirmedBalance": 0, "spendableBalance": 0, "sent": 0, "received": 533000000, "unconfirmedSends": 0, "unconfirmedReceives": 533000000 }, { "_id": "5726858f42929c5c066825b6cc7f9d96", "id": "2My1FGBG6erNXfzeGXg5DXnLk2PWxhpk9SL", "label": "123labeling", "isActive": true, "type": "safehd", "freeze": {}, "adminCount": 1, "disableTransactionNotifications": false, "private": { "keychains": [ { "xpub": "xpub661MyMwAqRbcG8zA12U9YnJU3fSxoCq857uMcFon6ViZdYMSeXbeVUGtGkBCasmE3SGdJVRr4ui1V8TzsmM7K6eKikSi9gZQiJXoVumGTnU", "path": "/0/0", "params": { "pubKey": "031ec65814f7e46e274a78671f6ed2b945c386e378160b19613ce8ba555e793635", "chainCode": "a046f6f6ea9fd06974b333e87a483c9c3cbdeb41c0e56c8f21a6954f9f95e387", "depth": 0, "index": 0, "parentFingerprint": 0 } }, { "xpub": "xpub6GiRC55CRvMEnS2CtwqrMNKDnpsogbX1tukjM2xWyW1PttVLSEVgDiJq7bWh6esAQWeQ9oGNR56RHqwzKLCzzneM6DGq91v8sA9DAJbUbtm", "path": "/0/0", "params": { "pubKey": "027f30812a8f33ffeb44515d0fe9cc99041c2369f407ecc537d492d34da48c29d2", "chainCode": "4f493067187019f78a773e3e559c699b98c0e82a53cbf76cf9ba5db8103062a0", "depth": 5, "index": 58402, "parentFingerprint": 2966462100 } }, { "xpub": "xpub661MyMwAqRbcF6cBQfmTER34dwShTVu6x2h24nxhHxMX3jAHCP9DJYuLcHABvaMeaBcSMdcte8mhAMnCcBCypK5iGe6H33aUH8JWbstSKP9", "path": "/0/0", "params": { "pubKey": "02ec33597217f00f1ad5689b0abbfd01d850c921d4560d1333f52f9f70eea2aafd", "chainCode": "37b746ccdf38587d6a943f8007ef9e3012f52c73079f53c094f446cd43f9bb4f", "depth": 0, "index": 0, "parentFingerprint": 0 } } ] }, "canSendInstant": true, "permissions": "admin,spend,view", "admin": { "users": [ { "user": "56f9e3b2cd8f2e5906530948fe1af2d2", "permissions": "admin,spend,view" } ] }, "tags": [], "approvalsRequired": 1, "spendingAccount": true, "pendingApprovals": [], "balance": 30000000, "instantBalance": 0, "spendableConfirmedBalance": 0, "confirmedBalance": 0, "spendableBalance": 0, "sent": 0, "received": 30000000, "unconfirmedSends": 0, "unconfirmedReceives": 30000000 }, { "_id": "5726855c0cb29c5b060918484524d6d9", "id": "2N3qPHp7bNaL4hiztrzZCzCywryKUzb5aLE", "label": "123labeling", "isActive": true, "type": "safehd", "freeze": {}, "adminCount": 1, "disableTransactionNotifications": false, "private": { "keychains": [ { "xpub": "xpub661MyMwAqRbcFcyTCTTNVmYcAmPJ2yRJanFPv9To7n1wk5cZmgUpzY16pAz9m6XriziHnk6c4Y2Hov9HJSmWPSHdmSLn9KNBLMr5Qi29HGN", "path": "/0/0", "params": { "pubKey": "032536e93aa4cd5feb0706d118b6ecf695d26544c00f92a2fb36ef9208ee216821", "chainCode": "6c4dbaed05b8a09d2d64c3688e3fb53f12e62ef64b70ad808a700830e2063bb2", "depth": 0, "index": 0, "parentFingerprint": 0 } }, { "xpub": "xpub6GiRC55CRvMEhQVtxhHwwpmG7NVyjT1SUuyYXfafsRDW4XoYRwzPp6Af5vY83hLN8vaFCzk9FdtUqdeXoQbS7fXNJU3eFYPnU1bfkjimsT4", "path": "/0/0", "params": { "pubKey": "035d12105eab700c637ca9fbbea066beca88c6603f90f98c07a9e5b84713d6ff20", "chainCode": "5670e6ede3379f33cc635981c7fbe9255c3f69a5e4868e5491e42bf3d5fb380a", "depth": 5, "index": 58400, "parentFingerprint": 2966462100 } }, { "xpub": "xpub661MyMwAqRbcEZBgr8PUbKfGBaQ98GFp2wGdR6vDwTAzruGenMnn3PqDo9MMmzSsF8dFWgUfUUV1Yor2ErVrohBNHj9Ug81ih7VAohfnEQu", "path": "/0/0", "params": { "pubKey": "024f7b2f85a3e0b859c4bdb975f9953973de4806300b2148afec66fb610af380a7", "chainCode": "014ce166076a6045fb5a88fa4a51052d13c5449a4367d030e0f6bc058a9d0191", "depth": 0, "index": 0, "parentFingerprint": 0 } } ] }, "canSendInstant": true, "permissions": "admin,spend,view", "admin": { "users": [ { "user": "56f9e3b2cd8f2e5906530948fe1af2d2", "permissions": "admin,spend,view" } ] }, "tags": [], "approvalsRequired": 1, "spendingAccount": true, "pendingApprovals": [], "balance": 330000000, "instantBalance": 0, "spendableConfirmedBalance": 0, "confirmedBalance": 0, "spendableBalance": 0, "sent": 0, "received": 330000000, "unconfirmedSends": 0, "unconfirmedReceives": 330000000 } ]


Hoping that someone has done something like this and can easily spot the error.

Apr 6, 2022 in Blockchain by Rahul
• 9,680 points
738 views

1 answer to this question.

0 votes

 After noticing your following code:

docs.forEach(function(address) { 
console.log(address); 
var allWallets = []; 
allWallets.push(address.playerWallet);

You have to define allWallets array in the loop and assign the current element which is the address and then in the next loop, you redefine the allWallets array as [] and push the next address into it. I guess that's one source of the problem. You might do it like
 

var allWallets = []; 
docs.forEach(function(address) { 
console.log(address); 
allWallets.push(address.playerWallet);

answered Apr 12, 2022 by Soham
• 9,710 points

Related Questions In Blockchain

0 votes
1 answer

How to handle Overlapping of array objects in node js

Try this code, may be it works for ...READ MORE

answered Sep 18, 2018 in Blockchain by digger
• 26,740 points
803 views
0 votes
1 answer

how loop through a JSON Array of bitcoin transaction data

for($i=0; $i<count($result['out']); $i++) { echo ...READ MORE

answered Sep 21, 2018 in Blockchain by slayer
• 29,370 points
744 views
0 votes
1 answer

How Bitcoin wallet find unspent transaction output(UTXOs)?

The wallet apps are connected to the ...READ MORE

answered May 22, 2018 in Blockchain by Shashank
• 10,400 points
1,287 views
0 votes
1 answer

Web3j v3.3.1 : Error while generating compiled solidity smart contracts which returns array of struct

Solidity does not support returning structs in ...READ MORE

answered Jun 19, 2018 in Blockchain by aryya
• 7,460 points
1,681 views
+1 vote
1 answer

Protocols used in a distributed/dlt system for the nodes to establish communication

yes all are over TCP/IP connections secured ...READ MORE

answered Aug 6, 2018 in Blockchain by aryya
• 7,460 points
1,449 views
0 votes
1 answer

Truffle tests not running after truffle init

This was a bug. They've fixed it. ...READ MORE

answered Sep 11, 2018 in Blockchain by Christine
• 15,790 points
1,936 views
0 votes
1 answer

Hyperledger Sawtooth vs Quorum in concurrency and speed Ask

Summary: Both should provide similar reliability of ...READ MORE

answered Sep 26, 2018 in IoT (Internet of Things) by Upasana
• 8,620 points
1,461 views
0 votes
1 answer

How can I import a Bitcoin paper wallet key into a bitcoinlib wallet?

Your code should work in the latest ...READ MORE

answered Apr 12, 2022 in Blockchain by Soham
• 9,710 points
1,391 views
0 votes
1 answer

Kotlin Convert Bitcoin value into Satoshi Value

java.lang.NumberFormatException: For input string: "45,586" This means ...READ MORE

answered Apr 12, 2022 in Blockchain by Soham
• 9,710 points
835 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP