|
| 1 | +mod chains; |
| 2 | + |
| 3 | +use crate::chains::ada; |
| 4 | +use kos::chains::{get_chain_by_base_id, Chain, ChainError, ChainType, Transaction}; |
| 5 | + |
| 6 | +pub fn encode_for_signing( |
| 7 | + chain_id: u32, |
| 8 | + transaction: Transaction, |
| 9 | +) -> Result<Transaction, ChainError> { |
| 10 | + let chain = match get_chain_by_base_id(chain_id) { |
| 11 | + Some(chain) => chain, |
| 12 | + None => return Err(ChainError::UnsupportedChain), |
| 13 | + }; |
| 14 | + |
| 15 | + Ok(match chain.get_chain_type() { |
| 16 | + ChainType::ETH => transaction, |
| 17 | + ChainType::BTC => transaction, |
| 18 | + ChainType::TRX => transaction, |
| 19 | + ChainType::KLV => transaction, |
| 20 | + ChainType::SUBSTRATE => transaction, |
| 21 | + ChainType::XRP => transaction, |
| 22 | + ChainType::ICP => transaction, |
| 23 | + ChainType::SOL => transaction, |
| 24 | + ChainType::ADA => ada::encode_for_sign(transaction)?, |
| 25 | + ChainType::SUI => transaction, |
| 26 | + ChainType::APT => transaction, |
| 27 | + ChainType::ATOM => transaction, |
| 28 | + ChainType::BCH => transaction, |
| 29 | + ChainType::BNB => transaction, |
| 30 | + }) |
| 31 | +} |
| 32 | + |
| 33 | +pub fn encode_for_broadcast( |
| 34 | + chain: Box<dyn Chain>, |
| 35 | + transaction: Transaction, |
| 36 | +) -> Result<Transaction, ChainError> { |
| 37 | + Ok(match chain.get_chain_type() { |
| 38 | + ChainType::ETH => transaction, |
| 39 | + ChainType::BTC => transaction, |
| 40 | + ChainType::TRX => transaction, |
| 41 | + ChainType::KLV => transaction, |
| 42 | + ChainType::SUBSTRATE => transaction, |
| 43 | + ChainType::XRP => transaction, |
| 44 | + ChainType::ICP => transaction, |
| 45 | + ChainType::SOL => transaction, |
| 46 | + ChainType::ADA => ada::encode_for_broadcast(transaction)?, |
| 47 | + ChainType::SUI => transaction, |
| 48 | + ChainType::APT => transaction, |
| 49 | + ChainType::ATOM => transaction, |
| 50 | + ChainType::BCH => transaction, |
| 51 | + ChainType::BNB => transaction, |
| 52 | + }) |
| 53 | +} |
| 54 | + |
| 55 | +#[cfg(test)] |
| 56 | +mod test { |
| 57 | + use super::*; |
| 58 | + |
| 59 | + #[test] |
| 60 | + fn test_encode() { |
| 61 | + let tx = Transaction { |
| 62 | + raw_data: vec![], |
| 63 | + tx_hash: vec![], |
| 64 | + signature: vec![], |
| 65 | + options: None, |
| 66 | + }; |
| 67 | + |
| 68 | + let result = encode_for_signing(20, tx); |
| 69 | + |
| 70 | + assert!(result.is_ok()); |
| 71 | + } |
| 72 | +} |
0 commit comments