Skip to content

Commit 01b02aa

Browse files
committed
feat: extract data
1 parent d291d54 commit 01b02aa

File tree

4 files changed

+26
-10
lines changed

4 files changed

+26
-10
lines changed

lib/ckb/api.rb

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ def initialize(host: CKB::RPC::DEFAULT_URL, mode: MODE::TESTNET)
1616
@rpc = CKB::RPC.new(host: host)
1717
if mode == MODE::TESTNET
1818
# Testnet system script code_hash
19-
expected_code_hash = "0x54811ce986d5c3e57eaafab22cdd080e32209e39590e204a99b32935f835a13c"
19+
expected_code_hash = "0xa76801d09a0eabbfa545f1577084b6f3bafb0b6250e7f5c89efcfd4e3499fb55"
2020
# For testnet chain, we can assume the second cell of the first transaction
2121
# in the genesis block contains default lock script we can use here.
2222
system_cell_transaction = genesis_block.transactions.first
@@ -26,8 +26,7 @@ def initialize(host: CKB::RPC::DEFAULT_URL, mode: MODE::TESTNET)
2626
index: "1"
2727
)
2828
)
29-
cell_data = CKB::Utils.hex_to_bin(system_cell_transaction.outputs[1].data)
30-
code_hash = CKB::Blake2b.hexdigest(cell_data)
29+
code_hash = system_cell_transaction.outputs[1].data_hash
3130

3231
raise "System script code_hash error!" unless code_hash == expected_code_hash
3332

lib/ckb/types/output.rb

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,28 +3,38 @@
33
module CKB
44
module Types
55
class Output
6-
attr_accessor :data, :lock, :type, :out_point
6+
attr_accessor :data_hash, :lock, :type, :out_point, :data
77
attr_reader :capacity
88

99
# @param capacity [String]
1010
# @param data: [String] 0x...
11+
# @param data_hash: [String] 0x...
1112
# @param lock [CKB::Types::Script]
1213
# @param type [CKB::Types::Script | nil]
1314
# @param out_point [CKB::Types::OutPoint | nil]
14-
def initialize(capacity:, data: "0x", lock:, type: nil, out_point: nil)
15+
def initialize(capacity:, data_hash: nil, lock:, type: nil, out_point: nil, data: "0x")
1516
@capacity = capacity.to_s
16-
@data = data
1717
@lock = lock
1818
@type = type
1919
@out_point = out_point
20+
if data_hash
21+
@data_hash = data_hash
22+
@data = nil
23+
elsif data == "0x"
24+
@data = data
25+
@data_hash = "0x0000000000000000000000000000000000000000000000000000000000000000"
26+
else
27+
@data = data
28+
@data_hash = CKB::Blake2b.hexdigest(CKB::Utils.hex_to_bin(data))
29+
end
2030
end
2131

2232
def capacity=(value)
2333
@capacity = value.to_s
2434
end
2535

2636
def calculate_bytesize
27-
bytesize = 8 + Utils.hex_to_bin(@data).bytesize + @lock.calculate_bytesize
37+
bytesize = 8 + Utils.hex_to_bin(@data_hash).bytesize + @lock.calculate_bytesize
2838
bytesize += @type.calculate_bytesize if @type
2939
bytesize
3040
end
@@ -39,7 +49,7 @@ def to_h(data = true)
3949
lock: @lock.to_h,
4050
type: @type&.to_h
4151
}
42-
hash[:data] = @data if data
52+
hash[:data_hash] = @data_hash if data_hash
4353
hash[:out_point] = @out_point if @out_point
4454
hash
4555
end
@@ -51,7 +61,7 @@ def self.from_h(hash)
5161
out_point = OutPoint.from_h(hash[:out_point]) if hash[:out_point]
5262
new(
5363
capacity: hash[:capacity],
54-
data: hash[:data],
64+
data_hash: hash[:data_hash],
5565
lock: Script.from_h(hash[:lock]),
5666
type: type,
5767
out_point: out_point

lib/ckb/types/transaction.rb

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,27 +3,30 @@
33
module CKB
44
module Types
55
class Transaction
6-
attr_accessor :version, :deps, :inputs, :outputs, :witnesses, :hash
6+
attr_accessor :version, :deps, :inputs, :outputs, :outputs_data, :witnesses, :hash
77

88
# @param hash [String | nil] 0x...
99
# @param version [String]
1010
# @param deps [CKB::Types::OutPoint[]]
1111
# @param inputs [CKB::Types::Input[]]
1212
# @param outputs [CKB::Types::Output[]]
13+
# @param outputs_data [String[]]
1314
# @param witnesses [CKB::Types::Witness[]]
1415
def initialize(
1516
hash: nil,
1617
version: 0,
1718
deps: [],
1819
inputs: [],
1920
outputs: [],
21+
outputs_data: [],
2022
witnesses: []
2123
)
2224
@hash = hash
2325
@version = version.to_s
2426
@deps = deps
2527
@inputs = inputs
2628
@outputs = outputs
29+
@outputs_data = outputs_data
2730
@witnesses = witnesses
2831
end
2932

@@ -50,6 +53,7 @@ def sign(key, tx_hash)
5053
deps: deps,
5154
inputs: inputs,
5255
outputs: outputs,
56+
outputs_data: outputs_data,
5357
witnesses: signed_witnesses
5458
)
5559
end
@@ -60,6 +64,7 @@ def to_h
6064
deps: @deps.map(&:to_h),
6165
inputs: @inputs.map(&:to_h),
6266
outputs: @outputs.map(&:to_h),
67+
outputs_data: @outputs_data,
6368
witnesses: @witnesses.map(&:to_h)
6469
}
6570
hash[:hash] = @hash if @hash
@@ -75,6 +80,7 @@ def self.from_h(hash)
7580
deps: hash[:deps]&.map { |dep| OutPoint.from_h(dep) },
7681
inputs: hash[:inputs].map { |input| Input.from_h(input) },
7782
outputs: hash[:outputs].map { |output| Output.from_h(output) },
83+
outputs_data: hash[:outputs_data],
7884
witnesses: hash[:witnesses].map { |witness| Witness.from_h(witness) }
7985
)
8086
end

lib/ckb/wallet.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@ def generate_tx(target_address, capacity, data = "0x", key: nil, fee: 0)
9494
deps: [api.system_script_out_point],
9595
inputs: i.inputs,
9696
outputs: outputs,
97+
outputs_data: outputs.map(&:data),
9798
witnesses: i.witnesses
9899
)
99100
tx_hash = api.compute_transaction_hash(tx)

0 commit comments

Comments
 (0)