Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] feat: segwit #10

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion bitcoin/sighash_all.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# This contract needs 2 arguments:
# This contract needs 2 witnesses:
# 0. pubkey, used to identify token owner
# 1. signature, signature used to present ownership
if ARGV.length != 2
Expand Down
2 changes: 1 addition & 1 deletion bitcoin/sighash_all_anyonecanpay.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# This contract needs 2 arguments:
# This contract needs 2 witnesses:
# 0. pubkey, used to identify token owner
# 1. signature, signature used to present ownership
if ARGV.length != 2
Expand Down
16 changes: 9 additions & 7 deletions bitcoin/sighash_multiple.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
# This contract needs 3 arguments:
# 0. pubkey, used to identify token owner
# 1. signature, signature used to present ownership
# 2. string of `,` separated array denoting outputs to sign.
# This contract needs 1 arguments:
# 0. string of `,` separated array denoting outputs to sign.
# It's up to transaction assembler to arrange outputs, this script
# only cares that correct data are signed.
#
# This contract needs 2 witnesses:
# 1. pubkey, used to identify token owner
# 2. signature, signature used to present ownership
if ARGV.length != 3
raise "Wrong number of arguments!"
end
Expand All @@ -24,7 +26,7 @@ def hex_to_bin(s)
blake2b.update(input["hash"])
blake2b.update(input["index"].to_s)
end
ARGV[2].split(",").each do |output_index|
ARGV[0].split(",").each do |output_index|
output_index = output_index.to_i
if output = tx["outputs"][output_index]
blake2b.update(output["capacity"].to_s)
Expand All @@ -39,8 +41,8 @@ def hex_to_bin(s)

hash = blake2b.final

pubkey = ARGV[0]
signature = ARGV[1]
pubkey = ARGV[1]
signature = ARGV[2]

unless Secp256k1.verify(hex_to_bin(pubkey), hex_to_bin(signature), hash)
raise "Signature verification error!"
Expand Down
16 changes: 9 additions & 7 deletions bitcoin/sighash_multiple_anyonecanpay.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
# This contract needs 3 arguments:
# 0. pubkey, used to identify token owner
# 1. signature, signature used to present ownership
# 2. string of `,` separated array denoting outputs to sign.
# This contract needs 1 arguments:
# 0. string of `,` separated array denoting outputs to sign.
# It's up to transaction assembler to arrange outputs, this script
# only cares that correct data are signed.
#
# This contract needs 2 witnesses:
# 1. pubkey, used to identify token owner
# 2. signature, signature used to present ownership
if ARGV.length != 3
raise "Wrong number of arguments!"
end
Expand All @@ -21,7 +23,7 @@ def hex_to_bin(s)
out_point = CKB.load_input_out_point(0, CKB::Source::CURRENT)
blake2b.update(out_point["hash"])
blake2b.update(out_point["index"].to_s)
ARGV[2].split(",").each do |output_index|
ARGV[0].split(",").each do |output_index|
output_index = output_index.to_i
output = tx["outputs"][output_index]
blake2b.update(output["capacity"].to_s)
Expand All @@ -33,8 +35,8 @@ def hex_to_bin(s)

hash = blake2b.final

pubkey = ARGV[0]
signature = ARGV[1]
pubkey = ARGV[1]
signature = ARGV[2]

