bcoin: some code cleanup.

This commit is contained in:
Christopher Jeffrey 2017-10-19 01:48:09 -07:00
parent a1ec1c255e
commit 6bc8701f23
No known key found for this signature in database
GPG Key ID: 8962AB9DE6666BBD
6 changed files with 55 additions and 17 deletions

36
bin/cli
View File

@ -63,12 +63,14 @@ CLI.prototype.createWallet = async function createWallet() {
CLI.prototype.getMaster = async function getMaster() { CLI.prototype.getMaster = async function getMaster() {
const master = await this.wallet.getMaster(); const master = await this.wallet.getMaster();
this.log(master); this.log(master);
}; };
CLI.prototype.getKey = async function getKey() { CLI.prototype.getKey = async function getKey() {
const address = this.config.str(0); const address = this.config.str(0);
const key = await this.wallet.getKey(address); const key = await this.wallet.getKey(address);
this.log(key); this.log(key);
}; };
@ -76,40 +78,49 @@ CLI.prototype.getWIF = async function getWIF() {
const address = this.config.str(0); const address = this.config.str(0);
const passphrase = this.config.str('passphrase'); const passphrase = this.config.str('passphrase');
const key = await this.wallet.getWIF(address, passphrase); const key = await this.wallet.getWIF(address, passphrase);
if (!key) { if (!key) {
this.log('Key not found.'); this.log('Key not found.');
return; return;
} }
this.log(key.privateKey); this.log(key.privateKey);
}; };
CLI.prototype.addSharedKey = async function addSharedKey() { CLI.prototype.addSharedKey = async function addSharedKey() {
const key = this.config.str(0); const key = this.config.str(0);
const account = this.config.str('account'); const account = this.config.str('account');
await this.wallet.addSharedKey(account, key); await this.wallet.addSharedKey(account, key);
this.log('Added key.'); this.log('Added key.');
}; };
CLI.prototype.removeSharedKey = async function removeSharedKey() { CLI.prototype.removeSharedKey = async function removeSharedKey() {
const key = this.config.str(0); const key = this.config.str(0);
const account = this.config.str('account'); const account = this.config.str('account');
await this.wallet.removeSharedKey(account, key); await this.wallet.removeSharedKey(account, key);
this.log('Removed key.'); this.log('Removed key.');
}; };
CLI.prototype.getSharedKeys = async function getSharedKeys() { CLI.prototype.getSharedKeys = async function getSharedKeys() {
const acct = this.config.str([0, 'account']); const acct = this.config.str([0, 'account']);
const account = await this.wallet.getAccount(acct); const account = await this.wallet.getAccount(acct);
if (!account) { if (!account) {
this.log('Account not found.'); this.log('Account not found.');
return; return;
} }
this.log(account.keys); this.log(account.keys);
}; };
CLI.prototype.getAccount = async function getAccount() { CLI.prototype.getAccount = async function getAccount() {
const acct = this.config.str([0, 'account']); const acct = this.config.str([0, 'account']);
const account = await this.wallet.getAccount(acct); const account = await this.wallet.getAccount(acct);
this.log(account); this.log(account);
}; };
@ -132,18 +143,21 @@ CLI.prototype.createAccount = async function createAccount() {
CLI.prototype.createAddress = async function createAddress() { CLI.prototype.createAddress = async function createAddress() {
const account = this.config.str([0, 'account']); const account = this.config.str([0, 'account']);
const addr = await this.wallet.createAddress(account); const addr = await this.wallet.createAddress(account);
this.log(addr); this.log(addr);
}; };
CLI.prototype.createChange = async function createChange() { CLI.prototype.createChange = async function createChange() {
const account = this.config.str([0, 'account']); const account = this.config.str([0, 'account']);
const addr = await this.wallet.createChange(account); const addr = await this.wallet.createChange(account);
this.log(addr); this.log(addr);
}; };
CLI.prototype.createNested = async function createNested() { CLI.prototype.createNested = async function createNested() {
const account = this.config.str([0, 'account']); const account = this.config.str([0, 'account']);
const addr = await this.wallet.createNested(account); const addr = await this.wallet.createNested(account);
this.log(addr); this.log(addr);
}; };
@ -215,18 +229,21 @@ CLI.prototype.getCoin = async function getCoin() {
CLI.prototype.getWalletHistory = async function getWalletHistory() { CLI.prototype.getWalletHistory = async function getWalletHistory() {
const account = this.config.str('account'); const account = this.config.str('account');
const txs = await this.wallet.getHistory(account); const txs = await this.wallet.getHistory(account);
this.log(txs); this.log(txs);
}; };
CLI.prototype.getWalletPending = async function getWalletPending() { CLI.prototype.getWalletPending = async function getWalletPending() {
const account = this.config.str('account'); const account = this.config.str('account');
const txs = await this.wallet.getPending(account); const txs = await this.wallet.getPending(account);
this.log(txs); this.log(txs);
}; };
CLI.prototype.getWalletCoins = async function getWalletCoins() { CLI.prototype.getWalletCoins = async function getWalletCoins() {
const account = this.config.str('account'); const account = this.config.str('account');
const coins = await this.wallet.getCoins(account); const coins = await this.wallet.getCoins(account);
this.log(coins); this.log(coins);
}; };
@ -269,11 +286,13 @@ CLI.prototype.listenWallet = async function listenWallet() {
CLI.prototype.getBalance = async function getBalance() { CLI.prototype.getBalance = async function getBalance() {
const account = this.config.str('account'); const account = this.config.str('account');
const balance = await this.wallet.getBalance(account); const balance = await this.wallet.getBalance(account);
this.log(balance); this.log(balance);
}; };
CLI.prototype.getMempool = async function getMempool() { CLI.prototype.getMempool = async function getMempool() {
const txs = await this.client.getMempool(); const txs = await this.client.getMempool();
this.log(txs); this.log(txs);
}; };
@ -352,13 +371,16 @@ CLI.prototype.signTX = async function signTX() {
CLI.prototype.zapWallet = async function zapWallet() { CLI.prototype.zapWallet = async function zapWallet() {
const age = this.config.uint([0, 'age'], 72 * 60 * 60); const age = this.config.uint([0, 'age'], 72 * 60 * 60);
await this.wallet.zap(this.config.str('account'), age); await this.wallet.zap(this.config.str('account'), age);
this.log('Zapped!'); this.log('Zapped!');
}; };
CLI.prototype.broadcast = async function broadcast() { CLI.prototype.broadcast = async function broadcast() {
const raw = this.config.str([0, 'tx']); const raw = this.config.str([0, 'tx']);
const tx = await this.client.broadcast(raw); const tx = await this.client.broadcast(raw);
this.log('Broadcasted:'); this.log('Broadcasted:');
this.log(tx); this.log(tx);
}; };
@ -366,12 +388,14 @@ CLI.prototype.broadcast = async function broadcast() {
CLI.prototype.viewTX = async function viewTX() { CLI.prototype.viewTX = async function viewTX() {
const raw = this.config.str([0, 'tx']); const raw = this.config.str([0, 'tx']);
const tx = await this.wallet.fill(raw); const tx = await this.wallet.fill(raw);
this.log(tx); this.log(tx);
}; };
CLI.prototype.getDetails = async function getDetails() { CLI.prototype.getDetails = async function getDetails() {
const hash = this.config.str(0); const hash = this.config.str(0);
const details = await this.wallet.getTX(hash); const details = await this.wallet.getTX(hash);
this.log(details); this.log(details);
}; };
@ -383,18 +407,22 @@ CLI.prototype.getWalletBlocks = async function getWalletBlocks() {
CLI.prototype.getWalletBlock = async function getWalletBlock() { CLI.prototype.getWalletBlock = async function getWalletBlock() {
const height = this.config.uint(0); const height = this.config.uint(0);
const block = await this.wallet.getBlock(height); const block = await this.wallet.getBlock(height);
this.log(block); this.log(block);
}; };
CLI.prototype.retoken = async function retoken() { CLI.prototype.retoken = async function retoken() {
const passphrase = this.config.str('passphrase'); const passphrase = this.config.str('passphrase');
const result = await this.wallet.retoken(passphrase); const result = await this.wallet.retoken(passphrase);
this.log(result); this.log(result);
}; };
CLI.prototype.rescan = async function rescan() { CLI.prototype.rescan = async function rescan() {
const height = this.config.uint(0); const height = this.config.uint(0);
await this.client.rescan(height); await this.client.rescan(height);
this.log('Rescanning...'); this.log('Rescanning...');
}; };
@ -411,11 +439,13 @@ CLI.prototype.reset = async function reset() {
CLI.prototype.resend = async function resend() { CLI.prototype.resend = async function resend() {
await this.client.resend(); await this.client.resend();
this.log('Resending...'); this.log('Resending...');
}; };
CLI.prototype.resendWallet = async function resendWallet() { CLI.prototype.resendWallet = async function resendWallet() {
await this.wallet.resend(); await this.wallet.resend();
this.log('Resending...'); this.log('Resending...');
}; };
@ -431,6 +461,7 @@ CLI.prototype.importKey = async function importKey() {
const key = this.config.str(0); const key = this.config.str(0);
const account = this.config.str('account'); const account = this.config.str('account');
const passphrase = this.config.str('passphrase'); const passphrase = this.config.str('passphrase');
if (!key) if (!key)
throw new Error('No key for import.'); throw new Error('No key for import.');
@ -452,19 +483,24 @@ CLI.prototype.importKey = async function importKey() {
CLI.prototype.importAddress = async function importAddress() { CLI.prototype.importAddress = async function importAddress() {
const address = this.config.str(0); const address = this.config.str(0);
const account = this.config.str('account'); const account = this.config.str('account');
await this.wallet.importAddress(account, address); await this.wallet.importAddress(account, address);
this.log('Imported address.'); this.log('Imported address.');
}; };
CLI.prototype.lock = async function lock() { CLI.prototype.lock = async function lock() {
await this.wallet.lock(); await this.wallet.lock();
this.log('Locked.'); this.log('Locked.');
}; };
CLI.prototype.unlock = async function unlock() { CLI.prototype.unlock = async function unlock() {
const passphrase = this.config.str(0); const passphrase = this.config.str(0);
const timeout = this.config.uint(1); const timeout = this.config.uint(1);
await this.wallet.unlock(passphrase, timeout); await this.wallet.unlock(passphrase, timeout);
this.log('Unlocked.'); this.log('Unlocked.');
}; };

View File

@ -823,8 +823,8 @@ HTTPClient.prototype.importAddress = function importAddress(id, account, address
* @returns {Promise} * @returns {Promise}
*/ */
HTTPClient.prototype.lockCoin = function lockCoin(id, hash, index, passphrase) { HTTPClient.prototype.lockCoin = function lockCoin(id, hash, index) {
return this._put(`/wallet/${id}/locked/${hash}/${index}`, { passphrase }); return this._put(`/wallet/${id}/locked/${hash}/${index}`, {});
}; };
/** /**
@ -835,8 +835,8 @@ HTTPClient.prototype.lockCoin = function lockCoin(id, hash, index, passphrase) {
* @returns {Promise} * @returns {Promise}
*/ */
HTTPClient.prototype.unlockCoin = function unlockCoin(id, hash, index, passphrase) { HTTPClient.prototype.unlockCoin = function unlockCoin(id, hash, index) {
return this._del(`/wallet/${id}/locked/${hash}/${index}`, { passphrase }); return this._del(`/wallet/${id}/locked/${hash}/${index}`, {});
}; };
/** /**

View File

@ -279,7 +279,7 @@ HTTPServer.prototype.initRouter = function initRouter() {
} }
const height = await this.chain.getHeight(hash); const height = await this.chain.getHeight(hash);
res.send(200, block.getJSON(this.network, view, height, confirmations)); res.send(200, block.getJSON(this.network, view, height, confirmations));
}); });

View File

@ -360,7 +360,7 @@ HTTPWallet.prototype.retoken = async function retoken(passphrase) {
* @returns {Promise} * @returns {Promise}
*/ */
HTTPWallet.prototype.importPrivate = function importPrivate(account, key, passphrase='') { HTTPWallet.prototype.importPrivate = function importPrivate(account, key, passphrase) {
return this.client.importPrivate(this.id, account, key, passphrase); return this.client.importPrivate(this.id, account, key, passphrase);
}; };

View File

@ -151,16 +151,17 @@ TXMeta.prototype.toJSON = function toJSON() {
* @returns {Object} * @returns {Object}
*/ */
TXMeta.prototype.getJSON = function getJSON(network, view, chainheight) { TXMeta.prototype.getJSON = function getJSON(network, view, chainHeight) {
const json = this.tx.getJSON(network, view, null, this.index); const json = this.tx.getJSON(network, view, null, this.index);
json.mtime = this.mtime; json.mtime = this.mtime;
json.height = this.height; json.height = this.height;
json.block = this.block ? util.revHex(this.block) : null; json.block = this.block ? util.revHex(this.block) : null;
json.time = this.time; json.time = this.time;
if (this.block === null) json.confirmations = 0;
json.confirmations = 0;
else if (chainHeight != null)
json.confirmations = chainheight - this.height + 1; json.confirmations = chainHeight - this.height + 1;
return json; return json;
}; };

View File

@ -168,6 +168,7 @@ HTTPServer.prototype.initRouter = function initRouter() {
// Resend // Resend
this.post('/_admin/resend', async (req, res) => { this.post('/_admin/resend', async (req, res) => {
await this.walletdb.resend(); await this.walletdb.resend();
res.send(200, { success: true }); res.send(200, { success: true });
}); });
@ -641,7 +642,7 @@ HTTPServer.prototype.initRouter = function initRouter() {
// Locked coins // Locked coins
this.get('/:id/locked', async (req, res) => { this.get('/:id/locked', async (req, res) => {
const locked = await req.wallet.getLocked(); const locked = req.wallet.getLocked();
const result = []; const result = [];
for (const outpoint of locked) for (const outpoint of locked)
@ -660,7 +661,9 @@ HTTPServer.prototype.initRouter = function initRouter() {
enforce(index != null, 'Index is required.'); enforce(index != null, 'Index is required.');
const outpoint = new Outpoint(hash, index); const outpoint = new Outpoint(hash, index);
await req.wallet.lockCoin(outpoint);
req.wallet.lockCoin(outpoint);
res.send(200, { success: true }); res.send(200, { success: true });
}); });
@ -675,7 +678,8 @@ HTTPServer.prototype.initRouter = function initRouter() {
const outpoint = new Outpoint(hash, index); const outpoint = new Outpoint(hash, index);
await req.wallet.unlockCoin(outpoint); req.wallet.unlockCoin(outpoint);
res.send(200, { success: true }); res.send(200, { success: true });
}); });
@ -725,7 +729,6 @@ HTTPServer.prototype.initRouter = function initRouter() {
common.sortTX(txs); common.sortTX(txs);
const details = await req.wallet.toDetails(txs); const details = await req.wallet.toDetails(txs);
const result = []; const result = [];
for (const item of details) for (const item of details)
@ -747,9 +750,7 @@ HTTPServer.prototype.initRouter = function initRouter() {
}; };
const txs = await req.wallet.getRange(acct, options); const txs = await req.wallet.getRange(acct, options);
const details = await req.wallet.toDetails(txs); const details = await req.wallet.toDetails(txs);
const result = []; const result = [];
for (const item of details) for (const item of details)