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

Fix/substrate sign tx #89

Merged
merged 15 commits into from
Feb 11, 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
1 change: 1 addition & 0 deletions Cargo.lock

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

64 changes: 64 additions & 0 deletions packages/kos-mobile/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ pub mod number;

use hex::FromHexError;
use hex::ToHex;
use kos::chains::util::hex_string_to_vec;
use kos::chains::{
create_custom_evm, get_chain_by_base_id, Chain, ChainError, ChainOptions, Transaction,
};
Expand Down Expand Up @@ -60,6 +61,48 @@ enum TransactionChainOptions {
prev_scripts: Vec<Vec<u8>>,
input_amounts: Vec<u64>,
},
Substrate {
call: Vec<u8>,
era: Vec<u8>,
nonce: u32,
tip: u8,
block_hash: Vec<u8>,
genesis_hash: Vec<u8>,
spec_version: u32,
transaction_version: u32,
app_id: Option<u32>,
},
}

#[allow(clippy::too_many_arguments)]
#[uniffi::export]
fn new_substrate_transaction_options(
call: String,
era: String,
nonce: u32,
tip: u8,
block_hash: String,
genesis_hash: String,
spec_version: u32,
transaction_version: u32,
app_id: Option<u32>,
) -> TransactionChainOptions {
let call = hex_string_to_vec(call.as_str()).unwrap_or_default();
let era = hex_string_to_vec(era.as_str()).unwrap_or_default();
let block_hash = hex_string_to_vec(block_hash.as_str()).unwrap_or_default();
let genesis_hash = hex_string_to_vec(genesis_hash.as_str()).unwrap_or_default();

TransactionChainOptions::Substrate {
call,
era,
nonce,
tip,
block_hash,
genesis_hash,
spec_version,
transaction_version,
app_id,
}
}

#[uniffi::export]
Expand Down Expand Up @@ -184,6 +227,27 @@ fn sign_transaction(
prev_scripts,
input_amounts,
}),
Some(TransactionChainOptions::Substrate {
call,
era,
nonce,
tip,
block_hash,
genesis_hash,
spec_version,
transaction_version,
app_id,
}) => Some(ChainOptions::SUBSTRATE {
call,
era,
nonce,
tip,
block_hash,
genesis_hash,
spec_version,
transaction_version,
app_id,
}),
None => None,
};

Expand Down
45 changes: 45 additions & 0 deletions packages/kos-web/src/wallet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use strum::{EnumCount, IntoStaticStr};

use crate::error::Error;
use crate::utils::unpack;
use kos::chains::util::hex_string_to_vec;
use kos::chains::{get_chain_by_base_id, ChainOptions, Transaction as KosTransaction};
use kos::crypto::base64;
use wasm_bindgen::prelude::*;
Expand Down Expand Up @@ -60,6 +61,50 @@ impl TransactionChainOptions {
},
}
}

#[wasm_bindgen(js_name = "newEthereumSignOptions")]
pub fn new_ethereum_sign_options(chain_id: u32) -> TransactionChainOptions {
TransactionChainOptions {
data: ChainOptions::EVM { chain_id },
}
}

#[allow(clippy::too_many_arguments)]
#[wasm_bindgen(js_name = "newSubstrateSignOptions")]
pub fn new_substrate_sign_options(
call: String,
era: String,
nonce: u32,
tip: u8,
block_hash: String,
genesis_hash: String,
spec_version: u32,
transaction_version: u32,
app_id: Option<u32>,
) -> Result<TransactionChainOptions, Error> {
let call = hex_string_to_vec(call.as_str())
.map_err(|e| Error::WalletManager(format!("Invalid call hex: {}", e)))?;
let era = hex_string_to_vec(era.as_str())
.map_err(|e| Error::WalletManager(format!("Invalid era hex: {}", e)))?;
let block_hash = hex_string_to_vec(block_hash.as_str())
.map_err(|e| Error::WalletManager(format!("Invalid block hash hex: {}", e)))?;
let genesis_hash = hex_string_to_vec(genesis_hash.as_str())
.map_err(|e| Error::WalletManager(format!("Invalid genesis hash hex: {}", e)))?;

Ok(TransactionChainOptions {
data: ChainOptions::SUBSTRATE {
call,
era,
nonce,
tip,
block_hash,
genesis_hash,
spec_version,
transaction_version,
app_id,
},
})
}
}

