Merge pull request #785 from nspcc-dev/feature/uint160_marshalling

util: JSONify uint160 using LE instead of BE
This commit is contained in:
Roman Khimov 2020-03-24 12:41:07 +03:00 committed by GitHub
commit 751e79d480
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
10 changed files with 19 additions and 58 deletions

View file

@ -179,7 +179,7 @@ func getMatchingTokenAux(get func(i int) *wallet.Token, n int, name string) (*wa
var count int
for i := 0; i < n; i++ {
t := get(i)
if t != nil && (t.Name == name || t.Symbol == name || t.Address == name || t.Hash.StringLE() == name) {
if t != nil && (t.Name == name || t.Symbol == name || t.Address() == name || t.Hash.StringLE() == name) {
if count == 1 {
printTokenInfo(token)
printTokenInfo(t)
@ -240,7 +240,7 @@ func printTokenInfo(tok *wallet.Token) {
fmt.Printf("Symbol:\t%s\n", tok.Symbol)
fmt.Printf("Hash:\t%s\n", tok.Hash.StringLE())
fmt.Printf("Decimals: %d\n", tok.Decimals)
fmt.Printf("Address: %s\n", tok.Address)
fmt.Printf("Address: %s\n", tok.Address())
}
func printNEP5Info(ctx *cli.Context) error {

View file

@ -44,12 +44,9 @@ func NewAccountState(a *state.Account) AccountState {
sort.Sort(balances)
// reverse scriptHash to be consistent with other client
scriptHash := a.ScriptHash.Reverse()
return AccountState{
Version: a.Version,
ScriptHash: scriptHash,
ScriptHash: a.ScriptHash,
IsFrozen: a.IsFrozen,
Votes: a.Votes,
Balances: balances,

View file

@ -10,14 +10,12 @@ import (
// AssetState wrapper used for the representation of
// state.Asset on the RPC Server.
type AssetState struct {
ID util.Uint256 `json:"assetID"`
AssetType transaction.AssetType `json:"assetType"`
ID util.Uint256 `json:"id"`
AssetType transaction.AssetType `json:"type"`
Name string `json:"name"`
Amount util.Fixed8 `json:"amount"`
Available util.Fixed8 `json:"available"`
Precision uint8 `json:"precision"`
FeeMode uint8 `json:"fee"`
FeeAddress util.Uint160 `json:"address"`
Owner string `json:"owner"`
Admin string `json:"admin"`
Issuer string `json:"issuer"`
@ -34,8 +32,6 @@ func NewAssetState(a *state.Asset) AssetState {
Amount: a.Amount,
Available: a.Available,
Precision: a.Precision,
FeeMode: a.FeeMode,
FeeAddress: a.FeeAddress,
Owner: a.Owner.String(),
Admin: address.Uint160ToString(a.Admin),
Issuer: address.Uint160ToString(a.Issuer),

View file

@ -31,9 +31,6 @@ type Properties struct {
// NewContractState creates a new Contract wrapper.
func NewContractState(c *state.Contract) ContractState {
// reverse scriptHash to be consistent with other client
scriptHash := c.ScriptHash().Reverse()
properties := Properties{
HasStorage: c.HasStorage(),
HasDynamicInvoke: c.HasDynamicInvoke(),
@ -42,7 +39,7 @@ func NewContractState(c *state.Contract) ContractState {
return ContractState{
Version: 0,
ScriptHash: scriptHash,
ScriptHash: c.ScriptHash(),
Script: c.Script,
ParamList: c.ParamList,
ReturnType: c.ReturnType,

View file

@ -128,7 +128,7 @@ var rpcTestCases = map[string][]rpcTestCase{
res, ok := cs.(*result.ContractState)
require.True(t, ok)
assert.Equal(t, byte(0), res.Version)
assert.Equal(t, testContractHash, res.ScriptHash.StringBE())
assert.Equal(t, testContractHash, res.ScriptHash.StringLE())
assert.Equal(t, "0.99", res.CodeVersion)
},
},

View file

@ -66,7 +66,9 @@ func (p *Parameter) MarshalJSON() ([]byte, error) {
resultErr error
)
switch p.Type {
case BoolType, StringType, Hash256Type, Hash160Type:
case Hash160Type:
resultRawValue, resultErr = json.Marshal(p.Value.(util.Uint160).Reverse()) // Hash160 should be marshaled in BE but default marshaler uses LE.
case BoolType, StringType, Hash256Type:
resultRawValue, resultErr = json.Marshal(p.Value)
case IntegerType:
val, ok := p.Value.(int64)
@ -200,7 +202,7 @@ func (p *Parameter) UnmarshalJSON(data []byte) (err error) {
if err = json.Unmarshal(r.Value, &h); err != nil {
return
}
p.Value = h
p.Value = h.Reverse() // Hash160 should be marshaled in BE but default marshaler uses LE.
case Hash256Type:
var h util.Uint256
if err = json.Unmarshal(r.Value, &h); err != nil {

View file

@ -123,11 +123,11 @@ func (u *Uint160) UnmarshalJSON(data []byte) (err error) {
return err
}
js = strings.TrimPrefix(js, "0x")
*u, err = Uint160DecodeStringBE(js)
*u, err = Uint160DecodeStringLE(js)
return err
}
// MarshalJSON implements the json marshaller interface.
func (u Uint160) MarshalJSON() ([]byte, error) {
return []byte(`"0x` + u.String() + `"`), nil
return []byte(`"0x` + u.StringLE() + `"`), nil
}

View file

@ -9,8 +9,8 @@ import (
)
func TestUint160UnmarshalJSON(t *testing.T) {
str := "2d3b96ae1bcc5a585e075e3b81920210dec16302"
expected, err := Uint160DecodeStringBE(str)
str := "0263c1de100292813b5e075e585acc1bae963b2d"
expected, err := Uint160DecodeStringLE(str)
assert.NoError(t, err)
// UnmarshalJSON decodes hex-strings

View file

@ -1,22 +1,12 @@
package wallet
import (
"encoding/json"
"github.com/nspcc-dev/neo-go/pkg/encoding/address"
"github.com/nspcc-dev/neo-go/pkg/util"
)
// Token represents imported token contract.
type Token struct {
Name string
Hash util.Uint160
Decimals int64
Symbol string
Address string
}
type tokenAux struct {
Name string `json:"name"`
Hash util.Uint160 `json:"script_hash"`
Decimals int64 `json:"decimals"`
@ -30,31 +20,10 @@ func NewToken(tokenHash util.Uint160, name, symbol string, decimals int64) *Toke
Hash: tokenHash,
Decimals: decimals,
Symbol: symbol,
Address: address.Uint160ToString(tokenHash),
}
}
// MarshalJSON implements json.Marshaler interface.
func (t *Token) MarshalJSON() ([]byte, error) {
m := &tokenAux{
Name: t.Name,
Hash: t.Hash.Reverse(), // address should be marshaled in LE but default marshaler uses BE.
Decimals: t.Decimals,
Symbol: t.Symbol,
}
return json.Marshal(m)
}
// UnmarshalJSON implements json.Unmarshaler interface.
func (t *Token) UnmarshalJSON(data []byte) error {
aux := new(tokenAux)
if err := json.Unmarshal(data, aux); err != nil {
return err
}
t.Name = aux.Name
t.Hash = aux.Hash.Reverse()
t.Decimals = aux.Decimals
t.Symbol = aux.Symbol
t.Address = address.Uint160ToString(t.Hash)
return nil
// Address returns token address from hash
func (t *Token) Address() string {
return address.Uint160ToString(t.Hash)
}

View file

@ -18,7 +18,7 @@ func TestToken_MarshalJSON(t *testing.T) {
require.Equal(t, "NEP5", tok.Symbol)
require.EqualValues(t, 8, tok.Decimals)
require.Equal(t, h, tok.Hash)
require.Equal(t, "AYhE3Svuqdfh1RtzvE8hUhNR7HSpaSDFQg", tok.Address)
require.Equal(t, "AYhE3Svuqdfh1RtzvE8hUhNR7HSpaSDFQg", tok.Address())
data, err := json.Marshal(tok)
require.NoError(t, err)