Skip to content

Commit

Permalink
feat: Add hash type per CKB changes
Browse files Browse the repository at this point in the history
  • Loading branch information
xxuejie committed Jul 13, 2019
1 parent 9120386 commit aa88f93
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 6 deletions.
2 changes: 1 addition & 1 deletion lib/ckb/api.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ def initialize(host: CKB::RPC::DEFAULT_URL, mode: MODE::TESTNET)
@rpc = CKB::RPC.new(host: host)
if mode == MODE::TESTNET
# Testnet system script code_hash
expected_code_hash = "0x94334bdda40b69bae067d84937aa6bbccf8acd0df6626d4b9ac70d4612a11933"
expected_code_hash = "0x54811ce986d5c3e57eaafab22cdd080e32209e39590e204a99b32935f835a13c"
# For testnet chain, we can assume the second cell of the first transaction
# in the genesis block contains default lock script we can use here.
system_cell_transaction = genesis_block.transactions.first
Expand Down
19 changes: 15 additions & 4 deletions lib/ckb/types/script.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,14 @@
module CKB
module Types
class Script
attr_reader :code_hash, :args
attr_reader :code_hash, :args, :hash_type

# @param code_hash [String]
# @param args [String[]]
def initialize(code_hash:, args:)
def initialize(code_hash:, args:, hash_type: "Data")
@code_hash = code_hash
@args = args
@hash_type = hash_type
end

# @return [Integer] Byte
Expand All @@ -22,7 +23,8 @@ def calculate_bytesize
def to_h
{
code_hash: @code_hash,
args: @args
args: @args,
hash_type: @hash_type
}
end

Expand All @@ -31,13 +33,22 @@ def self.from_h(hash)

new(
code_hash: hash[:code_hash],
args: hash[:args]
args: hash[:args],
hash_type: hash[:hash_type]
)
end

def to_hash
blake2b = CKB::Blake2b.new
blake2b << Utils.hex_to_bin(@code_hash) if @code_hash
blake2b << case @hash_type
when "Data"
"\x0"
when "Type"
"\x1"
else
raise "Invalid hash type!"
end
args = @args || []
args.each do |arg|
blake2b << Utils.hex_to_bin(arg)
Expand Down
2 changes: 1 addition & 1 deletion spec/ckb/types/script_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
)
end

let(:code_hash) { "0x86f4f705a8e85905b1c73b84b1f3a3cf6dbfdd7eb8e47bfd1c489681ee2762cb" }
let(:code_hash) { "0xc00073200d2b2f4ad816a8d04bb2431ce0d3ebd49141b086eda4ab4e06bc3a21" }

it "to_hash" do
expect(
Expand Down

0 comments on commit aa88f93

Please sign in to comment.