From 6dbd7826a86612433b563373660a588ea5c8bc82 Mon Sep 17 00:00:00 2001 From: Bruno Gomes Date: Fri, 14 Jun 2024 13:34:12 -0300 Subject: [PATCH] producer refactor --- acp/identity/identity.go | 20 +++++++++++++++++--- acp/identity/identity_test.go | 27 ++++++++------------------- acp/source_hub_client.go | 1 - 3 files changed, 25 insertions(+), 23 deletions(-) diff --git a/acp/identity/identity.go b/acp/identity/identity.go index fef08b905c..0bda231558 100644 --- a/acp/identity/identity.go +++ b/acp/identity/identity.go @@ -11,10 +11,15 @@ package identity import ( + "github.com/cyware/ssi-sdk/crypto" + "github.com/cyware/ssi-sdk/did/key" "github.com/decred/dcrd/dcrec/secp256k1/v4" "github.com/sourcenetwork/immutable" ) +// didProducer generates a did:key from a public key +type didProducer = func(crypto.KeyType, []byte) (*key.DIDKey, error) + // None specifies an anonymous actor. var None = immutable.None[Identity]() @@ -41,8 +46,17 @@ func FromPublicKey(publicKey *secp256k1.PublicKey) (immutable.Option[Identity], return newIdentityProvider().FromPublicKey(publicKey) } -// DIDFromPublicKey returns the unique address of the given public key. +// DIDFromPublicKey returns a did:key generated from the the given public key. func DIDFromPublicKey(publicKey *secp256k1.PublicKey) (string, error) { - provider := defaultDIDProvider{} - return provider.DIDFromSecp256k1(publicKey) + return didFromPublicKey(publicKey, key.CreateDIDKey) +} + +// didFromPublicKey produces a did from a secp256k1 key and a producer function +func didFromPublicKey(publicKey *secp256k1.PublicKey, producer didProducer) (string, error) { + bytes := publicKey.SerializeUncompressed() + did, err := producer(crypto.SECP256k1, bytes) + if err != nil { + return "", NewErrDIDCreation(err, "secp256k1", bytes) + } + return did.String(), nil } diff --git a/acp/identity/identity_test.go b/acp/identity/identity_test.go index b3714bfd2f..88e490fcdd 100644 --- a/acp/identity/identity_test.go +++ b/acp/identity/identity_test.go @@ -14,6 +14,8 @@ import ( "fmt" "testing" + "github.com/cyware/ssi-sdk/crypto" + "github.com/cyware/ssi-sdk/did/key" "github.com/decred/dcrd/dcrec/secp256k1/v4" "github.com/stretchr/testify/require" ) @@ -44,28 +46,15 @@ func Test_DIDFromPublicKey_ProducesDIDForPublicKey(t *testing.T) { require.NoError(t, err) } -func Test_DIDFromPublicKey_ReturnsErrorWhenProducerFails(t *testing.T) { - pubKey := &secp256k1.PublicKey{} - did, err := DIDFromPublicKey(pubKey) - - require.Empty(t, did) - require.ErrorIs(t, err, ErrDIDCreation) -} +func Test_didFromPublicKey_ReturnsErrorWhenProducerFails(t *testing.T) { + mockedProducer := func(crypto.KeyType, []byte) (*key.DIDKey, error) { + return nil, fmt.Errorf("did generation err") + } -func Test_FromPublicKey_ProducerFailureCausesError(t *testing.T) { pubKey := &secp256k1.PublicKey{} - provider := newFailableIdentityProvider() - identity, err := provider.FromPublicKey(pubKey) - require.Equal(t, None, identity) - require.ErrorIs(t, err, ErrDIDCreation) -} + did, err := didFromPublicKey(pubKey, mockedProducer) -func Test_FromPrivateKey_ProducerFailureCausesError(t *testing.T) { - key := &secp256k1.PrivateKey{} - provider := newFailableIdentityProvider() - identity, err := provider.FromPrivateKey(key) - - require.Equal(t, None, identity) + require.Empty(t, did) require.ErrorIs(t, err, ErrDIDCreation) } diff --git a/acp/source_hub_client.go b/acp/source_hub_client.go index 2e18241103..30cf773c96 100644 --- a/acp/source_hub_client.go +++ b/acp/source_hub_client.go @@ -90,7 +90,6 @@ type sourceHubBridge struct { client sourceHubClient } -var _ ACP = (*sourceHubBridge)(nil) var _ ACP = (*sourceHubBridge)(nil) func NewLocalACP() ACP {