Skip to content

Commit

Permalink
feat: segwit
Browse files Browse the repository at this point in the history
  • Loading branch information
zhangsoledad committed Apr 1, 2019
1 parent 259a76e commit d9be331
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 41 deletions.
40 changes: 20 additions & 20 deletions fixed_amount_udt/genesis_unlock.rb
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
# This contract needs following required arguments:
# 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,
# This contracts provide 3 optional arguments:
# 0. signature, signature used to present ownership
# 1. type, SIGHASH type
# 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 following required arguments:
# 3. input hash, used to uniquely identify current cell
# 4. rate, used to tell how many tokens can 1 CKB capacity exchange.
# 5. lock hash, used to receive capacity in ICO phase
# 6. pubkey, used to identify token owner
# If they exist, we will do the proper signature verification way, if not
# we will check and perform an ICO step using rate.
if ARGV.length != 4 && ARGV.length != 6 && ARGV.length != 7
Expand Down Expand Up @@ -45,10 +45,10 @@ def blake2b_single_output(blake2b, output, output_index)

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

if sighash_type & SIGHASH_ANYONECANPAY != 0
# Only hash current input
Expand All @@ -69,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[6]
output_index = ARGV[6].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[6]
ARGV[6].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 @@ -89,8 +89,8 @@ def blake2b_single_output(blake2b, output, output_index)
end
hash = blake2b.final

pubkey = ARGV[3]
signature = ARGV[4]
pubkey = ARGV[6]
signature = ARGV[0]

unless Secp256k1.verify(hex_to_bin(pubkey), hex_to_bin(signature), hash)
raise "Signature verification error!"
Expand Down Expand Up @@ -129,7 +129,7 @@ def blake2b_single_output(blake2b, output, output_index)
# Finally, we test that in exchange for tokens, the sender has paid enough capacity
# in a new empty cell.
paid_output_index = tx["outputs"].length.times.find do |i|
CKB.load_script_hash(i, CKB::Source::OUTPUT, CKB::HashType::LOCK) == hex_to_bin(ARGV[2])
CKB.load_script_hash(i, CKB::Source::OUTPUT, CKB::HashType::LOCK) == hex_to_bin(ARGV[5])
end
unless paid_output_index
raise "Cannot find paid output!"
Expand All @@ -142,7 +142,7 @@ def blake2b_single_output(blake2b, output, output_index)
end
input_tokens = CKB::CellField.new(CKB::Source::CURRENT, 0, CKB::CellField::DATA).read(0, 8).unpack("Q<")[0]
output_tokens = CKB::CellField.new(CKB::Source::OUTPUT, current_output_index, CKB::CellField::DATA).read(0, 8).unpack("Q<")[0]
rate = ARGV[1].to_i
rate = ARGV[4].to_i
required_capacity = (input_tokens - output_tokens + rate - 1) / rate
paid_output = tx["outputs"][paid_output_index]
if paid_output["capacity"] != required_capacity
Expand Down
10 changes: 5 additions & 5 deletions udt/unlock.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
# This contract needs 4 required arguments:
# 0. token name, this is here so we can have different lock hash for
# 0. pubkey, used to identify token owner
# 1. signature, signature used to present ownership
# 2. 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
# One optional argument might be needed here:
# 4. output(s), this is only used for SIGHASH_SINGLE and SIGHASH_MULTIPLE types,
Expand Down Expand Up @@ -82,8 +82,8 @@ def blake2b_single_output(blake2b, output, output_index)
end
hash = blake2b.final

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

unless Secp256k1.verify(hex_to_bin(pubkey), hex_to_bin(signature), hash)
raise "Signature verification error!"
Expand Down
33 changes: 17 additions & 16 deletions udt/unlock_single_cell.rb
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
# This contract needs 2 required arguments:
# 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
# This contracts also 3 optional arguments:
# 2. signature, signature used to present ownership
# 3. type, SIGHASH type
# 4. output(s), this is only used for SIGHASH_SINGLE and SIGHASH_MULTIPLE types,
# This contracts provide 3 optional arguments:
# 0. signature, signature used to present ownership
# 1. type, SIGHASH type
# 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 required arguments:
# 3. 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.
# 4. pubkey, used to identify token owner
# If they exist, we will do the proper signature verification way, if not
# we will check for lock hash, and only accept transactions that have more
# tokens in the output cell than input cell so as to allow receiving tokens.
Expand Down Expand Up @@ -44,8 +45,8 @@ def blake2b_single_output(blake2b, output, output_index)
blake2b = Blake2b.new

if ARGV.length >= 4
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 @@ -66,15 +67,15 @@ 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]
raise "Not enough arguments" unless ARGV[2]
ARGV[4].split(",").each do |output_index|
output_index = output_index.to_i
if output = tx["outputs"][output_index]
Expand All @@ -86,8 +87,8 @@ def blake2b_single_output(blake2b, output, output_index)
end
hash = blake2b.final

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

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

0 comments on commit d9be331

Please sign in to comment.