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: smart contract klv tx #96

Merged
merged 2 commits into from
Feb 5, 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
17 changes: 17 additions & 0 deletions packages/kos-web/src/wallet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -487,4 +487,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());
}
}
25 changes: 25 additions & 0 deletions packages/kos/src/chains/klv/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,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
3 changes: 3 additions & 0 deletions packages/kos/src/protos/generated/klv/proto.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1184,6 +1184,7 @@ pub mod tx_contract {
UpdateAccountPermissionContractType = 22,
DepositContractType = 23,
ItoTriggerContractType = 24,
SmartContractType = 63,
}
impl ContractType {
/// String value of the enum field names used in the ProtoBuf definition.
Expand Down Expand Up @@ -1229,6 +1230,7 @@ pub mod tx_contract {
}
ContractType::DepositContractType => "DepositContractType",
ContractType::ItoTriggerContractType => "ITOTriggerContractType",
ContractType::SmartContractType => "SmartContractType",
}
}
/// Creates an enum from field names used in the ProtoBuf definition.
Expand Down Expand Up @@ -1267,6 +1269,7 @@ pub mod tx_contract {
}
"DepositContractType" => Some(Self::DepositContractType),
"ITOTriggerContractType" => Some(Self::ItoTriggerContractType),
"SmartContractType" => Some(Self::SmartContractType),
_ => None,
}
}
Expand Down
1 change: 1 addition & 0 deletions packages/kos/src/protos/klever/transaction.proto
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ message TXContract {
UpdateAccountPermissionContractType = 22;
DepositContractType = 23;
ITOTriggerContractType = 24;
SmartContractType = 63;
}
ContractType Type = 1 [json_name = "Type"];
google.protobuf.Any Parameter = 2 [json_name = "Parameter"];
Expand Down