|
6 | 6 | #include "Bitcoin/SigHashType.h"
|
7 | 7 | #include "HexCoding.h"
|
8 | 8 | #include "proto/Bitcoin.pb.h"
|
| 9 | +#include "proto/BitcoinV2.pb.h" |
9 | 10 | #include "TestUtilities.h"
|
10 | 11 |
|
11 | 12 | #include <TrustWalletCore/TWBitcoinSigHashType.h>
|
@@ -161,6 +162,76 @@ TEST(BitcoinCash, SignTransaction) {
|
161 | 162 | "e510000000000000" "1976a9149e089b6889e032d46e3b915a3392edfd616fb1c488ac"
|
162 | 163 | "00000000");
|
163 | 164 | }
|
| 165 | + |
| 166 | +TEST(BitcoinCash, SignTransactionV2) { |
| 167 | + auto privateKey = parse_hex("7fdafb9db5bc501f2096e7d13d331dc7a75d9594af3d251313ba8b6200f4e384"); |
| 168 | + auto txId = parse_hex("050d00e2e18ef13969606f1ceee290d3f49bd940684ce39898159352952b8ce2"); |
| 169 | + std::reverse(txId.begin(), txId.end()); |
| 170 | + |
| 171 | + BitcoinV2::Proto::SigningInput signing; |
| 172 | + signing.add_private_keys(privateKey.data(), privateKey.size()); |
| 173 | + signing.mutable_chain_info()->set_p2pkh_prefix(0); |
| 174 | + signing.mutable_chain_info()->set_p2sh_prefix(5); |
| 175 | + signing.mutable_chain_info()->set_hrp("bitcoincash"); |
| 176 | + |
| 177 | + auto& builder = *signing.mutable_builder(); |
| 178 | + builder.set_version(BitcoinV2::Proto::TransactionVersion::V1); |
| 179 | + builder.set_input_selector(BitcoinV2::Proto::InputSelector::UseAll); |
| 180 | + builder.set_fixed_dust_threshold(546); |
| 181 | + |
| 182 | + auto& in = *builder.add_inputs(); |
| 183 | + auto& inOutPoint = *in.mutable_out_point(); |
| 184 | + inOutPoint.set_hash(txId.data(), txId.size()); |
| 185 | + inOutPoint.set_vout(2); |
| 186 | + in.set_value(5151); |
| 187 | + // Cash address without prefix. |
| 188 | + in.set_receiver_address("qzhlrcrcne07x94h99thved2pgzdtv8ccujjy73xya"); |
| 189 | + in.set_sighash_type(TWBitcoinSigHashTypeAll | TWBitcoinSigHashTypeFork); |
| 190 | + |
| 191 | + auto& out0 = *builder.add_outputs(); |
| 192 | + out0.set_value(600); |
| 193 | + // Legacy address. |
| 194 | + out0.set_to_address("1Bp9U1ogV3A14FMvKbRJms7ctyso4Z4Tcx"); |
| 195 | + |
| 196 | + auto& explicitChangeOutput = *builder.add_outputs(); |
| 197 | + explicitChangeOutput.set_value(4325); |
| 198 | + // Cash address with an explicit prefix. |
| 199 | + explicitChangeOutput.set_to_address("bitcoincash:qz0q3xmg38sr94rw8wg45vujah7kzma3cskxymnw06"); |
| 200 | + |
| 201 | + Proto::SigningInput legacy; |
| 202 | + *legacy.mutable_signing_v2() = signing; |
| 203 | + legacy.set_coin_type(TWCoinTypeBitcoinCash); |
| 204 | + |
| 205 | + Proto::TransactionPlan plan; |
| 206 | + ANY_PLAN(legacy, plan, TWCoinTypeBitcoin); |
| 207 | + |
| 208 | + ASSERT_EQ(plan.error(), Common::Proto::SigningError::OK); |
| 209 | + const auto planV2 = plan.planning_result_v2(); |
| 210 | + EXPECT_EQ(planV2.error(), Common::Proto::SigningError::OK) << planV2.error_message(); |
| 211 | + |
| 212 | + EXPECT_EQ(planV2.inputs_size(), 1); |
| 213 | + EXPECT_EQ(planV2.outputs_size(), 2); |
| 214 | + EXPECT_EQ(planV2.vsize_estimate(), 227); |
| 215 | + EXPECT_EQ(planV2.fee_estimate(), 226); |
| 216 | + EXPECT_EQ(planV2.change(), 0); |
| 217 | + |
| 218 | + Proto::SigningOutput output; |
| 219 | + ANY_SIGN(legacy, TWCoinTypeBitcoin); |
| 220 | + |
| 221 | + EXPECT_EQ(output.error(), Common::Proto::OK); |
| 222 | + ASSERT_TRUE(output.has_signing_result_v2()); |
| 223 | + const auto outputV2 = output.signing_result_v2(); |
| 224 | + EXPECT_EQ(outputV2.error(), Common::Proto::SigningError::OK) << outputV2.error_message(); |
| 225 | + ASSERT_EQ(hex(outputV2.encoded()), |
| 226 | + "01000000" |
| 227 | + "01" |
| 228 | + "e28c2b955293159898e34c6840d99bf4d390e2ee1c6f606939f18ee1e2000d05" "02000000" "6b483045022100b70d158b43cbcded60e6977e93f9a84966bc0cec6f2dfd1463d1223a90563f0d02207548d081069de570a494d0967ba388ff02641d91cadb060587ead95a98d4e3534121038eab72ec78e639d02758e7860cdec018b49498c307791f785aa3019622f4ea5b" "ffffffff" |
| 229 | + "02" |
| 230 | + "5802000000000000" "1976a914769bdff96a02f9135a1d19b749db6a78fe07dc9088ac" |
| 231 | + "e510000000000000" "1976a9149e089b6889e032d46e3b915a3392edfd616fb1c488ac" |
| 232 | + "00000000"); |
| 233 | +} |
| 234 | + |
164 | 235 | // clang-format on
|
165 | 236 |
|
166 | 237 | } // namespace TW::Bitcoin::tests
|
0 commit comments