我正在尝试使用本指南在ruby中生成比特币地址:
https://bhelx.simst.im/articles/generating-bitcoin-keys-from-scratch-with-ruby/
但有些事情并不完全正确,因为生成的地址并不是很正确。
这是我正在使用的课程:
require 'openssl'
require 'ecdsa'
require 'securerandom'
require 'base58'
class BitcoinAddressGenerator
ADDRESS_VERSION = '00'
def self.generate_address
# Bitcoin uses the secp256k1 curve
curve = OpenSSL::PKey::EC.new('secp256k1')
# Now we generate the public and private key together
curve.generate_key
private_key_hex = curve.private_key.to_s(16)
puts "private_key_hex: #{private_key_hex}"
public_key_hex = curve.public_key.to_bn.to_s(16)
puts "public_key_hex: #{public_key_hex}"
pub_key_hash = public_key_hash(public_key_hex)
puts "pub_key_hash: #{pub_key_hash}"
address = generate_address_from_public_key_hash(public_key_hash(public_key_hex))
puts "address: #{address}"
end
def self.generate_address_from_public_key_hash(pub_key_hash)
pk = ADDRESS_VERSION + pub_key_hash
encode_base58(pub_key_hash + checksum(pub_key_hash))
end
def self.int_to_base58(int_val, leading_zero_bytes=0)
alpha = "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz"
base58_val, base = '', alpha.size
while int_val > 0
int_val, remainder = int_val.divmod(base)
base58_val = alpha[remainder] + base58_val
end
base58_val
end
def self.encode_base58(hex)
leading_zero_bytes = (hex.match(/^([0]+)/) ? $1 : '').size / 2
("1"*leading_zero_bytes) + int_to_base58( hex.to_i(16) )
end
def self.checksum(hex)
sha256(sha256(hex))[0...8]
end
# RIPEMD-160 (160 bit) hash
def self.rmd160(hex)
Digest::RMD160.hexdigest([hex].pack("H*"))
end
def self.sha256(hex)
Digest::SHA256.hexdigest([hex].pack("H*"))
end
# Turns public key into the 160 bit public key hash
def self.public_key_hash(hex)
rmd160(sha256(hex))
end
end
输出如下内容:
private_key_hex: C96DE079BAE4877E086288DEDD6F9F70B671862B7E6E4FC0EC401CADB81EDF45
public_key_hex: 0422435DF80F62E643D3CFBA66194052EC9ED0DFB47A1B26A4731079A5FF84FBF98FF0A540B6981D75BA789E6192F3B38BABEF6B0286CAEB4CAFCB51BB96D97B46
public_key_hash: db34927cc5ec0066411f366d9a95f9c6369c6e1d
address: Lz3xnxx6Uh79PEzPpWSMMZJVWR36hJgVL
如果我将此地址插入blockchain.info及类似工具,则表示该地址无效。
非常感谢任何帮助。
答案 0 :(得分:2)
在func spawnTrolls(){
let minValue = self.size.width / 10
let maxValue = self.size.width - 30
let spawnpoint = UInt32(maxValue - minValue)
Troll.position = CGPointMake(CGFloat(arc4random_uniform(spawnpoint)), self.size.height)
self.addChild(Troll)
let action = SKAction.moveToY(-70, duration: 2)
let actionDone = SKAction.removeFromParent()
Troll.runAction(SKAction.sequence([action, actionDone]))
Troll.physicsBody? = SKPhysicsBody(rectangleOfSize: Troll.size)
Troll.physicsBody?.categoryBitMask = physicsCategory.Troll
Troll.physicsBody?.contactTestBitMask = physicsCategory.Rocket
Troll.physicsBody?.affectedByGravity = false
Troll.physicsBody?.dynamic = true
}
_ = NSTimer.scheduledTimerWithTimeInterval(0.5, target: self, selector: Selector("spawnTrolls"), userInfo: nil, repeats: true)
方法中,校验和应该在哈希上,包括地址前缀。在分配之后,您实际上根本没有使用generate_address_from_public_key_hash
变量。代码应该类似于:
pk
错误似乎也出现在您链接的页面上,我猜作者必须犯了一个复制/粘贴错误。
顺便说一句,将所有内容保存在十六进制字符串中并来回解码似乎是一种奇怪的方法。我原以为使用原始二进制字符串会更容易,并且在打印出值时只编码为十六进制。