A golang package to work with Decentralized Identifiers (DIDs)

did

Build Status Go Report Card cover.run GoDoc License

did is a Go package that provides tools to work with Decentralized Identifiers (DIDs).

Install

go get github.com/ockam-network/did

Example

package main

import (
	"fmt"
	"log"

	"github.com/ockam-network/did"
)

func main() {
	d, err := did.Parse("did:example:q7ckgxeq1lxmra0r")
	if err != nil {
		log.Fatal(err)
	}
	fmt.Printf("%#v", d)
}

The above example parses the input string according to the rules defined in the DID Grammar and prints the following value of DID type.

&did.DID{
	Method:"example",
	ID:"q7ckgxeq1lxmra0r",
	IDStrings:[]string{"q7ckgxeq1lxmra0r"},
	Path:"",
	PathSegments:[]string(nil),
	Query:"",
	Fragment:""
}

The input string may also be a DID Reference with a DID Path:

d, err := did.Parse("did:example:q7ckgxeq1lxmra0r/abc/pqr")

which would result in:

&did.DID{
	Method:"example",
	ID:"q7ckgxeq1lxmra0r",
	IDStrings:[]string{"q7ckgxeq1lxmra0r"},
	Path:"abc/pqr",
	PathSegments:[]string{"abc", "pqr"},
	Query:"",
	Fragment:""
}

or a DID Reference with a DID Path and a DID Query:

d, err := did.Parse("did:example:q7ckgxeq1lxmra0r/abc/pqr?xyz")
fmt.Println(d.Query)
// Output: xyz

or a DID Reference with a DID Fragment:

d, err := did.Parse("did:example:q7ckgxeq1lxmra0r#keys-1")
fmt.Println(d.Fragment)
// Output: keys-1

This package also implements the Stringer interface for the DID type. It is easy to convert DID type structures into valid DID strings:

d := &did.DID{Method: "example", ID: "q7ckgxeq1lxmra0r"}
fmt.Println(d.String())
// Output: did:example:q7ckgxeq1lxmra0r

or with a refence with a fragment:

d := &did.DID{Method: "example", ID: "q7ckgxeq1lxmra0r", Fragment: "keys-1"}
fmt.Println(d.String())
// Output: did:example:q7ckgxeq1lxmra0r#keys-1

For more documentation and examples, please see godoc.

Build

To compile the code in this repository, run:

go build

Test

This repository includes a comprehensive suite of tests that check for various edge cases within the DID Grammar.

To run the tests, run:

go test -v -cover

Benchmark

We haven't spent any time tuning the performance of the parser, however this repository includes some benchmarks that compare the speed of did.Parse against Go's url.Parse with inputs of similar length, this is intended as a sanity check to ensure that did.Parse is at least comparable in performance to url.Parse

go test -bench=.

did.Parse included in this package:

BenchmarkParse-8                  	 5000000	       365 ns/op
BenchmarkParseWithPath-8          	 3000000	       500 ns/op
BenchmarkParseWithQuery-8         	 3000000	       558 ns/op
BenchmarkParseWithFragment-8      	 3000000	       552 ns/op

Go's url.Parse:

BenchmarkUrlParse-8               	 3000000	       475 ns/op
BenchmarkUrlParseWithPath-8       	 3000000	       505 ns/op
BenchmarkUrlParseWithQuery-8      	 5000000	       294 ns/op
BenchmarkUrlParseWithFragment-8   	 5000000	       369 ns/op

Contributing

This package is early in its development and we welcome all contributions from the DID community. Please open issues and send pull requests.

We follow the conventions specified in Conventional Commits for our commit messages.

License

This package is licensed under Apache License 2.0.

Owner
Ockam
Ockam makes it easy to build devices that communicate securely, privately and trustfully with cloud services and other devices.
Ockam
Comments
  • Comply with the new DID ABNF

    Comply with the new DID ABNF

    There is ongoing discussion around updating the DID ABNF that we would need to update the parser for

    https://github.com/w3c-ccg/did-spec/pull/168 https://pr-preview.s3.amazonaws.com/w3c-ccg/did-spec/pull/168.html

    screen shot 2019-02-21 at 7 56 50 pm
  • Should a semi-colon be allowed in a specific-idstring?

    Should a semi-colon be allowed in a specific-idstring?

    The current version of the DID spec includes examples like this: did:example:123456789abcdefghi;photos

    However the grammar doesn't allow that semi-colon. Does this mean that specific schemes can override the generic scheme?

    did-reference      = did [ "/" did-path ] [ "#" did-fragment ]
    did                = "did:" method ":" specific-idstring
    method             = 1*methodchar
    methodchar         = %x61-7A / DIGIT
    specific-idstring  = idstring *( ":" idstring )
    idstring           = 1*idchar
    idchar             = ALPHA / DIGIT / "." / "-"
    
  • chore: adding cla check workflow

    chore: adding cla check workflow

    As all users making contributions into Ockam repositories needs to sign the CLA beforehand this workflow will give prompt them for approval and manage the persistence.

  • Feature/did spec update 1.0

    Feature/did spec update 1.0

    • Updated did ABNF documentation.

    • Updated Code to handle rule changes from previous ABNF.

    • Updated/Added additional tests to handle rule changes/additions.

    • Updated code level comments.

  • feat: add did query support

    feat: add did query support

    1. add query to grammar
    2. add query support to Parse()
    3. add query support to IsReference()
    4. add query support tp String()
    5. add tests
    6. add query examples
    7. update README

    For #5

  • Bug on parsing DID URLs with a path and fragment

    Bug on parsing DID URLs with a path and fragment

    Expect this to succeed:

    t.Run("ok - parse a DID URL", func(t *testing.T) {
        id, err := ParseDID("did:nuts:123/path#fragment")
        assert.NoError(t, err)
        assert.Equal(t, "did:nuts:123/path#fragment", id.String())
    })
    

    Get this instead:

    === RUN   TestParseDIDURL/ok_-_parse_a_DID_URL
        did_test.go:86: 
            	Error Trace:	did_test.go:86
            	Error:      	Received unexpected error:
            	            	character is not allowed in path
            	Test:       	TestParseDIDURL/ok_-_parse_a_DID_URL
    

    It seems that this fails when there is a path present, but no query between path and the fragment.

  • Add support for Service URIs

    Add support for Service URIs

    Related to #3 https://github.com/w3c-ccg/did-spec/issues/90 https://github.com/decentralized-identity/did-common-java/blob/master/src/main/resources/did.abnf https://lists.w3.org/Archives/Public/public-credentials/2018Nov/thread.html#msg159

  • Support passing in additional checks on ID method specific id strings

    Support passing in additional checks on ID method specific id strings

    We could maybe support passing in functions that return bool that are run at this point in parseID https://github.com/ockam-network/did/blob/master/did.go#L260

    If the functions returns true only then the ID would be considered valid.

