From f19bd33fe30f10457b942d23bb2bf907091f16e1 Mon Sep 17 00:00:00 2001 From: Christopher Jeffrey Date: Thu, 13 Nov 2014 15:28:42 -0800 Subject: [PATCH] remove todos. implement WalletGetTransaction. --- lib/bitcoind.js | 3 +++ src/bitcoindjs.cc | 60 ++++++++++++++++++++++++++++++++++++++++++----- 2 files changed, 57 insertions(+), 6 deletions(-) diff --git a/lib/bitcoind.js b/lib/bitcoind.js index 29618468..82a4b0bc 100644 --- a/lib/bitcoind.js +++ b/lib/bitcoind.js @@ -808,11 +808,13 @@ Wallet.prototype.getUnconfirmedBalance = function(options) { return bitcoindjs.walletGetUnconfirmedBalance(options || {}); }; +// XXX Wallet Transactions Wallet.prototype.listTransactions = Wallet.prototype.getTransactions = function(options) { return bitcoindjs.walletListTransactions(options || {}); }; +// XXX Wallet Transactions Wallet.prototype.listReceivedByAddress = Wallet.prototype.getReceivedByAddress = function(options) { return bitcoindjs.walletReceivedByAddress(options || {}); @@ -823,6 +825,7 @@ Wallet.prototype.getAccounts = function(options) { return bitcoindjs.walletListAccounts(options || {}); }; +// XXX Wallet Transactions Wallet.prototype.getTransaction = function(options) { return bitcoindjs.walletGetTransaction(options || {}); }; diff --git a/src/bitcoindjs.cc b/src/bitcoindjs.cc index b5f27d2d..858dd940 100644 --- a/src/bitcoindjs.cc +++ b/src/bitcoindjs.cc @@ -4283,8 +4283,7 @@ NAN_METHOD(WalletReceivedByAddress) { * WalletListAccounts() * bitcoindjs.walletListAccounts(options) * This will list all accounts, addresses, balanced, private keys, public keys, - * and whether these keys are in compressed format. TODO: Only output private - * keys if wallet is decrypted. + * and whether these keys are in compressed format. */ NAN_METHOD(WalletListAccounts) { @@ -4394,8 +4393,7 @@ NAN_METHOD(WalletListAccounts) { /** * WalletGetTransaction() * bitcoindjs.walletGetTransaction(options) - * Get any transaction pertaining to any owned addresses. NOT YET IMPLEMENTED. - * XXX TODO + * Get any transaction pertaining to any owned addresses. */ NAN_METHOD(WalletGetTransaction) { @@ -4408,13 +4406,63 @@ NAN_METHOD(WalletGetTransaction) { Local options = Local::Cast(args[0]); + std::string txid; if (options->Get(NanNew("txid"))->IsString()) { String::Utf8Value txid_(options->Get(NanNew("txid"))->ToString()); - std::string txid = std::string(*txid_); - NanReturnValue(NanNew(txid)); + txid = std::string(*txid_); } +#if 0 + // XXX - SLOW + Local txs = WalletListTransactions(args); + for (unsigned int i = 0; i < txs->Length(); ti++) { + String::Utf8Value id_(txs[i]->Get(NanNew("txid"))->ToString()); + std::string id = std::string(*id_); + if (id == txid) { + NanReturnValue(txs[i]); + } + } NanReturnValue(Undefined()); +#endif + + uint256 hash; + hash.SetHex(txid); + + isminefilter filter = ISMINE_SPENDABLE; + if (options->Get(NanNew("watch"))->IsBoolean() + && options->Get(NanNew("watch"))->IsTrue()) { + filter = filter | ISMINE_WATCH_ONLY; + } + + Local entry = NanNew(); + if (!pwalletMain->mapWallet.count(hash)) { + return NanThrowError("Invalid or non-wallet transaction id"); + } + + const CWalletTx& wtx = pwalletMain->mapWallet[hash]; + + CAmount nCredit = wtx.GetCredit(filter != 0); + CAmount nDebit = wtx.GetDebit(filter); + CAmount nNet = nCredit - nDebit; + CAmount nFee = (wtx.IsFromMe(filter) ? wtx.GetValueOut() - nDebit : 0); + + entry->Set(NanNew("amount"), NanNew(SatoshiFromAmount(nNet - nFee))); + + if (wtx.IsFromMe(filter)) { + entry->Set(NanNew("fee"), NanNew(SatoshiFromAmount(nFee))); + } + + WalletTxToJSON_V8(wtx, entry); + + Local details = NanNew(); + int a_count = 0; + ListTransactions_V8(wtx, "*", 0, false, details, filter, &a_count); + entry->Set(NanNew("details"), details); + + std::string strHex = EncodeHexTx(static_cast(wtx)); + entry->Set(NanNew("hex"), NanNew(strHex)); + + NanReturnValue(entry); } /**