2025-03-12 14:13:31 +01:00
|
|
|
package did
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"encoding/json"
|
|
|
|
|
"net/url"
|
2025-06-24 14:05:42 +02:00
|
|
|
|
|
|
|
|
"github.com/INFURA/go-did/crypto"
|
2025-03-12 14:13:31 +01:00
|
|
|
)
|
|
|
|
|
|
2025-04-09 14:16:59 +02:00
|
|
|
// DID is a decoded (i.e. from a string) Decentralized Identifier.
|
2025-03-13 11:26:39 +01:00
|
|
|
type DID interface {
|
2025-05-08 08:15:04 +02:00
|
|
|
// Method returns the name of the DID method (e.g. "key" for did:key).
|
2025-03-12 14:13:31 +01:00
|
|
|
Method() string
|
2025-04-09 14:16:59 +02:00
|
|
|
|
2025-05-08 08:15:04 +02:00
|
|
|
// Document resolves the DID into a DID Document usable for e.g. signature check.
|
|
|
|
|
// This can be simply expanding the DID into a Document, or involve external resolution.
|
2025-03-12 14:13:31 +01:00
|
|
|
Document() (Document, error)
|
2025-03-16 12:17:33 +01:00
|
|
|
|
2025-05-08 08:15:04 +02:00
|
|
|
// String returns the string representation of the DID.
|
|
|
|
|
String() string
|
|
|
|
|
|
|
|
|
|
// ResolutionIsExpensive returns true if resolving to a Document is an expensive operation,
|
|
|
|
|
// e.g. requiring an external HTTP request. By contrast, a self-contained DID (e.g. did:key)
|
|
|
|
|
// can be resolved cheaply without an external call.
|
|
|
|
|
// This can be an indication whether to cache the resolved state.
|
2025-04-09 14:16:59 +02:00
|
|
|
ResolutionIsExpensive() bool
|
|
|
|
|
|
2025-05-08 08:15:04 +02:00
|
|
|
// Equal returns true if this and the given DID are the same.
|
2025-03-16 12:17:33 +01:00
|
|
|
Equal(DID) bool
|
2025-03-12 14:13:31 +01:00
|
|
|
}
|
|
|
|
|
|
2025-03-13 11:26:39 +01:00
|
|
|
// Document is the interface for a DID document. It represents the "resolved" state of a DID.
|
|
|
|
|
type Document interface {
|
2025-03-12 14:13:31 +01:00
|
|
|
json.Marshaler
|
|
|
|
|
|
2025-05-08 08:15:04 +02:00
|
|
|
// Context is the set of JSON-LD context documents.
|
|
|
|
|
Context() []string
|
|
|
|
|
|
2025-06-11 19:02:29 +02:00
|
|
|
// ID is the identifier of the Document, which is the DID itself as string.
|
|
|
|
|
ID() string
|
2025-03-13 11:26:39 +01:00
|
|
|
|
|
|
|
|
// Controllers is the set of DID that is authorized to make changes to the Document. It's often the same as ID.
|
2025-06-11 19:02:29 +02:00
|
|
|
Controllers() []string
|
2025-03-12 14:13:31 +01:00
|
|
|
|
|
|
|
|
// AlsoKnownAs returns an optional set of URL describing ???TODO
|
2025-05-08 08:15:04 +02:00
|
|
|
AlsoKnownAs() []*url.URL
|
2025-03-12 14:13:31 +01:00
|
|
|
|
|
|
|
|
// VerificationMethods returns all the VerificationMethod known in the document.
|
|
|
|
|
VerificationMethods() map[string]VerificationMethod
|
|
|
|
|
|
|
|
|
|
// Authentication defines how the DID is able to authenticate, for purposes such as logging into a website
|
|
|
|
|
// or engaging in any sort of challenge-response protocol.
|
2025-03-16 12:17:33 +01:00
|
|
|
Authentication() []VerificationMethodSignature
|
2025-03-12 14:13:31 +01:00
|
|
|
|
|
|
|
|
// Assertion specifies how the DID subject is expected to express claims, such as for the purposes of issuing
|
|
|
|
|
// a Verifiable Credential.
|
|
|
|
|
// See https://www.w3.org/TR/vc-data-model/
|
2025-03-16 12:17:33 +01:00
|
|
|
Assertion() []VerificationMethodSignature
|
2025-03-12 14:13:31 +01:00
|
|
|
|
|
|
|
|
// KeyAgreement specifies how an entity can generate encryption material in order to transmit confidential
|
|
|
|
|
// information intended for the DID subject, such as for the purposes of establishing a secure communication channel
|
|
|
|
|
// with the recipient.
|
2025-03-16 12:17:33 +01:00
|
|
|
KeyAgreement() []VerificationMethodKeyAgreement
|
2025-03-12 14:13:31 +01:00
|
|
|
|
|
|
|
|
// CapabilityInvocation specifies a verification method that might be used by the DID subject to invoke a
|
|
|
|
|
// cryptographic capability, such as the authorization to update the DID Document.
|
2025-03-16 12:17:33 +01:00
|
|
|
CapabilityInvocation() []VerificationMethodSignature
|
2025-03-12 14:13:31 +01:00
|
|
|
|
|
|
|
|
// CapabilityDelegation specifies a mechanism that might be used by the DID subject to delegate a cryptographic
|
|
|
|
|
// capability to another party, such as delegating the authority to access a specific HTTP API to a subordinate.
|
2025-03-16 12:17:33 +01:00
|
|
|
CapabilityDelegation() []VerificationMethodSignature
|
2025-03-12 14:13:31 +01:00
|
|
|
|
|
|
|
|
// TODO: Service
|
|
|
|
|
// https://www.w3.org/TR/did-extensions-properties/#service-types
|
|
|
|
|
}
|
|
|
|
|
|
2025-03-13 11:26:39 +01:00
|
|
|
// VerificationMethod is a common interface for a cryptographic signature verification method.
|
|
|
|
|
// For example, Ed25519VerificationKey2020 implements the Ed25519 signature verification.
|
|
|
|
|
type VerificationMethod interface {
|
2025-03-12 14:13:31 +01:00
|
|
|
json.Marshaler
|
|
|
|
|
json.Unmarshaler
|
|
|
|
|
|
2025-03-13 11:26:39 +01:00
|
|
|
// ID is a string identifier for the VerificationMethod. It can be referenced in a Document.
|
2025-03-12 14:13:31 +01:00
|
|
|
ID() string
|
2025-03-13 11:26:39 +01:00
|
|
|
|
2025-03-12 14:13:31 +01:00
|
|
|
// Type is a string identifier of a verification method.
|
|
|
|
|
// See https://www.w3.org/TR/did-extensions-properties/#verification-method-types
|
|
|
|
|
Type() string
|
|
|
|
|
|
2025-03-13 11:26:39 +01:00
|
|
|
// Controller is a DID able to control the VerificationMethod.
|
|
|
|
|
// This is not necessarily the same as for DID itself or the Document.
|
|
|
|
|
Controller() string
|
|
|
|
|
|
|
|
|
|
// JsonLdContext reports the JSON-LD context definition required for this verification method.
|
|
|
|
|
JsonLdContext() string
|
2025-03-16 12:17:33 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// VerificationMethodSignature is a VerificationMethod implementing signature verification.
|
|
|
|
|
// It can be used for Authentication, Assertion, CapabilityInvocation, CapabilityDelegation
|
|
|
|
|
// in a Document.
|
|
|
|
|
type VerificationMethodSignature interface {
|
|
|
|
|
VerificationMethod
|
2025-03-13 11:26:39 +01:00
|
|
|
|
|
|
|
|
// Verify checks that 'sig' is a valid signature of 'data'.
|
2025-06-24 18:10:36 +02:00
|
|
|
Verify(data []byte, sig []byte) (bool, error)
|
2025-03-12 14:13:31 +01:00
|
|
|
}
|
2025-03-16 12:17:33 +01:00
|
|
|
|
|
|
|
|
// VerificationMethodKeyAgreement is a VerificationMethod implementing a shared key agreement.
|
|
|
|
|
// It can be used for KeyAgreement in a Document.
|
|
|
|
|
type VerificationMethodKeyAgreement interface {
|
|
|
|
|
VerificationMethod
|
|
|
|
|
|
2025-06-17 16:34:39 +02:00
|
|
|
// PrivateKeyIsCompatible checks that the given PrivateKey is compatible with this method.
|
2025-06-24 14:05:42 +02:00
|
|
|
PrivateKeyIsCompatible(local crypto.KeyExchangePrivateKey) bool
|
2025-06-17 16:34:39 +02:00
|
|
|
|
2025-06-24 14:05:42 +02:00
|
|
|
// KeyExchange computes the shared key using the given PrivateKey.
|
|
|
|
|
KeyExchange(local crypto.KeyExchangePrivateKey) ([]byte, error)
|
2025-03-16 12:17:33 +01:00
|
|
|
}
|