| 26 | ~Impl() { curl_global_cleanup(); } |
| 27 | |
| 28 | bool transferWithWinternitz(const WinternitzAddress &winternitz_address, |
| 29 | const Signature &pq_sig, |
| 30 | const Address &to_address, Amount amount) { |
| 31 | try { |
| 32 | // Get private key from environment |
| 33 | const char *env_private_key = std::getenv("PRIVATE_KEY"); |
| 34 | if (!env_private_key) { |
| 35 | throw std::runtime_error("PRIVATE_KEY environment variable not set"); |
| 36 | } |
| 37 | PrivateKey private_key = env_private_key; |
| 38 | |
| 39 | // Convert C++ types to the format expected by the ABI encoder |
| 40 | nlohmann::json nextPqOwner = { |
| 41 | {"publicSeed", toHex(winternitz_address.publicSeed)}, |
| 42 | {"publicKeyHash", toHex(winternitz_address.publicKeyHash)}}; |
| 43 | |
| 44 | // Convert signature elements to hex strings array (ABI expects bytes32[67]) |
| 45 | nlohmann::json elements = nlohmann::json::array(); |
| 46 | for (const auto &element : pq_sig) { |
| 47 | elements.push_back(toHex(element)); |
| 48 | } |
| 49 | nlohmann::json pqSig = {{"elements", elements}}; |
| 50 | |
| 51 | |
| 52 | // Create parameters for ABI encoding |
| 53 | nlohmann::json params = {nextPqOwner, pqSig, to_address, |
| 54 | std::to_string(amount)}; |
| 55 | |
| 56 | // Encode the function call using shared abiEncode function |
| 57 | std::string params_json = params.dump(); |
| 58 | std::string data = |
| 59 | abiEncode("transferWithWinternitz", abi_json, params_json); |
| 60 | |
| 61 | // Get the transfer fee |
| 62 | Amount transferFee = getTransferFee(); |
| 63 | |
| 64 | // Get account address from private key |
| 65 | std::string from_address = deriveClassicalPublicKey(private_key); |
| 66 | |
| 67 | // Get nonce |
| 68 | nlohmann::json nonce_params = {from_address, "latest"}; |
| 69 | auto nonce_response = |
| 70 | sendJsonRpc("eth_getTransactionCount", nonce_params); |
| 71 | std::string nonce_hex = nonce_response["result"]; |
| 72 | uint64_t nonce = std::stoull(nonce_hex.substr(2), nullptr, 16); |
| 73 | |
| 74 | // Get gas price and convert to EIP-1559 format |
| 75 | auto gas_price_response = |
| 76 | sendJsonRpc("eth_gasPrice", nlohmann::json::array()); |
| 77 | std::string gas_price_hex = gas_price_response["result"]; |
| 78 | uint64_t gas_price = std::stoull(gas_price_hex.substr(2), nullptr, 16); |
| 79 | |
| 80 | uint64_t max_fee_per_gas = gas_price * 2; |
| 81 | uint64_t max_priority_fee_per_gas = gas_price; |
| 82 | |
| 83 | std::stringstream max_fee_ss; |
| 84 | max_fee_ss << "0x" << std::hex << max_fee_per_gas; |
| 85 | std::string max_fee_hex = max_fee_ss.str(); |
nothing calls this directly
no test coverage detected