From 4a231301415bdaa25a3ff9937497be41e7b55fc1 Mon Sep 17 00:00:00 2001 From: Wenfeng Wang Date: Tue, 3 Mar 2020 21:43:00 +0800 Subject: [PATCH 01/14] Add BTG coin definition --- coins.json | 28 ++++++++++++++++++++++ docs/coins.md | 1 + include/TrustWalletCore/TWCoinType.h | 1 + tests/BitcoinGold/TWCoinTypeTests.cpp | 34 +++++++++++++++++++++++++++ 4 files changed, 64 insertions(+) create mode 100644 tests/BitcoinGold/TWCoinTypeTests.cpp diff --git a/coins.json b/coins.json index 18047f66535..feb7fe0a8bc 100644 --- a/coins.json +++ b/coins.json @@ -100,6 +100,34 @@ "clientDocs": "https://github.com/trezor/blockbook/blob/master/docs/api.md" } }, + { + "id": "bitcoingold", + "name": "Bitcoin Gold", + "symbol": "BTG", + "decimals": 8, + "blockchain": "Bitcoin", + "derivationPath": "m/84'/156'/0'/0/0", + "curve": "secp256k1", + "publicKeyType": "secp256k1", + "p2pkhPrefix": 38, + "p2shPrefix": 23, + "hrp": "btg", + "publicKeyHasher": "sha256ripemd", + "base58Hasher": "sha256d", + "xpub": "zpub", + "xprv": "zprv", + "explorer": { + "url": "https://explorer.bitcoingold.org/insight", + "txPath": "/tx/", + "accountPath": "/address/" + }, + "info": { + "url": "https://bitcoingold.org", + "client": "https://github.com/trezor/blockbook", + "clientPublic": "", + "clientDocs": "https://github.com/trezor/blockbook/blob/master/docs/api.md" + } + }, { "id": "callisto", "name": "Callisto", diff --git a/docs/coins.md b/docs/coins.md index 2fae8caf2e8..9c3b68dccdd 100644 --- a/docs/coins.md +++ b/docs/coins.md @@ -22,6 +22,7 @@ This list is generated from [./coins.json](../coins.json) | 144 | XRP | XRP | | | | 145 | Bitcoin Cash | BCH | | | | 148 | Stellar | XLM | | | +| 156 | Bitcoin Gold | BTG | | | | 165 | Nano | NANO | | | | 175 | Ravencoin | RVN | | | | 178 | POA Network | POA | | | diff --git a/include/TrustWalletCore/TWCoinType.h b/include/TrustWalletCore/TWCoinType.h index 0c2a152cec0..1e5e7ed053f 100644 --- a/include/TrustWalletCore/TWCoinType.h +++ b/include/TrustWalletCore/TWCoinType.h @@ -27,6 +27,7 @@ enum TWCoinType { TWCoinTypeBinance = 714, TWCoinTypeBitcoin = 0, TWCoinTypeBitcoinCash = 145, + TWCoinTypeBitcoinGold = 156, TWCoinTypeCallisto = 820, TWCoinTypeCardano = 1815, // Note: Cardano Shelley testnet uses purpose 1852 (not 44) 1852/1815 TWCoinTypeCosmos = 118, diff --git a/tests/BitcoinGold/TWCoinTypeTests.cpp b/tests/BitcoinGold/TWCoinTypeTests.cpp new file mode 100644 index 00000000000..ddbad39b6ec --- /dev/null +++ b/tests/BitcoinGold/TWCoinTypeTests.cpp @@ -0,0 +1,34 @@ +// Copyright © 2017-2020 Trust Wallet. +// +// This file is part of Trust. The full Trust copyright notice, including +// terms governing use, modification, and redistribution, is contained in the +// file LICENSE at the root of the source code distribution tree. +// +// This is a GENERATED FILE, changes made here MAY BE LOST. +// Generated one-time (codegen/bin/cointests) +// + +#include "../interface/TWTestUtilities.h" +#include +#include + + +TEST(TWBitcoinGoldCoinType, TWCoinType) { + auto symbol = WRAPS(TWCoinTypeConfigurationGetSymbol(TWCoinTypeBitcoinGold)); + auto txId = TWStringCreateWithUTF8Bytes("2f807d7734de35d2236a1b3d8704eb12954f5f82ea66987949b10e94d9999b23"); + auto txUrl = WRAPS(TWCoinTypeConfigurationGetTransactionURL(TWCoinTypeBitcoinGold, txId)); + auto accId = TWStringCreateWithUTF8Bytes("GJjz2Du9BoJQ3CPcoyVTHUJZSj62i1693U"); + auto accUrl = WRAPS(TWCoinTypeConfigurationGetAccountURL(TWCoinTypeBitcoinGold, accId)); + auto id = WRAPS(TWCoinTypeConfigurationGetID(TWCoinTypeBitcoinGold)); + auto name = WRAPS(TWCoinTypeConfigurationGetName(TWCoinTypeBitcoinGold)); + + ASSERT_EQ(TWCoinTypeConfigurationGetDecimals(TWCoinTypeBitcoinGold), 8); + ASSERT_EQ(TWBlockchainBitcoin, TWCoinTypeBlockchain(TWCoinTypeBitcoinGold)); + ASSERT_EQ(23, TWCoinTypeP2shPrefix(TWCoinTypeBitcoinGold)); + ASSERT_EQ(0, TWCoinTypeStaticPrefix(TWCoinTypeBitcoinGold)); + assertStringsEqual(symbol, "BTG"); + assertStringsEqual(txUrl, "https://explorer.bitcoingold.org/insight/tx/2f807d7734de35d2236a1b3d8704eb12954f5f82ea66987949b10e94d9999b23"); + assertStringsEqual(accUrl, "https://explorer.bitcoingold.org/insight/address/GJjz2Du9BoJQ3CPcoyVTHUJZSj62i1693U"); + assertStringsEqual(id, "bitcoingold"); + assertStringsEqual(name, "Bitcoin Gold"); +} From d14e4b8d94d36c5f1f8b83ce7ea14f3e015eced4 Mon Sep 17 00:00:00 2001 From: Wenfeng Wang Date: Sat, 21 Mar 2020 14:05:31 +0800 Subject: [PATCH 02/14] Add c++ implementation and tests --- src/BitcoinGold/Address.cpp | 10 +++ src/BitcoinGold/Address.h | 35 +++++++++ src/BitcoinGold/SegwitAddress.cpp | 10 +++ src/BitcoinGold/SegwitAddress.h | 45 ++++++++++++ src/BitcoinGold/Signer.cpp | 12 ++++ src/BitcoinGold/Signer.h | 32 +++++++++ tests/BitcoinGold/TWAddressTests.cpp | 34 +++++++++ tests/BitcoinGold/TWSegwitAddressTests.cpp | 73 +++++++++++++++++++ tests/BitcoinGold/TWSignerTests.cpp | 84 ++++++++++++++++++++++ 9 files changed, 335 insertions(+) create mode 100644 src/BitcoinGold/Address.cpp create mode 100644 src/BitcoinGold/Address.h create mode 100644 src/BitcoinGold/SegwitAddress.cpp create mode 100644 src/BitcoinGold/SegwitAddress.h create mode 100644 src/BitcoinGold/Signer.cpp create mode 100644 src/BitcoinGold/Signer.h create mode 100644 tests/BitcoinGold/TWAddressTests.cpp create mode 100644 tests/BitcoinGold/TWSegwitAddressTests.cpp create mode 100644 tests/BitcoinGold/TWSignerTests.cpp diff --git a/src/BitcoinGold/Address.cpp b/src/BitcoinGold/Address.cpp new file mode 100644 index 00000000000..eec0670f30c --- /dev/null +++ b/src/BitcoinGold/Address.cpp @@ -0,0 +1,10 @@ +// Copyright © 2017-2020 Trust Wallet. +// +// This file is part of Trust. The full Trust copyright notice, including +// terms governing use, modification, and redistribution, is contained in the +// file LICENSE at the root of the source code distribution tree. + +#include "Address.h" + +using namespace TW::BitcoinGold; + diff --git a/src/BitcoinGold/Address.h b/src/BitcoinGold/Address.h new file mode 100644 index 00000000000..c35304f8f94 --- /dev/null +++ b/src/BitcoinGold/Address.h @@ -0,0 +1,35 @@ +// Copyright © 2017-2020 Trust Wallet. +// +// This file is part of Trust. The full Trust copyright notice, including +// terms governing use, modification, and redistribution, is contained in the +// file LICENSE at the root of the source code distribution tree. + +#pragma once + +#include "../Base58Address.h" +#include "../Data.h" +#include "../PublicKey.h" + +#include + +namespace TW::BitcoinGold { + +/// BitcoinGold base58 address +class Address : public TW::Base58Address<21> { + public: + /// Initializes a address with a string representation. + explicit Address(const std::string& string) : TW::Base58Address<21>(string) {} + + /// Initializes a address with a collection of bytes. + explicit Address(const Data& data) : TW::Base58Address<21>(data) {} + + /// Initializes a address with a public key and a prefix. + Address(const PublicKey& publicKey, byte prefix) : TW::Base58Address<21>(publicKey, {prefix}) {} +}; + +} // namespace TW::BitcoinGold + +/// Wrapper for C interface. +struct TWBitcoinGoldAddress { + TW::BitcoinGold::Address impl; +}; diff --git a/src/BitcoinGold/SegwitAddress.cpp b/src/BitcoinGold/SegwitAddress.cpp new file mode 100644 index 00000000000..60d14044de2 --- /dev/null +++ b/src/BitcoinGold/SegwitAddress.cpp @@ -0,0 +1,10 @@ +// Copyright © 2017-2020 Trust Wallet. +// +// This file is part of Trust. The full Trust copyright notice, including +// terms governing use, modification, and redistribution, is contained in the +// file LICENSE at the root of the source code distribution tree. + +#include "SegwitAddress.h" + +using namespace TW::BitcoinGold; + diff --git a/src/BitcoinGold/SegwitAddress.h b/src/BitcoinGold/SegwitAddress.h new file mode 100644 index 00000000000..e85e4036c05 --- /dev/null +++ b/src/BitcoinGold/SegwitAddress.h @@ -0,0 +1,45 @@ +// Copyright © 2017-2020 Trust Wallet. +// +// This file is part of Trust. The full Trust copyright notice, including +// terms governing use, modification, and redistribution, is contained in the +// file LICENSE at the root of the source code distribution tree. + +#pragma once + +#include "../Bitcoin/SegwitAddress.h" + +#include + +namespace TW::BitcoinGold { + +/// BitcoinGold segwit address +class SegwitAddress : public TW::Bitcoin::SegwitAddress { + + public: + + /// Initializes a Bech32 address with a human-readable part, a witness + /// version, and a witness program. + SegwitAddress(std::string hrp, int witver, std::vector witprog) + : TW::Bitcoin::SegwitAddress(hrp, witver, witprog) {} + + /// Initializes a Bech32 address with a public key and a HRP prefix. + SegwitAddress(const PublicKey& publicKey, int witver, std::string hrp) + : TW::Bitcoin::SegwitAddress(publicKey, witver, hrp) {} + + bool operator==(const SegwitAddress& rhs) const { + return hrp == rhs.hrp && witnessVersion == rhs.witnessVersion && + witnessProgram == rhs.witnessProgram; + } + + private: + SegwitAddress() = default; +}; + +} // namespace TW::BitcoinGold + +/// Wrapper for C interface. +struct TWBitcoinGoldSegwitAddress { + TW::BitcoinGold::SegwitAddress impl; +}; + + diff --git a/src/BitcoinGold/Signer.cpp b/src/BitcoinGold/Signer.cpp new file mode 100644 index 00000000000..2676ff614e3 --- /dev/null +++ b/src/BitcoinGold/Signer.cpp @@ -0,0 +1,12 @@ +// Copyright © 2017-2020 Trust Wallet. +// +// This file is part of Trust. The full Trust copyright notice, including +// terms governing use, modification, and redistribution, is contained in the +// file LICENSE at the root of the source code distribution tree. + +#include "Signer.h" +#include "Address.h" +#include "../PublicKey.h" + +using namespace TW; +using namespace TW::BitcoinGold; diff --git a/src/BitcoinGold/Signer.h b/src/BitcoinGold/Signer.h new file mode 100644 index 00000000000..f8ba59b35bc --- /dev/null +++ b/src/BitcoinGold/Signer.h @@ -0,0 +1,32 @@ +// Copyright © 2017-2020 Trust Wallet. +// +// This file is part of Trust. The full Trust copyright notice, including +// terms governing use, modification, and redistribution, is contained in the +// file LICENSE at the root of the source code distribution tree. + +#pragma once + +#include "../Data.h" +#include "../PrivateKey.h" +#include "../proto/BitcoinGold.pb.h" + +#include "../Bitcoin/Signer.h" + +namespace TW::BitcoinGold { + +/// Helper class that performs BitcoinGold transaction signing. +class Signer : public TW::Bitcoin::Signer { +public: + /// Hide default constructor + Signer() = delete; + + /// BitcoinGold transaction use Bitcoin signer + // static Proto::SigningOutput sign(const Proto::SigningInput& input) noexcept; +}; + +} // namespace TW::BitcoinGold + +/// Wrapper for C interface. +struct TWBitcoinGoldSigner { + TW::BitcoinGold::Signer impl; +}; diff --git a/tests/BitcoinGold/TWAddressTests.cpp b/tests/BitcoinGold/TWAddressTests.cpp new file mode 100644 index 00000000000..31cd50d2db9 --- /dev/null +++ b/tests/BitcoinGold/TWAddressTests.cpp @@ -0,0 +1,34 @@ +// Copyright © 2017-2020 Trust Wallet. +// +// This file is part of Trust. The full Trust copyright notice, including +// terms governing use, modification, and redistribution, is contained in the +// file LICENSE at the root of the source code distribution tree. +// + +#include "../interface/TWTestUtilities.h" +#include "BitcoinGold/Address.h" +#include "PrivateKey.h" +#include "PublicKey.h" +#include "HexCoding.h" +#include +#include + +using namespace TW; + +const char *PRIVATE_KEY = "CA6D1402199530A5D610A01A53505B6A344CF61B0CCB2902D5AEFBEA63C274BB"; +const char *ADDRESS = "GSGUyooxtCUVBonYV8AANp7FvKy3WTvpMR"; +const char *FAKEADDRESS = "GSGUyooxtCUVBonYV9AANp7FvKy3WTvpMR"; + +TEST(TWBitcoinGoldAddress, Valid) { + ASSERT_TRUE(BitcoinGold::Address::isValid(std::string(ADDRESS))); + ASSERT_FALSE(BitcoinGold::Address::isValid(std::string(FAKEADDRESS))); +} + +TEST(TWBitcoinGoldAddress, PubkeyToAddress) { + const auto privateKey = PrivateKey(parse_hex(PRIVATE_KEY)); + const auto publicKey = privateKey.getPublicKey(TWPublicKeyTypeSECP256k1); + + /// construct with public key + auto address = BitcoinGold::Address(PublicKey(publicKey), 38); + ASSERT_EQ(address.string(), ADDRESS); +} diff --git a/tests/BitcoinGold/TWSegwitAddressTests.cpp b/tests/BitcoinGold/TWSegwitAddressTests.cpp new file mode 100644 index 00000000000..7da06633ec2 --- /dev/null +++ b/tests/BitcoinGold/TWSegwitAddressTests.cpp @@ -0,0 +1,73 @@ +// Copyright © 2017-2020 Trust Wallet. +// +// This file is part of Trust. The full Trust copyright notice, including +// terms governing use, modification, and redistribution, is contained in the +// file LICENSE at the root of the source code distribution tree. +// + +#include "../interface/TWTestUtilities.h" +#include "BitcoinGold/SegwitAddress.h" +#include "PrivateKey.h" +#include "PublicKey.h" +#include "HexCoding.h" +#include +#include + +using namespace TW; + + +// { +// "address": "btg1qtesn92ddy8m5yvypgsdtft3zj5qldj9g2u52sk", +// "scriptPubKey": "00145e6132a9ad21f7423081441ab4ae229501f6c8a8", +// "ismine": true, +// "iswatchonly": false, +// "isscript": false, +// "iswitness": true, +// "witness_version": 0, +// "witness_program": "5e6132a9ad21f7423081441ab4ae229501f6c8a8", +// "pubkey": "02f74712b5d765a73b52a14c1e113f2ef3f9502d09d5987ee40f53828cfe68b9a6", +// "label": "", +// "timestamp": 1520169519, +// "hdkeypath": "m/0'/0'/7'", +// "hdseedid": "78087f46f7ca96b35bd3a5ab2331f7c96780166d", +// "hdmasterkeyid": "78087f46f7ca96b35bd3a5ab2331f7c96780166d", +// "labels": [ +// { +// "name": "", +// "purpose": "receive" +// } +// ] +// } + +TEST(TWBitcoinGoldSegwitAddress, Valid) { + ASSERT_TRUE(BitcoinGold::SegwitAddress::isValid("btg1qtesn92ddy8m5yvypgsdtft3zj5qldj9g2u52sk")); + ASSERT_FALSE(BitcoinGold::SegwitAddress::isValid("btg1qtesn92ddy8m5yvypgsdtft3zj5qldj9g2u52sl")); +} + +/// Initializes a Bech32 address with a human-readable part, a witness +/// version, and a witness program. +TEST(TWBitcoinGoldSegwitAddress, WitnessProgramToAddress) { + auto address = BitcoinGold::SegwitAddress("btg", 0, parse_hex("5e6132a9ad21f7423081441ab4ae229501f6c8a8")); + + ASSERT_TRUE(BitcoinGold::SegwitAddress::isValid(address.string())); + ASSERT_EQ(address.string(), "btg1qtesn92ddy8m5yvypgsdtft3zj5qldj9g2u52sk"); +} + +/// Initializes a Bech32 address with a public key and a HRP prefix. +TEST(TWBitcoinGoldSegwitAddress, PubkeyToAddress) { + const auto publicKey = PublicKey(parse_hex("02f74712b5d765a73b52a14c1e113f2ef3f9502d09d5987ee40f53828cfe68b9a6"), TWPublicKeyTypeSECP256k1); + + /// construct with public key + auto address = BitcoinGold::SegwitAddress(publicKey, 0, "btg"); + + ASSERT_TRUE(BitcoinGold::SegwitAddress::isValid(address.string())); + ASSERT_EQ(address.string(), "btg1qtesn92ddy8m5yvypgsdtft3zj5qldj9g2u52sk"); +} + +/// Decodes a SegWit address. +TEST(TWBitcoinGoldSegwitAddress, Decode) { + std::pair result = BitcoinGold::SegwitAddress::decode("btg1qtesn92ddy8m5yvypgsdtft3zj5qldj9g2u52sk"); + + ASSERT_TRUE(result.second); + ASSERT_EQ(result.first.string(), "btg1qtesn92ddy8m5yvypgsdtft3zj5qldj9g2u52sk"); +} diff --git a/tests/BitcoinGold/TWSignerTests.cpp b/tests/BitcoinGold/TWSignerTests.cpp new file mode 100644 index 00000000000..7517b50d8b4 --- /dev/null +++ b/tests/BitcoinGold/TWSignerTests.cpp @@ -0,0 +1,84 @@ +// Copyright © 2017-2020 Trust Wallet. +// +// This file is part of Trust. The full Trust copyright notice, including +// terms governing use, modification, and redistribution, is contained in the +// file LICENSE at the root of the source code distribution tree. + +#include +#include +#include +#include + +#include "BitcoinGold/SegwitAddress.h" +#include "proto/Bitcoin.pb.h" +#include "Bitcoin/OutPoint.h" +#include "Bitcoin/Script.h" +#include "Bitcoin/Transaction.h" +#include "Bitcoin/TransactionBuilder.h" +#include "Bitcoin/TransactionSigner.h" +#include "HexCoding.h" +#include "../interface/TWTestUtilities.h" + +using namespace TW; +using namespace TW::Bitcoin; + +TEST(TWBitcoinGoldSigner, SignTransaction) { + const int64_t amount = 10000; + + // Setup input + Proto::SigningInput input; + input.set_coin_type(TWCoinTypeBitcoinGold); + input.set_hash_type(TWBitcoinSigHashTypeFork | TWBitcoinSigHashTypeAll | (79 << 8)); + input.set_amount(amount); + input.set_byte_fee(1); + input.set_to_address("btg1qmd6x5awe4t5fjhgntv0pngzdwajjg250wxdcs0"); + input.set_change_address("btg1qawhpp9gv3g662phqufjmj2ps2ge7sq4thy5g07"); + + auto utxoKey0 = parse_hex("cbe13a79b82ec7f8871b336a64fd8d531f598e7c9022e29c67e824cfd54af57f"); + input.add_private_key(utxoKey0.data(), utxoKey0.size()); + + + auto scriptPub1 = Script(parse_hex("0014db746a75d9aae8995d135b1e19a04d7765242a8f")); + auto scriptHash = std::vector(); + scriptPub1.matchPayToWitnessPublicKeyHash(scriptHash); + auto scriptHashHex = hex(scriptHash.begin(), scriptHash.end()); + + auto redeemScript = Script::buildPayToPublicKeyHash(scriptHash); + auto scriptString = std::string(redeemScript.bytes.begin(), redeemScript.bytes.end()); + (*input.mutable_scripts())[scriptHashHex] = scriptString; + + auto utxo0 = input.add_utxo(); + auto utxo0Script = parse_hex("0014d53cae7c6fb6c8efe4fd8bfecea36534105b1674"); + utxo0->set_script(utxo0Script.data(), utxo0Script.size()); + utxo0->set_amount(99000); + + auto hash0 = parse_hex("1d4653041a1915b3a52d47aeaa119c8f79ed7634a93692a6e811173067464f03"); + utxo0->mutable_out_point()->set_hash(hash0.data(), hash0.size()); + utxo0->mutable_out_point()->set_index(1); + utxo0->mutable_out_point()->set_sequence(0xfffffffd); + + // Sign + auto txSinger = TransactionSigner(std::move(input)); + txSinger.transaction.lockTime = 0x00098971; + auto result = txSinger.sign(); + + ASSERT_TRUE(result) << result.error();; + auto signedTx = result.payload(); + + Data serialized; + signedTx.encode(true, serialized); + ASSERT_EQ(hex(serialized), + "01000000" + "0001" + "01" + "1d4653041a1915b3a52d47aeaa119c8f79ed7634a93692a6e811173067464f03" "01000000" "00" "fdffffff" + "02" + "1027000000000000" "160014db746a75d9aae8995d135b1e19a04d7765242a8f" + "c65a010000000000" "160014ebae10950c8a35a506e0e265b928305233e802ab" + "02" + "473044022029b81a6b8f57f76aaf510d8a222ca835bd806936e329aead433f120007d6847002203afa611ff7823ec2a6770359901b0cacf56527cbf947b226ed86b61811545e2b41" + "2103e00b5dec8078d526fba090247bd92db6b67a4dd1953b788cea9b52de9471b8cf" + "71890900" + ); +} + From 368fdbf95f56adf376c2edfedebfb97782ec7cc6 Mon Sep 17 00:00:00 2001 From: Wenfeng Wang Date: Sat, 21 Mar 2020 14:10:16 +0800 Subject: [PATCH 03/14] Add proto models definition --- src/proto/BitcoinGold.proto | 12 ++++++++++++ 1 file changed, 12 insertions(+) create mode 100644 src/proto/BitcoinGold.proto diff --git a/src/proto/BitcoinGold.proto b/src/proto/BitcoinGold.proto new file mode 100644 index 00000000000..538e038cc4e --- /dev/null +++ b/src/proto/BitcoinGold.proto @@ -0,0 +1,12 @@ +// Copyright © 2017-2020 Trust Wallet. +// +// This file is part of Trust. The full Trust copyright notice, including +// terms governing use, modification, and redistribution, is contained in the +// file LICENSE at the root of the source code distribution tree. + +syntax = "proto3"; + +package TW.BitcoinGold.Proto; +option java_package = "wallet.core.jni.proto"; + +import "Bitcoin.proto"; From 7481242450d09ac16c8f3ceefdfbcfbea28553fe Mon Sep 17 00:00:00 2001 From: Wenfeng Wang Date: Tue, 24 Mar 2020 21:49:43 +0800 Subject: [PATCH 04/14] Remove unused comments --- tests/BitcoinGold/TWSegwitAddressTests.cpp | 24 ---------------------- 1 file changed, 24 deletions(-) diff --git a/tests/BitcoinGold/TWSegwitAddressTests.cpp b/tests/BitcoinGold/TWSegwitAddressTests.cpp index 7da06633ec2..d05147caa9b 100644 --- a/tests/BitcoinGold/TWSegwitAddressTests.cpp +++ b/tests/BitcoinGold/TWSegwitAddressTests.cpp @@ -15,30 +15,6 @@ using namespace TW; - -// { -// "address": "btg1qtesn92ddy8m5yvypgsdtft3zj5qldj9g2u52sk", -// "scriptPubKey": "00145e6132a9ad21f7423081441ab4ae229501f6c8a8", -// "ismine": true, -// "iswatchonly": false, -// "isscript": false, -// "iswitness": true, -// "witness_version": 0, -// "witness_program": "5e6132a9ad21f7423081441ab4ae229501f6c8a8", -// "pubkey": "02f74712b5d765a73b52a14c1e113f2ef3f9502d09d5987ee40f53828cfe68b9a6", -// "label": "", -// "timestamp": 1520169519, -// "hdkeypath": "m/0'/0'/7'", -// "hdseedid": "78087f46f7ca96b35bd3a5ab2331f7c96780166d", -// "hdmasterkeyid": "78087f46f7ca96b35bd3a5ab2331f7c96780166d", -// "labels": [ -// { -// "name": "", -// "purpose": "receive" -// } -// ] -// } - TEST(TWBitcoinGoldSegwitAddress, Valid) { ASSERT_TRUE(BitcoinGold::SegwitAddress::isValid("btg1qtesn92ddy8m5yvypgsdtft3zj5qldj9g2u52sk")); ASSERT_FALSE(BitcoinGold::SegwitAddress::isValid("btg1qtesn92ddy8m5yvypgsdtft3zj5qldj9g2u52sl")); From 91095cf29347230ef0cc8fed081bf5fb1391c962 Mon Sep 17 00:00:00 2001 From: Wenfeng Wang Date: Tue, 24 Mar 2020 21:51:41 +0800 Subject: [PATCH 05/14] Add TWCoin type BitcoinGold into entry --- src/Bitcoin/Entry.cpp | 2 ++ src/Bitcoin/Entry.h | 1 + 2 files changed, 3 insertions(+) diff --git a/src/Bitcoin/Entry.cpp b/src/Bitcoin/Entry.cpp index a64de869007..42c39c99cd1 100644 --- a/src/Bitcoin/Entry.cpp +++ b/src/Bitcoin/Entry.cpp @@ -22,6 +22,7 @@ bool Entry::validateAddress(TWCoinType coin, const string& address, TW::byte p2p case TWCoinTypeMonacoin: case TWCoinTypeQtum: case TWCoinTypeViacoin: + case TWCoinTypeBitcoinGold: return SegwitAddress::isValid(address, hrp) || Address::isValid(address, {{p2pkh}, {p2sh}}); @@ -60,6 +61,7 @@ string Entry::deriveAddress(TWCoinType coin, const PublicKey& publicKey, TW::byt case TWCoinTypeDigiByte: case TWCoinTypeLitecoin: case TWCoinTypeViacoin: + case TWCoinTypeBitcoinGold: return SegwitAddress(publicKey, 0, hrp).string(); case TWCoinTypeBitcoinCash: diff --git a/src/Bitcoin/Entry.h b/src/Bitcoin/Entry.h index 51e0de06ad8..09d5ff27d81 100644 --- a/src/Bitcoin/Entry.h +++ b/src/Bitcoin/Entry.h @@ -18,6 +18,7 @@ class Entry: public CoinEntry { return { TWCoinTypeBitcoin, TWCoinTypeBitcoinCash, + TWCoinTypeBitcoinGold, TWCoinTypeDash, TWCoinTypeDigiByte, TWCoinTypeDogecoin, From 673d01132fe7fcad6fe13fe7a494d622e3e62df9 Mon Sep 17 00:00:00 2001 From: Wenfeng Wang Date: Thu, 2 Apr 2020 23:11:31 +0800 Subject: [PATCH 06/14] Remove unused import --- src/proto/BitcoinGold.proto | 1 - 1 file changed, 1 deletion(-) diff --git a/src/proto/BitcoinGold.proto b/src/proto/BitcoinGold.proto index 538e038cc4e..10fcbcd8b60 100644 --- a/src/proto/BitcoinGold.proto +++ b/src/proto/BitcoinGold.proto @@ -9,4 +9,3 @@ syntax = "proto3"; package TW.BitcoinGold.Proto; option java_package = "wallet.core.jni.proto"; -import "Bitcoin.proto"; From bc6e9f2a40f7b72b2d725750999af16a78fbf2a8 Mon Sep 17 00:00:00 2001 From: Wang Wenfeng Date: Mon, 20 Apr 2020 09:29:02 +0800 Subject: [PATCH 07/14] add bitcoingold address test in swift test case --- swift/Tests/CoinAddressDerivationTests.swift | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/swift/Tests/CoinAddressDerivationTests.swift b/swift/Tests/CoinAddressDerivationTests.swift index 81b55b548ed..1286a1eba2c 100644 --- a/swift/Tests/CoinAddressDerivationTests.swift +++ b/swift/Tests/CoinAddressDerivationTests.swift @@ -34,6 +34,9 @@ class CoinAddressDerivationTests: XCTestCase { case .bitcoinCash: let expectedResult = "bitcoincash:qpzl3jxkzgvfd9flnd26leud5duv795fnv7vuaha70" AssetCoinDerivation(coin, expectedResult, derivedAddress, address) + case .bitcoingold: + let expectedResult = "btg1qwz9sed0k4neu6ycrudzkca6cnqe3zweq35hvtg" + AssetCoinDerivation(coin, expectedResult, derivedAddress, address) case .callisto: let expectedResult = "0x3E6FFC80745E6669135a76F4A7ce6BCF02436e04" AssetCoinDerivation(coin, expectedResult, derivedAddress, address) @@ -190,8 +193,6 @@ class CoinAddressDerivationTests: XCTestCase { case .filecoin: let expectedResult = "f1zzykebxldfcakj5wdb5n3n7priul522fnmjzori" AssetCoinDerivation(coin, expectedResult, derivedAddress, address) - @unknown default: - fatalError() } } } From 334b367434de3659401e40eb4db16b8dbe31d2fa Mon Sep 17 00:00:00 2001 From: Wang Wenfeng Date: Mon, 20 Apr 2020 09:48:01 +0800 Subject: [PATCH 08/14] add bitcoingold address test in android test case --- .../core/app/blockchains/CoinAddressDerivationTests.kt | 1 + 1 file changed, 1 insertion(+) diff --git a/android/app/src/androidTest/java/com/trustwallet/core/app/blockchains/CoinAddressDerivationTests.kt b/android/app/src/androidTest/java/com/trustwallet/core/app/blockchains/CoinAddressDerivationTests.kt index 8bfe4dd5268..43ec26c494a 100644 --- a/android/app/src/androidTest/java/com/trustwallet/core/app/blockchains/CoinAddressDerivationTests.kt +++ b/android/app/src/androidTest/java/com/trustwallet/core/app/blockchains/CoinAddressDerivationTests.kt @@ -29,6 +29,7 @@ class CoinAddressDerivationTests { BINANCE -> assertEquals("bnb12vtaxl9952zm6rwf7v8jerq74pvaf77fcmvzhw", address) BITCOIN -> assertEquals("bc1quvuarfksewfeuevuc6tn0kfyptgjvwsvrprk9d", address) BITCOINCASH -> assertEquals("bitcoincash:qpzl3jxkzgvfd9flnd26leud5duv795fnv7vuaha70", address) + BITCOINGOLD -> assertEquals("btg1qwz9sed0k4neu6ycrudzkca6cnqe3zweq35hvtg", address) CALLISTO -> assertEquals("0x3E6FFC80745E6669135a76F4A7ce6BCF02436e04", address) DASH -> assertEquals("XqHiz8EXYbTAtBEYs4pWTHh7ipEDQcNQeT", address) DIGIBYTE -> assertEquals("dgb1qtjgmerfqwdffyf8ghcrkgy52cghsqptynmyswu", address) From 8c51d6eb94e2aa6a54a7b7a3ff6f7c9fe07894d8 Mon Sep 17 00:00:00 2001 From: h4x3rotab Date: Mon, 20 Apr 2020 19:30:45 +0000 Subject: [PATCH 09/14] Fix typo in swift test --- swift/Tests/CoinAddressDerivationTests.swift | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/swift/Tests/CoinAddressDerivationTests.swift b/swift/Tests/CoinAddressDerivationTests.swift index 1286a1eba2c..470062eee70 100644 --- a/swift/Tests/CoinAddressDerivationTests.swift +++ b/swift/Tests/CoinAddressDerivationTests.swift @@ -34,7 +34,7 @@ class CoinAddressDerivationTests: XCTestCase { case .bitcoinCash: let expectedResult = "bitcoincash:qpzl3jxkzgvfd9flnd26leud5duv795fnv7vuaha70" AssetCoinDerivation(coin, expectedResult, derivedAddress, address) - case .bitcoingold: + case .bitcoinGold: let expectedResult = "btg1qwz9sed0k4neu6ycrudzkca6cnqe3zweq35hvtg" AssetCoinDerivation(coin, expectedResult, derivedAddress, address) case .callisto: From 4461644172f8973968fd3808649750a30593f9d3 Mon Sep 17 00:00:00 2001 From: Wang Wenfeng Date: Thu, 23 Apr 2020 09:25:24 +0800 Subject: [PATCH 10/14] revert unknow coin handler in swift --- swift/Tests/CoinAddressDerivationTests.swift | 2 ++ 1 file changed, 2 insertions(+) diff --git a/swift/Tests/CoinAddressDerivationTests.swift b/swift/Tests/CoinAddressDerivationTests.swift index 470062eee70..594d030e919 100644 --- a/swift/Tests/CoinAddressDerivationTests.swift +++ b/swift/Tests/CoinAddressDerivationTests.swift @@ -193,6 +193,8 @@ class CoinAddressDerivationTests: XCTestCase { case .filecoin: let expectedResult = "f1zzykebxldfcakj5wdb5n3n7priul522fnmjzori" AssetCoinDerivation(coin, expectedResult, derivedAddress, address) + @unknown default: + fatalError() } } } From 0dc1a39637d35f3c9be337238694e0eab50554f0 Mon Sep 17 00:00:00 2001 From: Wang Wenfeng Date: Thu, 23 Apr 2020 10:48:49 +0800 Subject: [PATCH 11/14] replace magic number 38 with p2pkhPrefix in bitcoingold address test --- tests/BitcoinGold/TWAddressTests.cpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/tests/BitcoinGold/TWAddressTests.cpp b/tests/BitcoinGold/TWAddressTests.cpp index 31cd50d2db9..d78a3c58881 100644 --- a/tests/BitcoinGold/TWAddressTests.cpp +++ b/tests/BitcoinGold/TWAddressTests.cpp @@ -10,6 +10,7 @@ #include "PrivateKey.h" #include "PublicKey.h" #include "HexCoding.h" +#include "Coin.h" #include #include @@ -29,6 +30,6 @@ TEST(TWBitcoinGoldAddress, PubkeyToAddress) { const auto publicKey = privateKey.getPublicKey(TWPublicKeyTypeSECP256k1); /// construct with public key - auto address = BitcoinGold::Address(PublicKey(publicKey), 38); + auto address = BitcoinGold::Address(PublicKey(publicKey), p2pkhPrefix(TWCoinTypeBitcoinGold)); ASSERT_EQ(address.string(), ADDRESS); } From ec1e7863367efa878af2995dcc45f4db98abf66d Mon Sep 17 00:00:00 2001 From: Wang Wenfeng Date: Thu, 23 Apr 2020 14:33:05 +0800 Subject: [PATCH 12/14] Remove unnecessary implementation in Bitcoin Gold --- src/BitcoinGold/Address.cpp | 10 ----- src/BitcoinGold/Address.h | 35 ----------------- src/BitcoinGold/SegwitAddress.cpp | 10 ----- src/BitcoinGold/SegwitAddress.h | 45 ---------------------- src/BitcoinGold/Signer.cpp | 12 ------ src/BitcoinGold/Signer.h | 32 --------------- tests/BitcoinGold/TWAddressTests.cpp | 8 ++-- tests/BitcoinGold/TWSegwitAddressTests.cpp | 16 ++++---- tests/BitcoinGold/TWSignerTests.cpp | 2 +- 9 files changed, 13 insertions(+), 157 deletions(-) delete mode 100644 src/BitcoinGold/Address.cpp delete mode 100644 src/BitcoinGold/Address.h delete mode 100644 src/BitcoinGold/SegwitAddress.cpp delete mode 100644 src/BitcoinGold/SegwitAddress.h delete mode 100644 src/BitcoinGold/Signer.cpp delete mode 100644 src/BitcoinGold/Signer.h diff --git a/src/BitcoinGold/Address.cpp b/src/BitcoinGold/Address.cpp deleted file mode 100644 index eec0670f30c..00000000000 --- a/src/BitcoinGold/Address.cpp +++ /dev/null @@ -1,10 +0,0 @@ -// Copyright © 2017-2020 Trust Wallet. -// -// This file is part of Trust. The full Trust copyright notice, including -// terms governing use, modification, and redistribution, is contained in the -// file LICENSE at the root of the source code distribution tree. - -#include "Address.h" - -using namespace TW::BitcoinGold; - diff --git a/src/BitcoinGold/Address.h b/src/BitcoinGold/Address.h deleted file mode 100644 index c35304f8f94..00000000000 --- a/src/BitcoinGold/Address.h +++ /dev/null @@ -1,35 +0,0 @@ -// Copyright © 2017-2020 Trust Wallet. -// -// This file is part of Trust. The full Trust copyright notice, including -// terms governing use, modification, and redistribution, is contained in the -// file LICENSE at the root of the source code distribution tree. - -#pragma once - -#include "../Base58Address.h" -#include "../Data.h" -#include "../PublicKey.h" - -#include - -namespace TW::BitcoinGold { - -/// BitcoinGold base58 address -class Address : public TW::Base58Address<21> { - public: - /// Initializes a address with a string representation. - explicit Address(const std::string& string) : TW::Base58Address<21>(string) {} - - /// Initializes a address with a collection of bytes. - explicit Address(const Data& data) : TW::Base58Address<21>(data) {} - - /// Initializes a address with a public key and a prefix. - Address(const PublicKey& publicKey, byte prefix) : TW::Base58Address<21>(publicKey, {prefix}) {} -}; - -} // namespace TW::BitcoinGold - -/// Wrapper for C interface. -struct TWBitcoinGoldAddress { - TW::BitcoinGold::Address impl; -}; diff --git a/src/BitcoinGold/SegwitAddress.cpp b/src/BitcoinGold/SegwitAddress.cpp deleted file mode 100644 index 60d14044de2..00000000000 --- a/src/BitcoinGold/SegwitAddress.cpp +++ /dev/null @@ -1,10 +0,0 @@ -// Copyright © 2017-2020 Trust Wallet. -// -// This file is part of Trust. The full Trust copyright notice, including -// terms governing use, modification, and redistribution, is contained in the -// file LICENSE at the root of the source code distribution tree. - -#include "SegwitAddress.h" - -using namespace TW::BitcoinGold; - diff --git a/src/BitcoinGold/SegwitAddress.h b/src/BitcoinGold/SegwitAddress.h deleted file mode 100644 index e85e4036c05..00000000000 --- a/src/BitcoinGold/SegwitAddress.h +++ /dev/null @@ -1,45 +0,0 @@ -// Copyright © 2017-2020 Trust Wallet. -// -// This file is part of Trust. The full Trust copyright notice, including -// terms governing use, modification, and redistribution, is contained in the -// file LICENSE at the root of the source code distribution tree. - -#pragma once - -#include "../Bitcoin/SegwitAddress.h" - -#include - -namespace TW::BitcoinGold { - -/// BitcoinGold segwit address -class SegwitAddress : public TW::Bitcoin::SegwitAddress { - - public: - - /// Initializes a Bech32 address with a human-readable part, a witness - /// version, and a witness program. - SegwitAddress(std::string hrp, int witver, std::vector witprog) - : TW::Bitcoin::SegwitAddress(hrp, witver, witprog) {} - - /// Initializes a Bech32 address with a public key and a HRP prefix. - SegwitAddress(const PublicKey& publicKey, int witver, std::string hrp) - : TW::Bitcoin::SegwitAddress(publicKey, witver, hrp) {} - - bool operator==(const SegwitAddress& rhs) const { - return hrp == rhs.hrp && witnessVersion == rhs.witnessVersion && - witnessProgram == rhs.witnessProgram; - } - - private: - SegwitAddress() = default; -}; - -} // namespace TW::BitcoinGold - -/// Wrapper for C interface. -struct TWBitcoinGoldSegwitAddress { - TW::BitcoinGold::SegwitAddress impl; -}; - - diff --git a/src/BitcoinGold/Signer.cpp b/src/BitcoinGold/Signer.cpp deleted file mode 100644 index 2676ff614e3..00000000000 --- a/src/BitcoinGold/Signer.cpp +++ /dev/null @@ -1,12 +0,0 @@ -// Copyright © 2017-2020 Trust Wallet. -// -// This file is part of Trust. The full Trust copyright notice, including -// terms governing use, modification, and redistribution, is contained in the -// file LICENSE at the root of the source code distribution tree. - -#include "Signer.h" -#include "Address.h" -#include "../PublicKey.h" - -using namespace TW; -using namespace TW::BitcoinGold; diff --git a/src/BitcoinGold/Signer.h b/src/BitcoinGold/Signer.h deleted file mode 100644 index f8ba59b35bc..00000000000 --- a/src/BitcoinGold/Signer.h +++ /dev/null @@ -1,32 +0,0 @@ -// Copyright © 2017-2020 Trust Wallet. -// -// This file is part of Trust. The full Trust copyright notice, including -// terms governing use, modification, and redistribution, is contained in the -// file LICENSE at the root of the source code distribution tree. - -#pragma once - -#include "../Data.h" -#include "../PrivateKey.h" -#include "../proto/BitcoinGold.pb.h" - -#include "../Bitcoin/Signer.h" - -namespace TW::BitcoinGold { - -/// Helper class that performs BitcoinGold transaction signing. -class Signer : public TW::Bitcoin::Signer { -public: - /// Hide default constructor - Signer() = delete; - - /// BitcoinGold transaction use Bitcoin signer - // static Proto::SigningOutput sign(const Proto::SigningInput& input) noexcept; -}; - -} // namespace TW::BitcoinGold - -/// Wrapper for C interface. -struct TWBitcoinGoldSigner { - TW::BitcoinGold::Signer impl; -}; diff --git a/tests/BitcoinGold/TWAddressTests.cpp b/tests/BitcoinGold/TWAddressTests.cpp index d78a3c58881..77c192461bb 100644 --- a/tests/BitcoinGold/TWAddressTests.cpp +++ b/tests/BitcoinGold/TWAddressTests.cpp @@ -6,7 +6,7 @@ // #include "../interface/TWTestUtilities.h" -#include "BitcoinGold/Address.h" +#include "Bitcoin/Address.h" #include "PrivateKey.h" #include "PublicKey.h" #include "HexCoding.h" @@ -21,8 +21,8 @@ const char *ADDRESS = "GSGUyooxtCUVBonYV8AANp7FvKy3WTvpMR"; const char *FAKEADDRESS = "GSGUyooxtCUVBonYV9AANp7FvKy3WTvpMR"; TEST(TWBitcoinGoldAddress, Valid) { - ASSERT_TRUE(BitcoinGold::Address::isValid(std::string(ADDRESS))); - ASSERT_FALSE(BitcoinGold::Address::isValid(std::string(FAKEADDRESS))); + ASSERT_TRUE(Bitcoin::Address::isValid(std::string(ADDRESS))); + ASSERT_FALSE(Bitcoin::Address::isValid(std::string(FAKEADDRESS))); } TEST(TWBitcoinGoldAddress, PubkeyToAddress) { @@ -30,6 +30,6 @@ TEST(TWBitcoinGoldAddress, PubkeyToAddress) { const auto publicKey = privateKey.getPublicKey(TWPublicKeyTypeSECP256k1); /// construct with public key - auto address = BitcoinGold::Address(PublicKey(publicKey), p2pkhPrefix(TWCoinTypeBitcoinGold)); + auto address = Bitcoin::Address(PublicKey(publicKey), p2pkhPrefix(TWCoinTypeBitcoinGold)); ASSERT_EQ(address.string(), ADDRESS); } diff --git a/tests/BitcoinGold/TWSegwitAddressTests.cpp b/tests/BitcoinGold/TWSegwitAddressTests.cpp index d05147caa9b..9d2f4965807 100644 --- a/tests/BitcoinGold/TWSegwitAddressTests.cpp +++ b/tests/BitcoinGold/TWSegwitAddressTests.cpp @@ -6,7 +6,7 @@ // #include "../interface/TWTestUtilities.h" -#include "BitcoinGold/SegwitAddress.h" +#include "Bitcoin/SegwitAddress.h" #include "PrivateKey.h" #include "PublicKey.h" #include "HexCoding.h" @@ -16,16 +16,16 @@ using namespace TW; TEST(TWBitcoinGoldSegwitAddress, Valid) { - ASSERT_TRUE(BitcoinGold::SegwitAddress::isValid("btg1qtesn92ddy8m5yvypgsdtft3zj5qldj9g2u52sk")); - ASSERT_FALSE(BitcoinGold::SegwitAddress::isValid("btg1qtesn92ddy8m5yvypgsdtft3zj5qldj9g2u52sl")); + ASSERT_TRUE(Bitcoin::SegwitAddress::isValid("btg1qtesn92ddy8m5yvypgsdtft3zj5qldj9g2u52sk")); + ASSERT_FALSE(Bitcoin::SegwitAddress::isValid("btg1qtesn92ddy8m5yvypgsdtft3zj5qldj9g2u52sl")); } /// Initializes a Bech32 address with a human-readable part, a witness /// version, and a witness program. TEST(TWBitcoinGoldSegwitAddress, WitnessProgramToAddress) { - auto address = BitcoinGold::SegwitAddress("btg", 0, parse_hex("5e6132a9ad21f7423081441ab4ae229501f6c8a8")); + auto address = Bitcoin::SegwitAddress("btg", 0, parse_hex("5e6132a9ad21f7423081441ab4ae229501f6c8a8")); - ASSERT_TRUE(BitcoinGold::SegwitAddress::isValid(address.string())); + ASSERT_TRUE(Bitcoin::SegwitAddress::isValid(address.string())); ASSERT_EQ(address.string(), "btg1qtesn92ddy8m5yvypgsdtft3zj5qldj9g2u52sk"); } @@ -34,15 +34,15 @@ TEST(TWBitcoinGoldSegwitAddress, PubkeyToAddress) { const auto publicKey = PublicKey(parse_hex("02f74712b5d765a73b52a14c1e113f2ef3f9502d09d5987ee40f53828cfe68b9a6"), TWPublicKeyTypeSECP256k1); /// construct with public key - auto address = BitcoinGold::SegwitAddress(publicKey, 0, "btg"); + auto address = Bitcoin::SegwitAddress(publicKey, 0, "btg"); - ASSERT_TRUE(BitcoinGold::SegwitAddress::isValid(address.string())); + ASSERT_TRUE(Bitcoin::SegwitAddress::isValid(address.string())); ASSERT_EQ(address.string(), "btg1qtesn92ddy8m5yvypgsdtft3zj5qldj9g2u52sk"); } /// Decodes a SegWit address. TEST(TWBitcoinGoldSegwitAddress, Decode) { - std::pair result = BitcoinGold::SegwitAddress::decode("btg1qtesn92ddy8m5yvypgsdtft3zj5qldj9g2u52sk"); + std::pair result = Bitcoin::SegwitAddress::decode("btg1qtesn92ddy8m5yvypgsdtft3zj5qldj9g2u52sk"); ASSERT_TRUE(result.second); ASSERT_EQ(result.first.string(), "btg1qtesn92ddy8m5yvypgsdtft3zj5qldj9g2u52sk"); diff --git a/tests/BitcoinGold/TWSignerTests.cpp b/tests/BitcoinGold/TWSignerTests.cpp index 7517b50d8b4..19abc521089 100644 --- a/tests/BitcoinGold/TWSignerTests.cpp +++ b/tests/BitcoinGold/TWSignerTests.cpp @@ -9,7 +9,7 @@ #include #include -#include "BitcoinGold/SegwitAddress.h" +#include "Bitcoin/SegwitAddress.h" #include "proto/Bitcoin.pb.h" #include "Bitcoin/OutPoint.h" #include "Bitcoin/Script.h" From 11aa44fba0378469c51fa08f60a4c130fe9f2204 Mon Sep 17 00:00:00 2001 From: Wenfeng Wang Date: Fri, 24 Apr 2020 03:48:24 +0000 Subject: [PATCH 13/14] Add lockscript&zpub test and generated a tx which already mined by mainnet --- tests/BitcoinGold/TWBitcoinGoldTests.cpp | 125 +++++++++++++++++++++++ 1 file changed, 125 insertions(+) create mode 100644 tests/BitcoinGold/TWBitcoinGoldTests.cpp diff --git a/tests/BitcoinGold/TWBitcoinGoldTests.cpp b/tests/BitcoinGold/TWBitcoinGoldTests.cpp new file mode 100644 index 00000000000..39636277196 --- /dev/null +++ b/tests/BitcoinGold/TWBitcoinGoldTests.cpp @@ -0,0 +1,125 @@ +// Copyright © 2017-2020 Trust Wallet. +// +// This file is part of Trust. The full Trust copyright notice, including +// terms governing use, modification, and redistribution, is contained in the +// file LICENSE at the root of the source code distribution tree. + +#include +#include +#include +#include +#include +#include + +#include "Bitcoin/SegwitAddress.h" +#include "proto/Bitcoin.pb.h" +#include "Bitcoin/OutPoint.h" +#include "Bitcoin/Script.h" +#include "Bitcoin/Transaction.h" +#include "Bitcoin/TransactionBuilder.h" +#include "Bitcoin/TransactionSigner.h" +#include "HexCoding.h" +#include "../interface/TWTestUtilities.h" + +using namespace TW; +using namespace TW::Bitcoin; + +TEST(TWBitcoinGoldScript, LockScriptTest) { + auto script = WRAP(TWBitcoinScript, TWBitcoinScriptBuildForAddress(STRING("btg1q6572ulr0kmywle8a30lvagm9xsg9k9n5cmzfdj").get(), TWCoinTypeBitcoinGold)); + auto scriptData = WRAPD(TWBitcoinScriptData(script.get())); + assertHexEqual(scriptData, "0014d53cae7c6fb6c8efe4fd8bfecea36534105b1674"); + + auto script2 = WRAP(TWBitcoinScript, TWBitcoinScriptBuildForAddress(STRING("btg1qawhpp9gv3g662phqufjmj2ps2ge7sq4thy5g07").get(), TWCoinTypeBitcoinGold)); + auto scriptData2 = WRAPD(TWBitcoinScriptData(script2.get())); + assertHexEqual(scriptData2, "0014ebae10950c8a35a506e0e265b928305233e802ab"); +} + +TEST(BitcoinGoldKey, ExtendedKeys) { + auto wallet = WRAP(TWHDWallet, TWHDWalletCreateWithMnemonic( + STRING("shoot island position soft burden budget tooth cruel issue economy destroy above").get(), + STRING("TREZOR").get() + )); + + // .bip84 + auto zprv = WRAPS(TWHDWalletGetExtendedPrivateKey(wallet.get(), TWPurposeBIP84, TWCoinTypeBitcoinGold, TWHDVersionZPRV)); + auto zpub = WRAPS(TWHDWalletGetExtendedPublicKey(wallet.get(), TWPurposeBIP84, TWCoinTypeBitcoinGold, TWHDVersionZPUB)); + + assertStringsEqual(zprv, "zprvAdB7dYnT955ubXEkdBWhDqFSyeDfVpKQmVJPbRXGiAg4mnGT7dCBsZZFeik1mNt6bS4zkdZSNtZm8dqu1Lrp1brQ16NgYgeEoiz6ftUfVAW"); + assertStringsEqual(zpub, "zpub6rAU34KLySeCp1KDjD3hayCBXg49uH3G8iDzPovtGWD3eabbfAWSRMsjVyfuRfCCquiKTD6YV42nHUBtwh2TbVPvWqxrGuyEvHN17c3XUXw"); +} + +TEST(BitcoinGoldKey, DeriveFromZPub) { + auto zpub = STRING("zpub6rAU34KLySeCp1KDjD3hayCBXg49uH3G8iDzPovtGWD3eabbfAWSRMsjVyfuRfCCquiKTD6YV42nHUBtwh2TbVPvWqxrGuyEvHN17c3XUXw"); + auto pubKey2 = TWHDWalletGetPublicKeyFromExtended(zpub.get(), STRING("m/84'/156'/0'/0/2").get()); + auto pubKey9 = TWHDWalletGetPublicKeyFromExtended(zpub.get(), STRING("m/84'/156'/0'/0/9").get()); + + auto address2 = WRAP(TWAnyAddress, TWAnyAddressCreateWithPublicKey(pubKey2, TWCoinTypeBitcoinGold)); + auto address2String = WRAPS(TWAnyAddressDescription(address2.get())); + + auto address9 = WRAP(TWAnyAddress, TWAnyAddressCreateWithPublicKey(pubKey9, TWCoinTypeBitcoinGold)); + auto address9String = WRAPS(TWAnyAddressDescription(address9.get())); + + assertStringsEqual(address2String, "btg1qkdgxykht6nww9l9rn0xhslf78nl605gwka9zak"); + assertStringsEqual(address9String, "btg1qzw2ptuyaw023gm7te2r5e3xkufn9wrm3kzrg8t"); +} + +TEST(TWBitcoinGoldTxGeneration, TxGeneration) { + const int64_t amount = 5000; + + // Setup input + Proto::SigningInput input; + input.set_coin_type(TWCoinTypeBitcoinGold); + input.set_hash_type(TWBitcoinSigHashTypeFork | TWBitcoinSigHashTypeAll | (79 << 8)); + input.set_amount(amount); + input.set_byte_fee(1); + input.set_to_address("btg1qmd6x5awe4t5fjhgntv0pngzdwajjg250wxdcs0"); + input.set_change_address("btg1qawhpp9gv3g662phqufjmj2ps2ge7sq4thy5g07"); + + auto utxoKey0 = parse_hex("cbe13a79b82ec7f8871b336a64fd8d531f598e7c9022e29c67e824cfd54af57f"); + input.add_private_key(utxoKey0.data(), utxoKey0.size()); + + + auto scriptPub1 = Script(parse_hex("0014db746a75d9aae8995d135b1e19a04d7765242a8f")); + auto scriptHash = std::vector(); + scriptPub1.matchPayToWitnessPublicKeyHash(scriptHash); + auto scriptHashHex = hex(scriptHash.begin(), scriptHash.end()); + + auto redeemScript = Script::buildPayToPublicKeyHash(scriptHash); + auto scriptString = std::string(redeemScript.bytes.begin(), redeemScript.bytes.end()); + (*input.mutable_scripts())[scriptHashHex] = scriptString; + + auto utxo0 = input.add_utxo(); + auto utxo0Script = parse_hex("0014d53cae7c6fb6c8efe4fd8bfecea36534105b1674"); + utxo0->set_script(utxo0Script.data(), utxo0Script.size()); + utxo0->set_amount(10000); + + auto hash0 = parse_hex("5727794fa2b94aa22a226e206130524201ede9b50e032526e713c848493a890f"); + utxo0->mutable_out_point()->set_hash(hash0.data(), hash0.size()); + utxo0->mutable_out_point()->set_index(0); + utxo0->mutable_out_point()->set_sequence(0xfffffffd); + + // Sign + auto txSinger = TransactionSigner(std::move(input)); + txSinger.transaction.lockTime = 0x00098971; + auto result = txSinger.sign(); + + ASSERT_TRUE(result) << result.error();; + auto signedTx = result.payload(); + + Data serialized; + signedTx.encode(true, serialized); + ASSERT_EQ(hex(serialized), + "01000000" + "0001" + "01" + "5727794fa2b94aa22a226e206130524201ede9b50e032526e713c848493a890f" "00000000" "00" "fdffffff" + "02" + "8813000000000000" "160014db746a75d9aae8995d135b1e19a04d7765242a8f" + "a612000000000000" "160014ebae10950c8a35a506e0e265b928305233e802ab" + "02" + "483045022100bf1dcc37c2d3794e216b0b1cfcb04c7f49ef360ae941e46dc9b168f54f5447fe02205a0912bf3a3c0ac0e490c665bcde5239f553c013b2447a6fb5df6387ac029c8c41" + "2103e00b5dec8078d526fba090247bd92db6b67a4dd1953b788cea9b52de9471b8cf" + "71890900" + ); +} + From a2775aa83c55aba774223a0b19bc9549360d56ec Mon Sep 17 00:00:00 2001 From: Wenfeng Wang Date: Fri, 24 Apr 2020 14:55:54 +0000 Subject: [PATCH 14/14] Remove unnecessary protobuf file --- src/proto/BitcoinGold.proto | 11 ----------- 1 file changed, 11 deletions(-) delete mode 100644 src/proto/BitcoinGold.proto diff --git a/src/proto/BitcoinGold.proto b/src/proto/BitcoinGold.proto deleted file mode 100644 index 10fcbcd8b60..00000000000 --- a/src/proto/BitcoinGold.proto +++ /dev/null @@ -1,11 +0,0 @@ -// Copyright © 2017-2020 Trust Wallet. -// -// This file is part of Trust. The full Trust copyright notice, including -// terms governing use, modification, and redistribution, is contained in the -// file LICENSE at the root of the source code distribution tree. - -syntax = "proto3"; - -package TW.BitcoinGold.Proto; -option java_package = "wallet.core.jni.proto"; -