Sha256: 8864cbbee8d9383a7edaf23944b1324a5b1e7857ddb000c1b6a6ba51bb71df3d

Contents?: true

Size: 1.36 KB

Versions: 3

Compression:

Stored size: 1.36 KB

Contents

module Sidetree
  module Util
    module JWS
      module_function

      # Sign to +claim+ by +private_key+.
      # @param [Hash] claim
      # @param [Sidetree::Key] private_key Private key used by sign.
      # @return [JSON::JWS]
      def sign(claim, private_key)
        jwt = JSON::JWT.new(claim)
        jwt.header.delete(:typ)
        jwt.sign(private_key.jws_sign_key, :ES256K)
      end

      # Parse +jws_string+ to JSON::JWS
      # @param [String] jws_string JWS data string.
      # @return [JSON::JWS]
      # @raise [Sidetree::Error]
      def parse(jws_string)
        jws =
          JSON::JWS.decode_compact_serialized(jws_string, :skip_verification)
        validate!(jws)
        jws
      end

      # Check whether valid +jws+ or not as sidetree jws.
      # @param [JSON::JWS] jws
      # @return [Boolean]
      def valid?(jws)
        begin
          validate!(jws)
          true
        rescue Sidetree::Error
          false
        end
      end

      # Validate +jws+ as sidetree jws.
      # @param [JSON::JWS] jws
      # @raise [Sidetree::Error]
      def validate!(jws)
        unless jws.header.length == 1
          raise Sidetree::Error, "jws header missing or unknown property"
        end
        unless jws.header[:alg] == "ES256K"
          raise Sidetree::Error, "jws header missing or incorrect alg"
        end
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
sidetree-0.1.5 lib/sidetree/util/jws.rb
sidetree-0.1.4 lib/sidetree/util/jws.rb
sidetree-0.1.3 lib/sidetree/util/jws.rb