Skip to content

Commit

Permalink
feat: update SDK based on new verification model
Browse files Browse the repository at this point in the history
  • Loading branch information
xxuejie committed May 28, 2019
1 parent 61a47a7 commit 76eb522
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 22 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 = "0x9e3b3557f11b2b3532ce352bfe8017e9fd11d154c4c7f9b7aaaa1e621b539a08"
expected_code_hash = "0xa4a3d87cd69733562865ddfaf12dd44cf4cd396c4128ff1a5839c2fe48b2a1a1"
# 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
20 changes: 13 additions & 7 deletions lib/ckb/types/transaction.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,12 @@ def initialize(
# @param key [CKB::Key]
# @param tx_hash [String] 0x...
def sign(key, tx_hash)
signature_hex_var = signature_hex(key, 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 || []
signature_hex_var = signature_hex(key, [tx_hash] + old_data)
data = [key.pubkey, signature_hex_var] + old_data
Types::Witness.from_h(data: data)
end

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

Expand Down Expand Up @@ -74,11 +76,15 @@ def self.from_h(hash)

private

def signature_hex(key, tx_hash)
def signature_hex(key, data)
blake2b = CKB::Blake2b.new
data.each do |datum|
blake2b.update(Utils.hex_to_bin(datum))
end
privkey_bin = Utils.hex_to_bin(key.privkey)
secp_key = Secp256k1::PrivateKey.new(privkey: privkey_bin)
signature_bin = secp_key.ecdsa_serialize(
secp_key.ecdsa_sign(Utils.hex_to_bin(tx_hash), raw: true)
secp_key.ecdsa_sign(blake2b.digest, raw: true)
)
Utils.bin_to_hex(signature_bin)
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 @@ -77,7 +77,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 @@ -118,7 +119,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 @@ -158,11 +160,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 @@ -202,15 +208,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 @@ -219,7 +224,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

0 comments on commit 76eb522

Please sign in to comment.