Skip to content

Commit

Permalink
Added rust-useable FromBytes trait
Browse files Browse the repository at this point in the history
The old FromBytes returns Result<T, JsValue> 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.
  • Loading branch information
rooooooooob committed Jul 3, 2020
1 parent 9a7904e commit 0fcb1c6
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 40 deletions.
7 changes: 3 additions & 4 deletions rust/src/crypto.rs
Expand Up @@ -284,7 +284,7 @@ impl Vkey {
}

pub fn from_bytes(data: Vec<u8>) -> Result<Vkey, JsValue> {
FromBytes::from_bytes(data)
WasmFromBytes::from_bytes(data)
}

pub fn new(pk: &PublicKey) -> Self {
Expand Down Expand Up @@ -318,7 +318,7 @@ impl Vkeywitness {
}

pub fn from_bytes(data: Vec<u8>) -> Result<Vkeywitness, JsValue> {
FromBytes::from_bytes(data)
WasmFromBytes::from_bytes(data)
}

pub fn new(vkey: &Vkey, signature: &Ed25519Signature) -> Self {
Expand Down Expand Up @@ -432,7 +432,7 @@ impl BootstrapWitness {
}

pub fn from_bytes(data: Vec<u8>) -> Result<BootstrapWitness, JsValue> {
FromBytes::from_bytes(data)
WasmFromBytes::from_bytes(data)
}

pub fn new(vkey: &Vkey, signature: &Ed25519Signature, index_2: Vec<u8>, index_3: Vec<u8>, index_4: Vec<u8>) -> Self {
Expand Down Expand Up @@ -631,7 +631,6 @@ macro_rules! impl_signature {
}

impl_signature!(Ed25519Signature, Vec<u8>, crypto::Ed25519);

macro_rules! impl_hash_type {
($name:ident, $byte_count:expr) => {
#[wasm_bindgen]
Expand Down
2 changes: 1 addition & 1 deletion rust/src/fees.rs
Expand Up @@ -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]
Expand Down
68 changes: 34 additions & 34 deletions rust/src/lib.rs
Expand Up @@ -32,7 +32,7 @@ impl UnitInterval {
}

pub fn from_bytes(data: Vec<u8>) -> Result<UnitInterval, JsValue> {
FromBytes::from_bytes(data)
WasmFromBytes::from_bytes(data)
}

pub fn new(index_0: u64, index_1: u64) -> Self {
Expand Down Expand Up @@ -62,7 +62,7 @@ impl Transaction {
}

pub fn from_bytes(data: Vec<u8>) -> Result<Transaction, JsValue> {
FromBytes::from_bytes(data)
WasmFromBytes::from_bytes(data)
}

pub fn new(body: &TransactionBody, witness_set: &TransactionWitnessSet, metadata: Option<TransactionMetadata>) -> Self {
Expand Down Expand Up @@ -164,7 +164,7 @@ impl TransactionBody {
}

pub fn from_bytes(data: Vec<u8>) -> Result<TransactionBody, JsValue> {
FromBytes::from_bytes(data)
WasmFromBytes::from_bytes(data)
}

pub fn set_certs(&mut self, certs: &Certificates) {
Expand Down Expand Up @@ -206,7 +206,7 @@ impl TransactionInput {
}

pub fn from_bytes(data: Vec<u8>) -> Result<TransactionInput, JsValue> {
FromBytes::from_bytes(data)
WasmFromBytes::from_bytes(data)
}

pub fn new(transaction_id: &TransactionHash, index: u32) -> Self {
Expand All @@ -231,7 +231,7 @@ impl TransactionOutput {
}

pub fn from_bytes(data: Vec<u8>) -> Result<TransactionOutput, JsValue> {
FromBytes::from_bytes(data)
WasmFromBytes::from_bytes(data)
}

pub fn new(address: &Address, amount: Coin) -> Self {
Expand All @@ -255,7 +255,7 @@ impl StakeRegistration {
}

pub fn from_bytes(data: Vec<u8>) -> Result<StakeRegistration, JsValue> {
FromBytes::from_bytes(data)
WasmFromBytes::from_bytes(data)
}

pub fn new(stake_credential: &StakeCredential) -> Self {
Expand All @@ -278,7 +278,7 @@ impl StakeDeregistration {
}

pub fn from_bytes(data: Vec<u8>) -> Result<StakeDeregistration, JsValue> {
FromBytes::from_bytes(data)
WasmFromBytes::from_bytes(data)
}

pub fn new(stake_credential: &StakeCredential) -> Self {
Expand All @@ -302,7 +302,7 @@ impl StakeDelegation {
}

pub fn from_bytes(data: Vec<u8>) -> Result<StakeDelegation, JsValue> {
FromBytes::from_bytes(data)
WasmFromBytes::from_bytes(data)
}

pub fn new(stake_credential: &StakeCredential, pool_keyhash: &PoolKeyHash) -> Self {
Expand Down Expand Up @@ -380,7 +380,7 @@ impl PoolParams {
}

pub fn from_bytes(data: Vec<u8>) -> Result<PoolParams, JsValue> {
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<PoolMetadata>) -> Self {
Expand Down Expand Up @@ -411,7 +411,7 @@ impl PoolRegistration {
}

pub fn from_bytes(data: Vec<u8>) -> Result<PoolRegistration, JsValue> {
FromBytes::from_bytes(data)
WasmFromBytes::from_bytes(data)
}

pub fn new(pool_params: &PoolParams) -> Self {
Expand All @@ -435,7 +435,7 @@ impl PoolRetirement {
}

pub fn from_bytes(data: Vec<u8>) -> Result<PoolRetirement, JsValue> {
FromBytes::from_bytes(data)
WasmFromBytes::from_bytes(data)
}

pub fn new(pool_keyhash: &PoolKeyHash, epoch: Epoch) -> Self {
Expand All @@ -460,7 +460,7 @@ impl GenesisKeyDelegation {
}

pub fn from_bytes(data: Vec<u8>) -> Result<GenesisKeyDelegation, JsValue> {
FromBytes::from_bytes(data)
WasmFromBytes::from_bytes(data)
}

pub fn new(genesishash: &GenesisHash, genesis_delegate_hash: &GenesisDelegateHash) -> Self {
Expand All @@ -484,7 +484,7 @@ impl MoveInstantaneousRewardsCert {
}

pub fn from_bytes(data: Vec<u8>) -> Result<MoveInstantaneousRewardsCert, JsValue> {
FromBytes::from_bytes(data)
WasmFromBytes::from_bytes(data)
}

pub fn new(move_instantaneous_reward: &MoveInstantaneousReward) -> Self {
Expand Down Expand Up @@ -516,7 +516,7 @@ impl Certificate {
}

pub fn from_bytes(data: Vec<u8>) -> Result<Certificate, JsValue> {
FromBytes::from_bytes(data)
WasmFromBytes::from_bytes(data)
}

pub fn new_stake_registration(stake_registration: &StakeRegistration) -> Self {
Expand Down Expand Up @@ -565,7 +565,7 @@ impl I0OrI1 {
}

pub fn from_bytes(data: Vec<u8>) -> Result<I0OrI1, JsValue> {
FromBytes::from_bytes(data)
WasmFromBytes::from_bytes(data)
}

pub fn new_i0() -> Self {
Expand All @@ -588,7 +588,7 @@ impl MapStakeCredentialToCoin {
}

pub fn from_bytes(data: Vec<u8>) -> Result<MapStakeCredentialToCoin, JsValue> {
FromBytes::from_bytes(data)
WasmFromBytes::from_bytes(data)
}

pub fn new() -> Self {
Expand Down Expand Up @@ -618,7 +618,7 @@ impl MoveInstantaneousReward {
}

pub fn from_bytes(data: Vec<u8>) -> Result<MoveInstantaneousReward, JsValue> {
FromBytes::from_bytes(data)
WasmFromBytes::from_bytes(data)
}

pub fn new(index_0: &I0OrI1, index_1: &MapStakeCredentialToCoin) -> Self {
Expand All @@ -642,7 +642,7 @@ impl Ipv4 {
}

pub fn from_bytes(data: Vec<u8>) -> Result<Ipv4, JsValue> {
FromBytes::from_bytes(data)
WasmFromBytes::from_bytes(data)
}

pub fn new(data: Vec<u8>) -> Self {
Expand All @@ -661,7 +661,7 @@ impl Ipv6 {
}

pub fn from_bytes(data: Vec<u8>) -> Result<Ipv6, JsValue> {
FromBytes::from_bytes(data)
WasmFromBytes::from_bytes(data)
}

pub fn new(data: Vec<u8>) -> Self {
Expand All @@ -686,7 +686,7 @@ impl SingleHostAddr {
}

pub fn from_bytes(data: Vec<u8>) -> Result<SingleHostAddr, JsValue> {
FromBytes::from_bytes(data)
WasmFromBytes::from_bytes(data)
}

pub fn new(port: Option<Port>, ipv4: Option<Ipv4>, ipv6: Option<Ipv6>) -> Self {
Expand All @@ -712,7 +712,7 @@ impl SingleHostName {
}

pub fn from_bytes(data: Vec<u8>) -> Result<SingleHostName, JsValue> {
FromBytes::from_bytes(data)
WasmFromBytes::from_bytes(data)
}

pub fn new(port: Option<Port>, dns_name: DnsName) -> Self {
Expand All @@ -736,7 +736,7 @@ impl MultiHostName {
}

pub fn from_bytes(data: Vec<u8>) -> Result<MultiHostName, JsValue> {
FromBytes::from_bytes(data)
WasmFromBytes::from_bytes(data)
}

pub fn new(dns_name: DnsName) -> Self {
Expand Down Expand Up @@ -764,7 +764,7 @@ impl Relay {
}

pub fn from_bytes(data: Vec<u8>) -> Result<Relay, JsValue> {
FromBytes::from_bytes(data)
WasmFromBytes::from_bytes(data)
}

pub fn new_single_host_addr(single_host_addr: &SingleHostAddr) -> Self {
Expand Down Expand Up @@ -794,7 +794,7 @@ impl PoolMetadata {
}

pub fn from_bytes(data: Vec<u8>) -> Result<PoolMetadata, JsValue> {
FromBytes::from_bytes(data)
WasmFromBytes::from_bytes(data)
}

pub fn new(url: Url, metadata_hash: &MetadataHash) -> Self {
Expand All @@ -818,7 +818,7 @@ impl Withdrawals {
}

pub fn from_bytes(data: Vec<u8>) -> Result<Withdrawals, JsValue> {
FromBytes::from_bytes(data)
WasmFromBytes::from_bytes(data)
}

pub fn new() -> Self {
Expand Down Expand Up @@ -872,7 +872,7 @@ impl TransactionWitnessSet {
}

pub fn from_bytes(data: Vec<u8>) -> Result<TransactionWitnessSet, JsValue> {
FromBytes::from_bytes(data)
WasmFromBytes::from_bytes(data)
}

pub fn set_vkeys(&mut self, vkeys: &Vkeywitnesses) {
Expand Down Expand Up @@ -907,7 +907,7 @@ impl MapTransactionMetadatumToTransactionMetadatum {
}

pub fn from_bytes(data: Vec<u8>) -> Result<MapTransactionMetadatumToTransactionMetadatum, JsValue> {
FromBytes::from_bytes(data)
WasmFromBytes::from_bytes(data)
}

pub fn new() -> Self {
Expand Down Expand Up @@ -966,7 +966,7 @@ impl TransactionMetadatum {
}

pub fn from_bytes(data: Vec<u8>) -> Result<TransactionMetadatum, JsValue> {
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 {
Expand Down Expand Up @@ -1003,7 +1003,7 @@ impl TransactionMetadata {
}

pub fn from_bytes(data: Vec<u8>) -> Result<TransactionMetadata, JsValue> {
FromBytes::from_bytes(data)
WasmFromBytes::from_bytes(data)
}

pub fn new() -> Self {
Expand Down Expand Up @@ -1032,7 +1032,7 @@ impl MsigPubkey {
}

pub fn from_bytes(data: Vec<u8>) -> Result<MsigPubkey, JsValue> {
FromBytes::from_bytes(data)
WasmFromBytes::from_bytes(data)
}

pub fn new(addr_keyhash: &AddrKeyHash) -> Self {
Expand All @@ -1055,7 +1055,7 @@ impl MsigAll {
}

pub fn from_bytes(data: Vec<u8>) -> Result<MsigAll, JsValue> {
FromBytes::from_bytes(data)
WasmFromBytes::from_bytes(data)
}

pub fn new(multisig_scripts: &MultisigScripts) -> Self {
Expand All @@ -1078,7 +1078,7 @@ impl MsigAny {
}

pub fn from_bytes(data: Vec<u8>) -> Result<MsigAny, JsValue> {
FromBytes::from_bytes(data)
WasmFromBytes::from_bytes(data)
}

pub fn new(multisig_scripts: &MultisigScripts) -> Self {
Expand All @@ -1102,7 +1102,7 @@ impl MsigNOfK {
}

pub fn from_bytes(data: Vec<u8>) -> Result<MsigNOfK, JsValue> {
FromBytes::from_bytes(data)
WasmFromBytes::from_bytes(data)
}

pub fn new(n: u32, multisig_scripts: &MultisigScripts) -> Self {
Expand Down Expand Up @@ -1132,7 +1132,7 @@ impl MultisigScript {
}

pub fn from_bytes(data: Vec<u8>) -> Result<MultisigScript, JsValue> {
FromBytes::from_bytes(data)
WasmFromBytes::from_bytes(data)
}

pub fn new_msig_pubkey(addr_keyhash: &AddrKeyHash) -> Self {
Expand Down
15 changes: 14 additions & 1 deletion rust/src/prelude.rs
Expand Up @@ -169,10 +169,23 @@ impl<T: cbor_event::se::Serialize> ToBytes for T {
}

pub trait FromBytes {
fn from_bytes(data: Vec<u8>) -> Result<Self, JsValue> where Self: Sized;
fn from_bytes(data: &[u8]) -> Result<Self, DeserializeError> where Self: Sized;
}

impl<T: Deserialize + Sized> FromBytes for T {
fn from_bytes(data: &[u8]) -> Result<Self, DeserializeError> {
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<u8>) -> Result<Self, JsValue> where Self: Sized;
}

impl<T: Deserialize + Sized> WasmFromBytes for T {
fn from_bytes(data: Vec<u8>) -> Result<Self, JsValue> {
let mut raw = Deserializer::from(std::io::Cursor::new(data));
Self::deserialize(&mut raw).map_err(|e| JsValue::from_str(&e.to_string()))
Expand Down

0 comments on commit 0fcb1c6

Please sign in to comment.