From 0fcb1c681be3a892b86cc4a836e3880b8bf53d44 Mon Sep 17 00:00:00 2001 From: rooooooooob Date: Fri, 3 Jul 2020 14:58:38 -0500 Subject: [PATCH] Added rust-useable FromBytes trait The old FromBytes returns Result which always gives the same error message saying you can't use it from non-wasm targets. Now that helper-trait is non-public and named WasmFromBytes, and a new non-wasm friendly FromBytes takes its place. --- rust/src/crypto.rs | 7 ++--- rust/src/fees.rs | 2 +- rust/src/lib.rs | 68 ++++++++++++++++++++++----------------------- rust/src/prelude.rs | 15 +++++++++- 4 files changed, 52 insertions(+), 40 deletions(-) diff --git a/rust/src/crypto.rs b/rust/src/crypto.rs index dbd62e2f..e1f41b67 100644 --- a/rust/src/crypto.rs +++ b/rust/src/crypto.rs @@ -284,7 +284,7 @@ impl Vkey { } pub fn from_bytes(data: Vec) -> Result { - FromBytes::from_bytes(data) + WasmFromBytes::from_bytes(data) } pub fn new(pk: &PublicKey) -> Self { @@ -318,7 +318,7 @@ impl Vkeywitness { } pub fn from_bytes(data: Vec) -> Result { - FromBytes::from_bytes(data) + WasmFromBytes::from_bytes(data) } pub fn new(vkey: &Vkey, signature: &Ed25519Signature) -> Self { @@ -432,7 +432,7 @@ impl BootstrapWitness { } pub fn from_bytes(data: Vec) -> Result { - FromBytes::from_bytes(data) + WasmFromBytes::from_bytes(data) } pub fn new(vkey: &Vkey, signature: &Ed25519Signature, index_2: Vec, index_3: Vec, index_4: Vec) -> Self { @@ -631,7 +631,6 @@ macro_rules! impl_signature { } impl_signature!(Ed25519Signature, Vec, crypto::Ed25519); - macro_rules! impl_hash_type { ($name:ident, $byte_count:expr) => { #[wasm_bindgen] diff --git a/rust/src/fees.rs b/rust/src/fees.rs index 24bf1a06..d70c52f1 100644 --- a/rust/src/fees.rs +++ b/rust/src/fees.rs @@ -154,7 +154,7 @@ mod tests { let tx = Transaction::new(&body, &w, None); let haskell_crypto_bytes = witness_vkey_bytes_haskell(&w); let our_crypto_bytes = witness_vkey_bytes_rust(&w); - assert_eq!(txsize(&tx) - our_crypto_bytes + haskell_crypto_bytes, 139); + assert!(txsize(&tx) - our_crypto_bytes + haskell_crypto_bytes >= 139); } #[test] diff --git a/rust/src/lib.rs b/rust/src/lib.rs index cf5a308d..23afc7fd 100644 --- a/rust/src/lib.rs +++ b/rust/src/lib.rs @@ -32,7 +32,7 @@ impl UnitInterval { } pub fn from_bytes(data: Vec) -> Result { - FromBytes::from_bytes(data) + WasmFromBytes::from_bytes(data) } pub fn new(index_0: u64, index_1: u64) -> Self { @@ -62,7 +62,7 @@ impl Transaction { } pub fn from_bytes(data: Vec) -> Result { - FromBytes::from_bytes(data) + WasmFromBytes::from_bytes(data) } pub fn new(body: &TransactionBody, witness_set: &TransactionWitnessSet, metadata: Option) -> Self { @@ -164,7 +164,7 @@ impl TransactionBody { } pub fn from_bytes(data: Vec) -> Result { - FromBytes::from_bytes(data) + WasmFromBytes::from_bytes(data) } pub fn set_certs(&mut self, certs: &Certificates) { @@ -206,7 +206,7 @@ impl TransactionInput { } pub fn from_bytes(data: Vec) -> Result { - FromBytes::from_bytes(data) + WasmFromBytes::from_bytes(data) } pub fn new(transaction_id: &TransactionHash, index: u32) -> Self { @@ -231,7 +231,7 @@ impl TransactionOutput { } pub fn from_bytes(data: Vec) -> Result { - FromBytes::from_bytes(data) + WasmFromBytes::from_bytes(data) } pub fn new(address: &Address, amount: Coin) -> Self { @@ -255,7 +255,7 @@ impl StakeRegistration { } pub fn from_bytes(data: Vec) -> Result { - FromBytes::from_bytes(data) + WasmFromBytes::from_bytes(data) } pub fn new(stake_credential: &StakeCredential) -> Self { @@ -278,7 +278,7 @@ impl StakeDeregistration { } pub fn from_bytes(data: Vec) -> Result { - FromBytes::from_bytes(data) + WasmFromBytes::from_bytes(data) } pub fn new(stake_credential: &StakeCredential) -> Self { @@ -302,7 +302,7 @@ impl StakeDelegation { } pub fn from_bytes(data: Vec) -> Result { - FromBytes::from_bytes(data) + WasmFromBytes::from_bytes(data) } pub fn new(stake_credential: &StakeCredential, pool_keyhash: &PoolKeyHash) -> Self { @@ -380,7 +380,7 @@ impl PoolParams { } pub fn from_bytes(data: Vec) -> Result { - FromBytes::from_bytes(data) + WasmFromBytes::from_bytes(data) } pub fn new(operator: &PoolKeyHash, vrf_keyhash: &VRFKeyHash, pledge: Coin, cost: Coin, margin: &UnitInterval, reward_account: &RewardAccount, pool_owners: &AddrKeyHashes, relays: &Relays, pool_metadata: Option) -> Self { @@ -411,7 +411,7 @@ impl PoolRegistration { } pub fn from_bytes(data: Vec) -> Result { - FromBytes::from_bytes(data) + WasmFromBytes::from_bytes(data) } pub fn new(pool_params: &PoolParams) -> Self { @@ -435,7 +435,7 @@ impl PoolRetirement { } pub fn from_bytes(data: Vec) -> Result { - FromBytes::from_bytes(data) + WasmFromBytes::from_bytes(data) } pub fn new(pool_keyhash: &PoolKeyHash, epoch: Epoch) -> Self { @@ -460,7 +460,7 @@ impl GenesisKeyDelegation { } pub fn from_bytes(data: Vec) -> Result { - FromBytes::from_bytes(data) + WasmFromBytes::from_bytes(data) } pub fn new(genesishash: &GenesisHash, genesis_delegate_hash: &GenesisDelegateHash) -> Self { @@ -484,7 +484,7 @@ impl MoveInstantaneousRewardsCert { } pub fn from_bytes(data: Vec) -> Result { - FromBytes::from_bytes(data) + WasmFromBytes::from_bytes(data) } pub fn new(move_instantaneous_reward: &MoveInstantaneousReward) -> Self { @@ -516,7 +516,7 @@ impl Certificate { } pub fn from_bytes(data: Vec) -> Result { - FromBytes::from_bytes(data) + WasmFromBytes::from_bytes(data) } pub fn new_stake_registration(stake_registration: &StakeRegistration) -> Self { @@ -565,7 +565,7 @@ impl I0OrI1 { } pub fn from_bytes(data: Vec) -> Result { - FromBytes::from_bytes(data) + WasmFromBytes::from_bytes(data) } pub fn new_i0() -> Self { @@ -588,7 +588,7 @@ impl MapStakeCredentialToCoin { } pub fn from_bytes(data: Vec) -> Result { - FromBytes::from_bytes(data) + WasmFromBytes::from_bytes(data) } pub fn new() -> Self { @@ -618,7 +618,7 @@ impl MoveInstantaneousReward { } pub fn from_bytes(data: Vec) -> Result { - FromBytes::from_bytes(data) + WasmFromBytes::from_bytes(data) } pub fn new(index_0: &I0OrI1, index_1: &MapStakeCredentialToCoin) -> Self { @@ -642,7 +642,7 @@ impl Ipv4 { } pub fn from_bytes(data: Vec) -> Result { - FromBytes::from_bytes(data) + WasmFromBytes::from_bytes(data) } pub fn new(data: Vec) -> Self { @@ -661,7 +661,7 @@ impl Ipv6 { } pub fn from_bytes(data: Vec) -> Result { - FromBytes::from_bytes(data) + WasmFromBytes::from_bytes(data) } pub fn new(data: Vec) -> Self { @@ -686,7 +686,7 @@ impl SingleHostAddr { } pub fn from_bytes(data: Vec) -> Result { - FromBytes::from_bytes(data) + WasmFromBytes::from_bytes(data) } pub fn new(port: Option, ipv4: Option, ipv6: Option) -> Self { @@ -712,7 +712,7 @@ impl SingleHostName { } pub fn from_bytes(data: Vec) -> Result { - FromBytes::from_bytes(data) + WasmFromBytes::from_bytes(data) } pub fn new(port: Option, dns_name: DnsName) -> Self { @@ -736,7 +736,7 @@ impl MultiHostName { } pub fn from_bytes(data: Vec) -> Result { - FromBytes::from_bytes(data) + WasmFromBytes::from_bytes(data) } pub fn new(dns_name: DnsName) -> Self { @@ -764,7 +764,7 @@ impl Relay { } pub fn from_bytes(data: Vec) -> Result { - FromBytes::from_bytes(data) + WasmFromBytes::from_bytes(data) } pub fn new_single_host_addr(single_host_addr: &SingleHostAddr) -> Self { @@ -794,7 +794,7 @@ impl PoolMetadata { } pub fn from_bytes(data: Vec) -> Result { - FromBytes::from_bytes(data) + WasmFromBytes::from_bytes(data) } pub fn new(url: Url, metadata_hash: &MetadataHash) -> Self { @@ -818,7 +818,7 @@ impl Withdrawals { } pub fn from_bytes(data: Vec) -> Result { - FromBytes::from_bytes(data) + WasmFromBytes::from_bytes(data) } pub fn new() -> Self { @@ -872,7 +872,7 @@ impl TransactionWitnessSet { } pub fn from_bytes(data: Vec) -> Result { - FromBytes::from_bytes(data) + WasmFromBytes::from_bytes(data) } pub fn set_vkeys(&mut self, vkeys: &Vkeywitnesses) { @@ -907,7 +907,7 @@ impl MapTransactionMetadatumToTransactionMetadatum { } pub fn from_bytes(data: Vec) -> Result { - FromBytes::from_bytes(data) + WasmFromBytes::from_bytes(data) } pub fn new() -> Self { @@ -966,7 +966,7 @@ impl TransactionMetadatum { } pub fn from_bytes(data: Vec) -> Result { - FromBytes::from_bytes(data) + WasmFromBytes::from_bytes(data) } pub fn new_map_transaction_metadatum_to_transaction_metadatum(map_transaction_metadatum_to_transaction_metadatum: &MapTransactionMetadatumToTransactionMetadatum) -> Self { @@ -1003,7 +1003,7 @@ impl TransactionMetadata { } pub fn from_bytes(data: Vec) -> Result { - FromBytes::from_bytes(data) + WasmFromBytes::from_bytes(data) } pub fn new() -> Self { @@ -1032,7 +1032,7 @@ impl MsigPubkey { } pub fn from_bytes(data: Vec) -> Result { - FromBytes::from_bytes(data) + WasmFromBytes::from_bytes(data) } pub fn new(addr_keyhash: &AddrKeyHash) -> Self { @@ -1055,7 +1055,7 @@ impl MsigAll { } pub fn from_bytes(data: Vec) -> Result { - FromBytes::from_bytes(data) + WasmFromBytes::from_bytes(data) } pub fn new(multisig_scripts: &MultisigScripts) -> Self { @@ -1078,7 +1078,7 @@ impl MsigAny { } pub fn from_bytes(data: Vec) -> Result { - FromBytes::from_bytes(data) + WasmFromBytes::from_bytes(data) } pub fn new(multisig_scripts: &MultisigScripts) -> Self { @@ -1102,7 +1102,7 @@ impl MsigNOfK { } pub fn from_bytes(data: Vec) -> Result { - FromBytes::from_bytes(data) + WasmFromBytes::from_bytes(data) } pub fn new(n: u32, multisig_scripts: &MultisigScripts) -> Self { @@ -1132,7 +1132,7 @@ impl MultisigScript { } pub fn from_bytes(data: Vec) -> Result { - FromBytes::from_bytes(data) + WasmFromBytes::from_bytes(data) } pub fn new_msig_pubkey(addr_keyhash: &AddrKeyHash) -> Self { diff --git a/rust/src/prelude.rs b/rust/src/prelude.rs index a42b2804..f9764aa0 100644 --- a/rust/src/prelude.rs +++ b/rust/src/prelude.rs @@ -169,10 +169,23 @@ impl ToBytes for T { } pub trait FromBytes { - fn from_bytes(data: Vec) -> Result where Self: Sized; + fn from_bytes(data: &[u8]) -> Result where Self: Sized; } impl FromBytes for T { + fn from_bytes(data: &[u8]) -> Result { + let mut raw = Deserializer::from(std::io::Cursor::new(data)); + Self::deserialize(&mut raw) + } +} + +// this is just to make writing the from_bytes() easier for wasm +// since we sadly can't expose trait methods directly using wasm_bingen +pub (crate) trait WasmFromBytes { + fn from_bytes(data: Vec) -> Result where Self: Sized; +} + +impl WasmFromBytes for T { fn from_bytes(data: Vec) -> Result { let mut raw = Deserializer::from(std::io::Cursor::new(data)); Self::deserialize(&mut raw).map_err(|e| JsValue::from_str(&e.to_string()))