did

package
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Jan 23, 2025 License: Apache-2.0, MIT Imports: 14 Imported by: 0

README

did

Testing

The test suite for this package includes test vectors provided by the authors of the did:key method specification. Some of these tests provide the public key associated with a did:key as JWKs and an extra (test-only) dependency has been added to unmarshal the JWK into a Go struct. Support for the secp256k1 encryption algorithm is experimental (but stable in my experience) and requires the addition of the following build tag to properly run:

// go:build jwx_es256k

WARNING: These tests will not run by default!

To include these tests from the CLI, execute the following command:

go test -v ./did -tags jwx_es256k

It should also be possible to configure your IDE to run these tests. For instance, in Codium, add the following JSON snippet to your local project configuration:

"go.testTags": "jwx_es256k",

Documentation

Index

Constants

View Source
const (
	X25519    = multicodec.X25519Pub
	Ed25519   = multicodec.Ed25519Pub // UCAN required/recommended
	P256      = multicodec.P256Pub    // UCAN required
	P384      = multicodec.P384Pub
	P521      = multicodec.P521Pub
	Secp256k1 = multicodec.Secp256k1Pub // UCAN required
	RSA       = multicodec.RsaPub
)

Signature algorithms from the did:key specification

Variables

View Source
var Undef = DID{}

Undef can be used to represent a nil or undefined DID, using DID{} directly is also acceptable.

Functions

func ToPubKey

func ToPubKey(s string) (crypto.PubKey, error)

ToPubKey returns the crypto.PubKey encapsulated in the DID formed by parsing the provided string.

Types

type DID

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

DID is a Decentralized Identifier of the did:key type, directly holding a cryptographic public key. [did:key format]: https://w3c-ccg.github.io/did-method-key/

func FromPrivKey

func FromPrivKey(privKey crypto.PrivKey) (DID, error)

FromPrivKey is a convenience function that returns the DID associated with the public key associated with the provided private key.

func FromPubKey

func FromPubKey(pubKey crypto.PubKey) (DID, error)

FromPubKey returns a did:key constructed from the provided public key.

func GenerateECDSA

func GenerateECDSA() (crypto.PrivKey, DID, error)

GenerateECDSA generates an ECDSA private key and the matching DID for the default P256 curve.

func GenerateECDSAWithCurve

func GenerateECDSAWithCurve(code multicodec.Code) (crypto.PrivKey, DID, error)

GenerateECDSAWithCurve generates an ECDSA private key and matching DID for the user-supplied curve

func GenerateEd25519

func GenerateEd25519() (crypto.PrivKey, DID, error)

GenerateEd25519 generates an Ed25519 private key and the matching DID. This is the RECOMMENDED algorithm.

func GenerateRSA

func GenerateRSA() (crypto.PrivKey, DID, error)

GenerateRSA generates a RSA private key and the matching DID.

func GenerateSecp256k1

func GenerateSecp256k1() (crypto.PrivKey, DID, error)

GenerateEd25519 generates a Secp256k1 private key and the matching DID.

func MustParse

func MustParse(str string) DID

MustParse is like Parse but panics instead of returning an error.

func Parse

func Parse(str string) (DID, error)

Parse returns the DID from the string representation or an error if the prefix and method are incorrect, if an unknown encryption algorithm is specified or if the method-specific-identifier's bytes don't represent a public key for the specified encryption algorithm.

func (DID) Defined

func (d DID) Defined() bool

Defined tells if the DID is defined, not equal to Undef.

func (DID) PubKey

func (d DID) PubKey() (crypto.PubKey, error)

PubKey returns the public key encapsulated by the did:key.

func (DID) String

func (d DID) String() string

String formats the decentralized identity document (DID) as a string.

Directories

Path Synopsis
Package didtest provides Personas that can be used for testing.
Package didtest provides Personas that can be used for testing.

Jump to

Keyboard shortcuts

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