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

Base/Enterprise/PointerAddress WASM bindings #291

Merged
merged 1 commit into from
Dec 29, 2023
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
2 changes: 1 addition & 1 deletion chain/rust/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
// possibly impacting PartialOrd performance on top of being unnecessary and occuring in generated code.
// Possibly the derivative crate could get updated to suppress this lint
// from within their proc macros itself. Issue: https://github.com/mcarton/rust-derivative/issues/115
#![allow(clippy::incorrect_partial_ord_impl_on_ord_type)]
#![allow(clippy::non_canonical_partial_ord_impl)]

pub mod address;
pub mod assets;
Expand Down
163 changes: 146 additions & 17 deletions chain/wasm/src/address.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ use super::*;

pub use cml_chain::address::{AddressHeaderKind, AddressKind};

use cml_core_wasm::impl_wasm_conversions;
use cml_core::CertificateIndex;
use cml_core_wasm::{impl_wasm_conversions, impl_wasm_json_api};

use crate::certs::StakeCredential;

Expand All @@ -12,6 +13,8 @@ pub struct Address(cml_chain::address::Address);

impl_wasm_conversions!(cml_chain::address::Address, Address);

impl_wasm_json_api!(Address);

#[wasm_bindgen]
impl Address {
/// header has 4 bits addr type discrim then 4 bits network discrim.
Expand Down Expand Up @@ -112,18 +115,147 @@ impl Address {
.map(Into::into)
.map_err(Into::into)
}
}

#[wasm_bindgen]
#[derive(Clone, Debug)]
pub struct BaseAddress(cml_chain::address::BaseAddress);

impl_wasm_conversions!(cml_chain::address::BaseAddress, BaseAddress);

#[wasm_bindgen]
impl BaseAddress {
pub fn new(network: u8, payment: &StakeCredential, stake: &StakeCredential) -> Self {
Self(cml_chain::address::BaseAddress::new(
network,
payment.as_ref().clone(),
stake.as_ref().clone(),
))
}

pub fn to_address(&self) -> Address {
Address(self.0.clone().to_address())
}

pub fn from_address(address: &Address) -> Option<BaseAddress> {
match &address.0 {
cml_chain::address::Address::Base(ba) => Some(ba.clone().into()),
_ => None,
}
}

pub fn network_id(&self) -> u8 {
self.0.network
}

pub fn payment(&self) -> StakeCredential {
self.0.payment.clone().into()
}

pub fn stake(&self) -> StakeCredential {
self.0.stake.clone().into()
}
}

#[wasm_bindgen]
#[derive(Clone, Debug)]
pub struct EnterpriseAddress(cml_chain::address::EnterpriseAddress);

impl_wasm_conversions!(cml_chain::address::EnterpriseAddress, EnterpriseAddress);

#[wasm_bindgen]
impl EnterpriseAddress {
pub fn new(network: u8, payment: &StakeCredential) -> Self {
Self(cml_chain::address::EnterpriseAddress::new(
network,
payment.as_ref().clone(),
))
}

pub fn to_address(&self) -> Address {
Address(self.0.clone().to_address())
}

pub fn from_address(address: &Address) -> Option<EnterpriseAddress> {
match &address.0 {
cml_chain::address::Address::Enterprise(ea) => Some(ea.clone().into()),
_ => None,
}
}

pub fn network_id(&self) -> u8 {
self.0.network
}

pub fn payment(&self) -> StakeCredential {
self.0.payment.clone().into()
}
}

#[wasm_bindgen]
#[derive(Clone, Debug)]
pub struct Pointer(cml_chain::address::Pointer);

impl_wasm_conversions!(cml_chain::address::Pointer, Pointer);

impl Pointer {
pub fn new(slot: Slot, tx_index: TransactionIndex, cert_index: CertificateIndex) -> Self {
Self(cml_chain::address::Pointer::new(slot, tx_index, cert_index))
}

/// This will be truncated if above u64::MAX
pub fn slot(&self) -> Slot {
self.0.slot()
}

/// This will be truncated if above u64::MAX
pub fn tx_index(&self) -> Slot {
self.0.tx_index()
}

/// This will be truncated if above u64::MAX
pub fn cert_index(&self) -> Slot {
self.0.cert_index()
}
}

#[wasm_bindgen]
#[derive(Clone, Debug)]
pub struct PointerAddress(cml_chain::address::PointerAddress);

impl_wasm_conversions!(cml_chain::address::PointerAddress, PointerAddress);

#[wasm_bindgen]
impl PointerAddress {
pub fn new(network: u8, payment: &StakeCredential, stake: &Pointer) -> Self {
Self(cml_chain::address::PointerAddress::new(
network,
payment.as_ref().clone(),
stake.as_ref().clone(),
))
}

pub fn to_address(&self) -> Address {
Address(self.0.clone().to_address())
}

pub fn to_json(&self) -> Result<String, JsError> {
serde_json::to_string_pretty(&self.0).map_err(Into::into)
pub fn from_address(address: &Address) -> Option<PointerAddress> {
match &address.0 {
cml_chain::address::Address::Ptr(pa) => Some(pa.clone().into()),
_ => None,
}
}

pub fn to_json_value(&self) -> Result<JsValue, JsError> {
serde_wasm_bindgen::to_value(&self.0)
.map_err(|e| JsError::new(&format!("Address::to_js_value: {e}")))
pub fn network_id(&self) -> u8 {
self.0.network
}

pub fn from_json(json: &str) -> Result<Address, JsError> {
serde_json::from_str(json).map(Self).map_err(Into::into)
pub fn payment(&self) -> StakeCredential {
self.0.payment.clone().into()
}

pub fn stake(&self) -> Pointer {
self.0.stake.clone().into()
}
}

Expand All @@ -135,6 +267,8 @@ pub struct RewardAddress(cml_chain::address::RewardAddress);

impl_wasm_conversions!(cml_chain::address::RewardAddress, RewardAddress);

impl_wasm_json_api!(RewardAddress);

#[wasm_bindgen]
impl RewardAddress {
pub fn new(network: u8, payment: &StakeCredential) -> Self {
Expand All @@ -155,16 +289,11 @@ impl RewardAddress {
}
}

pub fn to_json(&self) -> Result<String, JsError> {
serde_json::to_string_pretty(&self.0).map_err(Into::into)
}

pub fn to_json_value(&self) -> Result<JsValue, JsError> {
serde_wasm_bindgen::to_value(&self.0)
.map_err(|e| JsError::new(&format!("RewardAddress::to_js_value: {e}")))
pub fn network_id(&self) -> u8 {
self.0.network
}

pub fn from_json(json: &str) -> Result<RewardAddress, JsError> {
serde_json::from_str(json).map(Self).map_err(Into::into)
pub fn payment(&self) -> StakeCredential {
self.0.payment.clone().into()
}
}
2 changes: 1 addition & 1 deletion core/rust/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
// possibly impacting PartialOrd performance on top of being unnecessary and occuring in generated code.
// Possibly the derivative crate could get updated to suppress this lint
// from within their proc macros itself. Issue: https://github.com/mcarton/rust-derivative/issues/115
#![allow(clippy::incorrect_partial_ord_impl_on_ord_type)]
#![allow(clippy::non_canonical_partial_ord_impl)]

pub use error::*;

Expand Down
Loading