diff --git a/cli/smartcontract/smart_contract.go b/cli/smartcontract/smart_contract.go index f80c93ce2..7c5d3c4f7 100644 --- a/cli/smartcontract/smart_contract.go +++ b/cli/smartcontract/smart_contract.go @@ -585,6 +585,6 @@ func contractDeploy(ctx *cli.Context) error { if err != nil { 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 } diff --git a/pkg/compiler/codegen.go b/pkg/compiler/codegen.go index 45c9dde69..42970458b 100644 --- a/pkg/compiler/codegen.go +++ b/pkg/compiler/codegen.go @@ -670,7 +670,7 @@ func (c *codegen) convertBuiltin(expr *ast.CallExpr) { c.prog.Err = err return } - bytes := uint160.Bytes() + bytes := uint160.BytesBE() emitBytes(c.prog.BinWriter, bytes) } } diff --git a/pkg/core/account_state.go b/pkg/core/account_state.go index f2779340c..638db3cf0 100644 --- a/pkg/core/account_state.go +++ b/pkg/core/account_state.go @@ -35,7 +35,7 @@ func (a Accounts) getAndUpdate(s storage.Store, hash util.Uint160) (*AccountStat // present there. Returns nil otherwise. func getAccountStateFromStore(s storage.Store, hash util.Uint160) (*AccountState, error) { var account *AccountState - key := storage.AppendPrefix(storage.STAccount, hash.Bytes()) + key := storage.AppendPrefix(storage.STAccount, hash.BytesBE()) b, err := s.Get(key) if err == nil { account = new(AccountState) @@ -55,7 +55,7 @@ func putAccountStateIntoStore(store storage.Store, as *AccountState) error { if buf.Err != nil { 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()) } diff --git a/pkg/core/blockchain.go b/pkg/core/blockchain.go index 1552aa14f..283fde0c8 100644 --- a/pkg/core/blockchain.go +++ b/pkg/core/blockchain.go @@ -602,7 +602,7 @@ func processValidatorStateDescriptor(descriptor *transaction.StateDescriptor, st } func processAccountStateDescriptor(descriptor *transaction.StateDescriptor, state *BlockChainState) error { - hash, err := util.Uint160DecodeBytes(descriptor.Key) + hash, err := util.Uint160DecodeBytesBE(descriptor.Key) if err != nil { return err } @@ -744,7 +744,7 @@ func (bc *Blockchain) GetStorageItems(hash util.Uint160) (map[string]*StorageIte // Cut prefix and hash. 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 { return nil, err } @@ -893,7 +893,7 @@ func (bc *Blockchain) GetContractState(hash util.Uint160) *ContractState { // getContractStateFromStore returns contract state as recorded in the given // store by the given script hash. 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) if err != nil { return nil @@ -1366,7 +1366,7 @@ func (bc *Blockchain) GetScriptHashesForVerifying(t *transaction.Transaction) ([ } for _, a := range t.Attributes { if a.Usage == transaction.Script { - h, err := util.Uint160DecodeBytes(a.Data) + h, err := util.Uint160DecodeBytesBE(a.Data) if err != nil { return nil, err } diff --git a/pkg/core/contract_state.go b/pkg/core/contract_state.go index 2b3a32138..6d976b209 100644 --- a/pkg/core/contract_state.go +++ b/pkg/core/contract_state.go @@ -70,13 +70,13 @@ func putContractStateIntoStore(s storage.Store, cs *ContractState) error { if buf.Err != nil { 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()) } // deleteContractStateInStore deletes given contract state in the given store. 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) } diff --git a/pkg/core/interop_neo.go b/pkg/core/interop_neo.go index 1aa46e532..89a38c8fb 100644 --- a/pkg/core/interop_neo.go +++ b/pkg/core/interop_neo.go @@ -74,7 +74,7 @@ func (ic *interopContext) headerGetNextConsensus(v *vm.VM) error { if err != nil { return err } - v.Estack().PushVal(header.NextConsensus.BytesReverse()) + v.Estack().PushVal(header.NextConsensus.BytesLE()) return nil } @@ -271,7 +271,7 @@ func (ic *interopContext) outputGetScriptHash(v *vm.VM) error { if err != nil { return err } - v.Estack().PushVal(output.ScriptHash.Bytes()) + v.Estack().PushVal(output.ScriptHash.BytesBE()) return nil } @@ -310,7 +310,7 @@ func (ic *interopContext) attrGetUsage(v *vm.VM) error { // bcGetAccount returns or creates an account. func (ic *interopContext) bcGetAccount(v *vm.VM) error { accbytes := v.Estack().Pop().Bytes() - acchash, err := util.Uint160DecodeBytes(accbytes) + acchash, err := util.Uint160DecodeBytesBE(accbytes) if err != nil { return err } @@ -364,7 +364,7 @@ func (ic *interopContext) accountGetScriptHash(v *vm.VM) error { if !ok { return fmt.Errorf("%T is not an account state", acc) } - v.Estack().PushVal(acc.ScriptHash.Bytes()) + v.Estack().PushVal(acc.ScriptHash.BytesBE()) return nil } @@ -389,7 +389,7 @@ func (ic *interopContext) accountGetVotes(v *vm.VM) error { // accountIsStandard checks whether given account is standard. func (ic *interopContext) accountIsStandard(v *vm.VM) error { accbytes := v.Estack().Pop().Bytes() - acchash, err := util.Uint160DecodeBytes(accbytes) + acchash, err := util.Uint160DecodeBytesBE(accbytes) if err != nil { return err } @@ -601,11 +601,11 @@ func (ic *interopContext) assetCreate(v *vm.VM) error { if !witnessOk { 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 { 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 { return gherr.Wrap(err, "failed to get issuer") } @@ -635,7 +635,7 @@ func (ic *interopContext) assetGetAdmin(v *vm.VM) error { if !ok { return fmt.Errorf("%T is not an asset state", as) } - v.Estack().PushVal(as.Admin.Bytes()) + v.Estack().PushVal(as.Admin.BytesBE()) return nil } @@ -690,7 +690,7 @@ func (ic *interopContext) assetGetIssuer(v *vm.VM) error { if !ok { return fmt.Errorf("%T is not an asset state", as) } - v.Estack().PushVal(as.Issuer.Bytes()) + v.Estack().PushVal(as.Issuer.BytesBE()) return nil } diff --git a/pkg/core/interop_neo_test.go b/pkg/core/interop_neo_test.go index eaaa84fc9..e43c2d118 100644 --- a/pkg/core/interop_neo_test.go +++ b/pkg/core/interop_neo_test.go @@ -74,7 +74,7 @@ func TestHeaderGetNextConsensus(t *testing.T) { err := context.headerGetNextConsensus(v) require.NoError(t, err) value := v.Estack().Pop().Value() - require.Equal(t, block.NextConsensus.BytesReverse(), value) + require.Equal(t, block.NextConsensus.BytesLE(), value) } func TestTxGetAttributes(t *testing.T) { @@ -168,7 +168,7 @@ func TestOutputGetScriptHash(t *testing.T) { err := context.outputGetScriptHash(v) require.NoError(t, err) 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) { @@ -208,7 +208,7 @@ func TestAccountGetScriptHash(t *testing.T) { err := context.accountGetScriptHash(v) require.NoError(t, err) hash := v.Estack().Pop().Value() - require.Equal(t, accState.ScriptHash.Bytes(), hash) + require.Equal(t, accState.ScriptHash.BytesBE(), hash) } func TestAccountGetVotes(t *testing.T) { @@ -248,7 +248,7 @@ func TestAssetGetAdmin(t *testing.T) { err := context.assetGetAdmin(v) require.NoError(t, err) admin := v.Estack().Pop().Value() - require.Equal(t, assetState.Admin.Bytes(), admin) + require.Equal(t, assetState.Admin.BytesBE(), admin) } func TestAssetGetAmount(t *testing.T) { @@ -298,7 +298,7 @@ func TestAssetGetIssuer(t *testing.T) { err := context.assetGetIssuer(v) require.NoError(t, err) issuer := v.Estack().Pop().Value() - require.Equal(t, assetState.Issuer.Bytes(), issuer) + require.Equal(t, assetState.Issuer.BytesBE(), issuer) } 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) { v := vm.New() rawHash := "4d3b96ae1bcc5a585e075e3b81920210dec16302" - hash, err := util.Uint160DecodeString(rawHash) + hash, err := util.Uint160DecodeStringBE(rawHash) accountState := NewAccountState(hash) key := &keys.PublicKey{X: big.NewInt(1), Y: big.NewInt(1)} diff --git a/pkg/core/interop_system.go b/pkg/core/interop_system.go index 54ddb4df3..bebfd7d0a 100644 --- a/pkg/core/interop_system.go +++ b/pkg/core/interop_system.go @@ -63,7 +63,7 @@ func (ic *interopContext) bcGetBlock(v *vm.VM) error { // bcGetContract returns contract. func (ic *interopContext) bcGetContract(v *vm.VM) error { hashbytes := v.Estack().Pop().Bytes() - hash, err := util.Uint160DecodeBytes(hashbytes) + hash, err := util.Uint160DecodeBytesBE(hashbytes) if err != nil { return err } @@ -260,7 +260,7 @@ func getContextScriptHash(v *vm.VM, n int) util.Uint160 { // invocation stack element number n. func pushContextScriptHash(v *vm.VM, n int) error { h := getContextScriptHash(v, n) - v.Estack().PushVal(h.Bytes()) + v.Estack().PushVal(h.BytesBE()) return nil } @@ -318,7 +318,7 @@ func (ic *interopContext) runtimeCheckWitness(v *vm.VM) error { var err error hashOrKey := v.Estack().Pop().Bytes() - hash, err := util.Uint160DecodeBytes(hashOrKey) + hash, err := util.Uint160DecodeBytesBE(hashOrKey) if err != nil { key := &keys.PublicKey{} err = key.DecodeBytes(hashOrKey) diff --git a/pkg/core/storage_item.go b/pkg/core/storage_item.go index a78de51dc..331104b46 100644 --- a/pkg/core/storage_item.go +++ b/pkg/core/storage_item.go @@ -14,7 +14,7 @@ type StorageItem struct { // makeStorageItemKey returns a key used to store StorageItem in the DB. 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. diff --git a/pkg/core/storage_item_test.go b/pkg/core/storage_item_test.go index d555cdd21..41bca1a94 100644 --- a/pkg/core/storage_item_test.go +++ b/pkg/core/storage_item_test.go @@ -14,7 +14,7 @@ func TestPutGetDeleteStorageItem(t *testing.T) { Value: []byte("smth"), } key := []byte("key") - cHash, err := util.Uint160DecodeBytes([]byte("abcdefghijklmnopqrst")) + cHash, err := util.Uint160DecodeBytesBE([]byte("abcdefghijklmnopqrst")) assert.Nil(t, err) assert.NoError(t, putStorageItemIntoStore(s, cHash, key, si)) siRead := getStorageItemFromStore(s, cHash, key) diff --git a/pkg/core/transaction/register_test.go b/pkg/core/transaction/register_test.go index 77ee583ac..a1b7c89d0 100644 --- a/pkg/core/transaction/register_test.go +++ b/pkg/core/transaction/register_test.go @@ -12,7 +12,7 @@ import ( ) func TestRegisterTX(t *testing.T) { - someuint160, _ := util.Uint160DecodeString("4d3b96ae1bcc5a585e075e3b81920210dec16302") + someuint160, _ := util.Uint160DecodeStringBE("4d3b96ae1bcc5a585e075e3b81920210dec16302") tx := &Transaction{ Type: RegisterType, Version: 0, diff --git a/pkg/crypto/address.go b/pkg/crypto/address.go index ca2cbc55f..3fb69017d 100644 --- a/pkg/crypto/address.go +++ b/pkg/crypto/address.go @@ -8,7 +8,7 @@ import ( // Uint160. func AddressFromUint160(u util.Uint160) string { // 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) } @@ -19,5 +19,5 @@ func Uint160DecodeAddress(s string) (u util.Uint160, err error) { if err != nil { return u, err } - return util.Uint160DecodeBytes(b[1:21]) + return util.Uint160DecodeBytesBE(b[1:21]) } diff --git a/pkg/crypto/address_test.go b/pkg/crypto/address_test.go index c4d54fd2c..62315eec0 100644 --- a/pkg/crypto/address_test.go +++ b/pkg/crypto/address_test.go @@ -29,6 +29,6 @@ func TestUint160DecodeKnownAddress(t *testing.T) { t.Fatal(err) } - assert.Equal(t, "b28427088a3729b2536d10122960394e8be6721f", val.ReverseString()) + assert.Equal(t, "b28427088a3729b2536d10122960394e8be6721f", val.StringLE()) assert.Equal(t, "1f72e68b4e39602912106d53b229378a082784b2", val.String()) } diff --git a/pkg/crypto/hash/hash.go b/pkg/crypto/hash/hash.go index 6a42f71a4..0f26ccbde 100644 --- a/pkg/crypto/hash/hash.go +++ b/pkg/crypto/hash/hash.go @@ -30,7 +30,7 @@ func RipeMD160(data []byte) util.Uint160 { hasher := ripemd160.New() _, _ = hasher.Write(data) - hash, _ = util.Uint160DecodeBytes(hasher.Sum(nil)) + hash, _ = util.Uint160DecodeBytesBE(hasher.Sum(nil)) return hash } @@ -41,7 +41,7 @@ func Hash160(data []byte) util.Uint160 { h1 := Sha256(data) h2 := RipeMD160(h1.Bytes()) - hash, _ = util.Uint160DecodeBytes(h2.Bytes()) + hash, _ = util.Uint160DecodeBytesBE(h2.BytesBE()) return hash } diff --git a/pkg/crypto/hash/hash_test.go b/pkg/crypto/hash/hash_test.go index 105bfd4b6..78f984c1d 100644 --- a/pkg/crypto/hash/hash_test.go +++ b/pkg/crypto/hash/hash_test.go @@ -36,7 +36,7 @@ func TestHashRipeMD160(t *testing.T) { data := RipeMD160(input) expected := "108f07b8382412612c048d07d13f814118445acd" - actual := hex.EncodeToString(data.Bytes()) + actual := hex.EncodeToString(data.BytesBE()) assert.Equal(t, expected, actual) } @@ -46,7 +46,7 @@ func TestHash160(t *testing.T) { data := Hash160(publicKeyBytes) expected := "c8e2b685cc70ec96743b55beb9449782f8f775d8" - actual := hex.EncodeToString(data.Bytes()) + actual := hex.EncodeToString(data.BytesBE()) assert.Equal(t, expected, actual) } diff --git a/pkg/crypto/keys/publickey.go b/pkg/crypto/keys/publickey.go index debe09d1e..8f5f3fde9 100644 --- a/pkg/crypto/keys/publickey.go +++ b/pkg/crypto/keys/publickey.go @@ -237,7 +237,7 @@ func (p *PublicKey) GetVerificationScript() []byte { func (p *PublicKey) Signature() []byte { sig := hash.Hash160(p.GetVerificationScript()) - return sig.Bytes() + return sig.BytesBE() } // Address returns a base58-encoded NEO-specific address based on the key hash. diff --git a/pkg/rpc/param.go b/pkg/rpc/param.go index 1e6ee7972..9e73dfb7c 100644 --- a/pkg/rpc/param.go +++ b/pkg/rpc/param.go @@ -85,11 +85,11 @@ func (p Param) GetUint160FromHex() (util.Uint160, error) { return util.Uint160{}, err } - scriptHashLE, err := util.Uint160DecodeString(s) + scriptHashLE, err := util.Uint160DecodeStringBE(s) if err != nil { return util.Uint160{}, err } - return util.Uint160DecodeBytes(scriptHashLE.BytesReverse()) + return util.Uint160DecodeBytesBE(scriptHashLE.BytesLE()) } // GetUint160FromAddress returns Uint160 value of the parameter that was diff --git a/pkg/rpc/param_test.go b/pkg/rpc/param_test.go index cd4465bfb..1e5aa4a1d 100644 --- a/pkg/rpc/param_test.go +++ b/pkg/rpc/param_test.go @@ -112,8 +112,8 @@ func TestParamGetUint256(t *testing.T) { func TestParamGetUint160FromHex(t *testing.T) { in := "50befd26fdf6e4d957c11e078b24ebce6291456f" - u160, _ := util.Uint160DecodeString(in) - u160, _ = util.Uint160DecodeBytes(util.ArrayReverse(u160[:])) + u160, _ := util.Uint160DecodeStringBE(in) + u160, _ = util.Uint160DecodeBytesBE(util.ArrayReverse(u160[:])) p := Param{stringT, in} u, err := p.GetUint160FromHex() assert.Equal(t, u160, u) diff --git a/pkg/rpc/stack_param.go b/pkg/rpc/stack_param.go index cf7e4c72c..f87f9e168 100644 --- a/pkg/rpc/stack_param.go +++ b/pkg/rpc/stack_param.go @@ -236,7 +236,7 @@ func (p StackParam) TryParse(dest interface{}) error { } switch dest := dest.(type) { case *util.Uint160: - if *dest, err = util.Uint160DecodeBytes(data); err != nil { + if *dest, err = util.Uint160DecodeBytesBE(data); err != nil { return err } return nil diff --git a/pkg/rpc/stack_param_test.go b/pkg/rpc/stack_param_test.go index 8e7b9965b..dfd289e19 100644 --- a/pkg/rpc/stack_param_test.go +++ b/pkg/rpc/stack_param_test.go @@ -157,11 +157,11 @@ func TestStackParam_TryParse(t *testing.T) { params := StackParams{ { Type: ByteArray, - Value: exp1.Bytes(), + Value: exp1.BytesBE(), }, { Type: ByteArray, - Value: exp2.Bytes(), + Value: exp2.BytesBE(), }, } diff --git a/pkg/rpc/txBuilder.go b/pkg/rpc/txBuilder.go index 28c31b24b..cc64ba518 100644 --- a/pkg/rpc/txBuilder.go +++ b/pkg/rpc/txBuilder.go @@ -41,7 +41,7 @@ func CreateRawContractTransaction(params ContractTxParams) (*transaction.Transac tx.Attributes = append(tx.Attributes, &transaction.Attribute{ Usage: transaction.Script, - Data: fromAddressHash.Bytes(), + Data: fromAddressHash.BytesBE(), }) 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 { return err } - if err := vm.EmitBytes(script, hash.Bytes()); err != nil { + if err := vm.EmitBytes(script, hash.BytesBE()); err != nil { return err } case Hash256: diff --git a/pkg/rpc/wrappers/account_state.go b/pkg/rpc/wrappers/account_state.go index 688a61e46..dcb6530f8 100644 --- a/pkg/rpc/wrappers/account_state.go +++ b/pkg/rpc/wrappers/account_state.go @@ -45,7 +45,7 @@ func NewAccountState(a *core.AccountState) AccountState { sort.Sort(balances) // 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 { scriptHash = a.ScriptHash } diff --git a/pkg/smartcontract/param_context.go b/pkg/smartcontract/param_context.go index ad509f17d..17cbbd566 100644 --- a/pkg/smartcontract/param_context.go +++ b/pkg/smartcontract/param_context.go @@ -158,7 +158,7 @@ func adjustValToType(typ ParamType, val string) (interface{}, error) { case Hash160Type: u, err := crypto.Uint160DecodeAddress(val) if err == nil { - return hex.EncodeToString(u.Bytes()), nil + return hex.EncodeToString(u.BytesBE()), nil } b, err := hex.DecodeString(val) if err != nil { diff --git a/pkg/util/uint160.go b/pkg/util/uint160.go index 0a7f35a6c..aa7b2490d 100644 --- a/pkg/util/uint160.go +++ b/pkg/util/uint160.go @@ -12,8 +12,8 @@ const uint160Size = 20 // Uint160 is a 20 byte long unsigned integer. type Uint160 [uint160Size]uint8 -// Uint160DecodeString attempts to decode the given string into an Uint160. -func Uint160DecodeString(s string) (Uint160, error) { +// Uint160DecodeStringBE attempts to decode the given string into an Uint160. +func Uint160DecodeStringBE(s string) (Uint160, error) { var u Uint160 if len(s) != uint160Size*2 { 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 { return u, err } - return Uint160DecodeBytes(b) + return Uint160DecodeBytesBE(b) } -// Uint160DecodeBytes attempts to decode the given bytes into an Uint160. -func Uint160DecodeBytes(b []byte) (u Uint160, err error) { +// Uint160DecodeBytesBE attempts to decode the given bytes into an Uint160. +func Uint160DecodeBytesBE(b []byte) (u Uint160, err error) { if len(b) != uint160Size { 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 } -// Bytes returns the byte slice representation of u. -func (u Uint160) Bytes() []byte { +// BytesBE returns a big-endian byte representation of u. +func (u Uint160) BytesBE() []byte { return u[:] } -// BytesReverse returns a reversed byte representation of u. -func (u Uint160) BytesReverse() []byte { - return ArrayReverse(u.Bytes()) +// BytesLE returns a little-endian byte representation of u. +func (u Uint160) BytesLE() []byte { + return ArrayReverse(u.BytesBE()) } // String implements the stringer interface. func (u Uint160) String() string { - return hex.EncodeToString(u.Bytes()) + return u.StringBE() } -// ReverseString is the same as String, but returns a reversed representation. -func (u Uint160) ReverseString() string { - return hex.EncodeToString(u.BytesReverse()) +// StringBE returns string representations of u with big-endian byte order. +func (u Uint160) StringBE() string { + 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. @@ -78,7 +83,7 @@ func (u *Uint160) UnmarshalJSON(data []byte) (err error) { return err } js = strings.TrimPrefix(js, "0x") - *u, err = Uint160DecodeString(js) + *u, err = Uint160DecodeStringBE(js) return err } diff --git a/pkg/util/uint160_test.go b/pkg/util/uint160_test.go index f8e37d297..6a489f1a7 100644 --- a/pkg/util/uint160_test.go +++ b/pkg/util/uint160_test.go @@ -10,7 +10,7 @@ import ( func TestUint160UnmarshalJSON(t *testing.T) { str := "2d3b96ae1bcc5a585e075e3b81920210dec16302" - expected, err := Uint160DecodeString(str) + expected, err := Uint160DecodeStringBE(str) assert.NoError(t, err) // UnmarshalJSON decodes hex-strings @@ -31,15 +31,15 @@ func TestUint160UnmarshalJSON(t *testing.T) { func TestUInt160DecodeString(t *testing.T) { hexStr := "2d3b96ae1bcc5a585e075e3b81920210dec16302" - val, err := Uint160DecodeString(hexStr) + val, err := Uint160DecodeStringBE(hexStr) assert.NoError(t, err) assert.Equal(t, hexStr, val.String()) - _, err = Uint160DecodeString(hexStr[1:]) + _, err = Uint160DecodeStringBE(hexStr[1:]) assert.Error(t, err) hexStr = "zz3b96ae1bcc5a585e075e3b81920210dec16302" - _, err = Uint160DecodeString(hexStr) + _, err = Uint160DecodeStringBE(hexStr) assert.Error(t, err) } @@ -48,11 +48,11 @@ func TestUint160DecodeBytes(t *testing.T) { b, err := hex.DecodeString(hexStr) require.NoError(t, err) - val, err := Uint160DecodeBytes(b) + val, err := Uint160DecodeBytesBE(b) assert.NoError(t, err) assert.Equal(t, hexStr, val.String()) - _, err = Uint160DecodeBytes(b[1:]) + _, err = Uint160DecodeBytesBE(b[1:]) assert.Error(t, err) } @@ -60,10 +60,10 @@ func TestUInt160Equals(t *testing.T) { a := "2d3b96ae1bcc5a585e075e3b81920210dec16302" b := "4d3b96ae1bcc5a585e075e3b81920210dec16302" - ua, err := Uint160DecodeString(a) + ua, err := Uint160DecodeStringBE(a) require.NoError(t, err) - ub, err := Uint160DecodeString(b) + ub, err := Uint160DecodeStringBE(b) require.NoError(t, err) 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) @@ -73,11 +73,11 @@ func TestUInt160Less(t *testing.T) { a := "2d3b96ae1bcc5a585e075e3b81920210dec16302" b := "2d3b96ae1bcc5a585e075e3b81920210dec16303" - ua, err := Uint160DecodeString(a) + ua, err := Uint160DecodeStringBE(a) assert.Nil(t, err) - ua2, err := Uint160DecodeString(a) + ua2, err := Uint160DecodeStringBE(a) assert.Nil(t, err) - ub, err := Uint160DecodeString(b) + ub, err := Uint160DecodeStringBE(b) assert.Nil(t, err) assert.Equal(t, true, ua.Less(ub)) assert.Equal(t, false, ua.Less(ua2)) @@ -88,9 +88,9 @@ func TestUInt160String(t *testing.T) { hexStr := "b28427088a3729b2536d10122960394e8be6721f" hexRevStr := "1f72e68b4e39602912106d53b229378a082784b2" - val, err := Uint160DecodeString(hexStr) + val, err := Uint160DecodeStringBE(hexStr) assert.Nil(t, err) assert.Equal(t, hexStr, val.String()) - assert.Equal(t, hexRevStr, val.ReverseString()) + assert.Equal(t, hexRevStr, val.StringLE()) } diff --git a/pkg/vm/emit.go b/pkg/vm/emit.go index f9c2f4a78..72720c2f3 100644 --- a/pkg/vm/emit.go +++ b/pkg/vm/emit.go @@ -118,7 +118,7 @@ func EmitAppCall(w *bytes.Buffer, scriptHash util.Uint160, tailCall bool) error if 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. diff --git a/pkg/vm/vm.go b/pkg/vm/vm.go index 9eb83f209..c9f13d437 100644 --- a/pkg/vm/vm.go +++ b/pkg/vm/vm.go @@ -1112,7 +1112,7 @@ func (v *VM) execute(ctx *Context, op opcode.Opcode, parameter []byte) (err erro v.checkInvocationStackSize() } - hash, err := util.Uint160DecodeBytes(parameter) + hash, err := util.Uint160DecodeBytesBE(parameter) if err != nil { panic(err) } @@ -1296,7 +1296,7 @@ func (v *VM) execute(ctx *Context, op opcode.Opcode, parameter []byte) (err erro case opcode.HASH160: b := v.estack.Pop().Bytes() - v.estack.PushVal(hash.Hash160(b).Bytes()) + v.estack.PushVal(hash.Hash160(b).BytesBE()) case opcode.HASH256: 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:] } - hash, err := util.Uint160DecodeBytes(hashBytes) + hash, err := util.Uint160DecodeBytesBE(hashBytes) if err != nil { panic(err) } diff --git a/pkg/vm/vm_test.go b/pkg/vm/vm_test.go index 7f89eb6e7..4f651594b 100644 --- a/pkg/vm/vm_test.go +++ b/pkg/vm/vm_test.go @@ -1403,7 +1403,7 @@ func TestSIGNByteArray(t *testing.T) { func TestAppCall(t *testing.T) { prog := []byte{byte(opcode.APPCALL)} hash := util.Uint160{} - prog = append(prog, hash.Bytes()...) + prog = append(prog, hash.BytesBE()...) prog = append(prog, byte(opcode.RET)) vm := load(prog)