Skip to content

Commit

Permalink
fix: browser klv tx (#93)
Browse files Browse the repository at this point in the history
  • Loading branch information
pedrxlz authored Feb 3, 2025
1 parent cfad8ae commit 43739a6
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 3 deletions.
34 changes: 32 additions & 2 deletions packages/kos/src/chains/klv/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,13 @@ impl Chain for KLV {
mut tx: Transaction,
) -> Result<Transaction, ChainError> {
let raw_tx = tx.raw_data;
let mut js_tx: models::Transaction =
tiny_json_rs::decode(String::from_utf8(raw_tx.clone())?)?;

// Parse [] empty arrays to [""] to avoid decoding errors
let json = String::from_utf8(raw_tx.clone())?;
let parsed = json.replace("[]", "[\"\"]").as_bytes().to_vec();

let mut js_tx: models::Transaction = tiny_json_rs::decode(String::from_utf8(parsed)?)?;

let klv_tx = proto::Transaction::try_from(js_tx.clone())
.map_err(|_| ChainError::ProtoDecodeError)?;

Expand Down Expand Up @@ -255,6 +260,31 @@ mod test {
);
}

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

let json = r#"{"Signature":[],"RawData":{"Contract":[{"Parameter":{"value":"CiAErjVpczGsXwth+8y37LCGSr5O6tPlR9nduy2Np+8wyBIDS0xWGMCEPQ==","type_url":"type.googleapis.com\/proto.TransferContract"}}],"Nonce":580,"BandwidthFee":2000000,"Data":[""],"ChainID":"MTA4","Version":1,"Sender":"SqQFUzLDtZNzXexeY++BMVH5GTKLDMSxo72GoRqjZz0=","KAppFee":1000000}}"#;

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("cb2741a67bb2e84f21ac892c9b6577446955debe9c9ef40c1799d212e617a55f")
.unwrap()
);
}

#[test]
fn test_decode_klv_tx() {
let raw_tx = hex::decode(
Expand Down
4 changes: 3 additions & 1 deletion packages/kos/src/chains/klv/models.rs
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,9 @@ impl TryFrom<chains::klv::models::TxContract> for proto::TxContract {
type Error = ConversionError;

fn try_from(value: chains::klv::models::TxContract) -> Result<Self, Self::Error> {
let contract_name = value.parameter.type_url.clone();
// Remove escapes
let contract_name = value.parameter.type_url.replace("\\", "");

//Remove the "type.googleapis.com/" prefix
let contract_name = contract_name
.strip_prefix("type.googleapis.com/proto.")
Expand Down

0 comments on commit 43739a6

Please sign in to comment.