Skip to content

Commit

Permalink
rpc/nns: support NEP-18 addresses
Browse files Browse the repository at this point in the history
Signed-off-by: Roman Khimov <[email protected]>
  • Loading branch information
roman-khimov committed Mar 13, 2024
1 parent 91962f2 commit 067eb50
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 3 deletions.
12 changes: 9 additions & 3 deletions rpc/nns/hashes.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package nns

import (
"errors"
"strings"

"github.com/nspcc-dev/neo-go/pkg/core/state"
"github.com/nspcc-dev/neo-go/pkg/encoding/address"
Expand All @@ -18,6 +19,9 @@ const ID = 1
// allows to find contract hashes more easily.
const ContractTLD = "neofs"

// nep18Prefix is the string prefix containing NEP-18 address attribute name.
const nep18Prefix = "address="

// ContractStateGetter is the interface required for contract state resolution
// using a known contract ID.
type ContractStateGetter interface {
Expand All @@ -40,8 +44,10 @@ func InferHash(sg ContractStateGetter) (util.Uint160, error) {
// contract itself (it doesn't care which data is stored there). It assumes
// that contracts follow the [ContractTLD] convention, gets simple contract
// names (like "container" or "netmap") and extracts the hash for the
// respective NNS record in any of the formats (of which historically there's
// been a few).
// respective NNS record in any of the formats:
// - hex-encoded reversed hash (the oldest historically)
// - plain address string
// - attribute-based NEP-18 string
func (c *ContractReader) ResolveFSContract(name string) (util.Uint160, error) {
strs, err := c.Resolve(name+"."+ContractTLD, TXT)
if err != nil {
Expand All @@ -53,7 +59,7 @@ func (c *ContractReader) ResolveFSContract(name string) (util.Uint160, error) {
return h, nil
}

h, err = address.StringToUint160(strs[i])
h, err = address.StringToUint160(strings.TrimPrefix(strs[i], nep18Prefix))
if err == nil {
return h, nil
}
Expand Down
24 changes: 24 additions & 0 deletions rpc/nns/hashes_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,18 @@ func TestBaseErrors(t *testing.T) {
require.Error(t, err)

h := util.Uint160{1, 2, 3, 4, 5}

ti.res = &result.Invoke{
State: "HALT",
Stack: []stackitem.Item{
stackitem.Make([]stackitem.Item{
stackitem.Make("addr=" + address.Uint160ToString(h)), // Wrong prefix
}),
},
}
_, err = r.ResolveFSContract("blah")
require.Error(t, err)

ti.res = &result.Invoke{
State: "HALT",
Stack: []stackitem.Item{
Expand All @@ -112,4 +124,16 @@ func TestBaseErrors(t *testing.T) {
res, err = r.ResolveFSContract("blah")
require.NoError(t, err)
require.Equal(t, h, res)

ti.res = &result.Invoke{
State: "HALT",
Stack: []stackitem.Item{
stackitem.Make([]stackitem.Item{
stackitem.Make("address=" + address.Uint160ToString(h)), // NEP-18
}),
},
}
res, err = r.ResolveFSContract("blah")
require.NoError(t, err)
require.Equal(t, h, res)
}

0 comments on commit 067eb50

Please sign in to comment.