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

evm custom network #79

Merged
merged 1 commit into from
Jan 15, 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
86 changes: 79 additions & 7 deletions packages/kos-mobile/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@ pub mod number;

use hex::FromHexError;
use hex::ToHex;
use kos::chains::{get_chain_by_base_id, Chain, ChainError, Transaction};
use kos::crypto::cipher;
use kos::chains::{
create_custom_evm, get_chain_by_base_id, Chain, ChainError, ChainOptions, Transaction,
};
use kos::crypto::cipher::CipherAlgo;
use kos::crypto::{base64, cipher};

uniffi::setup_scaffolding!();

Expand Down Expand Up @@ -49,6 +51,42 @@ struct KOSTransaction {
pub signature: String,
}

#[derive(uniffi::Enum)]
enum TransactionChainOptions {
Evm {
chain_id: u32,
network_type: u32,
},
Btc {
prev_scripts: Vec<Vec<u8>>,
input_amounts: Vec<u64>,
},
}

#[uniffi::export]
fn new_bitcoin_transaction_options(
input_amounts: Vec<u64>,
prev_scripts: Vec<String>,
) -> TransactionChainOptions {
let prev_scripts = prev_scripts
.iter()
.map(|s| base64::simple_base64_decode(s).unwrap_or_default())
.collect();

TransactionChainOptions::Btc {
prev_scripts,
input_amounts,
}
}

#[uniffi::export]
fn new_evm_transaction_options(chain_id: u32, network_type: u32) -> TransactionChainOptions {
TransactionChainOptions::Evm {
chain_id,
network_type,
}
}

#[uniffi::export]
fn generate_mnemonic(size: i32) -> Result<String, KOSError> {
Ok(kos::crypto::mnemonic::generate_mnemonic(size as usize)?.to_phrase())
Expand Down Expand Up @@ -136,14 +174,48 @@ fn get_chain_by(id: u32) -> Result<Box<dyn Chain>, KOSError> {
}

#[uniffi::export]
fn sign_transaction(account: KOSAccount, raw: String) -> Result<KOSTransaction, KOSError> {
let chain = get_chain_by(account.chain_id)?;
fn sign_transaction(
account: KOSAccount,
raw: String,
options: Option<TransactionChainOptions>,
) -> Result<KOSTransaction, KOSError> {
let options = match options {
Some(TransactionChainOptions::Evm {
chain_id,
network_type,
}) => Some(ChainOptions::EVM {
chain_id,
network_type,
}),
Some(TransactionChainOptions::Btc {
prev_scripts,
input_amounts,
}) => Some(ChainOptions::BTC {
prev_scripts,
input_amounts,
}),
None => None,
};

let mut chain = get_chain_by(account.chain_id)?;

if let Some(ChainOptions::EVM {
chain_id,
network_type,
}) = options
{
chain = create_custom_evm(chain_id, network_type).ok_or(KOSError::KOSDelegate(
"Failed to create custom evm chain".to_string(),
))?;
}

let raw_tx_bytes = hex::decode(raw.clone())?;

let transaction = Transaction {
raw_data: raw_tx_bytes,
signature: Vec::new(),
tx_hash: Vec::new(),
options: None,
options,
};
let pk = hex::decode(account.private_key.clone())?;

Expand Down Expand Up @@ -374,9 +446,9 @@ mod tests {
0,
false,
)
.unwrap();
.unwrap();

let transaction = sign_transaction(account, raw.to_string()).unwrap();
let transaction = sign_transaction(account, raw.to_string(), None).unwrap();

assert_eq!(transaction.chain_id, chain_id, "The chain_id doesn't match");
assert_eq!(
Expand Down
21 changes: 17 additions & 4 deletions packages/kos/src/chains/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -363,7 +363,7 @@ impl ChainRegistry {
constants::BTC,
ChainInfo {
factory: || Box::new(btc::BTC::new()),
supported: false,
supported: true,
},
),
(
Expand All @@ -384,7 +384,7 @@ impl ChainRegistry {
constants::LTC,
ChainInfo {
factory: || Box::new(btc::BTC::new_btc_based(5, "ltc", "LTC", "Litecoin")),
supported: false,
supported: true,
},
),
(
Expand Down Expand Up @@ -419,7 +419,7 @@ impl ChainRegistry {
constants::SYS,
ChainInfo {
factory: || Box::new(btc::BTC::new_btc_based(15, "sys", "SYS", "Syscoin")),
supported: false,
supported: true,
},
),
(
Expand Down Expand Up @@ -463,7 +463,7 @@ impl ChainRegistry {
constants::DGB,
ChainInfo {
factory: || Box::new(btc::BTC::new_btc_based(16, "dgb", "DGB", "Digibyte")),
supported: false,
supported: true,
},
),
(
Expand Down Expand Up @@ -703,6 +703,15 @@ impl ChainRegistry {
}
ids
}

fn create_custom_evm(&self, chain_id: u32, _network_type: u32) -> Option<Box<dyn Chain>> {
Some(Box::new(eth::ETH::new_eth_based(
0,
chain_id,
format!("ETH {}", chain_id).as_str(),
format!("Eth Based {}", chain_id).as_str(),
)))
}
}

pub fn get_chain_by_id(id: u32) -> Option<Box<dyn Chain>> {
Expand Down Expand Up @@ -745,3 +754,7 @@ pub fn is_chain_supported(id: u32) -> bool {
pub fn get_supported_chains() -> Vec<u32> {
ChainRegistry::new().get_supported_chains()
}

pub fn create_custom_evm(chain_id: u32, network_type: u32) -> Option<Box<dyn Chain>> {
ChainRegistry::new().create_custom_evm(chain_id, network_type)
}
Loading