Module: Sidetree::Util::JWS

Defined in:
lib/sidetree/util/jws.rb

Class Method Summary collapse

Class Method Details

.parse(jws_string) ⇒ JSON::JWS

Parse jws_string to JSON::JWS

Parameters:

  • jws_string (String)

    JWS data string.

Returns:

  • (JSON::JWS)

Raises:



20
21
22
23
24
25
# File 'lib/sidetree/util/jws.rb', line 20

def parse(jws_string)
  jws =
    JSON::JWS.decode_compact_serialized(jws_string, :skip_verification)
  validate!(jws)
  jws
end

.sign(claim, private_key) ⇒ JSON::JWS

Sign to claim by private_key.

Parameters:

  • claim (Hash)
  • private_key (Sidetree::Key)

    Private key used by sign.

Returns:

  • (JSON::JWS)


10
11
12
13
14
# File 'lib/sidetree/util/jws.rb', line 10

def sign(claim, private_key)
  jwt = JSON::JWT.new(claim)
  jwt.header.delete(:typ)
  jwt.sign(private_key.jws_sign_key, :ES256K)
end

.valid?(jws) ⇒ Boolean

Check whether valid jws or not as sidetree jws.

Parameters:

  • jws (JSON::JWS)

Returns:

  • (Boolean)


30
31
32
33
34
35
36
37
# File 'lib/sidetree/util/jws.rb', line 30

def valid?(jws)
  begin
    validate!(jws)
    true
  rescue Sidetree::Error
    false
  end
end

.validate!(jws) ⇒ Object

Validate jws as sidetree jws.

Parameters:

  • jws (JSON::JWS)

Raises:



42
43
44
45
46
47
48
49
# File 'lib/sidetree/util/jws.rb', line 42

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