Skip to content

Commit 94cf269

Browse files
authored
Merge pull request #653 from eval-exec/exec/deploy-well-know-script-check
deploy: warn user if `deployment.toml`'s `[lock] is not well known
2 parents 1221235 + d938cd1 commit 94cf269

File tree

2 files changed

+39
-5
lines changed

2 files changed

+39
-5
lines changed

src/subcommands/deploy/mod.rs

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,12 @@ use ckb_hash::blake2b_256;
1010
use ckb_jsonrpc_types as json_types;
1111
use ckb_jsonrpc_types::JsonBytes;
1212
use ckb_sdk::{
13+
constants::{MultisigScript, SIGHASH_TYPE_HASH},
1314
traits::{DefaultTransactionDependencyProvider, Signer},
1415
unlock::MultisigConfig,
1516
Address, HumanCapacity,
1617
};
17-
use ckb_types::{bytes::Bytes, packed, prelude::*, H160, H256};
18+
use ckb_types::{bytes::Bytes, core::ScriptHashType, packed, prelude::*, H160, H256};
1819
use clap::{App, Arg, ArgMatches};
1920

2021
use super::{CliSubCommand, Output, ALLOW_ZERO_LOCK_HELP_MSG};
@@ -29,7 +30,7 @@ use crate::utils::{
2930
other::{get_live_cell_with_cache, get_network_type, read_password},
3031
rpc::HttpRpcClient,
3132
signer::KeyStoreHandlerSigner,
32-
tx_helper::SignerFn,
33+
tx_helper::{SignerFn, ZERO_HASH},
3334
};
3435

3536
mod deployment;
@@ -168,6 +169,9 @@ impl CliSubCommand for DeploySubCommand<'_> {
168169
load_deployment(&deployment_config).map_err(|err| err.to_string())?;
169170
let lock_script = packed::Script::from(deployment.lock.clone());
170171

172+
// * Validate lock script against known system scripts
173+
validate_lock_script(&lock_script);
174+
171175
// * Load last receipt
172176
let last_recipe =
173177
load_last_snapshot(&migration_dir).map_err(|err| err.to_string())?;
@@ -596,6 +600,36 @@ fn load_deployment(file_path: &Path) -> Result<Deployment> {
596600
Ok(deployment)
597601
}
598602

603+
fn validate_lock_script(lock_script: &packed::Script) {
604+
let code_hash: H256 = lock_script.code_hash().unpack();
605+
let hash_type: ScriptHashType = lock_script
606+
.hash_type()
607+
.try_into()
608+
.unwrap_or(ScriptHashType::Data);
609+
610+
// Check if it matches known system scripts
611+
let is_known =
612+
// Sighash
613+
(code_hash == SIGHASH_TYPE_HASH && hash_type == ScriptHashType::Type)
614+
// Multisig Legacy
615+
|| (code_hash == MultisigScript::Legacy.script_id().code_hash
616+
&& hash_type == MultisigScript::Legacy.script_id().hash_type)
617+
// Multisig V2
618+
|| (code_hash == MultisigScript::V2.script_id().code_hash
619+
&& hash_type == MultisigScript::V2.script_id().hash_type)
620+
// Zero lock
621+
|| code_hash == ZERO_HASH;
622+
623+
if !is_known {
624+
eprintln!(
625+
"Warning: The lock script does not match well known scripts (sighash, multisig[legacy, v2], or zero lock)."
626+
);
627+
eprintln!(" code_hash: {:#x}", code_hash);
628+
eprintln!(" hash_type: {:?}", hash_type);
629+
eprintln!(" Please verify this is the correct lock script for your deployment.");
630+
}
631+
}
632+
599633
fn load_snapshot(migration_dir: &Path, snapshot_name: String) -> Result<DeploymentRecipe> {
600634
let mut path = migration_dir.to_path_buf();
601635
path.push(snapshot_name);

src/utils/tx_helper.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@ use crate::utils::genesis_info::GenesisInfo;
2020

2121
// TODO: Add dao support
2222

23+
pub const ZERO_HASH: H256 =
24+
h256!("0x0000000000000000000000000000000000000000000000000000000000000000");
25+
2326
/// A transaction helper handle input/output with secp256k1(sighash/multisg) lock
2427
/// 1. Sign transaction
2528
/// 2. Inspect transaction information
@@ -423,9 +426,6 @@ pub fn check_lock_script(
423426
Other,
424427
}
425428

426-
pub const ZERO_HASH: H256 =
427-
h256!("0x0000000000000000000000000000000000000000000000000000000000000000");
428-
429429
let code_hash: H256 = lock.code_hash().unpack();
430430
let hash_type: ScriptHashType = lock.hash_type().try_into().expect("hash_type");
431431
let lock_args = lock.args().raw_data();

0 commit comments

Comments
 (0)