Skip to content

Commit b8ced44

Browse files
committed
feat: WASM compatible build
Signed-off-by: gsstoykov <[email protected]>
1 parent 5b874d5 commit b8ced44

32 files changed

+502
-95
lines changed

Cargo.lock

Lines changed: 5 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

hedera-proto-wasm/Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ prost = { version = "0.13.5", default-features = false, features = ["std", "pros
1212
getrandom = { version = "0.2", features = ["js"] }
1313
wasm-bindgen = "0.2"
1414
js-sys = "0.3"
15+
time = { version = "0.3", default-features = false }
16+
fraction = { version = "0.15", default-features = false, features = ["with-bigint"] }
1517

1618
[dependencies.web-sys]
1719
version = "0.3"

hedera-proto-wasm/src/fraction.rs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// SPDX-License-Identifier: Apache-2.0
2+
3+
use fraction::GenericFraction;
4+
5+
type Fraction = GenericFraction<u64>;
6+
7+
impl From<super::proto::proto::Fraction> for Fraction {
8+
fn from(pb: super::proto::proto::Fraction) -> Self {
9+
Fraction::new(pb.numerator as u64, pb.denominator as u64)
10+
}
11+
}
12+
13+
impl From<Fraction> for super::proto::proto::Fraction {
14+
fn from(frac: Fraction) -> Self {
15+
Self {
16+
numerator: frac.numer().copied().unwrap_or_default() as i64,
17+
denominator: frac.denom().copied().unwrap_or_default() as i64,
18+
}
19+
}
20+
}
21+

hedera-proto-wasm/src/lib.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,10 @@ pub mod proto {
2323
include!(concat!(env!("OUT_DIR"), "/hedera_protos.rs"));
2424
}
2525

26+
// Time conversions for WASM builds
27+
mod time_0_3;
28+
mod fraction;
29+
2630
// Re-export commonly used types for convenience
2731
// Re-export nested modules
2832
pub use proto::proto::transaction_body;

hedera-proto-wasm/src/time_0_3.rs

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
// SPDX-License-Identifier: Apache-2.0
2+
3+
use time::{
4+
Duration,
5+
OffsetDateTime,
6+
};
7+
8+
impl From<super::proto::proto::Duration> for Duration {
9+
fn from(pb: super::proto::proto::Duration) -> Self {
10+
Self::seconds(pb.seconds)
11+
}
12+
}
13+
14+
impl From<Duration> for super::proto::proto::Duration {
15+
fn from(duration: Duration) -> Self {
16+
Self { seconds: duration.whole_seconds() }
17+
}
18+
}
19+
20+
impl From<super::proto::proto::TimestampSeconds> for OffsetDateTime {
21+
fn from(pb: super::proto::proto::TimestampSeconds) -> Self {
22+
OffsetDateTime::from_unix_timestamp(pb.seconds).unwrap()
23+
}
24+
}
25+
26+
impl From<OffsetDateTime> for super::proto::proto::TimestampSeconds {
27+
fn from(dt: OffsetDateTime) -> Self {
28+
Self { seconds: dt.unix_timestamp() }
29+
}
30+
}
31+
32+
impl From<super::proto::proto::Timestamp> for OffsetDateTime {
33+
fn from(pb: super::proto::proto::Timestamp) -> Self {
34+
OffsetDateTime::from_unix_timestamp(pb.seconds).unwrap()
35+
+ Duration::nanoseconds(pb.nanos.into())
36+
}
37+
}
38+
39+
impl From<OffsetDateTime> for super::proto::proto::Timestamp {
40+
fn from(dt: OffsetDateTime) -> Self {
41+
Self { seconds: dt.unix_timestamp(), nanos: dt.nanosecond() as i32 }
42+
}
43+
}
44+

src/account/account_allowance_delete_transaction.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,6 @@ impl ToSchedulableTransactionDataProtobuf for AccountAllowanceDeleteTransactionD
124124
}
125125
}
126126

