Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
ckb-ruby-example.rb
# This contract needs 3 required arguments:
# 0. pubkey hash, double blake2b hash of pubkey, used to shield the real
# pubkey in lock script.
# 1. pubkey, real pubkey used to identify token owner
# 2. signature, signature used to present ownership
if ARGV.length != 3
raise "Wrong number of arguments!"
end
def hex_to_bin(s)
if s.start_with?("0x")
s = s[2..-1]
end
[s].pack("H*")
end
pubkey_hash = hex_to_bin(ARGV[0])
pubkey = hex_to_bin(ARGV[1])
hash = Blake2b.new.update(Blake2b.new.update(pubkey).final).final
unless hash == pubkey_hash
raise "Invalid pubkey!"
end
tx = CKB.load_tx
blake2b = Blake2b.new
tx["inputs"].each_with_index do |input, i|
blake2b.update(input["hash"])
blake2b.update(input["index"].to_s)
end
tx["outputs"].each_with_index do |output, i|
blake2b.update(output["capacity"].to_s)
blake2b.update(CKB.load_script_hash(i, CKB::Source::OUTPUT, CKB::HashType::LOCK))
if hash = CKB.load_script_hash(i, CKB::Source::OUTPUT, CKB::HashType::TYPE)
blake2b.update(hash)
end
end
hash = blake2b.final
pubkey = ARGV[0]
signature = ARGV[1]
unless Secp256k1.verify(hex_to_bin(pubkey), hex_to_bin(signature), hash)
raise "Signature verification error!"
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.