Skip to content

Commit

Permalink
Merge pull request #442 from TheWaWaR/address-info
Browse files Browse the repository at this point in the history
feat: add `util address-info` subcommand
  • Loading branch information
TheWaWaR authored Dec 1, 2021
2 parents fbef5b5 + 69dd498 commit 4ce4b7e
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 2 deletions.
21 changes: 20 additions & 1 deletion src/subcommands/util.rs
Original file line number Diff line number Diff line change
@@ -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,
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion test/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,13 +63,13 @@ fn run_spec(spec: Box<dyn Spec>, app: &App) {

fn all_specs() -> Vec<Box<dyn Spec>> {
vec![
Box::new(Util),
Box::new(Plugin),
Box::new(RpcGetTipBlockNumber),
Box::new(WalletTransfer),
Box::new(WalletTimelockedAddress),
Box::new(DaoPrepareOne),
Box::new(DaoPrepareMultiple),
Box::new(DaoWithdrawMultiple),
Box::new(Util),
]
}
12 changes: 12 additions & 0 deletions test/src/spec/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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");
}
}

0 comments on commit 4ce4b7e

Please sign in to comment.