Skip to content

Commit

Permalink
feat: add more types
Browse files Browse the repository at this point in the history
MerkleProof and TransactionProof
  • Loading branch information
shaojunda committed Oct 26, 2020
1 parent f097919 commit bfd881c
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 0 deletions.
32 changes: 32 additions & 0 deletions lib/ckb/types/merkle_proof.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# frozen_string_literal: true

module CKB
module Types
class MerkleProof
attr_accessor :indices, :lemmas

# @param indices [String[]]
# @param lemmas [String[]]
def initialize(indices:, lemmas:)
@indices = indices.map { |index| Utils.to_int(index) }
@lemmas = lemmas
end

def to_h
{
indices: indices.map { |index| Utils.to_hex(index) },
lemmas: lemmas
}
end

def self.from_h(hash)
return if hash.nil?

new(
indices: hash[:indices],
lemmas: hash[:lemmas]
)
end
end
end
end
36 changes: 36 additions & 0 deletions lib/ckb/types/transaction_proof.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# frozen_string_literal: true

module CKB
module Types
class TransactionProof
attr_accessor :block_hash, :witnesses_root, :proof

# @param block_hash [String]
# @param witnesses_root [String]
# @param proof CKB::Types::MerkleProof
def initialize(block_hash:, witnesses_root:, proof:)
@block_hash = block_hash
@witnesses_root = witnesses_root
@proof = proof
end

def to_h
{
block_hash: block_hash,
witnesses_root: witnesses_root,
proof: proof.to_h
}
end

def self.from_h(hash)
return if hash.nil?

new(
block_hash: hash[:block_hash],
witnesses_root: hash[:witnesses_root],
proof: MerkleProof.from_h(hash[:proof])
)
end
end
end
end
2 changes: 2 additions & 0 deletions lib/ckb/types/types.rb
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@
require_relative "peer_sync_state"
require_relative "local_node"
require_relative "local_node_protocol"
require_relative "transaction_proof"
require_relative "merkle_proof"

module CKB
module Types
Expand Down

0 comments on commit bfd881c

Please sign in to comment.