{"record":{"id":"46fa0974387ad569","repo":"diem/diem","slug":"failed-to-convert-str-to-chainid","errorCode":null,"errorMessage":"Failed to convert str to ChainId","messagePattern":"Failed to convert str to ChainId","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"error","filePath":"crates/swiss-knife/src/main.rs","lineNumber":221,"sourceCode":"            new_key_hex_encoded,\n        } => {\n            let new_url = new_url.into_bytes();\n            let new_key = hex::decode(new_key_hex_encoded)\n                .expect(\"Failed to hex decode new_key_hex_encoded field\");\n            transaction_builder::encode_rotate_dual_attestation_info_script(new_url, new_key)\n        }\n    };\n    let payload = TransactionPayload::Script(script);\n    let script_hex = hex::encode(bcs::to_bytes(&payload).unwrap());\n    let raw_txn = RawTransaction::new(\n        helpers::account_address_parser(&g.txn_params.sender_address),\n        g.txn_params.sequence_number,\n        payload,\n        g.txn_params.max_gas_amount,\n        g.txn_params.gas_unit_price,\n        g.txn_params.gas_currency_code,\n        g.txn_params.expiration_timestamp_secs,\n        ChainId::from_str(&g.txn_params.chain_id).expect(\"Failed to convert str to ChainId\"),\n    );\n    GenerateRawTxnResponse {\n        script: script_hex,\n        raw_txn: hex::encode(\n            bcs::to_bytes(&raw_txn)\n                .map_err(|err| {\n                    helpers::exit_with_error(format!(\n                        \"bcs serialization failure of raw_txn : {}\",\n                        err\n                    ))\n                })\n                .unwrap(),\n        ),\n    }\n}\n\n#[derive(Deserialize, Serialize)]\n#[serde(rename_all = \"snake_case\")]","sourceCodeStart":203,"sourceCodeEnd":239,"githubUrl":"https://github.com/diem/diem/blob/fc4714a8ea273b6efe8b13dbce72ea60aad9a16c/crates/swiss-knife/src/main.rs#L203-L239","documentation":"`generate_raw_txn` parses the `chain_id` string field with `ChainId::from_str` and panics via `expect` on failure. `ChainId` only parses numeric strings corresponding to known chain IDs, so any non-numeric or out-of-range value aborts the tool.","triggerScenarios":"Passing a `chain_id` in the request that is not a plain numeric string (e.g. \"mainnet\", \"TESTING\", empty, or with whitespace) to swiss-knife's generate_raw_txn endpoint.","commonSituations":"Client sends the chain name instead of its numeric ID; trailing newline from config parsing; using a chain id value unsupported by the linked diem types version.","solutions":["Send the numeric chain id string (e.g. \"2\" for testing) in `txn_params.chain_id`","Pre-validate with `ChainId::from_str` and return a client-side error","Replace `expect` with error propagation for a cleaner API"],"exampleFix":"// before\nChainId::from_str(&g.txn_params.chain_id).expect(\"Failed to convert str to ChainId\"),\n// after\nChainId::from_str(g.txn_params.chain_id.trim())\n    .map_err(|e| format!(\"invalid chain_id '{}': {e}\", g.txn_params.chain_id))?,","handlingStrategy":"validation","validationCode":"fn valid_chain_id(s: &str) -> bool {\n    s.trim().parse::<u8>().is_ok()\n}","typeGuard":null,"tryCatchPattern":"let chain_id = ChainId::from_str(g.txn_params.chain_id.trim())\n    .map_err(|e| bad_request(format!(\"chain_id must be numeric: {e}\")))?;","preventionTips":["Always send chain_id as a numeric string, not a network name","Trim whitespace from config-derived values","Check ChainId::try_from(u8) docs for the correct value (e.g. 2 = testing)"],"tags":[],"backgroundTag":null,"analyzedSha":"fc4714a8ea273b6efe8b13dbce72ea60aad9a16c","analyzedAt":"2026-09-04T21:07:05.890Z","contentChangedAt":"2026-09-04T21:07:05.890Z","schemaVersion":2},"datasetVersion":"2026-09-12T02:17:10.037Z"}