module Sidetree module OP # Deactivate operation class # https://identity.foundation/sidetree/spec/#deactivate class Deactivate < Base attr_reader :did_suffix attr_reader :signed_data attr_reader :revealed_value # Initialize # @param [String] did_suffix # @param [JSON::JWS] signed_data # @param [String] revealed_value def initialize(did_suffix, signed_data, revealed_value) Sidetree::Validator.validate_encoded_multi_hash!( did_suffix, "#{type} didSuffix" ) Sidetree::Validator.validate_encoded_multi_hash!( revealed_value, "#{type} revealValue" ) @did_suffix = did_suffix @signed_data = signed_data @revealed_value = revealed_value end # Parse Deactivate operation data from json string # @param [String] deactivate_data deactivate operation data(json string). # @return [Sidetree::OP::Deactivate] # @raise [Sidetree::Error] def self.from_json(deactivate_data) begin json = JSON.parse(deactivate_data, symbolize_names: true) jws, revealed_value, did_suffix = nil, nil, nil json.each do |k, v| case k when :type unless v == Sidetree::OP::Type::DEACTIVATE raise Sidetree::Error, "Deactivate operation type incorrect" end when :didSuffix did_suffix = v when :revealValue revealed_value = v when :signedData jws = Sidetree::Util::JWS.parse(v) unless jws.keys.length == 2 raise Sidetree::Error, "Deactivate operation signed data missing or unknown property" end Sidetree::Util::JWK.validate!( Sidetree::Util::JWK.parse(jws["recoveryKey"]) ) else raise Sidetree::Error, "Unexpected property #{k.to_s} in deactivate operation" end end if jws Validator.validate_canonicalize_object_hash!( jws["recoveryKey"], revealed_value, "Deactivate key" ) end unless did_suffix raise Sidetree::Error, "The deactivate didSuffix must be a string" end Deactivate.new(did_suffix, jws, revealed_value) rescue JSON::ParserError raise Sidetree::Error, "deactivate_data not json" rescue JSON::JWS::InvalidFormat raise Sidetree::Error, "Invalid signedData" end end def type Sidetree::OP::Type::DEACTIVATE end end end end