Skip to content
This repository has been archived by the owner on Sep 13, 2022. It is now read-only.

[WIP] feat: segregated witness #78

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
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
6 changes: 4 additions & 2 deletions lib/ckb/always_success_wallet.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@ def send_capacity(target_lock, capacity)
version: 0,
deps: [api.always_success_out_point],
inputs: i.inputs,
outputs: outputs
outputs: outputs,
witnesses: []
}
api.send_transaction(tx)
end
Expand Down Expand Up @@ -63,7 +64,8 @@ def install_mruby_cell!(mruby_cell_filename)
version: 0,
deps: [api.always_success_out_point],
inputs: i.inputs,
outputs: outputs
outputs: outputs,
witnesses: []
}
hash = api.send_transaction(tx)
{
Expand Down
9 changes: 6 additions & 3 deletions lib/ckb/udt_wallet.rb
Original file line number Diff line number Diff line change
Expand Up @@ -283,7 +283,8 @@ def send_amount(amount, partial_tx)
version: 0,
deps: [api.mruby_out_point],
inputs: inputs + self_inputs,
outputs: outputs
outputs: outputs,
witnesses: []
}
api.send_transaction(tx)
end
Expand Down Expand Up @@ -317,7 +318,8 @@ def merge_cells
version: 0,
deps: [api.mruby_out_point],
inputs: Ckb::Utils.sign_sighash_all_inputs(inputs, outputs, privkey),
outputs: outputs
outputs: outputs,
witnesses: []
}
api.send_transaction(tx)
end
Expand Down Expand Up @@ -440,7 +442,8 @@ def send_tokens(amount, target_wallet)
version: 0,
deps: [api.mruby_out_point],
inputs: signed_inputs + [target_input],
outputs: outputs
outputs: outputs,
witnesses: []
}
api.send_transaction(tx)
end
Expand Down
17 changes: 13 additions & 4 deletions lib/ckb/utils.rb
Original file line number Diff line number Diff line change
Expand Up @@ -85,11 +85,12 @@ def self.sign_sighash_all_anyonecanpay_inputs(inputs, outputs, privkey)
end
end

def self.sign_sighash_all_inputs(inputs, outputs, privkey)
def self.sign_sighash_all_inputs(i, outputs, privkey)
s = Ckb::Blake2b.new
sighash_type = 0x1.to_s
s.update(sighash_type)
inputs.each do |input|
witnesses = []
i.inputs.each do |input|
s.update(Ckb::Utils.hex_to_bin(input[:previous_output][:hash]))
s.update(input[:previous_output][:index].to_s)
end
Expand All @@ -104,10 +105,12 @@ def self.sign_sighash_all_inputs(inputs, outputs, privkey)
signature = key.ecdsa_serialize(key.ecdsa_sign(s.digest, raw: true))
signature_hex = Ckb::Utils.bin_to_hex(signature)

inputs.map do |input|
args = input[:args] + [signature_hex, sighash_type]
i.inputs = inputs.zip(i.pubkeys).map do |input, pubkey|
witnesses << [pubkey, signature_hex]
args = input[:args] + [sighash_type]
input.merge(args: args)
end
witnesses, inputs
end

def self.calculate_script_capacity(script)
Expand Down Expand Up @@ -147,6 +150,12 @@ def self.normalize_tx_for_json!(transaction)
end
end
end

transaction[:witnesses].each do |witness|
witness[:data] = witness[:data].map do |data|
Ckb::Utils.bin_to_prefix_hex(data)
end
end
transaction
end
end
Expand Down
34 changes: 23 additions & 11 deletions lib/ckb/wallet.rb
Original file line number Diff line number Diff line change
Expand Up @@ -63,11 +63,13 @@ def generate_tx(target_lock, capacity)
lock: lock
}
end
witnesses, inputs = Ckb::Utils.sign_sighash_all_inputs(i, outputs, privkey)
{
version: 0,
deps: [api.mruby_out_point],
inputs: Ckb::Utils.sign_sighash_all_inputs(i.inputs, outputs, privkey),
outputs: outputs
inputs: inputs,
outputs: outputs,
witnesses: witnesses
}
end

