From 69dd498980941fdc41aeac3ac4f73e339721b1ae Mon Sep 17 00:00:00 2001 From: Linfeng Qian Date: Thu, 28 Oct 2021 16:44:32 +0800 Subject: [PATCH] feat: add `util address-info` subcommand --- src/subcommands/util.rs | 21 ++++++++++++++++++++- test/src/main.rs | 2 +- test/src/spec/util.rs | 12 ++++++++++++ 3 files changed, 33 insertions(+), 2 deletions(-) diff --git a/src/subcommands/util.rs b/src/subcommands/util.rs index f58c4ebc..ebea764f 100644 --- a/src/subcommands/util.rs +++ b/src/subcommands/util.rs @@ -1,7 +1,7 @@ use chrono::prelude::*; use ckb_crypto::secp::SECP256K1; use ckb_hash::blake2b_256; -use ckb_jsonrpc_types::JsonBytes; +use ckb_jsonrpc_types::{self, JsonBytes}; use ckb_sdk::{ constants::{MULTISIG_TYPE_HASH, SIGHASH_TYPE_HASH}, rpc::ChainInfo, @@ -236,6 +236,9 @@ impl<'a> UtilSubCommand<'a> { .required(true) .about("The difficulty value") ), + App::new("address-info") + .about("Show information about an address") + .arg(arg_address), App::new("to-genesis-multisig-addr") .about("Convert address in single signature format to multisig format (only for mainnet genesis cells)") .arg( @@ -625,6 +628,22 @@ message = "0x" }); Ok(Output::new_output(resp)) } + ("address-info", Some(m)) => { + let address: Address = AddressParser::default().from_matches(m, "address")?; + let resp = serde_json::json!({ + "extra": { + "data-encoding": if address.is_new() { "bech32m" } else { "bech32"}, + "address-type": address.payload().ty(address.is_new()), + }, + "network": address.network().to_str(), + "lock_script": { + "code_hash": format!("{:#x}", address.payload().code_hash()), + "hash_type": ckb_jsonrpc_types::ScriptHashType::from(address.payload().hash_type()), + "args": format!("0x{}", hex_string(address.payload().args().as_ref())), + }, + }); + Ok(Output::new_output(resp)) + } ("to-genesis-multisig-addr", Some(m)) => { let chain_info: ChainInfo = self .rpc_client diff --git a/test/src/main.rs b/test/src/main.rs index 44020519..1f133e55 100644 --- a/test/src/main.rs +++ b/test/src/main.rs @@ -63,6 +63,7 @@ fn run_spec(spec: Box, app: &App) { fn all_specs() -> Vec> { vec![ + Box::new(Util), Box::new(Plugin), Box::new(RpcGetTipBlockNumber), Box::new(WalletTransfer), @@ -70,6 +71,5 @@ fn all_specs() -> Vec> { Box::new(DaoPrepareOne), Box::new(DaoPrepareMultiple), Box::new(DaoWithdrawMultiple), - Box::new(Util), ] } diff --git a/test/src/spec/util.rs b/test/src/spec/util.rs index f9918703..e32ac158 100644 --- a/test/src/spec/util.rs +++ b/test/src/spec/util.rs @@ -115,5 +115,17 @@ impl Spec for Util { let verify_ok = value["verify-ok"].as_bool().unwrap(); assert_eq!(recoverable, true); assert_eq!(verify_ok, false); + + let output = setup.cli("util address-info --address ckt1qn0wcya8hrssq4u4gyuyejh5k53rwvly54yrcwhvjhwufsw4afdjynxzuefxyp9wdghglncj77k5wt6p59sx6kukyjlwh5s467qgp8m2jyzt6r7d9jr0s9q8764qnqvze3mzrdks4p5c3j"); + let value: serde_yaml::Value = serde_yaml::from_str(&output).unwrap(); + assert_eq!(value["extra"]["address-type"].as_str().unwrap(), "FullType"); + assert_eq!(value["extra"]["data-encoding"].as_str().unwrap(), "bech32"); + assert_eq!(value["network"].as_str().unwrap(), "ckb_testnet"); + assert_eq!( + value["lock_script"]["code_hash"].as_str().unwrap(), + "0xdeec13a7b8e100579541384ccaf4b5223733e4a5483c3aec95ddc4c1d5ea5b22" + ); + assert_eq!(value["lock_script"]["hash_type"].as_str().unwrap(), "type"); + assert_eq!(value["lock_script"]["args"].as_str().unwrap(), "0x4cc2e6526204ae6a2e8fcf12f7ad472f41a1606d5b9624beebd215d780809f6a9104bd0fcd2c86f81407f6aa098182cc7621b6d0"); } }