Skip to content

Instantly share code, notes, and snippets.

@zpalmtree
Created March 20, 2018 22:27
Show Gist options
  • Save zpalmtree/93b8cb9f8e6fbb675dd00589b6bf0708 to your computer and use it in GitHub Desktop.
Save zpalmtree/93b8cb9f8e6fbb675dd00589b6bf0708 to your computer and use it in GitHub Desktop.
void printTxOutputs(Crypto::Hash transactionHash)
{
std::vector<TransactionDetails> transactions;
std::vector<Crypto::Hash> transactionHashes;
transactionHashes.push_back(transactionHash);
m_node->getTransactions(transactionHashes, transactions, callback);
TransactionDetails ourTransaction = transactions[0];
AccountKeys keys;
m_wallet->getAccountKeys(keys);
Crypto::SecretKey privateViewKey = keys.viewSecretKey;
Crypto::SecretKey privateSpendKey = keys.spendSecretKey;
/* The transaction public key */
Crypto::PublicKey publicTransactionKey = ourTransaction.extra.publicKey;
/* The users public spend key */
Crypto::PublicKey publicSpendKey;
Crypto::secret_key_to_public_key(privateSpendKey, publicSpendKey);
/* Derived key is created from the users private view key and the public
transaction key */
Crypto::KeyDerivation derivation;
Crypto::generate_key_derivation(publicTransactionKey, privateViewKey, derivation);
Crypto::PublicKey outputPublicKey;
bool found = false;
uint64_t sum = 0;
/* Loop through each output in the transaction */
for (size_t i = 0; i < ourTransaction.outputs.size(); ++i)
{
Crypto::derive_public_key(derivation, i, publicSpendKey, outputPublicKey);
TransactionOutputTarget target = ourTransaction.outputs[i].output.target;
KeyOutput targetPubKey = boost::get<CryptoNote::KeyOutput>(target);
if (targetPubKey.key == outputPublicKey)
{
uint64_t amount = ourTransaction.outputs[i].output.amount;
std::string xmr = m_currency.formatAmount(amount);
sum += amount;
found = true;
logger(INFO, GREEN) << "The transaction output of " << xmr << " XMR belongs to you!";
}
}
if (!found)
{
logger(ERROR, BRIGHT_RED) << "No outputs were found that belong to you, searched " << ourTransaction.outputs.size() << " outputs.";
}
else
{
std::string xmr = m_currency.formatAmount(sum);
logger(INFO, GREEN) << "Outputs totalling " << xmr << " XMR were sent to your wallet!";
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment