Skip to content

Commit

Permalink
feat(sdk): Improved did:key create function
Browse files Browse the repository at this point in the history
Added a new function for creating did:key DIDs. This new standalone function directly takes in a key instead of a keyWriter or keyReader, giving the caller more flexibility in terms of key management. It also minimizes input parameters to only what's required, helping reduce confusion caused by unused parameters.

Signed-off-by: Derek Trider <[email protected]>
  • Loading branch information
Derek Trider committed Sep 11, 2023
1 parent 973002e commit b13c34d
Show file tree
Hide file tree
Showing 4 changed files with 134 additions and 12 deletions.
51 changes: 51 additions & 0 deletions cmd/wallet-sdk-gomobile/didkey/didkey.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/*
Copyright Gen Digital Inc. All Rights Reserved.
SPDX-License-Identifier: Apache-2.0
*/

// Package didkey contains a function that can be used to create did:key documents.
package didkey

import (
"github.com/trustbloc/did-go/doc/did"

"github.com/trustbloc/wallet-sdk/cmd/wallet-sdk-gomobile/api"
"github.com/trustbloc/wallet-sdk/cmd/wallet-sdk-gomobile/wrapper"
"github.com/trustbloc/wallet-sdk/pkg/did/creator/key"
)

// Create creates a new did:key document using the given JWK.
func Create(jwk *api.JSONWebKey) (*api.DIDDocResolution, error) {
var vm *did.VerificationMethod

if jwk.JWK.Crv == "Ed25519" {
// Workaround: when the did:key VDR creates a DID for ed25519, Ed25519VerificationKey2018 is the expected
// verification method.
publicKeyBytes, err := jwk.JWK.PublicKeyBytes()
if err != nil {
return nil, wrapper.ToMobileError(err)
}

vm = &did.VerificationMethod{Value: publicKeyBytes, Type: "Ed25519VerificationKey2018"}
} else {
var err error

vm, err = did.NewVerificationMethodFromJWK("", "JsonWebKey2020", "", jwk.JWK)
if err != nil {
return nil, wrapper.ToMobileError(err)
}
}

didDocResolution, err := key.Create(vm)
if err != nil {
return nil, wrapper.ToMobileError(err)
}

didDocResolutionBytes, err := didDocResolution.JSONBytes()
if err != nil {
return nil, wrapper.ToMobileError(err)
}

return &api.DIDDocResolution{Content: string(didDocResolutionBytes)}, nil
}
50 changes: 50 additions & 0 deletions cmd/wallet-sdk-gomobile/didkey/didkey_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/*
Copyright Gen Digital Inc. All Rights Reserved.
SPDX-License-Identifier: Apache-2.0
*/

package didkey_test

import (
"testing"

"github.com/trustbloc/wallet-sdk/cmd/wallet-sdk-gomobile/didkey"

"github.com/stretchr/testify/require"
"github.com/trustbloc/wallet-sdk/cmd/wallet-sdk-gomobile/localkms"
)

func TestCreate(t *testing.T) {
t.Run("Using an ED25519 key", func(t *testing.T) {
localKMS := createTestKMS(t)

jwk, err := localKMS.Create(localkms.KeyTypeED25519)
require.NoError(t, err)

didDoc, err := didkey.Create(jwk)
require.NoError(t, err)
require.NotNil(t, didDoc)
})
t.Run("Using a P-384 key", func(t *testing.T) {
localKMS := createTestKMS(t)

jwk, err := localKMS.Create(localkms.KeyTypeP384)
require.NoError(t, err)

didDoc, err := didkey.Create(jwk)
require.NoError(t, err)
require.NotNil(t, didDoc)
})
}

func createTestKMS(t *testing.T) *localkms.KMS {
t.Helper()

kmsStore := localkms.NewMemKMSStore()

localKMS, err := localkms.NewKMS(kmsStore)
require.NoError(t, err)

return localKMS
}
9 changes: 7 additions & 2 deletions pkg/did/creator/key/key.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,14 @@ func NewCreator() *Creator {
return &Creator{vdr: key.New()}
}

// Create creates a new did:key document using the given rawKey and verificationMethodType.
// Create creates a new did:key document using the given verification method.
func (d *Creator) Create(vm *did.VerificationMethod) (*did.DocResolution, error) {
return Create(vm)
}

// Create creates a new did:key document using the given verification method.
func Create(vm *did.VerificationMethod) (*did.DocResolution, error) {
didDocArgument := &did.Doc{VerificationMethod: []did.VerificationMethod{*vm}}

return d.vdr.Create(didDocArgument)
return key.New().Create(didDocArgument)
}
36 changes: 26 additions & 10 deletions test/integration/pkg/helpers/openid4ci.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
"github.com/trustbloc/wallet-sdk/cmd/wallet-sdk-gomobile/activitylogger/mem"
"github.com/trustbloc/wallet-sdk/cmd/wallet-sdk-gomobile/api"
"github.com/trustbloc/wallet-sdk/cmd/wallet-sdk-gomobile/did"
"github.com/trustbloc/wallet-sdk/cmd/wallet-sdk-gomobile/didkey"
"github.com/trustbloc/wallet-sdk/cmd/wallet-sdk-gomobile/display"
"github.com/trustbloc/wallet-sdk/cmd/wallet-sdk-gomobile/localkms"
goapi "github.com/trustbloc/wallet-sdk/pkg/api"
Expand All @@ -36,20 +37,35 @@ func NewCITestHelper(t *testing.T, didMethod string, keyType string) *CITestHelp
kms, err := localkms.NewKMS(localkms.NewMemKMSStore())
require.NoError(t, err)

// create DID
c, err := did.NewCreator(kms)
require.NoError(t, err)
var didDoc *api.DIDDocResolution

metricsLogger := metricslogger.NewMetricsLogger()
if didMethod == "key" {
// Create the DID using our new DID creation pattern.

opts := did.NewCreateOpts()
opts.SetKeyType(keyType)
opts.SetMetricsLogger(metricsLogger)
if keyType == "" {
keyType = localkms.KeyTypeED25519
}

didDoc, err := c.Create(didMethod, opts)
require.NoError(t, err)
jwk, err := kms.Create(keyType)
require.NoError(t, err)

didDoc, err = didkey.Create(jwk)
require.NoError(t, err)
} else {
c, err := did.NewCreator(kms)
require.NoError(t, err)

metricsLogger := metricslogger.NewMetricsLogger()

opts := did.NewCreateOpts()
opts.SetKeyType(keyType)
opts.SetMetricsLogger(metricsLogger)

checkDIDCreationMetricsLoggerEvents(t, metricsLogger)
didDoc, err = c.Create(didMethod, opts)
require.NoError(t, err)

checkDIDCreationMetricsLoggerEvents(t, metricsLogger)
}

return &CITestHelper{
KMS: kms,
Expand Down

0 comments on commit b13c34d

Please sign in to comment.