From 7ae9c92bf68fa2f44823e29cc0692295bfd0f218 Mon Sep 17 00:00:00 2001 From: shaojunda Date: Wed, 4 Sep 2019 12:04:30 +0800 Subject: [PATCH] feat: add compute_hash to transaction --- lib/ckb/types/transaction.rb | 7 +++++++ lib/ckb/wallet.rb | 15 +++------------ 2 files changed, 10 insertions(+), 12 deletions(-) diff --git a/lib/ckb/types/transaction.rb b/lib/ckb/types/transaction.rb index 95f87ddb..20448ebd 100644 --- a/lib/ckb/types/transaction.rb +++ b/lib/ckb/types/transaction.rb @@ -76,6 +76,13 @@ def to_h hash end + def compute_hash + raw_transaction_serializer = CKB::Serializers::RawTransactionSerializer.new(self) + blake2b = CKB::Blake2b.new + blake2b << Utils.hex_to_bin("0x#{raw_transaction_serializer.serialize}") + blake2b.hexdigest + end + def self.from_h(hash) return if hash.nil? diff --git a/lib/ckb/wallet.rb b/lib/ckb/wallet.rb index df32855b..cb1d8ac0 100644 --- a/lib/ckb/wallet.rb +++ b/lib/ckb/wallet.rb @@ -112,9 +112,8 @@ def generate_tx(target_address, capacity, data = "0x", key: nil, fee: 0) outputs_data: outputs.map(&:data), witnesses: i.witnesses ) - tx_hash = compute_transaction_hash(tx) - tx.sign(key, tx_hash) + tx.sign(key, tx.compute_hash) end # @param target_address [String] @@ -172,7 +171,7 @@ def deposit_to_dao(capacity, key: nil) witnesses: i.witnesses ) - tx_hash = compute_transaction_hash(tx) + tx_hash = tx.compute_hash send_transaction(tx.sign(key, tx_hash)) Types::OutPoint.new(tx_hash: tx_hash, index: "0") @@ -236,8 +235,7 @@ def generate_withdraw_from_dao_transaction(out_point, key: nil) Types::Witness.new(data: ["0x0000000000000000"]) ] ) - tx_hash = compute_transaction_hash(tx) - tx.sign(key, tx_hash) + tx.sign(key, tx.compute_hash) end # @param hash_hex [String] "0x..." @@ -270,13 +268,6 @@ 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)