-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Bitcoin]: Add support for Taproot address generation (#4074)
* [Bitcoin]: Add taproot derivation * Refactor codegen tool to generate newly added derivations * Add `TWDerivation.h` to the git index * [Bitcoin]: Generate Rust enum variants as well * [Bitcoin]: Handle `TWDerivationBitcoinTaproot` enum variant * Add missing `file_editor.rb` file * [Bitcoin]: Forward deriveAddress to Rust if derivation is Taproot * [Bitcoin]: Add Android tests * [Bitcoin]: Add taproot derivation iOS tests
- Loading branch information
1 parent
8c605ed
commit 1640748
Showing
28 changed files
with
341 additions
and
45 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'file_editor' | ||
|
||
$derivation_file = "include/TrustWalletCore/TWDerivation.h" | ||
$derivation_file_rust = "rust/tw_coin_registry/src/tw_derivation.rs" | ||
|
||
# Returns a derivation name if specified. | ||
def derivation_name(deriv) | ||
return "" if deriv['name'].nil? | ||
deriv['name'].downcase | ||
end | ||
|
||
# Returns a string of `<Coin><Derivation>` if derivation's name is specified, otherwise returns `Default`. | ||
def derivation_enum_name_no_prefix(deriv, coin) | ||
return "Default" if deriv['name'].nil? | ||
format_name(coin['name']) + camel_case(deriv['name']) | ||
end | ||
|
||
# Returns a string of `TWDerivation<Coin><Derivation>` if derivation's name is specified, otherwise returns `TWDerivationDefault`. | ||
def derivation_enum_name(deriv, coin) | ||
return "TWDerivation" + derivation_enum_name_no_prefix(deriv, coin) | ||
end | ||
|
||
# Returns a derivation path. | ||
def derivation_path(coin) | ||
coin['derivation'][0]['path'] | ||
end | ||
|
||
# Get the last `TWDerivation` enum variant ID. | ||
def get_last_derivation(file_path) | ||
last_derivation_id = nil | ||
|
||
File.open(file_path, "r") do |file| | ||
file.each_line do |line| | ||
# Match lines that define a TWDerivation enum value | ||
if line =~ /TWDerivation\w+\s*=\s*(\d+),/ | ||
last_derivation_id = $1.to_i | ||
end | ||
end | ||
end | ||
|
||
last_derivation_id | ||
end | ||
|
||
# Returns whether the TWDerivation enum contains the given `derivation` variant. | ||
def find_derivation(file_path, derivation) | ||
File.open(file_path, "r") do |file| | ||
file.each_line do |line| | ||
return true if line.include?(derivation) | ||
end | ||
end | ||
return false | ||
end | ||
|
||
# Insert a new `TWDerivation<X> = N,` to the end of the enum. | ||
def insert_derivation(file_path, derivation, derivation_id) | ||
target_line = " #{derivation} = #{derivation_id}," | ||
insert_target_line(file_path, target_line, " // end_of_derivation_enum - USED TO GENERATE CODE\n") | ||
end | ||
|
||
# Update TWDerivation enum variants if new derivation appeared. | ||
def update_derivation_enum(coins) | ||
coins.each do |coin| | ||
coin['derivation'].each_with_index do |deriv, index| | ||
deriv_name = derivation_enum_name(deriv, coin) | ||
if !find_derivation($derivation_file, deriv_name) | ||
new_derivation_id = get_last_derivation($derivation_file) + 1 | ||
insert_derivation($derivation_file, deriv_name, new_derivation_id) | ||
|
||
rust_deriv_name = derivation_enum_name_no_prefix(deriv, coin) | ||
insert_derivation($derivation_file_rust, rust_deriv_name, new_derivation_id) | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
def insert_target_line(target_file, target_line, original_line) | ||
lines = File.readlines(target_file) | ||
index = lines.index(target_line) | ||
if !index.nil? | ||
puts "Line is already present, file: #{target_file} line: #{target_line}" | ||
return true | ||
end | ||
index = lines.index(original_line) | ||
if index.nil? | ||
puts "WARNING: Could not find line! file: #{target_file} line: #{original_line}" | ||
return false | ||
end | ||
lines.insert(index, target_line) | ||
File.open(target_file, "w+") do |f| | ||
f.puts(lines) | ||
end | ||
puts "Updated file: #{target_file} new line: #{target_line}" | ||
return true | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
// SPDX-License-Identifier: Apache-2.0 | ||
// | ||
// Copyright © 2017 Trust Wallet. | ||
// | ||
// This is a GENERATED FILE from \registry.json, changes made here WILL BE LOST. | ||
// | ||
|
||
#pragma once | ||
|
||
#include "TWBase.h" | ||
|
||
TW_EXTERN_C_BEGIN | ||
|
||
/// Non-default coin address derivation names (default, unnamed derivations are not included). | ||
/// Note the enum variant must be sync with `TWDerivation` enum in Rust: | ||
/// https://github.com/trustwallet/wallet-core/blob/master/rust/tw_coin_registry/src/tw_derivation.rs | ||
TW_EXPORT_ENUM() | ||
enum TWDerivation { | ||
TWDerivationDefault = 0, // default, for any coin | ||
TWDerivationCustom = 1, // custom, for any coin | ||
TWDerivationBitcoinSegwit = 2, | ||
TWDerivationBitcoinLegacy = 3, | ||
TWDerivationBitcoinTestnet = 4, | ||
TWDerivationLitecoinLegacy = 5, | ||
TWDerivationSolanaSolana = 6, | ||
TWDerivationStratisSegwit = 7, | ||
TWDerivationBitcoinTaproot = 8, | ||
// end_of_derivation_enum - USED TO GENERATE CODE | ||
}; | ||
|
||
TW_EXTERN_C_END |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.