I just tested this, and it does return the same principal id.
main.rs
use ic_agent::{identity::BasicIdentity, Identity};
fn main() {
println!("Hello, world!");
let my_identity = use_identity();
let my_principal = my_identity.sender().unwrap();
println!("My identity is: {:?}", my_principal.to_text());
}
fn use_identity() -> impl Identity {
let pem_path = "../identity.pem".to_string();
BasicIdentity::from_pem_file(pem_path).expect("Could not read the key pair.")
}
Cargo.toml
[package]
name = "rust-test-pem"
version = "0.1.0"
edition = "2018"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
ic-agent = "0.12.1"
The challenge is in parsing the .pem file - as far as I’ve been able to tell, there is no equivalent tool to parse the file, so we need to re-implement the Rust package in order to get reproducible identities.
Joke beside with Ed25519KeyIdentity I know understand it expects a Uint8Array(64) as parameter (doing so no error “bad secret key size”). Being said, did not manager to transform the pem file content to such format yet.
const initEd25519Identity = () => {
const buffer = readFileSync('/Users/david.../default/identity.pem');
const key = buffer.toString('utf-8');
// TODO - BEGIN: transform key to Uint8Array(64)
const privateKey = crypto.createHash('sha512').update(key).digest('base64');
const secretKey = new Uint8Array(Buffer.from(privateKey, 'base64'));
console.log(secretKey);
// END
return Ed25519KeyIdentity.fromSecretKey(secretKey);
};
That’s an excellent hint but I wasn’t successful. Like I tried all node-rsa formats (private, public, der, pem) with key as string, as buffer, hardcoded, all failed with various errors:
Unsupported key format
Invalid Public key format
[InvalidAsn1Error]: encoding too long
Also tried to sublet nodejs native crypto lib (node - mdn) but there are even more options and therefore even more error msgs