Code Monkey home page Code Monkey logo

did's Introduction

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),
	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"},
	Fragment:""
}

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	       345 ns/op
BenchmarkParseWithPath-8          	 3000000	       477 ns/op
BenchmarkParseWithFragment-8      	 3000000	       542 ns/op

Go's url.Parse:

BenchmarkUrlParse-8               	 3000000	       574 ns/op
BenchmarkUrlParseWithPath-8       	 3000000	       514 ns/op
BenchmarkUrlParseWithFragment-8   	 5000000	       382 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.

did's People

Contributors

mrinalwadhwa avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.