#ethereum #crypto #transaction #public-key

ethereum-private-key-to-address

Library that converts private key to valid ethereum address

1 unstable release

0.1.0 Jan 8, 2023

#173 in #public-key

33 downloads per month

MIT license

13KB
174 lines

Ethereum Private Key To Address

This library will calculate ethereum address from a private key.

Calculate Address

Step by step example to generate address:

  1. Add ethereum-private-key-to-address to your Cargo.toml:
[dependencies]
ethereum-private-key-to-address = "0.1"
  1. Import PrivateKey struct into your project:
use ethereum_private_key_to_address::PrivateKey;
  1. Create PrivateKey struct with one of these methods:
use ethereum_private_key_to_address::PrivateKey;

let private_key = PrivateKey::from_str("<your private key as string>").unwrap();

let private_key = PrivateKey::from_slice("<your private key as &[u8]>").unwrap();
  1. Call the address() method on the PrivateKey struct:
use ethereum_private_key_to_address::PrivateKey;

let private_key = PrivateKey::from_str("<your private key as string>").unwrap();

// This will calculate your address from the given private key
let address = private_key.address();

println!("{}", address);

Calculate Public Key

Step by step example to generate public key:

  1. Add ethereum-private-key-to-address to your Cargo.toml:
[dependencies]
ethereum-private-key-to-address = "0.1"
  1. Import PrivateKey struct into your project:
use ethereum_private_key_to_address::PrivateKey;
  1. Create PrivateKey struct with one of these methods:
use ethereum_private_key_to_address::PrivateKey;

let private_key = PrivateKey::from_str("<your private key as string>").unwrap();

let private_key = PrivateKey::from_slice("<your private key as &[u8]>").unwrap();
  1. There are multiple options when it comes to the public key. Since public key consists of: prefix: 1 byte, x-coordinate: 32 bytes, y-coordinate: 32 bytes. Here are all the methods you can use.
use ethereum_private_key_to_address::PrivateKey;

let private_key = PrivateKey::from_str("<your private key as string>").unwrap();

// Returns Full 65 byte Public Key including the prefix as a String. In this case prefix is 0x04. 
// 0x04 is used to specify the type of the public key.
let full_public_key_with_prefix = private_key.public_key_full();

// Returns Full 64 byte Public Key from Private Key without 0x04 in the front as a String. 
// 0x04 is used to specify the type of the public key. 0x04 in front means the public key is uncompressed.
let full_public_key = private_key.public_key();

// Returns the x-coordiante of the public key as a string.
let x_coordinate = private_key.public_key_x();

// Returns the y-coordinate of the public key.
let y_coordiante = private_key.public_key_y();

// Returns the entire public key in [u8; 65] format.
let public_key_slice = private_key.public_key_slice();

Dependencies

~6.5MB
~58K SLoC