Skip to content

Commit

Permalink
feat: extract data
Browse files Browse the repository at this point in the history
  • Loading branch information
classicalliu committed Aug 7, 2019
1 parent d291d54 commit 01b02aa
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 10 deletions.
5 changes: 2 additions & 3 deletions lib/ckb/api.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ def initialize(host: CKB::RPC::DEFAULT_URL, mode: MODE::TESTNET)
@rpc = CKB::RPC.new(host: host)
if mode == MODE::TESTNET
# Testnet system script code_hash
expected_code_hash = "0x54811ce986d5c3e57eaafab22cdd080e32209e39590e204a99b32935f835a13c"
expected_code_hash = "0xa76801d09a0eabbfa545f1577084b6f3bafb0b6250e7f5c89efcfd4e3499fb55"
# For testnet chain, we can assume the second cell of the first transaction
# in the genesis block contains default lock script we can use here.
system_cell_transaction = genesis_block.transactions.first
Expand All @@ -26,8 +26,7 @@ def initialize(host: CKB::RPC::DEFAULT_URL, mode: MODE::TESTNET)
index: "1"
)
)
cell_data = CKB::Utils.hex_to_bin(system_cell_transaction.outputs[1].data)
code_hash = CKB::Blake2b.hexdigest(cell_data)
code_hash = system_cell_transaction.outputs[1].data_hash

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

Expand Down
22 changes: 16 additions & 6 deletions lib/ckb/types/output.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,28 +3,38 @@
module CKB
module Types
class Output
attr_accessor :data, :lock, :type, :out_point
attr_accessor :data_hash, :lock, :type, :out_point, :data
attr_reader :capacity

# @param capacity [String]
# @param data: [String] 0x...
# @param data_hash: [String] 0x...
# @param lock [CKB::Types::Script]
# @param type [CKB::Types::Script | nil]
# @param out_point [CKB::Types::OutPoint | nil]
def initialize(capacity:, data: "0x", lock:, type: nil, out_point: nil)
def initialize(capacity:, data_hash: nil, lock:, type: nil, out_point: nil, data: "0x")
@capacity = capacity.to_s
@data = data
@lock = lock
@type = type
@out_point = out_point
if data_hash
@data_hash = data_hash
@data = nil
elsif data == "0x"
@data = data
@data_hash = "0x0000000000000000000000000000000000000000000000000000000000000000"
else
@data = data
@data_hash = CKB::Blake2b.hexdigest(CKB::Utils.hex_to_bin(data))
end
end

def capacity=(value)
@capacity = value.to_s
end

def calculate_bytesize
bytesize = 8 + Utils.hex_to_bin(@data).bytesize + @lock.calculate_bytesize
bytesize = 8 + Utils.hex_to_bin(@data_hash).bytesize + @lock.calculate_bytesize
bytesize += @type.calculate_bytesize if @type
bytesize
end
Expand All @@ -39,7 +49,7 @@ def to_h(data = true)
lock: @lock.to_h,
type: @type&.to_h
}
hash[:data] = @data if data
hash[:data_hash] = @data_hash if data_hash
hash[:out_point] = @out_point if @out_point
hash
end
Expand All @@ -51,7 +61,7 @@ def self.from_h(hash)
out_point = OutPoint.from_h(hash[:out_point]) if hash[:out_point]
new(
capacity: hash[:capacity],
data: hash[:data],
data_hash: hash[:data_hash],
lock: Script.from_h(hash[:lock]),
type: type,
out_point: out_point
Expand Down
8 changes: 7 additions & 1 deletion lib/ckb/types/transaction.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,27 +3,30 @@
module CKB
module Types
class Transaction
attr_accessor :version, :deps, :inputs, :outputs, :witnesses, :hash
attr_accessor :version, :deps, :inputs, :outputs, :outputs_data, :witnesses, :hash

# @param hash [String | nil] 0x...
# @param version [String]
# @param deps [CKB::Types::OutPoint[]]
# @param inputs [CKB::Types::Input[]]
# @param outputs [CKB::Types::Output[]]
# @param outputs_data [String[]]
# @param witnesses [CKB::Types::Witness[]]
def initialize(
hash: nil,
version: 0,
deps: [],
inputs: [],
outputs: [],
outputs_data: [],
witnesses: []
)
@hash = hash
@version = version.to_s
@deps = deps
@inputs = inputs
@outputs = outputs
@outputs_data = outputs_data
@witnesses = witnesses
end

Expand All @@ -50,6 +53,7 @@ def sign(key, tx_hash)
deps: deps,
inputs: inputs,
outputs: outputs,
outputs_data: outputs_data,
witnesses: signed_witnesses
)
end
Expand All @@ -60,6 +64,7 @@ def to_h
deps: @deps.map(&:to_h),
inputs: @inputs.map(&:to_h),
outputs: @outputs.map(&:to_h),
outputs_data: @outputs_data,
witnesses: @witnesses.map(&:to_h)
}
hash[:hash] = @hash if @hash
Expand All @@ -75,6 +80,7 @@ def self.from_h(hash)
deps: hash[:deps]&.map { |dep| OutPoint.from_h(dep) },
inputs: hash[:inputs].map { |input| Input.from_h(input) },
outputs: hash[:outputs].map { |output| Output.from_h(output) },
outputs_data: hash[:outputs_data],
witnesses: hash[:witnesses].map { |witness| Witness.from_h(witness) }
)
end
Expand Down
1 change: 1 addition & 0 deletions lib/ckb/wallet.rb
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ def generate_tx(target_address, capacity, data = "0x", key: nil, fee: 0)
deps: [api.system_script_out_point],
inputs: i.inputs,
outputs: outputs,
outputs_data: outputs.map(&:data),
witnesses: i.witnesses
)
tx_hash = api.compute_transaction_hash(tx)
Expand Down

0 comments on commit 01b02aa

Please sign in to comment.