Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Railsをブロックチェーン基板のGatewayに使う話

 Railsをブロックチェーン基板のGatewayに使う話

Fukuoka.rb 200回 LT大会 (#202)
https://fukuokarb.connpass.com/event/206956/

ブロックチェーンのフロントサーバーにRailsを使ってみた話

shibuyamio

March 24, 2021
Tweet

Other Decks in Programming

Transcript

  1. ࡞ͬͨαʔϏε A社DB 統⼀認証基盤 A社 B社 分散台帳基盤 p2p通信 公証⼈ (Notary) リモート認証

    電⼦認証局 ・電⼦契約 ・EDI ・⾃動決済 Etc… 【分散台帳を拡張】 ブロックチェーンによる 取引の整合性を保証 B社DB
  2. ໰୊఺ɿෳࡶ A社DB 統⼀認証基盤 A社 B社 分散台帳基盤 p2p通信 公証⼈ (Notary) リモート認証

    電⼦認証局 ・電⼦契約 ・EDI ・⾃動決済 Etc… 【分散台帳を拡張】 ブロックチェーンによる 取引の整合性を保証 B社DB ϒϩοΫνΣʔϯΛ࢖ͬͨγεςϜ ී௨ͷγεςϜ サービス事業者 データベース A社 B社 Ұ໨ྎવ
  3. ໰୊఺ɿϊʔυͷ؅ཧ • ෼ࢄ୆ாʹ1ڌ఺1DBɾ1αʔόʔ $PSEB/PEF "SUFNJT .24FSWFS 4QSJOH4FSWFS RPC 0SBDMF 0S

    1PTUHSFT 0Sʜ Corda ϊʔυߏ੒ ͜Ε͕औҾڌ఺͝ͱʹඞཁɻ ʢϊʔυͱ͔ʣ ݁࿦ɿKubernetesͰͲ͏ʹ͔͠·ͨ͠ɻ ʢৄࡉ͸·ͨͲ͔͜Ͱʣ
  4. ΞʔΩςΫνϟߏ੒ $PSEB/PEF "SUFNJT 4QSJOH RP 0SBDMF 0S …… 3BJMT"1*4FSWFS 3FBDU4JOHMF1BHF"QQMJDBUJPO

    $PHOJUP "1*(BUFXBZ औҾσʔλ͸࣋ͨͳ͍ɻ ΩϡʔΠϯάɺόϦσʔγϣ ϯɺσʔλՃ޻౳Λ୲౰ ϩʔϧϕʔεೝՄʢ1VOEJUʣ ೝূɺ Ϣʔβʔϓʔϧ $PSEB-FEHFSͰऔҾ࣮ߦ औҾσʔλ͸શ֤ͯڌ఺ ϊʔυ͕ಠཱͯ͠อ࣋ ,VCFSOFUFT ϊʔυ؅ཧ ಠࣗΦϖϨʔλʔ ʢ؅ཧը໘Λ3BJMT͕ఏڙʣ
  5. Controller͸͜Μͳ෩ʹॻ͚Δ module Api class V1::CordaController < V1Controller # rubocop:disable Metrics/ClassLength

    def index @contracts = Corda::Contract[current_pod_name, current_org].all(**pagination_params, **search_params) end def show @contract = Corda::Contract[current_pod_name, current_org].find(params[:id]) @contract.cache_and_set_public_urls(current_org, request.base_url) contract = Contract.find_by(client_contract_id: @contract.client_contract_id) if contract.present? contract.corda_contract_id = @contract.corda_id contract.save! else Rails.logger.warn "corda record not found for client_contract_id:#{@contract.client_contract_id}" end end def create CreatePropose.call(contract_params, current_org, user_info) head :no_content end …….
  6. ModelɿBase module CordaRecord module FinderMethods extend ActiveSupport::Concern module ClassMethods def

    new_from_corda_params(params) new(find_mapping(params)) end def find_mapping(*) raise NotImplementedError end def find(id) response = corda_client.get "#{api_name}/#{id}" new_from_corda_params(JSON.parse(response.body)) rescue Faraday::ResourceNotFound raise Errors::RecordNotFound, "Cannot find #{name}" end def find_by(attributes) body = find_response(attributes) return nil if body.empty? new_from_corda_params(body.first) rescue Faraday::ResourceNotFound nil end …….. private def find_response(attributes) response = corda_client.get "#{api_name}/" do |req| req.params = attributes.stringify_keys.transform_keys { |key| key.camelize(:lower) }.symbolize_keys end JSON.parse(response.body) end end end end module Corda class ContractBase < CordaRecord::Base # rubocop:disable Metrics/ClassLength include CordaRecord::FinderMethods attribute :corda_id, :string attribute :client_transaction_id, :string attribute :title, :string ……. def self.find_mapping(json) # rubocop:disable Metrics/MethodLength, Metrics/AbcSize normalize_json(json) proposer = proposer_json(json) proposee = proposee_json(json) { corda_id: json['linerId'], client_contract_id: json['guestContractId'], title.... } end def self.proposer_json(json) proposer = parse_jsons_and_merge(json['proposerCompanyInfo'], json['proposerDepartmentInfo']) user_infos = json['userInfo'].deep_symbolize_keys proposer.merge(userInfo: user_infos[:proposer]) end ……. end end
  7. Modelɿར༻ଆ module Corda class Propose < CordaRecord::Base include CordaRecord::CreateMethods include

    AwsSdkMethods attribute :id, :string attribute :title, :string validates :title, presence: true, length: { maximum: 60 } def self.api_name @api_name ||= "contracts/#{name.demodulize.downcase.underscore}" end def document_type=(document_type) # ఆٛ֎ͷ஋͕͖ͨ৔߹͸nil͕ઃఆ͞ΕɺpresenceͷνΣοΫͰΤϥʔʹ͢Δ self[:document_type] = DOCUMENT_TYPE_MAPPING[document_type] end end end