diff --git a/example/index.js b/example/index.js index fe91222f..8ff9b249 100755 --- a/example/index.js +++ b/example/index.js @@ -231,6 +231,7 @@ bitcoind.on('open', function(status) { bitcoind.log(bitcoind.getInfo()); bitcoind.log(bitcoind.getPeerInfo()); bitcoind.log(bitcoind.wallet.listAccounts()); + bitcoind.log(bitcoind.wallet.getRecipients()); bitcoind.once('version', function(version) { bitcoind.log('VERSION packet:'); diff --git a/lib/bitcoind.js b/lib/bitcoind.js index a6aad13b..5e5c8654 100644 --- a/lib/bitcoind.js +++ b/lib/bitcoind.js @@ -353,24 +353,6 @@ Bitcoin.prototype.getAddresses = function() { return bitcoindjs.getAddresses(); }; -Bitcoin.prototype.getRecipients = function(options) { - return bitcoindjs.getRecipients(options || {}); -}; - -Bitcoin.prototype.getRecipient = function(options) { - options = options || {}; - var label = options.label || label; - return bitcoindjs.getRecipients({ _label: label }); -}; - -Bitcoin.prototype.setRecipient = function(options) { - return bitcoindjs.setRecipient(options || {}); -}; - -Bitcoin.prototype.removeRecipient = function(options) { - return bitcoindjs.removeRecipient(options || {}); -}; - Bitcoin.prototype.getProgress = function(callback) { return bitcoindjs.getProgress(callback); }; @@ -701,6 +683,24 @@ Wallet.prototype.getAccount = function(options) { return bitcoindjs.walletGetAccount(options || {}); }; +Wallet.prototype.getRecipients = function(options) { + return bitcoindjs.walletGetRecipients(options || {}); +}; + +Wallet.prototype.getRecipient = function(options) { + options = options || {}; + var label = options.label || label; + return bitcoindjs.walletGetRecipients({ _label: label }); +}; + +Wallet.prototype.setRecipient = function(options) { + return bitcoindjs.walletSetRecipient(options || {}); +}; + +Wallet.prototype.removeRecipient = function(options) { + return bitcoindjs.walletRemoveRecipient(options || {}); +}; + Wallet.prototype.sendTo = function(options) { return bitcoindjs.walletSendTo(options || {}); }; diff --git a/src/bitcoindjs.cc b/src/bitcoindjs.cc index b1572491..9cb23cb3 100644 --- a/src/bitcoindjs.cc +++ b/src/bitcoindjs.cc @@ -178,9 +178,6 @@ NAN_METHOD(FillTransaction); NAN_METHOD(GetInfo); NAN_METHOD(GetPeerInfo); NAN_METHOD(GetAddresses); -NAN_METHOD(GetRecipients); -NAN_METHOD(SetRecipient); -NAN_METHOD(RemoveRecipient); NAN_METHOD(GetProgress); NAN_METHOD(SetGenerate); NAN_METHOD(GetGenerate); @@ -196,6 +193,9 @@ NAN_METHOD(WalletNewAddress); NAN_METHOD(WalletGetAccountAddress); NAN_METHOD(WalletSetAccount); NAN_METHOD(WalletGetAccount); +NAN_METHOD(WalletGetRecipients); +NAN_METHOD(WalletSetRecipient); +NAN_METHOD(WalletRemoveRecipient); NAN_METHOD(WalletSendTo); NAN_METHOD(WalletSignMessage); NAN_METHOD(WalletVerifyMessage); @@ -1473,102 +1473,6 @@ NAN_METHOD(GetAddresses) { NanReturnValue(array); } -/** - * GetRecipients() - * bitcoindjs.getRecipients() - * Get all recipients - */ - -NAN_METHOD(GetRecipients) { - NanScope(); - - if (args.Length() < 1 || !args[0]->IsObject()) { - return NanThrowError( - "Usage: bitcoindjs.getRecipients(options)"); - } - - Local options = Local::Cast(args[0]); - - Local array = NanNew(); - int i = 0; - - BOOST_FOREACH(const PAIRTYPE(CBitcoinAddress, CAddressBookData)& item, pwalletMain->mapAddressBook) { - const CBitcoinAddress& address = item.first; - const string& strName = item.second.name; - if (item.second.purpose == "send" && address.IsValid()) { - Local recipient = NanNew(); - recipient->Set(NanNew("label"), NanNew(strName)); - recipient->Set(NanNew("address"), NanNew(address.ToString())); - array->Set(i, recipient); - i++; - if (options->Get(NanNew("_label"))->IsString()) { - break; - } - } - } - - if (options->Get(NanNew("_label"))->IsString()) { - NanReturnValue(array->Get(0)); - } - - NanReturnValue(array); -} - -/** - * SetRecipient() - * bitcoindjs.setRecipient() - * Set a recipient - */ - -NAN_METHOD(SetRecipient) { - NanScope(); - - if (args.Length() < 1 || !args[0]->IsObject()) { - return NanThrowError( - "Usage: bitcoindjs.setRecipient(options)"); - } - - Local options = Local::Cast(args[0]); - - String::Utf8Value addr_(options->Get(NanNew("address"))->ToString()); - std::string addr = std::string(*addr_); - - String::Utf8Value label_(options->Get(NanNew("label"))->ToString()); - std::string label = std::string(*label_); - - CTxDestination address = CBitcoinAddress(addr).Get(); - - pwalletMain->SetAddressBook(address, label, "send"); - - NanReturnValue(True()); -} - -/** - * RemoveRecipient() - * bitcoindjs.removeRecipient() - * Remove a recipient - */ - -NAN_METHOD(RemoveRecipient) { - NanScope(); - - if (args.Length() < 1 || !args[0]->IsObject()) { - return NanThrowError( - "Usage: bitcoindjs.removeRecipient(options)"); - } - - Local options = Local::Cast(args[0]); - - String::Utf8Value addr_(options->Get(NanNew("address"))->ToString()); - std::string addr = std::string(*addr_); - - CTxDestination address = CBitcoinAddress(addr).Get(); - - pwalletMain->DelAddressBook(address); - - NanReturnValue(True()); -} - /** * GetProgress() * bitcoindjs.getProgress(callback) @@ -2719,6 +2623,102 @@ NAN_METHOD(WalletGetAccount) { NanReturnValue(NanNew(strAccount.c_str())); } +/** + * WalletGetRecipients() + * bitcoindjs.walletGetRecipients() + * Get all recipients + */ + +NAN_METHOD(WalletGetRecipients) { + NanScope(); + + if (args.Length() < 1 || !args[0]->IsObject()) { + return NanThrowError( + "Usage: bitcoindjs.walletGetRecipients(options)"); + } + + Local options = Local::Cast(args[0]); + + Local array = NanNew(); + int i = 0; + + BOOST_FOREACH(const PAIRTYPE(CBitcoinAddress, CAddressBookData)& item, pwalletMain->mapAddressBook) { + const CBitcoinAddress& address = item.first; + const string& strName = item.second.name; + if (item.second.purpose == "send" && address.IsValid()) { + Local recipient = NanNew(); + recipient->Set(NanNew("label"), NanNew(strName)); + recipient->Set(NanNew("address"), NanNew(address.ToString())); + array->Set(i, recipient); + i++; + if (options->Get(NanNew("_label"))->IsString()) { + break; + } + } + } + + if (options->Get(NanNew("_label"))->IsString()) { + NanReturnValue(array->Get(0)); + } + + NanReturnValue(array); +} + +/** + * WalletSetRecipient() + * bitcoindjs.walletSetRecipient() + * Set a recipient + */ + +NAN_METHOD(WalletSetRecipient) { + NanScope(); + + if (args.Length() < 1 || !args[0]->IsObject()) { + return NanThrowError( + "Usage: bitcoindjs.walletSetRecipient(options)"); + } + + Local options = Local::Cast(args[0]); + + String::Utf8Value addr_(options->Get(NanNew("address"))->ToString()); + std::string addr = std::string(*addr_); + + String::Utf8Value label_(options->Get(NanNew("label"))->ToString()); + std::string label = std::string(*label_); + + CTxDestination address = CBitcoinAddress(addr).Get(); + + pwalletMain->SetAddressBook(address, label, "send"); + + NanReturnValue(True()); +} + +/** + * WalletRemoveRecipient() + * bitcoindjs.walletRemoveRecipient() + * Remove a recipient + */ + +NAN_METHOD(WalletRemoveRecipient) { + NanScope(); + + if (args.Length() < 1 || !args[0]->IsObject()) { + return NanThrowError( + "Usage: bitcoindjs.walletRemoveRecipient(options)"); + } + + Local options = Local::Cast(args[0]); + + String::Utf8Value addr_(options->Get(NanNew("address"))->ToString()); + std::string addr = std::string(*addr_); + + CTxDestination address = CBitcoinAddress(addr).Get(); + + pwalletMain->DelAddressBook(address); + + NanReturnValue(True()); +} + /** * WalletSendTo() * bitcoindjs.walletSendTo(options) @@ -4959,9 +4959,9 @@ init(Handle target) { NODE_SET_METHOD(target, "getInfo", GetInfo); NODE_SET_METHOD(target, "getPeerInfo", GetPeerInfo); NODE_SET_METHOD(target, "getAddresses", GetAddresses); - NODE_SET_METHOD(target, "getRecipients", GetRecipients); - NODE_SET_METHOD(target, "setRecipient", SetRecipient); - NODE_SET_METHOD(target, "removeRecipient", RemoveRecipient); + NODE_SET_METHOD(target, "walletGetRecipients", WalletGetRecipients); + NODE_SET_METHOD(target, "walletSetRecipient", WalletSetRecipient); + NODE_SET_METHOD(target, "walletRemoveRecipient", WalletRemoveRecipient); NODE_SET_METHOD(target, "getProgress", GetProgress); NODE_SET_METHOD(target, "setGenerate", SetGenerate); NODE_SET_METHOD(target, "getGenerate", GetGenerate);