diff --git a/impl/internal/did/did.go b/impl/internal/did/did.go index a834fae6..8dcbc4e1 100644 --- a/impl/internal/did/did.go +++ b/impl/internal/did/did.go @@ -4,6 +4,7 @@ import ( "crypto/ed25519" "encoding/base64" "fmt" + "math" "strconv" "strings" @@ -74,6 +75,24 @@ type VerificationMethod struct { Purposes []did.PublicKeyPurpose `json:"purposes"` } +func GenerateVanityDIDDHT(prefix string, opts CreateDIDDHTOpts) (ed25519.PrivateKey, *did.Document, error) { + // generate the identity key + for i := 0; i < math.MaxInt32; i++ { + pubKey, privKey, err := crypto.GenerateEd25519Key() + if err != nil { + return nil, nil, err + } + + id := GetDIDDHTIdentifier(pubKey) + + if strings.HasPrefix(id, prefix) { + doc, err := CreateDIDDHTDID(pubKey, opts) + return privKey, doc, err + } + } + return nil, nil, fmt.Errorf("failed to generate vanity did:dht identifier with prefix %s", prefix) +} + // GenerateDIDDHT generates a did:dht identifier given a set of options func GenerateDIDDHT(opts CreateDIDDHTOpts) (ed25519.PrivateKey, *did.Document, error) { // generate the identity key diff --git a/impl/internal/did/did_test.go b/impl/internal/did/did_test.go index 378a04ee..9c1ee606 100644 --- a/impl/internal/did/did_test.go +++ b/impl/internal/did/did_test.go @@ -3,6 +3,7 @@ package did import ( "crypto/ed25519" "testing" + "time" "github.com/goccy/go-json" @@ -309,3 +310,16 @@ func TestVectors(t *testing.T) { } }) } + +func TestVanityDID(t *testing.T) { + now := time.Now() + pk, doc, err := GenerateVanityDIDDHT("gabe", CreateDIDDHTOpts{}) + require.NoError(t, err) + require.NotEmpty(t, pk) + require.NotEmpty(t, doc) + + b, _ := json.Marshal(doc) + println(string(b)) + + println(time.Since(now).String()) +} diff --git a/impl/pkg/service/gateway_test.go b/impl/pkg/service/gateway_test.go new file mode 100644 index 00000000..5dd6fe71 --- /dev/null +++ b/impl/pkg/service/gateway_test.go @@ -0,0 +1,34 @@ +package service + +import ( + "testing" + + "github.com/stretchr/testify/require" + + "github.com/TBD54566975/did-dht-method/config" + "github.com/TBD54566975/did-dht-method/pkg/storage" +) + +func TestGatewayService(t *testing.T) { + svc := newGatewayService(t) + require.NotEmpty(t, svc) + + t.Run("Publish and Get a DID", func(t *testing.T) { + + }) +} + +func newGatewayService(t *testing.T) GatewayService { + defaultConfig := config.GetDefaultConfig() + db, err := storage.NewStorage(defaultConfig.ServerConfig.DBFile) + require.NoError(t, err) + require.NotEmpty(t, db) + pkarrService, err := NewPkarrService(&defaultConfig, db) + require.NoError(t, err) + require.NotEmpty(t, pkarrService) + + gatewayService, err := NewGatewayService(&defaultConfig, db, pkarrService) + require.NoError(t, err) + require.NotEmpty(t, gatewayService) + return *gatewayService +} diff --git a/impl/pkg/service/pkarr_test.go b/impl/pkg/service/pkarr_test.go index 9b4372a2..3f67a853 100644 --- a/impl/pkg/service/pkarr_test.go +++ b/impl/pkg/service/pkarr_test.go @@ -13,8 +13,8 @@ import ( "github.com/TBD54566975/did-dht-method/pkg/storage" ) -func TestPKARRService(t *testing.T) { - svc := newPKARRService(t) +func TestPkarrService(t *testing.T) { + svc := newPkarrService(t) require.NotEmpty(t, svc) t.Run("test put bad record", func(t *testing.T) { @@ -100,7 +100,7 @@ func TestPKARRService(t *testing.T) { }) } -func newPKARRService(t *testing.T) PkarrService { +func newPkarrService(t *testing.T) PkarrService { defaultConfig := config.GetDefaultConfig() db, err := storage.NewStorage(defaultConfig.ServerConfig.DBFile) require.NoError(t, err)