Skip to content

Commit 609ad13

Browse files
authored
Merge pull request #5317 from stacks-network/feat/cli-custom-chain-id
feat: support custom chain ids in blockstack-cli
2 parents b0a1b84 + d253dd7 commit 609ad13

File tree

1 file changed

+63
-12
lines changed

1 file changed

+63
-12
lines changed

stackslib/src/blockstack_cli.rs

Lines changed: 63 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,9 @@ For usage information on those methods, call `blockstack-cli [method] -h`
7373
7474
`blockstack-cli` accepts flag options as well:
7575
76-
--testnet instruct the transaction generator to use a testnet version byte instead of MAINNET (default)
76+
--testnet[=chain-id]
77+
instruct the transaction generator to use a testnet version byte instead of MAINNET (default)
78+
optionally, you can specify a custom chain ID to use for the transaction
7779
7880
";
7981

@@ -185,6 +187,7 @@ enum CliError {
185187
ClarityGeneralError(ClarityError),
186188
Message(String),
187189
Usage,
190+
InvalidChainId(std::num::ParseIntError),
188191
}
189192

190193
impl std::error::Error for CliError {
@@ -204,6 +207,7 @@ impl std::fmt::Display for CliError {
204207
CliError::ClarityGeneralError(e) => write!(f, "Clarity error: {}", e),
205208
CliError::Message(e) => write!(f, "{}", e),
206209
CliError::Usage => write!(f, "{}", USAGE),
210+
CliError::InvalidChainId(e) => write!(f, "Invalid chain ID: {}", e),
207211
}
208212
}
209213
}
@@ -848,18 +852,26 @@ fn main() {
848852
}
849853

850854
fn main_handler(mut argv: Vec<String>) -> Result<String, CliError> {
851-
let tx_version = if let Some(ix) = argv.iter().position(|x| x == "--testnet") {
852-
argv.remove(ix);
853-
TransactionVersion::Testnet
854-
} else {
855-
TransactionVersion::Mainnet
856-
};
855+
let mut tx_version = TransactionVersion::Mainnet;
856+
let mut chain_id = CHAIN_ID_MAINNET;
857+
858+
// Look for the `--testnet` flag
859+
if let Some(ix) = argv.iter().position(|x| x.starts_with("--testnet")) {
860+
let flag = argv.remove(ix);
861+
862+
// Check if `--testnet=<chain_id>` is used
863+
if let Some(custom_chain_id) = flag.split('=').nth(1) {
864+
// Attempt to parse the custom chain ID from hex
865+
chain_id = u32::from_str_radix(custom_chain_id.trim_start_matches("0x"), 16)
866+
.map_err(|err| CliError::InvalidChainId(err))?;
867+
} else {
868+
// Use the default testnet chain ID
869+
chain_id = CHAIN_ID_TESTNET;
870+
}
857871

858-
let chain_id = if tx_version == TransactionVersion::Testnet {
859-
CHAIN_ID_TESTNET
860-
} else {
861-
CHAIN_ID_MAINNET
862-
};
872+
// Set the transaction version to Testnet
873+
tx_version = TransactionVersion::Testnet;
874+
}
863875

864876
if let Some((method, args)) = argv.split_first() {
865877
match method.as_str() {
@@ -1220,4 +1232,43 @@ mod test {
12201232
let result = main_handler(to_string_vec(&header_args)).unwrap();
12211233
eprintln!("result:\n{}", result);
12221234
}
1235+
1236+
#[test]
1237+
fn custom_chain_id() {
1238+
// Standard chain id
1239+
let tt_args = [
1240+
"--testnet",
1241+
"token-transfer",
1242+
"043ff5004e3d695060fa48ac94c96049b8c14ef441c50a184a6a3875d2a000f3",
1243+
"1",
1244+
"0",
1245+
"ST1A14RBKJ289E3DP89QAZE2RRHDPWP5RHMYFRCHV",
1246+
"10",
1247+
];
1248+
1249+
let result = main_handler(to_string_vec(&tt_args));
1250+
assert!(result.is_ok());
1251+
1252+
let result = result.unwrap();
1253+
let tx = decode_transaction(&[result], TransactionVersion::Testnet).unwrap();
1254+
assert!(tx.contains("chain_id\":2147483648"));
1255+
1256+
// Custom chain id
1257+
let tt_args = [
1258+
"--testnet=0x12345678",
1259+
"token-transfer",
1260+
"043ff5004e3d695060fa48ac94c96049b8c14ef441c50a184a6a3875d2a000f3",
1261+
"1",
1262+
"0",
1263+
"ST1A14RBKJ289E3DP89QAZE2RRHDPWP5RHMYFRCHV",
1264+
"10",
1265+
];
1266+
1267+
let result = main_handler(to_string_vec(&tt_args));
1268+
assert!(result.is_ok());
1269+
1270+
let result = result.unwrap();
1271+
let tx = decode_transaction(&[result], TransactionVersion::Testnet).unwrap();
1272+
assert!(tx.contains("chain_id\":305419896"));
1273+
}
12231274
}

0 commit comments

Comments
 (0)