util: add LE suffix to Uint160 methods

This commit is contained in:
Evgenii Stratonikov 2019-11-27 12:20:31 +03:00
parent 5d2fb41991
commit 57efad912c
28 changed files with 88 additions and 83 deletions

View file

@ -585,6 +585,6 @@ func contractDeploy(ctx *cli.Context) error {
if err != nil { if err != nil {
return cli.NewExitError(fmt.Errorf("failed to push invocation tx: %v", err), 1) return cli.NewExitError(fmt.Errorf("failed to push invocation tx: %v", err), 1)
} }
fmt.Printf("Sent deployment transaction %s for contract %s\n", txHash.ReverseString(), hash.Hash160(avm).ReverseString()) fmt.Printf("Sent deployment transaction %s for contract %s\n", txHash.ReverseString(), hash.Hash160(avm).StringLE())
return nil return nil
} }

View file

@ -670,7 +670,7 @@ func (c *codegen) convertBuiltin(expr *ast.CallExpr) {
c.prog.Err = err c.prog.Err = err
return return
} }
bytes := uint160.Bytes() bytes := uint160.BytesBE()
emitBytes(c.prog.BinWriter, bytes) emitBytes(c.prog.BinWriter, bytes)
} }
} }

View file

@ -35,7 +35,7 @@ func (a Accounts) getAndUpdate(s storage.Store, hash util.Uint160) (*AccountStat
// present there. Returns nil otherwise. // present there. Returns nil otherwise.
func getAccountStateFromStore(s storage.Store, hash util.Uint160) (*AccountState, error) { func getAccountStateFromStore(s storage.Store, hash util.Uint160) (*AccountState, error) {
var account *AccountState var account *AccountState
key := storage.AppendPrefix(storage.STAccount, hash.Bytes()) key := storage.AppendPrefix(storage.STAccount, hash.BytesBE())
b, err := s.Get(key) b, err := s.Get(key)
if err == nil { if err == nil {
account = new(AccountState) account = new(AccountState)
@ -55,7 +55,7 @@ func putAccountStateIntoStore(store storage.Store, as *AccountState) error {
if buf.Err != nil { if buf.Err != nil {
return buf.Err return buf.Err
} }
key := storage.AppendPrefix(storage.STAccount, as.ScriptHash.Bytes()) key := storage.AppendPrefix(storage.STAccount, as.ScriptHash.BytesBE())
return store.Put(key, buf.Bytes()) return store.Put(key, buf.Bytes())
} }

View file

@ -602,7 +602,7 @@ func processValidatorStateDescriptor(descriptor *transaction.StateDescriptor, st
} }
func processAccountStateDescriptor(descriptor *transaction.StateDescriptor, state *BlockChainState) error { func processAccountStateDescriptor(descriptor *transaction.StateDescriptor, state *BlockChainState) error {
hash, err := util.Uint160DecodeBytes(descriptor.Key) hash, err := util.Uint160DecodeBytesBE(descriptor.Key)
if err != nil { if err != nil {
return err return err
} }
@ -744,7 +744,7 @@ func (bc *Blockchain) GetStorageItems(hash util.Uint160) (map[string]*StorageIte
// Cut prefix and hash. // Cut prefix and hash.
siMap[string(k[21:])] = si siMap[string(k[21:])] = si
} }
bc.store.Seek(storage.AppendPrefix(storage.STStorage, hash.BytesReverse()), saveToMap) bc.store.Seek(storage.AppendPrefix(storage.STStorage, hash.BytesLE()), saveToMap)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -893,7 +893,7 @@ func (bc *Blockchain) GetContractState(hash util.Uint160) *ContractState {
// getContractStateFromStore returns contract state as recorded in the given // getContractStateFromStore returns contract state as recorded in the given
// store by the given script hash. // store by the given script hash.
func getContractStateFromStore(s storage.Store, hash util.Uint160) *ContractState { func getContractStateFromStore(s storage.Store, hash util.Uint160) *ContractState {
key := storage.AppendPrefix(storage.STContract, hash.Bytes()) key := storage.AppendPrefix(storage.STContract, hash.BytesBE())
contractBytes, err := s.Get(key) contractBytes, err := s.Get(key)
if err != nil { if err != nil {
return nil return nil
@ -1366,7 +1366,7 @@ func (bc *Blockchain) GetScriptHashesForVerifying(t *transaction.Transaction) ([
} }
for _, a := range t.Attributes { for _, a := range t.Attributes {
if a.Usage == transaction.Script { if a.Usage == transaction.Script {
h, err := util.Uint160DecodeBytes(a.Data) h, err := util.Uint160DecodeBytesBE(a.Data)
if err != nil { if err != nil {
return nil, err return nil, err
} }

View file

@ -70,13 +70,13 @@ func putContractStateIntoStore(s storage.Store, cs *ContractState) error {
if buf.Err != nil { if buf.Err != nil {
return buf.Err return buf.Err
} }
key := storage.AppendPrefix(storage.STContract, cs.ScriptHash().Bytes()) key := storage.AppendPrefix(storage.STContract, cs.ScriptHash().BytesBE())
return s.Put(key, buf.Bytes()) return s.Put(key, buf.Bytes())
} }
// deleteContractStateInStore deletes given contract state in the given store. // deleteContractStateInStore deletes given contract state in the given store.
func deleteContractStateInStore(s storage.Store, hash util.Uint160) error { func deleteContractStateInStore(s storage.Store, hash util.Uint160) error {
key := storage.AppendPrefix(storage.STContract, hash.Bytes()) key := storage.AppendPrefix(storage.STContract, hash.BytesBE())
return s.Delete(key) return s.Delete(key)
} }

View file

@ -74,7 +74,7 @@ func (ic *interopContext) headerGetNextConsensus(v *vm.VM) error {
if err != nil { if err != nil {
return err return err
} }
v.Estack().PushVal(header.NextConsensus.BytesReverse()) v.Estack().PushVal(header.NextConsensus.BytesLE())
return nil return nil
} }
@ -271,7 +271,7 @@ func (ic *interopContext) outputGetScriptHash(v *vm.VM) error {
if err != nil { if err != nil {
return err return err
} }
v.Estack().PushVal(output.ScriptHash.Bytes()) v.Estack().PushVal(output.ScriptHash.BytesBE())
return nil return nil
} }
@ -310,7 +310,7 @@ func (ic *interopContext) attrGetUsage(v *vm.VM) error {
// bcGetAccount returns or creates an account. // bcGetAccount returns or creates an account.
func (ic *interopContext) bcGetAccount(v *vm.VM) error { func (ic *interopContext) bcGetAccount(v *vm.VM) error {
accbytes := v.Estack().Pop().Bytes() accbytes := v.Estack().Pop().Bytes()
acchash, err := util.Uint160DecodeBytes(accbytes) acchash, err := util.Uint160DecodeBytesBE(accbytes)
if err != nil { if err != nil {
return err return err
} }
@ -364,7 +364,7 @@ func (ic *interopContext) accountGetScriptHash(v *vm.VM) error {
if !ok { if !ok {
return fmt.Errorf("%T is not an account state", acc) return fmt.Errorf("%T is not an account state", acc)
} }
v.Estack().PushVal(acc.ScriptHash.Bytes()) v.Estack().PushVal(acc.ScriptHash.BytesBE())
return nil return nil
} }
@ -389,7 +389,7 @@ func (ic *interopContext) accountGetVotes(v *vm.VM) error {
// accountIsStandard checks whether given account is standard. // accountIsStandard checks whether given account is standard.
func (ic *interopContext) accountIsStandard(v *vm.VM) error { func (ic *interopContext) accountIsStandard(v *vm.VM) error {
accbytes := v.Estack().Pop().Bytes() accbytes := v.Estack().Pop().Bytes()
acchash, err := util.Uint160DecodeBytes(accbytes) acchash, err := util.Uint160DecodeBytesBE(accbytes)
if err != nil { if err != nil {
return err return err
} }
@ -601,11 +601,11 @@ func (ic *interopContext) assetCreate(v *vm.VM) error {
if !witnessOk { if !witnessOk {
return errors.New("witness check didn't succeed") return errors.New("witness check didn't succeed")
} }
admin, err := util.Uint160DecodeBytes(v.Estack().Pop().Bytes()) admin, err := util.Uint160DecodeBytesBE(v.Estack().Pop().Bytes())
if err != nil { if err != nil {
return gherr.Wrap(err, "failed to get admin") return gherr.Wrap(err, "failed to get admin")
} }
issuer, err := util.Uint160DecodeBytes(v.Estack().Pop().Bytes()) issuer, err := util.Uint160DecodeBytesBE(v.Estack().Pop().Bytes())
if err != nil { if err != nil {
return gherr.Wrap(err, "failed to get issuer") return gherr.Wrap(err, "failed to get issuer")
} }
@ -635,7 +635,7 @@ func (ic *interopContext) assetGetAdmin(v *vm.VM) error {
if !ok { if !ok {
return fmt.Errorf("%T is not an asset state", as) return fmt.Errorf("%T is not an asset state", as)
} }
v.Estack().PushVal(as.Admin.Bytes()) v.Estack().PushVal(as.Admin.BytesBE())
return nil return nil
} }
@ -690,7 +690,7 @@ func (ic *interopContext) assetGetIssuer(v *vm.VM) error {
if !ok { if !ok {
return fmt.Errorf("%T is not an asset state", as) return fmt.Errorf("%T is not an asset state", as)
} }
v.Estack().PushVal(as.Issuer.Bytes()) v.Estack().PushVal(as.Issuer.BytesBE())
return nil return nil
} }

View file

@ -74,7 +74,7 @@ func TestHeaderGetNextConsensus(t *testing.T) {
err := context.headerGetNextConsensus(v) err := context.headerGetNextConsensus(v)
require.NoError(t, err) require.NoError(t, err)
value := v.Estack().Pop().Value() value := v.Estack().Pop().Value()
require.Equal(t, block.NextConsensus.BytesReverse(), value) require.Equal(t, block.NextConsensus.BytesLE(), value)
} }
func TestTxGetAttributes(t *testing.T) { func TestTxGetAttributes(t *testing.T) {
@ -168,7 +168,7 @@ func TestOutputGetScriptHash(t *testing.T) {
err := context.outputGetScriptHash(v) err := context.outputGetScriptHash(v)
require.NoError(t, err) require.NoError(t, err)
scriptHash := v.Estack().Pop().Value() scriptHash := v.Estack().Pop().Value()
require.Equal(t, tx.Outputs[0].ScriptHash.Bytes(), scriptHash) require.Equal(t, tx.Outputs[0].ScriptHash.BytesBE(), scriptHash)
} }
func TestOutputGetValue(t *testing.T) { func TestOutputGetValue(t *testing.T) {
@ -208,7 +208,7 @@ func TestAccountGetScriptHash(t *testing.T) {
err := context.accountGetScriptHash(v) err := context.accountGetScriptHash(v)
require.NoError(t, err) require.NoError(t, err)
hash := v.Estack().Pop().Value() hash := v.Estack().Pop().Value()
require.Equal(t, accState.ScriptHash.Bytes(), hash) require.Equal(t, accState.ScriptHash.BytesBE(), hash)
} }
func TestAccountGetVotes(t *testing.T) { func TestAccountGetVotes(t *testing.T) {
@ -248,7 +248,7 @@ func TestAssetGetAdmin(t *testing.T) {
err := context.assetGetAdmin(v) err := context.assetGetAdmin(v)
require.NoError(t, err) require.NoError(t, err)
admin := v.Estack().Pop().Value() admin := v.Estack().Pop().Value()
require.Equal(t, assetState.Admin.Bytes(), admin) require.Equal(t, assetState.Admin.BytesBE(), admin)
} }
func TestAssetGetAmount(t *testing.T) { func TestAssetGetAmount(t *testing.T) {
@ -298,7 +298,7 @@ func TestAssetGetIssuer(t *testing.T) {
err := context.assetGetIssuer(v) err := context.assetGetIssuer(v)
require.NoError(t, err) require.NoError(t, err)
issuer := v.Estack().Pop().Value() issuer := v.Estack().Pop().Value()
require.Equal(t, assetState.Issuer.Bytes(), issuer) require.Equal(t, assetState.Issuer.BytesBE(), issuer)
} }
func TestAssetGetOwner(t *testing.T) { func TestAssetGetOwner(t *testing.T) {
@ -381,7 +381,7 @@ func createVMAndContractState(t *testing.T) (*vm.VM, *ContractState, *interopCon
func createVMAndAccState(t *testing.T) (*vm.VM, *AccountState, *interopContext) { func createVMAndAccState(t *testing.T) (*vm.VM, *AccountState, *interopContext) {
v := vm.New() v := vm.New()
rawHash := "4d3b96ae1bcc5a585e075e3b81920210dec16302" rawHash := "4d3b96ae1bcc5a585e075e3b81920210dec16302"
hash, err := util.Uint160DecodeString(rawHash) hash, err := util.Uint160DecodeStringBE(rawHash)
accountState := NewAccountState(hash) accountState := NewAccountState(hash)
key := &keys.PublicKey{X: big.NewInt(1), Y: big.NewInt(1)} key := &keys.PublicKey{X: big.NewInt(1), Y: big.NewInt(1)}

View file

@ -63,7 +63,7 @@ func (ic *interopContext) bcGetBlock(v *vm.VM) error {
// bcGetContract returns contract. // bcGetContract returns contract.
func (ic *interopContext) bcGetContract(v *vm.VM) error { func (ic *interopContext) bcGetContract(v *vm.VM) error {
hashbytes := v.Estack().Pop().Bytes() hashbytes := v.Estack().Pop().Bytes()
hash, err := util.Uint160DecodeBytes(hashbytes) hash, err := util.Uint160DecodeBytesBE(hashbytes)
if err != nil { if err != nil {
return err return err
} }
@ -260,7 +260,7 @@ func getContextScriptHash(v *vm.VM, n int) util.Uint160 {
// invocation stack element number n. // invocation stack element number n.
func pushContextScriptHash(v *vm.VM, n int) error { func pushContextScriptHash(v *vm.VM, n int) error {
h := getContextScriptHash(v, n) h := getContextScriptHash(v, n)
v.Estack().PushVal(h.Bytes()) v.Estack().PushVal(h.BytesBE())
return nil return nil
} }
@ -318,7 +318,7 @@ func (ic *interopContext) runtimeCheckWitness(v *vm.VM) error {
var err error var err error
hashOrKey := v.Estack().Pop().Bytes() hashOrKey := v.Estack().Pop().Bytes()
hash, err := util.Uint160DecodeBytes(hashOrKey) hash, err := util.Uint160DecodeBytesBE(hashOrKey)
if err != nil { if err != nil {
key := &keys.PublicKey{} key := &keys.PublicKey{}
err = key.DecodeBytes(hashOrKey) err = key.DecodeBytes(hashOrKey)

View file

@ -14,7 +14,7 @@ type StorageItem struct {
// makeStorageItemKey returns a key used to store StorageItem in the DB. // makeStorageItemKey returns a key used to store StorageItem in the DB.
func makeStorageItemKey(scripthash util.Uint160, key []byte) []byte { func makeStorageItemKey(scripthash util.Uint160, key []byte) []byte {
return storage.AppendPrefix(storage.STStorage, append(scripthash.BytesReverse(), key...)) return storage.AppendPrefix(storage.STStorage, append(scripthash.BytesLE(), key...))
} }
// getStorageItemFromStore returns StorageItem if it exists in the given Store. // getStorageItemFromStore returns StorageItem if it exists in the given Store.

View file

@ -14,7 +14,7 @@ func TestPutGetDeleteStorageItem(t *testing.T) {
Value: []byte("smth"), Value: []byte("smth"),
} }
key := []byte("key") key := []byte("key")
cHash, err := util.Uint160DecodeBytes([]byte("abcdefghijklmnopqrst")) cHash, err := util.Uint160DecodeBytesBE([]byte("abcdefghijklmnopqrst"))
assert.Nil(t, err) assert.Nil(t, err)
assert.NoError(t, putStorageItemIntoStore(s, cHash, key, si)) assert.NoError(t, putStorageItemIntoStore(s, cHash, key, si))
siRead := getStorageItemFromStore(s, cHash, key) siRead := getStorageItemFromStore(s, cHash, key)

View file

@ -12,7 +12,7 @@ import (
) )
func TestRegisterTX(t *testing.T) { func TestRegisterTX(t *testing.T) {
someuint160, _ := util.Uint160DecodeString("4d3b96ae1bcc5a585e075e3b81920210dec16302") someuint160, _ := util.Uint160DecodeStringBE("4d3b96ae1bcc5a585e075e3b81920210dec16302")
tx := &Transaction{ tx := &Transaction{
Type: RegisterType, Type: RegisterType,
Version: 0, Version: 0,

View file

@ -8,7 +8,7 @@ import (
// Uint160. // Uint160.
func AddressFromUint160(u util.Uint160) string { func AddressFromUint160(u util.Uint160) string {
// Dont forget to prepend the Address version 0x17 (23) A // Dont forget to prepend the Address version 0x17 (23) A
b := append([]byte{0x17}, u.Bytes()...) b := append([]byte{0x17}, u.BytesBE()...)
return Base58CheckEncode(b) return Base58CheckEncode(b)
} }
@ -19,5 +19,5 @@ func Uint160DecodeAddress(s string) (u util.Uint160, err error) {
if err != nil { if err != nil {
return u, err return u, err
} }
return util.Uint160DecodeBytes(b[1:21]) return util.Uint160DecodeBytesBE(b[1:21])
} }

View file

@ -29,6 +29,6 @@ func TestUint160DecodeKnownAddress(t *testing.T) {
t.Fatal(err) t.Fatal(err)
} }
assert.Equal(t, "b28427088a3729b2536d10122960394e8be6721f", val.ReverseString()) assert.Equal(t, "b28427088a3729b2536d10122960394e8be6721f", val.StringLE())
assert.Equal(t, "1f72e68b4e39602912106d53b229378a082784b2", val.String()) assert.Equal(t, "1f72e68b4e39602912106d53b229378a082784b2", val.String())
} }

View file

@ -30,7 +30,7 @@ func RipeMD160(data []byte) util.Uint160 {
hasher := ripemd160.New() hasher := ripemd160.New()
_, _ = hasher.Write(data) _, _ = hasher.Write(data)
hash, _ = util.Uint160DecodeBytes(hasher.Sum(nil)) hash, _ = util.Uint160DecodeBytesBE(hasher.Sum(nil))
return hash return hash
} }
@ -41,7 +41,7 @@ func Hash160(data []byte) util.Uint160 {
h1 := Sha256(data) h1 := Sha256(data)
h2 := RipeMD160(h1.Bytes()) h2 := RipeMD160(h1.Bytes())
hash, _ = util.Uint160DecodeBytes(h2.Bytes()) hash, _ = util.Uint160DecodeBytesBE(h2.BytesBE())
return hash return hash
} }

View file

@ -36,7 +36,7 @@ func TestHashRipeMD160(t *testing.T) {
data := RipeMD160(input) data := RipeMD160(input)
expected := "108f07b8382412612c048d07d13f814118445acd" expected := "108f07b8382412612c048d07d13f814118445acd"
actual := hex.EncodeToString(data.Bytes()) actual := hex.EncodeToString(data.BytesBE())
assert.Equal(t, expected, actual) assert.Equal(t, expected, actual)
} }
@ -46,7 +46,7 @@ func TestHash160(t *testing.T) {
data := Hash160(publicKeyBytes) data := Hash160(publicKeyBytes)
expected := "c8e2b685cc70ec96743b55beb9449782f8f775d8" expected := "c8e2b685cc70ec96743b55beb9449782f8f775d8"
actual := hex.EncodeToString(data.Bytes()) actual := hex.EncodeToString(data.BytesBE())
assert.Equal(t, expected, actual) assert.Equal(t, expected, actual)
} }

View file

@ -237,7 +237,7 @@ func (p *PublicKey) GetVerificationScript() []byte {
func (p *PublicKey) Signature() []byte { func (p *PublicKey) Signature() []byte {
sig := hash.Hash160(p.GetVerificationScript()) sig := hash.Hash160(p.GetVerificationScript())
return sig.Bytes() return sig.BytesBE()
} }
// Address returns a base58-encoded NEO-specific address based on the key hash. // Address returns a base58-encoded NEO-specific address based on the key hash.

View file

@ -85,11 +85,11 @@ func (p Param) GetUint160FromHex() (util.Uint160, error) {
return util.Uint160{}, err return util.Uint160{}, err
} }
scriptHashLE, err := util.Uint160DecodeString(s) scriptHashLE, err := util.Uint160DecodeStringBE(s)
if err != nil { if err != nil {
return util.Uint160{}, err return util.Uint160{}, err
} }
return util.Uint160DecodeBytes(scriptHashLE.BytesReverse()) return util.Uint160DecodeBytesBE(scriptHashLE.BytesLE())
} }
// GetUint160FromAddress returns Uint160 value of the parameter that was // GetUint160FromAddress returns Uint160 value of the parameter that was

View file

@ -112,8 +112,8 @@ func TestParamGetUint256(t *testing.T) {
func TestParamGetUint160FromHex(t *testing.T) { func TestParamGetUint160FromHex(t *testing.T) {
in := "50befd26fdf6e4d957c11e078b24ebce6291456f" in := "50befd26fdf6e4d957c11e078b24ebce6291456f"
u160, _ := util.Uint160DecodeString(in) u160, _ := util.Uint160DecodeStringBE(in)
u160, _ = util.Uint160DecodeBytes(util.ArrayReverse(u160[:])) u160, _ = util.Uint160DecodeBytesBE(util.ArrayReverse(u160[:]))
p := Param{stringT, in} p := Param{stringT, in}
u, err := p.GetUint160FromHex() u, err := p.GetUint160FromHex()
assert.Equal(t, u160, u) assert.Equal(t, u160, u)

View file

@ -236,7 +236,7 @@ func (p StackParam) TryParse(dest interface{}) error {
} }
switch dest := dest.(type) { switch dest := dest.(type) {
case *util.Uint160: case *util.Uint160:
if *dest, err = util.Uint160DecodeBytes(data); err != nil { if *dest, err = util.Uint160DecodeBytesBE(data); err != nil {
return err return err
} }
return nil return nil

View file

@ -157,11 +157,11 @@ func TestStackParam_TryParse(t *testing.T) {
params := StackParams{ params := StackParams{
{ {
Type: ByteArray, Type: ByteArray,
Value: exp1.Bytes(), Value: exp1.BytesBE(),
}, },
{ {
Type: ByteArray, Type: ByteArray,
Value: exp2.Bytes(), Value: exp2.BytesBE(),
}, },
} }

View file

@ -41,7 +41,7 @@ func CreateRawContractTransaction(params ContractTxParams) (*transaction.Transac
tx.Attributes = append(tx.Attributes, tx.Attributes = append(tx.Attributes,
&transaction.Attribute{ &transaction.Attribute{
Usage: transaction.Script, Usage: transaction.Script,
Data: fromAddressHash.Bytes(), Data: fromAddressHash.BytesBE(),
}) })
if err = AddInputsAndUnspentsToTx(tx, fromAddress, assetID, amount, balancer); err != nil { if err = AddInputsAndUnspentsToTx(tx, fromAddress, assetID, amount, balancer); err != nil {
@ -194,7 +194,7 @@ func expandArrayIntoScript(script *bytes.Buffer, slice []Param) error {
if err != nil { if err != nil {
return err return err
} }
if err := vm.EmitBytes(script, hash.Bytes()); err != nil { if err := vm.EmitBytes(script, hash.BytesBE()); err != nil {
return err return err
} }
case Hash256: case Hash256:

View file

@ -45,7 +45,7 @@ func NewAccountState(a *core.AccountState) AccountState {
sort.Sort(balances) sort.Sort(balances)
// reverse scriptHash to be consistent with other client // reverse scriptHash to be consistent with other client
scriptHash, err := util.Uint160DecodeBytes(a.ScriptHash.BytesReverse()) scriptHash, err := util.Uint160DecodeBytesBE(a.ScriptHash.BytesLE())
if err != nil { if err != nil {
scriptHash = a.ScriptHash scriptHash = a.ScriptHash
} }

View file

@ -158,7 +158,7 @@ func adjustValToType(typ ParamType, val string) (interface{}, error) {
case Hash160Type: case Hash160Type:
u, err := crypto.Uint160DecodeAddress(val) u, err := crypto.Uint160DecodeAddress(val)
if err == nil { if err == nil {
return hex.EncodeToString(u.Bytes()), nil return hex.EncodeToString(u.BytesBE()), nil
} }
b, err := hex.DecodeString(val) b, err := hex.DecodeString(val)
if err != nil { if err != nil {

View file

@ -12,8 +12,8 @@ const uint160Size = 20
// Uint160 is a 20 byte long unsigned integer. // Uint160 is a 20 byte long unsigned integer.
type Uint160 [uint160Size]uint8 type Uint160 [uint160Size]uint8
// Uint160DecodeString attempts to decode the given string into an Uint160. // Uint160DecodeStringBE attempts to decode the given string into an Uint160.
func Uint160DecodeString(s string) (Uint160, error) { func Uint160DecodeStringBE(s string) (Uint160, error) {
var u Uint160 var u Uint160
if len(s) != uint160Size*2 { if len(s) != uint160Size*2 {
return u, fmt.Errorf("expected string size of %d got %d", uint160Size*2, len(s)) return u, fmt.Errorf("expected string size of %d got %d", uint160Size*2, len(s))
@ -22,11 +22,11 @@ func Uint160DecodeString(s string) (Uint160, error) {
if err != nil { if err != nil {
return u, err return u, err
} }
return Uint160DecodeBytes(b) return Uint160DecodeBytesBE(b)
} }
// Uint160DecodeBytes attempts to decode the given bytes into an Uint160. // Uint160DecodeBytesBE attempts to decode the given bytes into an Uint160.
func Uint160DecodeBytes(b []byte) (u Uint160, err error) { func Uint160DecodeBytesBE(b []byte) (u Uint160, err error) {
if len(b) != uint160Size { if len(b) != uint160Size {
return u, fmt.Errorf("expected byte size of %d got %d", uint160Size, len(b)) return u, fmt.Errorf("expected byte size of %d got %d", uint160Size, len(b))
} }
@ -34,24 +34,29 @@ func Uint160DecodeBytes(b []byte) (u Uint160, err error) {
return return
} }
// Bytes returns the byte slice representation of u. // BytesBE returns a big-endian byte representation of u.
func (u Uint160) Bytes() []byte { func (u Uint160) BytesBE() []byte {
return u[:] return u[:]
} }
// BytesReverse returns a reversed byte representation of u. // BytesLE returns a little-endian byte representation of u.
func (u Uint160) BytesReverse() []byte { func (u Uint160) BytesLE() []byte {
return ArrayReverse(u.Bytes()) return ArrayReverse(u.BytesBE())
} }
// String implements the stringer interface. // String implements the stringer interface.
func (u Uint160) String() string { func (u Uint160) String() string {
return hex.EncodeToString(u.Bytes()) return u.StringBE()
} }
// ReverseString is the same as String, but returns a reversed representation. // StringBE returns string representations of u with big-endian byte order.
func (u Uint160) ReverseString() string { func (u Uint160) StringBE() string {
return hex.EncodeToString(u.BytesReverse()) return hex.EncodeToString(u.BytesBE())
}
// StringLE returns string representations of u with little-endian byte order.
func (u Uint160) StringLE() string {
return hex.EncodeToString(u.BytesLE())
} }
// Equals returns true if both Uint256 values are the same. // Equals returns true if both Uint256 values are the same.
@ -78,7 +83,7 @@ func (u *Uint160) UnmarshalJSON(data []byte) (err error) {
return err return err
} }
js = strings.TrimPrefix(js, "0x") js = strings.TrimPrefix(js, "0x")
*u, err = Uint160DecodeString(js) *u, err = Uint160DecodeStringBE(js)
return err return err
} }

View file

@ -10,7 +10,7 @@ import (
func TestUint160UnmarshalJSON(t *testing.T) { func TestUint160UnmarshalJSON(t *testing.T) {
str := "2d3b96ae1bcc5a585e075e3b81920210dec16302" str := "2d3b96ae1bcc5a585e075e3b81920210dec16302"
expected, err := Uint160DecodeString(str) expected, err := Uint160DecodeStringBE(str)
assert.NoError(t, err) assert.NoError(t, err)
// UnmarshalJSON decodes hex-strings // UnmarshalJSON decodes hex-strings
@ -31,15 +31,15 @@ func TestUint160UnmarshalJSON(t *testing.T) {
func TestUInt160DecodeString(t *testing.T) { func TestUInt160DecodeString(t *testing.T) {
hexStr := "2d3b96ae1bcc5a585e075e3b81920210dec16302" hexStr := "2d3b96ae1bcc5a585e075e3b81920210dec16302"
val, err := Uint160DecodeString(hexStr) val, err := Uint160DecodeStringBE(hexStr)
assert.NoError(t, err) assert.NoError(t, err)
assert.Equal(t, hexStr, val.String()) assert.Equal(t, hexStr, val.String())
_, err = Uint160DecodeString(hexStr[1:]) _, err = Uint160DecodeStringBE(hexStr[1:])
assert.Error(t, err) assert.Error(t, err)
hexStr = "zz3b96ae1bcc5a585e075e3b81920210dec16302" hexStr = "zz3b96ae1bcc5a585e075e3b81920210dec16302"
_, err = Uint160DecodeString(hexStr) _, err = Uint160DecodeStringBE(hexStr)
assert.Error(t, err) assert.Error(t, err)
} }
@ -48,11 +48,11 @@ func TestUint160DecodeBytes(t *testing.T) {
b, err := hex.DecodeString(hexStr) b, err := hex.DecodeString(hexStr)
require.NoError(t, err) require.NoError(t, err)
val, err := Uint160DecodeBytes(b) val, err := Uint160DecodeBytesBE(b)
assert.NoError(t, err) assert.NoError(t, err)
assert.Equal(t, hexStr, val.String()) assert.Equal(t, hexStr, val.String())
_, err = Uint160DecodeBytes(b[1:]) _, err = Uint160DecodeBytesBE(b[1:])
assert.Error(t, err) assert.Error(t, err)
} }
@ -60,10 +60,10 @@ func TestUInt160Equals(t *testing.T) {
a := "2d3b96ae1bcc5a585e075e3b81920210dec16302" a := "2d3b96ae1bcc5a585e075e3b81920210dec16302"
b := "4d3b96ae1bcc5a585e075e3b81920210dec16302" b := "4d3b96ae1bcc5a585e075e3b81920210dec16302"
ua, err := Uint160DecodeString(a) ua, err := Uint160DecodeStringBE(a)
require.NoError(t, err) require.NoError(t, err)
ub, err := Uint160DecodeString(b) ub, err := Uint160DecodeStringBE(b)
require.NoError(t, err) require.NoError(t, err)
assert.False(t, ua.Equals(ub), "%s and %s cannot be equal", ua, ub) assert.False(t, ua.Equals(ub), "%s and %s cannot be equal", ua, ub)
assert.True(t, ua.Equals(ua), "%s and %s must be equal", ua, ua) assert.True(t, ua.Equals(ua), "%s and %s must be equal", ua, ua)
@ -73,11 +73,11 @@ func TestUInt160Less(t *testing.T) {
a := "2d3b96ae1bcc5a585e075e3b81920210dec16302" a := "2d3b96ae1bcc5a585e075e3b81920210dec16302"
b := "2d3b96ae1bcc5a585e075e3b81920210dec16303" b := "2d3b96ae1bcc5a585e075e3b81920210dec16303"
ua, err := Uint160DecodeString(a) ua, err := Uint160DecodeStringBE(a)
assert.Nil(t, err) assert.Nil(t, err)
ua2, err := Uint160DecodeString(a) ua2, err := Uint160DecodeStringBE(a)
assert.Nil(t, err) assert.Nil(t, err)
ub, err := Uint160DecodeString(b) ub, err := Uint160DecodeStringBE(b)
assert.Nil(t, err) assert.Nil(t, err)
assert.Equal(t, true, ua.Less(ub)) assert.Equal(t, true, ua.Less(ub))
assert.Equal(t, false, ua.Less(ua2)) assert.Equal(t, false, ua.Less(ua2))
@ -88,9 +88,9 @@ func TestUInt160String(t *testing.T) {
hexStr := "b28427088a3729b2536d10122960394e8be6721f" hexStr := "b28427088a3729b2536d10122960394e8be6721f"
hexRevStr := "1f72e68b4e39602912106d53b229378a082784b2" hexRevStr := "1f72e68b4e39602912106d53b229378a082784b2"
val, err := Uint160DecodeString(hexStr) val, err := Uint160DecodeStringBE(hexStr)
assert.Nil(t, err) assert.Nil(t, err)
assert.Equal(t, hexStr, val.String()) assert.Equal(t, hexStr, val.String())
assert.Equal(t, hexRevStr, val.ReverseString()) assert.Equal(t, hexRevStr, val.StringLE())
} }

View file

@ -118,7 +118,7 @@ func EmitAppCall(w *bytes.Buffer, scriptHash util.Uint160, tailCall bool) error
if tailCall { if tailCall {
op = opcode.TAILCALL op = opcode.TAILCALL
} }
return Emit(w, op, scriptHash.Bytes()) return Emit(w, op, scriptHash.BytesBE())
} }
// EmitAppCallWithOperationAndData emits an appcall with the given operation and data. // EmitAppCallWithOperationAndData emits an appcall with the given operation and data.

View file

@ -1112,7 +1112,7 @@ func (v *VM) execute(ctx *Context, op opcode.Opcode, parameter []byte) (err erro
v.checkInvocationStackSize() v.checkInvocationStackSize()
} }
hash, err := util.Uint160DecodeBytes(parameter) hash, err := util.Uint160DecodeBytesBE(parameter)
if err != nil { if err != nil {
panic(err) panic(err)
} }
@ -1296,7 +1296,7 @@ func (v *VM) execute(ctx *Context, op opcode.Opcode, parameter []byte) (err erro
case opcode.HASH160: case opcode.HASH160:
b := v.estack.Pop().Bytes() b := v.estack.Pop().Bytes()
v.estack.PushVal(hash.Hash160(b).Bytes()) v.estack.PushVal(hash.Hash160(b).BytesBE())
case opcode.HASH256: case opcode.HASH256:
b := v.estack.Pop().Bytes() b := v.estack.Pop().Bytes()
@ -1341,7 +1341,7 @@ func (v *VM) execute(ctx *Context, op opcode.Opcode, parameter []byte) (err erro
hashBytes = parameter[2:] hashBytes = parameter[2:]
} }
hash, err := util.Uint160DecodeBytes(hashBytes) hash, err := util.Uint160DecodeBytesBE(hashBytes)
if err != nil { if err != nil {
panic(err) panic(err)
} }

View file

@ -1403,7 +1403,7 @@ func TestSIGNByteArray(t *testing.T) {
func TestAppCall(t *testing.T) { func TestAppCall(t *testing.T) {
prog := []byte{byte(opcode.APPCALL)} prog := []byte{byte(opcode.APPCALL)}
hash := util.Uint160{} hash := util.Uint160{}
prog = append(prog, hash.Bytes()...) prog = append(prog, hash.BytesBE()...)
prog = append(prog, byte(opcode.RET)) prog = append(prog, byte(opcode.RET))
vm := load(prog) vm := load(prog)