Created
May 6, 2019 08:17
-
-
Save Mine77/c6dca07d306304a579e80f9184397065 to your computer and use it in GitHub Desktop.
ckb-ruby-example.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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