From fee2f5a330df24cb654e02be5d31889d2f42451c Mon Sep 17 00:00:00 2001 From: Pavel Karpy Date: Fri, 21 Jan 2022 15:15:10 +0300 Subject: [PATCH] [#1100] *: Adopt new SDK's `owner.ID` API Signed-off-by: Pavel Karpy --- .../internal/modules/morph/subnet.go | 8 +----- cmd/neofs-cli/modules/accounting.go | 5 +--- cmd/neofs-cli/modules/container.go | 5 +--- cmd/neofs-cli/modules/object.go | 8 +----- cmd/neofs-node/config.go | 8 ++---- cmd/neofs-node/object.go | 7 ------ go.mod | 2 +- go.sum | Bin 93109 -> 93109 bytes pkg/core/container/fmt_test.go | 5 ++-- pkg/core/object/fmt.go | 9 +------ pkg/core/object/fmt_test.go | 23 ++++++------------ pkg/innerring/processors/container/common.go | 12 +-------- .../processors/settlement/audit/calculate.go | 10 +------- .../processors/settlement/basic/context.go | 11 +++------ pkg/innerring/settlement.go | 10 +------- pkg/innerring/subnet.go | 7 +----- .../engine/engine_test.go | 5 +--- pkg/local_object_storage/metabase/db_test.go | 5 +--- pkg/local_object_storage/shard/shard_test.go | 5 +--- pkg/services/object/acl/acl.go | 7 +----- pkg/services/object/acl/classifier.go | 10 +------- 21 files changed, 30 insertions(+), 132 deletions(-) diff --git a/cmd/neofs-adm/internal/modules/morph/subnet.go b/cmd/neofs-adm/internal/modules/morph/subnet.go index 961e2e1fd..9430f39b7 100644 --- a/cmd/neofs-adm/internal/modules/morph/subnet.go +++ b/cmd/neofs-adm/internal/modules/morph/subnet.go @@ -192,12 +192,6 @@ var cmdSubnetCreate = &cobra.Command{ return fmt.Errorf("read private key: %w", err) } - // calculate wallet address from key - n3Wallet, err := owner.NEO3WalletFromPublicKey(&key.PrivateKey.PublicKey) - if err != nil { - return fmt.Errorf("wallet from key: %w", err) - } - // generate subnet ID and marshal it var ( id subnetid.ID @@ -220,7 +214,7 @@ var cmdSubnetCreate = &cobra.Command{ } // declare creator ID and encode it - creator := *owner.NewIDFromNeo3Wallet(n3Wallet) + creator := *owner.NewIDFromPublicKey(&key.PrivateKey.PublicKey) // fill subnet info and encode it var info subnet.Info diff --git a/cmd/neofs-cli/modules/accounting.go b/cmd/neofs-cli/modules/accounting.go index 5e0cc50b3..e7c9f2848 100644 --- a/cmd/neofs-cli/modules/accounting.go +++ b/cmd/neofs-cli/modules/accounting.go @@ -41,10 +41,7 @@ var accountingBalanceCmd = &cobra.Command{ exitOnErr(cmd, err) if balanceOwner == "" { - wallet, err := owner.NEO3WalletFromPublicKey(&key.PublicKey) - exitOnErr(cmd, err) - - oid = owner.NewIDFromNeo3Wallet(wallet) + oid = owner.NewIDFromPublicKey(&key.PublicKey) } else { oid, err = ownerFromString(balanceOwner) exitOnErr(cmd, err) diff --git a/cmd/neofs-cli/modules/container.go b/cmd/neofs-cli/modules/container.go index 90cfa7380..61028c25d 100644 --- a/cmd/neofs-cli/modules/container.go +++ b/cmd/neofs-cli/modules/container.go @@ -116,10 +116,7 @@ var listContainersCmd = &cobra.Command{ exitOnErr(cmd, err) if containerOwner == "" { - wallet, err := owner.NEO3WalletFromPublicKey(&key.PublicKey) - exitOnErr(cmd, err) - - oid = owner.NewIDFromNeo3Wallet(wallet) + oid = owner.NewIDFromPublicKey(&key.PublicKey) } else { oid, err = ownerFromString(containerOwner) exitOnErr(cmd, err) diff --git a/cmd/neofs-cli/modules/object.go b/cmd/neofs-cli/modules/object.go index 473dda9d8..281d5f238 100644 --- a/cmd/neofs-cli/modules/object.go +++ b/cmd/neofs-cli/modules/object.go @@ -594,13 +594,7 @@ func getObjectHash(cmd *cobra.Command, _ []string) { } func getOwnerID(key *ecdsa.PrivateKey) (*owner.ID, error) { - w, err := owner.NEO3WalletFromPublicKey(&key.PublicKey) - if err != nil { - return nil, err - } - ownerID := owner.NewID() - ownerID.SetNeo3Wallet(w) - return ownerID, nil + return owner.NewIDFromPublicKey(&key.PublicKey), nil } var searchUnaryOpVocabulary = map[string]object.SearchMatchType{ diff --git a/cmd/neofs-node/config.go b/cmd/neofs-node/config.go index ed54d2243..1e6e6def4 100644 --- a/cmd/neofs-node/config.go +++ b/cmd/neofs-node/config.go @@ -228,15 +228,11 @@ func initCfg(path string) *cfg { key := nodeconfig.Key(appCfg) - neo3Wallet, err := owner.NEO3WalletFromPublicKey(&key.PrivateKey.PublicKey) - fatalOnErr(err) - - ownerIDFromKey := owner.NewID() - ownerIDFromKey.SetNeo3Wallet(neo3Wallet) + ownerIDFromKey := owner.NewIDFromPublicKey(&key.PrivateKey.PublicKey) var logPrm logger.Prm - err = logPrm.SetLevelString( + err := logPrm.SetLevelString( loggerconfig.Level(appCfg), ) fatalOnErr(err) diff --git a/cmd/neofs-node/object.go b/cmd/neofs-node/object.go index 410eb3823..b11517c3b 100644 --- a/cmd/neofs-node/object.go +++ b/cmd/neofs-node/object.go @@ -3,7 +3,6 @@ package main import ( "bytes" "context" - "crypto/ecdsa" "fmt" "github.com/nspcc-dev/neofs-api-go/v2/object" @@ -183,12 +182,6 @@ func (x *coreClientConstructor) Get(info coreclient.NodeInfo) (coreclient.MultiA func initObjectService(c *cfg) { ls := c.cfgObject.cfgLocalStorage.localStorage keyStorage := util.NewKeyStorage(&c.key.PrivateKey, c.privateTokenStore, c.cfgNetmap.state) - nodeOwner := owner.NewID() - - neo3Wallet, err := owner.NEO3WalletFromPublicKey((*ecdsa.PublicKey)(c.key.PublicKey())) - fatalOnErr(err) - - nodeOwner.SetNeo3Wallet(neo3Wallet) clientConstructor := &reputationClientConstructor{ log: c.log, diff --git a/go.mod b/go.mod index 6c54493a8..53ecbf928 100644 --- a/go.mod +++ b/go.mod @@ -13,7 +13,7 @@ require ( github.com/nspcc-dev/hrw v1.0.9 github.com/nspcc-dev/neo-go v0.98.0 github.com/nspcc-dev/neofs-api-go/v2 v2.11.2-0.20220114101721-227a871a04ac - github.com/nspcc-dev/neofs-sdk-go v0.0.0-20220119080627-f83ff628fb19 + github.com/nspcc-dev/neofs-sdk-go v0.0.0-20220121080144-596774ce5bd3 github.com/nspcc-dev/tzhash v1.4.0 github.com/panjf2000/ants/v2 v2.4.0 github.com/paulmach/orb v0.2.2 diff --git a/go.sum b/go.sum index 36b0eb4790263c76b35c92b51dcd630df5e8afad..1cef2c1f1915dbb1d7d923c70c2fc22f5af63d35 100644 GIT binary patch delta 120 zcmdmboptMV)(r`LE=Gn176yhUCc37UX6EK5$*HDEDaHyJhE_f~`6c@1$$5e0&S@FJ zMR|TcE*Yhf72X9ACXSWb6^UjQ=1zuLo<^yOp_3=JiBCS?%{KW$ub_mHfgx5iH^1sT GeFp%1hbZd+ delta 120 zcmdmboptMV)(r`LE{2u{76xWU=DKMX#%XD0MiyyFhL#E$hE`#v{#7L&j%8(~22tMT zdFf`ZnK?NbIj*I79+k!>A%R7v`i92Y25G^ep_3=JiBCS?%{KW$ub_mHfgx5iH^1sT GeFp%P$tg4d diff --git a/pkg/core/container/fmt_test.go b/pkg/core/container/fmt_test.go index b4a5abf51..7800990ca 100644 --- a/pkg/core/container/fmt_test.go +++ b/pkg/core/container/fmt_test.go @@ -26,10 +26,9 @@ func TestCheckFormat(t *testing.T) { require.Error(t, CheckFormat(c)) - wallet, err := owner.NEO3WalletFromPublicKey(&test.DecodeKey(-1).PublicKey) - require.NoError(t, err) + oid := owner.NewIDFromPublicKey(&test.DecodeKey(-1).PublicKey) - c.SetOwnerID(owner.NewIDFromNeo3Wallet(wallet)) + c.SetOwnerID(oid) // set incorrect nonce cV2 := c.ToV2() diff --git a/pkg/core/object/fmt.go b/pkg/core/object/fmt.go index e179bd573..b9e6cef62 100644 --- a/pkg/core/object/fmt.go +++ b/pkg/core/object/fmt.go @@ -122,14 +122,7 @@ func (v *FormatValidator) checkOwnerKey(id *owner.ID, key []byte) error { return err } - wallet, err := owner.NEO3WalletFromPublicKey((*ecdsa.PublicKey)(pub)) - if err != nil { - // TODO: check via NeoFSID - return err - } - - id2 := owner.NewID() - id2.SetNeo3Wallet(wallet) + id2 := owner.NewIDFromPublicKey((*ecdsa.PublicKey)(pub)) if !id.Equal(id2) { return fmt.Errorf("(%T) different owner identifiers %s/%s", v, id, id2) diff --git a/pkg/core/object/fmt_test.go b/pkg/core/object/fmt_test.go index 358f15137..855121c42 100644 --- a/pkg/core/object/fmt_test.go +++ b/pkg/core/object/fmt_test.go @@ -34,16 +34,10 @@ func testObjectID(t *testing.T) *object.ID { return id } -func blankValidObject(t *testing.T, key *ecdsa.PrivateKey) *RawObject { - wallet, err := owner.NEO3WalletFromPublicKey(&key.PublicKey) - require.NoError(t, err) - - ownerID := owner.NewID() - ownerID.SetNeo3Wallet(wallet) - +func blankValidObject(key *ecdsa.PrivateKey) *RawObject { obj := NewRaw() obj.SetContainerID(cidtest.ID()) - obj.SetOwnerID(ownerID) + obj.SetOwnerID(owner.NewIDFromPublicKey(&key.PublicKey)) return obj } @@ -94,11 +88,10 @@ func TestFormatValidator_Validate(t *testing.T) { }) t.Run("correct w/ session token", func(t *testing.T) { - w, err := owner.NEO3WalletFromPublicKey((*ecdsa.PublicKey)(ownerKey.PublicKey())) - require.NoError(t, err) + oid := owner.NewIDFromPublicKey((*ecdsa.PublicKey)(ownerKey.PublicKey())) tok := sessiontest.Token() - tok.SetOwnerID(owner.NewIDFromNeo3Wallet(w)) + tok.SetOwnerID(oid) obj := NewRaw() obj.SetContainerID(cidtest.ID()) @@ -111,7 +104,7 @@ func TestFormatValidator_Validate(t *testing.T) { }) t.Run("correct w/o session token", func(t *testing.T) { - obj := blankValidObject(t, &ownerKey.PrivateKey) + obj := blankValidObject(&ownerKey.PrivateKey) require.NoError(t, object.SetIDWithSignature(&ownerKey.PrivateKey, obj.SDK())) @@ -188,7 +181,7 @@ func TestFormatValidator_Validate(t *testing.T) { t.Run("expiration", func(t *testing.T) { fn := func(val string) *Object { - obj := blankValidObject(t, &ownerKey.PrivateKey) + obj := blankValidObject(&ownerKey.PrivateKey) a := object.NewAttribute() a.SetKey(objectV2.SysAttributeExpEpoch) @@ -222,7 +215,7 @@ func TestFormatValidator_Validate(t *testing.T) { t.Run("attributes", func(t *testing.T) { t.Run("duplication", func(t *testing.T) { - obj := blankValidObject(t, &ownerKey.PrivateKey) + obj := blankValidObject(&ownerKey.PrivateKey) a1 := object.NewAttribute() a1.SetKey("key1") @@ -244,7 +237,7 @@ func TestFormatValidator_Validate(t *testing.T) { }) t.Run("empty value", func(t *testing.T) { - obj := blankValidObject(t, &ownerKey.PrivateKey) + obj := blankValidObject(&ownerKey.PrivateKey) a := object.NewAttribute() a.SetKey("key") diff --git a/pkg/innerring/processors/container/common.go b/pkg/innerring/processors/container/common.go index 33e0aee44..e709e9ba8 100644 --- a/pkg/innerring/processors/container/common.go +++ b/pkg/innerring/processors/container/common.go @@ -52,17 +52,7 @@ func (cp *Processor) checkKeyOwnership(ownerIDSrc ownerIDSource, key *keys.Publi } } - // TODO: need more convenient way to do this - w, err := owner.NEO3WalletFromPublicKey(&ecdsa.PublicKey{ - Curve: key.Curve, - X: key.X, - Y: key.Y, - }) - if err != nil { - return err - } - - if ownerIDSrc.OwnerID().Equal(owner.NewIDFromNeo3Wallet(w)) { + if ownerIDSrc.OwnerID().Equal(owner.NewIDFromPublicKey((*ecdsa.PublicKey)(key))) { return nil } diff --git a/pkg/innerring/processors/settlement/audit/calculate.go b/pkg/innerring/processors/settlement/audit/calculate.go index b23396204..e4ffa3cae 100644 --- a/pkg/innerring/processors/settlement/audit/calculate.go +++ b/pkg/innerring/processors/settlement/audit/calculate.go @@ -319,13 +319,5 @@ func ownerFromKey(key []byte) (*owner.ID, error) { return nil, err } - n3wallet, err := owner.NEO3WalletFromPublicKey((*ecdsa.PublicKey)(pubKey)) - if err != nil { - return nil, err - } - - o := owner.NewID() - o.SetNeo3Wallet(n3wallet) - - return o, nil + return owner.NewIDFromPublicKey((*ecdsa.PublicKey)(pubKey)), nil } diff --git a/pkg/innerring/processors/settlement/basic/context.go b/pkg/innerring/processors/settlement/basic/context.go index 0a5dbe5d9..425777f74 100644 --- a/pkg/innerring/processors/settlement/basic/context.go +++ b/pkg/innerring/processors/settlement/basic/context.go @@ -4,7 +4,6 @@ import ( "math/big" "sync" - "github.com/mr-tron/base58" "github.com/nspcc-dev/neo-go/pkg/encoding/address" "github.com/nspcc-dev/neo-go/pkg/util" "github.com/nspcc-dev/neofs-node/pkg/innerring/processors/settlement/common" @@ -85,16 +84,12 @@ func bankOwnerID() (*owner.ID, error) { u := util.Uint160{1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // todo: define const 1, 1, 1, 1, 1, 1, 1, 1, 1, 1} - d, err := base58.Decode(address.Uint160ToString(u)) + o := owner.NewID() + + err := o.Parse(address.Uint160ToString(u)) if err != nil { return nil, err } - var w owner.NEO3Wallet - copy(w[:], d) - - o := owner.NewID() - o.SetNeo3Wallet(&w) - return o, nil } diff --git a/pkg/innerring/settlement.go b/pkg/innerring/settlement.go index 4038fe970..3c9cf94c9 100644 --- a/pkg/innerring/settlement.go +++ b/pkg/innerring/settlement.go @@ -187,15 +187,7 @@ func (s settlementDeps) ResolveKey(ni common.NodeInfo) (*owner.ID, error) { return nil, err } - w, err := owner.NEO3WalletFromPublicKey((*ecdsa.PublicKey)(pub)) - if err != nil { - return nil, err - } - - id := owner.NewID() - id.SetNeo3Wallet(w) - - return id, nil + return owner.NewIDFromPublicKey((*ecdsa.PublicKey)(pub)), nil } func (s settlementDeps) Transfer(sender, recipient *owner.ID, amount *big.Int, details []byte) { diff --git a/pkg/innerring/subnet.go b/pkg/innerring/subnet.go index 418515be0..108154569 100644 --- a/pkg/innerring/subnet.go +++ b/pkg/innerring/subnet.go @@ -204,13 +204,8 @@ func (x putSubnetEvent) ReadCreator(id *owner.ID) error { return err } - wal, err := owner.NEO3WalletFromPublicKey((*ecdsa.PublicKey)(key)) - if err != nil { - return err - } - // it would be better if we could do it not like this - *id = *owner.NewIDFromNeo3Wallet(wal) + *id = *owner.NewIDFromPublicKey((*ecdsa.PublicKey)(key)) return nil } diff --git a/pkg/local_object_storage/engine/engine_test.go b/pkg/local_object_storage/engine/engine_test.go index e94b4d28e..67c7a508e 100644 --- a/pkg/local_object_storage/engine/engine_test.go +++ b/pkg/local_object_storage/engine/engine_test.go @@ -88,11 +88,8 @@ func generateRawObjectWithCID(t *testing.T, cid *cid.ID) *object.RawObject { version.SetMajor(2) version.SetMinor(1) - w, err := owner.NEO3WalletFromPublicKey(&test.DecodeKey(-1).PublicKey) - require.NoError(t, err) - csum := new(checksum.Checksum) - csum.SetSHA256(sha256.Sum256(w.Bytes())) + csum.SetSHA256(sha256.Sum256(owner.PublicKeyToIDBytes(&test.DecodeKey(-1).PublicKey))) csumTZ := new(checksum.Checksum) csumTZ.SetTillichZemor(tz.Sum(csum.Sum())) diff --git a/pkg/local_object_storage/metabase/db_test.go b/pkg/local_object_storage/metabase/db_test.go index 44ed892e7..3a3e4732e 100644 --- a/pkg/local_object_storage/metabase/db_test.go +++ b/pkg/local_object_storage/metabase/db_test.go @@ -69,11 +69,8 @@ func generateRawObjectWithCID(t *testing.T, cid *cid.ID) *object.RawObject { version.SetMajor(2) version.SetMinor(1) - w, err := owner.NEO3WalletFromPublicKey(&test.DecodeKey(-1).PublicKey) - require.NoError(t, err) - csum := new(checksum.Checksum) - csum.SetSHA256(sha256.Sum256(w.Bytes())) + csum.SetSHA256(sha256.Sum256(owner.PublicKeyToIDBytes(&test.DecodeKey(-1).PublicKey))) csumTZ := new(checksum.Checksum) csumTZ.SetTillichZemor(tz.Sum(csum.Sum())) diff --git a/pkg/local_object_storage/shard/shard_test.go b/pkg/local_object_storage/shard/shard_test.go index f89552600..d605fb006 100644 --- a/pkg/local_object_storage/shard/shard_test.go +++ b/pkg/local_object_storage/shard/shard_test.go @@ -73,11 +73,8 @@ func generateRawObjectWithCID(t *testing.T, cid *cid.ID) *object.RawObject { version.SetMajor(2) version.SetMinor(1) - w, err := owner.NEO3WalletFromPublicKey(&test.DecodeKey(-1).PublicKey) - require.NoError(t, err) - csum := new(checksum.Checksum) - csum.SetSHA256(sha256.Sum256(w.Bytes())) + csum.SetSHA256(sha256.Sum256(owner.PublicKeyToIDBytes(&test.DecodeKey(-1).PublicKey))) csumTZ := new(checksum.Checksum) csumTZ.SetTillichZemor(tz.Sum(csum.Sum())) diff --git a/pkg/services/object/acl/acl.go b/pkg/services/object/acl/acl.go index c28852ddf..f5b873f2f 100644 --- a/pkg/services/object/acl/acl.go +++ b/pkg/services/object/acl/acl.go @@ -780,12 +780,7 @@ func isOwnerFromKey(id *owner.ID, key *keys.PublicKey) bool { return false } - wallet, err := owner.NEO3WalletFromPublicKey((*ecdsa.PublicKey)(key)) - if err != nil { - return false - } - - return id.Equal(owner.NewIDFromNeo3Wallet(wallet)) + return id.Equal(owner.NewIDFromPublicKey((*ecdsa.PublicKey)(key))) } // originalBearerToken goes down to original request meta header and fetches diff --git a/pkg/services/object/acl/classifier.go b/pkg/services/object/acl/classifier.go index 233c4c0b8..d8e661097 100644 --- a/pkg/services/object/acl/classifier.go +++ b/pkg/services/object/acl/classifier.go @@ -113,16 +113,8 @@ func requestOwner(req metaWithToken) (*owner.ID, *keys.PublicKey, error) { } key := unmarshalPublicKey(bodySignature.Key()) - neo3wallet, err := owner.NEO3WalletFromPublicKey((*ecdsa.PublicKey)(key)) - if err != nil { - return nil, nil, fmt.Errorf("can't create neo3 wallet: %w", err) - } - // form user from public key - user := new(owner.ID) - user.SetNeo3Wallet(neo3wallet) - - return user, key, nil + return owner.NewIDFromPublicKey((*ecdsa.PublicKey)(key)), key, nil } func originalBodySignature(v *session.RequestVerificationHeader) *signature.Signature {