2020-08-11 12:04:46 +00:00
|
|
|
package invoke
|
|
|
|
|
|
|
|
import (
|
2020-12-18 12:52:27 +00:00
|
|
|
"crypto/sha256"
|
|
|
|
|
2020-08-11 12:04:46 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/crypto/keys"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/util"
|
2020-12-18 12:52:27 +00:00
|
|
|
"github.com/nspcc-dev/neofs-api-go/pkg/container"
|
2020-08-11 12:04:46 +00:00
|
|
|
"github.com/nspcc-dev/neofs-node/pkg/morph/client"
|
2020-12-18 12:52:27 +00:00
|
|
|
"github.com/pkg/errors"
|
2020-08-11 12:04:46 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type (
|
|
|
|
// ContainerParams for container put invocation.
|
|
|
|
ContainerParams struct {
|
|
|
|
Key *keys.PublicKey
|
|
|
|
Container []byte
|
|
|
|
Signature []byte
|
|
|
|
}
|
2020-09-02 16:04:29 +00:00
|
|
|
|
|
|
|
// ContainerParams for container put invocation.
|
|
|
|
RemoveContainerParams struct {
|
|
|
|
ContainerID []byte
|
|
|
|
Signature []byte
|
|
|
|
}
|
2020-08-11 12:04:46 +00:00
|
|
|
)
|
|
|
|
|
2020-12-18 12:52:27 +00:00
|
|
|
var ErrParseTestInvoke = errors.New("can't parse NEO node response")
|
|
|
|
|
2020-08-11 12:04:46 +00:00
|
|
|
const (
|
2020-09-02 16:04:29 +00:00
|
|
|
putContainerMethod = "put"
|
|
|
|
deleteContainerMethod = "delete"
|
2020-12-18 12:52:27 +00:00
|
|
|
listContainersMethod = "list"
|
2020-08-11 12:04:46 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// RegisterContainer invokes Put method.
|
|
|
|
func RegisterContainer(cli *client.Client, con util.Uint160, p *ContainerParams) error {
|
|
|
|
if cli == nil {
|
|
|
|
return client.ErrNilClient
|
|
|
|
}
|
|
|
|
|
2020-12-17 13:08:19 +00:00
|
|
|
return cli.Invoke(con, 5*extraFee, putContainerMethod,
|
2020-08-11 12:04:46 +00:00
|
|
|
p.Container,
|
|
|
|
p.Signature,
|
|
|
|
p.Key.Bytes(),
|
|
|
|
)
|
|
|
|
}
|
2020-09-02 16:04:29 +00:00
|
|
|
|
|
|
|
// RegisterContainer invokes Delete method.
|
|
|
|
func RemoveContainer(cli *client.Client, con util.Uint160, p *RemoveContainerParams) error {
|
|
|
|
if cli == nil {
|
|
|
|
return client.ErrNilClient
|
|
|
|
}
|
|
|
|
|
|
|
|
return cli.Invoke(con, extraFee, deleteContainerMethod,
|
|
|
|
p.ContainerID,
|
|
|
|
p.Signature,
|
|
|
|
)
|
|
|
|
}
|
2020-12-18 12:52:27 +00:00
|
|
|
|
|
|
|
func ListContainers(cli *client.Client, con util.Uint160) ([]*container.ID, error) {
|
|
|
|
if cli == nil {
|
|
|
|
return nil, client.ErrNilClient
|
|
|
|
}
|
|
|
|
|
|
|
|
item, err := cli.TestInvoke(con, listContainersMethod, []byte{})
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(item) < 1 {
|
|
|
|
return nil, errors.Wrap(ErrParseTestInvoke, "nested array expected")
|
|
|
|
}
|
|
|
|
|
|
|
|
rawIDs, err := client.ArrayFromStackItem(item[0])
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
result := make([]*container.ID, 0, len(rawIDs))
|
|
|
|
|
|
|
|
var bufHash [sha256.Size]byte
|
|
|
|
|
|
|
|
for i := range rawIDs {
|
|
|
|
cid, err := client.BytesFromStackItem(rawIDs[i])
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(cid) != sha256.Size {
|
|
|
|
return nil, errors.Wrap(ErrParseTestInvoke, "invalid container ID size")
|
|
|
|
}
|
|
|
|
|
|
|
|
copy(bufHash[:], cid)
|
|
|
|
|
|
|
|
containerID := container.NewID()
|
|
|
|
containerID.SetSHA256(bufHash)
|
|
|
|
|
|
|
|
result = append(result, containerID)
|
|
|
|
}
|
|
|
|
|
|
|
|
return result, nil
|
|
|
|
}
|