diff --git a/acp/identity/identity.go b/acp/identity/identity.go index f708099c2f..fef08b905c 100644 --- a/acp/identity/identity.go +++ b/acp/identity/identity.go @@ -43,5 +43,6 @@ func FromPublicKey(publicKey *secp256k1.PublicKey) (immutable.Option[Identity], // DIDFromPublicKey returns the unique address of the given public key. func DIDFromPublicKey(publicKey *secp256k1.PublicKey) (string, error) { - return generateDID(publicKey, getDefaultDIDProducer()) + provider := defaultDIDProvider{} + return provider.DIDFromSecp256k1(publicKey) } diff --git a/acp/identity/identity_test.go b/acp/identity/identity_test.go index 7e50caf2d3..b3714bfd2f 100644 --- a/acp/identity/identity_test.go +++ b/acp/identity/identity_test.go @@ -14,20 +14,23 @@ 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" ) -func mockedDIDProducer(crypto.KeyType, []byte) (*key.DIDKey, error) { - return nil, fmt.Errorf("some did generation error") +var _ didProvider = (*mockedDIDProvider)(nil) + +// mockedDIDProvider implemented didProvider but always fails +type mockedDIDProvider struct{} + +func (p *mockedDIDProvider) DIDFromSecp256k1(key *secp256k1.PublicKey) (string, error) { + return "", fmt.Errorf("some did generation error") } // newFailableIdentityProvider returns an identityProvider that always fails func newFailableIdentityProvider() identityProvider { return identityProvider{ - producer: mockedDIDProducer, + didProv: &mockedDIDProvider{}, } } @@ -43,7 +46,7 @@ func Test_DIDFromPublicKey_ProducesDIDForPublicKey(t *testing.T) { func Test_DIDFromPublicKey_ReturnsErrorWhenProducerFails(t *testing.T) { pubKey := &secp256k1.PublicKey{} - did, err := generateDID(pubKey, mockedDIDProducer) + did, err := DIDFromPublicKey(pubKey) require.Empty(t, did) require.ErrorIs(t, err, ErrDIDCreation) diff --git a/acp/identity/provider.go b/acp/identity/provider.go index 35544bda03..b776e9c018 100644 --- a/acp/identity/provider.go +++ b/acp/identity/provider.go @@ -17,39 +17,41 @@ import ( "github.com/sourcenetwork/immutable" ) -// didProducerFn produces a did:key from public keys -type didProducerFn func(crypto.KeyType, []byte) (*key.DIDKey, error) +var _ didProvider = (*defaultDIDProvider)(nil) -// getDefaultDIDProducer returns the package default didProducer -func getDefaultDIDProducer() didProducerFn { return key.CreateDIDKey } +// didProvider produces a did:key from public keys +type didProvider interface { + // DIDFromSecp256k1 returns a did:key from a secp256k1 pub key + DIDFromSecp256k1(key *secp256k1.PublicKey) (string, error) +} -// generateDID receives a public key, a didProducerFn and returns a did:key string or an error -func generateDID(pubKey *secp256k1.PublicKey, producer didProducerFn) (string, error) { - bytes := pubKey.SerializeUncompressed() - didKey, err := producer(crypto.SECP256k1, bytes) +// defaultDIDProvider implements didProvier +type defaultDIDProvider struct{} +func (p *defaultDIDProvider) DIDFromSecp256k1(pubKey *secp256k1.PublicKey) (string, error) { + bytes := pubKey.SerializeUncompressed() + did, err := key.CreateDIDKey(crypto.SECP256k1, bytes) if err != nil { return "", NewErrDIDCreation(err, "secp256k1", bytes) } - - return didKey.String(), err + return did.String(), nil } -// identityProvider provides Identity from key material +// identityProvider wraps a didProvider and constructs Identity from key material type identityProvider struct { - producer didProducerFn + didProv didProvider } -// newIdentityProvider returns an identityProvider which uses the defaultDIDProducer +// newIdentityProvider returns an identityProvider which uses the defaultDIDProvider func newIdentityProvider() *identityProvider { return &identityProvider{ - producer: getDefaultDIDProducer(), + didProv: &defaultDIDProvider{}, } } // FromPublicKey returns a new identity using the given public key. func (p *identityProvider) FromPublicKey(publicKey *secp256k1.PublicKey) (immutable.Option[Identity], error) { - did, err := generateDID(publicKey, p.producer) + did, err := p.didProv.DIDFromSecp256k1(publicKey) if err != nil { return None, err } @@ -62,7 +64,7 @@ func (p *identityProvider) FromPublicKey(publicKey *secp256k1.PublicKey) (immuta // FromPrivateKey returns a new identity using the given private key. func (p *identityProvider) FromPrivateKey(privateKey *secp256k1.PrivateKey) (immutable.Option[Identity], error) { pubKey := privateKey.PubKey() - did, err := generateDID(pubKey, p.producer) + did, err := p.didProv.DIDFromSecp256k1(pubKey) if err != nil { return None, err } @@ -72,4 +74,5 @@ func (p *identityProvider) FromPrivateKey(privateKey *secp256k1.PrivateKey) (imm PublicKey: pubKey, PrivateKey: privateKey, }), nil + } diff --git a/acp/source_hub_client.go b/acp/source_hub_client.go index 30cf773c96..2e18241103 100644 --- a/acp/source_hub_client.go +++ b/acp/source_hub_client.go @@ -90,6 +90,7 @@ type sourceHubBridge struct { client sourceHubClient } +var _ ACP = (*sourceHubBridge)(nil) var _ ACP = (*sourceHubBridge)(nil) func NewLocalACP() ACP {