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

Parity for ION Tools #318

Merged
merged 17 commits into from
Mar 27, 2023
16 changes: 16 additions & 0 deletions crypto/jwk.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,22 @@ type PrivateKeyJWK struct {
QI string `json:"qi,omitempty"`
}

// ToPublicKeyJWK converts a PrivateKeyJWK to a PublicKeyJWK
func (k PrivateKeyJWK) ToPublicKeyJWK() PublicKeyJWK {
return PublicKeyJWK{
KTY: k.KTY,
CRV: k.CRV,
X: k.X,
Y: k.Y,
N: k.N,
E: k.E,
Use: k.Use,
KeyOps: k.KeyOps,
Alg: k.Alg,
KID: k.KID,
}
}

// PublicKeyJWK complies with RFC7517 https://datatracker.ietf.org/doc/html/rfc7517
type PublicKeyJWK struct {
KTY string `json:"kty,omitempty" validate:"required"`
Expand Down
45 changes: 24 additions & 21 deletions did/ion/did.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,20 +25,14 @@ func CreateLongFormDID(recoveryKey, updateKey crypto.PublicKeyJWK, document Docu
if err != nil {
return "", err
}

shortFormDID, err := CreateShortFormDID(createRequest.SuffixData)
if err != nil {
return "", err
}

b, _ := json.Marshal(createRequest.SuffixData)
println(string(b))

is := InitialState{
Delta: createRequest.Delta,
SuffixData: createRequest.SuffixData,
}

initialStateBytesCanonical, err := CanonicalizeAny(is)
if err != nil {
return "", errors.Wrap(err, "canonicalizing long form DID suffix data")
Expand All @@ -47,21 +41,6 @@ func CreateLongFormDID(recoveryKey, updateKey crypto.PublicKeyJWK, document Docu
return strings.Join([]string{shortFormDID, encoded}, ":"), nil
}

// CreateShortFormDID follows the process on did uri composition from the spec:
// https://identity.foundation/sidetree/spec/#did-uri-composition, used to generate a short form DID URI,
// which is most frequently used in the protocol and when sharing out ION DIDs.
func CreateShortFormDID(suffixData any) (string, error) {
createOpSuffixDataCanonical, err := CanonicalizeAny(suffixData)
if err != nil {
return "", errors.Wrap(err, "canonicalizing suffix data")
}
hash, err := HashEncode(createOpSuffixDataCanonical)
if err != nil {
return "", errors.Wrap(err, "generating multihash for DID URI")
}
return strings.Join([]string{"did", did.IONMethod.String(), hash}, ":"), nil
}

// 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 All @@ -82,3 +61,27 @@ func DecodeLongFormDID(longFormDID string) (string, *InitialState, error) {
}
return strings.Join(split[0:3], ":"), &initialState, nil
}

// CreateShortFormDID follows the process on did uri composition from the spec:
// https://identity.foundation/sidetree/spec/#did-uri-composition, used to generate a short form DID URI,
// which is most frequently used in the protocol and when sharing out ION DIDs.
func CreateShortFormDID(suffixData any) (string, error) {
createOpSuffixDataCanonical, err := CanonicalizeAny(suffixData)
if err != nil {
return "", errors.Wrap(err, "canonicalizing suffix data")
}
hash, err := HashEncode(createOpSuffixDataCanonical)
if err != nil {
return "", errors.Wrap(err, "generating multihash for DID URI")
}
return strings.Join([]string{"did", did.IONMethod.String(), hash}, ":"), nil
}

// GetShortFormDIDFromLongFormDID returns the short form DID from a long form DID
Copy link
Contributor

Choose a reason for hiding this comment

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

A unit test for this would be nice.

func GetShortFormDIDFromLongFormDID(longFormDID string) (string, error) {
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
func GetShortFormDIDFromLongFormDID(longFormDID string) (string, error) {
func LongToShortFormDID(longFormDID string) (string, error) {

shortFormDID, _, err := DecodeLongFormDID(longFormDID)
if err != nil {
return "", errors.Wrap(err, "decoding long form DID")
}
return shortFormDID, nil
}
24 changes: 24 additions & 0 deletions did/ion/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ type Document struct {
Services []Service `json:"services,omitempty"`
}

func (d Document) IsEmpty() bool {
return len(d.PublicKeys) == 0 && len(d.Services) == 0
}

// Service declaration in a DID Document
type Service struct {
ID string `json:"id,omitempty"`
Expand Down Expand Up @@ -59,12 +63,20 @@ type ReplaceAction struct {

// request models

type AnchorOperation interface {
GetType() OperationType
}

type CreateRequest struct {
Type OperationType `json:"type,omitempty"`
SuffixData SuffixData `json:"suffixData,omitempty"`
Delta Delta `json:"delta,omitempty"`
}

func (CreateRequest) GetType() OperationType {
return Create
}

type SuffixData struct {
DeltaHash string `json:"deltaHash,omitempty"`
RecoveryCommitment string `json:"recoveryCommitment,omitempty"`
Expand All @@ -78,6 +90,10 @@ type UpdateRequest struct {
SignedData string `json:"signedData,omitempty"`
}

func (UpdateRequest) GetType() OperationType {
return Update
}

// UpdateSignedDataObject https://identity.foundation/sidetree/spec/#update-signed-data-object
type UpdateSignedDataObject struct {
UpdateKey crypto.PublicKeyJWK `json:"updateKey,omitempty"`
Expand Down Expand Up @@ -127,6 +143,10 @@ type DeactivateRequest struct {
SignedData string `json:"signedData,omitempty"`
}

func (DeactivateRequest) GetType() OperationType {
return Deactivate
}

// DeactivateSignedDataObject https://identity.foundation/sidetree/spec/#deactivate-signed-data-object
type DeactivateSignedDataObject struct {
DIDSuffix string `json:"didSuffix,omitempty"`
Expand All @@ -141,6 +161,10 @@ type RecoverRequest struct {
SignedData string `json:"signedData,omitempty"`
}

func (RecoverRequest) GetType() OperationType {
return Recover
}

// RecoverySignedDataObject https://identity.foundation/sidetree/spec/#recovery-signed-data-object
type RecoverySignedDataObject struct {
RecoveryCommitment string `json:"recoveryCommitment,omitempty"`
Expand Down
Loading