2020-03-25 10:00:11 +00:00
|
|
|
package state
|
|
|
|
|
|
|
|
import (
|
2020-08-03 12:00:27 +00:00
|
|
|
"crypto/elliptic"
|
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-03-25 10:00:11 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/io"
|
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
|
|
|
)
|
|
|
|
|
2020-11-24 08:14:25 +00:00
|
|
|
// NEP17BalanceState represents balance state of a NEP17-token.
|
|
|
|
type NEP17BalanceState struct {
|
2020-03-25 10:00:11 +00:00
|
|
|
Balance big.Int
|
|
|
|
}
|
|
|
|
|
|
|
|
// NEOBalanceState represents balance state of a NEO-token.
|
|
|
|
type NEOBalanceState struct {
|
2020-11-24 08:14:25 +00:00
|
|
|
NEP17BalanceState
|
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
|
|
|
}
|
|
|
|
|
2020-11-24 08:14:25 +00:00
|
|
|
// NEP17BalanceStateFromBytes converts serialized NEP17BalanceState to structure.
|
|
|
|
func NEP17BalanceStateFromBytes(b []byte) (*NEP17BalanceState, error) {
|
|
|
|
balance := new(NEP17BalanceState)
|
2020-04-23 18:28:37 +00:00
|
|
|
if len(b) == 0 {
|
|
|
|
return balance, nil
|
|
|
|
}
|
|
|
|
r := io.NewBinReaderFromBuf(b)
|
|
|
|
balance.DecodeBinary(r)
|
|
|
|
if r.Err != nil {
|
|
|
|
return nil, r.Err
|
|
|
|
}
|
|
|
|
return balance, nil
|
|
|
|
}
|
|
|
|
|
2020-11-24 08:14:25 +00:00
|
|
|
// Bytes returns serialized NEP17BalanceState.
|
|
|
|
func (s *NEP17BalanceState) Bytes() []byte {
|
2020-04-23 18:28:37 +00:00
|
|
|
w := io.NewBufBinWriter()
|
|
|
|
s.EncodeBinary(w.BinWriter)
|
|
|
|
if w.Err != nil {
|
|
|
|
panic(w.Err)
|
|
|
|
}
|
|
|
|
return w.Bytes()
|
|
|
|
}
|
|
|
|
|
2020-11-24 08:14:25 +00:00
|
|
|
func (s *NEP17BalanceState) toStackItem() stackitem.Item {
|
2020-06-05 16:12:10 +00:00
|
|
|
return stackitem.NewStruct([]stackitem.Item{stackitem.NewBigInteger(&s.Balance)})
|
|
|
|
}
|
|
|
|
|
2020-11-24 08:14:25 +00:00
|
|
|
func (s *NEP17BalanceState) fromStackItem(item stackitem.Item) {
|
2020-06-05 16:12:10 +00:00
|
|
|
s.Balance = *item.(*stackitem.Struct).Value().([]stackitem.Item)[0].Value().(*big.Int)
|
|
|
|
}
|
|
|
|
|
2020-03-25 10:00:11 +00:00
|
|
|
// EncodeBinary implements io.Serializable interface.
|
2020-11-24 08:14:25 +00:00
|
|
|
func (s *NEP17BalanceState) EncodeBinary(w *io.BinWriter) {
|
2020-06-05 16:12:10 +00:00
|
|
|
si := s.toStackItem()
|
|
|
|
stackitem.EncodeBinaryStackItem(si, w)
|
2020-03-25 10:00:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// DecodeBinary implements io.Serializable interface.
|
2020-11-24 08:14:25 +00:00
|
|
|
func (s *NEP17BalanceState) DecodeBinary(r *io.BinReader) {
|
2020-06-05 16:12:10 +00:00
|
|
|
si := stackitem.DecodeBinaryStackItem(r)
|
2020-03-25 10:00:11 +00:00
|
|
|
if r.Err != nil {
|
|
|
|
return
|
|
|
|
}
|
2020-06-05 16:12:10 +00:00
|
|
|
s.fromStackItem(si)
|
2020-03-25 10:00:11 +00:00
|
|
|
}
|
|
|
|
|
2020-04-23 18:28:37 +00:00
|
|
|
// NEOBalanceStateFromBytes converts serialized NEOBalanceState to structure.
|
|
|
|
func NEOBalanceStateFromBytes(b []byte) (*NEOBalanceState, error) {
|
|
|
|
balance := new(NEOBalanceState)
|
|
|
|
if len(b) == 0 {
|
|
|
|
return balance, nil
|
|
|
|
}
|
|
|
|
r := io.NewBinReaderFromBuf(b)
|
|
|
|
balance.DecodeBinary(r)
|
|
|
|
|
|
|
|
if r.Err != nil {
|
|
|
|
return nil, r.Err
|
|
|
|
}
|
|
|
|
return balance, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Bytes returns serialized NEOBalanceState.
|
|
|
|
func (s *NEOBalanceState) Bytes() []byte {
|
|
|
|
w := io.NewBufBinWriter()
|
|
|
|
s.EncodeBinary(w.BinWriter)
|
|
|
|
if w.Err != nil {
|
|
|
|
panic(w.Err)
|
|
|
|
}
|
|
|
|
return w.Bytes()
|
|
|
|
}
|
|
|
|
|
2020-03-25 10:00:11 +00:00
|
|
|
// EncodeBinary implements io.Serializable interface.
|
|
|
|
func (s *NEOBalanceState) EncodeBinary(w *io.BinWriter) {
|
2020-06-05 16:12:10 +00:00
|
|
|
si := s.toStackItem()
|
|
|
|
stackitem.EncodeBinaryStackItem(si, w)
|
2020-03-25 10:00:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// DecodeBinary implements io.Serializable interface.
|
|
|
|
func (s *NEOBalanceState) DecodeBinary(r *io.BinReader) {
|
2020-06-05 16:12:10 +00:00
|
|
|
si := stackitem.DecodeBinaryStackItem(r)
|
|
|
|
if r.Err != nil {
|
|
|
|
return
|
|
|
|
}
|
2020-06-23 14:38:19 +00:00
|
|
|
r.Err = s.fromStackItem(si)
|
2020-06-05 16:12:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *NEOBalanceState) toStackItem() stackitem.Item {
|
2020-11-24 08:14:25 +00:00
|
|
|
result := s.NEP17BalanceState.toStackItem().(*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{})
|
|
|
|
}
|
2020-06-05 16:12:10 +00:00
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
2020-06-23 14:38:19 +00:00
|
|
|
func (s *NEOBalanceState) fromStackItem(item stackitem.Item) error {
|
2020-06-05 16:12:10 +00:00
|
|
|
structItem := item.Value().([]stackitem.Item)
|
|
|
|
s.Balance = *structItem[0].Value().(*big.Int)
|
|
|
|
s.BalanceHeight = uint32(structItem[1].Value().(*big.Int).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 {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
pub, err := keys.NewPublicKeyFromBytes(bs, elliptic.P256())
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
s.VoteTo = pub
|
|
|
|
return nil
|
2020-03-25 10:00:11 +00:00
|
|
|
}
|