Skip to content

Commit

Permalink
wip: feat(namada): decoding transactions from rust/wasm, pt.2
Browse files Browse the repository at this point in the history
  • Loading branch information
egasimus committed Mar 12, 2024
1 parent 110d6e5 commit bf86e5c
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 11 deletions.
1 change: 1 addition & 0 deletions packages/namada/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
name = "fadroma-namada"
version = "0.1.0"
edition = "2021"
resolver = "2"

[lib]
crate-type = [ "cdylib", "rlib" ]
Expand Down
60 changes: 49 additions & 11 deletions packages/namada/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
extern crate wasm_bindgen;
use wasm_bindgen::prelude::*;
use js_sys::{Uint8Array, JsString, Error, Object, Array, Reflect};
use namada::address::Address;
use namada::string_encoding::Format;
use namada::governance::parameters::GovernanceParameters;
use namada::tx::{Tx, Section};
use namada::tx::data::TxType;
use js_sys::{Uint8Array, JsString, Error, Object, Array, Reflect, BigInt, NULL};

use masp_primitives::consensus::BranchId;
use masp_primitives::transaction::Transaction;
use masp_primitives::transaction::TxVersion;
use namada::address::Address;
use namada::core::borsh::BorshDeserialize;
//use namada::core::masp_primitives::TxVersion;
use namada::governance::parameters::GovernanceParameters;
use namada::storage::KeySeg;
use namada::string_encoding::Format;
use namada::tx::data::TxType;
use namada::tx::{Tx, Section};

#[wasm_bindgen]
pub struct Decode;
Expand Down Expand Up @@ -98,12 +102,39 @@ impl Decode {
]),
Section::MaspTx(transaction) => populate(&section, &[
("type".into(), "MaspTx".into()),
("txid".into(), transaction.txid().into()),
("version".into(), transaction.version().into()),
("consensusBranchId".into(), transaction.consensus_branch_id().into()),
("txid".into(), format!("{}", transaction.txid()).into()),
//("version".into(), match transaction.version() {
//TxVersion::MASPv5 => "MASPv5"
//}.into()),
//("consensusBranchId".into(), match transaction.consensus_branch_id() {
//BranchID::MASP => "MASP"
//}.into()),
("lockTime".into(), transaction.lock_time().into()),
("expiryHeight".into(), transaction.expiry_height().into()),
("transparentBundle".into(), transaction.transparent_bundle().into()),
("expiryHeight".into(), format!("{}", transaction.expiry_height()).into()),
("transparentBundle".into(), if let Some(bundle_data) = transaction.transparent_bundle() {
let vin = Array::new();
for tx_data in bundle_data.vin.iter() {
vin.push(&object(&[
("assetType".into(), format!("{}", tx_data.asset_type).into()),
("value".into(), *BigInt::from(tx_data.value).as_ref()),
("address".into(), hex::encode_upper(tx_data.address.0).into()),
])?.into());
}
let vout = Array::new();
for tx_data in bundle_data.vout.iter() {
vout.push(&object(&[
("assetType".into(), format!("{}", tx_data.asset_type).into()),
("value".into(), *BigInt::from(tx_data.value).as_ref()),
("address".into(), hex::encode_upper(tx_data.address.0).into()),
])?.into());
}
object(&[
("vin".into(), vin.into()),
("vout".into(), vout.into()),
])?.into()
} else {
NULL
}),
("saplingBundle".into(), transaction.sapling_bundle().into()),
("digest".into(), transaction.digest().into()),
("saplingValueBalance".into(), transaction.sapling_value_balance().into()),
Expand Down Expand Up @@ -147,6 +178,13 @@ fn populate (object: &Object, fields: &[(JsValue, JsValue)]) -> Result<(), Error
Ok(())
}

#[inline]
fn object (fields: &[(JsValue, JsValue)]) -> Result<Object, Error> {
let object = Object::new();
populate(&object, fields)?;
Ok(object)
}

#[inline]
fn to_bytes (source: &Uint8Array) -> Vec<u8> {
let mut bytes: Vec<u8> = vec![0u8; source.length() as usize];
Expand Down

0 comments on commit bf86e5c

Please sign in to comment.