在 ruby 中创建比特币地址
create bitcoin address in ruby
我正在尝试根据比特币 wiki (bitcoin creation according bitcoin wiki) 的文档在 ruby 中创建一个比特币地址。
起点只是一些模拟 ripmed160 输出的随机字符串。
不幸的是,我并没有完全成功,这是我的代码:
require 'base58_gmp'
tx_hash = "a8d0c0184dde994a09ec054286f1ce581bebf46446a512166eae7628734ea0a5"
ripmed160 = tx_hash[0..39]
ripmed160_with_pre = "00" + ripmed160
sha1 = Digest::SHA256.hexdigest ripmed160_with_pre
sha2 = Digest::SHA256.hexdigest sha1
bin_address = Integer("0x" + ripmed160_with_pre + sha2[0..7])
bitcoin_address = "1" + Base58GMP.encode(bin_address, 'bitcoin') # => "1GPcbTYDBwJ42MfKkedxjmJ3nrgoaNd2Sf"
我得到一些看起来像比特币地址的东西,但 blockchain.info 无法识别它,所以我猜它是无效的。
你能帮我完成这项工作吗?
计算 SHA256 校验和时,请确保根据上一步的实际字节数计算它,而不是这些字节的十六进制编码:
# First convert to actual bytes.
bytes = [ripmed160_with_pre].pack('H*')
# Now calculate the first hash over the raw bytes, and
# return the raw bytes again for the next hash
# (note: digest not hexdigest).
sha1 = Digest::SHA256.digest bytes
# Second SHA256, using the raw bytes from the previous step
# but this time we can use hexdigest as the rest of the code
# assumes hex encoded strings
sha2 = Digest::SHA256.hexdigest sha1
我正在尝试根据比特币 wiki (bitcoin creation according bitcoin wiki) 的文档在 ruby 中创建一个比特币地址。 起点只是一些模拟 ripmed160 输出的随机字符串。 不幸的是,我并没有完全成功,这是我的代码:
require 'base58_gmp'
tx_hash = "a8d0c0184dde994a09ec054286f1ce581bebf46446a512166eae7628734ea0a5"
ripmed160 = tx_hash[0..39]
ripmed160_with_pre = "00" + ripmed160
sha1 = Digest::SHA256.hexdigest ripmed160_with_pre
sha2 = Digest::SHA256.hexdigest sha1
bin_address = Integer("0x" + ripmed160_with_pre + sha2[0..7])
bitcoin_address = "1" + Base58GMP.encode(bin_address, 'bitcoin') # => "1GPcbTYDBwJ42MfKkedxjmJ3nrgoaNd2Sf"
我得到一些看起来像比特币地址的东西,但 blockchain.info 无法识别它,所以我猜它是无效的。 你能帮我完成这项工作吗?
计算 SHA256 校验和时,请确保根据上一步的实际字节数计算它,而不是这些字节的十六进制编码:
# First convert to actual bytes.
bytes = [ripmed160_with_pre].pack('H*')
# Now calculate the first hash over the raw bytes, and
# return the raw bytes again for the next hash
# (note: digest not hexdigest).
sha1 = Digest::SHA256.digest bytes
# Second SHA256, using the raw bytes from the previous step
# but this time we can use hexdigest as the rest of the code
# assumes hex encoded strings
sha2 = Digest::SHA256.hexdigest sha1