forked from TrueCloudLab/neoneo-go
Merge pull request #785 from nspcc-dev/feature/uint160_marshalling
util: JSONify uint160 using LE instead of BE
This commit is contained in:
commit
751e79d480
10 changed files with 19 additions and 58 deletions
|
@ -179,7 +179,7 @@ func getMatchingTokenAux(get func(i int) *wallet.Token, n int, name string) (*wa
|
||||||
var count int
|
var count int
|
||||||
for i := 0; i < n; i++ {
|
for i := 0; i < n; i++ {
|
||||||
t := get(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 {
|
if count == 1 {
|
||||||
printTokenInfo(token)
|
printTokenInfo(token)
|
||||||
printTokenInfo(t)
|
printTokenInfo(t)
|
||||||
|
@ -240,7 +240,7 @@ func printTokenInfo(tok *wallet.Token) {
|
||||||
fmt.Printf("Symbol:\t%s\n", tok.Symbol)
|
fmt.Printf("Symbol:\t%s\n", tok.Symbol)
|
||||||
fmt.Printf("Hash:\t%s\n", tok.Hash.StringLE())
|
fmt.Printf("Hash:\t%s\n", tok.Hash.StringLE())
|
||||||
fmt.Printf("Decimals: %d\n", tok.Decimals)
|
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 {
|
func printNEP5Info(ctx *cli.Context) error {
|
||||||
|
|
|
@ -44,12 +44,9 @@ func NewAccountState(a *state.Account) AccountState {
|
||||||
|
|
||||||
sort.Sort(balances)
|
sort.Sort(balances)
|
||||||
|
|
||||||
// reverse scriptHash to be consistent with other client
|
|
||||||
scriptHash := a.ScriptHash.Reverse()
|
|
||||||
|
|
||||||
return AccountState{
|
return AccountState{
|
||||||
Version: a.Version,
|
Version: a.Version,
|
||||||
ScriptHash: scriptHash,
|
ScriptHash: a.ScriptHash,
|
||||||
IsFrozen: a.IsFrozen,
|
IsFrozen: a.IsFrozen,
|
||||||
Votes: a.Votes,
|
Votes: a.Votes,
|
||||||
Balances: balances,
|
Balances: balances,
|
||||||
|
|
|
@ -10,14 +10,12 @@ import (
|
||||||
// AssetState wrapper used for the representation of
|
// AssetState wrapper used for the representation of
|
||||||
// state.Asset on the RPC Server.
|
// state.Asset on the RPC Server.
|
||||||
type AssetState struct {
|
type AssetState struct {
|
||||||
ID util.Uint256 `json:"assetID"`
|
ID util.Uint256 `json:"id"`
|
||||||
AssetType transaction.AssetType `json:"assetType"`
|
AssetType transaction.AssetType `json:"type"`
|
||||||
Name string `json:"name"`
|
Name string `json:"name"`
|
||||||
Amount util.Fixed8 `json:"amount"`
|
Amount util.Fixed8 `json:"amount"`
|
||||||
Available util.Fixed8 `json:"available"`
|
Available util.Fixed8 `json:"available"`
|
||||||
Precision uint8 `json:"precision"`
|
Precision uint8 `json:"precision"`
|
||||||
FeeMode uint8 `json:"fee"`
|
|
||||||
FeeAddress util.Uint160 `json:"address"`
|
|
||||||
Owner string `json:"owner"`
|
Owner string `json:"owner"`
|
||||||
Admin string `json:"admin"`
|
Admin string `json:"admin"`
|
||||||
Issuer string `json:"issuer"`
|
Issuer string `json:"issuer"`
|
||||||
|
@ -34,8 +32,6 @@ func NewAssetState(a *state.Asset) AssetState {
|
||||||
Amount: a.Amount,
|
Amount: a.Amount,
|
||||||
Available: a.Available,
|
Available: a.Available,
|
||||||
Precision: a.Precision,
|
Precision: a.Precision,
|
||||||
FeeMode: a.FeeMode,
|
|
||||||
FeeAddress: a.FeeAddress,
|
|
||||||
Owner: a.Owner.String(),
|
Owner: a.Owner.String(),
|
||||||
Admin: address.Uint160ToString(a.Admin),
|
Admin: address.Uint160ToString(a.Admin),
|
||||||
Issuer: address.Uint160ToString(a.Issuer),
|
Issuer: address.Uint160ToString(a.Issuer),
|
||||||
|
|
|
@ -31,9 +31,6 @@ type Properties struct {
|
||||||
|
|
||||||
// NewContractState creates a new Contract wrapper.
|
// NewContractState creates a new Contract wrapper.
|
||||||
func NewContractState(c *state.Contract) ContractState {
|
func NewContractState(c *state.Contract) ContractState {
|
||||||
// reverse scriptHash to be consistent with other client
|
|
||||||
scriptHash := c.ScriptHash().Reverse()
|
|
||||||
|
|
||||||
properties := Properties{
|
properties := Properties{
|
||||||
HasStorage: c.HasStorage(),
|
HasStorage: c.HasStorage(),
|
||||||
HasDynamicInvoke: c.HasDynamicInvoke(),
|
HasDynamicInvoke: c.HasDynamicInvoke(),
|
||||||
|
@ -42,7 +39,7 @@ func NewContractState(c *state.Contract) ContractState {
|
||||||
|
|
||||||
return ContractState{
|
return ContractState{
|
||||||
Version: 0,
|
Version: 0,
|
||||||
ScriptHash: scriptHash,
|
ScriptHash: c.ScriptHash(),
|
||||||
Script: c.Script,
|
Script: c.Script,
|
||||||
ParamList: c.ParamList,
|
ParamList: c.ParamList,
|
||||||
ReturnType: c.ReturnType,
|
ReturnType: c.ReturnType,
|
||||||
|
|
|
@ -128,7 +128,7 @@ var rpcTestCases = map[string][]rpcTestCase{
|
||||||
res, ok := cs.(*result.ContractState)
|
res, ok := cs.(*result.ContractState)
|
||||||
require.True(t, ok)
|
require.True(t, ok)
|
||||||
assert.Equal(t, byte(0), res.Version)
|
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)
|
assert.Equal(t, "0.99", res.CodeVersion)
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
|
|
@ -66,7 +66,9 @@ func (p *Parameter) MarshalJSON() ([]byte, error) {
|
||||||
resultErr error
|
resultErr error
|
||||||
)
|
)
|
||||||
switch p.Type {
|
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)
|
resultRawValue, resultErr = json.Marshal(p.Value)
|
||||||
case IntegerType:
|
case IntegerType:
|
||||||
val, ok := p.Value.(int64)
|
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 {
|
if err = json.Unmarshal(r.Value, &h); err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
p.Value = h
|
p.Value = h.Reverse() // Hash160 should be marshaled in BE but default marshaler uses LE.
|
||||||
case Hash256Type:
|
case Hash256Type:
|
||||||
var h util.Uint256
|
var h util.Uint256
|
||||||
if err = json.Unmarshal(r.Value, &h); err != nil {
|
if err = json.Unmarshal(r.Value, &h); err != nil {
|
||||||
|
|
|
@ -123,11 +123,11 @@ func (u *Uint160) UnmarshalJSON(data []byte) (err error) {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
js = strings.TrimPrefix(js, "0x")
|
js = strings.TrimPrefix(js, "0x")
|
||||||
*u, err = Uint160DecodeStringBE(js)
|
*u, err = Uint160DecodeStringLE(js)
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
// MarshalJSON implements the json marshaller interface.
|
// MarshalJSON implements the json marshaller interface.
|
||||||
func (u Uint160) MarshalJSON() ([]byte, error) {
|
func (u Uint160) MarshalJSON() ([]byte, error) {
|
||||||
return []byte(`"0x` + u.String() + `"`), nil
|
return []byte(`"0x` + u.StringLE() + `"`), nil
|
||||||
}
|
}
|
||||||
|
|
|
@ -9,8 +9,8 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestUint160UnmarshalJSON(t *testing.T) {
|
func TestUint160UnmarshalJSON(t *testing.T) {
|
||||||
str := "2d3b96ae1bcc5a585e075e3b81920210dec16302"
|
str := "0263c1de100292813b5e075e585acc1bae963b2d"
|
||||||
expected, err := Uint160DecodeStringBE(str)
|
expected, err := Uint160DecodeStringLE(str)
|
||||||
assert.NoError(t, err)
|
assert.NoError(t, err)
|
||||||
|
|
||||||
// UnmarshalJSON decodes hex-strings
|
// UnmarshalJSON decodes hex-strings
|
||||||
|
|
|
@ -1,22 +1,12 @@
|
||||||
package wallet
|
package wallet
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
|
||||||
|
|
||||||
"github.com/nspcc-dev/neo-go/pkg/encoding/address"
|
"github.com/nspcc-dev/neo-go/pkg/encoding/address"
|
||||||
"github.com/nspcc-dev/neo-go/pkg/util"
|
"github.com/nspcc-dev/neo-go/pkg/util"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Token represents imported token contract.
|
// Token represents imported token contract.
|
||||||
type Token struct {
|
type Token struct {
|
||||||
Name string
|
|
||||||
Hash util.Uint160
|
|
||||||
Decimals int64
|
|
||||||
Symbol string
|
|
||||||
Address string
|
|
||||||
}
|
|
||||||
|
|
||||||
type tokenAux struct {
|
|
||||||
Name string `json:"name"`
|
Name string `json:"name"`
|
||||||
Hash util.Uint160 `json:"script_hash"`
|
Hash util.Uint160 `json:"script_hash"`
|
||||||
Decimals int64 `json:"decimals"`
|
Decimals int64 `json:"decimals"`
|
||||||
|
@ -30,31 +20,10 @@ func NewToken(tokenHash util.Uint160, name, symbol string, decimals int64) *Toke
|
||||||
Hash: tokenHash,
|
Hash: tokenHash,
|
||||||
Decimals: decimals,
|
Decimals: decimals,
|
||||||
Symbol: symbol,
|
Symbol: symbol,
|
||||||
Address: address.Uint160ToString(tokenHash),
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// MarshalJSON implements json.Marshaler interface.
|
// Address returns token address from hash
|
||||||
func (t *Token) MarshalJSON() ([]byte, error) {
|
func (t *Token) Address() string {
|
||||||
m := &tokenAux{
|
return address.Uint160ToString(t.Hash)
|
||||||
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
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -18,7 +18,7 @@ func TestToken_MarshalJSON(t *testing.T) {
|
||||||
require.Equal(t, "NEP5", tok.Symbol)
|
require.Equal(t, "NEP5", tok.Symbol)
|
||||||
require.EqualValues(t, 8, tok.Decimals)
|
require.EqualValues(t, 8, tok.Decimals)
|
||||||
require.Equal(t, h, tok.Hash)
|
require.Equal(t, h, tok.Hash)
|
||||||
require.Equal(t, "AYhE3Svuqdfh1RtzvE8hUhNR7HSpaSDFQg", tok.Address)
|
require.Equal(t, "AYhE3Svuqdfh1RtzvE8hUhNR7HSpaSDFQg", tok.Address())
|
||||||
|
|
||||||
data, err := json.Marshal(tok)
|
data, err := json.Marshal(tok)
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
|
|
Loading…
Reference in a new issue