Skip to content

Commit

Permalink
Revert "identity refactor"
Browse files Browse the repository at this point in the history
This reverts commit 71364aa.
  • Loading branch information
Lodek committed Jun 14, 2024
1 parent b80aa03 commit 549c406
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 23 deletions.
3 changes: 2 additions & 1 deletion acp/identity/identity.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
15 changes: 9 additions & 6 deletions acp/identity/identity_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{},
}
}

Expand All @@ -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)
Expand Down
35 changes: 19 additions & 16 deletions acp/identity/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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
}
Expand All @@ -72,4 +74,5 @@ func (p *identityProvider) FromPrivateKey(privateKey *secp256k1.PrivateKey) (imm
PublicKey: pubKey,
PrivateKey: privateKey,
}), nil

}
1 change: 1 addition & 0 deletions acp/source_hub_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ type sourceHubBridge struct {
client sourceHubClient
}

var _ ACP = (*sourceHubBridge)(nil)
var _ ACP = (*sourceHubBridge)(nil)

func NewLocalACP() ACP {
Expand Down

0 comments on commit 549c406

Please sign in to comment.