Expand Down Expand Up @@ -133,11 +135,13 @@ def create_udt_account_wallet_cell(capacity, token_info)
lock: self.lock
}
end
witnesses, inputs = Ckb::Utils.sign_sighash_all_inputs(i, outputs, privkey)
tx = {
version: 0,
deps: [api.mruby_out_point],
inputs: Ckb::Utils.sign_sighash_all_inputs(i.inputs, outputs, privkey),
outputs: outputs
inputs: inputs,
outputs: outputs,
witnesses: witnesses
}
hash = api.send_transaction(tx)
# This is in fact an OutPoint here
Expand Down Expand Up @@ -206,11 +210,14 @@ def create_fixed_amount_token(capacity, tokens, rate, lock: nil)

outputs[0][:data] += signature

witnesses, inputs = Ckb::Utils.sign_sighash_all_inputs(i, outputs, privkey)

tx = {
version: 0,
deps: [api.mruby_out_point],
inputs: Ckb::Utils.sign_sighash_all_inputs(i.inputs, outputs, privkey),
outputs: outputs
inputs: inputs,
outputs: outputs,
witnesses: witnesses
}
hash = api.send_transaction(tx)
OpenStruct.new(tx_hash: hash, token_info: info)
Expand Down Expand Up @@ -280,7 +287,8 @@ def purchase_fixed_amount_token(tokens, token_info)
version: 0,
deps: [api.mruby_out_point],
inputs: signed_inputs + additional_inputs,
outputs: outputs
outputs: outputs,
witnesses: []
}
api.send_transaction(tx)
end
Expand Down Expand Up @@ -318,11 +326,13 @@ def create_udt_token(capacity, token_name, tokens, account_wallet: false)
lock: self.lock
}
end
witnesses, inputs = Ckb::Utils.sign_sighash_all_inputs(i, outputs, privkey)
tx = {
version: 0,
deps: [api.mruby_out_point],
inputs: Ckb::Utils.sign_sighash_all_inputs(i.inputs, outputs, privkey),
outputs: outputs
inputs: inputs,
outputs: outputs,
witnesses: witnesses
}
hash = api.send_transaction(tx)
OpenStruct.new(tx_hash: hash, token_info: token_info)
Expand All @@ -348,14 +358,16 @@ def gather_inputs(capacity, min_capacity)

input_capacities = 0
inputs = []
pubkeys = []
get_unspent_cells.each do |cell|
input = {
previous_output: {
hash: cell[:out_point][:hash],
index: cell[:out_point][:index]
},
args: [pubkey]
args: []
}
pubkeys << pubkey
inputs << input
input_capacities += cell[:capacity]
if input_capacities >= capacity && (input_capacities - capacity) >= min_capacity
Expand All @@ -365,7 +377,7 @@ def gather_inputs(capacity, min_capacity)
if input_capacities < capacity
raise "Not enough capacity!"
end
OpenStruct.new(inputs: inputs, capacities: input_capacities)
OpenStruct.new(inputs: inputs, capacities: input_capacities, pubkeys: pubkeys)
end

def pubkey
Expand Down
6 changes: 4 additions & 2 deletions test/ckb/api_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,8 @@ def test_send_transaction
version: 0,
deps: [],
inputs: [],
outputs: []
outputs: [],
witnesses: []
}

result = api.send_transaction(tx)
Expand All @@ -87,7 +88,8 @@ def test_trace_transaction
version: 2,
deps: [],
inputs: [],
outputs: []
outputs: [],
witnesses: []
}
result = api.trace_transaction(tx)
refute_nil result
Expand Down
10 changes: 10 additions & 0 deletions test/ckb/utils_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,11 @@ def test_normalize_tx_for_json
},
type: nil
}
],
witnesses: [
{
data: ["0x", "0x616263"]
}
]
}

Expand All @@ -85,6 +90,11 @@ def test_normalize_tx_for_json
},
type: nil
}
],
witnesses: [
{
data: ["", "abc"]
}
]
}

Expand Down