Skip to content

Commit f94adf5

Browse files
[Rust/BNB]: Move BNB Beacon chain to Rust (trustwallet#3589)
* [BNB]: Generate BNB Beacon chain skeleton files in Rust * [BNB]: Implement `BinanceAddress`, add Address tests * [BNB]: Add TBinance address tests * [BNB]: Add Transaction and its encoding, prehashing * Add Amino encoding * [BNB]: Sign a transaction * TODO Add tests, handle all message types * [BNB]: Add a signing test * [BNB]: Add `SendOrder`, `CancelOrder` * [BNB]: Add Token orders * [BNB]: Add `TokenBurnOrder`, `HTLTOrder` * [BNB]: Add `DepositHTLT` * [BNB]: Add `ClaimHTLTOrder`, `RefundHTLTOrder` * [BNB]: Add `TransferOutOrder` * [BNB]: Add `SideChainDelegate`, `SideChainRedelegate`, `SideChainUndelegate` orders * [BNB]: Add `TimeLock`, `TimeRelock`, `TimeUnlock` orders * [BNB]: Implement Transaction Compiler * [BNB]: Increase code coverage * [BNB]: Replace C++ implementation * [BNB]: Move TX preimage implementation to `JsonPreimager` * Remove C++ Signing tests * [BNB]: Extend `CoinEntry` documentation * [CI] Trigger CI * [BNB]: Remove `TransactionCompilerBuildInput` * [BNB]: Avoid duplicating code * [BNB]: Add fuzz tests
1 parent acd8b9b commit f94adf5

File tree

99 files changed

+3419
-1643
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

99 files changed

+3419
-1643
lines changed

codegen-v2/Cargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ path = "src/main.rs"
1515
aho-corasick = "1.1.2"
1616
convert_case = "0.6.0"
1717
pathdiff = "0.2.1"
18-
serde = { version = "1.0.159", features = ["derive"] }
19-
serde_json = "1.0.95"
18+
serde = { version = "1.0", features = ["derive"] }
19+
serde_json = "1.0"
2020
serde_yaml = "0.9.21"
2121
toml_edit = "0.21.0"
2222
handlebars = "4.3.6"

include/TrustWalletCore/TWTransactionCompiler.h

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -18,23 +18,6 @@ TW_EXTERN_C_BEGIN
1818
TW_EXPORT_STRUCT
1919
struct TWTransactionCompiler;
2020

21-
/// Builds a coin-specific SigningInput (proto object) from a simple transaction.
22-
///
23-
/// \deprecated `TWTransactionCompilerBuildInput` will be removed soon.
24-
/// \param coin coin type.
25-
/// \param from sender of the transaction.
26-
/// \param to receiver of the transaction.
27-
/// \param amount transaction amount in string
28-
/// \param asset optional asset name, like "BNB"
29-
/// \param memo optional memo
30-
/// \param chainId optional chainId to override default
31-
/// \return serialized data of the SigningInput proto object.
32-
TW_EXPORT_STATIC_METHOD
33-
TWData* _Nonnull TWTransactionCompilerBuildInput(enum TWCoinType coinType, TWString* _Nonnull from,
34-
TWString* _Nonnull to, TWString* _Nonnull amount,
35-
TWString* _Nonnull asset, TWString* _Nonnull memo,
36-
TWString* _Nonnull chainId);
37-
3821
/// Obtains pre-signing hashes of a transaction.
3922
///
4023
/// We provide a default `PreSigningOutput` in TransactionCompiler.proto.

registry.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2440,6 +2440,7 @@
24402440
],
24412441
"curve": "secp256k1",
24422442
"publicKeyType": "secp256k1",
2443+
"addressHasher": "sha256ripemd",
24432444
"hrp": "bnb",
24442445
"chainId": "Binance-Chain-Tigris",
24452446
"explorer": {
@@ -2472,6 +2473,7 @@
24722473
],
24732474
"curve": "secp256k1",
24742475
"publicKeyType": "secp256k1",
2476+
"addressHasher": "sha256ripemd",
24752477
"hrp": "tbnb",
24762478
"explorer": {
24772479
"url": "https://testnet-explorer.binance.org",

rust/Cargo.lock

Lines changed: 33 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

rust/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
[workspace]
22
members = [
3+
"chains/tw_binance",
34
"chains/tw_cosmos",
45
"chains/tw_native_evmos",
56
"chains/tw_native_injective",

rust/chains/tw_binance/Cargo.toml

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
[package]
2+
name = "tw_binance"
3+
version = "0.1.0"
4+
edition = "2021"
5+
6+
[dependencies]
7+
quick-protobuf = "0.8.1"
8+
serde = { version = "1.0", features = ["derive"] }
9+
serde_json = "1.0"
10+
serde_repr = "0.1"
11+
strum_macros = "0.25"
12+
tw_bech32_address = { path = "../../tw_bech32_address" }
13+
tw_coin_entry = { path = "../../tw_coin_entry" }
14+
tw_cosmos_sdk = { path = "../../tw_cosmos_sdk" }
15+
tw_encoding = { path = "../../tw_encoding" }
16+
tw_evm = { path = "../../tw_evm" }
17+
tw_hash = { path = "../../tw_hash" }
18+
tw_keypair = { path = "../../tw_keypair" }
19+
tw_memory = { path = "../../tw_memory" }
20+
tw_misc = { path = "../../tw_misc" }
21+
tw_proto = { path = "../../tw_proto" }
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
target
2+
corpus
3+
artifacts
4+
coverage
5+
Cargo.lock
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
[package]
2+
name = "tw_binance-fuzz"
3+
version = "0.0.0"
4+
publish = false
5+
edition = "2021"
6+
7+
[package.metadata]
8+
cargo-fuzz = true
9+
10+
[dependencies]
11+
libfuzzer-sys = "0.4"
12+
tw_any_coin = { path = "../../../tw_any_coin", features = ["test-utils"] }
13+
tw_coin_registry = { path = "../../../tw_coin_registry" }
14+
tw_proto = { path = "../../../tw_proto", features = ["fuzz"] }
15+
16+
[dependencies.tw_binance]
17+
path = ".."
18+
19+
# Prevent this from interfering with workspaces
20+
[workspace]
21+
members = ["."]
22+
23+
[profile.release]
24+
debug = 1
25+
26+
[[bin]]
27+
name = "sign"
28+
path = "fuzz_targets/sign.rs"
29+
test = false
30+
doc = false
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#![no_main]
2+
3+
use libfuzzer_sys::fuzz_target;
4+
use tw_any_coin::test_utils::sign_utils::AnySignerHelper;
5+
use tw_coin_registry::coin_type::CoinType;
6+
use tw_proto::Binance::Proto;
7+
8+
fuzz_target!(|input: Proto::SigningInput<'_>| {
9+
let mut signer = AnySignerHelper::<Proto::SigningOutput>::default();
10+
let _ = signer.sign(CoinType::Binance, input);
11+
});
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
// Copyright © 2017-2023 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+
use serde::Serialize;
8+
use std::fmt;
9+
use std::str::FromStr;
10+
use tw_bech32_address::bech32_prefix::Bech32Prefix;
11+
use tw_bech32_address::Bech32Address;
12+
use tw_coin_entry::coin_context::CoinContext;
13+
use tw_coin_entry::coin_entry::CoinAddress;
14+
use tw_coin_entry::error::{AddressError, AddressResult};
15+
use tw_keypair::tw::PublicKey;
16+
use tw_memory::Data;
17+
18+
/// The list of known BNB hrps.
19+
const BNB_KNOWN_HRPS: [&str; 2] = [
20+
BinanceAddress::VALIDATOR_HRP, // BNB Validator HRP.
21+
"bca",
22+
];
23+
24+
#[derive(Serialize)]
25+
pub struct BinanceAddress(Bech32Address);
26+
27+
impl CoinAddress for BinanceAddress {
28+
#[inline]
29+
fn data(&self) -> Data {
30+
self.0.data()
31+
}
32+
}
33+
34+
impl BinanceAddress {
35+
pub const VALIDATOR_HRP: &'static str = "bva";
36+
37+
pub fn new_validator_addr(key_hash: Data) -> AddressResult<BinanceAddress> {
38+
Bech32Address::new(Self::VALIDATOR_HRP.to_string(), key_hash).map(BinanceAddress)
39+
}
40+
41+
/// Creates a Binance address with the only `prefix`
42+
pub fn from_str_with_coin_and_prefix(
43+
coin: &dyn CoinContext,
44+
address_str: String,
45+
prefix: Option<Bech32Prefix>,
46+
) -> AddressResult<BinanceAddress>
47+
where
48+
Self: Sized,
49+
{
50+
let possible_hrps = match prefix {
51+
Some(Bech32Prefix { hrp }) => vec![hrp],
52+
None => {
53+
let coin_hrp = coin.hrp().ok_or(AddressError::InvalidHrp)?;
54+
let other_hrps = BNB_KNOWN_HRPS
55+
.iter()
56+
.map(|another_hrp| another_hrp.to_string());
57+
std::iter::once(coin_hrp).chain(other_hrps).collect()
58+
},
59+
};
60+
Bech32Address::from_str_checked(possible_hrps, address_str).map(BinanceAddress)
61+
}
62+
63+
pub fn with_public_key_coin_context(
64+
coin: &dyn CoinContext,
65+
public_key: &PublicKey,
66+
prefix: Option<Bech32Prefix>,
67+
) -> AddressResult<BinanceAddress> {
68+
Bech32Address::with_public_key_coin_context(coin, public_key, prefix).map(BinanceAddress)
69+
}
70+
71+
pub fn from_key_hash_with_coin(
72+
coin: &dyn CoinContext,
73+
key_hash: Data,
74+
) -> AddressResult<BinanceAddress> {
75+
Bech32Address::from_key_hash_with_coin(coin, key_hash).map(BinanceAddress)
76+
}
77+
}
78+
79+
impl FromStr for BinanceAddress {
80+
type Err = AddressError;
81+
82+
fn from_str(s: &str) -> Result<Self, Self::Err> {
83+
Bech32Address::from_str(s).map(BinanceAddress)
84+
}
85+
}
86+
87+
impl fmt::Display for BinanceAddress {
88+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
89+
fmt::Display::fmt(&self.0, f)
90+
}
91+
}

0 commit comments

Comments
 (0)