Skip to content

Commit

Permalink
Update features to build with wasm
Browse files Browse the repository at this point in the history
  • Loading branch information
crodas committed Nov 27, 2024
1 parent dc515a7 commit 42e2958
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 8 deletions.
7 changes: 5 additions & 2 deletions crates/cdk/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,10 @@ uuid = { version = "1", features = ["v4"] }
# -Z minimal-versions
sync_wrapper = "0.1.2"
bech32 = "0.9.1"
tokio-tungstenite = { version = "0.24.0", features = ["tokio-native-tls"] }
rand = "0.8.5"
tokio-tungstenite = { version = "0.23.0", features = [
"rustls",
"rustls-tls-native-roots",
] }

[target.'cfg(not(target_arch = "wasm32"))'.dependencies]
tokio = { version = "1.21", features = [
Expand All @@ -68,6 +70,7 @@ tokio = { version = "1.21", features = [
"macros",
"sync",
] }
getrandom = { version = "0.2" }

[target.'cfg(target_arch = "wasm32")'.dependencies]
tokio = { version = "1.21", features = ["rt", "macros", "sync", "time"] }
Expand Down
20 changes: 14 additions & 6 deletions crates/cdk/src/wallet/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ use crate::{
};
use bitcoin::{bip32::Xpriv, Network};
use client::HttpClientMethods;
use rand::{distributions::Alphanumeric, thread_rng, Rng};
use getrandom::getrandom;
use std::{collections::HashMap, str::FromStr, sync::Arc};
use subscription::{ActiveSubscription, SubscriptionManager};
use tracing::instrument;
Expand Down Expand Up @@ -63,6 +63,8 @@ pub struct Wallet {
subscription: SubscriptionManager,
}

const ALPHANUMERIC: &[u8] = b"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";

/// Wallet Subscription filter
#[derive(Debug, Clone)]
pub enum WalletSubscription {
Expand All @@ -76,11 +78,17 @@ pub enum WalletSubscription {

impl From<WalletSubscription> for Params {
fn from(val: WalletSubscription) -> Self {
let id: String = thread_rng()
.sample_iter(&Alphanumeric)
.take(10)
.map(char::from)
.collect();
let mut buffer = vec![0u8; 10];

getrandom(&mut buffer).expect("Failed to generate random bytes");

let id = buffer
.iter()
.map(|&byte| {
let index = byte as usize % ALPHANUMERIC.len(); // 62 alphanumeric characters (A-Z, a-z, 0-9)
ALPHANUMERIC[index] as char
})
.collect::<String>();

match val {
WalletSubscription::ProofState(filters) => Params {
Expand Down

0 comments on commit 42e2958

Please sign in to comment.