diff --git a/lib/bitcoind.js b/lib/bitcoind.js index 9d7ada9c..7a63db29 100644 --- a/lib/bitcoind.js +++ b/lib/bitcoind.js @@ -768,6 +768,10 @@ Wallet.prototype.verifyMessage = function(options) { return bitcoindjs.walletVerifyMessage(options || {}); }; +Wallet.prototype.createMultiSigAddress = function(options) { + return bitcoindjs.walletCreateMultiSigAddress(options || {}); +}; + Wallet.prototype.getBalance = function(options) { return bitcoindjs.walletGetBalance(options || {}); }; diff --git a/src/bitcoindjs.cc b/src/bitcoindjs.cc index ac9022c7..834e1709 100644 --- a/src/bitcoindjs.cc +++ b/src/bitcoindjs.cc @@ -1631,12 +1631,14 @@ NAN_METHOD(WalletCreateMultiSigAddress) { // Gather public keys if (nRequired < 1) { - return NanThrowError("a multisignature address must require at least one key to redeem"); + return NanThrowError( + "a multisignature address must require at least one key to redeem"); } - if ((int)keys.size() < nRequired) { + if ((int)keys.Length() < nRequired) { return NanThrowError( strprintf("not enough keys supplied " - "(got %"PRIszu" keys, but need at least %d to redeem)", keys.Length(), nRequired)); + "(got %"PRIszu" keys, but need at least %d to redeem)", + keys.Length(), nRequired)); } std::vector pubkeys; @@ -1697,12 +1699,54 @@ NAN_METHOD(WalletGetBalance) { "Usage: bitcoindjs.walletGetBalance(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_); - NanReturnValue(Undefined()); + std::string strAccount = ""; + int nMinDepth = 1; + + if (options->Get(NanNew("account"))->IsString()) { + String::Utf8Value strAccount_(options->Get(NanNew("account"))->ToString()); + strAccount = std::string(*strAccount_); + } + + if (options->Get(NanNew("nMinDepth"))->IsNumber()) { + nMinDepth = options->Get(NanNew("nMinDepth"))->IntegerValue(); + } + + if (strAccount == "*") { + // Calculate total balance a different way from GetBalance() + // (GetBalance() sums up all unspent TxOuts) + // getbalance and getbalance '*' 0 should return the same number + int64_t nBalance = 0; + for (map::iterator it = pwalletMain->mapWallet.begin(); + it != pwalletMain->mapWallet.end(); ++it) { + const CWalletTx& wtx = (*it).second; + + if (!wtx.IsTrusted() || wtx.GetBlocksToMaturity() > 0) { + continue; + } + + int64_t allFee; + string strSentAccount; + list > listReceived; + list > listSent; + wtx.GetAmounts(listReceived, listSent, allFee, strSentAccount); + if (wtx.GetDepthInMainChain() >= nMinDepth) { + BOOST_FOREACH(const PAIRTYPE(CTxDestination,int64_t)& r, listReceived) { + nBalance += r.second; + } + } + BOOST_FOREACH(const PAIRTYPE(CTxDestination,int64_t)& r, listSent) { + nBalance -= r.second; + } + nBalance -= allFee; + } + NanReturnValue(NanNew(nBalance)); + } + + int64_t nBalance = GetAccountBalance(strAccount, nMinDepth); + + NanReturnValue(NanNew(nBalance)); } NAN_METHOD(WalletGetUnconfirmedBalance) {