diff --git a/lib/bitcoind.js b/lib/bitcoind.js index c2f242ef..8edac06b 100644 --- a/lib/bitcoind.js +++ b/lib/bitcoind.js @@ -651,6 +651,91 @@ script.encode = function encode(s) { return res; }; +/** + * Wallet + */ + +function Wallet() {} + +Wallet.prototype.createAddress = function(name) { + return bitcoindjs.walletNewAddress({ name: name }); +}; + +Wallet.prototype.getAccountAddress = function(options) { + return bitcoindjs.getAccountAddress(options); +}; + +Wallet.prototype.setAccount = function(options) { + return bitcoindjs.setAccount(options); +}; + +Wallet.prototype.getAccount = function(options) { + return bitcoindjs.getAccount(options); +}; + +Wallet.prototype.sendToAddress = function(options) { + return bitcoindjs.sendToAddress(options); +}; + +Wallet.prototype.signMessage = function(options) { + return bitcoindjs.signMessage(options); +}; + +Wallet.prototype.verifyMessage = function(options) { + return bitcoindjs.verifyMessage(options); +}; + +Wallet.prototype.getBalance = function(options) { + return bitcoindjs.getBalance(options); +}; + +Wallet.prototype.getUnconfirmedBalance = function(options) { + return bitcoindjs.getUnconfirmedBalance(options); +}; + +Wallet.prototype.sendFrom = function(options) { + return bitcoindjs.sendFrom(options); +}; + +Wallet.prototype.listTransactions = function(options) { + return bitcoindjs.listTransactions(options); +}; + +Wallet.prototype.listAccounts = function(options) { + return bitcoindjs.listAccounts(options); +}; + +Wallet.prototype.getTransaction = function(options) { + return bitcoindjs.getTransaction(options); +}; + +Wallet.prototype.backupWallet = function(options) { + return bitcoindjs.backupWallet(options); +}; + +Wallet.prototype.walletPassphrase = function(options) { + return bitcoindjs.walletPassphrase(options); +}; + +Wallet.prototype.walletPassphraseChange = function(options) { + return bitcoindjs.walletPassphraseChange(options); +}; + +Wallet.prototype.walletLock = function(options) { + return bitcoindjs.walletLock(options); +}; + +Wallet.prototype.encryptWallet = function(options) { + return bitcoindjs.encryptWallet(options); +}; + +Wallet.prototype.setTxFee = function(options) { + return bitcoindjs.setTxFee(options); +}; + +// singleton +Wallet = new Wallet; + /** * Utils */ diff --git a/src/bitcoindjs.cc b/src/bitcoindjs.cc index 24669856..5dc8d8c3 100644 --- a/src/bitcoindjs.cc +++ b/src/bitcoindjs.cc @@ -128,6 +128,7 @@ NAN_METHOD(PollMempool); NAN_METHOD(BroadcastTx); NAN_METHOD(VerifyBlock); NAN_METHOD(VerifyTransaction); +NAN_METHOD(WalletNewAddress); static void async_start_node_work(uv_work_t *req); @@ -1143,6 +1144,46 @@ NAN_METHOD(VerifyTransaction) { NanReturnValue(NanNew(valid && standard)); } +/** + * Wallet + */ + +NAN_METHOD(WalletNewAddress) { + NanScope(); + + if (args.Length() < 1 || !args[0]->IsObject()) { + return NanThrowError( + "Usage: bitcoindjs.walletNewAddress(options)"); + } + + // Parse the account first so we don't generate a key if there's an error + Local options = Local::Cast(args[0]); + String::Utf8Value name_(options->Get(NanNew("name"))->ToString()); + std::string strAccount = std::string(*name_); + + if (!pwalletMain->IsLocked()) { + pwalletMain->TopUpKeyPool(); + } + + // Generate a new key that is added to wallet + CPubKey newKey; + + if (!pwalletMain->GetKeyFromPool(newKey)) { + // return NanThrowError("Keypool ran out, please call keypoolrefill first"); + EnsureWalletIsUnlocked(); + pwalletMain->TopUpKeyPool(100); + if (pwalletMain->GetKeyPoolSize() < 100) { + return NanThrowError("Error refreshing keypool."); + } + } + + CKeyID keyID = newKey.GetID(); + + pwalletMain->SetAddressBook(keyID, strAccount, "receive"); + + NanReturnValue(NanNew(CBitcoinAddress(keyID).ToString())); +} + /** * Conversions */ @@ -1376,6 +1417,7 @@ init(Handle target) { NODE_SET_METHOD(target, "broadcastTx", BroadcastTx); NODE_SET_METHOD(target, "verifyBlock", VerifyBlock); NODE_SET_METHOD(target, "verifyTransaction", VerifyTransaction); + NODE_SET_METHOD(target, "walletNewAddress", WalletNewAddress); } NODE_MODULE(bitcoindjs, init)