Skip to content
This repository was archived by the owner on Apr 9, 2024. It is now read-only.

Commit cf803e4

Browse files
author
Eason Gao
authored
refactor(common): use full address global (#325)
* refactor: use full address global * cargo fmt * fix test
1 parent c489bbd commit cf803e4

File tree

5 files changed

+56
-172
lines changed

5 files changed

+56
-172
lines changed

Cargo.lock

+12-39
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

common/src/address.rs

+40-113
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use crate::{
55

66
use bech32::{convert_bits, ToBase32, Variant};
77
use ckb_hash::blake2b_256;
8-
use ckb_types::{bytes::Bytes, core::ScriptHashType, packed, prelude::*, H160, H256};
8+
use ckb_types::{bytes::Bytes, core::ScriptHashType, packed, prelude::*, H160};
99
use serde::{Deserialize, Serialize};
1010

1111
use std::convert::TryInto;
@@ -74,6 +74,7 @@ pub enum AddressPayload {
7474
}
7575

7676
impl AddressPayload {
77+
#[deprecated]
7778
pub fn new_short(net_ty: NetworkType, index: CodeHashIndex, hash: H160) -> AddressPayload {
7879
AddressPayload::Short {
7980
net_ty,
@@ -93,11 +94,13 @@ impl AddressPayload {
9394
args,
9495
}
9596
}
97+
9698
pub fn new_full_data(code_hash: packed::Byte32, args: Bytes) -> AddressPayload {
97-
Self::new_full(ScriptHashType::Data, code_hash, args)
99+
AddressPayload::new_full(ScriptHashType::Data, code_hash, args)
98100
}
101+
99102
pub fn new_full_type(code_hash: packed::Byte32, args: Bytes) -> AddressPayload {
100-
Self::new_full(ScriptHashType::Type, code_hash, args)
103+
AddressPayload::new_full(ScriptHashType::Type, code_hash, args)
101104
}
102105

103106
pub fn ty(&self, is_new: bool) -> AddressType {
@@ -185,19 +188,18 @@ impl AddressPayload {
185188
.unwrap_or_else(|_| panic!("Encode address failed: payload={:?}", self))
186189
}
187190

188-
pub fn from_pubkey(net_ty: NetworkType, pubkey: &secp256k1::PublicKey) -> AddressPayload {
191+
pub fn from_pubkey(pubkey: &secp256k1::PublicKey) -> AddressPayload {
189192
// Serialize pubkey as compressed format
190193
let hash = H160::from_slice(&blake2b_256(&pubkey.serialize()[..])[0..20])
191194
.expect("Generate hash(H160) from pubkey failed");
192-
AddressPayload::from_pubkey_hash(net_ty, hash)
195+
AddressPayload::from_pubkey_hash(hash)
193196
}
194197

195-
pub fn from_pubkey_hash(net_ty: NetworkType, hash: H160) -> AddressPayload {
196-
let index = CodeHashIndex::Sighash;
197-
AddressPayload::Short {
198-
net_ty,
199-
index,
200-
hash,
198+
pub fn from_pubkey_hash(hash: H160) -> AddressPayload {
199+
AddressPayload::Full {
200+
hash_type: ScriptHashType::Type,
201+
code_hash: SIGHASH_TYPE_HASH.pack(),
202+
args: Bytes::from(hash.as_bytes().to_vec()),
201203
}
202204
}
203205

@@ -220,74 +222,23 @@ impl AddressPayload {
220222
}
221223
}
222224

223-
#[allow(clippy::if_same_then_else)]
224-
pub fn from_script(lock: &packed::Script, net_ty: NetworkType) -> Self {
225-
let hash_type: ScriptHashType = lock.hash_type().try_into().expect("Invalid hash_type");
226-
let code_hash = lock.code_hash();
227-
let code_hash_h256: H256 = code_hash.unpack();
228-
let args = lock.args().raw_data();
229-
230-
if hash_type == ScriptHashType::Type
231-
&& code_hash_h256 == SIGHASH_TYPE_HASH
232-
&& args.len() == 20
233-
{
234-
let index = CodeHashIndex::Sighash;
235-
let hash = H160::from_slice(args.as_ref()).unwrap();
236-
AddressPayload::Short {
237-
net_ty,
238-
index,
239-
hash,
240-
}
241-
} else if hash_type == ScriptHashType::Type
242-
&& code_hash_h256 == MULTISIG_TYPE_HASH
243-
&& args.len() == 20
244-
{
245-
let index = CodeHashIndex::Multisig;
246-
let hash = H160::from_slice(args.as_ref()).unwrap();
247-
AddressPayload::Short {
248-
net_ty,
249-
index,
250-
hash,
251-
}
252-
} else if hash_type == ScriptHashType::Type
253-
&& net_ty == NetworkType::Mainnet
254-
&& code_hash_h256 == ACP_MAINNET_TYPE_HASH
255-
{
256-
let index = CodeHashIndex::AnyoneCanPay;
257-
let hash = H160::from_slice(&args.as_ref()[0..20]).unwrap();
258-
AddressPayload::Short {
259-
net_ty,
260-
index,
261-
hash,
262-
}
263-
} else if hash_type == ScriptHashType::Type
264-
&& net_ty == NetworkType::Testnet
265-
&& code_hash_h256 == ACP_TESTNET_TYPE_HASH
266-
{
267-
let index = CodeHashIndex::AnyoneCanPay;
268-
let hash = H160::from_slice(&args.as_ref()[0..20]).unwrap();
269-
AddressPayload::Short {
270-
net_ty,
271-
index,
272-
hash,
273-
}
274-
} else {
275-
AddressPayload::Full {
276-
hash_type,
277-
code_hash,
278-
args,
279-
}
225+
pub fn from_script(lock: &packed::Script) -> Self {
226+
AddressPayload::Full {
227+
hash_type: lock.hash_type().try_into().expect("Invalid hash_type"),
228+
code_hash: lock.code_hash(),
229+
args: lock.args().raw_data(),
280230
}
281231
}
282232
}
283233

284234
impl fmt::Debug for AddressPayload {
285235
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
286-
let hash_type = if self.hash_type() == ScriptHashType::Type {
287-
"type"
288-
} else {
289-
"data"
236+
let hash_type = match self.hash_type() {
237+
ScriptHashType::Type => "type",
238+
ScriptHashType::Data => "data",
239+
ScriptHashType::Data1 => "data1",
290240
};
241+
291242
f.debug_struct("AddressPayload")
292243
.field("hash_type", &hash_type)
293244
.field("code_hash", &self.code_hash())
@@ -307,47 +258,20 @@ impl From<&AddressPayload> for packed::Script {
307258
}
308259

309260
impl From<packed::Script> for AddressPayload {
310-
#[allow(clippy::fallible_impl_from)]
311261
fn from(lock: packed::Script) -> AddressPayload {
312262
let hash_type: ScriptHashType = lock.hash_type().try_into().expect("Invalid hash_type");
313263
let code_hash = lock.code_hash();
314-
let code_hash_h256: H256 = code_hash.unpack();
315264
let args = lock.args().raw_data();
316-
let net_ty = NetworkType::Mainnet;
317-
318-
if hash_type == ScriptHashType::Type
319-
&& code_hash_h256 == SIGHASH_TYPE_HASH
320-
&& args.len() == 20
321-
{
322-
let index = CodeHashIndex::Sighash;
323-
let hash = H160::from_slice(args.as_ref()).unwrap();
324-
AddressPayload::Short {
325-
net_ty,
326-
index,
327-
hash,
328-
}
329-
} else if hash_type == ScriptHashType::Type
330-
&& code_hash_h256 == MULTISIG_TYPE_HASH
331-
&& args.len() == 20
332-
{
333-
let index = CodeHashIndex::Multisig;
334-
let hash = H160::from_slice(args.as_ref()).unwrap();
335-
AddressPayload::Short {
336-
net_ty,
337-
index,
338-
hash,
339-
}
340-
} else {
341-
AddressPayload::Full {
342-
hash_type,
343-
code_hash,
344-
args,
345-
}
265+
266+
AddressPayload::Full {
267+
hash_type,
268+
code_hash,
269+
args,
346270
}
347271
}
348272
}
349273

350-
#[derive(Hash, Eq, PartialEq, Debug, Clone)]
274+
#[derive(Clone, Debug, Hash, PartialEq, Eq)]
351275
pub struct Address {
352276
network: NetworkType,
353277
payload: AddressPayload,
@@ -507,29 +431,32 @@ mod test {
507431
use ckb_types::{h160, h256};
508432

509433
#[test]
434+
#[allow(deprecated)]
510435
fn test_short_address() {
511-
let payload = AddressPayload::from_pubkey_hash(
436+
let payload =
437+
AddressPayload::from_pubkey_hash(h160!("0xb39bbc0b3673c7d36450bc14cfcdad2d559c6c64"));
438+
439+
let short_payload = AddressPayload::new_short(
512440
NetworkType::Mainnet,
441+
CodeHashIndex::Sighash,
513442
h160!("0xb39bbc0b3673c7d36450bc14cfcdad2d559c6c64"),
514443
);
515444
let address = Address::new(NetworkType::Mainnet, payload, false);
516445
assert_eq!(
517446
address.to_string(),
518-
"ckb1qyqt8xaupvm8837nv3gtc9x0ekkj64vud3jqfwyw5v"
447+
"ckb1qjda0cr08m85hc8jlnfp3zer7xulejywt49kt2rr0vthywaa50xw3vumhs9nvu786dj9p0q5elx66t24n3kxgj53qks"
519448
);
520449
assert_eq!(
521-
address,
450+
Address::new(NetworkType::Mainnet, short_payload, false),
522451
Address::from_str("ckb1qyqt8xaupvm8837nv3gtc9x0ekkj64vud3jqfwyw5v").unwrap()
523452
);
524453

525-
let payload = AddressPayload::from_pubkey_hash(
526-
NetworkType::Mainnet,
527-
h160!("0xb39bbc0b3673c7d36450bc14cfcdad2d559c6c64"),
528-
);
454+
let payload =
455+
AddressPayload::from_pubkey_hash(h160!("0xb39bbc0b3673c7d36450bc14cfcdad2d559c6c64"));
529456
let address = Address::new(NetworkType::Mainnet, payload, true);
530457
assert_eq!(
531458
address.to_string(),
532-
"ckb1qyqt8xaupvm8837nv3gtc9x0ekkj64vud3jqfwyw5v"
459+
"ckb1qzda0cr08m85hc8jlnfp3zer7xulejywt49kt2rr0vthywaa50xwsqdnnw7qkdnnclfkg59uzn8umtfd2kwxceqxwquc4"
533460
);
534461

535462
let index = CodeHashIndex::Multisig;

common/src/utils.rs

+2-17
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,7 @@
1-
use crate::MercuryError;
2-
3-
use crate::address::{Address, AddressPayload, CodeHashIndex};
4-
use crate::NetworkType;
1+
use crate::{address::Address, MercuryError};
52

63
use anyhow::Result;
7-
use ckb_types::{packed, H160, U256};
4+
use ckb_types::{packed, U256};
85
use derive_more::Display;
96
use num_bigint::BigUint;
107

@@ -34,18 +31,6 @@ pub fn parse_address(input: &str) -> Result<Address> {
3431
}
3532
}
3633

37-
pub fn to_universal_identity(net_ty: NetworkType, input: &Address, is_new: bool) -> Address {
38-
Address::new(
39-
input.network(),
40-
AddressPayload::new_short(
41-
net_ty,
42-
CodeHashIndex::Sighash,
43-
H160::from_slice(input.payload().args().as_ref()).unwrap(),
44-
),
45-
is_new,
46-
)
47-
}
48-
4934
pub fn to_fixed_array<const LEN: usize>(input: &[u8]) -> [u8; LEN] {
5035
assert_eq!(input.len(), LEN);
5136
let mut list = [0; LEN];

core/rpc/core/src/impl/adjust_account.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -289,7 +289,7 @@ impl<C: CkbRpc> MercuryRpcImpl<C> {
289289

290290
let address = Address::new(
291291
self.network_type,
292-
AddressPayload::from_pubkey_hash(self.network_type, pub_key),
292+
AddressPayload::from_pubkey_hash(pub_key),
293293
true,
294294
)
295295
.to_string();

0 commit comments

Comments
 (0)