Skip to content

Commit

Permalink
fix: fix segwit logic
Browse files Browse the repository at this point in the history
  • Loading branch information
classicalliu committed Apr 8, 2019
1 parent 6959395 commit 460caab
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 7 deletions.
10 changes: 7 additions & 3 deletions lib/ckb/utils.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,11 @@ def self.json_script_to_type_hash(script)
bin_to_prefix_hex(blake2b.digest)
end

def self.sign_sighash_all_inputs(inputs, outputs, privkey)
def self.sign_sighash_all_inputs(inputs, outputs, privkey, pubkeys)
blake2b = CKB::Blake2b.new
sighash_type = 0x1.to_s
blake2b.update(sighash_type)
witnesses = []
inputs.each do |input|
previous_output = input[:previous_output]
blake2b.update(hex_to_bin(previous_output[:hash]))
Expand All @@ -57,10 +58,13 @@ def self.sign_sighash_all_inputs(inputs, outputs, privkey)
)
signature_hex = bin_to_hex(signature_bin)

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

[inputs, witnesses]
end

# In Ruby, bytes are represented using String,
Expand Down
13 changes: 9 additions & 4 deletions lib/ckb/wallet.rb
Original file line number Diff line number Diff line change
Expand Up @@ -64,12 +64,15 @@ def generate_tx(target_lock, capacity)
lock: lock
}
end

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

{
version: 0,
deps: [api.system_script_out_point],
inputs: CKB::Utils.sign_sighash_all_inputs(i.inputs, outputs, privkey),
inputs: inputs,
outputs: outputs,
witnesses: []
witnesses: witnesses
}
end

Expand Down Expand Up @@ -118,11 +121,13 @@ def gather_inputs(capacity, min_capacity)

input_capacities = 0
inputs = []
pubkeys = []
get_unspent_cells.each do |cell|
input = {
previous_output: cell[:out_point],
args: [pubkey]
args: []
}
pubkeys << pubkey
inputs << input
input_capacities += cell[:capacity]

Expand All @@ -131,7 +136,7 @@ def gather_inputs(capacity, min_capacity)

raise "Not enough capacity!" if input_capacities < capacity

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

def pubkey
Expand Down

0 comments on commit 460caab

Please sign in to comment.