2020-08-11 11:46:31 +00:00
|
|
|
package container
|
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/elliptic"
|
2021-05-18 08:12:51 +00:00
|
|
|
"fmt"
|
2020-08-11 11:46:31 +00:00
|
|
|
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/crypto/keys"
|
2020-10-26 14:46:15 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/vm/stackitem"
|
2020-08-11 11:46:31 +00:00
|
|
|
"github.com/nspcc-dev/neofs-node/pkg/morph/client"
|
|
|
|
"github.com/nspcc-dev/neofs-node/pkg/morph/event"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Put structure of container.Put notification from morph chain.
|
|
|
|
type Put struct {
|
|
|
|
rawContainer []byte
|
|
|
|
signature []byte
|
|
|
|
publicKey *keys.PublicKey
|
|
|
|
}
|
|
|
|
|
|
|
|
// MorphEvent implements Neo:Morph Event interface.
|
|
|
|
func (Put) MorphEvent() {}
|
|
|
|
|
|
|
|
// Container is a marshalled container structure, defined in API.
|
|
|
|
func (p Put) Container() []byte { return p.rawContainer }
|
|
|
|
|
|
|
|
// Signature of marshalled container by container owner.
|
|
|
|
func (p Put) Signature() []byte { return p.signature }
|
|
|
|
|
|
|
|
// PublicKey of container owner.
|
|
|
|
func (p Put) PublicKey() *keys.PublicKey { return p.publicKey }
|
|
|
|
|
|
|
|
// ParsePut from notification into container event structure.
|
2020-10-26 14:46:15 +00:00
|
|
|
func ParsePut(params []stackitem.Item) (event.Event, error) {
|
2020-08-11 11:46:31 +00:00
|
|
|
var (
|
|
|
|
ev Put
|
|
|
|
err error
|
|
|
|
)
|
|
|
|
|
|
|
|
if ln := len(params); ln != 3 {
|
|
|
|
return nil, event.WrongNumberOfParameters(3, ln)
|
|
|
|
}
|
|
|
|
|
|
|
|
// parse container
|
2020-10-26 14:46:15 +00:00
|
|
|
ev.rawContainer, err = client.BytesFromStackItem(params[0])
|
2020-08-11 11:46:31 +00:00
|
|
|
if err != nil {
|
2021-05-18 08:12:51 +00:00
|
|
|
return nil, fmt.Errorf("could not get container: %w", err)
|
2020-08-11 11:46:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// parse signature
|
2020-10-26 14:46:15 +00:00
|
|
|
ev.signature, err = client.BytesFromStackItem(params[1])
|
2020-08-11 11:46:31 +00:00
|
|
|
if err != nil {
|
2021-05-18 08:12:51 +00:00
|
|
|
return nil, fmt.Errorf("could not get signature: %w", err)
|
2020-08-11 11:46:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// parse public key
|
2020-10-26 14:46:15 +00:00
|
|
|
key, err := client.BytesFromStackItem(params[2])
|
2020-08-11 11:46:31 +00:00
|
|
|
if err != nil {
|
2021-05-18 08:12:51 +00:00
|
|
|
return nil, fmt.Errorf("could not get public key: %w", err)
|
2020-08-11 11:46:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ev.publicKey, err = keys.NewPublicKeyFromBytes(key, elliptic.P256())
|
|
|
|
if err != nil {
|
2021-05-18 08:12:51 +00:00
|
|
|
return nil, fmt.Errorf("could not parse public key: %w", err)
|
2020-08-11 11:46:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return ev, nil
|
|
|
|
}
|