From ca1a6b83136c57c3d29855cd2edfca2142c0f5a5 Mon Sep 17 00:00:00 2001 From: shaojunda Date: Fri, 27 Sep 2019 13:19:11 +0800 Subject: [PATCH] feat: change args type to string --- lib/ckb/types/script.rb | 10 +++++----- spec/ckb/types/script_spec.rb | 13 ++++++------- 2 files changed, 11 insertions(+), 12 deletions(-) diff --git a/lib/ckb/types/script.rb b/lib/ckb/types/script.rb index 242de549..9d229834 100644 --- a/lib/ckb/types/script.rb +++ b/lib/ckb/types/script.rb @@ -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 @@ -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 @@ -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 diff --git a/spec/ckb/types/script_spec.rb b/spec/ckb/types/script_spec.rb index ea7f91fa..be247a0c 100644 --- a/spec/ckb/types/script_spec.rb +++ b/spec/ckb/types/script_spec.rb @@ -7,7 +7,7 @@ ) CKB::Types::Script.new( code_hash: code_hash, - args: [] + args: "0x" ) end @@ -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( @@ -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) @@ -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(