unless Secp256k1.verify(hex_to_bin(pubkey), hex_to_bin(signature), hash)
raise "Signature verification error!"
Expand Down
2 changes: 1 addition & 1 deletion bitcoin/sighash_none.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# This contract needs 2 arguments:
# This contract needs 2 witnesses:
# 0. pubkey, used to identify token owner
# 1. signature, signature used to present ownership
if ARGV.length != 2
Expand Down
2 changes: 1 addition & 1 deletion bitcoin/sighash_none_anyonecanpay.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# This contract needs 2 arguments:
# This contract needs 2 witnesses:
# 0. pubkey, used to identify token owner
# 1. signature, signature used to present ownership
if ARGV.length != 2
Expand Down
16 changes: 9 additions & 7 deletions bitcoin/sighash_single.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
# This contract needs 3 arguments:
# 0. pubkey, used to identify token owner
# 1. signature, signature used to present ownership
# 2. index, an integer denoting the index of output to be signed.
# This contract needs 1 arguments:
# 0. index, an integer denoting the index of output to be signed.
# It's up to transaction assembler to arrange outputs, this script
# only cares that correct data are signed.
#
# This contract needs 2 witnesses:
# 1. pubkey, used to identify token owner
# 2. signature, signature used to present ownership
if ARGV.length != 3
raise "Wrong number of arguments!"
end
Expand All @@ -24,7 +26,7 @@ def hex_to_bin(s)
blake2b.update(input["hash"])
blake2b.update(input["index"].to_s)
end
output_index = ARGV[2].to_i
output_index = ARGV[0].to_i
if output = tx["outputs"][output_index]
blake2b.update(output["capacity"].to_s)
blake2b.update(CKB.load_script_hash(output_index, CKB::Source::OUTPUT, CKB::HashType::LOCK))
Expand All @@ -37,8 +39,8 @@ def hex_to_bin(s)

hash = blake2b.final

pubkey = ARGV[0]
signature = ARGV[1]
pubkey = ARGV[1]
signature = ARGV[2]

unless Secp256k1.verify(hex_to_bin(pubkey), hex_to_bin(signature), hash)
raise "Signature verification error!"
Expand Down
16 changes: 9 additions & 7 deletions bitcoin/sighash_single_anyonecanpay.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
# This contract needs 3 arguments:
# 0. pubkey, used to identify token owner
# 1. signature, signature used to present ownership
# 2. index, an integer denoting the index of output to be signed.
# This contract needs 1 arguments:
# 0. index, an integer denoting the index of output to be signed.
# It's up to transaction assembler to arrange outputs, this script
# only cares that correct data are signed.
#
# This contract needs 2 witnesses:
# 1. pubkey, used to identify token owner
# 2. signature, signature used to present ownership
if ARGV.length != 3
raise "Wrong number of arguments!"
end
Expand All @@ -23,7 +25,7 @@ def hex_to_bin(s)
out_point = CKB.load_input_out_point(0, CKB::Source::CURRENT)
blake2b.update(out_point["hash"])
blake2b.update(out_point["index"].to_s)
output_index = ARGV[2].to_i
output_index = ARGV[0].to_i
if output = tx["outputs"][output_index]
blake2b.update(output["capacity"].to_s)
blake2b.update(CKB.load_script_hash(output_index, CKB::Source::OUTPUT, CKB::HashType::LOCK))
Expand All @@ -36,8 +38,8 @@ def hex_to_bin(s)

hash = blake2b.final

pubkey = ARGV[0]
signature = ARGV[1]
pubkey = ARGV[1]
signature = ARGV[2]

unless Secp256k1.verify(hex_to_bin(pubkey), hex_to_bin(signature), hash)
raise "Signature verification error!"
Expand Down
26 changes: 14 additions & 12 deletions fixed_amount_udt/genesis_unlock.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,19 @@
# 0. input hash, used to uniquely identify current cell
# 1. rate, used to tell how many tokens can 1 CKB capacity exchange.
# 2. lock hash, used to receive capacity in ICO phase
# 3. pubkey, used to identify token owner
#
# This contracts also 3 optional arguments:
# 4. signature, signature used to present ownership
# 5. type, SIGHASH type
# 6. output(s), this is only used for SIGHASH_SINGLE and SIGHASH_MULTIPLE types,
# 3. type, SIGHASH type
# 4. output(s), this is only used for SIGHASH_SINGLE and SIGHASH_MULTIPLE types,
# for SIGHASH_SINGLE, it stores an integer denoting the index of output to be
# signed; for SIGHASH_MULTIPLE, it stores a string of `,` separated array denoting
# outputs to sign.
# If they exist, we will do the proper signature verification way, if not
# we will check and perform an ICO step using rate.
#
# Witnesses:
# 5. pubkey, used to identify token owner
# 6. signature, signature used to present ownership
if ARGV.length != 4 && ARGV.length != 6 && ARGV.length != 7
raise "Wrong number of arguments!"
end
Expand Down Expand Up @@ -45,10 +47,10 @@ def blake2b_single_output(blake2b, output, output_index)

