diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000..66d2c07 --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,3 @@ +{ + "deepscan.enable": true +} \ No newline at end of file diff --git a/config/_default.js b/config/_default.js index c1618d3..363f96d 100644 --- a/config/_default.js +++ b/config/_default.js @@ -19,6 +19,10 @@ module.exports = { guildID: "", // [string] sdk_url sdkURL: "", + // [string] The path to the main wallet file to back up + walletPath: "~/.lbryum/wallets/default_wallet", + // [string] The folder to store wallet backups after every deletion + walletBackupFolder: "~/.lbryum_backup/", // [string] Amount to auto-fund upon account creation startingBalance: "", // [Object] Eris client options (https://abal.moe/Eris/docs/Client) diff --git a/src/commands/admin/deposit.js b/src/commands/admin/deposit.js index 97c3b6f..aa7ed8b 100644 --- a/src/commands/admin/deposit.js +++ b/src/commands/admin/deposit.js @@ -1,4 +1,5 @@ const Command = require('../../structures/Command'); +const Util = require('../../util'); module.exports = class Deposit extends Command { get name() { return 'deposit'; } @@ -9,7 +10,8 @@ module.exports = class Deposit extends Command { }; } async exec(message) { - const response = await this.client.lbry.listAddresses(); + const account = await Util.LBRY.findSDKAccount(this.client, account => account.is_default); + const response = await this.client.lbry.listAddresses({ account_id: account.id }); const address = await response.json(); if (await this.handleResponse(message, response, address)) return; return message.channel.createMessage(`Address: ${address.result.items[0].address}`); diff --git a/src/structures/LBRY.js b/src/structures/LBRY.js index 1e873b1..f29d2e2 100644 --- a/src/structures/LBRY.js +++ b/src/structures/LBRY.js @@ -182,10 +182,13 @@ class LBRY { } /** - * List account addresses or details of single address. + * List account addresses or details. + * @param {object} options + * @param {string} options.to How many items should be per page + * @param {string} options.amount The amount to send */ - listAddresses() { - return this._sdkRequest('address_list', { page_size: 1 }); + listAddresses({ page_size = 1, account_id } = {}) { + return this._sdkRequest('address_list', { page_size, account_id }); } /** diff --git a/src/util.js b/src/util.js index fdcfe37..0b66f79 100644 --- a/src/util.js +++ b/src/util.js @@ -1,5 +1,7 @@ const fetch = require('node-fetch'); const config = require('config'); +const fs = require('fs'); +const path = require('path'); /** * Represents the utilities for the bot @@ -220,6 +222,11 @@ Util.Hastebin = { * @memberof Util. */ Util.LBRY = { + async findSDKAccount(client, fn) { + const response = await client.lbry.listAccounts({ page_size: await Util.LBRY.getAccountCount(client) }); + const accounts = await response.json(); + return accounts.result.items.find(fn); + }, async findOrCreateAccount(client, discordID, create = true) { // Check SQLite const pair = await client.sqlite.get(discordID); @@ -227,9 +234,7 @@ Util.LBRY = { return { accountID: pair.lbryID }; // Check accounts via SDK - const response = await client.lbry.listAccounts({ page_size: await Util.LBRY.getAccountCount(client) }); - const accounts = await response.json(); - const foundAccount = accounts.result.items.find(account => account.name === discordID); + const foundAccount = await Util.LBRY.findSDKAccount(client, account => account.name === discordID); if (foundAccount) { await client.sqlite.pair(discordID, foundAccount.id); return { accountID: foundAccount.id }; @@ -269,14 +274,26 @@ Util.LBRY = { return Number.isInteger(num) ? `${num}.0` : num.toString(); }, async deleteAccount(client, discordID, lbryID) { + // Backup the wallet before doing any delete function + try { + Util.LBRY.backupWallet(); + } catch (err) { + console.error('Error occurred while backing up wallet file!'); + console.error(err); + } + // Abandon supports await Util.LBRY.abandonAllClaims(client, lbryID); // Take out funds from account const balanceResponse = await client.lbry.accountBalance(lbryID); - const amount = (await balanceResponse.json()).result.total; - if (parseFloat(amount) > 0) + let amount = (await balanceResponse.json()).result.total; + while (amount >= 2) { await client.lbry.fundAccount({ from: lbryID, everything: true, amount }); + const finalBalance = await client.lbry.accountBalance(lbryID); + amount = (await finalBalance.json()).result.total; + await Util.halt(3000); + } // Remove account from SDK & SQLite await client.lbry.removeAccount(lbryID); @@ -293,7 +310,27 @@ Util.LBRY = { for (let i = 0, len = supports.length; i < len; i++) { const support = supports[i]; await client.lbry.abandonSupport({ claimID: support.claim_id, accountID: lbryID }); + await Util.halt(3000); } return { count: supports.length }; + }, + backupWallet() { + const wallet = fs.readFileSync(path.join(__dirname, config.walletPath)); + const d = new Date(); + const date = [ + d.getUTCFullYear(), + d.getUTCMonth().toString().padStart(2, '0'), + d.getUTCDay().toString().padStart(2, '0'), + ].join('-'); + const time = [ + d.getUTCHours().toString().padStart(2, '0'), + d.getUTCMinutes().toString().padStart(2, '0'), + d.getUTCSeconds().toString().padStart(2, '0'), + d.getUTCMilliseconds().toString() + ].join('-'); + const backupName = 'default_wallet.' + date + '_' + time + '.bak'; + const backupPath = path.join(__dirname, config.walletBackupFolder, backupName); + fs.writeFileSync(backupPath, wallet); + console.log(`Backed up wallet file: ${backupPath}`); } };