Skip to content

Commit

Permalink
feat: change args type to string
Browse files Browse the repository at this point in the history
  • Loading branch information
shaojunda committed Sep 27, 2019
1 parent c59691a commit ca1a6b8
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 12 deletions.
10 changes: 5 additions & 5 deletions lib/ckb/types/script.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ class Script
attr_accessor :code_hash, :args, :hash_type

# @param code_hash [String]
# @param args [String[]]
# @param args [String]
# @param hash_type [String] data/type
def initialize(code_hash:, args:, hash_type: "data")
@code_hash = code_hash
Expand All @@ -18,7 +18,9 @@ def initialize(code_hash:, args:, hash_type: "data")
def calculate_bytesize
bytesize = 1
bytesize += Utils.hex_to_bin(@code_hash).bytesize if @code_hash
(@args || []).map { |arg| Utils.hex_to_bin(arg).bytesize }.reduce(bytesize, &:+)
bytesize += Utils.hex_to_bin(args).bytesize if args

bytesize
end

def to_h
Expand Down Expand Up @@ -50,9 +52,7 @@ def compute_hash
def self.generate_lock(target_pubkey_blake160, secp_cell_type_hash, hash_type = "type")
new(
code_hash: secp_cell_type_hash,
args: [
target_pubkey_blake160
],
args: target_pubkey_blake160,
hash_type: hash_type
)
end
Expand Down
13 changes: 6 additions & 7 deletions spec/ckb/types/script_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
)
CKB::Types::Script.new(
code_hash: code_hash,
args: []
args: "0x"
)
end

Expand All @@ -27,9 +27,9 @@
end

it "should build correct hash when there is only one arg" do
CKB::Types::Script.new(
script = CKB::Types::Script.new(
code_hash: code_hash,
args: ["0x3954acece65096bfa81258983ddb83915fc56bd8"]
args: "0x3954acece65096bfa81258983ddb83915fc56bd8"
)

expect(
Expand All @@ -38,11 +38,10 @@
end

it "should build correct hash when args more than one" do
CKB::Types::Script.new(
script = CKB::Types::Script.new(
code_hash: code_hash,
args: ["0x3954acece65096bfa81258983ddb83915fc56bd8", "0x3954acece65096bfa81258983ddb83915fc56bd83232323"]
args: "0x3954acece65096bfa81258983ddb83915fc56bd83954acece65096bfa81258983ddb83915fc56bd8"
)

expect(
script.compute_hash
).to eq api.compute_script_hash(script)
Expand All @@ -51,7 +50,7 @@

context "calculate bytesize" do
let(:code_hash) { "0x9e3b3557f11b2b3532ce352bfe8017e9fd11d154c4c7f9b7aaaa1e621b539a08" }
let(:args) { ["0x36c329ed630d6ce750712a477543672adab57f4c"] }
let(:args) { "0x36c329ed630d6ce750712a477543672adab57f4c" }

let(:lock_script) do
CKB::Types::Script.new(
Expand Down

0 comments on commit ca1a6b8

Please sign in to comment.