if ARGV.length >= 6
blake2b = Blake2b.new
ARGV.drop(5).each do |argument|
ARGV[3..4].each do |argument|
blake2b.update(argument)
end
sighash_type = ARGV[5].to_i
sighash_type = ARGV[3].to_i

if sighash_type & SIGHASH_ANYONECANPAY != 0
# Only hash current input
Expand All @@ -69,16 +71,16 @@ def blake2b_single_output(blake2b, output, output_index)
blake2b_single_output(blake2b, output, i)
end
when SIGHASH_SINGLE
raise "Not enough arguments" unless ARGV[6]
output_index = ARGV[6].to_i
raise "Not enough arguments" unless ARGV[4]
output_index = ARGV[4].to_i
if output = tx["outputs"][output_index]
blake2b_single_output(blake2b, output, output_index)
else
raise OUTPUT_INDEX_ERR
end
when SIGHASH_MULTIPLE
raise "Not enough arguments" unless ARGV[6]
ARGV[6].split(",").each do |output_index|
raise "Not enough arguments" unless ARGV[4]
ARGV[4].split(",").each do |output_index|
output_index = output_index.to_i
if output = tx["outputs"][output_index]
blake2b_single_output(blake2b, output, output_index)
Expand All @@ -89,8 +91,8 @@ def blake2b_single_output(blake2b, output, output_index)
end
hash = blake2b.final

pubkey = ARGV[3]
signature = ARGV[4]
pubkey = ARGV[-2]
signature = ARGV[-1]

unless Secp256k1.verify(hex_to_bin(pubkey), hex_to_bin(signature), hash)
raise "Signature verification error!"
Expand Down
26 changes: 14 additions & 12 deletions secp256k1_blake2b_lock.rb
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
# This contract needs 4 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
# 3. type, SIGHASH type
# 1. type, SIGHASH type
# One optional argument is supported here:
# 4. output(s), this is only used for SIGHASH_SINGLE and SIGHASH_MULTIPLE types,
# 2. output(s), this is only used for SIGHASH_SINGLE and SIGHASH_MULTIPLE types,
# for SIGHASH_SINGLE, it stores an integer denoting the index of output to be
# signed; for SIGHASH_MULTIPLE, it stores a string of `,` separated array denoting
# outputs to sign
#
# This contract needs 2 witnesses:
# 3. pubkey, real pubkey used to identify token owner
# 4. signature, signature used to present ownership
if ARGV.length != 4 && ARGV.length != 5
raise "Wrong number of arguments!"
end
Expand Down Expand Up @@ -37,16 +39,16 @@ def blake2b_single_output(blake2b, output, output_index)
OUTPUT_INDEX_ERR = "Output index error!".freeze

pubkey_hash = hex_to_bin(ARGV[0])
pubkey = hex_to_bin(ARGV[1])
pubkey = hex_to_bin(ARGV[-2])
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
blake2b.update(ARGV[3])
sighash_type = ARGV[3].to_i
blake2b.update(ARGV[1])
sighash_type = ARGV[1].to_i

