Skip to content

Commit f827659

Browse files
authored
Merge pull request #144 from Officeyutong/wasm
fix build issue on wasm32 introduced by #140
2 parents 50e3e22 + 94c810a commit f827659

File tree

8 files changed

+175
-14
lines changed

8 files changed

+175
-14
lines changed

src/constants.rs

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use std::convert::TryFrom;
22

3-
use crate::{CkbRpcClient, NetworkInfo, NetworkType, ScriptId};
3+
use crate::{CkbRpcAsyncClient, NetworkInfo, NetworkType, ScriptId};
44
use ckb_system_scripts_v0_5_4::{
55
CODE_HASH_SECP256K1_BLAKE160_MULTISIG_ALL as CODE_HASH_SECP256K1_BLAKE160_MULTISIG_ALL_LEGACY,
66
CODE_HASH_SECP256K1_DATA,
@@ -122,12 +122,23 @@ impl MultisigScript {
122122
/// 2. MULTISIG_V2_DEP_GROUP=0x6888aa39ab30c570c2c30d9d5684d3769bf77265a7973211a3c087fe8efbf738,2
123123
///
124124
/// If env not set, then get it from dep_group_inner
125+
#[cfg(not(target_arch = "wasm32"))]
125126
pub fn dep_group(&self, network: NetworkInfo) -> Option<(H256, u32)> {
126127
self.dep_group_from_env(network.clone())
127-
.or(self.dep_group_inner(network))
128+
.or(crate::rpc::block_on(self.dep_group_inner(network)))
128129
}
129130

130-
pub fn dep_group_inner(&self, network: NetworkInfo) -> Option<(H256, u32)> {
131+
/// Get dep group from env first:
132+
/// 1. MULTISIG_LEGACY_DEP_GROUP=0x71a7ba8fc96349fea0ed3a5c47992e3b4084b031a42264a018e0072e8172e46c,1
133+
/// 2. MULTISIG_V2_DEP_GROUP=0x6888aa39ab30c570c2c30d9d5684d3769bf77265a7973211a3c087fe8efbf738,2
134+
///
135+
/// If env not set, then get it from dep_group_inner
136+
pub async fn dep_group_async(&self, network: NetworkInfo) -> Option<(H256, u32)> {
137+
self.dep_group_from_env(network.clone())
138+
.or(self.dep_group_inner(network).await)
139+
}
140+
141+
async fn dep_group_inner(&self, network: NetworkInfo) -> Option<(H256, u32)> {
131142
match network.network_type {
132143
NetworkType::Mainnet => Some(match self {
133144
MultisigScript::Legacy => (
@@ -150,8 +161,8 @@ impl MultisigScript {
150161
),
151162
}),
152163
NetworkType::Staging | NetworkType::Preview | NetworkType::Dev => {
153-
let client = CkbRpcClient::new(network.url.as_str());
154-
let json_genesis_block = client.get_block_by_number(0_u64.into()).ok()??;
164+
let client = CkbRpcAsyncClient::new(network.url.as_str());
165+
let json_genesis_block = client.get_block_by_number(0_u64.into()).await.ok()??;
155166
let genesis_block: ckb_types::core::BlockView = json_genesis_block.into();
156167

157168
let secp256k1_data_outpoint =

src/traits/default_impls.rs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,8 +72,17 @@ impl DefaultCellDepResolver {
7272
/// You can customize the multisig script's depgroup by these two env variables, for example:
7373
/// 1. MULTISIG_LEGACY_DEP_GROUP=0x71a7ba8fc96349fea0ed3a5c47992e3b4084b031a42264a018e0072e8172e46c,1
7474
/// 2. MULTISIG_V2_DEP_GROUP=0x6888aa39ab30c570c2c30d9d5684d3769bf77265a7973211a3c087fe8efbf738,2
75+
#[cfg(not(target_arch = "wasm32"))]
7576
pub fn from_genesis(
7677
genesis_block: &BlockView,
78+
) -> Result<DefaultCellDepResolver, ParseGenesisInfoError> {
79+
crate::rpc::block_on(Self::from_genesis_async(genesis_block))
80+
}
81+
/// You can customize the multisig script's depgroup by these two env variables, for example:
82+
/// 1. MULTISIG_LEGACY_DEP_GROUP=0x71a7ba8fc96349fea0ed3a5c47992e3b4084b031a42264a018e0072e8172e46c,1
83+
/// 2. MULTISIG_V2_DEP_GROUP=0x6888aa39ab30c570c2c30d9d5684d3769bf77265a7973211a3c087fe8efbf738,2
84+
pub async fn from_genesis_async(
85+
genesis_block: &BlockView,
7786
) -> Result<DefaultCellDepResolver, ParseGenesisInfoError> {
7887
let header = genesis_block.header();
7988
if header.number() != 0 {
@@ -180,7 +189,9 @@ impl DefaultCellDepResolver {
180189
NetworkInfo::devnet()
181190
};
182191

183-
if let Some((v2_dep_hash, v2_dep_index)) = MultisigScript::V2.dep_group(network_info) {
192+
if let Some((v2_dep_hash, v2_dep_index)) =
193+
MultisigScript::V2.dep_group_async(network_info).await
194+
{
184195
let multisig_v2_dep = CellDep::new_builder()
185196
.out_point(OutPoint::new(v2_dep_hash.pack(), v2_dep_index))
186197
.dep_type(DepType::DepGroup.into())

src/transaction/handler/mod.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ pub mod sighash;
1414
pub mod sudt;
1515
pub mod typeid;
1616

17+
#[cfg_attr(target_arch="wasm32", async_trait::async_trait(?Send))]
18+
#[cfg_attr(not(target_arch = "wasm32"), async_trait::async_trait)]
1719
pub trait ScriptHandler: Send + Sync {
1820
/// Try to build transaction with the given script_group and context.
1921
///
@@ -24,8 +26,9 @@ pub trait ScriptHandler: Send + Sync {
2426
script_group: &mut ScriptGroup,
2527
context: &dyn HandlerContext,
2628
) -> Result<bool, TxBuilderError>;
27-
29+
#[cfg(not(target_arch = "wasm32"))]
2830
fn init(&mut self, network: &NetworkInfo) -> Result<(), TxBuilderError>;
31+
async fn init_async(&mut self, network: &NetworkInfo) -> Result<(), TxBuilderError>;
2932
}
3033

3134
pub trait Type2Any: 'static {

src/transaction/handler/multisig.rs

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ impl Secp256k1Blake160MultisigAllScriptHandler {
3636
&& script.hash_type() == multisig_script_id.hash_type.into()
3737
}
3838

39+
#[cfg(not(target_arch = "wasm32"))]
3940
pub fn new(
4041
network: &NetworkInfo,
4142
multisig_script: MultisigScript,
@@ -48,14 +49,27 @@ impl Secp256k1Blake160MultisigAllScriptHandler {
4849
Ok(ret)
4950
}
5051

52+
pub async fn new_async(
53+
network: &NetworkInfo,
54+
multisig_script: MultisigScript,
55+
) -> Result<Self, TxBuilderError> {
56+
let mut ret = Self {
57+
multisig_script,
58+
cell_deps: vec![],
59+
};
60+
ret.init_async(network).await?;
61+
Ok(ret)
62+
}
63+
5164
pub fn new_with_customize(multisig_script: MultisigScript, cell_deps: Vec<CellDep>) -> Self {
5265
Self {
5366
multisig_script,
5467
cell_deps,
5568
}
5669
}
5770
}
58-
71+
#[cfg_attr(target_arch="wasm32", async_trait::async_trait(?Send))]
72+
#[cfg_attr(not(target_arch = "wasm32"), async_trait::async_trait)]
5973
impl ScriptHandler for Secp256k1Blake160MultisigAllScriptHandler {
6074
fn build_transaction(
6175
&self,
@@ -80,6 +94,7 @@ impl ScriptHandler for Secp256k1Blake160MultisigAllScriptHandler {
8094
}
8195
}
8296

97+
#[cfg(not(target_arch = "wasm32"))]
8398
#[allow(clippy::if_same_then_else)]
8499
fn init(&mut self, network: &NetworkInfo) -> Result<(), TxBuilderError> {
85100
let dep_group =
@@ -101,4 +116,25 @@ impl ScriptHandler for Secp256k1Blake160MultisigAllScriptHandler {
101116
self.cell_deps.push(cell_dep);
102117
Ok(())
103118
}
119+
async fn init_async(&mut self, network: &NetworkInfo) -> Result<(), TxBuilderError> {
120+
let dep_group = self
121+
.multisig_script
122+
.dep_group_async(network.to_owned())
123+
.await
124+
.ok_or(TxBuilderError::Other(anyhow!(
125+
"not found multisig dep on network: {:?}",
126+
network
127+
)))?;
128+
let out_point = OutPoint::new_builder()
129+
.tx_hash(dep_group.0.pack())
130+
.index(dep_group.1.pack())
131+
.build();
132+
133+
let cell_dep = CellDep::new_builder()
134+
.out_point(out_point)
135+
.dep_type(DepType::DepGroup.into())
136+
.build();
137+
self.cell_deps.push(cell_dep);
138+
Ok(())
139+
}
104140
}

src/transaction/handler/sighash.rs

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,16 +24,24 @@ impl Secp256k1Blake160SighashAllScriptHandler {
2424
pub fn is_match(&self, script: &Script) -> bool {
2525
script.code_hash() == constants::SIGHASH_TYPE_HASH.pack()
2626
}
27+
#[cfg(not(target_arch = "wasm32"))]
2728
pub fn new_with_network(network: &NetworkInfo) -> Result<Self, TxBuilderError> {
2829
let mut ret = Self { cell_deps: vec![] };
2930
ret.init(network)?;
3031
Ok(ret)
3132
}
33+
pub async fn new_with_network_async(network: &NetworkInfo) -> Result<Self, TxBuilderError> {
34+
let mut ret = Self { cell_deps: vec![] };
35+
ret.init_async(network).await?;
36+
Ok(ret)
37+
}
38+
3239
pub fn new_with_customize(cell_deps: Vec<CellDep>) -> Self {
3340
Self { cell_deps }
3441
}
3542
}
36-
43+
#[cfg_attr(target_arch="wasm32", async_trait::async_trait(?Send))]
44+
#[cfg_attr(not(target_arch = "wasm32"), async_trait::async_trait)]
3745
impl ScriptHandler for Secp256k1Blake160SighashAllScriptHandler {
3846
fn build_transaction(
3947
&self,
@@ -71,6 +79,7 @@ impl ScriptHandler for Secp256k1Blake160SighashAllScriptHandler {
7179
}
7280
}
7381

82+
#[cfg(not(target_arch = "wasm32"))]
7483
fn init(&mut self, network: &NetworkInfo) -> Result<(), TxBuilderError> {
7584
let out_point = if network.network_type == NetworkType::Mainnet {
7685
OutPoint::new_builder()
@@ -100,6 +109,42 @@ impl ScriptHandler for Secp256k1Blake160SighashAllScriptHandler {
100109
return Err(TxBuilderError::UnsupportedNetworkType(network.network_type));
101110
};
102111

112+
let cell_dep = CellDep::new_builder()
113+
.out_point(out_point)
114+
.dep_type(DepType::DepGroup.into())
115+
.build();
116+
self.cell_deps.push(cell_dep);
117+
Ok(())
118+
}
119+
async fn init_async(&mut self, network: &NetworkInfo) -> Result<(), TxBuilderError> {
120+
let out_point = if network.network_type == NetworkType::Mainnet {
121+
OutPoint::new_builder()
122+
.tx_hash(
123+
h256!("0x71a7ba8fc96349fea0ed3a5c47992e3b4084b031a42264a018e0072e8172e46c")
124+
.pack(),
125+
)
126+
.index(0u32.pack())
127+
.build()
128+
} else if network.network_type == NetworkType::Testnet {
129+
OutPoint::new_builder()
130+
.tx_hash(
131+
h256!("0xf8de3bb47d055cdf460d93a2a6e1b05f7432f9777c8c474abf4eec1d4aee5d37")
132+
.pack(),
133+
)
134+
.index(0u32.pack())
135+
.build()
136+
} else if network.network_type == NetworkType::Preview {
137+
OutPoint::new_builder()
138+
.tx_hash(
139+
h256!("0x0fab65924f2784f17ad7f86d6aef4b04ca1ca237102a68961594acebc5c77816")
140+
.pack(),
141+
)
142+
.index(0u32.pack())
143+
.build()
144+
} else {
145+
return Err(TxBuilderError::UnsupportedNetworkType(network.network_type));
146+
};
147+
103148
let cell_dep = CellDep::new_builder()
104149
.out_point(out_point)
105150
.dep_type(DepType::DepGroup.into())

src/transaction/handler/sudt.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,8 @@ impl SudtHandler {
7272
}
7373
}
7474
}
75-
75+
#[cfg_attr(target_arch="wasm32", async_trait::async_trait(?Send))]
76+
#[cfg_attr(not(target_arch = "wasm32"), async_trait::async_trait)]
7677
impl ScriptHandler for SudtHandler {
7778
fn build_transaction(
7879
&self,
@@ -91,8 +92,11 @@ impl ScriptHandler for SudtHandler {
9192
}
9293
Ok(false)
9394
}
94-
95+
#[cfg(not(target_arch = "wasm32"))]
9596
fn init(&mut self, _network: &NetworkInfo) -> Result<(), TxBuilderError> {
9697
Ok(())
9798
}
99+
async fn init_async(&mut self, _network: &NetworkInfo) -> Result<(), TxBuilderError> {
100+
Ok(())
101+
}
98102
}

src/transaction/handler/typeid.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@ pub struct TypeIdHandler;
1616
pub struct TypeIdContext;
1717

1818
impl HandlerContext for TypeIdContext {}
19-
19+
#[cfg_attr(target_arch="wasm32", async_trait::async_trait(?Send))]
20+
#[cfg_attr(not(target_arch = "wasm32"), async_trait::async_trait)]
2021
impl ScriptHandler for TypeIdHandler {
2122
fn build_transaction(
2223
&self,
@@ -50,10 +51,13 @@ impl ScriptHandler for TypeIdHandler {
5051
}
5152
Ok(false)
5253
}
53-
54+
#[cfg(not(target_arch = "wasm32"))]
5455
fn init(&mut self, _network: &NetworkInfo) -> Result<(), TxBuilderError> {
5556
Ok(())
5657
}
58+
async fn init_async(&mut self, _network: &NetworkInfo) -> Result<(), TxBuilderError> {
59+
Ok(())
60+
}
5761
}
5862

5963
fn calculate_type_id(first_cell_input: &CellInput, output_index: u64) -> [u8; 32] {

src/transaction/mod.rs

Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,23 @@ pub struct TransactionBuilderConfiguration {
2121
}
2222

2323
impl TransactionBuilderConfiguration {
24+
#[cfg(not(target_arch = "wasm32"))]
2425
pub fn new() -> Result<Self, TxBuilderError> {
2526
Self::new_with_network(NetworkInfo::mainnet())
2627
}
27-
28+
#[cfg(not(target_arch = "wasm32"))]
2829
pub fn new_testnet() -> Result<Self, TxBuilderError> {
2930
Self::new_with_network(NetworkInfo::testnet())
3031
}
3132

33+
pub async fn new_async() -> Result<Self, TxBuilderError> {
34+
Self::new_with_network_async(NetworkInfo::mainnet()).await
35+
}
36+
37+
pub async fn new_testnet_async() -> Result<Self, TxBuilderError> {
38+
Self::new_with_network_async(NetworkInfo::testnet()).await
39+
}
40+
3241
pub fn new_devnet() -> Result<Self, TxBuilderError> {
3342
Ok(Self {
3443
network: NetworkInfo::devnet(),
@@ -38,6 +47,7 @@ impl TransactionBuilderConfiguration {
3847
})
3948
}
4049

50+
#[cfg(not(target_arch = "wasm32"))]
4151
pub fn new_with_network(network: NetworkInfo) -> Result<Self, TxBuilderError> {
4252
let script_handlers = Self::generate_system_handlers(&network)?;
4353
Ok(Self {
@@ -47,7 +57,17 @@ impl TransactionBuilderConfiguration {
4757
estimate_tx_size: 128000,
4858
})
4959
}
60+
pub async fn new_with_network_async(network: NetworkInfo) -> Result<Self, TxBuilderError> {
61+
let script_handlers = Self::generate_system_handlers_async(&network).await?;
62+
Ok(Self {
63+
network,
64+
script_handlers,
65+
fee_rate: 1000,
66+
estimate_tx_size: 128000,
67+
})
68+
}
5069

70+
#[cfg(not(target_arch = "wasm32"))]
5171
fn generate_system_handlers(
5272
network: &NetworkInfo,
5373
) -> Result<Vec<Box<dyn ScriptHandler>>, TxBuilderError> {
@@ -74,6 +94,33 @@ impl TransactionBuilderConfiguration {
7494
];
7595
Ok(ret)
7696
}
97+
async fn generate_system_handlers_async(
98+
network: &NetworkInfo,
99+
) -> Result<Vec<Box<dyn ScriptHandler>>, TxBuilderError> {
100+
let ret =
101+
vec![
102+
Box::new(
103+
handler::sighash::Secp256k1Blake160SighashAllScriptHandler::new_with_network_async(
104+
network,
105+
).await?,
106+
) as Box<_>,
107+
Box::new(
108+
handler::multisig::Secp256k1Blake160MultisigAllScriptHandler::new_async(
109+
network,
110+
MultisigScript::Legacy,
111+
).await?,
112+
) as Box<_>,
113+
Box::new(
114+
handler::multisig::Secp256k1Blake160MultisigAllScriptHandler::new_async(
115+
network,
116+
MultisigScript::V2,
117+
).await?,
118+
) as Box<_>,
119+
Box::new(handler::sudt::SudtHandler::new_with_network(network)?) as Box<_>,
120+
Box::new(handler::typeid::TypeIdHandler) as Box<_>,
121+
];
122+
Ok(ret)
123+
}
77124

78125
pub fn network_info(&self) -> &NetworkInfo {
79126
&self.network

0 commit comments

Comments
 (0)