127-
#[cfg(not(target_arch = "wasm32"))]
128127
impl From<AccountAllowanceDeleteTransactionData> for AnyTransactionData {
129128
fn from(transaction: AccountAllowanceDeleteTransactionData) -> Self {
130129
Self::AccountAllowanceDelete(transaction)

src/account/account_id.rs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ use crate::entity_id::{
1717
};
1818
use crate::ledger_id::RefLedgerId;
1919
use crate::{
20-
Client,
2120
EntityId,
2221
Error,
2322
EvmAddress,
@@ -26,6 +25,9 @@ use crate::{
2625
ToProtobuf,
2726
};
2827

28+
#[cfg(not(target_arch = "wasm32"))]
29+
use crate::Client;
30+
2931
/// A unique identifier for a cryptocurrency account on Hiero.
3032
#[derive(Copy, Hash, PartialEq, Eq, Clone)]
3133
pub struct AccountId {
@@ -100,6 +102,7 @@ impl AccountId {
100102
///
101103
/// # Errors
102104
/// - [`Error::CannotCreateChecksum`] if self has an `alias` or `evm_address`.
105+
#[cfg(not(target_arch = "wasm32"))]
103106
pub fn to_string_with_checksum(&self, client: &Client) -> Result<String, Error> {
104107
if self.alias.is_some() || self.evm_address.is_some() {
105108
Err(Error::CannotCreateChecksum)
@@ -112,6 +115,7 @@ impl AccountId {
112115
///
113116
/// # Errors
114117
/// - [`Error::BadEntityId`] if there is a checksum, and the checksum is not valid for the client's `ledger_id`.
118+
#[cfg(not(target_arch = "wasm32"))]
115119
pub fn validate_checksum(&self, client: &Client) -> crate::Result<()> {
116120
if self.alias.is_some() || self.evm_address.is_some() {
117121
Ok(())
@@ -122,6 +126,7 @@ impl AccountId {
122126
}
123127

124128
impl ValidateChecksums for AccountId {
129+
#[cfg(not(target_arch = "wasm32"))]
125130
fn validate_checksums(&self, ledger_id: &RefLedgerId) -> Result<(), Error> {
126131
if self.alias.is_some() || self.evm_address.is_some() {
127132
Ok(())
@@ -135,6 +140,12 @@ impl ValidateChecksums for AccountId {
135140
)
136141
}
137142
}
143+
144+
#[cfg(target_arch = "wasm32")]
145+
fn validate_checksums(&self, _ledger_id: &RefLedgerId) -> Result<(), Error> {
146+
// Checksum validation requires networking context, not available in WASM
147+
Ok(())
148+
}
138149
}
139150

140151
impl Debug for AccountId {

src/account/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ mod account_id;
1111
mod account_info;
1212
// note(sr): there's absolutely no way I'm going to write an enum or struct for namespacing here.
1313
/// Flow for verifying signatures via account info.
14+
#[cfg(not(target_arch = "wasm32"))] // Account info flow requires client networking
1415
pub mod account_info_flow;
1516
#[cfg(not(target_arch = "wasm32"))] // Info query requires networking
1617
mod account_info_query;

src/contract/contract_id.rs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,15 @@ use crate::entity_id::{
1818
use crate::ethereum::SolidityAddress;
1919
use crate::ledger_id::RefLedgerId;
2020
use crate::{
21-
Client,
2221
EntityId,
2322
Error,
2423
FromProtobuf,
2524
ToProtobuf,
2625
};
2726

27+
#[cfg(not(target_arch = "wasm32"))]
28+
use crate::Client;
29+
2830
/// A unique identifier for a smart contract on Hiero.
2931
#[derive(Hash, PartialEq, Eq, Clone, Copy)]
3032
pub struct ContractId {
@@ -117,6 +119,7 @@ impl ContractId {
117119
///
118120
/// # Errors
119121
/// - [`Error::CannotCreateChecksum`] if self has an `evm_address`.
122+
#[cfg(not(target_arch = "wasm32"))]
120123
pub fn to_string_with_checksum(&self, client: &Client) -> Result<String, Error> {
121124
if self.evm_address.is_some() {
122125
Err(Error::CannotCreateChecksum)
@@ -129,6 +132,7 @@ impl ContractId {
129132
///
130133
/// # Errors
131134
/// - [`Error::BadEntityId`] if there is a checksum, and the checksum is not valid for the client's `ledger_id`.
135+
#[cfg(not(target_arch = "wasm32"))]
132136
pub fn validate_checksum(&self, client: &Client) -> Result<(), Error> {
133137
if self.evm_address.is_some() {
134138
Ok(())
@@ -139,6 +143,7 @@ impl ContractId {
139143
}
140144

141145
impl ValidateChecksums for ContractId {
146+
#[cfg(not(target_arch = "wasm32"))]
142147
fn validate_checksums(&self, ledger_id: &RefLedgerId) -> Result<(), Error> {
143148
if self.evm_address.is_some() {
144149
Ok(())
@@ -152,6 +157,12 @@ impl ValidateChecksums for ContractId {
152157
)
153158
}
154159
}
160+
161+
#[cfg(target_arch = "wasm32")]
162+
fn validate_checksums(&self, _ledger_id: &RefLedgerId) -> Result<(), Error> {
163+
// Checksum validation requires networking context, not available in WASM
164+
Ok(())
165+
}
155166
}
156167

157168
impl Debug for ContractId {

src/contract/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
mod contract_bytecode_query;
55
#[cfg(not(target_arch = "wasm32"))] // Call query requires networking
66
mod contract_call_query;
7+
#[cfg(not(target_arch = "wasm32"))] // Contract create flow requires client networking
78
mod contract_create_flow;
89
mod contract_create_transaction;
910
mod contract_delete_transaction;
@@ -28,6 +29,7 @@ pub(crate) use contract_bytecode_query::ContractBytecodeQueryData;
2829
pub use contract_call_query::ContractCallQuery;
2930
#[cfg(not(target_arch = "wasm32"))]
3031
pub(crate) use contract_call_query::ContractCallQueryData;
32+
#[cfg(not(target_arch = "wasm32"))]
3133
pub use contract_create_flow::ContractCreateFlow;
3234
pub use contract_create_transaction::ContractCreateTransaction;
3335
pub(crate) use contract_create_transaction::ContractCreateTransactionData;

0 commit comments

Comments
 (0)