if sighash_type & SIGHASH_ANYONECANPAY != 0
# Only hash current input
Expand All @@ -67,16 +69,16 @@ def blake2b_single_output(blake2b, output, output_index)
blake2b_single_output(blake2b, output, i)
end
when SIGHASH_SINGLE
raise "Not enough arguments" unless ARGV[4]
output_index = ARGV[4].to_i
raise "Not enough arguments" unless ARGV[2]
output_index = ARGV[2].to_i
if output = tx["outputs"][output_index]
blake2b_single_output(blake2b, output, output_index)
else
raise OUTPUT_INDEX_ERR
end
when SIGHASH_MULTIPLE
raise "Not enough arguments" unless ARGV[4]
ARGV[4].split(",").each do |output_index|
raise "Not enough arguments" unless ARGV[2]
ARGV[2].split(",").each do |output_index|
output_index = output_index.to_i
if output = tx["outputs"][output_index]
blake2b_single_output(blake2b, output, output_index)
Expand All @@ -87,6 +89,6 @@ def blake2b_single_output(blake2b, output, output_index)
end
hash = blake2b.final

unless Secp256k1.verify(pubkey, hex_to_bin(ARGV[2]), hash)
unless Secp256k1.verify(pubkey, hex_to_bin(ARGV[-1]), hash)
raise "Signature verification error!"
end
26 changes: 14 additions & 12 deletions udt/unlock.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,16 @@
# 0. token name, this is here so we can have different lock hash for
# different token for ease of querying. In the actual contract this is
# not used.
# 1. pubkey, used to identify token owner
# 2. signature, signature used to present ownership
# 3. type, SIGHASH type
# 1. type, SIGHASH type
# One optional argument might be needed here:
# 4. output(s), this is only used for SIGHASH_SINGLE and SIGHASH_MULTIPLE types,
# 2. output(s), this is only used for SIGHASH_SINGLE and SIGHASH_MULTIPLE types,
# for SIGHASH_SINGLE, it stores an integer denoting the index of output to be
# signed; for SIGHASH_MULTIPLE, it stores a string of `,` separated array denoting
# outputs to sign
#
# This contract needs 2 witnesses:
# 3. pubkey, used to identify token owner
# 4. signature, signature used to present ownership
if ARGV.length != 4 && ARGV.length != 5
raise "Wrong number of arguments!"
end
Expand Down Expand Up @@ -40,8 +42,8 @@ def blake2b_single_output(blake2b, output, output_index)
tx = CKB.load_tx
blake2b = Blake2b.new

blake2b.update(ARGV[3])
sighash_type = ARGV[3].to_i
blake2b.update(ARGV[1])
sighash_type = ARGV[1].to_i

if sighash_type & SIGHASH_ANYONECANPAY != 0
# Only hash current input
Expand All @@ -62,16 +64,16 @@ def blake2b_single_output(blake2b, output, output_index)
blake2b_single_output(blake2b, output, i)
end
when SIGHASH_SINGLE
raise "Not enough arguments" unless ARGV[4]
output_index = ARGV[4].to_i
raise "Not enough arguments" unless ARGV[2]
output_index = ARGV[2].to_i
if output = tx["outputs"][output_index]
blake2b_single_output(blake2b, output, output_index)
else
raise OUTPUT_INDEX_ERR
end
when SIGHASH_MULTIPLE
raise "Not enough arguments" unless ARGV[4]
ARGV[4].split(",").each do |output_index|
raise "Not enough arguments" unless ARGV[2]
ARGV[2].split(",").each do |output_index|
output_index = output_index.to_i
if output = tx["outputs"][output_index]
blake2b_single_output(blake2b, output, output_index)
Expand All @@ -82,8 +84,8 @@ def blake2b_single_output(blake2b, output, output_index)
end
hash = blake2b.final

pubkey = ARGV[1]
signature = ARGV[2]
pubkey = ARGV[-2]
signature = ARGV[-1]

unless Secp256k1.verify(hex_to_bin(pubkey), hex_to_bin(signature), hash)
raise "Signature verification error!"
Expand Down
Loading