2020-03-25 10:00:11 +00:00
|
|
|
package state
|
|
|
|
|
|
|
|
import (
|
2020-08-03 12:00:27 +00:00
|
|
|
"crypto/elliptic"
|
2021-05-31 07:39:27 +00:00
|
|
|
"errors"
|
|
|
|
"fmt"
|
2020-03-25 10:00:11 +00:00
|
|
|
"math/big"
|
|
|
|
|
2020-04-26 11:00:17 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/crypto/keys"
|
2020-06-05 16:12:10 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/vm/stackitem"
|
2020-03-25 10:00:11 +00:00
|
|
|
)
|
|
|
|
|
2021-07-18 09:39:31 +00:00
|
|
|
// NEP17Balance represents balance state of a NEP17-token.
|
|
|
|
type NEP17Balance struct {
|
2020-03-25 10:00:11 +00:00
|
|
|
Balance big.Int
|
|
|
|
}
|
|
|
|
|
2021-07-18 09:39:31 +00:00
|
|
|
// NEOBalance represents balance state of a NEO-token.
|
|
|
|
type NEOBalance struct {
|
|
|
|
NEP17Balance
|
2020-03-25 10:00:11 +00:00
|
|
|
BalanceHeight uint32
|
2020-08-03 12:00:27 +00:00
|
|
|
VoteTo *keys.PublicKey
|
2020-03-25 10:00:11 +00:00
|
|
|
}
|
|
|
|
|
2021-07-18 09:39:31 +00:00
|
|
|
// NEP17BalanceFromBytes converts serialized NEP17Balance to structure.
|
|
|
|
func NEP17BalanceFromBytes(b []byte) (*NEP17Balance, error) {
|
|
|
|
balance := new(NEP17Balance)
|
2021-07-17 15:37:33 +00:00
|
|
|
err := balanceFromBytes(b, balance)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2020-04-23 18:28:37 +00:00
|
|
|
}
|
|
|
|
return balance, nil
|
|
|
|
}
|
|
|
|
|
2021-07-18 09:39:31 +00:00
|
|
|
// Bytes returns serialized NEP17Balance.
|
|
|
|
func (s *NEP17Balance) Bytes() []byte {
|
2021-07-17 15:37:33 +00:00
|
|
|
return balanceToBytes(s)
|
2020-04-23 18:28:37 +00:00
|
|
|
}
|
|
|
|
|
2021-07-17 15:37:33 +00:00
|
|
|
func balanceFromBytes(b []byte, item stackitem.Convertible) error {
|
|
|
|
if len(b) == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return stackitem.DeserializeConvertible(b, item)
|
2020-06-05 16:12:10 +00:00
|
|
|
}
|
|
|
|
|
2021-07-17 15:37:33 +00:00
|
|
|
func balanceToBytes(item stackitem.Convertible) []byte {
|
|
|
|
data, err := stackitem.SerializeConvertible(item)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
return data
|
2020-06-05 16:12:10 +00:00
|
|
|
}
|
|
|
|
|
2021-07-17 15:37:33 +00:00
|
|
|
// ToStackItem implements stackitem.Convertible. It never returns an error.
|
2021-07-18 09:39:31 +00:00
|
|
|
func (s *NEP17Balance) ToStackItem() (stackitem.Item, error) {
|
2021-07-17 15:37:33 +00:00
|
|
|
return stackitem.NewStruct([]stackitem.Item{stackitem.NewBigInteger(&s.Balance)}), nil
|
2020-03-25 10:00:11 +00:00
|
|
|
}
|
|
|
|
|
2021-07-17 15:37:33 +00:00
|
|
|
// FromStackItem implements stackitem.Convertible.
|
2021-07-18 09:39:31 +00:00
|
|
|
func (s *NEP17Balance) FromStackItem(item stackitem.Item) error {
|
2021-07-17 15:37:33 +00:00
|
|
|
items, ok := item.Value().([]stackitem.Item)
|
|
|
|
if !ok {
|
|
|
|
return errors.New("not a struct")
|
|
|
|
}
|
|
|
|
if len(items) < 1 {
|
|
|
|
return errors.New("no balance value")
|
|
|
|
}
|
|
|
|
balance, err := items[0].TryInteger()
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("invalid balance: %w", err)
|
2020-03-25 10:00:11 +00:00
|
|
|
}
|
2021-07-17 15:37:33 +00:00
|
|
|
s.Balance = *balance
|
|
|
|
return nil
|
2020-03-25 10:00:11 +00:00
|
|
|
}
|
|
|
|
|
2021-07-18 09:39:31 +00:00
|
|
|
// NEOBalanceFromBytes converts serialized NEOBalance to structure.
|
|
|
|
func NEOBalanceFromBytes(b []byte) (*NEOBalance, error) {
|
|
|
|
balance := new(NEOBalance)
|
2021-07-17 15:37:33 +00:00
|
|
|
err := balanceFromBytes(b, balance)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2020-04-23 18:28:37 +00:00
|
|
|
}
|
|
|
|
return balance, nil
|
|
|
|
}
|
|
|
|
|
2021-07-18 09:39:31 +00:00
|
|
|
// Bytes returns serialized NEOBalance.
|
|
|
|
func (s *NEOBalance) Bytes() []byte {
|
2021-07-17 15:37:33 +00:00
|
|
|
return balanceToBytes(s)
|
2020-06-05 16:12:10 +00:00
|
|
|
}
|
|
|
|
|
2021-07-17 15:37:33 +00:00
|
|
|
// ToStackItem implements stackitem.Convertible interface. It never returns an error.
|
2021-07-18 09:39:31 +00:00
|
|
|
func (s *NEOBalance) ToStackItem() (stackitem.Item, error) {
|
|
|
|
resItem, _ := s.NEP17Balance.ToStackItem()
|
2021-07-17 15:37:33 +00:00
|
|
|
result := resItem.(*stackitem.Struct)
|
2020-06-05 16:12:10 +00:00
|
|
|
result.Append(stackitem.NewBigInteger(big.NewInt(int64(s.BalanceHeight))))
|
2020-08-03 12:00:27 +00:00
|
|
|
if s.VoteTo != nil {
|
|
|
|
result.Append(stackitem.NewByteArray(s.VoteTo.Bytes()))
|
|
|
|
} else {
|
|
|
|
result.Append(stackitem.Null{})
|
|
|
|
}
|
2021-07-17 15:37:33 +00:00
|
|
|
return result, nil
|
2020-06-05 16:12:10 +00:00
|
|
|
}
|
|
|
|
|
2021-07-18 09:39:31 +00:00
|
|
|
// FromStackItem converts stackitem.Item to NEOBalance.
|
|
|
|
func (s *NEOBalance) FromStackItem(item stackitem.Item) error {
|
2021-05-31 07:39:27 +00:00
|
|
|
structItem, ok := item.Value().([]stackitem.Item)
|
|
|
|
if !ok || len(structItem) < 3 {
|
|
|
|
return errors.New("invalid stackitem length")
|
|
|
|
}
|
|
|
|
balance, err := structItem[0].TryInteger()
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("invalid balance stackitem: %w", err)
|
|
|
|
}
|
|
|
|
s.Balance = *balance
|
|
|
|
h, err := structItem[1].TryInteger()
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("invalid heigh stackitem")
|
|
|
|
}
|
|
|
|
s.BalanceHeight = uint32(h.Int64())
|
2020-08-03 12:00:27 +00:00
|
|
|
if _, ok := structItem[2].(stackitem.Null); ok {
|
|
|
|
s.VoteTo = nil
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
bs, err := structItem[2].TryBytes()
|
|
|
|
if err != nil {
|
2021-05-31 07:39:27 +00:00
|
|
|
return fmt.Errorf("invalid public key stackitem: %w", err)
|
2020-08-03 12:00:27 +00:00
|
|
|
}
|
|
|
|
pub, err := keys.NewPublicKeyFromBytes(bs, elliptic.P256())
|
|
|
|
if err != nil {
|
2021-05-31 07:39:27 +00:00
|
|
|
return fmt.Errorf("invalid public key bytes: %w", err)
|
2020-08-03 12:00:27 +00:00
|
|
|
}
|
|
|
|
s.VoteTo = pub
|
|
|
|
return nil
|
2020-03-25 10:00:11 +00:00
|
|
|
}
|