use k256::{ecdsa::{SigningKey, Signature, signature::Signer, signature::Verifier, VerifyingKey}}; use rand_core::OsRng; use sha2::{Digest, Sha256}; use ripemd::{Ripemd160}; fn new_keys() { let signing_key = SigningKey::random(&mut OsRng); let private_key = hex::encode(signing_key.to_bytes()); println!("private key: {:x?}", private_key); let verifying_key = signing_key.verifying_key(); let public_key = hex::encode(verifying_key.to_sec1_bytes()); println!("public key: {:x?}", public_key); new_address(&verifying_key); } fn new_address(verifying_key: &VerifyingKey) { let vk = verifying_key.to_sec1_bytes(); let mut hasher = Sha256::new(); hasher.update(vk); let hashed_sha256 = hasher.finalize(); let mut hasher = Ripemd160::new(); hasher.update(hashed_sha256); let account_id = hasher.finalize(); let mut payload = account_id.to_vec(); payload.insert(0, 0x00); let mut hasher = Sha256::new(); hasher.update(&payload); let hash = hasher.finalize(); let mut hasher = Sha256::new(); hasher.update(hash); let checksum = hasher.finalize(); payload.append(&mut checksum[0..4].to_vec()); const ALPHABET: &str = "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz"; let address = base_x::encode(ALPHABET, &payload); println!("address: {:?}", address); } fn main() { new_keys(); }
$ cargo run
…
private key: “745aa1c916085b2e3423dc7a22792945bae57c38bd5bc1bb0426ba2156f8a39c”
public key: “029e1baf2992b44af147c306fb728f8b00e908aa7f09e25eaa0a2fed3f71ad4cf6”
address: “1E5b59jN4nyM9kpzqdXfW7MkLJ2CApAVjT”
これをWebフレームワークで表現したい。使うのはaxumかな。。。