did

package
v1.1.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 24, 2025 License: MIT Imports: 13 Imported by: 0

Documentation

Index

Constants

View Source
const (
	VerificationID   = "#keys-1"
	KeyAgreementID   = "#keys-2"
	VerificationType = "Ed25519VerificationKey2018"
	KeyAgreementType = "X25519KeyAgreementKey2019"
	DIDContext       = "https://www.w3.org/ns/did/v1"
)
View Source
const (
	// New
	Ed25519VerificationKey2020 = "Ed25519VerificationKey2020"
	X25519KeyAgreementKey2020  = "X25519KeyAgreementKey2020"

	// Old
	Ed25519VerificationKey2018 = "Ed25519VerificationKey2018"
	X25519KeyAgreementKey2019  = "X25519KeyAgreementKey2019"
)

Variables

View Source
var (
	ErrExpired            = errors.New("verification result expired")
	ErrNotFound           = errors.New("verification result not found")
	ErrMissingCreatedAt   = errors.New("document missing createdAt field")
	ErrMissingTrustedRoot = errors.New("document not signed by trusted root")
	ErrDocNotController   = errors.New("document controller not in trusted roots")
	ErrTimestampInvalid   = errors.New("document timestamp is invalid")
)
View Source
var (
	DIDVersion = 1
)

Functions

This section is empty.

Types

type DIDIdentifier

type DIDIdentifier struct {
	ID       string
	Address  string
	KeyPair  KeyPair
	Metadata Metadata
	Services []ServiceEndpoint
}

DIDIdentifier represents a Decentralized Identifier.

func (*DIDIdentifier) Addr

func (d *DIDIdentifier) Addr() string

Addr returns the DID address.

func (*DIDIdentifier) Document

func (d *DIDIdentifier) Document() *Document

Document converts the DID to a DID Document.

func (*DIDIdentifier) SignDocument

func (d *DIDIdentifier) SignDocument() ([]byte, error)

SignDocument signs the DID Document.

func (*DIDIdentifier) SignMessage

func (d *DIDIdentifier) SignMessage(data []byte) ([]byte, error)

SignMessage signs a message using the DID's key pair.

type DIDVerifier

type DIDVerifier struct {
	// contains filtered or unexported fields
}

DIDVerifier implements the VerifierDID interface.

func (*DIDVerifier) AddTrustedRoot

func (v *DIDVerifier) AddTrustedRoot(rootDID string)

AddTrustedRoot adds a trusted root DID to the verifier.

func (*DIDVerifier) ClearCache

func (v *DIDVerifier) ClearCache()

ClearCache clears the verification result cache.

func (*DIDVerifier) GetStats

func (v *DIDVerifier) GetStats() VerificationStats

GetStats returns the current verification statistics.

func (*DIDVerifier) VerifyDocument

func (v *DIDVerifier) VerifyDocument(doc *Document, signature []byte) (bool, error)

VerifyDocument verifies the DID Document using the provided signature.

type Document

type Document struct {
	Context              []string             `json:"@context"`
	ID                   string               `json:"id"`
	VerificationMethod   []VerificationMethod `json:"verificationMethod"`
	Authentication       []string             `json:"authentication"`
	AssertionMethod      []string             `json:"assertionMethod"`
	KeyAgreement         []string             `json:"keyAgreement"`
	CapabilityInvocation []string             `json:"capabilityInvocation"`
	CapabilityDelegation []string             `json:"capabilityDelegation"`
	Service              []ServiceEndpoint    `json:"service,omitempty"`
	Created              string               `json:"created,omitempty"`
	Updated              string               `json:"updated,omitempty"`
}

Document represents the DID's Document structure following W3C DID spec.

func NewDocument

func NewDocument(did DIDIdentifier, createdAt time.Time) *Document

NewDocument creates a new DID Document based on the provided DIDIdentifier and creation time.

func NewDocumentWithNewStandards

func NewDocumentWithNewStandards(did DIDIdentifier, createdAt time.Time) *Document

NewDocumentWithNewStandards creates a new DID Document following the latest W3C DID standards.

func (*Document) JSONMarshal

func (d *Document) JSONMarshal() ([]byte, error)

func (*Document) JSONUnmarshal

func (d *Document) JSONUnmarshal(data []byte) error

type IdentifierDID

type IdentifierDID interface {
	Addr() string
	Document() *Document
	SignDocument() ([]byte, error)
	SignMessage(data []byte) ([]byte, error)
}

IdentifierDID defines the interface for a Identifier DID.

func NewDIDIdentifier

func NewDIDIdentifier(services []ServiceEndpoint) IdentifierDID

