Class: Sidetree::DID

Inherits:
Object
  • Object
show all
Defined in:
lib/sidetree/did.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(did) ⇒ DID

Returns a new instance of DID.

Raises:



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/sidetree/did.rb', line 8

def initialize(did)
  if !did.start_with?("did:ion:") && !did.start_with?("did:sidetree:")
    raise Error, "Expected DID method not given in DID."
  end
  if did.count(":") > (Sidetree::Params.testnet? ? 4 : 3)
    raise Error, "Unsupported DID format."
  end
  if Sidetree::Params.testnet?
    _, @method, _, @suffix, @long_suffix = did.split(":")
  else
    _, @method, @suffix, @long_suffix = did.split(":")
  end

  if @long_suffix
    unless suffix == create_op.suffix.unique_suffix
      raise Error, "DID document mismatches short-form DID."
    end
  end
end

Instance Attribute Details

#long_suffixObject (readonly)

Returns the value of attribute long_suffix.



5
6
7
# File 'lib/sidetree/did.rb', line 5

def long_suffix
  @long_suffix
end

#methodObject (readonly)

Returns the value of attribute method.



3
4
5
# File 'lib/sidetree/did.rb', line 3

def method
  @method
end

#suffixObject (readonly)

Returns the value of attribute suffix.



4
5
6
# File 'lib/sidetree/did.rb', line 4

def suffix
  @suffix
end

Class Method Details

.create(document, update_key, recovery_key, method: Sidetree::Params::DEFAULT_METHOD) ⇒ Sidetree::DID

Create DID from document, update_key and recovery_key.

Parameters:

Returns:

Raises:



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/sidetree/did.rb', line 35

def self.create(
  document,
  update_key,
  recovery_key,
  method: Sidetree::Params::DEFAULT_METHOD
)
  unless document.is_a?(Sidetree::Model::Document)
    raise Error, "document must be Sidetree::Model::Document instance."
  end
  unless update_key.is_a?(Sidetree::Key)
    raise Error, "update_key must be Sidetree::Key instance."
  end
  unless recovery_key.is_a?(Sidetree::Key)
    raise Error, "recovery_key must be Sidetree::Key instance."
  end

  delta =
    Model::Delta.new([document.to_replace_patch], update_key.to_commitment)
  suffix =
    Sidetree::Model::Suffix.new(delta.to_hash, recovery_key.to_commitment)
  DID.new(
    OP::Create.new(suffix, delta).did(method: method, include_long: true)
  )
end

Instance Method Details

#create_opObject



68
69
70
# File 'lib/sidetree/did.rb', line 68

def create_op
  long_form? ? OP::Create.from_base64(@long_suffix) : nil
end

#long_form?Boolean

Returns:

  • (Boolean)


64
65
66
# File 'lib/sidetree/did.rb', line 64

def long_form?
  !short_form?
end

#short_formString

Return short form did.

Returns:

  • (String)


74
75
76
77
78
79
# File 'lib/sidetree/did.rb', line 74

def short_form
  did = "did:#{method}"
  did += ":#{Sidetree::Params.network}" if Sidetree::Params.testnet?
  did += ":#{suffix}"
  did
end

#short_form?Boolean

Returns:

  • (Boolean)


60
61
62
# File 'lib/sidetree/did.rb', line 60

def short_form?
  long_suffix.nil?
end

#to_sString

Return DID string.

Returns:

  • (String)


83
84
85
86
87
# File 'lib/sidetree/did.rb', line 83

def to_s
  did = short_form
  did += ":#{long_suffix}" if long_form?
  did
end