diff --git a/bin/cli b/bin/cli index 93b4e269..20631252 100755 --- a/bin/cli +++ b/bin/cli @@ -103,6 +103,17 @@ CLI.prototype.createAccount = function createAccount(callback) { }); }; +CLI.prototype.createAddress = function createAddress(callback) { + var self = this; + var account = this.argv[0]; + this.wallet.createAddress(account, function(err, account) { + if (err) + return callback(err); + self.log(account); + callback(); + }); +}; + CLI.prototype.getAccounts = function getAccounts(callback) { var self = this; this.wallet.getAccounts(function(err, accounts) { @@ -362,6 +373,16 @@ CLI.prototype.getDetails = function getDetails(callback) { }); }; +CLI.prototype.retoken = function retoken(callback) { + var self = this; + this.wallet.retoken(function(err, result) { + if (err) + return callback(err); + self.log(result); + callback(); + }); +}; + CLI.prototype.rpc = function rpc(callback) { var self = this; var method = this.argv.shift(); @@ -418,13 +439,21 @@ CLI.prototype.handleWallet = function handleWallet(callback) { case 'history': return self.getWalletHistory(callback); case 'account': + if (self.argv[0] === 'list') { + self.argv.shift(); + return self.getAccounts(callback); + } if (self.argv[0] === 'create') { self.argv.shift(); return self.createAccount(callback); } + if (self.argv[0] === 'get') + self.argv.shift(); return self.getAccount(callback); - case 'accounts': - return self.getAccounts(callback); + case 'address': + return self.createAddress(callback); + case 'retoken': + return self.retoken(callback); case 'sign': return self.signTX(callback); case 'mktx': @@ -440,30 +469,26 @@ CLI.prototype.handleWallet = function handleWallet(callback) { default: self.log('Unrecognized command.'); self.log('Commands:'); - self.log(' $ wallet [id] --keys [hdkeys]' - + ' --type [pubkeyhash/multisig] -m [m-value]' - + ' -n [n-value] --witness: View or create wallet by ID.'); - self.log(' $ listen [id]: Listen for wallet events.'); - self.log(' $ getwallet [id]: View wallet by ID.'); - self.log(' $ addkey [id] --keys [hdkeys]: Add keys to wallet.'); - self.log(' $ rmkey [id] --keys [hdkeys]: Remove keys from wallet.'); - self.log(' $ balance [id]: Get wallet balance.'); - self.log(' $ history [id]: View wallet TX history.'); - self.log(' $ accounts [id]: List account names.'); - self.log(' $ account [id] [acct]: Get account details.'); - self.log(' $ send [id] [address] [value] --script [code]: Send transaction.'); - self.log(' $ create [id] [address] [value] --script [code]: Create transaction.'); - self.log(' $ sign [id] [tx-hex]: Sign transaction.'); - self.log(' $ zap [id] --age [age]: Zap pending wallet TXs.'); - self.log(' $ broadcast [tx-hex]: Broadcast transaction.'); - self.log(' $ view [tx-hex]: View transaction.'); - self.log(' $ mempool: Get mempool snapshot.'); - self.log(' $ tx [hash/address]: View transactions.'); - self.log(' $ coin [hash+index/address]: View coins.'); - self.log(' $ block [hash/height]: View block.'); + self.log(' $ listen: Listen for events.'); + self.log(' $ get: View wallet.'); + self.log(' $ addkey [xpubkey]: Add key to wallet.'); + self.log(' $ rmkey [xpubkey]: Remove key from wallet.'); + self.log(' $ balance: Get wallet balance.'); + self.log(' $ history: View wallet TX history.'); + self.log(' $ account list: List account names.'); + self.log(' $ account create [account-name]: Create account.'); + self.log(' $ account get [account-name]: Get account details.'); + self.log(' $ address: Derive new address.'); + self.log(' $ retoken: Create new api key.'); + self.log(' $ send [address] [value]: Send transaction.'); + self.log(' $ mktx [address] [value]: Create transaction.'); + self.log(' $ sign [tx-hex]: Sign transaction.'); + self.log(' $ zap --age [age]: Zap pending wallet TXs.'); + self.log(' $ tx [hash]: View transaction details.'); + self.log(' $ view [tx-hex]: Parse and view transaction.'); self.log('Other Options:'); self.log(' --passphrase [passphrase]: For signing and account creation.'); - self.log(' --account [acctname]: Account name.'); + self.log(' --account [account-name]: Account name.'); return callback(); } }); @@ -500,30 +525,12 @@ CLI.prototype.handleNode = function handleNode(callback) { default: self.log('Unrecognized command.'); self.log('Commands:'); - self.log(' $ wallet [id] --keys [hdkeys]' - + ' --type [pubkeyhash/multisig] -m [m-value]' - + ' -n [n-value] --witness: View or create wallet by ID.'); - self.log(' $ listen [id]: Listen for wallet events.'); - self.log(' $ getwallet [id]: View wallet by ID.'); - self.log(' $ addkey [id] --keys [hdkeys]: Add keys to wallet.'); - self.log(' $ rmkey [id] --keys [hdkeys]: Remove keys from wallet.'); - self.log(' $ balance [id]: Get wallet balance.'); - self.log(' $ history [id]: View wallet TX history.'); - self.log(' $ accounts [id]: List account names.'); - self.log(' $ account [id] [acct]: Get account details.'); - self.log(' $ send [id] [address] [value] --script [code]: Send transaction.'); - self.log(' $ create [id] [address] [value] --script [code]: Create transaction.'); - self.log(' $ sign [id] [tx-hex]: Sign transaction.'); - self.log(' $ zap [id] --age [age]: Zap pending wallet TXs.'); + self.log(' $ wallet create [id]: Create wallet.'); self.log(' $ broadcast [tx-hex]: Broadcast transaction.'); - self.log(' $ view [tx-hex]: View transaction.'); self.log(' $ mempool: Get mempool snapshot.'); self.log(' $ tx [hash/address]: View transactions.'); self.log(' $ coin [hash+index/address]: View coins.'); self.log(' $ block [hash/height]: View block.'); - self.log('Other Options:'); - self.log(' --passphrase [passphrase]: For signing and account creation.'); - self.log(' --account [acctname]: Account name.'); return callback(); } }); diff --git a/lib/http/client.js b/lib/http/client.js index 0e9a0338..c06fbd03 100644 --- a/lib/http/client.js +++ b/lib/http/client.js @@ -617,7 +617,14 @@ HTTPClient.prototype.send = function send(id, options, callback) { */ HTTPClient.prototype.retoken = function retoken(id, passphrase, callback) { - var options = { passphrase: passphrase }; + var options; + + if (typeof passphrase === 'function') { + callback = passphrase; + passphrase = null; + } + + options = { passphrase: passphrase }; this._post('/wallet/' + id + '/retoken', options, function(err, body) { if (err) @@ -823,6 +830,32 @@ HTTPClient.prototype.createAccount = function createAccount(id, options, callbac this._post(path, options, callback); }; +/** + * Create address. + * @param {WalletID} id + * @param {Object} options + * @param {Function} callback - Returns [Error, Array]. + */ + +HTTPClient.prototype.createAddress = function createAddress(id, options, callback) { + var path; + + if (typeof options === 'function') { + callback = options; + options = null; + } + + if (!options) + options = {}; + + if (typeof options === 'string') + options = { account: options }; + + path = '/wallet/' + id + '/address'; + + this._post(path, options, callback); +}; + /* * Helpers */ diff --git a/lib/http/wallet.js b/lib/http/wallet.js index 4645758f..ad405248 100644 --- a/lib/http/wallet.js +++ b/lib/http/wallet.js @@ -287,6 +287,14 @@ HTTPWallet.prototype.createAccount = function createAccount(options, callback) { this.client.createAccount(this.id, options, callback); }; +/** + * @see Wallet#createAddress + */ + +HTTPWallet.prototype.createAddress = function createAddress(account, callback) { + this.client.createAddress(this.id, account, callback); +}; + /** * @see Wallet#setPassphrase */ @@ -301,6 +309,12 @@ HTTPWallet.prototype.setPassphrase = function setPassphrase(old, new_, callback) HTTPWallet.prototype.retoken = function retoken(passphrase, callback) { var self = this; + + if (typeof passphrase === 'function') { + callback = passphrase; + passphrase = null; + } + this.client.retoken(this.id, passphrase, function(err, token) { if (err) return callback(err); diff --git a/lib/wallet/wallet.js b/lib/wallet/wallet.js index e740690d..f8afe079 100644 --- a/lib/wallet/wallet.js +++ b/lib/wallet/wallet.js @@ -259,9 +259,12 @@ Wallet.prototype.addKey = function addKey(account, key, callback) { if (typeof key === 'function') { callback = key; key = account; - account = 0; + account = null; } + if (account == null) + account = 0; + callback = this._lockWrite(addKey, [account, key, callback]); if (!callback) @@ -303,9 +306,12 @@ Wallet.prototype.removeKey = function removeKey(account, key, callback) { if (typeof key === 'function') { callback = key; key = account; - account = 0; + account = null; } + if (account == null) + account = 0; + callback = this._lockWrite(removeKey, [account, key, callback]); if (!callback) @@ -489,6 +495,7 @@ Wallet.prototype.createAccount = function createAccount(options, callback) { var self = this; var passphrase = options.passphrase; var timeout = options.timeout; + var name = options.name; var key; callback = this._lockWrite(createAccount, [options, callback]); @@ -496,6 +503,12 @@ Wallet.prototype.createAccount = function createAccount(options, callback) { if (!callback) return; + if (typeof options.account === 'string') + name = options.account; + + if (!name) + name = self.accountDepth + ''; + this.unlock(passphrase, timeout, function(err, master) { if (err) return callback(err); @@ -506,7 +519,7 @@ Wallet.prototype.createAccount = function createAccount(options, callback) { network: self.network, wid: self.wid, id: self.id, - name: self.accountDepth === 0 ? 'default' : options.name, + name: self.accountDepth === 0 ? 'default' : name, witness: options.witness, accountKey: key.hdPublicKey, accountIndex: self.accountDepth, @@ -624,7 +637,7 @@ Wallet.prototype.hasAccount = function hasAccount(account, callback) { Wallet.prototype.createReceive = function createReceive(account, callback) { if (typeof account === 'function') { callback = account; - account = 0; + account = null; } return this.createAddress(account, false, callback); }; @@ -638,7 +651,7 @@ Wallet.prototype.createReceive = function createReceive(account, callback) { Wallet.prototype.createChange = function createChange(account, callback) { if (typeof account === 'function') { callback = account; - account = 0; + account = null; } return this.createAddress(account, true, callback); }; @@ -656,9 +669,12 @@ Wallet.prototype.createAddress = function createAddress(account, change, callbac if (typeof change === 'function') { callback = change; change = account; - account = 0; + account = null; } + if (account == null) + account = 0; + callback = this._lockWrite(createAddress, [account, change, callback]); if (!callback) @@ -813,9 +829,12 @@ Wallet.prototype.importKey = function importKey(account, ring, passphrase, callb if (typeof ring === 'function') { callback = ring; ring = account; - account = 0; + account = null; } + if (account == null) + account = 0; + callback = this._lockWrite(importKey, [account, ring, passphrase, callback]); if (!callback) diff --git a/lib/wallet/walletdb.js b/lib/wallet/walletdb.js index 68f65074..81211707 100644 --- a/lib/wallet/walletdb.js +++ b/lib/wallet/walletdb.js @@ -836,7 +836,7 @@ WalletDB.prototype.createAccount = function createAccount(options, callback) { var self = this; var account; - this.hasAccount(options.wid, options.accountIndex, function(err, exists) { + this.hasAccount(options.wid, options.name, function(err, exists) { if (err) return callback(err);