From bfd881cd2a325e14fa37c855b30b8eb588d768cc Mon Sep 17 00:00:00 2001 From: shaojunda Date: Mon, 26 Oct 2020 15:11:23 +0800 Subject: [PATCH] feat: add more types MerkleProof and TransactionProof --- lib/ckb/types/merkle_proof.rb | 32 ++++++++++++++++++++++++++ lib/ckb/types/transaction_proof.rb | 36 ++++++++++++++++++++++++++++++ lib/ckb/types/types.rb | 2 ++ 3 files changed, 70 insertions(+) create mode 100644 lib/ckb/types/merkle_proof.rb create mode 100644 lib/ckb/types/transaction_proof.rb diff --git a/lib/ckb/types/merkle_proof.rb b/lib/ckb/types/merkle_proof.rb new file mode 100644 index 00000000..5c97b285 --- /dev/null +++ b/lib/ckb/types/merkle_proof.rb @@ -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 diff --git a/lib/ckb/types/transaction_proof.rb b/lib/ckb/types/transaction_proof.rb new file mode 100644 index 00000000..8bdff135 --- /dev/null +++ b/lib/ckb/types/transaction_proof.rb @@ -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 diff --git a/lib/ckb/types/types.rb b/lib/ckb/types/types.rb index 9fbf3085..0472fedf 100644 --- a/lib/ckb/types/types.rb +++ b/lib/ckb/types/types.rb @@ -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