did

package module
v0.0.0-...-081d7a7 Latest Latest
Warning

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

Go to latest
Published: Apr 20, 2023 License: MIT Imports: 20 Imported by: 0

README

did-sdk-go

Introduction

Provides general purpose operations for MetaBlox according to W3C spec.

Installation and usage

The import path for the package is gopkg.in/yaml.v3.

To install it, run:

go get github.com/metablox/did-sdk-go

API documentation

API stability

License

The did-sdk-go package is licensed under the MIT license. Please see the LICENSE file for details.

Example

package main

import (
	"fmt"
)

func main() {
	fmt.Println("did-sdk-go,hello world!")
}

Documentation

Overview

Package did Package did-sdk-go provides general purpose operations for MetaBlox according to W3C spec.

Index

Constants

View Source
const BaseIDString = "https://metablox.io/credentials/"

All credential ids use a format of this value plus a number. ex. 'http://metablox.com/credentials/5' Only the number is stored in the db as the ID; the full string is only used in formal credentials

View Source
const ContextCredential = "https://www.w3.org/2018/credentials/v1"
View Source
const ContextDID = "https://w3id.org/did/v1"
View Source
const ContextSecp256k1 = "https://identity.foundation/EcdsaSecp256k1RecoverySignature2020#"
View Source
const PurposeAuth = "Authentication"
View Source
const Secp256k1Key = "EcdsaSecp256k1RecoveryMethod2020"
View Source
const Secp256k1Sig = "EcdsaSecp256k1Signature2019"
View Source
const TypeCredential = "VerifiableCredential"
View Source
const TypeMining = "MiningLicense"
View Source
const TypeWifi = "WifiAccess"

Variables

View Source
var (
	ErrRenewRevoked         = errors.New("VC has been revoked, cannot renew")
	ErrUnknownIssuer        = errors.New("unknown issuer")
	ErrSecp256k1WrongVMType = errors.New("must use a verification method with a type of 'EcdsaSecp256k1RecoveryMethod2020' to verify a 'EcdsaSecp256k1Signature2019' proof")
	ErrUnknownProofType     = errors.New("unable to verify unknown proof type")
	ErrMissingVM            = errors.New("failed to find verification method")
	ErrWrongAddress         = errors.New("provided public key does not match issuer address")
)

Functions

func CompareAddresses

func CompareAddresses(vm VerificationMethod, pubKey *ecdsa.PublicKey) bool

make sure that the address created from pubKey matches the address stored in vm's BlockChainAccountId field

func ConvertDocToBytes

func ConvertDocToBytes(doc DIDDocument) []byte

convert document into byte array so it can be hashed (appears to be unused currently)

func ConvertServiceToBytes

func ConvertServiceToBytes(service Service) []byte

convert service to byte array. Used as part of converting document to bytes

func ConvertTimesFromDBFormat

func ConvertTimesFromDBFormat(vc *VerifiableCredential) error

convert issuance and expiration times of credential from db format to RFC3339

func ConvertTimesToDBFormat

func ConvertTimesToDBFormat(vc *VerifiableCredential) error

convert issuance and expiration times of credential from RFC3339 to db format

func ConvertVCToBytes

func ConvertVCToBytes(vc VerifiableCredential) []byte

convert credential to bytes so it can be hashed

func ConvertVMToBytes

func ConvertVMToBytes(vm VerificationMethod) []byte

convert VM to byte array. Used as part of converting document to bytes

func ConvertVPToBytes

func ConvertVPToBytes(vp VerifiablePresentation) []byte

convert presentation to bytes so it can be hashed

func CreateJWSSignature

func CreateJWSSignature(privKey *ecdsa.PrivateKey, message []byte) (string, error)

use a private key and a message to create a JWS format signature

func DocumentToJson

func DocumentToJson(document *DIDDocument) ([]byte, error)

TODO: check that this function can be safely removed

func GenerateDIDString

