|
| 1 | +// Copyright © 2017-2020 Trust Wallet. |
| 2 | +// |
| 3 | +// This file is part of Trust. The full Trust copyright notice, including |
| 4 | +// terms governing use, modification, and redistribution, is contained in the |
| 5 | +// file LICENSE at the root of the source code distribution tree. |
| 6 | + |
| 7 | +#include <TrustWalletCore/TWAnyAddress.h> |
| 8 | +#include <TrustWalletCore/TWAnySigner.h> |
| 9 | +#include <TrustWalletCore/TWBitcoinSigHashType.h> |
| 10 | +#include <TrustWalletCore/TWBitcoinScript.h> |
| 11 | +#include <TrustWalletCore/TWHDWallet.h> |
| 12 | +#include <gtest/gtest.h> |
| 13 | + |
| 14 | +#include "Bitcoin/SegwitAddress.h" |
| 15 | +#include "proto/Bitcoin.pb.h" |
| 16 | +#include "Bitcoin/OutPoint.h" |
| 17 | +#include "Bitcoin/Script.h" |
| 18 | +#include "Bitcoin/Transaction.h" |
| 19 | +#include "Bitcoin/TransactionBuilder.h" |
| 20 | +#include "Bitcoin/TransactionSigner.h" |
| 21 | +#include "HexCoding.h" |
| 22 | +#include "../interface/TWTestUtilities.h" |
| 23 | + |
| 24 | +using namespace TW; |
| 25 | +using namespace TW::Bitcoin; |
| 26 | + |
| 27 | +TEST(TWBitcoinGoldScript, LockScriptTest) { |
| 28 | + auto script = WRAP(TWBitcoinScript, TWBitcoinScriptBuildForAddress(STRING("btg1q6572ulr0kmywle8a30lvagm9xsg9k9n5cmzfdj").get(), TWCoinTypeBitcoinGold)); |
| 29 | + auto scriptData = WRAPD(TWBitcoinScriptData(script.get())); |
| 30 | + assertHexEqual(scriptData, "0014d53cae7c6fb6c8efe4fd8bfecea36534105b1674"); |
| 31 | + |
| 32 | + auto script2 = WRAP(TWBitcoinScript, TWBitcoinScriptBuildForAddress(STRING("btg1qawhpp9gv3g662phqufjmj2ps2ge7sq4thy5g07").get(), TWCoinTypeBitcoinGold)); |
| 33 | + auto scriptData2 = WRAPD(TWBitcoinScriptData(script2.get())); |
| 34 | + assertHexEqual(scriptData2, "0014ebae10950c8a35a506e0e265b928305233e802ab"); |
| 35 | +} |
| 36 | + |
| 37 | +TEST(BitcoinGoldKey, ExtendedKeys) { |
| 38 | + auto wallet = WRAP(TWHDWallet, TWHDWalletCreateWithMnemonic( |
| 39 | + STRING("shoot island position soft burden budget tooth cruel issue economy destroy above").get(), |
| 40 | + STRING("TREZOR").get() |
| 41 | + )); |
| 42 | + |
| 43 | + // .bip84 |
| 44 | + auto zprv = WRAPS(TWHDWalletGetExtendedPrivateKey(wallet.get(), TWPurposeBIP84, TWCoinTypeBitcoinGold, TWHDVersionZPRV)); |
| 45 | + auto zpub = WRAPS(TWHDWalletGetExtendedPublicKey(wallet.get(), TWPurposeBIP84, TWCoinTypeBitcoinGold, TWHDVersionZPUB)); |
| 46 | + |
| 47 | + assertStringsEqual(zprv, "zprvAdB7dYnT955ubXEkdBWhDqFSyeDfVpKQmVJPbRXGiAg4mnGT7dCBsZZFeik1mNt6bS4zkdZSNtZm8dqu1Lrp1brQ16NgYgeEoiz6ftUfVAW"); |
| 48 | + assertStringsEqual(zpub, "zpub6rAU34KLySeCp1KDjD3hayCBXg49uH3G8iDzPovtGWD3eabbfAWSRMsjVyfuRfCCquiKTD6YV42nHUBtwh2TbVPvWqxrGuyEvHN17c3XUXw"); |
| 49 | +} |
| 50 | + |
| 51 | +TEST(BitcoinGoldKey, DeriveFromZPub) { |
| 52 | + auto zpub = STRING("zpub6rAU34KLySeCp1KDjD3hayCBXg49uH3G8iDzPovtGWD3eabbfAWSRMsjVyfuRfCCquiKTD6YV42nHUBtwh2TbVPvWqxrGuyEvHN17c3XUXw"); |
| 53 | + auto pubKey2 = TWHDWalletGetPublicKeyFromExtended(zpub.get(), STRING("m/84'/156'/0'/0/2").get()); |
| 54 | + auto pubKey9 = TWHDWalletGetPublicKeyFromExtended(zpub.get(), STRING("m/84'/156'/0'/0/9").get()); |
| 55 | + |
| 56 | + auto address2 = WRAP(TWAnyAddress, TWAnyAddressCreateWithPublicKey(pubKey2, TWCoinTypeBitcoinGold)); |
| 57 | + auto address2String = WRAPS(TWAnyAddressDescription(address2.get())); |
| 58 | + |
| 59 | + auto address9 = WRAP(TWAnyAddress, TWAnyAddressCreateWithPublicKey(pubKey9, TWCoinTypeBitcoinGold)); |
| 60 | + auto address9String = WRAPS(TWAnyAddressDescription(address9.get())); |
| 61 | + |
| 62 | + assertStringsEqual(address2String, "btg1qkdgxykht6nww9l9rn0xhslf78nl605gwka9zak"); |
| 63 | + assertStringsEqual(address9String, "btg1qzw2ptuyaw023gm7te2r5e3xkufn9wrm3kzrg8t"); |
| 64 | +} |
| 65 | + |
| 66 | +/// |
| 67 | +/// Explore this signed transaction in BitcoinGold Mainnet: |
| 68 | +/// https://btg2.trezor.io/tx/3e818ad25d73123b6c1c8099ed462aa5413a4ef57d66d9d260306c012753ba43 |
| 69 | +/// |
| 70 | +TEST(TWBitcoinGoldTxGeneration, TxGeneration) { |
| 71 | + const int64_t amount = 5000; |
| 72 | + |
| 73 | + // Setup input |
| 74 | + Proto::SigningInput input; |
| 75 | + input.set_coin_type(TWCoinTypeBitcoinGold); |
| 76 | + input.set_hash_type(TWBitcoinSigHashTypeAll | TWBitcoinSigHashTypeForkBTG); |
| 77 | + input.set_amount(amount); |
| 78 | + input.set_byte_fee(1); |
| 79 | + input.set_to_address("btg1qmd6x5awe4t5fjhgntv0pngzdwajjg250wxdcs0"); |
| 80 | + input.set_change_address("btg1qawhpp9gv3g662phqufjmj2ps2ge7sq4thy5g07"); |
| 81 | + |
| 82 | + auto utxoKey0 = parse_hex("cbe13a79b82ec7f8871b336a64fd8d531f598e7c9022e29c67e824cfd54af57f"); |
| 83 | + input.add_private_key(utxoKey0.data(), utxoKey0.size()); |
| 84 | + |
| 85 | + |
| 86 | + auto scriptPub1 = Script(parse_hex("0014db746a75d9aae8995d135b1e19a04d7765242a8f")); |
| 87 | + auto scriptHash = std::vector<uint8_t>(); |
| 88 | + scriptPub1.matchPayToWitnessPublicKeyHash(scriptHash); |
| 89 | + auto scriptHashHex = hex(scriptHash.begin(), scriptHash.end()); |
| 90 | + |
| 91 | + auto redeemScript = Script::buildPayToPublicKeyHash(scriptHash); |
| 92 | + auto scriptString = std::string(redeemScript.bytes.begin(), redeemScript.bytes.end()); |
| 93 | + (*input.mutable_scripts())[scriptHashHex] = scriptString; |
| 94 | + |
| 95 | + auto utxo0 = input.add_utxo(); |
| 96 | + auto utxo0Script = parse_hex("0014d53cae7c6fb6c8efe4fd8bfecea36534105b1674"); |
| 97 | + utxo0->set_script(utxo0Script.data(), utxo0Script.size()); |
| 98 | + utxo0->set_amount(10000); |
| 99 | + |
| 100 | + auto hash0 = parse_hex("5727794fa2b94aa22a226e206130524201ede9b50e032526e713c848493a890f"); |
| 101 | + utxo0->mutable_out_point()->set_hash(hash0.data(), hash0.size()); |
| 102 | + utxo0->mutable_out_point()->set_index(0); |
| 103 | + utxo0->mutable_out_point()->set_sequence(0xfffffffd); |
| 104 | + |
| 105 | + // Sign |
| 106 | + auto txSinger = TransactionSigner<Transaction, TransactionBuilder>(std::move(input)); |
| 107 | + txSinger.transaction.lockTime = 0x00098971; |
| 108 | + auto result = txSinger.sign(); |
| 109 | + |
| 110 | + ASSERT_TRUE(result) << result.error();; |
| 111 | + auto signedTx = result.payload(); |
| 112 | + |
| 113 | + Data serialized; |
| 114 | + signedTx.encode(true, serialized); |
| 115 | + ASSERT_EQ(hex(serialized), |
| 116 | + "01000000" |
| 117 | + "0001" |
| 118 | + "01" |
| 119 | + "5727794fa2b94aa22a226e206130524201ede9b50e032526e713c848493a890f" "00000000" "00" "fdffffff" |
| 120 | + "02" |
| 121 | + "8813000000000000" "160014db746a75d9aae8995d135b1e19a04d7765242a8f" |
| 122 | + "a612000000000000" "160014ebae10950c8a35a506e0e265b928305233e802ab" |
| 123 | + "02" |
| 124 | + "483045022100bf1dcc37c2d3794e216b0b1cfcb04c7f49ef360ae941e46dc9b168f54f5447fe02205a0912bf3a3c0ac0e490c665bcde5239f553c013b2447a6fb5df6387ac029c8c41" |
| 125 | + "2103e00b5dec8078d526fba090247bd92db6b67a4dd1953b788cea9b52de9471b8cf" |
| 126 | + "71890900" |
| 127 | + ); |
| 128 | +} |
| 129 | + |
0 commit comments