Skip to content

Commit

Permalink
Merge branch 'master' into s/ton-connect
Browse files Browse the repository at this point in the history
  • Loading branch information
satoshiotomakan authored Aug 5, 2024
2 parents 3fd8925 + f96c6b7 commit 911db69
Show file tree
Hide file tree
Showing 31 changed files with 111 additions and 138 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -52,17 +52,17 @@ class TestCoinType {
fun testDerivationPath() {
var res = CoinType.createFromValue(CoinType.BITCOIN.value()).derivationPath().toString()
assertEquals(res, "m/84'/0'/0'/0/0")
res = CoinType.createFromValue(CoinType.BITCOIN.value()).derivationPathWithDerivation(Derivation.LEGACY).toString()
res = CoinType.createFromValue(CoinType.BITCOIN.value()).derivationPathWithDerivation(Derivation.BITCOINLEGACY).toString()
assertEquals(res, "m/44'/0'/0'/0/0")
res = CoinType.createFromValue(CoinType.SOLANA.value()).derivationPathWithDerivation(Derivation.SOLANA).toString()
res = CoinType.createFromValue(CoinType.SOLANA.value()).derivationPathWithDerivation(Derivation.SOLANASOLANA).toString()
assertEquals(res, "m/44'/501'/0'/0'")
}

@Test
fun testDeriveAddressFromPublicKeyAndDerivation() {
val publicKey = PublicKey("0279BE667EF9DCBBAC55A06295CE870B07029BFCDB2DCE28D959F2815B16F81798".toHexByteArray(), PublicKeyType.SECP256K1)

val address = CoinType.BITCOIN.deriveAddressFromPublicKeyAndDerivation(publicKey, Derivation.SEGWIT)
val address = CoinType.BITCOIN.deriveAddressFromPublicKeyAndDerivation(publicKey, Derivation.BITCOINSEGWIT)
assertEquals(address, "bc1qw508d6qejxtdg4y5r3zarvary0c5xw7kv8f3t4")
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,10 @@ class TestAnyAddress {
fun testCreateWithPublicKeyDerivation() {
val coin = CoinType.BITCOIN
val pubkey = PublicKey(any_address_test_pubkey.toHexByteArray(), PublicKeyType.SECP256K1)
val address1 = AnyAddress(pubkey, coin, Derivation.SEGWIT)
val address1 = AnyAddress(pubkey, coin, Derivation.BITCOINSEGWIT)
assertEquals(address1.description(), any_address_test_address)

val address2 = AnyAddress(pubkey, coin, Derivation.LEGACY)
val address2 = AnyAddress(pubkey, coin, Derivation.BITCOINLEGACY)
assertEquals(address2.description(), "1JvRfEQFv5q5qy9uTSAezH7kVQf4hqnHXx")
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,13 +113,13 @@ class TestHDWallet {
val coin = CoinType.BITCOIN
val wallet = HDWallet(words, password)

val key1 = wallet.getKeyDerivation(coin, Derivation.SEGWIT)
val key1 = wallet.getKeyDerivation(coin, Derivation.BITCOINSEGWIT)
assertEquals(key1.data().toHex(), "0x1901b5994f075af71397f65bd68a9fff8d3025d65f5a2c731cf90f5e259d6aac")

val key2 = wallet.getKeyDerivation(coin, Derivation.LEGACY)
val key2 = wallet.getKeyDerivation(coin, Derivation.BITCOINLEGACY)
assertEquals(key2.data().toHex(), "0x28071bf4e2b0340db41b807ed8a5514139e5d6427ff9d58dbd22b7ed187103a4")

val key3 = wallet.getKeyDerivation(coin, Derivation.TESTNET)
val key3 = wallet.getKeyDerivation(coin, Derivation.BITCOINTESTNET)
assertEquals(key3.data().toHex(), "0xca5845e1b43e3adf577b7f110b60596479425695005a594c88f9901c3afe864f")
}

Expand All @@ -137,13 +137,13 @@ class TestHDWallet {
val coin = CoinType.BITCOIN
val wallet = HDWallet(words, password)

val address1 = wallet.getAddressDerivation(coin, Derivation.SEGWIT)
val address1 = wallet.getAddressDerivation(coin, Derivation.BITCOINSEGWIT)
assertEquals(address1, "bc1qumwjg8danv2vm29lp5swdux4r60ezptzz7ce85")

val address2 = wallet.getAddressDerivation(coin, Derivation.LEGACY)
val address2 = wallet.getAddressDerivation(coin, Derivation.BITCOINLEGACY)
assertEquals(address2, "1PeUvjuxyf31aJKX6kCXuaqxhmG78ZUdL1")

val address3 = wallet.getAddressDerivation(coin, Derivation.TESTNET)
val address3 = wallet.getAddressDerivation(coin, Derivation.BITCOINTESTNET)
assertEquals(address3, "tb1qwgpxgwn33z3ke9s7q65l976pseh4edrzfmyvl0")
}

Expand Down
5 changes: 2 additions & 3 deletions codegen/bin/coins
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,9 @@ def self.derivation_name(deriv)
deriv['name'].downcase
end

def self.derivation_enum_name(deriv)
def self.derivation_enum_name(deriv, coin)
return "TWDerivationDefault" if deriv['name'].nil?
"TWDerivation" + camel_case(deriv['name'])
"TWDerivation" + format_name(coin['name']) + camel_case(deriv['name'])
end

def self.coin_img(coin)
Expand All @@ -53,7 +53,6 @@ coins = JSON.parse(json_string).sort_by { |x| x['coinId'] }

# used in some cases for numbering enum values
enum_count = 0
derivations = {}

erbs = [
{'template' => 'TWDerivation.h.erb', 'folder' => 'include/TrustWalletCore', 'file' => 'TWDerivation.h'},
Expand Down
2 changes: 1 addition & 1 deletion codegen/lib/templates/CoinInfoData.cpp.erb
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ const CoinInfo getCoinInfo(TWCoinType coin) {
TWCurve<%= format_name(coin['curve']) %>,
{
<% coin['derivation'].each do |deriv| -%>{
<%= derivation_enum_name(deriv) %>,
<%= derivation_enum_name(deriv, coin) %>,
"<%= deriv['path'] %>",
"<%= derivation_name(deriv) %>",
TWHDVersion<% if deriv['xpub'].nil? -%>None<% else -%><%= format_name(deriv['xpub']) %><% end -%>,
Expand Down
5 changes: 2 additions & 3 deletions codegen/lib/templates/TWDerivation.h.erb
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,8 @@ enum TWDerivation {
<% enum_count += 1 -%>
<% coins.each do |coin| -%>
<% coin['derivation'].each_with_index do |deriv, index| -%>
<% if !deriv['name'].nil? and !derivations.has_key?(deriv['name']) -%>
<% derivations[deriv['name']] = true -%>
<%= derivation_enum_name(deriv) %> = <% enum_count += 1 -%><%= enum_count %>,
<% if index > 0 or !deriv['name'].nil? -%>
<%= derivation_enum_name(deriv, coin) %> = <% enum_count += 1 -%><%= enum_count %>,
<% end -%>
<% end -%>
<% end -%>
Expand Down
37 changes: 0 additions & 37 deletions registry.json
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,6 @@
"blockchain": "Bitcoin",
"derivation": [
{
"name": "segwit",
"path": "m/84'/2'/0'/0/0",
"xpub": "zpub",
"xprv": "zprv"
Expand Down Expand Up @@ -160,16 +159,9 @@
"blockchain": "Bitcoin",
"derivation": [
{
"name": "segwit",
"path": "m/84'/14'/0'/0/0",
"xpub": "zpub",
"xprv": "zprv"
},
{
"name": "legacy",
"path": "m/44'/14'/0'/0/0",
"xpub": "xpub",
"xprv": "xprv"
}
],
"curve": "secp256k1",
Expand Down Expand Up @@ -233,7 +225,6 @@
"blockchain": "Bitcoin",
"derivation": [
{
"name": "segwit",
"path": "m/84'/20'/0'/0/0",
"xpub": "zpub",
"xprv": "zprv"
Expand Down Expand Up @@ -267,16 +258,9 @@
"blockchain": "Bitcoin",
"derivation": [
{
"name": "legacy",
"path": "m/44'/22'/0'/0/0",
"xpub": "xpub",
"xprv": "xprv"
},
{
"name": "segwit",
"path": "m/84'/22'/0'/0/0",
"xpub": "zpub",
"xprv": "zprv"
}
],
"curve": "secp256k1",
Expand Down Expand Up @@ -340,16 +324,9 @@
"blockchain": "Bitcoin",
"derivation": [
{
"name": "segwit",
"path": "m/84'/57'/0'/0/0",
"xpub": "zpub",
"xprv": "zprv"
},
{
"name": "legacy",
"path": "m/44'/57'/0'/0/0",
"xpub": "xpub",
"xprv": "xprv"
}
],
"curve": "secp256k1",
Expand Down Expand Up @@ -1540,16 +1517,9 @@
"blockchain": "Bitcoin",
"derivation": [
{
"name": "segwit",
"path": "m/84'/156'/0'/0/0",
"xpub": "zpub",
"xprv": "zprv"
},
{
"name": "legacy",
"path": "m/44'/156'/0'/0/0",
"xpub": "xpub",
"xprv": "xprv"
}
],
"curve": "secp256k1",
Expand Down Expand Up @@ -2872,16 +2842,9 @@
"blockchain": "Bitcoin",
"derivation": [
{
"name": "legacy",
"path": "m/44'/2301'/0'/0/0",
"xpub": "xpub",
"xprv": "xprv"
},
{
"name": "segwit",
"path": "m/84'/2301'/0'/0/0",
"xpub": "zpub",
"xprv": "zprv"
}
],
"curve": "secp256k1",
Expand Down
39 changes: 25 additions & 14 deletions rust/frameworks/tw_utxo/src/address/derivation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
// Copyright © 2017 Trust Wallet.

use tw_coin_entry::coin_context::CoinContext;
use tw_coin_entry::derivation::Derivation;
use tw_coin_entry::derivation::{ChildIndex, Derivation};

pub const SEGWIT_DERIVATION_PATH_TYPE: ChildIndex = ChildIndex::Hardened(84);

pub enum BitcoinDerivation {
Legacy,
Expand All @@ -15,27 +17,36 @@ impl BitcoinDerivation {
/// https://github.com/trustwallet/wallet-core/blob/b65adc4c86e49eb905f659ade025185a62e87ca9/src/Bitcoin/Entry.cpp#L67
pub fn tw_derivation(coin: &dyn CoinContext, derivation: Derivation) -> BitcoinDerivation {
match derivation {
Derivation::Default
// In case of a default derivation specified by the function caller,
// we should check the default derivation in the `coin`'s context.
// Please note that testnet derivation is no longer supported. Instead, use address prefix.
| Derivation::Testnet => {
let Some(default_derivation) = coin.derivations().first() else {
return BitcoinDerivation::Segwit;
};
if default_derivation.name == Derivation::Segwit {
return BitcoinDerivation::Segwit;
}
Derivation::Default | Derivation::Testnet => (),
Derivation::Segwit => return BitcoinDerivation::Segwit,
Derivation::Legacy => return BitcoinDerivation::Legacy,
}

let Some(default_derivation) = coin.derivations().first() else {
return BitcoinDerivation::Legacy;
};
let derivation_path_type = default_derivation.path.path().first().copied();

match default_derivation.name {
Derivation::Segwit => BitcoinDerivation::Segwit,
Derivation::Default if derivation_path_type == Some(SEGWIT_DERIVATION_PATH_TYPE) => {
BitcoinDerivation::Segwit
},
Derivation::Default | Derivation::Legacy | Derivation::Testnet => {
BitcoinDerivation::Legacy
},
Derivation::Segwit => BitcoinDerivation::Segwit,
Derivation::Legacy => BitcoinDerivation::Legacy,
}
}

/// TrustWallet behaviour inherited from:
/// https://github.com/trustwallet/wallet-core/blob/b65adc4c86e49eb905f659ade025185a62e87ca9/src/Bitcoin/Entry.cpp#L14
pub fn tw_supports_segwit(coin: &dyn CoinContext) -> bool {
coin.derivations()
.iter()
.any(|der| der.name == Derivation::Segwit)
coin.derivations().iter().any(|der| {
der.name == Derivation::Segwit
|| der.path.path().first().copied() == Some(SEGWIT_DERIVATION_PATH_TYPE)
})
}
}
7 changes: 4 additions & 3 deletions src/Bitcoin/Entry.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -79,13 +79,14 @@ std::string Entry::deriveAddress(TWCoinType coin, const PublicKey& publicKey, TW
case TWCoinTypeSyscoin:
case TWCoinTypeStratis:
switch (derivation) {
case TWDerivationLegacy:
case TWDerivationBitcoinLegacy:
case TWDerivationLitecoinLegacy:
return Address(publicKey, p2pkh).string();

case TWDerivationTestnet:
case TWDerivationBitcoinTestnet:
return SegwitAddress::createTestnetFromPublicKey(publicKey).string();

case TWDerivationSegwit:
case TWDerivationBitcoinSegwit:
case TWDerivationDefault:
default:
return SegwitAddress(publicKey, hrp).string();
Expand Down
2 changes: 1 addition & 1 deletion src/BitcoinDiamond/Entry.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ string Entry::deriveAddress(TWCoinType coin, const PublicKey& publicKey, TWDeriv
const char* hrp = getFromPrefixHrpOrDefault(addressPrefix, coin);

switch (derivation) {
case TWDerivationLegacy:
case TWDerivationBitcoinLegacy:
case TWDerivationDefault:
return Bitcoin::Address(publicKey, p2pkh).string();
default:
Expand Down
2 changes: 1 addition & 1 deletion src/Groestlcoin/Entry.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ std::string Entry::deriveAddress([[maybe_unused]] TWCoinType coin, const PublicK
auto p2pkh = getFromPrefixPkhOrDefault(addressPrefix, coin);

switch (derivation) {
case TWDerivationLegacy:
case TWDerivationBitcoinLegacy:
return Address(publicKey, p2pkh).string();
default:
return TW::Bitcoin::SegwitAddress(publicKey, hrp).string();
Expand Down
2 changes: 1 addition & 1 deletion src/Verge/Entry.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ string Entry::deriveAddress(TWCoinType coin, const PublicKey& publicKey, TWDeriv
const char* hrp = getFromPrefixHrpOrDefault(addressPrefix, coin);

switch (derivation) {
case TWDerivationLegacy:
case TWDerivationBitcoinLegacy:
case TWDerivationDefault:
return Bitcoin::Address(publicKey, p2pkh).string();
default:
Expand Down
4 changes: 2 additions & 2 deletions swift/Tests/AnyAddressTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,10 @@ class AnyAddressTests: XCTestCase {
func testCreateWithPublicKeyDerivation() {
let coin = CoinType.bitcoin
let pubkey = PublicKey(data: Data(hexString: any_address_test_pubkey)!, type: .secp256k1)!
let address1 = AnyAddress(publicKey: pubkey, coin: coin, derivation: .segwit)
let address1 = AnyAddress(publicKey: pubkey, coin: coin, derivation: .bitcoinSegwit)
XCTAssertEqual(address1.description, any_address_test_address)

let address2 = AnyAddress(publicKey: pubkey, coin: coin, derivation: .legacy)
let address2 = AnyAddress(publicKey: pubkey, coin: coin, derivation: .bitcoinLegacy)
XCTAssertEqual(address2.description, "1JvRfEQFv5q5qy9uTSAezH7kVQf4hqnHXx")
}

Expand Down
10 changes: 5 additions & 5 deletions swift/Tests/CoinTypeTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,15 @@ class CoinTypeTests: XCTestCase {

func testCoinDerivation() {
XCTAssertEqual(CoinType.bitcoin.derivationPath(), "m/84'/0'/0'/0/0")
XCTAssertEqual(CoinType.bitcoin.derivationPathWithDerivation(derivation: Derivation.legacy), "m/44'/0'/0'/0/0")
XCTAssertEqual(CoinType.solana.derivationPathWithDerivation(derivation: Derivation.solana), "m/44'/501'/0'/0'")
XCTAssertEqual(CoinType.bitcoin.derivationPathWithDerivation(derivation: Derivation.bitcoinLegacy), "m/44'/0'/0'/0/0")
XCTAssertEqual(CoinType.solana.derivationPathWithDerivation(derivation: Derivation.solanaSolana), "m/44'/501'/0'/0'")
}

func testDeriveAddressFromPublicKeyAndDerivation() {
let pkData = Data(hexString: "0279BE667EF9DCBBAC55A06295CE870B07029BFCDB2DCE28D959F2815B16F81798")!
let publicKey = PublicKey(data: pkData, type: .secp256k1)!
let address = CoinType.bitcoin.deriveAddressFromPublicKeyAndDerivation(publicKey: publicKey, derivation: Derivation.segwit)

let address = CoinType.bitcoin.deriveAddressFromPublicKeyAndDerivation(publicKey: publicKey, derivation: Derivation.bitcoinSegwit)
XCTAssertEqual(address, "bc1qw508d6qejxtdg4y5r3zarvary0c5xw7kv8f3t4")
}
}
12 changes: 6 additions & 6 deletions swift/Tests/HDWalletTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -92,13 +92,13 @@ class HDWalletTests: XCTestCase {
let coin = CoinType.bitcoin
let wallet = HDWallet.test

let key1 = wallet.getKeyDerivation(coin: coin, derivation: .segwit)
let key1 = wallet.getKeyDerivation(coin: coin, derivation: .bitcoinSegwit)
XCTAssertEqual(key1.data.hexString, "1901b5994f075af71397f65bd68a9fff8d3025d65f5a2c731cf90f5e259d6aac")

let key2 = wallet.getKeyDerivation(coin: coin, derivation: .legacy)
let key2 = wallet.getKeyDerivation(coin: coin, derivation: .bitcoinLegacy)
XCTAssertEqual(key2.data.hexString, "28071bf4e2b0340db41b807ed8a5514139e5d6427ff9d58dbd22b7ed187103a4")

let key3 = wallet.getKeyDerivation(coin: coin, derivation: .testnet)
let key3 = wallet.getKeyDerivation(coin: coin, derivation: .bitcoinTestnet)
XCTAssertEqual(key3.data.hexString, "ca5845e1b43e3adf577b7f110b60596479425695005a594c88f9901c3afe864f")
}

Expand All @@ -114,13 +114,13 @@ class HDWalletTests: XCTestCase {
let coin = CoinType.bitcoin
let wallet = HDWallet.test

let address1 = wallet.getAddressDerivation(coin: coin, derivation: .segwit)
let address1 = wallet.getAddressDerivation(coin: coin, derivation: .bitcoinSegwit)
XCTAssertEqual(address1, "bc1qumwjg8danv2vm29lp5swdux4r60ezptzz7ce85")

let address2 = wallet.getAddressDerivation(coin: coin, derivation: .legacy)
let address2 = wallet.getAddressDerivation(coin: coin, derivation: .bitcoinLegacy)
XCTAssertEqual(address2, "1PeUvjuxyf31aJKX6kCXuaqxhmG78ZUdL1")

let address3 = wallet.getAddressDerivation(coin: coin, derivation: .testnet)
let address3 = wallet.getAddressDerivation(coin: coin, derivation: .bitcoinTestnet)
XCTAssertEqual(address3, "tb1qwgpxgwn33z3ke9s7q65l976pseh4edrzfmyvl0")
}

Expand Down
Loading

0 comments on commit 911db69

Please sign in to comment.