@@ -73,7 +73,9 @@ For usage information on those methods, call `blockstack-cli [method] -h`
73
73
74
74
`blockstack-cli` accepts flag options as well:
75
75
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
77
79
78
80
" ;
79
81
@@ -185,6 +187,7 @@ enum CliError {
185
187
ClarityGeneralError ( ClarityError ) ,
186
188
Message ( String ) ,
187
189
Usage ,
190
+ InvalidChainId ( std:: num:: ParseIntError ) ,
188
191
}
189
192
190
193
impl std:: error:: Error for CliError {
@@ -204,6 +207,7 @@ impl std::fmt::Display for CliError {
204
207
CliError :: ClarityGeneralError ( e) => write ! ( f, "Clarity error: {}" , e) ,
205
208
CliError :: Message ( e) => write ! ( f, "{}" , e) ,
206
209
CliError :: Usage => write ! ( f, "{}" , USAGE ) ,
210
+ CliError :: InvalidChainId ( e) => write ! ( f, "Invalid chain ID: {}" , e) ,
207
211
}
208
212
}
209
213
}
@@ -848,18 +852,26 @@ fn main() {
848
852
}
849
853
850
854
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
+ }
857
871
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
+ }
863
875
864
876
if let Some ( ( method, args) ) = argv. split_first ( ) {
865
877
match method. as_str ( ) {
@@ -1220,4 +1232,43 @@ mod test {
1220
1232
let result = main_handler ( to_string_vec ( & header_args) ) . unwrap ( ) ;
1221
1233
eprintln ! ( "result:\n {}" , result) ;
1222
1234
}
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
+ }
1223
1274
}
0 commit comments