2

My json file looks like

{
  "keypairs": {
      "keypair1":[12345678],
      "keypair2":[12345678],
      "keypair3":[12345678],
  }
}

where [12345678] is actually the array I copied from the keypair file. How do I read these keypairs again with a rust client? I know there is a solana_sdk::signature::read_keypair_file but that's fore only 1 keypair in a file, and I'm not sure what to give as arguments to read_keypair.

1 Answer 1

2

You can get the number array for the desired keypair as a Vec<serde_json::Value> within the JSON file using serde_json and convert it to a Vec<u8> using serde::from_value.

Once you have this, you can provide it to the Keypair::from_bytes function to construct your keypair instance from the raw byte array.

let file = std::fs::File::open(path_to_json).unwrap();
let data: serde_json::Value = serde_json::from_reader(file).unwrap();
let key_bytes: Vec<u8> = serde_json::from_value(
    data.get("keypairs").unwrap().get("keypair1").unwrap().clone()
).unwrap();

let pair = solana_sdk::signature::Keypair::from_bytes(&key_bytes).unwrap();

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.