Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support ION DID Reconstruction & Long Form DID resolution #389

Merged
merged 11 commits into from
May 24, 2023
174 changes: 174 additions & 0 deletions did/ion/did.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package ion

import (
"fmt"
"strings"

"github.com/TBD54566975/ssi-sdk/crypto/jwx"
"github.com/TBD54566975/ssi-sdk/cryptosuite"
"github.com/goccy/go-json"

"github.com/TBD54566975/ssi-sdk/did"
Expand All @@ -17,6 +19,21 @@ type InitialState struct {
Delta Delta `json:"delta,omitempty"`
}

func (is InitialState) ToDIDStrings() (shortFormDID string, longFormDID string, err error) {
shortFormDID, err = CreateShortFormDID(is.SuffixData)
if err != nil {
return shortFormDID, longFormDID, err
}
initialStateBytesCanonical, err := CanonicalizeAny(is)
if err != nil {
err = errors.Wrap(err, "canonicalizing long form DID suffix data")
return shortFormDID, longFormDID, err
}
encoded := Encode(initialStateBytesCanonical)
longFormDID = strings.Join([]string{shortFormDID, encoded}, ":")
return shortFormDID, longFormDID, nil
}

// CreateLongFormDID generates a long form DID URI representation from a document, recovery, and update keys,
// intended to be the initial state of a DID Document. The method follows the guidelines in the spec:
// https://identity.foundation/sidetree/spec/#long-form-did-uris
Expand All @@ -41,6 +58,12 @@ func CreateLongFormDID(recoveryKey, updateKey jwx.PublicKeyJWK, document Documen
return strings.Join([]string{shortFormDID, encoded}, ":"), nil
}

// IsLongFormDID checks if a string is a long form DID URI
func IsLongFormDID(maybeLongFormDID string) bool {
split := strings.Split(maybeLongFormDID, ":")
return len(split) == 4
decentralgabe marked this conversation as resolved.
Show resolved Hide resolved
}

// DecodeLongFormDID decodes a long form DID into a short form DID and
// its create operation suffix data
func DecodeLongFormDID(longFormDID string) (string, *InitialState, error) {
Expand Down Expand Up @@ -85,3 +108,154 @@ func LongToShortFormDID(longFormDID string) (string, error) {
}
return shortFormDID, nil
}

// PatchesToDIDDocument applies a list of sidetree state patches in order resulting in a DID Document.
func PatchesToDIDDocument(shortFormDID, longFormDID string, patches []Patch) (*did.Document, error) {
if len(patches) == 0 {
return nil, errors.New("no patches to apply")
}
if shortFormDID == "" {
return nil, errors.New("short form DID is required")
}
doc := did.Document{
Context: []string{"https://www.w3.org/ns/did/v1"},
ID: shortFormDID,
AlsoKnownAs: longFormDID,
}
for _, patch := range patches {
switch patch.GetAction() {
case AddServices:
addServicePatch := patch.(AddServicesAction)
doc.Services = append(doc.Services, addServicePatch.Services...)
case RemoveServices:
removeServicePatch := patch.(RemoveServicesAction)
for _, id := range removeServicePatch.IDs {
for i, service := range doc.Services {
if service.ID == id {
doc.Services = append(doc.Services[:i], doc.Services[i+1:]...)
}
}
}
case AddPublicKeys:
addKeyPatch := patch.(AddPublicKeysAction)
gotDoc, err := addPublicKeysPatch(doc, addKeyPatch)
if err != nil {
return nil, err
}
doc = *gotDoc
case RemovePublicKeys:
removeKeyPatch := patch.(RemovePublicKeysAction)
gotDoc, err := removePublicKeysPatch(doc, removeKeyPatch)
if err != nil {
return nil, err
}
doc = *gotDoc
case Replace:
replacePatch := patch.(ReplaceAction)
gotDoc, err := replaceActionPatch(doc, replacePatch)
if err != nil {
return nil, err
}
doc = *gotDoc
default:
return nil, fmt.Errorf("unknown patch type: %T", patch)
}
}
return &doc, nil
}

func replaceActionPatch(doc did.Document, patch ReplaceAction) (*did.Document, error) {
// first zero out all public keys and services
doc.VerificationMethod = nil
doc.Authentication = nil
doc.AssertionMethod = nil
doc.KeyAgreement = nil
doc.CapabilityInvocation = nil
doc.CapabilityDelegation = nil
doc.Services = nil

// now add back what the patch includes
gotDoc, err := addPublicKeysPatch(doc, AddPublicKeysAction{PublicKeys: patch.Document.PublicKeys})
if err != nil {
return nil, err
}
doc = *gotDoc
for _, service := range patch.Document.Services {
doc.Services = append(doc.Services, service)
}
return &doc, nil
}

func addPublicKeysPatch(doc did.Document, patch AddPublicKeysAction) (*did.Document, error) {
for _, key := range patch.PublicKeys {
currKey := key
doc.VerificationMethod = append(doc.VerificationMethod, did.VerificationMethod{
ID: currKey.ID,
Type: cryptosuite.LDKeyType(currKey.Type),
Controller: doc.ID,
PublicKeyJWK: &currKey.PublicKeyJWK,
})
for _, purpose := range currKey.Purposes {
switch purpose {
case Authentication:
doc.Authentication = append(doc.Authentication, currKey.ID)
case AssertionMethod:
doc.AssertionMethod = append(doc.AssertionMethod, currKey.ID)
case KeyAgreement:
doc.KeyAgreement = append(doc.KeyAgreement, currKey.ID)
case CapabilityInvocation:
doc.CapabilityInvocation = append(doc.CapabilityInvocation, currKey.ID)
case CapabilityDelegation:
doc.CapabilityDelegation = append(doc.CapabilityDelegation, currKey.ID)
default:
return nil, fmt.Errorf("unknown key purpose: %s:%s", currKey.ID, purpose)
}
}
}
return &doc, nil
}

func removePublicKeysPatch(doc did.Document, patch RemovePublicKeysAction) (*did.Document, error) {
for _, id := range patch.IDs {
removed := false
for i, key := range doc.VerificationMethod {
if key.ID != id {
continue
}
doc.VerificationMethod = append(doc.VerificationMethod[:i], doc.VerificationMethod[i+1:]...)
removed = true

// TODO(gabe): in the future handle the case where the value is not a simple ID
// remove from all other key lists
for j, auth := range doc.Authentication {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you also unnest this and the following for loops?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

how? this needs to be nested as far as I can tell

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i have no idea what I was talking about... ignore please.

if auth == id {
doc.Authentication = append(doc.Authentication[:j], doc.Authentication[j+1:]...)
}
}
for j, auth := range doc.AssertionMethod {
decentralgabe marked this conversation as resolved.
Show resolved Hide resolved
if auth == id {
doc.AssertionMethod = append(doc.AssertionMethod[:j], doc.AssertionMethod[j+1:]...)
}
}
for j, auth := range doc.Authentication {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is repeated. Is it intentional?

If not, should there be a test for this?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed

if auth == id {
doc.Authentication = append(doc.Authentication[:j], doc.Authentication[j+1:]...)
}
}
for j, auth := range doc.CapabilityInvocation {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
for j, auth := range doc.CapabilityInvocation {
for j, ci := range doc.CapabilityInvocation {

if auth == id {
doc.CapabilityInvocation = append(doc.CapabilityInvocation[:j], doc.CapabilityInvocation[j+1:]...)
}
}
for j, auth := range doc.CapabilityDelegation {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
for j, auth := range doc.CapabilityDelegation {
for j, cd := range doc.CapabilityDelegation {

if auth == id {
doc.CapabilityDelegation = append(doc.CapabilityDelegation[:j], doc.CapabilityDelegation[j+1:]...)
}
}
}
if !removed {
return nil, fmt.Errorf("could not find key with id %s", id)
}
}
return &doc, nil
}
Loading