Skip to content
This repository has been archived by the owner on Sep 13, 2022. It is now read-only.

Commit

Permalink
fix: rename contract to type [skip ci]
Browse files Browse the repository at this point in the history
  • Loading branch information
u2 committed Mar 7, 2019
1 parent 0a3cc74 commit 4900266
Showing 1 changed file with 2 additions and 2 deletions.
4 changes: 2 additions & 2 deletions docs/how-to-write-contracts.md
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ To build `mruby-contracts`, first follow the [setup steps](https://github.com/ne
"version": 0,
"reference": "0x12b464bcab8f55822501cdb91ea35ea707d72ec970363972388a0c49b94d377c",
"signed_args": [
"# This contract needs 1 signed arguments:\n# 0. pubkey, used to identify token owner\n# This contracts also accepts 2 required unsigned arguments and 1\n# optional unsigned argument:\n# 1. signature, signature used to present ownership\n# 2. type, SIGHASH type\n# 3. output(s), this is only used for SIGHASH_SINGLE and SIGHASH_MULTIPLE types,\n# for SIGHASH_SINGLE, it stores an integer denoting the index of output to be\n# signed; for SIGHASH_MULTIPLE, it stores a string of `,` separated array denoting\n# outputs to sign\nif ARGV.length != 3 && ARGV.length != 4\n raise \"Wrong number of arguments!\"\nend\n\nSIGHASH_ALL = 0x1\nSIGHASH_NONE = 0x2\nSIGHASH_SINGLE = 0x3\nSIGHASH_MULTIPLE = 0x4\nSIGHASH_ANYONECANPAY = 0x80\n\ndef hex_to_bin(s)\n if s.start_with?(\"0x\")\n s = s[2..-1]\n end\n s.each_char.each_slice(2).map(&:join).map(&:hex).map(&:chr).join\nend\n\n\ntx = CKB.load_tx\nsha3 = Sha3.new\n\nsha3.update(ARGV[2])\nsighash_type = ARGV[2].to_i\n\nif sighash_type & SIGHASH_ANYONECANPAY != 0\n # Only hash current input\n outpoint = CKB.load_input_out_point(0, CKB::Source::CURRENT)\n sha3.update(outpoint[\"hash\"])\n sha3.update(outpoint[\"index\"].to_s)\n sha3.update(CKB::CellField.new(CKB::Source::CURRENT, 0, CKB::CellField::LOCK_HASH).readall)\nelse\n # Hash all inputs\n tx[\"inputs\"].each_with_index do |input, i|\n sha3.update(input[\"hash\"])\n sha3.update(input[\"index\"].to_s)\n sha3.update(CKB.load_script_hash(i, CKB::Source::INPUT, CKB::Category::LOCK))\n end\nend\n\ncase sighash_type & (~SIGHASH_ANYONECANPAY)\nwhen SIGHASH_ALL\n tx[\"outputs\"].each_with_index do |output, i|\n sha3.update(output[\"capacity\"].to_s)\n sha3.update(output[\"lock\"])\n if hash = CKB.load_script_hash(i, CKB::Source::OUTPUT, CKB::Category::CONTRACT)\n sha3.update(hash)\n end\n end\nwhen SIGHASH_SINGLE\n raise \"Not enough arguments\" unless ARGV[3]\n output_index = ARGV[3].to_i\n output = tx[\"outputs\"][output_index]\n sha3.update(output[\"capacity\"].to_s)\n sha3.update(output[\"lock\"])\n if hash = CKB.load_script_hash(output_index, CKB::Source::OUTPUT, CKB::Category::CONTRACT)\n sha3.update(hash)\n end\nwhen SIGHASH_MULTIPLE\n raise \"Not enough arguments\" unless ARGV[3]\n ARGV[3].split(\",\").each do |output_index|\n output_index = output_index.to_i\n output = tx[\"outputs\"][output_index]\n sha3.update(output[\"capacity\"].to_s)\n sha3.update(output[\"lock\"])\n if hash = CKB.load_script_hash(output_index, CKB::Source::OUTPUT, CKB::Category::CONTRACT)\n sha3.update(hash)\n end\n end\nend\nhash = sha3.final\n\npubkey = ARGV[0]\nsignature = ARGV[1]\n\nunless Secp256k1.verify(hex_to_bin(pubkey), hex_to_bin(signature), hash)\n raise \"Signature verification error!\"\nend\n",
"# This contract needs 1 signed arguments:\n# 0. pubkey, used to identify token owner\n# This contracts also accepts 2 required unsigned arguments and 1\n# optional unsigned argument:\n# 1. signature, signature used to present ownership\n# 2. type, SIGHASH type\n# 3. output(s), this is only used for SIGHASH_SINGLE and SIGHASH_MULTIPLE types,\n# for SIGHASH_SINGLE, it stores an integer denoting the index of output to be\n# signed; for SIGHASH_MULTIPLE, it stores a string of `,` separated array denoting\n# outputs to sign\nif ARGV.length != 3 && ARGV.length != 4\n raise \"Wrong number of arguments!\"\nend\n\nSIGHASH_ALL = 0x1\nSIGHASH_NONE = 0x2\nSIGHASH_SINGLE = 0x3\nSIGHASH_MULTIPLE = 0x4\nSIGHASH_ANYONECANPAY = 0x80\n\ndef hex_to_bin(s)\n if s.start_with?(\"0x\")\n s = s[2..-1]\n end\n s.each_char.each_slice(2).map(&:join).map(&:hex).map(&:chr).join\nend\n\n\ntx = CKB.load_tx\nsha3 = Sha3.new\n\nsha3.update(ARGV[2])\nsighash_type = ARGV[2].to_i\n\nif sighash_type & SIGHASH_ANYONECANPAY != 0\n # Only hash current input\n outpoint = CKB.load_input_out_point(0, CKB::Source::CURRENT)\n sha3.update(outpoint[\"hash\"])\n sha3.update(outpoint[\"index\"].to_s)\n sha3.update(CKB::CellField.new(CKB::Source::CURRENT, 0, CKB::CellField::LOCK_HASH).readall)\nelse\n # Hash all inputs\n tx[\"inputs\"].each_with_index do |input, i|\n sha3.update(input[\"hash\"])\n sha3.update(input[\"index\"].to_s)\n sha3.update(CKB.load_script_hash(i, CKB::Source::INPUT, CKB::Category::LOCK))\n end\nend\n\ncase sighash_type & (~SIGHASH_ANYONECANPAY)\nwhen SIGHASH_ALL\n tx[\"outputs\"].each_with_index do |output, i|\n sha3.update(output[\"capacity\"].to_s)\n sha3.update(output[\"lock\"])\n if hash = CKB.load_script_hash(i, CKB::Source::OUTPUT, CKB::Category::TYPE)\n sha3.update(hash)\n end\n end\nwhen SIGHASH_SINGLE\n raise \"Not enough arguments\" unless ARGV[3]\n output_index = ARGV[3].to_i\n output = tx[\"outputs\"][output_index]\n sha3.update(output[\"capacity\"].to_s)\n sha3.update(output[\"lock\"])\n if hash = CKB.load_script_hash(output_index, CKB::Source::OUTPUT, CKB::Category::TYPE)\n sha3.update(hash)\n end\nwhen SIGHASH_MULTIPLE\n raise \"Not enough arguments\" unless ARGV[3]\n ARGV[3].split(\",\").each do |output_index|\n output_index = output_index.to_i\n output = tx[\"outputs\"][output_index]\n sha3.update(output[\"capacity\"].to_s)\n sha3.update(output[\"lock\"])\n if hash = CKB.load_script_hash(output_index, CKB::Source::OUTPUT, CKB::Category::TYPE)\n sha3.update(hash)\n end\n end\nend\nhash = sha3.final\n\npubkey = ARGV[0]\nsignature = ARGV[1]\n\nunless Secp256k1.verify(hex_to_bin(pubkey), hex_to_bin(signature), hash)\n raise \"Signature verification error!\"\nend\n",
"024a501efd328e062c8675f2365970728c859c592beeefd6be8ead3d901330bc01"
],
"args": [
Expand Down Expand Up @@ -182,7 +182,7 @@ Following code can be used to load script hash:
CKB.load_script_hash(1, CKB::Source::INPUT, CKB::Category::LOCK)
# Load cell output 2's type script hash, note that type script is optional, so the
# returned value here could be nil
CKB.load_script_hash(2, CKB::Source::OUTPUT, CKB::Category::CONTRACT)
CKB.load_script_hash(2, CKB::Source::OUTPUT, CKB::Category::TYPE)
# Load current cell's lock hash
CKB.load_script_hash(0, CKB::Source::CURRENT, CKB::Category::LOCK)
```
Expand Down

0 comments on commit 4900266

Please sign in to comment.