Skip to content

Commit

Permalink
feat: add compute_transaction_hash method
Browse files Browse the repository at this point in the history
  • Loading branch information
shaojunda committed Sep 2, 2019
1 parent 7783444 commit d69ebf2
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 5 deletions.
14 changes: 10 additions & 4 deletions lib/ckb/wallet.rb
Original file line number Diff line number Diff line change
Expand Up @@ -112,8 +112,7 @@ def generate_tx(target_address, capacity, data = "0x", key: nil, fee: 0)
outputs_data: outputs.map(&:data),
witnesses: i.witnesses
)

tx_hash = api.compute_transaction_hash(tx)
tx_hash = compute_transaction_hash(tx)

tx.sign(key, tx_hash)
end
Expand Down Expand Up @@ -173,7 +172,7 @@ def deposit_to_dao(capacity, key: nil)
witnesses: i.witnesses
)

tx_hash = api.compute_transaction_hash(tx)
tx_hash = compute_transaction_hash(tx)
send_transaction(tx.sign(key, tx_hash))

Types::OutPoint.new(tx_hash: tx_hash, index: "0")
Expand Down Expand Up @@ -237,7 +236,7 @@ def generate_withdraw_from_dao_transaction(out_point, key: nil)
Types::Witness.new(data: ["0x0000000000000000"])
]
)
tx_hash = api.compute_transaction_hash(tx)
tx_hash = compute_transaction_hash(tx)
tx.sign(key, tx_hash)
end

Expand Down Expand Up @@ -271,6 +270,13 @@ def lock

private

def compute_transaction_hash(tx)
raw_transaction_serializer = CKB::Serializers::RawTransactionSerializer.new(tx)
blake2b = CKB::Blake2b.new
blake2b << Utils.hex_to_bin("0x#{raw_transaction_serializer.serialize}")
blake2b.hexdigest
end

# @param transaction [CKB::Transaction]
def send_transaction(transaction)
api.send_transaction(transaction)
Expand Down
1 change: 0 additions & 1 deletion spec/ckb/types/script_spec.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
require 'pry'
RSpec.describe CKB::Types::Script do
let(:script) do
code_hash = CKB::Blake2b.hexdigest(
Expand Down

0 comments on commit d69ebf2

Please sign in to comment.