// NodeJS version
// npm install keccakjs
// npm install secp256k1
const SHA3 = require('keccakjs')
let pubKeyHex = "046B8A6CC271D62F45301A3C6C443DC1879193CA1E4A8DF6CCFA8A82525F5A027C1333C132A93EA35857A30CB486BEF3045A4E33B29F18BC128A8EFF6C44074268"
let pubKeyBuf = new Buffer(pubKeyHex, 'hex')
let h = new SHA3(256)
h.update(pubKeyBuf.slice(1)) // 130 => 128
let address = h.digest('hex').slice(-40)
console.log(address)
# Ruby version
# gem install digest-sha3
require 'openssl'
require 'digest/sha3'
curve = OpenSSL::PKey::EC.new('secp256k1')
curve.generate_key
prv = curve.private_key.to_s(16)
pub = curve.public_key.to_bn.to_s(16)
puts "Private Key: #{prv}"
puts "Public Key: #{pub}"
def decode(s, base)
syms = '0123456789abcdef'.freeze
s = s.downcase if base == 16
result = 0
while s.size > 0
result *= base
result += syms.index(s[0])
s = s[1..-1]
end
result
end
def encode(v, base)
syms = 256.times.map {|i| i.chr }.join.freeze
result = ''
while v > 0
result = syms[v % base] + result
v /= base
end
result
end
def public_to_address(str)
str = str[2, 128]
Digest::SHA3.hexdigest(encode(decode(str, 16), 256), 256)[-40..-1]
end
puts "Address: #{public_to_address(pub)}"