[#793] adm: Support new FrostFS ID contract

Signed-off-by: Evgenii Stratonikov <e.stratonikov@yadro.com>
This commit is contained in:
Evgenii Stratonikov 2023-11-08 16:36:55 +03:00 committed by Evgenii Stratonikov
parent b62008daca
commit f871f5cc6c
3 changed files with 95 additions and 3 deletions

View file

@ -0,0 +1,41 @@
package morph
import (
"fmt"
"github.com/nspcc-dev/neo-go/pkg/crypto/keys"
"github.com/nspcc-dev/neo-go/pkg/encoding/address"
"github.com/nspcc-dev/neo-go/pkg/smartcontract"
"github.com/nspcc-dev/neo-go/pkg/util"
"github.com/spf13/viper"
)
// neo-go doesn't support []util.Uint160 type:
// https://github.com/nspcc-dev/neo-go/blob/v0.103.0/pkg/smartcontract/parameter.go#L262
// Thus, return []smartcontract.Parameter.
func getFrostfsIDAuthorizedKeys(v *viper.Viper) ([]smartcontract.Parameter, error) {
var res []smartcontract.Parameter
ks := v.GetStringSlice(frostfsIDAuthorizedKeysConfigKey)
for i := range ks {
h, err := address.StringToUint160(ks[i])
if err == nil {
res = append(res, smartcontract.Parameter{Type: smartcontract.Hash160Type, Value: h})
continue
}
h, err = util.Uint160DecodeStringLE(ks[i])
if err == nil {
res = append(res, smartcontract.Parameter{Type: smartcontract.Hash160Type, Value: h})
continue
}
pk, err := keys.NewPublicKeyFromString(ks[i])
if err == nil {
res = append(res, smartcontract.Parameter{Type: smartcontract.Hash160Type, Value: pk.GetScriptHash()})
continue
}
return nil, fmt.Errorf("frostfsid: #%d item in authorized_keys is invalid: '%s'", i, ks[i])
}
return res, nil
}

View file

@ -0,0 +1,45 @@
package morph
import (
"encoding/hex"
"testing"
"github.com/nspcc-dev/neo-go/pkg/crypto/keys"
"github.com/nspcc-dev/neo-go/pkg/encoding/address"
"github.com/nspcc-dev/neo-go/pkg/smartcontract"
"github.com/spf13/viper"
"github.com/stretchr/testify/require"
)
func TestFrostfsIDConfig(t *testing.T) {
pks := make([]*keys.PrivateKey, 4)
for i := range pks {
pk, err := keys.NewPrivateKey()
require.NoError(t, err)
pks[i] = pk
}
v := viper.New()
v.Set("frostfsid.authorized_keys", []string{
pks[0].GetScriptHash().StringLE(),
address.Uint160ToString(pks[1].GetScriptHash()),
hex.EncodeToString(pks[2].PublicKey().UncompressedBytes()),
hex.EncodeToString(pks[3].PublicKey().Bytes()),
})
actual, err := getFrostfsIDAuthorizedKeys(v)
require.NoError(t, err)
require.Equal(t, len(pks), len(actual))
for i := range pks {
require.Equal(t, smartcontract.Hash160Type, actual[i].Type)
require.Equal(t, pks[i].GetScriptHash(), actual[i].Value)
}
t.Run("bad key", func(t *testing.T) {
v := viper.New()
v.Set("frostfsid.authorized_keys", []string{"abc"})
_, err := getFrostfsIDAuthorizedKeys(v)
require.Error(t, err)
})
}

View file

@ -31,6 +31,7 @@ import (
"github.com/nspcc-dev/neo-go/pkg/vm/emit" "github.com/nspcc-dev/neo-go/pkg/vm/emit"
"github.com/nspcc-dev/neo-go/pkg/vm/opcode" "github.com/nspcc-dev/neo-go/pkg/vm/opcode"
"github.com/nspcc-dev/neo-go/pkg/vm/stackitem" "github.com/nspcc-dev/neo-go/pkg/vm/stackitem"
"github.com/spf13/viper"
) )
const ( const (
@ -46,6 +47,8 @@ const (
proxyContract = "proxy" proxyContract = "proxy"
) )
const frostfsIDAuthorizedKeysConfigKey = "frostfsid.authorized_keys"
var ( var (
contractList = []string{ contractList = []string{
balanceContract, balanceContract,
@ -535,9 +538,12 @@ func (c *initializeContext) getContractDeployData(ctrName string, keysParam []an
nnsCs.Hash, nnsCs.Hash,
"container") "container")
case frostfsIDContract: case frostfsIDContract:
items = append(items, hs, err := getFrostfsIDAuthorizedKeys(viper.GetViper())
c.Contracts[netmapContract].Hash, if err != nil {
c.Contracts[containerContract].Hash) panic(err)
}
items = append(items, hs)
case netmapContract: case netmapContract:
md := getDefaultNetmapContractConfigMap() md := getDefaultNetmapContractConfigMap()
if method == updateMethodName { if method == updateMethodName {