Skip to content

Commit

Permalink
feat: update SDK per lock script changes
Browse files Browse the repository at this point in the history
  • Loading branch information
xxuejie committed May 9, 2019
1 parent 7de38c6 commit 29ad6ae
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 46 deletions.
4 changes: 4 additions & 0 deletions lib/ckb/api.rb
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,10 @@ def send_transaction(transaction)
rpc.send_transaction(transaction.to_h)
end

def compute_transaction_hash(transaction)
rpc.compute_transaction_hash(transaction.to_h)
end

# @return [Hash]
def local_node_info
rpc.local_node_info
Expand Down
4 changes: 4 additions & 0 deletions lib/ckb/rpc.rb
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,10 @@ def get_peers_state
rpc_request("get_peers_state")
end

def compute_transaction_hash(transaction)
rpc_request("_compute_transaction_hash", params: [transaction])
end

def inspect
"\#<RPC@#{uri}>"
end
Expand Down
11 changes: 1 addition & 10 deletions lib/ckb/types/script.rb
Original file line number Diff line number Diff line change
Expand Up @@ -43,16 +43,7 @@ def self.generate_lock(target_pubkey_blake160, system_script_cell_hash)
new(
code_hash: system_script_cell_hash,
args: [
# There are 2 conversions from binary to hex string here:
# 1. The inner unpack1 is required since the deployed lock script
# now accepts a hex string version of the public key hash so we can
# treat it as a null-terminated string in C for ease of processing.
# So even though the inner unpack1 already converts the public key
# hash binary to a hex string format, we should still see it as a
# binary from the SDK point of view.
# 2. The outer bin_to_hex then converts the binary (in SDK
# point of view) to a hex string required by CKB RPC.
CKB::Utils.bin_to_hex(target_pubkey_blake160_bin.unpack1("H*"))
target_pubkey_blake160
]
)
end
Expand Down
41 changes: 6 additions & 35 deletions lib/ckb/types/transaction.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,22 +14,12 @@ def initialize(hash: nil, version: 0, deps: [], inputs: [], outputs: [], witness
@witnesses = witnesses
end

def sign(key)
signature_hex_var = signature_hex(key)
def sign(key, tx_hash)
signature_hex_var = signature_hex(key, tx_hash)

witnesses = inputs.map do |_input|
# Same as lock arguments, the witness data here will be considered hex
# strings by the C script, those exact hex strings are binaries to the
# SDK, hence we also need 2 binary to hex string conversions.
Types::Witness.from_h(
data: [
CKB::Utils.bin_to_hex(
CKB::Utils.hex_to_bin(key.pubkey).unpack1("H*")
),
CKB::Utils.bin_to_hex(
CKB::Utils.hex_to_bin(signature_hex_var).unpack1("H*")
)
]
data: [key.pubkey, signature_hex_var]
)
end

Expand Down Expand Up @@ -70,31 +60,12 @@ def self.from_h(hash)

private

def signature_hex(key)
def signature_hex(key, tx_hash)
blake2b = CKB::Blake2b.new
inputs.each do |input|
previous_output = input.previous_output
blake2b.update(Utils.hex_to_bin(previous_output.cell.tx_hash))
blake2b.update(previous_output.cell.index.to_s)
end
outputs.each do |output|
blake2b.update(output.capacity.to_s)
blake2b.update(
Utils.hex_to_bin(
output.lock.to_hash
)
)
next unless output.type

blake2b.update(
Utils.hex_to_bin(
output.type.to_hash
)
)
end
blake2b.update(Utils.hex_to_bin(tx_hash))
privkey_bin = Utils.hex_to_bin(key.privkey)
secp_key = Secp256k1::PrivateKey.new(privkey: privkey_bin)
signature_bin = secp_key.ecdsa_serialize(
signature_bin = secp_key.ecdsa_serialize_compact(
secp_key.ecdsa_sign(blake2b.digest, raw: true)
)
Utils.bin_to_hex(signature_bin)
Expand Down
3 changes: 2 additions & 1 deletion lib/ckb/wallet.rb
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,9 @@ def generate_tx(target_address, capacity)
inputs: i.inputs,
outputs: outputs
)
tx_hash = api.compute_transaction_hash(tx)

tx.sign(key)
tx.sign(key, tx_hash)
end

# @param target_address [String]
Expand Down

0 comments on commit 29ad6ae

Please sign in to comment.