🔑
Ethereum Rubyで秘密鍵からアドレスを算出
require 'ecdsa'
require 'keccak256'
require 'base16'
def calc(private_key)
public_key = ECDSA::Group::Secp256k1.generator.multiply_by_scalar(private_key)
xy = concat_xy(public_key)
xy16 = Base16.decode16(xy.downcase)
hexdigest = Digest::Keccak256.new.hexdigest(xy16)
"0x#{hexdigest[-40..-1]}"
end
def concat_xy(public_key)
x = public_key.x.to_s(16)
y = public_key.y.to_s(16)
x + y
end
private_key = 1
public_address = calc(private_key)
puts 'private_key: %064x' % private_key
puts "public_address: #{public_address}"
結果
private_key: 0000000000000000000000000000000000000000000000000000000000000001
public_address: 0x7e5f4552091a69125d5dfcb7b8c2659029395bdf
参考
Discussion