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

feat/icp #101

Open
wants to merge 2 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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
60 changes: 57 additions & 3 deletions packages/kos/src/chains/icp/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,39 @@ impl Chain for ICP {
Ok(addr)
}

fn sign_tx(&self, _private_key: Vec<u8>, _tx: Transaction) -> Result<Transaction, ChainError> {
todo!()
fn sign_tx(
&self,
private_key: Vec<u8>,
mut tx: Transaction,
) -> Result<Transaction, ChainError> {
let hex = hex::decode(tx.raw_data.clone()).unwrap();
let raw_data_str = String::from_utf8(hex).map_err(|_| ChainError::DecodeRawTx)?;

let icp_hashes: Vec<String> =
serde_json::from_str(raw_data_str.as_str()).map_err(|_| ChainError::DecodeHash)?;

let mut pvk_bytes = private_key_from_vec(&private_key)?;

let mut signatures: Vec<String> = Vec::new();

for hash_hex in icp_hashes {
let hash_bytes = hex::decode(&hash_hex).map_err(|_| ChainError::DecodeHash)?;

let signature = Ed25519::sign(&pvk_bytes, &hash_bytes)?;
signatures.push(hex::encode(signature));
}

pvk_bytes.fill(0);

let signatures_json = tiny_json_rs::encode(signatures);

tx.signature = signatures_json.into_bytes();

if tx.tx_hash.is_empty() {
tx.tx_hash = Vec::new();
}

Ok(tx)
}

fn sign_message(
Expand Down Expand Up @@ -148,8 +179,8 @@ pub fn crc_calc_singletable(buffer: &[u8]) -> u32 {

#[cfg(test)]
mod test {

use super::*;
use crate::crypto::base64::simple_base64_decode;
#[test]
fn test_icp_get_address() {
let icp = ICP {};
Expand All @@ -166,4 +197,27 @@ mod test {
"11d238129427ef0e44d86bd27cb6d9da4d7e8934cb0306a93a540e657082d885"
);
}

#[test]
fn test_icp_sign_tx() {
let icp = ICP {};

let mnemonic = "abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon about".to_string();
let seed = icp.mnemonic_to_seed(mnemonic, "".to_string()).unwrap();
let path = icp.get_path(0, false);
let pvk = icp.derive(seed, path).unwrap();

let raw_data = simple_base64_decode("NWIyMjMwNjEzNjM5MzYzMzMyNjQzNzMyMzYzNTM3MzEzNzM1MzYzNTM3MzMzNzM0MzUzMjMxMzc2NTM1MzYzNjM0Mzk2NjY2MzIzMTMyMzgzMzM2NjYzMDYxMzIzMjM2MzIzNjM3NjYzMzMwNjM2MTMwMzA2NTM4NjUzNDY2MzI2NTM4MzMzNDYyNjEzNjYzMzA2NDMyNjQzODMyMzQzMjM3MzIzNjM5MzAzNzMwMzAyMjJjMjIzMDYxMzYzOTM2MzMzMjY0MzczMjM2MzUzNzMxMzczNTM2MzUzNzMzMzczNDMxNjIzMTM0MzA2MjYyNjQzNzM2MzUzNzMxNjMzMDYxMzMzNDMzMzMzMTMzMzEzODM1MzQzMzM3NjI2NTM4NjIzODYyNjMzNDY0MzU2NTMyMzgzOTM4MzAzMTYxNjU2MTMxMzc2NTYxMzAzNjYxNjYzMjMwNjQzMTY1MzkzNzM1MjI1ZA==").unwrap();

let tx = Transaction {
raw_data,
signature: vec![],
tx_hash: vec![],
options: None,
};

let signed_tx = icp.sign_tx(pvk, tx).unwrap();

println!("{:?}", String::from_utf8(signed_tx.signature).unwrap());
}
}
10 changes: 10 additions & 0 deletions packages/kos/src/chains/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@ pub enum ChainError {
MissingOptions,
InvalidOptions,
InvalidHex,
DecodeRawTx,
DecodeHash,
}

impl Display for ChainError {
Expand Down Expand Up @@ -127,6 +129,12 @@ impl Display for ChainError {
ChainError::InvalidHex => {
write!(f, "invalid hex")
}
ChainError::DecodeRawTx => {
write!(f, "decode raw tx")
}
ChainError::DecodeHash => {
write!(f, "decode hash")
}
}
}
}
Expand Down Expand Up @@ -218,6 +226,8 @@ impl ChainError {
ChainError::MissingOptions => 21,
ChainError::InvalidOptions => 22,
ChainError::InvalidHex => 23,
ChainError::DecodeRawTx => 24,
ChainError::DecodeHash => 25,
}
}
}
Expand Down
Loading