Skip to content

Commit

Permalink
Ensip15 support
Browse files Browse the repository at this point in the history
  • Loading branch information
AnthonyAkentiev committed Jun 18, 2024
1 parent 817e49d commit 8ee79e5
Show file tree
Hide file tree
Showing 9 changed files with 106 additions and 28 deletions.
8 changes: 6 additions & 2 deletions account_abstraction/account_abstraction.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ func New() app.Component {
}

type anynsAA struct {
conf *config.Config
confAccount accountservice.Config
aaConfig config.AA
confContracts config.Contracts
Expand Down Expand Up @@ -70,6 +71,7 @@ type AccountAbstractionService interface {
}

func (aa *anynsAA) Init(a *app.App) (err error) {
aa.conf = a.MustComponent(config.CName).(*config.Config)
aa.confAccount = a.MustComponent(config.CName).(*config.Config).GetAccount()
aa.aaConfig = a.MustComponent(config.CName).(*config.Config).GetAA()
aa.confContracts = a.MustComponent(config.CName).(*config.Config).GetContracts()
Expand Down Expand Up @@ -364,7 +366,8 @@ func (aa *anynsAA) getDataNameRegister(ctx context.Context, fullName string, own
var id int = aa.getNextAlchemyRequestID()

// overwrites fullName
fullName, err = contracts.Normalize(fullName)
useEnsip15 := aa.conf.Ensip15Validation
fullName, err = contracts.NormalizeAnyName(fullName, useEnsip15)
if err != nil {
log.Error("failed to normalize name", zap.Error(err))
return nil, nil, err
Expand Down Expand Up @@ -714,7 +717,8 @@ func (aa *anynsAA) AdminNameRegister(ctx context.Context, in *nsp.NameRegisterRe
var chainID int64 = int64(aa.aaConfig.ChainID)

// overwrites in.FullName!
in.FullName, err = contracts.Normalize(in.FullName)
useEnsip15 := aa.conf.Ensip15Validation
in.FullName, err = contracts.NormalizeAnyName(in.FullName, useEnsip15)
if err != nil {
log.Error("failed to normalize name", zap.Error(err))
return "", err
Expand Down
10 changes: 8 additions & 2 deletions anynsaarpc/anynsaarpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ func New() app.Component {
}

type anynsAARpc struct {
conf *config.Config
confContracts config.Contracts
confAccount accountservice.Config
db dbservice.DbService
Expand All @@ -48,6 +49,7 @@ type anynsAARpc struct {
}

func (arpc *anynsAARpc) Init(a *app.App) (err error) {
arpc.conf = a.MustComponent(config.CName).(*config.Config)
arpc.confContracts = a.MustComponent(config.CName).(*config.Config).GetContracts()
arpc.db = a.MustComponent(dbservice.CName).(dbservice.DbService)
arpc.nodeConf = a.MustComponent(nodeconf.CName).(nodeconf.Service)
Expand Down Expand Up @@ -277,7 +279,9 @@ func (arpc *anynsAARpc) AdminFundGasOperations(ctx context.Context, in *nsp.Admi

func (arpc *anynsAARpc) GetDataNameRegister(ctx context.Context, in *nsp.NameRegisterRequest) (*nsp.GetDataNameRegisterResponse, error) {
// 1 - check params
err := verification.CheckRegisterParams(in)
useEnsip15 := arpc.conf.Ensip15Validation

err := verification.CheckRegisterParams(in, useEnsip15)
if err != nil {
log.Error("invalid parameters", zap.Error(err))
return nil, errors.New("invalid parameters")
Expand All @@ -301,7 +305,9 @@ func (arpc *anynsAARpc) GetDataNameRegister(ctx context.Context, in *nsp.NameReg

func (arpc *anynsAARpc) GetDataNameRegisterForSpace(ctx context.Context, in *nsp.NameRegisterForSpaceRequest) (*nsp.GetDataNameRegisterResponse, error) {
// 1 - check params
err := verification.CheckRegisterForSpaceParams(in)
useEnsip15 := arpc.conf.Ensip15Validation

err := verification.CheckRegisterForSpaceParams(in, useEnsip15)
if err != nil {
log.Error("invalid parameters", zap.Error(err))
return nil, errors.New("invalid parameters")
Expand Down
8 changes: 6 additions & 2 deletions anynsrpc/anynsrpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ func New() app.Component {

type anynsRpc struct {
cache cache.CacheService
conf *config.Config
confContracts config.Contracts
confAccount accountservice.Config
nodeConf nodeconf.NodeConf
Expand All @@ -49,6 +50,7 @@ type anynsRpc struct {

func (arpc *anynsRpc) Init(a *app.App) (err error) {
arpc.cache = a.MustComponent(cache.CName).(cache.CacheService)
arpc.conf = a.MustComponent(config.CName).(*config.Config)
arpc.confContracts = a.MustComponent(config.CName).(*config.Config).GetContracts()
arpc.confAccount = a.MustComponent(config.CName).(*config.Config).GetAccount()
arpc.nodeConf = a.MustComponent(nodeconf.CName).(nodeconf.NodeConf)
Expand All @@ -67,7 +69,8 @@ func (arpc *anynsRpc) Name() (name string) {

func (arpc *anynsRpc) IsNameAvailable(ctx context.Context, in *nsp.NameAvailableRequest) (*nsp.NameAvailableResponse, error) {
// 0 - normalize name (including .any suffix)
fullName, err := contracts.Normalize(in.FullName)
useEnsip15 := arpc.conf.Ensip15Validation
fullName, err := contracts.NormalizeAnyName(in.FullName, useEnsip15)
if err != nil {
log.Error("failed to normalize name", zap.Error(err))
return nil, err
Expand Down Expand Up @@ -178,7 +181,8 @@ func (arpc *anynsRpc) AdminNameRegisterSigned(ctx context.Context, in *nsp.NameR
}

// 3 - check all parameters
err = verification.CheckRegisterParams(&nrr)
useEnsip15 := arpc.conf.Ensip15Validation
err = verification.CheckRegisterParams(&nrr, useEnsip15)
if err != nil {
log.Error("invalid parameters", zap.Error(err))
return nil, err
Expand Down
9 changes: 9 additions & 0 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,15 @@ type Config struct {
Sentry Sentry `yaml:"sentry"`
// use mongo cache to read data from
ReadFromCache bool `yaml:"readFromCache"`

// if false -> will use old-school ENSIP1
//
// 1. ENSIP1 standard: ens-go v3.6.0 (current) is using it
// 2. ENSIP15 standard: that is an another standard for ENS namehashes
// that was accepted in June 2023.
//
// Current AnyNS (as of June 2024) implementation supports ENSIP1, ENSIP15
Ensip15Validation bool `yaml:"ensip15validation"`
}

func (c *Config) Init(a *app.App) (err error) {
Expand Down
41 changes: 34 additions & 7 deletions contracts/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ import (
"golang.org/x/net/idna"

"github.com/wealdtech/go-ens/v3"

"github.com/adraffy/ENSNormalize.go/ensip15"
)

var (
Expand Down Expand Up @@ -45,6 +47,29 @@ func normalize(input string) (string, error) {
return output, nil
}

func normalize15(input string) (string, error) {
// output, err := p.ToUnicode(input)
// if name has no .any suffix -> error
if len(input) < 4 || input[len(input)-4:] != ".any" {
return "", errors.New("name must have .any suffix")
}
// remove .any suffix
input = input[:len(input)-4]

// TODO: not a standard approach
ens := ensip15.New() // or ensip15.Shared()
output, err := ens.Normalize(input)

if err != nil {
return "", err
}

// add .any suffix
output += ".any"

return output, nil
}

func PeriodMonthsToTimestamp(registerPeriodMonths uint32) big.Int {
// default value!!!
if registerPeriodMonths == 0 {
Expand All @@ -61,23 +86,25 @@ func PeriodMonthsToTimestamp(registerPeriodMonths uint32) big.Int {
return regTime
}

func Normalize(name string) (string, error) {
//return ens.Normalize(name)

return normalize(name)
func NormalizeAnyName(name string, useEnsip15 bool) (string, error) {
if useEnsip15 {
return normalize15(name)
} else {
//return ens.Normalize(name)
return normalize(name)
}
}

// NameHash generates a hash from a name that can be used to
// look up the name in ENS
func NameHash(name string) (hash [32]byte, err error) {
// redirect to go-ens library

// 1. ENSIP1 standard: ens-go v3.6.0 is using it
// 1. ENSIP1 standard: ens-go v3.6.0 (current) is using it
// 2. ENSIP15 standard: that is an another standard for ENS namehashes
// that was accepted in June 2023.
//
// Current AnyNS (as of February 2024) implementation does not support it
//
// Current AnyNS (as of June 2024) implementation supports ENSIP1, ENSIP15
// https://eips.ethereum.org/EIPS/eip-137 (ENSIP1) grammar:
//
// <domain> ::= <label> | <domain> "." <label>
Expand Down
39 changes: 33 additions & 6 deletions contracts/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,28 +87,55 @@ func TestNameHash1_ENSIP1(t *testing.T) {
assert.Equal(t, "0xad4f933a04969d30ef4d7caa6ff10c8af110b25045454179e01999cc69cc34c8", hexOut)
}

func TestNormalize(t *testing.T) {
func TestNormalizeEnsip1(t *testing.T) {
useEnsip15 := false

// 1
_, err := Normalize("")
_, err := NormalizeAnyName("", useEnsip15)
assert.Error(t, err)

// 2
out, err := Normalize("Foo.any")
out, err := NormalizeAnyName("Foo.any", useEnsip15)
assert.NoError(t, err)
assert.Equal(t, "foo.any", out)

// 3
out, err = Normalize("❶❷❸❹❺❻❼❽❾❿.any")
out, err = NormalizeAnyName("❶❷❸❹❺❻❼❽❾❿.any", useEnsip15)
assert.NoError(t, err)
assert.Equal(t, "❶❷❸❹❺❻❼❽❾❿.any", out)

// 4
out, err = Normalize("fоо.any")
out, err = NormalizeAnyName("fоо.any", useEnsip15)
assert.NoError(t, err)
assert.Equal(t, "fоо.any", out)

// 5
_, err = Normalize("hello world.any")
_, err = NormalizeAnyName("hello world.any", useEnsip15)
assert.Error(t, err)
}

func TestNormalizeEnsip15(t *testing.T) {
useEnsip15 := true

// 1
_, err := NormalizeAnyName("", useEnsip15)
assert.Error(t, err)

// 2
out, err := NormalizeAnyName("Foo.any", useEnsip15)
assert.NoError(t, err)
assert.Equal(t, "foo.any", out)

// 3 - HERE!
_, err = NormalizeAnyName("❶❷❸❹❺❻❼❽❾❿.any", useEnsip15)
assert.Error(t, err)

// 4
_, err = NormalizeAnyName("fоо.any", useEnsip15)
assert.Error(t, err)

// 5
out, err = NormalizeAnyName("hello world.any", useEnsip15)
assert.Error(t, err)
}

Expand Down
5 changes: 2 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
module github.com/anyproto/any-ns-node

go 1.22

toolchain go1.22.1
go 1.22.4

require (
github.com/adraffy/ENSNormalize.go v0.0.1
github.com/ahmetb/govvv v0.3.0
github.com/anyproto/alchemy-aa-sdk v0.0.1
github.com/anyproto/any-sync v0.4.3
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ github.com/StackExchange/wmi v1.2.1 h1:VIkavFPXSjcnS+O8yTq7NI32k0R5Aj+v39y29VYDO
github.com/StackExchange/wmi v1.2.1/go.mod h1:rcmrprowKIVzvc+NUiLncP2uuArMWLCbu9SBzvHz7e8=
github.com/VictoriaMetrics/fastcache v1.12.1 h1:i0mICQuojGDL3KblA7wUNlY5lOK6a4bwt3uRKnkZU40=
github.com/VictoriaMetrics/fastcache v1.12.1/go.mod h1:tX04vaqcNoQeGLD+ra5pU5sWkuxnzWhEzLwhP9w653o=
github.com/adraffy/ENSNormalize.go v0.0.1 h1:ZqcS0qtpSL+dNd6tIFMqqKufjCi1njSFkhHYYw3a/2k=
github.com/adraffy/ENSNormalize.go v0.0.1/go.mod h1:LzQZRKQa2logPVgpDe1ow1PfFa2XANk0kIZqFqozzKs=
github.com/aead/siphash v1.0.1/go.mod h1:Nywa3cDsYNNK3gaciGTWPwHt0wlpNV15vwmswBAUSII=
github.com/ahmetb/govvv v0.3.0 h1:YGLGwEyiUwHFy5eh/RUhdupbuaCGBYn5T5GWXp+WJB0=
github.com/ahmetb/govvv v0.3.0/go.mod h1:4WRFpdWtc/YtKgPFwa1dr5+9hiRY5uKAL08bOlxOR6s=
Expand Down
12 changes: 6 additions & 6 deletions verification/verification.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@ import (

var log = logger.NewNamed("verification")

func CheckRegisterParams(in *nsp.NameRegisterRequest) error {
func CheckRegisterParams(in *nsp.NameRegisterRequest, useEnsip15 bool) error {
// 1 - check name
if !CheckName(in.FullName) {
if !CheckName(in.FullName, useEnsip15) {
log.Error("invalid name", zap.String("name", in.FullName))
return errors.New("invalid name")
}
Expand All @@ -39,9 +39,9 @@ func CheckRegisterParams(in *nsp.NameRegisterRequest) error {
return nil
}

func CheckRegisterForSpaceParams(in *nsp.NameRegisterForSpaceRequest) error {
func CheckRegisterForSpaceParams(in *nsp.NameRegisterForSpaceRequest, useEnsip15 bool) error {
// 1 - check name
if !CheckName(in.FullName) {
if !CheckName(in.FullName, useEnsip15) {
log.Error("invalid name", zap.String("name", in.FullName))
return errors.New("invalid name")
}
Expand Down Expand Up @@ -72,7 +72,7 @@ func CheckRegisterForSpaceParams(in *nsp.NameRegisterForSpaceRequest) error {
return nil
}

func CheckName(name string) bool {
func CheckName(name string, useEnsip15 bool) bool {
// get name parts
parts := strings.Split(name, ".")
if len(parts) != 2 {
Expand All @@ -97,7 +97,7 @@ func CheckName(name string) bool {
// use the same rules as in ENS
// if can not normalize -> then we won't be able to calculate ens.NameHash() later
// because it uses the same normalization procedure
_, err := contracts.Normalize(name)
_, err := contracts.NormalizeAnyName(name, useEnsip15)
if err != nil {
log.Error("failed to normalize name", zap.String("Name", name), zap.Error(err))
return false
Expand Down

0 comments on commit 8ee79e5

Please sign in to comment.