如何在ruby中进行base58编码?
我正在尝试将钱包地址编码为原始格式,如wiki中解释的Base58格式 Protoshares地址以p()开头 我已经给出了交易的原始地址。看起来是这样的:如何在ruby中进行base58编码?,ruby,encoding,cryptography,base64,bitcoin,Ruby,Encoding,Cryptography,Base64,Bitcoin,我正在尝试将钱包地址编码为原始格式,如wiki中解释的Base58格式 Protoshares地址以p()开头 我已经给出了交易的原始地址。看起来是这样的: 028401a2e512b1b91b882ee1c9291cd407c10916bf791662f7189c9c805643e51c 现在,我已经按照wiki中的指南一步一步地进行了操作,我的ruby代码如下所示: # 1 - Take the corresponding public key generated with it (65
028401a2e512b1b91b882ee1c9291cd407c10916bf791662f7189c9c805643e51c
现在,我已经按照wiki中的指南一步一步地进行了操作,我的ruby代码如下所示:
# 1 - Take the corresponding public key generated with it (65 bytes, 1 byte 0x04, 32 bytes corresponding to X coordinate, 32 bytes corresponding to Y coordinate)
sender = '028401a2e512b1b91b882ee1c9291cd407c10916bf791662f7189c9c805643e51c'
# 2 - Perform SHA-256 hashing on the public key
sender = Digest::SHA256.new.update(sender)
# 3 - Perform RIPEMD-160 hashing on the result of SHA-256
sender = Digest::RMD160.new.update(sender.to_s)
# 4 - Add version byte in front of RIPEMD-160 hash (0x00 for Main Network)
sender = '00' + sender.to_s
# 5 - Perform SHA-256 hash on the extended RIPEMD-160 result
checksum = Digest::SHA256.new.update(sender.to_s)
# 6 - Perform SHA-256 hash on the result of the previous SHA-256 hash
checksum = Digest::SHA256.new.update(checksum.to_s)
# 7 - Take the first 4 bytes of the second SHA-256 hash. This is the address checksum
checksum = checksum.to_s[0,8]
# 8 - Add the 4 checksum bytes from point 7 at the end of extended RIPEMD-160 hash from point 4. This is the 25-byte binary Bitcoin Address.
sender += checksum
0073eb40b21b02c08e93f6ef1bec5828763ac89e456c2f6fec
到目前为止,这似乎效果不错。我当前的结果如下所示:
# 1 - Take the corresponding public key generated with it (65 bytes, 1 byte 0x04, 32 bytes corresponding to X coordinate, 32 bytes corresponding to Y coordinate)
sender = '028401a2e512b1b91b882ee1c9291cd407c10916bf791662f7189c9c805643e51c'
# 2 - Perform SHA-256 hashing on the public key
sender = Digest::SHA256.new.update(sender)
# 3 - Perform RIPEMD-160 hashing on the result of SHA-256
sender = Digest::RMD160.new.update(sender.to_s)
# 4 - Add version byte in front of RIPEMD-160 hash (0x00 for Main Network)
sender = '00' + sender.to_s
# 5 - Perform SHA-256 hash on the extended RIPEMD-160 result
checksum = Digest::SHA256.new.update(sender.to_s)
# 6 - Perform SHA-256 hash on the result of the previous SHA-256 hash
checksum = Digest::SHA256.new.update(checksum.to_s)
# 7 - Take the first 4 bytes of the second SHA-256 hash. This is the address checksum
checksum = checksum.to_s[0,8]
# 8 - Add the 4 checksum bytes from point 7 at the end of extended RIPEMD-160 hash from point 4. This is the 25-byte binary Bitcoin Address.
sender += checksum
0073eb40b21b02c08e93f6ef1bec5828763ac89e456c2f6fec
但现在我被卡住了。我正在使用并尝试最终对地址进行编码:
# 9 - Convert the result from a byte string into a base58 string using Base58Check encoding. This is the most commonly used Bitcoin Address format
sender = Base58.encode(sender)
但我有以下问题:
/path/to/base58.rb:23:in `int_to_base58': Value passed is not an Integer. (ArgumentError)
当然不是整数。我把这里的数据类型搞砸了吗?如何使其正常工作
谢谢 在我看来就像是一个十六进制数字卡在字符串中,请将其设置为数字:
2.0.0p247 :001 > require 'base58'
=> true
2.0.0p247 :002 > x = '0073eb40b21b02c08e93f6ef1bec5828763ac89e456c2f6fec'
=> "0073eb40b21b02c08e93f6ef1bec5828763ac89e456c2f6fec"
2.0.0p247 :003 > Base58.encode(x.to_i(16))
=> "byVwGWzMZZ7HwsufSQx6T2pRapGZWdkAL"
不确定您期望的输出是什么,但这会执行…还有
x.hex
。