func GenerateDIDString(privKey *ecdsa.PrivateKey) string

func GenerateTestPrivKey

func GenerateTestPrivKey() *ecdsa.PrivateKey

func GetIssuerChainId

func GetIssuerChainId() *big.Int

func GetIssuerDid

func GetIssuerDid() string

func GetIssuerPrivateKey

func GetIssuerPrivateKey() *ecdsa.PrivateKey

func Init

func Init(cfg *Config) error

func IsDIDValid

func IsDIDValid(did []string) bool

check format of DID string

func PrepareDID

func PrepareDID(did string) ([]string, bool)

splits did and checks that it is formatted correctly

func Resolve

generate the did document that matches the provided did string. Any errors are returned in the ResolutionMetadata. Note that options currently does nothing; including it is a requirement according to W3C specifications, but we don't do anything with it right now

func ResolveRepresentation

generate a did document and return it in a specific data format (currently just JSON)

func SplitDIDString

func SplitDIDString(did string) []string

split did string into 3 sections. First two should be 'did' and 'metablox', last one wil be the identifier

func VCToJson

func VCToJson(vc *VerifiableCredential) ([]byte, error)

convert credential to a JSON format. Currently unused

func VerifyJWSSignature

func VerifyJWSSignature(signature string, pubKey *ecdsa.PublicKey, message []byte) (bool, error)

verify a JWS format signature using the matching public key and the original message

func VerifyVC

func VerifyVC(vc *VerifiableCredential, registry *registry.Registry) (bool, error)

