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

merge avax #1567

Merged
merged 3 commits into from
Jan 10, 2025
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
27 changes: 25 additions & 2 deletions rust/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 4 additions & 2 deletions rust/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ members = [
"apps/wallets",
"apps/xrp",
"apps/zcash",
"apps/avalanche",

# C interface entry
"rust_c",
Expand All @@ -36,6 +37,7 @@ app_bitcoin = { path = "apps/bitcoin" }
app_cardano = { path = "apps/cardano" }
app_cosmos = { path = "apps/cosmos", default-features = false }
app_ethereum = { path = "apps/ethereum" }
app_avalanche = { path = "apps/avalanche" }
app_near = { path = "apps/near" }
app_solana = { path = "apps/solana" }
app_stellar = { path = "apps/stellar" }
Expand Down Expand Up @@ -70,8 +72,8 @@ core2 = { version = "0.3.3", default_features = false, features = ["alloc"] }
thiserror = { version = "1.0", package = "thiserror-core", default-features = false }
rsa = { version = "0.8.2", default-features = false }
sha1 = { version = "0.10.5", default-features = false }
ur-registry = { git = "https://[email protected]/KeystoneHQ/keystone-sdk-rust.git", tag = "0.0.45" }
ur-parse-lib = { git = "https://[email protected]/KeystoneHQ/keystone-sdk-rust.git", tag = "0.0.45" }
ur-registry = { git = "https://[email protected]/KeystoneHQ/keystone-sdk-rust.git", tag = "0.0.46" }
ur-parse-lib = { git = "https://[email protected]/KeystoneHQ/keystone-sdk-rust.git", tag = "0.0.46" }
ed25519-bip32-core = { version = "0.1.1", default-features = false }
cryptoxide = "0.4"
arrayref = "0.3.6"
Expand Down
1 change: 0 additions & 1 deletion rust/apps/arweave/src/data_item.rs
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,6 @@ mod tests {
//01000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000a999bac8b7906c0bc94f7d163ea9e7fe6ef34045b6a27035e5298aaaddeea05355c50efd30da262c97a68b5aa7219726754bf8501818429e60b9f8175ed66a23349757dc8b3f126abc199272c91174bdb96a9a13aad43b9b6195583188c222002d29b105169dc237dccb0e371895aa10b9263e0b6fbe2d03d3a0073fa7f278ecfa890e75a3fe812ca86eb44f134a7edaa664a5582e72fa43b7accdfeb03f0492c118235b9ff7784106ca1a2f6e7bc4bcc6e1ed98775b7c023a1ae1e332f42e3183ab17c43c58e6605353a47331452ebf659fb267d27492b961ecdafcde9657a0a623aec761f6b3130f89ff7136cae26ebc58aaaa0c6c2264d8e0aa7c78cb46b5210cd69be2ffca64fd3cb0990116034c582828dd22d0235edf9ad999ef0b25afbcab802330d03e9653eff2dbee7f9e0a695a63e04d2aaef73152c255a1d8e5f9cc525cbcfd796ffff337f21d846ae7091037e2bfd06efaf262375100323335e62c79ca63aa31226e3655acab5f2861913630be567210d3d0d5b0f0a6bdc7edfc986e9c14b28b9d32deab5041872a26f8b95341a8cdf6326207d0c2f728ef85554f18c9e285c9f3e01e1d1cb1adf2546eeb9ddfc81a51b0fdf94c9f9116adcd5878815d21038968cbef2b51cc4a27fb1911008c6d1d587830645aca9ca775cf1d67dd9901aadb830a1e8abe0548a47619b8d80083316a645c646820640067653101c54f73164ab75f6650ea8970355bebd6f5162237379174d6afbc4a403e9d875d000800000000000000b100000000000000100c416374696f6e105472616e7366657212526563697069656e745671667a34427465626f714d556f4e536c74457077394b546462663736665252446667783841693644474a77105175616e746974791631303030303030303030301a446174612d50726f746f636f6c04616f0e56617269616e740e616f2e544e2e3108547970650e4d6573736167650653444b12616f636f6e6e65637418436f6e74656e742d5479706514746578742f706c61696e0037373037
let binary = hex::decode("01000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000a999bac8b7906c0bc94f7d163ea9e7fe6ef34045b6a27035e5298aaaddeea05355c50efd30da262c97a68b5aa7219726754bf8501818429e60b9f8175ed66a23349757dc8b3f126abc199272c91174bdb96a9a13aad43b9b6195583188c222002d29b105169dc237dccb0e371895aa10b9263e0b6fbe2d03d3a0073fa7f278ecfa890e75a3fe812ca86eb44f134a7edaa664a5582e72fa43b7accdfeb03f0492c118235b9ff7784106ca1a2f6e7bc4bcc6e1ed98775b7c023a1ae1e332f42e3183ab17c43c58e6605353a47331452ebf659fb267d27492b961ecdafcde9657a0a623aec761f6b3130f89ff7136cae26ebc58aaaa0c6c2264d8e0aa7c78cb46b5210cd69be2ffca64fd3cb0990116034c582828dd22d0235edf9ad999ef0b25afbcab802330d03e9653eff2dbee7f9e0a695a63e04d2aaef73152c255a1d8e5f9cc525cbcfd796ffff337f21d846ae7091037e2bfd06efaf262375100323335e62c79ca63aa31226e3655acab5f2861913630be567210d3d0d5b0f0a6bdc7edfc986e9c14b28b9d32deab5041872a26f8b95341a8cdf6326207d0c2f728ef85554f18c9e285c9f3e01e1d1cb1adf2546eeb9ddfc81a51b0fdf94c9f9116adcd5878815d21038968cbef2b51cc4a27fb1911008c6d1d587830645aca9ca775cf1d67dd9901aadb830a1e8abe0548a47619b8d80083316a645c646820640067653101c54f73164ab75f6650ea8970355bebd6f5162237379174d6afbc4a403e9d875d000800000000000000b100000000000000100c416374696f6e105472616e7366657212526563697069656e745671667a34427465626f714d556f4e536c74457077394b546462663736665252446667783841693644474a77105175616e746974791631303030303030303030301a446174612d50726f746f636f6c04616f0e56617269616e740e616f2e544e2e3108547970650e4d6573736167650653444b12616f636f6e6e65637418436f6e74656e742d5479706514746578742f706c61696e0037373037").unwrap();
let result = DataItem::deserialize(&binary).unwrap();
println!("{:?}", result);
assert_eq!(result.signature_type, 1);
assert_eq!(result.owner, "nSkowCiV4VBZJVyI2UK2wT_6g9LVX5BLZvYSTjd0bVQ");
assert_eq!(
Expand Down
4 changes: 0 additions & 4 deletions rust/apps/arweave/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,6 @@ pub mod transaction;
#[macro_use]
extern crate alloc;

#[cfg(test)]
#[macro_use]
extern crate std;

use crate::errors::{ArweaveError, Result};
use aes::cipher::block_padding::Pkcs7;
use aes::cipher::{generic_array::GenericArray, BlockDecryptMut, BlockEncryptMut, KeyIvInit};
Expand Down
33 changes: 33 additions & 0 deletions rust/apps/avalanche/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
[package]
name = "app_avalanche"
version = "0.1.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
keystore = { workspace = true, default-features = false }
app_utils = { workspace = true }
rust_tools = { workspace = true }
either = { workspace = true, default-features = false }
itertools = { workspace = true }
thiserror = { workspace = true }
core2 = { workspace = true }
bech32 = { workspace = true }
hex = { workspace = true }
ur-registry = { workspace = true }
serde = { workspace = true }
serde_json = { workspace = true }
base64 = { workspace = true }
cryptoxide = { workspace = true }
bitcoin = { workspace = true }
bytes = {version = "1.4.0", default-features = false}

[features]
no_std = []
default = ["std"]
std = []
testnet = []

[dev-dependencies]
keystore = { workspace = true }
116 changes: 116 additions & 0 deletions rust/apps/avalanche/src/address.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
use crate::constants::*;
use crate::errors::{AvaxError, Result};
#[cfg(feature = "testnet")]
use crate::network::TESTNET_ID;
use crate::network::{Network, MAINNET_ID};
use crate::ripple_keypair::hash160;
use crate::transactions::structs::ParsedSizeAble;
use alloc::string::{String, ToString};
use bech32::{self, Bech32};
use bytes::{Buf, Bytes};
use core::convert::TryFrom;
use keystore::algorithms::secp256k1::derive_public_key;

#[derive(Debug, Clone)]
pub struct Address {
address: [u8; ADDRESS_LEN],
}

impl ParsedSizeAble for Address {
fn parsed_size(&self) -> usize {
ADDRESS_LEN as usize
}
}

impl Address {
pub fn encode(&self) -> String {
bech32::encode::<Bech32>(bech32::Hrp::parse_unchecked("avax"), &self.address).unwrap()
}

pub fn to_evm_address(&self) -> String {
format!("0x{}", hex::encode(&self.address))
}
}

impl TryFrom<Bytes> for Address {
type Error = AvaxError;

fn try_from(mut bytes: Bytes) -> Result<Self> {
let mut address = [0u8; ADDRESS_LEN];
bytes.copy_to_slice(&mut address);
Ok(Address { address })
}
}

pub fn get_address(
network: Network,
hd_path: &str,
root_x_pub: &str,
root_path: &str,
) -> Result<String> {
let mut prefix = "avax";
match network {
Network::AvaxMainNet => {}
#[cfg(feature = "testnet")]
Network::AvaxTestNet => {
prefix = "fuji";
}
_ => {
return Err(AvaxError::UnsupportedNetwork(format!("{:?}", network)));
}
}

let root_path = if !root_path.ends_with('/') {
root_path.to_string() + "/"
} else {
root_path.to_string()
};

let public_key = derive_public_key(
&root_x_pub.to_string(),
&format!(
"m/{}",
hd_path
.strip_prefix(&root_path)
.ok_or(AvaxError::InvalidHDPath(hd_path.to_string()))?
),
)
.map_err(|e| AvaxError::InvalidHex(format!("derive public key error: {}", e)))?;
bech32::encode::<Bech32>(
bech32::Hrp::parse_unchecked(prefix),
&hash160(&public_key.serialize()),
)
.map_err(|e| AvaxError::InvalidHex(format!("bech32 encode error: {}", e)))
}

#[cfg(test)]
mod tests {
use super::*;
use alloc::string::ToString;
extern crate std;

#[test]
fn get_avax_address_test() {
{
let hd_path = "m/44'/9000'/0'/0/0";
let root_x_pub = "xpub6CPE4bhTujy9CeJJbyskjJsp8FGgyWBsWV2W9GfZwuP9aeDBEoPRBsLk3agq32Gp5gkb9nJSjCn9fgZmuvmV3nPLk5Bc2wfKUQZREp4eG13";
let root_path = "m/44'/9000'/0'";
let address = get_address(Network::AvaxMainNet, &hd_path, &root_x_pub, &root_path);
assert_eq!(
address.unwrap(),
"avax1fmlmwakmgkezcg95lk97m8p3tgc9anuxemenwh"
);
}
{
let prefix = "fuji";
let data = [
9, 105, 234, 98, 226, 187, 48, 230, 109, 130, 232, 47, 226, 103, 237, 246, 135, 30,
165, 247,
];
assert_eq!(
"fuji1p9575chzhvcwvmvzaqh7yeld76r3af0h3x77mq",
bech32::encode::<Bech32>(bech32::Hrp::parse_unchecked(prefix), &data).unwrap()
);
}
}
}
34 changes: 34 additions & 0 deletions rust/apps/avalanche/src/constants.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
pub const BLOCKCHAIN_ID_LEN: usize = 32;
pub const SUBNET_ID_LEN: usize = 32;
pub const SUBNET_AUTH_LEN: usize = 4;
pub const NODE_ID_LEN: usize = 20;
pub const PROOF_OF_POSESSION_PUBKEY_LEN: usize = 48;
pub const PROOF_OF_POSESSION_SIGNATURE_LEN: usize = 100;
pub const C_CHAIN_ADDRESS_LEN: usize = 20;
pub const ASSET_ID_LEN: usize = 32;
pub const ADDRESS_LEN: usize = 20;
pub const NAVAX_TO_AVAX_RATIO: f64 = 1_000_000_000.0;

pub const X_BLOCKCHAIN_ID: [u8; BLOCKCHAIN_ID_LEN] = [
237, 95, 56, 52, 30, 67, 110, 93, 70, 226, 187, 0, 180, 93, 98, 174, 151, 209, 176, 80, 198,
75, 198, 52, 174, 16, 98, 103, 57, 227, 92, 75,
];

pub const X_TEST_BLOCKCHAIN_ID: [u8; BLOCKCHAIN_ID_LEN] = [
171, 104, 235, 30, 225, 66, 160, 92, 254, 118, 140, 54, 225, 31, 11, 89, 109, 181, 163, 198,
199, 122, 171, 230, 101, 218, 217, 230, 56, 202, 148, 247,
];

pub const P_BLOCKCHAIN_ID: [u8; BLOCKCHAIN_ID_LEN] = [
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
];

pub const C_BLOCKCHAIN_ID: [u8; BLOCKCHAIN_ID_LEN] = [
4, 39, 212, 178, 42, 42, 120, 188, 221, 212, 86, 116, 44, 175, 145, 181, 107, 173, 191, 249,
133, 238, 25, 174, 241, 69, 115, 231, 52, 63, 214, 82,
];

pub const C_TEST_BLOCKCHAIN_ID: [u8; BLOCKCHAIN_ID_LEN] = [
127, 201, 61, 133, 198, 214, 44, 91, 42, 192, 181, 25, 200, 112, 16, 234, 82, 148, 1, 45, 30,
64, 112, 48, 214, 172, 208, 2, 28, 172, 16, 213,
];
27 changes: 27 additions & 0 deletions rust/apps/avalanche/src/encode/cb58.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
use alloc::string::String;
use alloc::string::ToString;
use bitcoin::base58;
use cryptoxide::digest::Digest;
use cryptoxide::sha2::Sha256;

pub trait Cb58Encodable {
fn get_prefix(&self) -> &'static str;
fn get_data(&self) -> &[u8];

fn to_cb58(&self) -> String {
let data = self.get_data();
let mut hasher = Sha256::new();
let mut checksum = [0u8; 32];
hasher.input(data);
hasher.result(&mut checksum);

let mut with_checksum = data.to_vec();
with_checksum.extend_from_slice(&checksum[checksum.len() - 4..]);

format!(
"{}-{}",
self.get_prefix(),
base58::encode(&with_checksum).to_string()
)
}
}
Loading
Loading