Skip to content

Commit

Permalink
feat: add compute_hash to transaction
Browse files Browse the repository at this point in the history
  • Loading branch information
shaojunda committed Sep 4, 2019
1 parent b140d3a commit 7ae9c92
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 12 deletions.
7 changes: 7 additions & 0 deletions lib/ckb/types/transaction.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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?

Expand Down
15 changes: 3 additions & 12 deletions lib/ckb/wallet.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down Expand Up @@ -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")
Expand Down Expand Up @@ -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..."
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 7ae9c92

Please sign in to comment.