Need to make sure that the stated issuer of the VC actually created it (using the proof alongside the issuer's verification methods), as well as check that the issuer is a trusted source

func VerifyVCSecp256k1

func VerifyVCSecp256k1(vc *VerifiableCredential, pubKey *ecdsa.PublicKey) (bool, error)

Verify that the provided public key matches the signature in the proof. Since we've made sure that the address in the issuer vm matches this public key, verifying the signature here proves that the signature was made with the issuer's private key

func VerifyVP

func VerifyVP(presentation *VerifiablePresentation, registry *registry.Registry) (bool, error)

Verify a presentation. Need to first verify the presentation's proof using the holder's DID document. Afterwards, need to verify the proof of each credential included inside the presentation

func VerifyVPSecp256k1

func VerifyVPSecp256k1(presentation *VerifiablePresentation, pubKey *ecdsa.PublicKey) (bool, error)

Verify that the provided public key matches the signature in the proof. Since we've made sure that the address in the holder vm matches this public key, verifying the signature here proves that the signature was made with the holder's private key

Types

type Config

type Config struct {
	Passphrase string   `json:"passphrase"`
	Keystore   string   `json:"keystore"`
	ChainId    *big.Int `json:"chainId"`
}

type DIDDocument

type DIDDocument struct {
	Context            []string             `json:"@context" mapstructure:"@context"`
	ID                 string               `json:"id"`
	Created            string               `json:"created"`
	Updated            string               `json:"updated"`
	Version            int                  `json:"version"`
	VerificationMethod []VerificationMethod `json:"verificationMethod"`
	Authentication     string               `json:"authentication"`
	Service            []Service            `json:"service"`
}

func CreateDID

func CreateDID(privKey *ecdsa.PrivateKey) *DIDDocument

TODO: check that this function can be safely removed. The foundation service doesn't need to create new DID documents; however, some other system may want to import this function

func CreateDIDDocument

func CreateDIDDocument() *DIDDocument

func GenerateTestDIDDocument

func GenerateTestDIDDocument() *DIDDocument

func GenerateTestResolvedDIDDocument

func GenerateTestResolvedDIDDocument() *DIDDocument

func GetDocument

func GetDocument(targetDID string, registry *registry.Registry) (*DIDDocument, [32]byte, error)

func JsonToDocument

func JsonToDocument(jsonDoc []byte) (*DIDDocument, error)

TODO: check that this function can be safely removed

func (*DIDDocument) AddService

func (doc *DIDDocument) AddService(service Service)

func (DIDDocument) RetrieveVerificationMethod

func (doc DIDDocument) RetrieveVerificationMethod(vmID string) (VerificationMethod, error)

type DocumentMetadata

type DocumentMetadata struct {
	Created       string   `json:"created"`
	Updated       string   `json:"updated"`
	Deactivated   string   `json:"deactivated"`
	NextUpdate    string   `json:"nextUpdate"`
	VersionID     string   `json:"versionId"`
	NextVersionID string   `json:"nextVersionId"`
	EquivalentID  []string `json:"equivalentId"`
	CanonicalID   string   `json:"canonicalId"`
}

type MiningLicenseInfo

type MiningLicenseInfo struct {
	CredentialID string `json:"-" db:"CredentialID"`
	ID           string `json:"id" db:"ID"`     //id of the user the credential is assigned to
	Name         string `json:"name" db:"Name"` //manufacturer name
	Model        string `json:"model" db:"Model"`
	Serial       string `json:"serial" db:"Serial"` //serial number
}

func CreateMiningLicenseInfo

func CreateMiningLicenseInfo() *MiningLicenseInfo

func GenerateTestMiningLicenseInfo

func GenerateTestMiningLicenseInfo() *MiningLicenseInfo

func NewMiningLicenseInfo

func NewMiningLicenseInfo(credentialID, id, name, model, serial string) *MiningLicenseInfo

type RepresentationResolutionMetadata

type RepresentationResolutionMetadata struct {
	ContentType string `json:"contentType"`
	Error       string `json:"error"`
}

type RepresentationResolutionOptions

type RepresentationResolutionOptions struct {
	Accept string `json:"accept"`
}

type ResolutionMetadata

type ResolutionMetadata struct {
	Error string `json:"error"`
}

type ResolutionOptions

type ResolutionOptions struct {
	Accept string `json:"accept"`
}

func CreateResolutionOptions

func CreateResolutionOptions() *ResolutionOptions

type Service

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

func CreateService

func CreateService() *Service

type SubjectInfo

type SubjectInfo struct {
	ID           string `json:"id"`
	GivenName    string `json:"givenName"`
	FamilyName   string `json:"familyName"`
	Gender       string `json:"gender"`
	BirthCountry string `json:"birthCountry"`
	BirthDate    string `json:"birthName"`
}

This can be a type of input form to set up the VC. Temp fields here currently, will be changed in the future

func CreateSubjectInfo

func CreateSubjectInfo() *SubjectInfo

func GenerateTestSubjectInfo

func GenerateTestSubjectInfo() *SubjectInfo

func NewSubjectInfo

func NewSubjectInfo(id string, givenName, familyName, gender, birthCountry, birthDate string) *SubjectInfo

type VCProof

type VCProof struct {
	Type               string `json:"type"`
	Created            string `json:"created"`
	VerificationMethod string `json:"verificationMethod"`
	ProofPurpose       string `json:"proofPurpose"`
	JWSSignature       string `json:"jws"`             //signature is created from a hash of the VC
	PublicKeyString    []byte `json:"publicKeyString"` //public key (belonging to issuer) used for verification
}

func CreateProof

func CreateProof(vm string) VCProof

create a credential proof using the provided verification method string

func CreateVCProof

func CreateVCProof() *VCProof

func NewVCProof

func NewVCProof(proofType, created, vm, purpose, sig string, pubKey []byte) *VCProof

type VCSchemaChanged

type VCSchemaChanged struct {
	VcName string
	Name   [32]byte
	Value  []byte
}

type VPProof

type VPProof struct {
	Type               string `json:"type"`
	Created            string `json:"created"`
	VerificationMethod string `json:"verificationMethod"`
	ProofPurpose       string `json:"proofPurpose"`
	JWSSignature       string `json:"jws"`             //signature is created from a hash of the VP
	Nonce              string `json:"nonce"`           //random value generated by verifier that must be included in proof
	PublicKeyString    []byte `json:"publicKeyString"` //public key (belonging to holder) used for verification
}

func CreateVPProof

func CreateVPProof() *VPProof

func NewVPProof

func NewVPProof(proofType, created, vm, purpose, sig, nonce string, pubKey []byte) *VPProof

type VerifiableCredential

type VerifiableCredential struct {
	Context           []string    `json:"@context" mapstructure:"@context"`
	ID                string      `json:"id" db:"ID"`
	Type              []string    `json:"type"`
	Issuer            string      `json:"issuer" db:"Issuer"`
	IssuanceDate      string      `json:"issuanceDate" db:"IssuanceDate"`
	ExpirationDate    string      `json:"expirationDate" db:"ExpirationDate"`
	Description       string      `json:"description" db:"Description"`
	CredentialSubject interface{} `json:"credentialSubject"`
	Proof             VCProof     `json:"proof"`
	Revoked           bool        `json:"revoked" db:"Revoked"`
}

func CreateVC

func CreateVC(issuerDocument *DIDDocument) (*VerifiableCredential, error)

Base function for creating VCs. Called by any function that creates a type of VC to initialize universal values

func CreateVerifiableCredential

func CreateVerifiableCredential() *VerifiableCredential

func GenerateTestMiningLicenseVC

func GenerateTestMiningLicenseVC() *VerifiableCredential

func GenerateTestVC

func GenerateTestVC() *VerifiableCredential

func GenerateTestWifiAccessVC

func GenerateTestWifiAccessVC() *VerifiableCredential

func JsonToVC

func JsonToVC(jsonVC []byte) (*VerifiableCredential, error)

convert JSON formatted credential to object. Currently unused

func NewVerifiableCredential

func NewVerifiableCredential(context []string, id string, vctype []string, issuer, issuanceDate, expirationDate, description string, subject interface{}, proof VCProof, revoked bool) *VerifiableCredential

type VerifiablePresentation

type VerifiablePresentation struct {
	Context              []string               `json:"@context" mapstructure:"@context"`
	Type                 []string               `json:"type"`
	VerifiableCredential []VerifiableCredential `json:"verifiableCredential"`
	Holder               string                 `json:"holder"`
	Proof                VPProof                `json:"proof"`
}

func CreatePresentation

func CreatePresentation(credentials []VerifiableCredential, holderDocument DIDDocument, holderPrivKey *ecdsa.PrivateKey, nonce string) (*VerifiablePresentation, error)

create a presentation using 1 or more credentials. Currently unused

func GenerateTestPresentation

func GenerateTestPresentation() *VerifiablePresentation

func GenerateTestWifiAccessPresentation

func GenerateTestWifiAccessPresentation() *VerifiablePresentation

func NewPresentation

func NewPresentation(context, presentationType []string, credentials []VerifiableCredential, holder string, proof VPProof) *VerifiablePresentation

type VerificationMethod

type VerificationMethod struct {
	ID                  string `json:"id"`
	MethodType          string `json:"type"`
	Controller          string `json:"controller"`
	BlockchainAccountId string `json:"blockchainAccountId"`
}

type WifiAccessInfo

type WifiAccessInfo struct {
	CredentialID string `json:"-" db:"CredentialID"`
	ID           string `json:"id" db:"ID"`     //id of the user the credential is assigned to
	Type         string `json:"type" db:"Type"` //user or validator
}

func CreateWifiAccessInfo

func CreateWifiAccessInfo() *WifiAccessInfo

func GenerateTestWifiAccessInfo

func GenerateTestWifiAccessInfo() *WifiAccessInfo

func NewWifiAccessInfo

func NewWifiAccessInfo(credentialID, id, userType string) *WifiAccessInfo

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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