NewDID creates a new IdentifierDID instance.

type KeyPair

type KeyPair interface {
	GenerateID() string
	GenerateAddr() string
	GetEd25519PublicKey() []byte
	GetX25519PublicKey() []byte
	SignData(data []byte) ([]byte, error)
	VerifyData(data []byte, signature []byte) (bool, error)
	Shake(peerPublicKey *ecdh.PublicKey) ([]byte, error)
	Unshake(peerPublicKey *ecdh.PublicKey, signature []byte, peerEdPublicKey ed25519.PublicKey) ([]byte, error)
}

KeyPair defines the interface for key pair operations.

func NewPeerKeyPair

func NewPeerKeyPair(r io.Reader) (KeyPair, error)

NewPeerKeyPair generates a new PeerKeyPair.

type Metadata

type Metadata struct {
	Controller string
	Version    int
}

Metadata holds metadata for a DID.

type PeerKeyPair

type PeerKeyPair struct {
	EdPublic  ed25519.PublicKey
	EdPrivate ed25519.PrivateKey
	XPublic   *ecdh.PublicKey
	XPrivate  *ecdh.PrivateKey
}

PeerKeyPair holds the key pairs for a Peer DID.

func (*PeerKeyPair) GenerateAddr

func (k *PeerKeyPair) GenerateAddr() string

GenerateAddr generates an address from the ED25519 public key.

func (*PeerKeyPair) GenerateID

func (k *PeerKeyPair) GenerateID() string

GenerateDID generates a DID from the Ed25519 public key.

func (*PeerKeyPair) GetEd25519PublicKey

func (k *PeerKeyPair) GetEd25519PublicKey() []byte

GetEd25519PublicKey returns the Ed25519 public key.

func (*PeerKeyPair) GetX25519PublicKey

func (k *PeerKeyPair) GetX25519PublicKey() []byte

GetX25519PublicKey returns the X25519 public key.

func (*PeerKeyPair) Shake

func (k *PeerKeyPair) Shake(peerPublicKey *ecdh.PublicKey) ([]byte, error)

Shake sign own X25519 public key using own Ed25519 private key

func (*PeerKeyPair) SignData

func (k *PeerKeyPair) SignData(data []byte) ([]byte, error)

using Ed25519 private key sign data

func (*PeerKeyPair) Unshake

func (k *PeerKeyPair) Unshake(peerPublicKey *ecdh.PublicKey, signature []byte, peerEdPublicKey ed25519.PublicKey) ([]byte, error)

Unshake verify peer's X25519 public key signature using peer's Ed25519 public key and generate shared secret

func (*PeerKeyPair) VerifyData

func (k *PeerKeyPair) VerifyData(data []byte, signature []byte) (bool, error)

using Ed25519 public key verify data signature

type ServiceEndpoint

type ServiceEndpoint struct {
	ID              string      `json:"id"`
	Type            string      `json:"type"`
	ServiceEndpoint interface{} `json:"serviceEndpoint"`
}

ServiceEndpoint represents a service endpoint in the DID Document.

type VerificationMethod

type VerificationMethod struct {
	ID                 string `json:"id"`
	Type               string `json:"type"`
	Controller         string `json:"controller"`
	PublicKeyMultibase string `json:"publicKeyMultibase"`
}

VerificationMethod represents a verification method in the DID Document.

type VerificationResult

type VerificationResult struct {
	IsValid    bool
	DID        string
	VerifiedAt time.Time
	ExpiresAt  time.Time
	ErrorMsg   string
	Signature  []byte
	PublicKey  ed25519.PublicKey
}

VerificationResult holds the result of a DID verification attempt.

type VerificationStats

type VerificationStats struct {
	TotalVerifications      int64
	SuccessfulVerifications int64
	FailedVerifications     int64
	CacheHits               int64
	CacheMisses             int64
}

VerificationStats holds statistics about the verification process.

type VerifierConfig

type VerifierConfig struct {
	EnableCache        bool
	CacheTTL           time.Duration
	MaxCacheSize       int
	ValidateTimestamp  bool
	TimestampTolerance time.Duration
	RequireTrustedRoot bool
}

VerifierConfig holds configuration for the DID verifier.

type VerifierDID

type VerifierDID interface {
	VerifyDocument(doc *Document, signature []byte) (bool, error)
	GetStats() VerificationStats
	AddTrustedRoot(did string)
	ClearCache()
}

VerifierDID defines the interface for verifying a DID Document.

func NewDIDVerifier

func NewDIDVerifier(config VerifierConfig) VerifierDID

func NewDefaultDIDVerifier

func NewDefaultDIDVerifier() VerifierDID

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL