Class: Sidetree::DID
- Inherits:
-
Object
- Object
- Sidetree::DID
- Defined in:
- lib/sidetree/did.rb
Instance Attribute Summary collapse
-
#long_suffix ⇒ Object
readonly
Returns the value of attribute long_suffix.
-
#method ⇒ Object
readonly
Returns the value of attribute method.
-
#suffix ⇒ Object
readonly
Returns the value of attribute suffix.
Class Method Summary collapse
-
.create(document, update_key, recovery_key, method: Sidetree::Params::DEFAULT_METHOD) ⇒ Sidetree::DID
Create DID from
document
,update_key
andrecovery_key
.
Instance Method Summary collapse
- #create_op ⇒ Object
-
#initialize(did) ⇒ DID
constructor
A new instance of DID.
- #long_form? ⇒ Boolean
-
#short_form ⇒ String
Return short form did.
- #short_form? ⇒ Boolean
-
#to_s ⇒ String
Return DID string.
Constructor Details
#initialize(did) ⇒ DID
Returns a new instance of DID.
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_suffix ⇒ Object (readonly)
Returns the value of attribute long_suffix.
5 6 7 |
# File 'lib/sidetree/did.rb', line 5 def long_suffix @long_suffix end |
#method ⇒ Object (readonly)
Returns the value of attribute method.
3 4 5 |
# File 'lib/sidetree/did.rb', line 3 def method @method end |
#suffix ⇒ Object (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
.
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_op ⇒ Object
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
64 65 66 |
# File 'lib/sidetree/did.rb', line 64 def long_form? !short_form? end |
#short_form ⇒ String
Return short form did.
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
60 61 62 |
# File 'lib/sidetree/did.rb', line 60 def short_form? long_suffix.nil? end |
#to_s ⇒ String
Return DID 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 |