Skip to content

Commit 4f8d175

Browse files
Merge pull request #118 from klever-io/feat/kos-codec
add kos-codec as a dependency
2 parents 98f6630 + 9925466 commit 4f8d175

File tree

25 files changed

+267
-305
lines changed

25 files changed

+267
-305
lines changed

Cargo.lock

+8
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
# https://doc.rust-lang.org/edition-guide/rust-2021/default-cargo-resolver.html#details
33
resolver = "2"
44
members = [
5-
"packages/kos", "packages/kos-mobile", "packages/kos-web",
5+
"packages/kos", "packages/kos-codec", "packages/kos-mobile", "packages/kos-web",
66
]
77
# This makes the compiled code faster and smaller, but it makes compiling slower,
88
# so it's only enabled in release mode.

packages/kos-codec/Cargo.toml

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
[package]
2+
name = "kos-codec"
3+
authors.workspace = true
4+
edition.workspace = true
5+
homepage.workspace = true
6+
license.workspace = true
7+
repository.workspace = true
8+
rust-version.workspace = true
9+
version.workspace = true
10+
11+
12+
[lib]
13+
name = "kos_codec"
14+
crate-type = ["lib"]
15+
16+
[dependencies]
17+
kos = { workspace = true }
+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
use kos::chains::{ChainError, Transaction};
2+
3+
pub fn encode_for_sign(transaction: Transaction) -> Result<Transaction, ChainError> {
4+
Ok(transaction)
5+
}
6+
7+
pub fn encode_for_broadcast(transaction: Transaction) -> Result<Transaction, ChainError> {
8+
Ok(transaction)
9+
}

packages/kos-codec/src/chains/mod.rs

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
pub mod ada;
2+
pub mod xrp;
+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
use kos::chains::{ChainError, Transaction};
2+
3+
pub fn _encode_for_sign(transaction: Transaction) -> Result<Transaction, ChainError> {
4+
Ok(transaction)
5+
}

packages/kos-codec/src/lib.rs

+72
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
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+
}

packages/kos-mobile/Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ path = "src/bin/uniffi-bindgen.rs"
2020

2121
[dependencies]
2222
kos = { workspace = true }
23+
kos-codec = { path = "../kos-codec" }
2324

2425
hex = { workspace = true }
2526
thiserror = { workspace = true }

packages/kos-mobile/src/lib.rs

+5-1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ use kos::chains::{
88
};
99
use kos::crypto::cipher::CipherAlgo;
1010
use kos::crypto::{base64, cipher};
11+
use kos_codec::encode_for_signing;
1112

1213
uniffi::setup_scaffolding!();
1314

@@ -267,9 +268,12 @@ fn sign_transaction(
267268
tx_hash: Vec::new(),
268269
options,
269270
};
271+
272+
let encoded = encode_for_signing(account.chain_id, transaction)?;
273+
270274
let pk = hex::decode(account.private_key.clone())?;
271275

272-
let signed_transaction = chain.sign_tx(pk, transaction)?;
276+
let signed_transaction = chain.sign_tx(pk, encoded)?;
273277
let signature = signed_transaction.signature;
274278

