Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: update SDK based on new verification model #105

Merged
merged 4 commits into from
Jun 4, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 = "0x9e3b3557f11b2b3532ce352bfe8017e9fd11d154c4c7f9b7aaaa1e621b539a08"
expected_code_hash = "0xa4a3d87cd69733562865ddfaf12dd44cf4cd396c4128ff1a5839c2fe48b2a1a1"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems to be changing again on CKB rc/v0.14.0. Confirming that now.

https://github.com/nervosnetwork/ckb/pull/941/files#diff-346487b1ea6102562958846069e090deR9

# 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
12 changes: 6 additions & 6 deletions lib/ckb/types/input.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,20 @@
module CKB
module Types
class Input
attr_reader :args, :previous_output, :since
attr_reader :block_number, :previous_output, :since

# @param args [String[]] ["0x..."]
# @param previous_output [CKB::Types::OutPoint]
# @param since [String]
def initialize(args:, previous_output:, since: "0")
@args = args
# @param block_number [String]
def initialize(previous_output:, since: "0", block_number: "0")
@previous_output = previous_output
@since = since.to_s
@block_number = block_number.to_s
end

def to_h
{
args: @args,
block_number: block_number,
previous_output: @previous_output.to_h,
since: since
}
Expand All @@ -26,7 +26,7 @@ def self.from_h(hash)
return if hash.nil?

new(
args: hash[:args],
block_number: hash[:block_number],
previous_output: OutPoint.from_h(hash[:previous_output]),
since: hash[:since]
)
Expand Down
17 changes: 12 additions & 5 deletions lib/ckb/types/transaction.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,17 @@ def initialize(
# @param key [CKB::Key]
# @param tx_hash [String] 0x...
def sign(key, tx_hash)
signature_hex_var = key.sign(tx_hash)
signature_size = Utils.hex_to_bin(signature_hex_var).size
data = [key.pubkey, signature_hex_var, Utils.bin_to_hex([signature_size].pack("Q<"))]
witnesses = inputs.size.times.map do
raise "Invalid number of witnesses!" if witnesses.length < inputs.length

signed_witnesses = witnesses.map do |witness|
old_data = witness.data || []
blake2b = CKB::Blake2b.new
blake2b.update(Utils.hex_to_bin(tx_hash))
old_data.each do |datum|
blake2b.update(Utils.hex_to_bin(datum))
end
message = Utils.bin_to_hex(blake2b.digest)
data = [key.pubkey, key.sign(message)] + old_data
Types::Witness.from_h(data: data)
end

Expand All @@ -43,7 +50,7 @@ def sign(key, tx_hash)
deps: deps,
inputs: inputs,
outputs: outputs,
witnesses: witnesses
witnesses: signed_witnesses
)
end

Expand Down
21 changes: 13 additions & 8 deletions lib/ckb/wallet.rb
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,8 @@ def generate_tx(target_address, capacity, data = "0x")
version: 0,
deps: [api.system_script_out_point],
inputs: i.inputs,
outputs: outputs
outputs: outputs,
witnesses: i.witnesses
)
tx_hash = api.compute_transaction_hash(tx)

Expand Down Expand Up @@ -116,7 +117,8 @@ def deposit_to_dao(capacity)
version: 0,
deps: [api.system_script_out_point],
inputs: i.inputs,
outputs: outputs
outputs: outputs,
witnesses: i.witnesses,
)
tx_hash = api.compute_transaction_hash(tx)
send_transaction(tx.sign(key, tx_hash))
Expand Down Expand Up @@ -156,11 +158,15 @@ def generate_withdraw_from_dao_transaction(cell_out_point)
version: 0,
deps: [{block_hash: current_block.hash}],
inputs: [
Types::Input.new(args: [current_block.hash], previous_output: new_cell_out_point, since: since),
Types::Input.new(args: [], previous_output: DAO_ISSUING_OUT_POINT)
Types::Input.new(previous_output: new_cell_out_point, since: since),
Types::Input.new(previous_output: DAO_ISSUING_OUT_POINT)
],
outputs: [
Types::Output.new(capacity: output_capacity, lock: lock)
],
witnesses: [
Types::Witness.new(data: [current_block.hash]),
Types::Witness.new(data: []),
]
)
tx_hash = api.compute_transaction_hash(tx)
Expand Down Expand Up @@ -200,15 +206,14 @@ def gather_inputs(capacity, min_capacity, min_charge_capacity)

input_capacities = 0
inputs = []
pubkeys = []
witnesses = []
get_unspent_cells.each do |cell|
input = Types::Input.new(
previous_output: cell.out_point,
args: [],
since: "0"
)
pubkeys << pubkey
inputs << input
witnesses << Types::Witness.new(data: [])
input_capacities += cell.capacity.to_i

diff = input_capacities - capacity
Expand All @@ -217,7 +222,7 @@ def gather_inputs(capacity, min_capacity, min_charge_capacity)

raise "Capacity not enough!" if input_capacities < capacity

OpenStruct.new(inputs: inputs, capacities: input_capacities, pubkeys: pubkeys)
OpenStruct.new(inputs: inputs, capacities: input_capacities, witnesses: witnesses)
end

def pubkey
Expand Down
9 changes: 6 additions & 3 deletions spec/ckb/types/transaction_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,11 @@
"data": "0x"
}
],
"witnesses": []
"witnesses": [
{
data: []
}
]
}
end

Expand All @@ -63,8 +67,7 @@
{
data: [
"0x024a501efd328e062c8675f2365970728c859c592beeefd6be8ead3d901330bc01",
"0x304402207c82346909c13b86a97c38a50b4868b52735ca2e7e59b14f99102159c12ff25f0220511ef5974060510e8cb363877843bd85550805f668676771bfc47e36bc5b8aa2",
"0x4600000000000000"
"0x304402202c643579e47045be050d3842ed9270151af8885e33954bddad0e53e81d1c2dbe02202dc637877a8302110846ebc6a16d9148c106e25f945063ad1c4d4db2b6952408"
]
}
])
Expand Down