Go (Golang) GNU gettext utilities package

Gotext GNU gettext utilities for Go. Features Implements GNU gettext support in native Go. Complete support for PO files including: Support for multil

Dec 18, 2022
htmlquery is golang XPath package for HTML query.

htmlquery Overview htmlquery is an XPath query package for HTML, lets you extract data or evaluate from HTML documents by an XPath expression. htmlque

Jan 4, 2023
Package sanitize provides functions for sanitizing text in golang strings.

sanitize Package sanitize provides functions to sanitize html and paths with go (golang). FUNCTIONS sanitize.Accents(s string) string Accents replaces

Dec 5, 2022
A minimalistic emoji package for Go (golang)

emoji ?? ?? ?? emoji is a minimalistic emoji library for Go. It lets you use emoji characters in strings. Inspired by spatie/emoji Install ?? go get g

Dec 14, 2022
xmlquery is Golang XPath package for XML query.

xmlquery Overview xmlquery is an XPath query package for XML documents, allowing you to extract data or evaluate from XML documents with an XPath expr

Jan 1, 2023
Frongo is a Golang package to create HTML/CSS components using only the Go language.

Frongo Frongo is a Go tool to make HTML/CSS document out of Golang code. It was designed with readability and usability in mind, so HTML objects are c

Jul 29, 2021
This package provides Go (golang) types and helper functions to do some basic but useful things with mxGraph diagrams in XML, which is most famously used by app.diagrams.net, the new name of draw.io.

Go Draw - Golang MX This package provides types and helper functions to do some basic but useful things with mxGraph diagrams in XML, which is most fa

Aug 30, 2022
Genex package for Go

genex Genex package for Go Easy and efficient package to expand any given regex into all the possible strings that it can match. This is the code that

Nov 2, 2022
A declarative struct-tag-based HTML unmarshaling or scraping package for Go built on top of the goquery library

goq Example import ( "log" "net/http" "astuart.co/goq" ) // Structured representation for github file name table type example struct { Title str

Dec 12, 2022
csvplus extends the standard Go encoding/csv package with fluent interface, lazy stream operations, indices and joins.

csvplus Package csvplus extends the standard Go encoding/csv package with fluent interface, lazy stream processing operations, indices and joins. The

Apr 9, 2022
[Go] Package of validators and sanitizers for strings, numerics, slices and structs

govalidator A package of validators and sanitizers for strings, structs and collections. Based on validator.js. Installation Make sure that Go is inst

Dec 28, 2022
Package strit introduces a new type of string iterator, along with a number of iterator constructors, wrappers and combinators.

strit Package strit (STRing ITerator) assists in development of string processing pipelines by providing a simple iteration model that allows for easy

Jun 21, 2022
A markdown renderer package for the terminal
A markdown renderer package for the terminal

go-term-markdown go-term-markdown is a go package implementing a Markdown renderer for the terminal. Note: Markdown being originally designed to rende

Nov 25, 2022
Go package for syntax highlighting of code

syntaxhighlight Package syntaxhighlight provides syntax highlighting for code. It currently uses a language-independent lexer and performs decently on

Nov 18, 2022
ByNom is a Go package for parsing byte sequences, suitable for parsing text and binary data

ByNom is a Go package for parsing byte sequences. Its goal is to provide tools to build safe byte parsers without compromising the speed or memo

May 5, 2021
Package i18n is a middleware that provides internationalization and localization for Flamego
Package i18n is a middleware that provides internationalization and localization for Flamego

i18n Package i18n is a middleware that provides internationalization and localization for Flamego. Installation The minimum requirement of Go is 1.16.

Dec 14, 2022
A dead simple parser package for Go
A dead simple parser package for Go

A dead simple parser package for Go V2 Introduction Tutorial Tag syntax Overview Grammar syntax Capturing Capturing boolean value Streaming Lexing Sta

Dec 30, 2022
A Package Searching and Installation tool for Go Projects.

Gosearch A Package Searching and Installation tool for Go Projects. Installation go install github.com/kinensake/[email protected] Usage gosearch <pack

Dec 19, 2022
[Crawler/Scraper for Golang]🕷A lightweight distributed friendly Golang crawler framework.一个轻量的分布式友好的 Golang 爬虫框架。

Goribot 一个分布式友好的轻量的 Golang 爬虫框架。 完整文档 | Document !! Warning !! Goribot 已经被迁移到 Gospider|github.com/zhshch2002/gospider。修复了一些调度问题并分离了网络请求部分到另一个仓库。此仓库会继续

Oct 29, 2022