Skip to content

Commit

Permalink
bcoin: some code cleanup.
Browse files Browse the repository at this point in the history
  • Loading branch information
chjj committed Oct 19, 2017
1 parent a1ec1c2 commit 6bc8701
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 17 deletions.
36 changes: 36 additions & 0 deletions bin/cli
Original file line number Diff line number Diff line change
Expand Up @@ -63,53 +63,64 @@ CLI.prototype.createWallet = async function createWallet() {

CLI.prototype.getMaster = async function getMaster() {
const master = await this.wallet.getMaster();

this.log(master);
};

CLI.prototype.getKey = async function getKey() {
const address = this.config.str(0);
const key = await this.wallet.getKey(address);

this.log(key);
};

CLI.prototype.getWIF = async function getWIF() {
const address = this.config.str(0);
const passphrase = this.config.str('passphrase');
const key = await this.wallet.getWIF(address, passphrase);

if (!key) {
this.log('Key not found.');
return;
}

this.log(key.privateKey);
};

CLI.prototype.addSharedKey = async function addSharedKey() {
const key = this.config.str(0);
const account = this.config.str('account');

await this.wallet.addSharedKey(account, key);

this.log('Added key.');
};

CLI.prototype.removeSharedKey = async function removeSharedKey() {
const key = this.config.str(0);
const account = this.config.str('account');

await this.wallet.removeSharedKey(account, key);

this.log('Removed key.');
};

CLI.prototype.getSharedKeys = async function getSharedKeys() {
const acct = this.config.str([0, 'account']);
const account = await this.wallet.getAccount(acct);

if (!account) {
this.log('Account not found.');
return;
}

this.log(account.keys);
};

CLI.prototype.getAccount = async function getAccount() {
const acct = this.config.str([0, 'account']);
const account = await this.wallet.getAccount(acct);

this.log(account);
};

Expand All @@ -132,18 +143,21 @@ CLI.prototype.createAccount = async function createAccount() {
CLI.prototype.createAddress = async function createAddress() {
const account = this.config.str([0, 'account']);
const addr = await this.wallet.createAddress(account);

this.log(addr);
};

CLI.prototype.createChange = async function createChange() {
const account = this.config.str([0, 'account']);
const addr = await this.wallet.createChange(account);

this.log(addr);
};

CLI.prototype.createNested = async function createNested() {
const account = this.config.str([0, 'account']);
const addr = await this.wallet.createNested(account);

this.log(addr);
};

Expand Down Expand Up @@ -215,18 +229,21 @@ CLI.prototype.getCoin = async function getCoin() {
CLI.prototype.getWalletHistory = async function getWalletHistory() {
const account = this.config.str('account');
const txs = await this.wallet.getHistory(account);

this.log(txs);
};

CLI.prototype.getWalletPending = async function getWalletPending() {
const account = this.config.str('account');
const txs = await this.wallet.getPending(account);

this.log(txs);
};

CLI.prototype.getWalletCoins = async function getWalletCoins() {
const account = this.config.str('account');
const coins = await this.wallet.getCoins(account);

this.log(coins);
};

Expand Down Expand Up @@ -269,11 +286,13 @@ CLI.prototype.listenWallet = async function listenWallet() {
CLI.prototype.getBalance = async function getBalance() {
const account = this.config.str('account');
const balance = await this.wallet.getBalance(account);

this.log(balance);
};

CLI.prototype.getMempool = async function getMempool() {
const txs = await this.client.getMempool();

this.log(txs);
};

Expand Down Expand Up @@ -352,26 +371,31 @@ CLI.prototype.signTX = async function signTX() {

CLI.prototype.zapWallet = async function zapWallet() {
const age = this.config.uint([0, 'age'], 72 * 60 * 60);

await this.wallet.zap(this.config.str('account'), age);

this.log('Zapped!');
};

CLI.prototype.broadcast = async function broadcast() {
const raw = this.config.str([0, 'tx']);
const tx = await this.client.broadcast(raw);

this.log('Broadcasted:');
this.log(tx);
};

CLI.prototype.viewTX = async function viewTX() {
const raw = this.config.str([0, 'tx']);
const tx = await this.wallet.fill(raw);

this.log(tx);
};

CLI.prototype.getDetails = async function getDetails() {
const hash = this.config.str(0);
const details = await this.wallet.getTX(hash);

this.log(details);
};

Expand All @@ -383,18 +407,22 @@ CLI.prototype.getWalletBlocks = async function getWalletBlocks() {
CLI.prototype.getWalletBlock = async function getWalletBlock() {
const height = this.config.uint(0);
const block = await this.wallet.getBlock(height);

this.log(block);
};

CLI.prototype.retoken = async function retoken() {
const passphrase = this.config.str('passphrase');
const result = await this.wallet.retoken(passphrase);

this.log(result);
};

CLI.prototype.rescan = async function rescan() {
const height = this.config.uint(0);

await this.client.rescan(height);

this.log('Rescanning...');
};

Expand All @@ -411,11 +439,13 @@ CLI.prototype.reset = async function reset() {

CLI.prototype.resend = async function resend() {
await this.client.resend();

this.log('Resending...');
};

CLI.prototype.resendWallet = async function resendWallet() {
await this.wallet.resend();

this.log('Resending...');
};

Expand All @@ -431,6 +461,7 @@ CLI.prototype.importKey = async function importKey() {
const key = this.config.str(0);
const account = this.config.str('account');
const passphrase = this.config.str('passphrase');

if (!key)
throw new Error('No key for import.');

Expand All @@ -452,19 +483,24 @@ CLI.prototype.importKey = async function importKey() {
CLI.prototype.importAddress = async function importAddress() {
const address = this.config.str(0);
const account = this.config.str('account');

await this.wallet.importAddress(account, address);

this.log('Imported address.');
};

CLI.prototype.lock = async function lock() {
await this.wallet.lock();

this.log('Locked.');
};

CLI.prototype.unlock = async function unlock() {
const passphrase = this.config.str(0);
const timeout = this.config.uint(1);

await this.wallet.unlock(passphrase, timeout);

this.log('Unlocked.');
};

Expand Down
8 changes: 4 additions & 4 deletions lib/http/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -823,8 +823,8 @@ HTTPClient.prototype.importAddress = function importAddress(id, account, address
* @returns {Promise}
*/

HTTPClient.prototype.lockCoin = function lockCoin(id, hash, index, passphrase) {
return this._put(`/wallet/${id}/locked/${hash}/${index}`, { passphrase });
HTTPClient.prototype.lockCoin = function lockCoin(id, hash, index) {
return this._put(`/wallet/${id}/locked/${hash}/${index}`, {});
};

/**
Expand All @@ -835,8 +835,8 @@ HTTPClient.prototype.lockCoin = function lockCoin(id, hash, index, passphrase) {
* @returns {Promise}
*/

HTTPClient.prototype.unlockCoin = function unlockCoin(id, hash, index, passphrase) {
return this._del(`/wallet/${id}/locked/${hash}/${index}`, { passphrase });
HTTPClient.prototype.unlockCoin = function unlockCoin(id, hash, index) {
return this._del(`/wallet/${id}/locked/${hash}/${index}`, {});
};

/**
Expand Down
2 changes: 1 addition & 1 deletion lib/http/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -279,7 +279,7 @@ HTTPServer.prototype.initRouter = function initRouter() {
}

const height = await this.chain.getHeight(hash);

res.send(200, block.getJSON(this.network, view, height, confirmations));
});

Expand Down
2 changes: 1 addition & 1 deletion lib/http/wallet.js
Original file line number Diff line number Diff line change
Expand Up @@ -360,7 +360,7 @@ HTTPWallet.prototype.retoken = async function retoken(passphrase) {
* @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);
};

Expand Down
11 changes: 6 additions & 5 deletions lib/primitives/txmeta.js
Original file line number Diff line number Diff line change
Expand Up @@ -151,16 +151,17 @@ TXMeta.prototype.toJSON = function toJSON() {
* @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);
json.mtime = this.mtime;
json.height = this.height;
json.block = this.block ? util.revHex(this.block) : null;
json.time = this.time;
if (this.block === null)
json.confirmations = 0;
else
json.confirmations = chainheight - this.height + 1;
json.confirmations = 0;

if (chainHeight != null)
json.confirmations = chainHeight - this.height + 1;

return json;
};

Expand Down
13 changes: 7 additions & 6 deletions lib/wallet/http.js
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,7 @@ HTTPServer.prototype.initRouter = function initRouter() {
// Resend
this.post('/_admin/resend', async (req, res) => {
await this.walletdb.resend();

res.send(200, { success: true });
});

Expand Down Expand Up @@ -641,7 +642,7 @@ HTTPServer.prototype.initRouter = function initRouter() {

// Locked coins
this.get('/:id/locked', async (req, res) => {
const locked = await req.wallet.getLocked();
const locked = req.wallet.getLocked();
const result = [];

for (const outpoint of locked)
Expand All @@ -660,7 +661,9 @@ HTTPServer.prototype.initRouter = function initRouter() {
enforce(index != null, 'Index is required.');

const outpoint = new Outpoint(hash, index);
await req.wallet.lockCoin(outpoint);

req.wallet.lockCoin(outpoint);

res.send(200, { success: true });
});

Expand All @@ -675,7 +678,8 @@ HTTPServer.prototype.initRouter = function initRouter() {

const outpoint = new Outpoint(hash, index);

await req.wallet.unlockCoin(outpoint);
req.wallet.unlockCoin(outpoint);

res.send(200, { success: true });
});

Expand Down Expand Up @@ -725,7 +729,6 @@ HTTPServer.prototype.initRouter = function initRouter() {
common.sortTX(txs);

const details = await req.wallet.toDetails(txs);

const result = [];

for (const item of details)
Expand All @@ -747,9 +750,7 @@ HTTPServer.prototype.initRouter = function initRouter() {
};

const txs = await req.wallet.getRange(acct, options);

const details = await req.wallet.toDetails(txs);

const result = [];

for (const item of details)
Expand Down

0 comments on commit 6bc8701

Please sign in to comment.