neo-go/pkg/rpc/wrappers/account_state.go
Roman Khimov b77e533d13 crypto/wallet: move public/private key into the new keys package
And drop associated _pkg.dev remnants (refs. #307).

Original `dev` branch had two separate packages for public and private keys,
but those are so intertwined (`TestHelper` subpackage is a proof) that it's
better unite them and all associated code (like WIF and NEP-2) in one
package. This patch also:
 * creates internal `keytestcases` package to share things with wallet (maybe
   it'll be changed in some future)
 * ports some tests from `dev`
 * ports Verify() method for public key from `dev`
 * expands TestPrivateKey() with public key check
2019-08-27 17:45:51 +03:00

60 lines
1.6 KiB
Go

package wrappers
import (
"bytes"
"sort"
"github.com/CityOfZion/neo-go/pkg/core"
"github.com/CityOfZion/neo-go/pkg/crypto/keys"
"github.com/CityOfZion/neo-go/pkg/util"
)
// AccountState wrapper used for the representation of
// core.AccountState on the RPC Server.
type AccountState struct {
Version uint8 `json:"version"`
ScriptHash util.Uint160 `json:"script_hash"`
IsFrozen bool `json:"frozen"`
Votes []*keys.PublicKey `json:"votes"`
Balances []Balance `json:"balances"`
}
// Balances type for sorting balances in rpc response
type Balances []Balance
func (b Balances) Len() int { return len(b) }
func (b Balances) Less(i, j int) bool { return bytes.Compare(b[i].Asset[:], b[j].Asset[:]) != -1 }
func (b Balances) Swap(i, j int) { b[i], b[j] = b[j], b[i] }
// Balance response wrapper
type Balance struct {
Asset util.Uint256 `json:"asset"`
Value util.Fixed8 `json:"value"`
}
// NewAccountState creates a new AccountState wrapper.
func NewAccountState(a *core.AccountState) AccountState {
balances := make(Balances, 0, len(a.Balances))
for k, v := range a.Balances {
balances = append(balances, Balance{
Asset: k,
Value: v,
})
}
sort.Sort(balances)
// reverse scriptHash to be consistent with other client
scriptHash, err := util.Uint160DecodeBytes(a.ScriptHash.BytesReverse())
if err != nil {
scriptHash = a.ScriptHash
}
return AccountState{
Version: a.Version,
ScriptHash: scriptHash,
IsFrozen: a.IsFrozen,
Votes: a.Votes,
Balances: balances,
}
}