#[wasm_bindgen]
Expand Down
1 change: 1 addition & 0 deletions packages/kos/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ pem = "3"
cfb-mode = "0.8"
cbc = { version = "0.1", features = ["block-padding", "std"] }
pbkdf2 = { version = "0.12", features = ["simple"] }
serde = { version = "1.0.215", features = ["derive"] }

[build-dependencies]
prost-build = "0.12.1"
Expand Down
38 changes: 27 additions & 11 deletions packages/kos/src/chains/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ mod sol;
mod substrate;
mod sui;
pub mod trx;
mod util;
pub mod util;
mod xrp;

#[derive(Debug)]
Expand All @@ -58,6 +58,7 @@ pub enum ChainError {
InvalidData(String),
MissingOptions,
InvalidOptions,
InvalidHex,
}

impl Display for ChainError {
Expand Down Expand Up @@ -123,6 +124,9 @@ impl Display for ChainError {
ChainError::InvalidOptions => {
write!(f, "invalid option")
}
ChainError::InvalidHex => {
write!(f, "invalid hex")
}
}
}
}
Expand Down Expand Up @@ -213,6 +217,7 @@ impl ChainError {
ChainError::InvalidData(_) => 20,
ChainError::MissingOptions => 21,
ChainError::InvalidOptions => 22,
ChainError::InvalidHex => 23,
}
}
}
Expand Down Expand Up @@ -263,6 +268,17 @@ pub enum ChainOptions {
prev_scripts: Vec<Vec<u8>>,
input_amounts: Vec<u64>,
},
SUBSTRATE {
call: Vec<u8>,
era: Vec<u8>,
nonce: u32,
tip: u8,
block_hash: Vec<u8>,
genesis_hash: Vec<u8>,
spec_version: u32,
transaction_version: u32,
app_id: Option<u32>,
},
}

#[allow(dead_code)]
Expand Down Expand Up @@ -369,14 +385,14 @@ impl ChainRegistry {
constants::DOT,
ChainInfo {
factory: || Box::new(substrate::Substrate::new(21, 0, "DOT", "Polkadot")),
supported: false,
supported: true,
},
),
(
constants::KSM,
ChainInfo {
factory: || Box::new(substrate::Substrate::new(27, 2, "KSM", "Kusama")),
supported: false,
supported: true,
},
),
(
Expand All @@ -390,28 +406,28 @@ impl ChainRegistry {
constants::REEF,
ChainInfo {
factory: || Box::new(substrate::Substrate::new(29, 42, "REEF", "Reef")),
supported: false,
supported: true,
},
),
(
constants::SDN,
ChainInfo {
factory: || Box::new(substrate::Substrate::new(35, 5, "SDN", "Shiden")),
supported: false,
supported: true,
},
),
(
constants::ASTR,
ChainInfo {
factory: || Box::new(substrate::Substrate::new(36, 5, "ASTR", "Astar")),
supported: false,
supported: true,
},
),
(
constants::CFG,
ChainInfo {
factory: || Box::new(substrate::Substrate::new(47, 36, "CFG", "Centrifuge")),
supported: false,
supported: true,
},
),
(
Expand All @@ -425,14 +441,14 @@ impl ChainRegistry {
constants::KILT,
ChainInfo {
factory: || Box::new(substrate::Substrate::new(44, 38, "KILT", "KILT")),
supported: false,
supported: true,
},
),
(
constants::ALTAIR,
ChainInfo {
factory: || Box::new(substrate::Substrate::new(42, 136, "ALTAIR", "Altair")),
supported: false,
supported: true,
},
),
(
Expand Down Expand Up @@ -571,8 +587,8 @@ impl ChainRegistry {
(
constants::AVAIL,
ChainInfo {
factory: || Box::new(substrate::Substrate::new(62, 42, "AVAIL", "Avail")),
supported: false,
factory: || Box::new(substrate::Substrate::new(62, 42, "Avail", "AVAIL")),
supported: true,
},
),
(
Expand Down
Loading