2021-05-19 09:19:33 +00:00
|
|
|
package neofsid
|
|
|
|
|
|
|
|
import (
|
2022-01-31 11:04:59 +00:00
|
|
|
"crypto/elliptic"
|
2021-05-19 09:19:33 +00:00
|
|
|
"fmt"
|
|
|
|
|
2022-01-31 11:04:59 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/crypto/keys"
|
2021-05-19 09:19:33 +00:00
|
|
|
"github.com/nspcc-dev/neofs-node/pkg/morph/client"
|
2022-05-17 13:59:46 +00:00
|
|
|
"github.com/nspcc-dev/neofs-sdk-go/user"
|
2021-05-19 09:19:33 +00:00
|
|
|
)
|
|
|
|
|
2022-01-31 11:04:59 +00:00
|
|
|
// AccountKeysPrm groups parameters of AccountKeys operation.
|
|
|
|
type AccountKeysPrm struct {
|
2022-05-31 17:00:41 +00:00
|
|
|
id user.ID
|
2021-05-19 09:19:33 +00:00
|
|
|
}
|
|
|
|
|
2022-01-31 11:04:59 +00:00
|
|
|
// SetID sets owner ID.
|
2022-05-31 17:00:41 +00:00
|
|
|
func (a *AccountKeysPrm) SetID(id user.ID) {
|
2022-01-31 11:04:59 +00:00
|
|
|
a.id = id
|
2021-05-19 09:19:33 +00:00
|
|
|
}
|
|
|
|
|
2022-01-31 11:04:59 +00:00
|
|
|
// AccountKeys requests public keys of NeoFS account from NeoFS ID contract.
|
|
|
|
func (x *Client) AccountKeys(p AccountKeysPrm) (keys.PublicKeys, error) {
|
|
|
|
prm := client.TestInvokePrm{}
|
|
|
|
prm.SetMethod(keyListingMethod)
|
2022-05-17 13:59:46 +00:00
|
|
|
prm.SetArgs(p.id.WalletBytes())
|
2021-05-19 09:19:33 +00:00
|
|
|
|
2022-01-31 11:04:59 +00:00
|
|
|
items, err := x.client.TestInvoke(prm)
|
2021-05-19 09:19:33 +00:00
|
|
|
if err != nil {
|
2022-01-29 13:06:36 +00:00
|
|
|
return nil, fmt.Errorf("could not perform test invocation (%s): %w", keyListingMethod, err)
|
2021-05-19 09:19:33 +00:00
|
|
|
} else if ln := len(items); ln != 1 {
|
2022-01-29 13:06:36 +00:00
|
|
|
return nil, fmt.Errorf("unexpected stack item count (%s): %d", keyListingMethod, ln)
|
2021-05-19 09:19:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
items, err = client.ArrayFromStackItem(items[0])
|
|
|
|
if err != nil {
|
2022-01-29 13:06:36 +00:00
|
|
|
return nil, fmt.Errorf("1st stack item must be an array (%s)", keyListingMethod)
|
2021-05-19 09:19:33 +00:00
|
|
|
}
|
|
|
|
|
2022-01-31 11:04:59 +00:00
|
|
|
pubs := make(keys.PublicKeys, len(items))
|
2021-05-19 09:19:33 +00:00
|
|
|
for i := range items {
|
2022-01-31 11:04:59 +00:00
|
|
|
rawPub, err := client.BytesFromStackItem(items[i])
|
2021-05-19 09:19:33 +00:00
|
|
|
if err != nil {
|
2022-01-29 13:06:36 +00:00
|
|
|
return nil, fmt.Errorf("invalid stack item, expected byte array (%s)", keyListingMethod)
|
2021-05-19 09:19:33 +00:00
|
|
|
}
|
|
|
|
|
2022-01-31 11:04:59 +00:00
|
|
|
pubs[i], err = keys.NewPublicKeyFromBytes(rawPub, elliptic.P256())
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("received invalid key (%s): %w", keyListingMethod, err)
|
|
|
|
}
|
2021-05-19 09:19:33 +00:00
|
|
|
}
|
|
|
|
|
2022-01-31 11:04:59 +00:00
|
|
|
return pubs, nil
|
2021-05-19 09:19:33 +00:00
|
|
|
}
|