2022-12-23 17:35:35 +00:00
|
|
|
package frostfs
|
2021-05-31 18:14:30 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/elliptic"
|
|
|
|
"fmt"
|
|
|
|
|
2023-03-07 13:38:26 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-node/pkg/morph/client/frostfsid"
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-node/pkg/morph/event/frostfs"
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-sdk-go/user"
|
2021-05-31 18:14:30 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/crypto/keys"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/util"
|
|
|
|
"go.uber.org/zap"
|
|
|
|
)
|
|
|
|
|
|
|
|
type bindCommon interface {
|
|
|
|
User() []byte
|
|
|
|
Keys() [][]byte
|
2021-11-11 08:59:14 +00:00
|
|
|
TxHash() util.Uint256
|
2021-05-31 18:14:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (np *Processor) processBind(e bindCommon) {
|
|
|
|
if !np.alphabetState.IsAlphabet() {
|
|
|
|
np.log.Info("non alphabet mode, ignore bind")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
c := &bindCommonContext{
|
|
|
|
bindCommon: e,
|
|
|
|
}
|
|
|
|
|
2022-12-23 17:35:35 +00:00
|
|
|
_, c.bind = e.(frostfs.Bind)
|
2021-05-31 18:14:30 +00:00
|
|
|
|
|
|
|
err := np.checkBindCommon(c)
|
|
|
|
if err != nil {
|
|
|
|
np.log.Error("invalid manage key event",
|
|
|
|
zap.Bool("bind", c.bind),
|
|
|
|
zap.String("error", err.Error()),
|
|
|
|
)
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
np.approveBindCommon(c)
|
|
|
|
}
|
|
|
|
|
|
|
|
type bindCommonContext struct {
|
|
|
|
bindCommon
|
|
|
|
|
|
|
|
bind bool
|
|
|
|
|
|
|
|
scriptHash util.Uint160
|
|
|
|
}
|
|
|
|
|
|
|
|
func (np *Processor) checkBindCommon(e *bindCommonContext) error {
|
|
|
|
var err error
|
|
|
|
|
|
|
|
e.scriptHash, err = util.Uint160DecodeBytesBE(e.User())
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
curve := elliptic.P256()
|
|
|
|
|
|
|
|
for _, key := range e.Keys() {
|
|
|
|
_, err = keys.NewPublicKeyFromBytes(key, curve)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (np *Processor) approveBindCommon(e *bindCommonContext) {
|
|
|
|
// calculate wallet address
|
|
|
|
scriptHash := e.User()
|
|
|
|
|
|
|
|
u160, err := util.Uint160DecodeBytesBE(scriptHash)
|
|
|
|
if err != nil {
|
|
|
|
np.log.Error("could not decode script hash from bytes",
|
|
|
|
zap.String("error", err.Error()),
|
|
|
|
)
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-05-17 13:59:46 +00:00
|
|
|
var id user.ID
|
|
|
|
id.SetScriptHash(u160)
|
|
|
|
|
2023-01-10 13:01:54 +00:00
|
|
|
prm := frostfsid.CommonBindPrm{}
|
2022-05-17 13:59:46 +00:00
|
|
|
prm.SetOwnerID(id.WalletBytes())
|
2021-11-11 08:59:14 +00:00
|
|
|
prm.SetKeys(e.Keys())
|
|
|
|
prm.SetHash(e.bindCommon.TxHash())
|
|
|
|
|
2022-01-31 11:04:59 +00:00
|
|
|
var typ string
|
|
|
|
if e.bind {
|
|
|
|
typ = "bind"
|
2022-12-23 17:35:35 +00:00
|
|
|
err = np.frostfsIDClient.AddKeys(prm)
|
2022-01-31 11:04:59 +00:00
|
|
|
} else {
|
|
|
|
typ = "unbind"
|
2022-12-23 17:35:35 +00:00
|
|
|
err = np.frostfsIDClient.RemoveKeys(prm)
|
2022-01-31 11:04:59 +00:00
|
|
|
}
|
2021-05-31 18:14:30 +00:00
|
|
|
|
2022-01-31 11:04:59 +00:00
|
|
|
if err != nil {
|
2021-05-31 18:14:30 +00:00
|
|
|
np.log.Error(fmt.Sprintf("could not approve %s", typ),
|
2022-01-31 11:04:59 +00:00
|
|
|
zap.String("error", err.Error()))
|
2021-05-31 18:14:30 +00:00
|
|
|
}
|
|
|
|
}
|