Skip to content

Commit d69ebf2

Browse files
committed
feat: add compute_transaction_hash method
1 parent 7783444 commit d69ebf2

File tree

2 files changed

+10
-5
lines changed

2 files changed

+10
-5
lines changed

lib/ckb/wallet.rb

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -112,8 +112,7 @@ def generate_tx(target_address, capacity, data = "0x", key: nil, fee: 0)
112112
outputs_data: outputs.map(&:data),
113113
witnesses: i.witnesses
114114
)
115-
116-
tx_hash = api.compute_transaction_hash(tx)
115+
tx_hash = compute_transaction_hash(tx)
117116

118117
tx.sign(key, tx_hash)
119118
end
@@ -173,7 +172,7 @@ def deposit_to_dao(capacity, key: nil)
173172
witnesses: i.witnesses
174173
)
175174

176-
tx_hash = api.compute_transaction_hash(tx)
175+
tx_hash = compute_transaction_hash(tx)
177176
send_transaction(tx.sign(key, tx_hash))
178177

179178
Types::OutPoint.new(tx_hash: tx_hash, index: "0")
@@ -237,7 +236,7 @@ def generate_withdraw_from_dao_transaction(out_point, key: nil)
237236
Types::Witness.new(data: ["0x0000000000000000"])
238237
]
239238
)
240-
tx_hash = api.compute_transaction_hash(tx)
239+
tx_hash = compute_transaction_hash(tx)
241240
tx.sign(key, tx_hash)
242241
end
243242

@@ -271,6 +270,13 @@ def lock
271270

272271
private
273272

273+
def compute_transaction_hash(tx)
274+
raw_transaction_serializer = CKB::Serializers::RawTransactionSerializer.new(tx)
275+
blake2b = CKB::Blake2b.new
276+
blake2b << Utils.hex_to_bin("0x#{raw_transaction_serializer.serialize}")
277+
blake2b.hexdigest
278+
end
279+
274280
# @param transaction [CKB::Transaction]
275281
def send_transaction(transaction)
276282
api.send_transaction(transaction)

spec/ckb/types/script_spec.rb

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
require 'pry'
21
RSpec.describe CKB::Types::Script do
32
let(:script) do
43
code_hash = CKB::Blake2b.hexdigest(

0 commit comments

Comments
 (0)