Status bridgeWatcherDelete(Wallet &self) { watcherSave(self).log(); // Failure is not fatal watchers_.erase(self.id()); return Status(); }
tABC_CC ABC_BridgeWatcherDelete(Wallet &self, tABC_Error *pError) { tABC_CC cc = ABC_CC_Ok; watcherSave(self); // Failure is not fatal watchers_.erase(self.id()); return cc; }
tABC_CC ABC_BridgeWatcherLoop(Wallet &self, tABC_BitCoin_Event_Callback fAsyncCallback, void *pData, tABC_Error *pError) { tABC_CC cc = ABC_CC_Ok; Watcher::block_height_callback heightCallback; Watcher::tx_callback txCallback; Watcher::tx_sent_callback sendCallback; Watcher::quiet_callback on_quiet; Watcher::fail_callback failCallback; WatcherInfo *watcherInfo = nullptr; ABC_CHECK_NEW(watcherFind(watcherInfo, self)); watcherInfo->fAsyncCallback = fAsyncCallback; watcherInfo->pData = pData; txCallback = [watcherInfo, fAsyncCallback, pData] (const libbitcoin::transaction_type& tx) { ABC_BridgeTxCallback(watcherInfo, tx, fAsyncCallback, pData); }; watcherInfo->watcher.set_tx_callback(txCallback); heightCallback = [watcherInfo, fAsyncCallback, pData](const size_t height) { if (fAsyncCallback) { tABC_AsyncBitCoinInfo info; info.eventType = ABC_AsyncEventType_BlockHeightChange; info.pData = pData; info.szDescription = "Block height change"; fAsyncCallback(&info); } watcherSave(watcherInfo->wallet); // Failure is not fatal }; watcherInfo->watcher.set_height_callback(heightCallback); on_quiet = [watcherInfo]() { ABC_BridgeQuietCallback(watcherInfo); }; watcherInfo->watcher.set_quiet_callback(on_quiet); failCallback = [watcherInfo]() { tABC_Error error; ABC_BridgeWatcherConnect(watcherInfo->wallet, &error); }; watcherInfo->watcher.set_fail_callback(failCallback); watcherInfo->watcher.loop(); exit: return cc; }
static void ABC_BridgeTxCallback(WatcherInfo *watcherInfo, const libbitcoin::transaction_type& tx, tABC_BitCoin_Event_Callback fAsyncBitCoinEventCallback, void *pData) { tABC_CC cc = ABC_CC_Ok; tABC_Error error; int64_t fees = 0; int64_t totalInSatoshi = 0, totalOutSatoshi = 0, totalMeSatoshi = 0, totalMeInSatoshi = 0; tABC_TxOutput **iarr = NULL, **oarr = NULL; unsigned int idx = 0, iCount = 0, oCount = 0; std::string txId, malTxId; if (watcherInfo == NULL) { cc = ABC_CC_Error; goto exit; } txId = ABC_BridgeNonMalleableTxId(tx); malTxId = bc::encode_hash(bc::hash_transaction(tx)); idx = 0; iCount = tx.inputs.size(); iarr = (tABC_TxOutput **) malloc(sizeof(tABC_TxOutput *) * iCount); for (auto i : tx.inputs) { bc::payment_address addr; bc::extract(addr, i.script); auto prev = i.previous_output; // Create output tABC_TxOutput *out = (tABC_TxOutput *) malloc(sizeof(tABC_TxOutput)); out->input = true; out->szTxId = stringCopy(bc::encode_hash(prev.hash)); out->szAddress = stringCopy(addr.encoded()); // Check prevouts for values auto tx = watcherInfo->watcher.find_tx(prev.hash); if (prev.index < tx.outputs.size()) { out->value = tx.outputs[prev.index].value; totalInSatoshi += tx.outputs[prev.index].value; auto row = watcherInfo->addresses.find(addr.encoded()); if (row != watcherInfo->addresses.end()) totalMeInSatoshi += tx.outputs[prev.index].value; } iarr[idx] = out; idx++; } idx = 0; oCount = tx.outputs.size(); oarr = (tABC_TxOutput **) malloc(sizeof(tABC_TxOutput *) * oCount); for (auto o : tx.outputs) { bc::payment_address addr; bc::extract(addr, o.script); // Create output tABC_TxOutput *out = (tABC_TxOutput *) malloc(sizeof(tABC_TxOutput)); out->input = false; out->value = o.value; out->szAddress = stringCopy(addr.encoded()); out->szTxId = stringCopy(malTxId); // Do we own this address? auto row = watcherInfo->addresses.find(addr.encoded()); if (row != watcherInfo->addresses.end()) { totalMeSatoshi += o.value; } totalOutSatoshi += o.value; oarr[idx] = out; idx++; } if (totalMeSatoshi == 0 && totalMeInSatoshi == 0) { ABC_DebugLog("values == 0, this tx does not concern me.\n"); goto exit; } fees = totalInSatoshi - totalOutSatoshi; totalMeSatoshi -= totalMeInSatoshi; ABC_DebugLog("calling ABC_TxReceiveTransaction"); ABC_DebugLog("Total Me: %s, Total In: %s, Total Out: %s, Fees: %s", std::to_string(totalMeSatoshi).c_str(), std::to_string(totalInSatoshi).c_str(), std::to_string(totalOutSatoshi).c_str(), std::to_string(fees).c_str()); ABC_CHECK_RET( ABC_TxReceiveTransaction( watcherInfo->wallet, totalMeSatoshi, fees, iarr, iCount, oarr, oCount, txId.c_str(), malTxId.c_str(), fAsyncBitCoinEventCallback, pData, &error)); watcherSave(watcherInfo->wallet); // Failure is not fatal exit: ABC_FREE(oarr); ABC_FREE(iarr); }
static Status bridgeTxCallback(Wallet &wallet, const libbitcoin::transaction_type &tx, tABC_BitCoin_Event_Callback fAsyncCallback, void *pData) { const auto addresses = wallet.addresses.list(); const auto info = wallet.txCache.txInfo(tx, addresses); // Does this transaction concern us? if (wallet.txCache.isRelevant(tx, addresses)) { // Does the transaction already exist? Tx meta; if (!wallet.txs.get(meta, info.ntxid)) { meta.ntxid = info.ntxid; meta.txid = info.txid; meta.timeCreation = time(nullptr); meta.internal = false; // Grab metadata from the address: TxMetadata metadata; for (const auto &io: info.ios) { Address address; if (wallet.addresses.get(address, io.address)) meta.metadata = address.metadata; } meta.metadata.amountSatoshi = info.balance; meta.metadata.amountFeesMinersSatoshi = info.fee; ABC_CHECK(gContext->exchangeCache.satoshiToCurrency( meta.metadata.amountCurrency, info.balance, static_cast<Currency>(wallet.currency()))); // Save the metadata: ABC_CHECK(wallet.txs.save(meta)); // Update the transaction cache: watcherSave(wallet).log(); // Failure is not fatal wallet.balanceDirty(); ABC_CHECK(wallet.addresses.markOutputs(info.ios)); // Update the GUI: ABC_DebugLog("IncomingBitCoin callback: wallet %s, txid: %s", wallet.id().c_str(), info.txid.c_str()); tABC_AsyncBitCoinInfo async; async.pData = pData; async.eventType = ABC_AsyncEventType_IncomingBitCoin; Status().toError(async.status, ABC_HERE()); async.szWalletUUID = wallet.id().c_str(); async.szTxID = info.txid.c_str(); async.sweepSatoshi = 0; fAsyncCallback(&async); } else { // Update the transaction cache: watcherSave(wallet).log(); // Failure is not fatal wallet.balanceDirty(); ABC_CHECK(wallet.addresses.markOutputs(info.ios)); // Update the GUI: ABC_DebugLog("BalanceUpdate callback: wallet %s, txid: %s", wallet.id().c_str(), info.txid.c_str()); tABC_AsyncBitCoinInfo async; async.pData = pData; async.eventType = ABC_AsyncEventType_BalanceUpdate; Status().toError(async.status, ABC_HERE()); async.szWalletUUID = wallet.id().c_str(); async.szTxID = info.txid.c_str(); async.sweepSatoshi = 0; fAsyncCallback(&async); } } else { ABC_DebugLog("New (irrelevant) transaction: wallet %s, txid: %s", wallet.id().c_str(), info.txid.c_str()); } return Status(); }
static Status bridgeDoSweep(Wallet &wallet, PendingSweep &sweep, tABC_BitCoin_Event_Callback fAsyncCallback, void *pData) { // Find utxos for this address: AddressSet addresses; addresses.insert(sweep.address); auto utxos = wallet.txCache.get_utxos(addresses); // Bail out if there are no funds to sweep: if (!utxos.size()) { // Tell the GUI if there were funds in the past: if (wallet.txCache.has_history(sweep.address)) { ABC_DebugLog("IncomingSweep callback: wallet %s, value: 0", wallet.id().c_str()); tABC_AsyncBitCoinInfo info; info.pData = pData; info.eventType = ABC_AsyncEventType_IncomingSweep; Status().toError(info.status, ABC_HERE()); info.szWalletUUID = wallet.id().c_str(); info.szTxID = nullptr; info.sweepSatoshi = 0; fAsyncCallback(&info); sweep.done = true; } return Status(); } // Build a transaction: bc::transaction_type tx; tx.version = 1; tx.locktime = 0; // Set up the output: Address address; wallet.addresses.getNew(address); bc::transaction_output_type output; ABC_CHECK(outputScriptForAddress(output.script, address.address)); tx.outputs.push_back(output); // Set up the inputs: uint64_t fee, funds; ABC_CHECK(inputsPickMaximum(fee, funds, tx, utxos)); if (outputIsDust(funds)) return ABC_ERROR(ABC_CC_InsufficientFunds, "Not enough funds"); tx.outputs[0].value = funds; // Now sign that: KeyTable keys; keys[sweep.address] = sweep.key; ABC_CHECK(signTx(tx, wallet.txCache, keys)); // Send: bc::data_chunk raw_tx(satoshi_raw_size(tx)); bc::satoshi_save(tx, raw_tx.begin()); ABC_CHECK(broadcastTx(wallet, raw_tx)); // Calculate transaction information: const auto info = wallet.txCache.txInfo(tx, wallet.addresses.list()); // Save the transaction metadata: Tx meta; meta.ntxid = info.ntxid; meta.txid = info.txid; meta.timeCreation = time(nullptr); meta.internal = true; meta.metadata.amountSatoshi = funds; meta.metadata.amountFeesAirbitzSatoshi = 0; ABC_CHECK(gContext->exchangeCache.satoshiToCurrency( meta.metadata.amountCurrency, info.balance, static_cast<Currency>(wallet.currency()))); ABC_CHECK(wallet.txs.save(meta)); // Update the transaction cache: if (wallet.txCache.insert(tx)) watcherSave(wallet).log(); // Failure is not fatal wallet.balanceDirty(); ABC_CHECK(wallet.addresses.markOutputs(info.ios)); // Done: ABC_DebugLog("IncomingSweep callback: wallet %s, txid: %s, value: %d", wallet.id().c_str(), info.txid.c_str(), output.value); tABC_AsyncBitCoinInfo async; async.pData = pData; async.eventType = ABC_AsyncEventType_IncomingSweep; Status().toError(async.status, ABC_HERE()); async.szWalletUUID = wallet.id().c_str(); async.szTxID = info.txid.c_str(); async.sweepSatoshi = output.value; fAsyncCallback(&async); sweep.done = true; return Status(); }
Status bridgeWatcherLoop(Wallet &self, tABC_BitCoin_Event_Callback fCallback, void *pData) { WatcherInfo *watcherInfo = nullptr; ABC_CHECK(watcherFind(watcherInfo, self)); // Set up the address-changed callback: auto wakeupCallback = [watcherInfo]() { watcherInfo->watcher.sendWakeup(); }; self.addressCache.wakeupCallbackSet(wakeupCallback); // Set up the address-synced callback: auto doneCallback = [watcherInfo, fCallback, pData]() { ABC_DebugLog("AddressCheckDone callback: wallet %s", watcherInfo->wallet.id().c_str()); tABC_AsyncBitCoinInfo info; info.pData = pData; info.eventType = ABC_AsyncEventType_AddressCheckDone; Status().toError(info.status, ABC_HERE()); info.szWalletUUID = watcherInfo->wallet.id().c_str(); info.szTxID = nullptr; info.sweepSatoshi = 0; fCallback(&info); }; self.addressCache.doneCallbackSet(doneCallback); // Set up new-transaction callback: auto txCallback = [watcherInfo, fCallback, pData] (const libbitcoin::transaction_type &tx) { bridgeTxCallback(watcherInfo->wallet, tx, fCallback, pData).log(); }; watcherInfo->watcher.set_tx_callback(txCallback); // Set up new-block callback: auto heightCallback = [watcherInfo, fCallback, pData](const size_t height) { // Update the GUI: ABC_DebugLog("BlockHeightChange callback: wallet %s", watcherInfo->wallet.id().c_str()); tABC_AsyncBitCoinInfo info; info.pData = pData; info.eventType = ABC_AsyncEventType_BlockHeightChange; Status().toError(info.status, ABC_HERE()); info.szWalletUUID = watcherInfo->wallet.id().c_str(); info.szTxID = nullptr; info.sweepSatoshi = 0; fCallback(&info); watcherSave(watcherInfo->wallet).log(); // Failure is not fatal }; watcherInfo->watcher.set_height_callback(heightCallback); // Set up sweep-trigger callback: auto onQuiet = [watcherInfo, fCallback, pData]() { bridgeQuietCallback(watcherInfo, fCallback, pData); }; watcherInfo->watcher.set_quiet_callback(onQuiet); // Do the loop: watcherInfo->watcher.loop(); // Cancel all callbacks: watcherInfo->watcher.set_quiet_callback(nullptr); watcherInfo->watcher.set_height_callback(nullptr); watcherInfo->watcher.set_tx_callback(nullptr); self.addressCache.wakeupCallbackSet(nullptr); self.addressCache.doneCallbackSet(nullptr); return Status(); }