Skip to content

Commit ea00c25

Browse files
committed
update
1 parent 6490c12 commit ea00c25

24 files changed

+267
-64
lines changed

Cargo.toml

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ license = "MIT"
1010
description = "Rust SDK for CKB"
1111
homepage = "https://github.com/nervosnetwork/ckb-sdk-rust"
1212
repository = "https://github.com/nervosnetwork/ckb-sdk-rust"
13-
13+
resolver = "2"
1414
[dependencies]
1515
serde = { version = "1.0", features = ["derive"] }
1616
serde_derive = "1.0"
@@ -50,10 +50,14 @@ rand = { version = "0.7.3", optional = true }
5050
ckb-mock-tx-types = { version = "0.200.0" }
5151
ckb-chain-spec = "0.200.0"
5252

53-
sparse-merkle-tree = { version = "0.6", optional = true}
53+
sparse-merkle-tree = { version = "0.6", optional = true }
54+
55+
[target.'cfg(target_arch = "wasm32")'.dependencies]
56+
getrandom = { version = "0.2.16", features = ["js"] }
5457

5558
[features]
56-
default = ["default-tls"]
59+
# default = ["default-tls"]
60+
default = []
5761
default-tls = ["reqwest/default-tls"]
5862
native-tls-vendored = ["reqwest/native-tls-vendored"]
5963
rustls-tls = ["reqwest/rustls-tls"]

examples/script_unlocker_example.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ use std::collections::HashMap;
1717
/// [CapacityDiff]: https://github.com/doitian/ckb-sdk-examples-capacity-diff
1818
struct CapacityDiffUnlocker {}
1919

