Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Bitcoin Gold implementation #926

Merged
merged 16 commits into from
May 5, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
28 changes: 28 additions & 0 deletions coins.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
1 change: 1 addition & 0 deletions docs/coins.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ This list is generated from [./coins.json](../coins.json)
| 144 | XRP | XRP | <img src="https://raw.githubusercontent.com/trustwallet/assets/master/blockchains/ripple/info/logo.png" width="32" /> | <https://ripple.com/xrp> |
| 145 | Bitcoin Cash | BCH | <img src="https://raw.githubusercontent.com/trustwallet/assets/master/blockchains/bitcoincash/info/logo.png" width="32" /> | <https://bitcoincash.org> |
| 148 | Stellar | XLM | <img src="https://raw.githubusercontent.com/trustwallet/assets/master/blockchains/stellar/info/logo.png" width="32" /> | <https://stellar.org> |
| 156 | Bitcoin Gold | BTG | <img src="https://raw.githubusercontent.com/trustwallet/assets/master/blockchains/bitcoingold/info/logo.png" width="32" /> | <https://bitcoingold.org> |
| 165 | Nano | NANO | <img src="https://raw.githubusercontent.com/trustwallet/assets/master/blockchains/nano/info/logo.png" width="32" /> | <https://nano.org> |
| 175 | Ravencoin | RVN | <img src="https://raw.githubusercontent.com/trustwallet/assets/master/blockchains/ravencoin/info/logo.png" width="32" /> | <https://ravencoin.org> |
| 178 | POA Network | POA | <img src="https://raw.githubusercontent.com/trustwallet/assets/master/blockchains/poa/info/logo.png" width="32" /> | <https://poa.network> |
Expand Down
1 change: 1 addition & 0 deletions include/TrustWalletCore/TWBitcoinSigHashType.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ enum TWBitcoinSigHashType {
TWBitcoinSigHashTypeNone = 0x02,
TWBitcoinSigHashTypeSingle = 0x03,
TWBitcoinSigHashTypeFork = 0x40,
TWBitcoinSigHashTypeForkBTG = 0x4f40,
TWBitcoinSigHashTypeAnyoneCanPay = 0x80
};

Expand Down
1 change: 1 addition & 0 deletions include/TrustWalletCore/TWCoinType.h
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
2 changes: 2 additions & 0 deletions src/Bitcoin/Entry.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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}});

Expand Down Expand Up @@ -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:
Expand Down
1 change: 1 addition & 0 deletions src/Bitcoin/Entry.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ class Entry: public CoinEntry {
return {
TWCoinTypeBitcoin,
TWCoinTypeBitcoinCash,
TWCoinTypeBitcoinGold,
TWCoinTypeDash,
TWCoinTypeDigiByte,
TWCoinTypeDogecoin,
Expand Down
3 changes: 3 additions & 0 deletions swift/Tests/CoinAddressDerivationTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
35 changes: 35 additions & 0 deletions tests/BitcoinGold/TWAddressTests.cpp
Original file line number Diff line number Diff line change
@@ -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.
//

#include "../interface/TWTestUtilities.h"
#include "Bitcoin/Address.h"
#include "PrivateKey.h"
#include "PublicKey.h"
#include "HexCoding.h"
#include "Coin.h"
#include <gtest/gtest.h>
#include <TrustWalletCore/TWAnyAddress.h>

using namespace TW;

const char *PRIVATE_KEY = "CA6D1402199530A5D610A01A53505B6A344CF61B0CCB2902D5AEFBEA63C274BB";
const char *ADDRESS = "GSGUyooxtCUVBonYV8AANp7FvKy3WTvpMR";
const char *FAKEADDRESS = "GSGUyooxtCUVBonYV9AANp7FvKy3WTvpMR";

TEST(TWBitcoinGoldAddress, Valid) {
ASSERT_TRUE(Bitcoin::Address::isValid(std::string(ADDRESS)));
ASSERT_FALSE(Bitcoin::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 = Bitcoin::Address(PublicKey(publicKey), p2pkhPrefix(TWCoinTypeBitcoinGold));
ASSERT_EQ(address.string(), ADDRESS);
}
125 changes: 125 additions & 0 deletions tests/BitcoinGold/TWBitcoinGoldTests.cpp
Original file line number Diff line number Diff line change
@@ -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 <TrustWalletCore/TWAnyAddress.h>
#include <TrustWalletCore/TWAnySigner.h>
#include <TrustWalletCore/TWBitcoinSigHashType.h>
#include <TrustWalletCore/TWBitcoinScript.h>
#include <TrustWalletCore/TWHDWallet.h>
#include <gtest/gtest.h>

#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(TWBitcoinSigHashTypeAll | TWBitcoinSigHashTypeForkBTG);
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<uint8_t>();
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<Transaction, TransactionBuilder>(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"
);
}

34 changes: 34 additions & 0 deletions tests/BitcoinGold/TWCoinTypeTests.cpp
Original file line number Diff line number Diff line change
@@ -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 <TrustWalletCore/TWCoinTypeConfiguration.h>
#include <gtest/gtest.h>


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");
}
49 changes: 49 additions & 0 deletions tests/BitcoinGold/TWSegwitAddressTests.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
// 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 "Bitcoin/SegwitAddress.h"
#include "PrivateKey.h"
#include "PublicKey.h"
#include "HexCoding.h"
#include <gtest/gtest.h>
#include <TrustWalletCore/TWAnyAddress.h>

using namespace TW;

TEST(TWBitcoinGoldSegwitAddress, Valid) {
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 = Bitcoin::SegwitAddress("btg", 0, parse_hex("5e6132a9ad21f7423081441ab4ae229501f6c8a8"));

ASSERT_TRUE(Bitcoin::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 = Bitcoin::SegwitAddress(publicKey, 0, "btg");

ASSERT_TRUE(Bitcoin::SegwitAddress::isValid(address.string()));
ASSERT_EQ(address.string(), "btg1qtesn92ddy8m5yvypgsdtft3zj5qldj9g2u52sk");
}

/// Decodes a SegWit address.
TEST(TWBitcoinGoldSegwitAddress, Decode) {
std::pair<Bitcoin::SegwitAddress, bool> result = Bitcoin::SegwitAddress::decode("btg1qtesn92ddy8m5yvypgsdtft3zj5qldj9g2u52sk");

ASSERT_TRUE(result.second);
ASSERT_EQ(result.first.string(), "btg1qtesn92ddy8m5yvypgsdtft3zj5qldj9g2u52sk");
}
Loading