-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(sdk): Improved did:key create function
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
32bc40e
commit 47f9383
Showing
4 changed files
with
163 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
/* | ||
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 ( | ||
"errors" | ||
|
||
"github.com/trustbloc/did-go/doc/did" | ||
"github.com/trustbloc/wallet-sdk/pkg/walleterror" | ||
|
||
"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" | ||
) | ||
|
||
// ErrorModule is the error module name for this package. | ||
const ErrorModule = "DIDKEY" | ||
|
||
// Create creates a new did:key document using the given JWK. | ||
func Create(jwk *api.JSONWebKey) (*api.DIDDocResolution, error) { | ||
if jwk == nil || jwk.JWK == nil { | ||
return nil, wrapper.ToMobileError(walleterror.NewInvalidSDKUsageError( | ||
ErrorModule, errors.New("jwk object cannot be null/nil/empty"))) | ||
} | ||
|
||
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
/* | ||
Copyright Gen Digital Inc. All Rights Reserved. | ||
SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package didkey_test | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
"github.com/trustbloc/kms-go/doc/jose/jwk" | ||
|
||
"github.com/trustbloc/wallet-sdk/cmd/wallet-sdk-gomobile/api" | ||
"github.com/trustbloc/wallet-sdk/cmd/wallet-sdk-gomobile/didkey" | ||
"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) | ||
|
||
jsonWebKey, err := localKMS.Create(localkms.KeyTypeED25519) | ||
require.NoError(t, err) | ||
|
||
didDoc, err := didkey.Create(jsonWebKey) | ||
require.NoError(t, err) | ||
require.NotNil(t, didDoc) | ||
}) | ||
t.Run("Using a P-384 key", func(t *testing.T) { | ||
localKMS := createTestKMS(t) | ||
|
||
jsonWebKey, err := localKMS.Create(localkms.KeyTypeP384) | ||
require.NoError(t, err) | ||
|
||
didDoc, err := didkey.Create(jsonWebKey) | ||
require.NoError(t, err) | ||
require.NotNil(t, didDoc) | ||
}) | ||
t.Run("Nil JWK", func(t *testing.T) { | ||
didDoc, err := didkey.Create(nil) | ||
require.Contains(t, err.Error(), "jwk object cannot be null/nil/empty") | ||
require.Nil(t, didDoc) | ||
}) | ||
t.Run("Fail to get public key bytes", func(t *testing.T) { | ||
didDoc, err := didkey.Create(&api.JSONWebKey{JWK: &jwk.JWK{Crv: "Ed25519"}}) | ||
require.Contains(t, err.Error(), "unsupported public key type in kid ''") | ||
require.Nil(t, didDoc) | ||
}) | ||
t.Run("Fail to create verification method from JWK", func(t *testing.T) { | ||
didDoc, err := didkey.Create(&api.JSONWebKey{JWK: &jwk.JWK{}}) | ||
require.Contains(t, err.Error(), | ||
"convert JWK to public key bytes: unsupported public key type in kid ''") | ||
require.Nil(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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters