Skip to content

Commit

Permalink
feat: use type hash
Browse files Browse the repository at this point in the history
  • Loading branch information
shaojunda committed Aug 23, 2019
1 parent 32429fa commit 5fa7967
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 34 deletions.
48 changes: 17 additions & 31 deletions lib/ckb/api.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@
module CKB
class API
attr_reader :rpc
attr_reader :system_script_out_point
attr_reader :system_script_group_out_point
attr_reader :system_script_code_hash
attr_reader :secp_group_out_point
attr_reader :secp_cell_type_hash
attr_reader :secp_cell_code_hash
attr_reader :dao_out_point
attr_reader :dao_code_hash

Expand All @@ -23,23 +23,21 @@ def initialize(host: CKB::RPC::DEFAULT_URL, mode: MODE::TESTNET)
# 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

system_scipt_group_transaction = genesis_block.transactions[1]
out_point = Types::OutPoint.new(
tx_hash: system_cell_transaction.hash,
index: "1"
)
cell_data = CKB::Utils.hex_to_bin(system_cell_transaction.outputs_data[1])
code_hash = CKB::Blake2b.hexdigest(cell_data)

raise "System script code_hash error!" unless code_hash == expected_code_hash

set_system_script_cell(out_point, code_hash)
group_out_point = Types::OutPoint.new(
tx_hash: system_scipt_group_transaction.hash,
@secp_cell_code_hash = code_hash

secp_group_cell_transaction = genesis_block.transactions[1]
secp_group_out_point = Types::OutPoint.new(
tx_hash: secp_group_cell_transaction.hash,
index: "0"
)
set_system_script_group_cell(group_out_point)

secp_cell_type_hash = rpc.compute_script_hash(system_cell_transaction.outputs[1].type.to_h)
set_secp_group_dep(secp_group_out_point, secp_cell_type_hash)

dao_out_point = Types::OutPoint.new(
tx_hash: system_cell_transaction.hash,
Expand All @@ -48,34 +46,22 @@ def initialize(host: CKB::RPC::DEFAULT_URL, mode: MODE::TESTNET)
dao_cell_data = CKB::Utils.hex_to_bin(system_cell_transaction.outputs_data[2])
dao_code_hash = CKB::Blake2b.hexdigest(dao_cell_data)

set_dao_cell(dao_out_point, dao_code_hash)
set_dao_dep(dao_out_point, dao_code_hash)
end
end

# @param out_point [CKB::Types::OutPoint]
# @param code_hash [String] "0x..."
def set_system_script_cell(out_point, code_hash)
@system_script_out_point = out_point
@system_script_code_hash = code_hash
# @param secp_cell_type_hash [String] 0x...
def set_secp_group_dep(out_point, secp_cell_type_hash)
@secp_group_out_point = out_point
@secp_cell_type_hash = secp_cell_type_hash
end

# @param out_point [CKB::Types::OutPoint]
def set_system_script_group_cell(out_point)
@system_script_group_out_point = out_point
end

def set_dao_cell(out_point, code_hash)
def set_dao_dep(out_point, code_hash)
@dao_out_point = out_point
@dao_code_hash = code_hash
end

def system_script_cell
{
out_point: system_script_out_point,
code_hash: system_script_code_hash
}
end

def genesis_block
@genesis_block ||= get_block_by_number("0")
end
Expand Down
6 changes: 3 additions & 3 deletions lib/ckb/wallet.rb
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ def generate_tx(target_address, capacity, data = "0x", key: nil, fee: 0)
data: data,
lock: Types::Script.generate_lock(
addr.parse(target_address),
api.system_script_code_hash
api.secp_cell_type_hash
)
)

Expand Down Expand Up @@ -127,7 +127,7 @@ def deposit_to_dao(capacity, key: nil)

output = Types::Output.new(
capacity: capacity,
lock: Types::Script.generate_lock(addr.blake160, api.system_script_code_hash),
lock: Types::Script.generate_lock(addr.blake160, api.secp_cell_type_hash),
type: Types::Script.new(
code_hash: api.dao_code_hash,
args: []
Expand Down Expand Up @@ -290,7 +290,7 @@ def gather_inputs(capacity, min_capacity, min_change_capacity, fee)
def lock
Types::Script.generate_lock(
addr.blake160,
api.system_script_code_hash
api.secp_cell_type_hash
)
end

Expand Down

0 comments on commit 5fa7967

Please sign in to comment.