2020-09-08 08:37:58 +00:00
|
|
|
package neofs
|
|
|
|
|
|
|
|
import (
|
2021-05-18 08:12:51 +00:00
|
|
|
"fmt"
|
2020-09-08 08:37:58 +00:00
|
|
|
|
2022-07-28 16:22:32 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/core/state"
|
2021-11-11 08:59:14 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/util"
|
2020-10-26 14:46:15 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/vm/stackitem"
|
2020-09-08 08:37:58 +00:00
|
|
|
"github.com/nspcc-dev/neofs-node/pkg/morph/client"
|
|
|
|
"github.com/nspcc-dev/neofs-node/pkg/morph/event"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Bind struct {
|
2021-05-31 18:09:02 +00:00
|
|
|
bindCommon
|
|
|
|
}
|
|
|
|
|
|
|
|
type bindCommon struct {
|
|
|
|
user []byte
|
|
|
|
keys [][]byte
|
2021-11-11 08:59:14 +00:00
|
|
|
|
|
|
|
// txHash is used in notary environmental
|
|
|
|
// for calculating unique but same for
|
|
|
|
// all notification receivers values.
|
|
|
|
txHash util.Uint256
|
|
|
|
}
|
|
|
|
|
|
|
|
// TxHash returns hash of the TX with new epoch
|
|
|
|
// notification.
|
|
|
|
func (b bindCommon) TxHash() util.Uint256 {
|
|
|
|
return b.txHash
|
2020-09-08 08:37:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// MorphEvent implements Neo:Morph Event interface.
|
2021-05-31 18:09:02 +00:00
|
|
|
func (bindCommon) MorphEvent() {}
|
2020-09-08 08:37:58 +00:00
|
|
|
|
2021-05-31 18:09:02 +00:00
|
|
|
func (b bindCommon) Keys() [][]byte { return b.keys }
|
2020-09-08 08:37:58 +00:00
|
|
|
|
2021-05-31 18:09:02 +00:00
|
|
|
func (b bindCommon) User() []byte { return b.user }
|
2020-09-08 08:37:58 +00:00
|
|
|
|
2022-07-28 16:22:32 +00:00
|
|
|
func ParseBind(e *state.ContainedNotificationEvent) (event.Event, error) {
|
2020-09-08 08:37:58 +00:00
|
|
|
var (
|
|
|
|
ev Bind
|
|
|
|
err error
|
|
|
|
)
|
|
|
|
|
2021-10-22 10:06:08 +00:00
|
|
|
params, err := event.ParseStackArray(e)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("could not parse stack items from notify event: %w", err)
|
|
|
|
}
|
|
|
|
|
2021-05-31 18:09:02 +00:00
|
|
|
err = parseBind(&ev.bindCommon, params)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2020-09-08 08:37:58 +00:00
|
|
|
}
|
|
|
|
|
2021-11-11 08:59:14 +00:00
|
|
|
ev.txHash = e.Container
|
|
|
|
|
2021-05-31 18:09:02 +00:00
|
|
|
return ev, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func parseBind(dst *bindCommon, params []stackitem.Item) error {
|
|
|
|
if ln := len(params); ln != 2 {
|
|
|
|
return event.WrongNumberOfParameters(2, ln)
|
2020-09-08 08:37:58 +00:00
|
|
|
}
|
|
|
|
|
2021-05-31 18:09:02 +00:00
|
|
|
var err error
|
|
|
|
|
|
|
|
// parse user
|
|
|
|
dst.user, err = client.BytesFromStackItem(params[0])
|
2020-09-08 08:37:58 +00:00
|
|
|
if err != nil {
|
2021-05-31 18:09:02 +00:00
|
|
|
return fmt.Errorf("could not get bind user: %w", err)
|
2020-09-08 08:37:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// parse keys
|
2020-10-26 14:46:15 +00:00
|
|
|
bindKeys, err := client.ArrayFromStackItem(params[1])
|
2020-09-08 08:37:58 +00:00
|
|
|
if err != nil {
|
2021-05-31 18:09:02 +00:00
|
|
|
return fmt.Errorf("could not get bind keys: %w", err)
|
2020-09-08 08:37:58 +00:00
|
|
|
}
|
|
|
|
|
2021-05-31 18:09:02 +00:00
|
|
|
dst.keys = make([][]byte, 0, len(bindKeys))
|
|
|
|
|
2020-09-08 08:37:58 +00:00
|
|
|
for i := range bindKeys {
|
2020-10-26 14:46:15 +00:00
|
|
|
rawKey, err := client.BytesFromStackItem(bindKeys[i])
|
2020-09-08 08:37:58 +00:00
|
|
|
if err != nil {
|
2021-05-31 18:09:02 +00:00
|
|
|
return fmt.Errorf("could not get bind public key: %w", err)
|
2020-09-08 08:37:58 +00:00
|
|
|
}
|
|
|
|
|
2021-05-31 18:09:02 +00:00
|
|
|
dst.keys = append(dst.keys, rawKey)
|
2020-09-08 08:37:58 +00:00
|
|
|
}
|
|
|
|
|
2021-05-31 18:09:02 +00:00
|
|
|
return nil
|
2020-09-08 08:37:58 +00:00
|
|
|
}
|