20-
#[async_trait::async_trait]
20+
#[async_trait::async_trait(?Send)]
2121
impl ScriptUnlocker for CapacityDiffUnlocker {
2222
// This works for any args
2323
fn match_args(&self, _args: &[u8]) -> bool {

rust-toolchain

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
1.81.0
1+
1.85.0

src/lib.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,9 @@ pub mod test_util;
1616
#[cfg(test)]
1717
mod tests;
1818

19-
pub use rpc::{CkbRpcAsyncClient, CkbRpcClient, IndexerRpcAsyncClient, IndexerRpcClient, RpcError};
19+
#[cfg(not(target_arch = "wasm32"))]
20+
pub use rpc::{CkbRpcAsyncClient, IndexerRpcAsyncClient};
21+
pub use rpc::{CkbRpcAsyncClient, IndexerRpcAsyncClient, RpcError};
2022
pub use types::{
2123
Address, AddressPayload, AddressType, CodeHashIndex, HumanCapacity, NetworkInfo, NetworkType,
2224
OldAddress, OldAddressFormat, ScriptGroup, ScriptGroupType, ScriptId, Since, SinceType,

src/rpc/ckb.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ use ckb_types::{core::Cycle, H256};
1313
use super::{ckb_indexer::CellsCapacity, ResponseFormatGetter};
1414

1515
pub use super::ckb_indexer::{Cell, Order, Pagination, SearchKey, Tip, Tx};
16-
16+
#[cfg(not(target_arch = "wasm32"))]
1717
crate::jsonrpc!(pub struct CkbRpcClient {
1818
// Chain
1919
pub fn get_block(&self, hash: H256) -> Option<BlockView>;
@@ -211,7 +211,7 @@ fn transform_cycles(cycles: Option<Vec<ckb_jsonrpc_types::Cycle>>) -> Vec<Cycle>
211211
.map(|c| c.into_iter().map(Into::into).collect())
212212
.unwrap_or_default()
213213
}
214-
214+
#[cfg(not(target_arch = "wasm32"))]
215215
impl From<&CkbRpcClient> for CkbRpcAsyncClient {
216216
fn from(value: &CkbRpcClient) -> Self {
217217
Self {
@@ -220,7 +220,7 @@ impl From<&CkbRpcClient> for CkbRpcAsyncClient {
220220
}
221221
}
222222
}
223-
223+
#[cfg(not(target_arch = "wasm32"))]
224224
impl CkbRpcClient {
225225
pub fn get_packed_block(&self, hash: H256) -> Result<Option<JsonBytes>, crate::RpcError> {
226226
self.post("get_block", (hash, Some(Uint32::from(0u32))))

src/rpc/ckb_indexer.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,7 @@ pub struct Pagination<T> {
188188
pub objects: Vec<T>,
189189
pub last_cursor: JsonBytes,
190190
}
191-
191+
#[cfg(not(target_arch = "wasm32"))]
192192
crate::jsonrpc!(pub struct IndexerRpcClient {
193193
pub fn get_indexer_tip(&self) -> Option<Tip>;
194194
pub fn get_cells(&self, search_key: SearchKey, order: Order, limit: Uint32, after: Option<JsonBytes>) -> Pagination<Cell>;
@@ -202,7 +202,7 @@ crate::jsonrpc_async!(pub struct IndexerRpcAsyncClient {
202202
pub fn get_transactions(&self, search_key: SearchKey, order: Order, limit: Uint32, after: Option<JsonBytes>) -> Pagination<Tx>;
203203
pub fn get_cells_capacity(&self, search_key: SearchKey) -> Option<CellsCapacity>;
204204
});
205-
205+
#[cfg(not(target_arch = "wasm32"))]
206206
impl From<&IndexerRpcClient> for IndexerRpcAsyncClient {
207207
fn from(value: &IndexerRpcClient) -> Self {
208208
Self {

src/rpc/ckb_light_client.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ pub struct PeerSyncState {
135135
/// Proved best known header of remote peer.
136136
pub proved_best_known_header: Option<HeaderView>,
137137
}
138-
138+
#[cfg(not(target_arch = "wasm32"))]
139139
crate::jsonrpc!(pub struct LightClientRpcClient {
140140
// BlockFilter
141141
pub fn set_scripts(&self, scripts: Vec<ScriptStatus>, command: Option<SetScriptsCommand>) -> ();
@@ -199,7 +199,7 @@ crate::jsonrpc_async!(pub struct LightClientRpcAsyncClient {
199199
pub fn get_peers(&self) -> Vec<RemoteNode>;
200200
pub fn local_node_info(&self) -> LocalNode;
201201
});
202-
202+
#[cfg(not(target_arch = "wasm32"))]
203203
impl From<&LightClientRpcClient> for LightClientRpcAsyncClient {
204204
fn from(value: &LightClientRpcClient) -> Self {
205205
Self {

src/rpc/mod.rs

Lines changed: 35 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,23 @@ pub mod ckb_indexer;
33
pub mod ckb_light_client;
44

55
use anyhow::anyhow;
6-
pub use ckb::{CkbRpcAsyncClient, CkbRpcClient};
7-
pub use ckb_indexer::{IndexerRpcAsyncClient, IndexerRpcClient};
6+
7+
#[cfg(not(target_arch = "wasm32"))]
8+
pub use ckb::CkbRpcClient;
9+
#[cfg(not(target_arch = "wasm32"))]
10+
pub use ckb_indexer::IndexerRpcClient;
11+
#[cfg(not(target_arch = "wasm32"))]
12+
pub use ckb_light_client::LightClientRpcClient;
13+
14+
pub use ckb::CkbRpcAsyncClient;
15+
pub use ckb_indexer::IndexerRpcAsyncClient;
816
use ckb_jsonrpc_types::{JsonBytes, ResponseFormat};
9-
pub use ckb_light_client::{LightClientRpcAsyncClient, LightClientRpcClient};
17+
pub use ckb_light_client::LightClientRpcAsyncClient;
1018

1119
use std::future::Future;
1220
use thiserror::Error;
1321

22+
#[cfg(not(target_arch = "wasm32"))]
1423
pub(crate) fn block_on<F: Send>(future: impl Future<Output = F> + Send) -> F {
1524
match tokio::runtime::Handle::try_current() {
1625
Ok(h)
@@ -55,6 +64,7 @@ pub enum RpcError {
5564
Other(#[from] anyhow::Error),
5665
}
5766

67+
#[cfg(not(target_arch = "wasm32"))]
5868
#[macro_export]
5969
macro_rules! jsonrpc {
6070
(
@@ -159,7 +169,7 @@ macro_rules! jsonrpc_async {
159169
pub fn new(uri: &str) -> Self {
160170
$struct_name { id: 0.into(), client: $crate::rpc::RpcClient::new(uri), }
161171
}
162-
172+
#[cfg(not(target_arch="wasm32"))]
163173
pub fn post<PARAM, RET>(&self, method:&str, params: PARAM)->impl std::future::Future<Output =Result<RET, $crate::rpc::RpcError>> + Send + 'static
164174
where
165175
PARAM:serde::ser::Serialize + Send + 'static,
@@ -181,7 +191,28 @@ macro_rules! jsonrpc_async {
181191
self.client.post(params_fn)
182192

183193
}
194+
#[cfg(target_arch="wasm32")]
195+
pub fn post<PARAM, RET>(&self, method:&str, params: PARAM)->impl std::future::Future<Output =Result<RET, $crate::rpc::RpcError>> + 'static
196+
where
197+
PARAM:serde::ser::Serialize + Send + 'static,
198+
RET: serde::de::DeserializeOwned + Send + 'static,
199+
{
200+
let id = self.id.fetch_add(1, std::sync::atomic::Ordering::Relaxed);
201+
let method = serde_json::json!(method);
202+
203+
let params_fn = move || -> Result<_,_> {
204+
let params = serde_json::to_value(params)?;
205+
let mut req_json = serde_json::Map::new();
206+
req_json.insert("id".to_owned(), serde_json::json!(id));
207+
req_json.insert("jsonrpc".to_owned(), serde_json::json!("2.0"));
208+
req_json.insert("method".to_owned(), method);
209+
req_json.insert("params".to_owned(), params);
210+
Ok(req_json)
211+
};
184212

213+
self.client.post(params_fn)
214+
215+
}
185216
$(
186217
$(#[$attr])*
187218
pub fn $method(&$selff $(, $arg_name: $arg_ty)*) -> impl std::future::Future<Output =Result<$return_ty, $crate::rpc::RpcError>> {

src/test_util.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -391,7 +391,7 @@ impl Context {
391391
}
392392
}
393393

394-
#[async_trait::async_trait]
394+
#[async_trait::async_trait(?Send)]
395395
impl TransactionDependencyProvider for Context {
396396
// For verify certain cell belong to certain transaction
397397
async fn get_transaction_async(
@@ -451,7 +451,7 @@ impl TransactionDependencyProvider for Context {
451451
}
452452
}
453453

454-
#[async_trait::async_trait]
454+
#[async_trait::async_trait(?Send)]
455455
impl HeaderDepResolver for Context {
456456
async fn resolve_by_tx_async(
457457
&self,
@@ -520,7 +520,7 @@ impl CellDepResolver for Context {
520520
}
521521
}
522522

523-
#[async_trait::async_trait]
523+
#[async_trait::async_trait(?Send)]
524524
impl CellCollector for LiveCellsContext {
525525
async fn collect_live_cells_async(
526526
&mut self,

src/tests/cycle.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ const CYCLE_BIN: &[u8] = include_bytes!("../test-data/cycle");
2727
pub struct CycleUnlocker {
2828
loops: u64,
2929
}
30-
#[async_trait::async_trait]
30+
#[async_trait::async_trait(?Send)]
3131
impl ScriptUnlocker for CycleUnlocker {
3232
fn match_args(&self, _args: &[u8]) -> bool {
3333
true

src/traits/default_impls.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,7 @@ impl DefaultHeaderDepResolver {
212212
}
213213
}
214214

215-
#[async_trait::async_trait]
215+
#[async_trait::async_trait(?Send)]
216216
impl HeaderDepResolver for DefaultHeaderDepResolver {
217217
async fn resolve_by_tx_async(
218218
&self,
@@ -277,7 +277,7 @@ impl DefaultCellCollector {
277277
pub fn set_acceptable_indexer_leftbehind(&mut self, value: u64) {
278278
self.acceptable_indexer_leftbehind = value;
279279
}
280-
280+
#[cfg(not(target_arch = "wasm32"))]
281281
/// wrapper check_ckb_chain_async future
282282
pub fn check_ckb_chain(&mut self) -> Result<(), CellCollectorError> {
283283
crate::rpc::block_on(self.check_ckb_chain_async())
@@ -319,7 +319,7 @@ impl DefaultCellCollector {
319319
}
320320
}
321321

322-
#[async_trait::async_trait]
322+
#[async_trait::async_trait(?Send)]
323323
impl CellCollector for DefaultCellCollector {
324324
async fn collect_live_cells_async(
325325
&mut self,
@@ -461,7 +461,7 @@ impl DefaultTransactionDependencyProvider {
461461
inner: Arc::new(Mutex::new(inner)),
462462
}
463463
}
464-
464+
#[cfg(not(target_arch = "wasm32"))]
465465
pub fn apply_tx(
466466
&mut self,
467467
tx: Transaction,
@@ -479,7 +479,7 @@ impl DefaultTransactionDependencyProvider {
479479
inner.offchain_cache.apply_tx(tx, tip_block_number)?;
480480
Ok(())
481481
}
482-
482+
#[cfg(not(target_arch = "wasm32"))]
483483
pub fn get_cell_with_data(
484484
&self,
485485
out_point: &OutPoint,
@@ -517,7 +517,7 @@ impl DefaultTransactionDependencyProvider {
517517
}
518518
}
519519

520-
#[async_trait::async_trait]
520+
#[async_trait::async_trait(?Send)]
521521
impl TransactionDependencyProvider for DefaultTransactionDependencyProvider {
522522
async fn get_transaction_async(
523523
&self,

src/traits/dummy_impls.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ use anyhow::anyhow;
1414
#[derive(Clone, Default)]
1515
pub struct DummyCellCollector;
1616

17-
#[async_trait::async_trait]
17+
#[async_trait::async_trait(?Send)]
1818
impl CellCollector for DummyCellCollector {
1919
async fn collect_live_cells_async(
2020
&mut self,
@@ -48,7 +48,7 @@ impl CellCollector for DummyCellCollector {
4848
#[derive(Default)]
4949
pub struct DummyHeaderDepResolver;
5050

51-
#[async_trait::async_trait]
51+
#[async_trait::async_trait(?Send)]
5252
impl HeaderDepResolver for DummyHeaderDepResolver {
5353
async fn resolve_by_tx_async(
5454
&self,
@@ -68,7 +68,7 @@ impl HeaderDepResolver for DummyHeaderDepResolver {
6868
#[derive(Default)]
6969
pub struct DummyTransactionDependencyProvider;
7070

71-
#[async_trait::async_trait]
71+
#[async_trait::async_trait(?Send)]
7272
impl TransactionDependencyProvider for DummyTransactionDependencyProvider {
7373
// For verify certain cell belong to certain transaction
7474
async fn get_transaction_async(

src/traits/light_client_impls.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ impl LightClientHeaderDepResolver {
4242
}
4343
}
4444

45-
#[async_trait::async_trait]
45+
#[async_trait::async_trait(?Send)]
4646
impl HeaderDepResolver for LightClientHeaderDepResolver {
4747
async fn resolve_by_tx_async(
4848
&self,
@@ -119,7 +119,7 @@ impl LightClientTransactionDependencyProvider {
119119
}
120120
}
121121

122-
#[async_trait::async_trait]
122+
#[async_trait::async_trait(?Send)]
123123
impl TransactionDependencyProvider for LightClientTransactionDependencyProvider {
124124
async fn get_transaction_async(
125125
&self,
@@ -270,7 +270,7 @@ impl LightClientCellCollector {
270270
}
271271
}
272272

273-
#[async_trait::async_trait]
273+
#[async_trait::async_trait(?Send)]
274274
impl CellCollector for LightClientCellCollector {
275275
async fn collect_live_cells_async(
276276
&mut self,

src/traits/mod.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ pub enum TransactionDependencyError {
9090
/// * inputs
9191
/// * cell_deps
9292
/// * header_deps
93-
#[async_trait::async_trait]
93+
#[async_trait::async_trait(?Send)]
9494
pub trait TransactionDependencyProvider: Sync + Send {
9595
async fn get_transaction_async(
9696
&self,
@@ -421,7 +421,7 @@ impl CellQueryOptions {
421421
}
422422
}
423423

424-
#[async_trait::async_trait]
424+
#[async_trait::async_trait(?Send)]
425425
pub trait CellCollector: DynClone + Send + Sync {
426426
/// Collect live cells by query options, if `apply_changes` is true will
427427
/// mark all collected cells as dead cells.
@@ -464,7 +464,7 @@ pub trait CellDepResolver: Send + Sync {
464464
fn resolve(&self, script: &Script) -> Option<CellDep>;
465465
}
466466

467-
#[async_trait::async_trait]
467+
#[async_trait::async_trait(?Send)]
468468
pub trait HeaderDepResolver: Send + Sync {
469469
/// Resolve header dep by trancation hash
470470
async fn resolve_by_tx_async(

src/traits/offchain_impls.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ pub struct OffchainHeaderDepResolver {
3737
pub by_number: HashMap<u64, HeaderView>,
3838
}
3939

40-
#[async_trait::async_trait]
40+
#[async_trait::async_trait(?Send)]
4141
impl HeaderDepResolver for OffchainHeaderDepResolver {
4242
async fn resolve_by_tx_async(
4343
&self,
@@ -228,7 +228,7 @@ impl OffchainTransactionDependencyProvider {
228228
}
229229
}
230230

231-
#[async_trait::async_trait]
231+
#[async_trait::async_trait(?Send)]
232232
impl TransactionDependencyProvider for OffchainTransactionDependencyProvider {
233233
// For verify certain cell belong to certain transaction
234234
async fn get_transaction_async(

src/tx_builder/acp.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ impl AcpTransferBuilder {
3737
}
3838
}
3939

40-
#[async_trait::async_trait]
40+
#[async_trait::async_trait(?Send)]
4141
impl TxBuilder for AcpTransferBuilder {
4242
async fn build_base_async(
4343
&self,

0 commit comments

Comments
 (0)