From 0ea619ff1565788c035513b076bc539b776e1458 Mon Sep 17 00:00:00 2001 From: Christopher Jeffrey Date: Tue, 28 Oct 2014 15:40:36 -0700 Subject: [PATCH] add received by address. --- lib/bitcoind.js | 4 ++++ src/bitcoindjs.cc | 59 +++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 63 insertions(+) diff --git a/lib/bitcoind.js b/lib/bitcoind.js index bd9d84b8..34a096b5 100644 --- a/lib/bitcoind.js +++ b/lib/bitcoind.js @@ -713,6 +713,10 @@ Wallet.prototype.listTransactions = function(options) { return bitcoindjs.walletListTransactions(options || {}); }; +Wallet.prototype.receivedByAddress = function(options) { + return bitcoindjs.walletReceivedByAddress(options || {}); +}; + Wallet.prototype.listAccounts = function(options) { return bitcoindjs.walletListAccounts(options || {}); }; diff --git a/src/bitcoindjs.cc b/src/bitcoindjs.cc index d70a3e92..8103619b 100644 --- a/src/bitcoindjs.cc +++ b/src/bitcoindjs.cc @@ -190,6 +190,7 @@ NAN_METHOD(WalletCreateMultiSigAddress); NAN_METHOD(WalletGetUnconfirmedBalance); NAN_METHOD(WalletSendFrom); NAN_METHOD(WalletListTransactions); +NAN_METHOD(WalletReceivedByAddress); NAN_METHOD(WalletListAccounts); NAN_METHOD(WalletGetTransaction); NAN_METHOD(WalletBackup); @@ -3128,6 +3129,63 @@ NAN_METHOD(WalletListTransactions) { NanReturnValue(Undefined()); } +/** + * WalletReceivedByAddress() + * bitcoindjs.walletReceivedByAddress(options) + * List all transactions pertaining to any owned addreses. NOT YET IMPLEMENTED> + */ + +NAN_METHOD(WalletReceivedByAddress) { + NanScope(); + + if (args.Length() < 1 || !args[0]->IsObject()) { + return NanThrowError( + "Usage: bitcoindjs.walletReceivedByAddress(options)"); + } + + Local options = Local::Cast(args[0]); + + String::Utf8Value addr_(options->Get(NanNew("address"))->ToString()); + std::string addr = std::string(*addr_); + + // Bitcoin address + CBitcoinAddress address = CBitcoinAddress(addr); + if (!address.IsValid()) { + return NanThrowError("Invalid Bitcoin address"); + } + CScript scriptPubKey = GetScriptForDestination(address.Get()); + + if (!IsMine(*pwalletMain, scriptPubKey)) { + NanReturnValue(NanNew((double)0.0)); + } + + // Minimum confirmations + int nMinDepth = 1; + if (options->Get(NanNew("confirmations"))->IsString()) { + nMinDepth = options->Get(NanNew("confirmations"))->ToInt32(); + } + + // Tally + CAmount nAmount = 0; + for (map::iterator it = pwalletMain->mapWallet.begin(); + it != pwalletMain->mapWallet.end(); + ++it) { + const CWalletTx& wtx = (*it).second; + if (wtx.IsCoinBase() || !IsFinalTx(wtx)) { + continue; + } + BOOST_FOREACH(const CTxOut& txout, wtx.vout) + if (txout.scriptPubKey == scriptPubKey) { + if (wtx.GetDepthInMainChain() >= nMinDepth) { + nAmount += txout.nValue; + } + } + } + } + + NanReturnValue(NanNew(nAmount)); +} + /** * WalletListAccounts() * bitcoindjs.walletListAccounts(options) @@ -4533,6 +4591,7 @@ init(Handle target) { NODE_SET_METHOD(target, "walletGetUnconfirmedBalance", WalletGetUnconfirmedBalance); NODE_SET_METHOD(target, "walletSendFrom", WalletSendFrom); NODE_SET_METHOD(target, "walletListTransactions", WalletListTransactions); + NODE_SET_METHOD(target, "walletReceivedByAddress", WalletReceivedByAddress); NODE_SET_METHOD(target, "walletListAccounts", WalletListAccounts); NODE_SET_METHOD(target, "walletGetTransaction", WalletGetTransaction); NODE_SET_METHOD(target, "walletBackup", WalletBackup);