@@ -5,7 +5,7 @@ use crate::{
5
5
6
6
use bech32:: { convert_bits, ToBase32 , Variant } ;
7
7
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 } ;
9
9
use serde:: { Deserialize , Serialize } ;
10
10
11
11
use std:: convert:: TryInto ;
@@ -74,6 +74,7 @@ pub enum AddressPayload {
74
74
}
75
75
76
76
impl AddressPayload {
77
+ #[ deprecated]
77
78
pub fn new_short ( net_ty : NetworkType , index : CodeHashIndex , hash : H160 ) -> AddressPayload {
78
79
AddressPayload :: Short {
79
80
net_ty,
@@ -93,11 +94,13 @@ impl AddressPayload {
93
94
args,
94
95
}
95
96
}
97
+
96
98
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)
98
100
}
101
+
99
102
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)
101
104
}
102
105
103
106
pub fn ty ( & self , is_new : bool ) -> AddressType {
@@ -185,19 +188,18 @@ impl AddressPayload {
185
188
. unwrap_or_else ( |_| panic ! ( "Encode address failed: payload={:?}" , self ) )
186
189
}
187
190
188
- pub fn from_pubkey ( net_ty : NetworkType , pubkey : & secp256k1:: PublicKey ) -> AddressPayload {
191
+ pub fn from_pubkey ( pubkey : & secp256k1:: PublicKey ) -> AddressPayload {
189
192
// Serialize pubkey as compressed format
190
193
let hash = H160 :: from_slice ( & blake2b_256 ( & pubkey. serialize ( ) [ ..] ) [ 0 ..20 ] )
191
194
. expect ( "Generate hash(H160) from pubkey failed" ) ;
192
- AddressPayload :: from_pubkey_hash ( net_ty , hash)
195
+ AddressPayload :: from_pubkey_hash ( hash)
193
196
}
194
197
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 ( ) ) ,
201
203
}
202
204
}
203
205
@@ -220,74 +222,23 @@ impl AddressPayload {
220
222
}
221
223
}
222
224
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 ( ) ,
280
230
}
281
231
}
282
232
}
283
233
284
234
impl fmt:: Debug for AddressPayload {
285
235
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" ,
290
240
} ;
241
+
291
242
f. debug_struct ( "AddressPayload" )
292
243
. field ( "hash_type" , & hash_type)
293
244
. field ( "code_hash" , & self . code_hash ( ) )
@@ -307,47 +258,20 @@ impl From<&AddressPayload> for packed::Script {
307
258
}
308
259
309
260
impl From < packed:: Script > for AddressPayload {
310
- #[ allow( clippy:: fallible_impl_from) ]
311
261
fn from ( lock : packed:: Script ) -> AddressPayload {
312
262
let hash_type: ScriptHashType = lock. hash_type ( ) . try_into ( ) . expect ( "Invalid hash_type" ) ;
313
263
let code_hash = lock. code_hash ( ) ;
314
- let code_hash_h256: H256 = code_hash. unpack ( ) ;
315
264
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,
346
270
}
347
271
}
348
272
}
349
273
350
- #[ derive( Hash , Eq , PartialEq , Debug , Clone ) ]
274
+ #[ derive( Clone , Debug , Hash , PartialEq , Eq ) ]
351
275
pub struct Address {
352
276
network : NetworkType ,
353
277
payload : AddressPayload ,
@@ -507,29 +431,32 @@ mod test {
507
431
use ckb_types:: { h160, h256} ;
508
432
509
433
#[ test]
434
+ #[ allow( deprecated) ]
510
435
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 (
512
440
NetworkType :: Mainnet ,
441
+ CodeHashIndex :: Sighash ,
513
442
h160 ! ( "0xb39bbc0b3673c7d36450bc14cfcdad2d559c6c64" ) ,
514
443
) ;
515
444
let address = Address :: new ( NetworkType :: Mainnet , payload, false ) ;
516
445
assert_eq ! (
517
446
address. to_string( ) ,
518
- "ckb1qyqt8xaupvm8837nv3gtc9x0ekkj64vud3jqfwyw5v "
447
+ "ckb1qjda0cr08m85hc8jlnfp3zer7xulejywt49kt2rr0vthywaa50xw3vumhs9nvu786dj9p0q5elx66t24n3kxgj53qks "
519
448
) ;
520
449
assert_eq ! (
521
- address ,
450
+ Address :: new ( NetworkType :: Mainnet , short_payload , false ) ,
522
451
Address :: from_str( "ckb1qyqt8xaupvm8837nv3gtc9x0ekkj64vud3jqfwyw5v" ) . unwrap( )
523
452
) ;
524
453
525
- let payload = AddressPayload :: from_pubkey_hash (
526
- NetworkType :: Mainnet ,
527
- h160 ! ( "0xb39bbc0b3673c7d36450bc14cfcdad2d559c6c64" ) ,
528
- ) ;
454
+ let payload =
455
+ AddressPayload :: from_pubkey_hash ( h160 ! ( "0xb39bbc0b3673c7d36450bc14cfcdad2d559c6c64" ) ) ;
529
456
let address = Address :: new ( NetworkType :: Mainnet , payload, true ) ;
530
457
assert_eq ! (
531
458
address. to_string( ) ,
532
- "ckb1qyqt8xaupvm8837nv3gtc9x0ekkj64vud3jqfwyw5v "
459
+ "ckb1qzda0cr08m85hc8jlnfp3zer7xulejywt49kt2rr0vthywaa50xwsqdnnw7qkdnnclfkg59uzn8umtfd2kwxceqxwquc4 "
533
460
) ;
534
461
535
462
let index = CodeHashIndex :: Multisig ;
0 commit comments