275279
Ok(KOSTransaction {

packages/kos/src/chains/ada/mod.rs

+5-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ mod address;
22

33
use crate::chains::ada::address::{Address, AddressType, StakeCredential};
44
use crate::chains::util::private_key_from_vec;
5-
use crate::chains::{util, Chain, ChainError, Transaction, TxInfo};
5+
use crate::chains::{util, Chain, ChainError, ChainType, Transaction, TxInfo};
66
use crate::crypto::bip32::{derive_ed25519_bip32, mnemonic_to_seed_ed25519_bip32};
77
use crate::crypto::ed25519;
88
use crate::crypto::ed25519::Ed25519Trait;
@@ -133,6 +133,10 @@ impl Chain for ADA {
133133
fn get_tx_info(&self, _raw_tx: Vec<u8>) -> Result<TxInfo, ChainError> {
134134
Err(ChainError::NotSupported)
135135
}
136+
137+
fn get_chain_type(&self) -> ChainType {
138+
ChainType::ADA
139+
}
136140
}
137141

138142
#[cfg(test)]

packages/kos/src/chains/apt/mod.rs

+5-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use crate::chains::util::private_key_from_vec;
2-
use crate::chains::{Chain, ChainError, Transaction, TxInfo};
2+
use crate::chains::{Chain, ChainError, ChainType, Transaction, TxInfo};
33
use crate::crypto::bip32;
44
use crate::crypto::ed25519::{Ed25519, Ed25519Trait};
55
use crate::crypto::hash::sha3_digest;
@@ -84,6 +84,10 @@ impl Chain for APT {
8484
fn get_tx_info(&self, _raw_tx: Vec<u8>) -> Result<TxInfo, ChainError> {
8585
Err(ChainError::NotSupported)
8686
}
87+
88+
fn get_chain_type(&self) -> ChainType {
89+
ChainType::APT
90+
}
8791
}
8892

8993
#[cfg(test)]

packages/kos/src/chains/atom/mod.rs

+5-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use crate::chains::util::{private_key_from_vec, slice_from_vec};
2-
use crate::chains::{Chain, ChainError, Transaction, TxInfo};
2+
use crate::chains::{Chain, ChainError, ChainType, Transaction, TxInfo};
33
use crate::crypto::hash::ripemd160_digest;
44
use crate::crypto::secp256k1::{Secp256K1, Secp256k1Trait};
55
use crate::crypto::{bip32, secp256k1};
@@ -123,6 +123,10 @@ impl Chain for ATOM {
123123
fn get_tx_info(&self, _raw_tx: Vec<u8>) -> Result<TxInfo, ChainError> {
124124
Err(ChainError::NotSupported)
125125
}
126+
127+
fn get_chain_type(&self) -> ChainType {
128+
ChainType::ATOM
129+
}
126130
}
127131

128132
#[cfg(test)]

packages/kos/src/chains/bch/mod.rs

+5-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use crate::chains::util::{private_key_from_vec, slice_from_vec};
2-
use crate::chains::{Chain, ChainError, Transaction, TxInfo};
2+
use crate::chains::{Chain, ChainError, ChainType, Transaction, TxInfo};
33
use crate::crypto::bip32;
44
use crate::crypto::hash::ripemd160_digest;
55
use crate::crypto::secp256k1::{Secp256K1, Secp256k1Trait};
@@ -162,6 +162,10 @@ impl Chain for BCH {
162162
fn get_tx_info(&self, _raw_tx: Vec<u8>) -> Result<TxInfo, ChainError> {
163163
Err(ChainError::NotSupported)
164164
}
165+
166+
fn get_chain_type(&self) -> ChainType {
167+
ChainType::BCH
168+
}
165169
}
166170

167171
#[cfg(test)]

packages/kos/src/chains/bnb/mod.rs

-118
This file was deleted.

packages/kos/src/chains/btc/mod.rs

+5-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use crate::chains::util::{private_key_from_vec, slice_from_vec};
2-
use crate::chains::{Chain, ChainError, ChainOptions, Transaction, TxInfo};
2+
use crate::chains::{Chain, ChainError, ChainOptions, ChainType, Transaction, TxInfo};
33
use crate::crypto::b58::b58enc;
44
use crate::crypto::bip32;
55
use crate::crypto::hash::{ripemd160_digest, sha256_digest};
@@ -291,6 +291,10 @@ impl Chain for BTC {
291291
fn get_tx_info(&self, _raw_tx: Vec<u8>) -> Result<TxInfo, ChainError> {
292292
Err(ChainError::NotSupported)
293293
}
294+
295+
fn get_chain_type(&self) -> ChainType {
296+
ChainType::BTC
297+
}
294298
}
295299

296300
#[cfg(test)]

0 commit comments

Comments
 (0)