Skip to content
Afri edited this page Oct 31, 2022 · 3 revisions

Create a new, random Secp256k1 private-public keypair.

key_pair = Eth::Key.new
# => #<Eth::Key:0x0000558e54f2e008 @private_key=#<Secp256k1::PrivateKey:0x0000558e54f2d658 @data="07d\e\xC7\x98v\x89\x8E\xD4.\xE1gO\x9CG[\xAF\xCFi@\x8CR\x8C\xA4\r\xE6\x95\x9F:\x13Y">, @public_key=#<Secp256k1::PublicKey:0x0000558e54f2d5b8>>

Create a keypair from a known private key.

key_pair = Eth::Key.new priv: "30137644b564785d01420f8043f043d74dcca64008e57c59f8ce713a0005a54b"
# => #<Eth::Key:0x0000558e54f60288 @private_key=#<Secp256k1::PrivateKey:0x0000558e54f6bc28 @data="0\x13vD\xB5dx]\x01B\x0F\x80C\xF0C\xD7M\xCC\xA6@\b\xE5|Y\xF8\xCEq:\x00\x05\xA5K">, @public_key=#<Secp256k1::PublicKey:0x0000558e54f6bbd8>>

Create a password-encrypted Ethereum key-store.

my_key = Eth::Key.new priv: "30137644b564785d01420f8043f043d74dcca64008e57c59f8ce713a0005a54b"
key_store = Eth::Key::Encrypter.perform my_key, "secret-password-1337"
# => "{\"crypto\":{\"cipher\":\"aes-128-ctr\",\"cipherparams\":{\"iv\":\"7e5c0fe1e27f4ea61b0f4427dd63555f\"},\"ciphertext\":\"6353653bba494cdae6bcd510febc980cdc6f7b23cfbdf950d7a909a69625c8fd\",\"kdf\":\"pbkdf2\",\"kdfparams\":{\"c\":262144,\"dklen\":32,\"prf\":\"hmac-sha256\",\"salt\":\"cce96286f3c32267fc91f756365307fe6a4c83b6b2a73c69535f721fa407736c\"},\"mac\":\"3361ffd2b158a1d7bca5a5fd86a251ba3e9d80b602c867a2e0f47023a0e17a57\"},\"id\":\"642ee9fc-72e4-4d0a-902f-247c0b59bfda\",\"version\":3}"
restored_key = Eth::Key::Decrypter.perform key_store, "secret-password-1337"
# => "30137644b564785d01420f8043f043d74dcca64008e57c59f8ce713a0005a54b"

Get the checksummed address of an account.

key_pair.address
# => #<Eth::Address:0x0000558e5508fa00 @address="0x0c53ffa57ec554451315c2568d22477db8e71356">
key_pair.address.to_s
# => "0x0c53FFA57Ec554451315c2568d22477dB8e71356"

Import an encrypted key-store file.

utc_file = File.open "path/to/UTC--2019-01-23T15-24-31.425421039Z--0x0c53ffa57ec554451315c2568d22477db8e71356"
#=> #<File:path/to/UTC--2019-01-23T15-24-31.425421039Z--0x0c53ffa57ec554451315c2568d22477db8e71356>
key_store = JSON.load utc_file
#=> {"address"=>"0x0c53ffa57ec554451315c2568d22477db8e71356",
#    "crypto"=>
#     {"cipher"=>"aes-128-ctr",
#      "ciphertext"=>"e02...35a",
#      "cipherparams"=>{"iv"=>"63c...efd"},
#      "kdf"=>"scrypt",
#      "kdfparams"=>{"dklen"=>32, "n"=>262144, "p"=>1, "r"=>8, "salt"=>"a3c...9a6"},
#      "mac"=>"5ca...9f1"},
#    "id"=>"0c6...a7b",
#    "version"=>3}
key_pair = Eth::Key::Decrypter.perform key_store, "secret-password-1337"
#=> #<Eth::Key:0x0000558e54fb9a40
# @private_key=#<Secp256k1::PrivateKey:0x0000558e54fb96a8 @data="\v\x92\xB27...\x7F\xC7\t">,
# @public_key=#<Secp256k1::PublicKey:0x0000558e54fb9658>>
key_pair.private_hex
#=> "30137644b564785d01420f8043f043d74dcca64008e57c59f8ce713a0005a54b"
key_pair.address.to_s
#=> "0x0c53FFA57Ec554451315c2568d22477dB8e71356"

Encrypt a keypair and generate a JSON key-store.

[16] pry(main)> key_pair = Eth::Key.new priv: "30137644b564785d01420f8043f043d74dcca64008e57c59f8ce713a0005a54b"
# => #<Eth::Key:0x0000558e54f0f9f0 @private_key=#<Secp256k1::PrivateKey:0x0000558e54f0f298 @data="0\x13vD\xB5dx]\x01B\x0F\x80C\xF0C\xD7M\xCC\xA6@\b\xE5|Y\xF8\xCEq:\x00\x05\xA5K">, @public_key=#<Secp256k1::PublicKey:0x0000558e54f0f248>>
[17] pry(main)> key_store = Eth::Key::Encrypter.perform key_pair, "secret-password-1337"
# => "{\"crypto\":{\"cipher\":\"aes-128-ctr\",\"cipherparams\":{\"iv\":\"a88b15f8b6094661d9a21cc2ad931752\"},\"ciphertext\":\"034b7ddd03d281d550c33cce596200e2fa02c69fd5169c03affe47bb909ece75\",\"kdf\":\"pbkdf2\",\"kdfparams\":{\"c\":262144,\"dklen\":32,\"prf\":\"hmac-sha256\",\"salt\":\"e9318c569e3c477ea8192b2d80cb7cd41336bba49872e14681d6f2a6a67e7004\"},\"mac\":\"7444051da0f6ca7653dc471875385930b538436df81544e39b41d5e43ad656a0\"},\"id\":\"0cb0d53e-3666-48cb-9e66-6b03e8443212\",\"version\":3}"

That's it. Check out the API documentation for all available attributes of a keypair.

Clone this wiki locally