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

Promote new version #99

Merged
merged 4 commits into from
Feb 12, 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
7 changes: 4 additions & 3 deletions Cargo.lock

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

4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ homepage = "https://klever.org/"
license = "Apache-2.0"
repository = "https://github.com/kleverio/kos-rs"
rust-version = "1.69.0"
version = "0.2.4"
version = "0.2.5"

[workspace.dependencies]
bech32 = "0.9.1"
Expand Down Expand Up @@ -50,5 +50,5 @@ log = "0.4"
lazy_static = "1.4.0"
thiserror = "1.0"

kos = { version = "0.2.4", path = "./packages/kos", default-features = false }
kos = { version = "0.2.5", path = "./packages/kos", default-features = false }
kos-mobile = { version = "0.1.0", path = "./packages/kos-mobile", default-features = false }
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
62 changes: 62 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 Expand Up @@ -487,4 +532,21 @@ mod tests {
let message = b"test message";
assert!(wallet.sign_message(message).is_err());
}

#[test]
fn test_sign_transaction() {
let chain_id = 38;
let chain = get_chain_by_base_id(chain_id).unwrap();
let path = chain.get_path(0, false);

let wallet =
Wallet::from_mnemonic(chain_id, TEST_MNEMONIC.to_string(), path, None).unwrap();

let tx_raw = r#"{"RawData":{"Sender":"UMjR49Dkn+HleedQY88TSjXXJhtbDpX7f7QVF/Dcqos=","Contract":[{"Type":63,"Parameter":{"type_url":"type.googleapis.com/proto.SmartContract","value":"EiAAAAAAAAAAAAUAIPnuq04LIuz1ew83LbqEVgLiyNyybBoRCghGUkctMlZCVRIFCIDh6xc="}}],"Data":["c3Rha2VGYXJt"],"KAppFee":2000000,"BandwidthFee":4622449,"Version":1,"ChainID":"MTAwMDAx"}}"#;

let signed_tx = wallet.sign(tx_raw.as_bytes(), None).unwrap();

assert!(!signed_tx.signature.is_empty());
assert!(!signed_tx.tx_hash.is_empty());
}
}
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
55 changes: 55 additions & 0 deletions packages/kos/src/chains/klv/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,36 @@ mod test {
);
}

#[test]
fn test_pvk_32() {
let pvk = hex::decode("8734062c1158f26a3ca8a4a0da87b527a7c168653f7f4c77045e5cf571497d9d")
.unwrap();

let pbk = crate::chains::klv::KLV {}.get_pbk(pvk).unwrap();

let address = crate::chains::klv::KLV {}.get_address(pbk.clone()).unwrap();
assert_eq!(
address,
"klv1usdnywjhrlv4tcyu6stxpl6yvhplg35nepljlt4y5r7yppe8er4qujlazy"
);
assert_eq!(pbk.len(), 32);
}

#[test]
fn test_pvk_64() {
let pvk = hex::decode("8734062c1158f26a3ca8a4a0da87b527a7c168653f7f4c77045e5cf571497d9de41b323a571fd955e09cd41660ff4465c3f44693c87f2faea4a0fc408727c8ea")
.unwrap();

let pbk = crate::chains::klv::KLV {}.get_pbk(pvk).unwrap();

let address = crate::chains::klv::KLV {}.get_address(pbk.clone()).unwrap();
assert_eq!(
address,
"klv1usdnywjhrlv4tcyu6stxpl6yvhplg35nepljlt4y5r7yppe8er4qujlazy"
);
assert_eq!(pbk.len(), 32);
}

#[test]
fn test_sign_raw() {
let mnemonic =
Expand Down Expand Up @@ -285,6 +315,31 @@ mod test {
);
}

#[test]
fn test_sign_tx_4() {
let pvk = hex::decode("1ab42cc412b618bdea3a599e3c9bae199ebf030895b039e9db1e30dafb12b727")
.unwrap();

let json = r#"{"RawData":{"Sender":"UMjR49Dkn+HleedQY88TSjXXJhtbDpX7f7QVF/Dcqos=","Contract":[{"Type":63,"Parameter":{"type_url":"type.googleapis.com/proto.SmartContract","value":"EiAAAAAAAAAAAAUAIPnuq04LIuz1ew83LbqEVgLiyNyybBoRCghGUkctMlZCVRIFCIDh6xc="}}],"Data":["c3Rha2VGYXJt"],"KAppFee":2000000,"BandwidthFee":4622449,"Version":1,"ChainID":"MTAwMDAx"}}"#;

let raw_tx = json.as_bytes().to_vec();

let tx = crate::chains::Transaction {
raw_data: raw_tx,
tx_hash: Vec::new(),
signature: Vec::new(),
options: None,
};

let result_tx = crate::chains::klv::KLV {}.sign_tx(pvk, tx).unwrap();

assert_eq!(
result_tx.tx_hash,
hex::decode("50fce82cb3f4bf851d86fd594133b13e891d1f565958b0a95b11ce47f2179926")
.unwrap()
);
}

#[test]
fn test_decode_klv_tx() {
let raw_tx = hex::decode(
Expand Down
4 changes: 2 additions & 2 deletions packages/kos/src/chains/klv/models.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ pub struct Transaction {
#[derive(Clone, PartialEq, Serialize, Deserialize)]
pub struct Raw {
#[Rename = "Nonce"]
pub nonce: u64,
pub nonce: Option<u64>,
#[Rename = "Sender"]
pub sender: String,
#[Rename = "Contract"]
Expand Down Expand Up @@ -151,7 +151,7 @@ impl TryFrom<chains::klv::models::Raw> for proto::transaction::Raw {
.collect::<Result<Vec<_>, _>>()?;

let proto_raw = proto::transaction::Raw {
nonce: value.nonce,
nonce: value.nonce.unwrap_or(0),
sender: simple_base64_decode(&value.sender)
.map_err(|_| ConversionError::Base64Error)?,
contract: contracts,
Expand Down
Loading
Loading