From a02a672a2264864806b47453e51b7bfe60f03af2 Mon Sep 17 00:00:00 2001 From: Christopher Jeffrey Date: Mon, 3 Nov 2014 16:59:19 -0800 Subject: [PATCH] add walletmove. fix callbacks. --- lib/bitcoind.js | 12 ++++--- src/bitcoindjs.cc | 81 +++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 89 insertions(+), 4 deletions(-) diff --git a/lib/bitcoind.js b/lib/bitcoind.js index d8946650..fb4919d3 100644 --- a/lib/bitcoind.js +++ b/lib/bitcoind.js @@ -701,8 +701,8 @@ Wallet.prototype.removeRecipient = function(options) { return bitcoindjs.walletRemoveRecipient(options || {}); }; -Wallet.prototype.sendTo = function(options) { - return bitcoindjs.walletSendTo(options || {}); +Wallet.prototype.sendTo = function(options, callback) { + return bitcoindjs.walletSendTo(options || {}, callback); }; Wallet.prototype.signMessage = function(options) { @@ -725,8 +725,12 @@ Wallet.prototype.getUnconfirmedBalance = function(options) { return bitcoindjs.walletGetUnconfirmedBalance(options || {}); }; -Wallet.prototype.sendFrom = function(options) { - return bitcoindjs.walletSendFrom(options || {}); +Wallet.prototype.sendFrom = function(options, callback) { + return bitcoindjs.walletSendFrom(options || {}, callback); +}; + +Wallet.prototype.move = function(options) { + return bitcoindjs.walletMove(options || {}); }; Wallet.prototype.listTransactions = diff --git a/src/bitcoindjs.cc b/src/bitcoindjs.cc index 3902628c..7009efba 100644 --- a/src/bitcoindjs.cc +++ b/src/bitcoindjs.cc @@ -259,6 +259,7 @@ NAN_METHOD(WalletGetBalance); NAN_METHOD(WalletCreateMultiSigAddress); NAN_METHOD(WalletGetUnconfirmedBalance); NAN_METHOD(WalletSendFrom); +NAN_METHOD(WalletMove); NAN_METHOD(WalletListTransactions); NAN_METHOD(WalletReceivedByAddress); NAN_METHOD(WalletListAccounts); @@ -3419,6 +3420,85 @@ async_wallet_sendfrom_after(uv_work_t *req) { delete req; } +/** + * WalletMove() + * bitcoindjs.walletMove(options) + * Move BTC from one account to another + */ + +NAN_METHOD(WalletMove) { + NanScope(); + + if (args.Length() < 1 || !args[0]->IsObject()) { + return NanThrowError( + "Usage: bitcoindjs.walletMove(options)"); + } + + Local options = Local::Cast(args[0]); + + std::string strFrom; + if (options->Get(NanNew("from"))->IsString()) { + String::Utf8Value s_(options->Get(NanNew("from"))->ToString()); + strFrom = std::string(*s_); + } + + std::string strTo; + if (options->Get(NanNew("to"))->IsString()) { + String::Utf8Value s_(options->Get(NanNew("to"))->ToString()); + strTo = std::string(*s_); + } + + CAmount nAmount; + if (options->Get(NanNew("amount"))->IsNumber()) { + nAmount = (CAmount)options->Get(NanNew("amount"))->IntegerValue(); + } + + // DEPRECATED + // int nMinDepth = 1; + // if (options->Get(NanNew("minDepth"))->IsNumber()) { + // nMinDepth = options->Get(NanNew("minDepth"))->IntegerValue(); + // } + + std::string strComment; + if (options->Get(NanNew("comment"))->IsString()) { + String::Utf8Value s_(options->Get(NanNew("comment"))->ToString()); + strComment = std::string(*s_); + } + + CWalletDB walletdb(pwalletMain->strWalletFile); + if (!walletdb.TxnBegin()) { + return NanThrowError("database error"); + } + + int64_t nNow = GetAdjustedTime(); + + // Debit + CAccountingEntry debit; + debit.nOrderPos = pwalletMain->IncOrderPosNext(&walletdb); + debit.strAccount = strFrom; + debit.nCreditDebit = -nAmount; + debit.nTime = nNow; + debit.strOtherAccount = strTo; + debit.strComment = strComment; + walletdb.WriteAccountingEntry(debit); + + // Credit + CAccountingEntry credit; + credit.nOrderPos = pwalletMain->IncOrderPosNext(&walletdb); + credit.strAccount = strTo; + credit.nCreditDebit = nAmount; + credit.nTime = nNow; + credit.strOtherAccount = strFrom; + credit.strComment = strComment; + walletdb.WriteAccountingEntry(credit); + + if (!walletdb.TxnCommit()) { + return NanThrowError("database error"); + } + + NanReturnValue(Undefined()); +} + /** * WalletListTransactions() * bitcoindjs.walletListTransactions(options) @@ -5171,6 +5251,7 @@ init(Handle target) { NODE_SET_METHOD(target, "walletCreateMultiSigAddress", WalletCreateMultiSigAddress); NODE_SET_METHOD(target, "walletGetUnconfirmedBalance", WalletGetUnconfirmedBalance); NODE_SET_METHOD(target, "walletSendFrom", WalletSendFrom); + NODE_SET_METHOD(target, "walletMove", WalletMove); NODE_SET_METHOD(target, "walletListTransactions", WalletListTransactions); NODE_SET_METHOD(target, "walletReceivedByAddress", WalletReceivedByAddress); NODE_SET_METHOD(target, "walletListAccounts", WalletListAccounts);