From b246653f6260d9a6c587d23dd340a3a996f8ef97 Mon Sep 17 00:00:00 2001 From: Roman Khimov Date: Wed, 25 Dec 2019 17:34:18 +0300 Subject: [PATCH] address: rename functions as per #579 comments Make them more clear to understand. --- integration/performance_test.go | 2 +- pkg/compiler/codegen.go | 2 +- pkg/core/block_test.go | 4 ++-- pkg/core/transaction/output.go | 2 +- pkg/core/transaction/register_test.go | 2 +- pkg/core/transaction/transaction_test.go | 2 +- pkg/core/util_test.go | 2 +- pkg/crypto/keys/publickey.go | 2 +- pkg/encoding/address/address.go | 8 ++++---- pkg/encoding/address/address_test.go | 10 +++++----- pkg/rpc/param.go | 2 +- pkg/rpc/param_test.go | 2 +- pkg/rpc/txBuilder.go | 6 +++--- pkg/rpc/wrappers/asset_state.go | 4 ++-- pkg/rpc/wrappers/validate_address.go | 2 +- pkg/smartcontract/param_context.go | 4 ++-- 16 files changed, 28 insertions(+), 28 deletions(-) diff --git a/integration/performance_test.go b/integration/performance_test.go index e4dce2490..d81c42e58 100644 --- a/integration/performance_test.go +++ b/integration/performance_test.go @@ -72,7 +72,7 @@ func getWif(t *testing.B) *keys.WIF { // getTX returns Invocation transaction with some random attributes in order to have different hashes. func getTX(t *testing.B, wif *keys.WIF) *transaction.Transaction { fromAddress := wif.PrivateKey.Address() - fromAddressHash, err := address.DecodeUint160(fromAddress) + fromAddressHash, err := address.StringToUint160(fromAddress) require.NoError(t, err) tx := &transaction.Transaction{ diff --git a/pkg/compiler/codegen.go b/pkg/compiler/codegen.go index 3cd354fd8..3544a38bc 100644 --- a/pkg/compiler/codegen.go +++ b/pkg/compiler/codegen.go @@ -673,7 +673,7 @@ func (c *codegen) convertBuiltin(expr *ast.CallExpr) { // contain double quotes that need to be stripped. addressStr := expr.Args[0].(*ast.BasicLit).Value addressStr = strings.Replace(addressStr, "\"", "", 2) - uint160, err := address.DecodeUint160(addressStr) + uint160, err := address.StringToUint160(addressStr) if err != nil { c.prog.Err = err return diff --git a/pkg/core/block_test.go b/pkg/core/block_test.go index 48b4d75a8..82118b6ea 100644 --- a/pkg/core/block_test.go +++ b/pkg/core/block_test.go @@ -34,7 +34,7 @@ func TestDecodeBlock1(t *testing.T) { assert.Equal(t, data["hash"].(string), block.Hash().StringLE()) assert.Equal(t, data["previousblockhash"].(string), block.PrevHash.StringLE()) assert.Equal(t, data["merkleroot"].(string), block.MerkleRoot.StringLE()) - assert.Equal(t, data["nextconsensus"].(string), address.EncodeUint160(block.NextConsensus)) + assert.Equal(t, data["nextconsensus"].(string), address.Uint160ToString(block.NextConsensus)) script := data["script"].(map[string]interface{}) assert.Equal(t, script["invocation"].(string), hex.EncodeToString(block.Script.InvocationScript)) @@ -273,7 +273,7 @@ func TestBlockSizeCalculation(t *testing.T) { assert.Equal(t, 1527894405, int(b.Timestamp)) assert.Equal(t, 2340363, int(b.Index)) - nextConsensus := address.EncodeUint160(b.NextConsensus) + nextConsensus := address.Uint160ToString(b.NextConsensus) assert.Equal(t, "APyEx5f4Zm4oCHwFWiSTaph1fPBxZacYVR", nextConsensus) assert.Equal(t, "4012afae6df64195041e4764b57caa9e27fc2cfc596833163904136ec95816d104b44b3737d0e9f6b1b4445cd3b6a5cc80f6b0935675bc44dba44415eb309832b3404dc95bcf85e4635556a1d618e4ce947b26972992ed74788df5f9501b850ac0b40b7112d1ff30e4ade00369e16f0d13932d1ba76725e7682db072f8e2cd7752b840d12bb7dd45dd3b0e2098db5c67b6de55b7c40164937491fcaca1239b25860251224ead23ab232add78ccccd347239eae50ffc98f50b2a84c60ec5c3d284647a7406fabf6ca241b759af6b71080c0dfad7395632e989226a7e52f8cd2c133aeb2226e6e1aea47666fd81f578405a9f9bbd9d0bc523c3a44d7a5099ddc649feabe5f406188b8ee478731a89beeb76fdbd108eb0071b8f2b8678f40c5a1f387a491314336783255dee8cc5af4bf914dfeaacecc318fc13e02262658e39e8ce0631941b1", hex.EncodeToString(b.Script.InvocationScript)) diff --git a/pkg/core/transaction/output.go b/pkg/core/transaction/output.go index 06058d674..e8cf91ea2 100644 --- a/pkg/core/transaction/output.go +++ b/pkg/core/transaction/output.go @@ -52,7 +52,7 @@ func (out *Output) MarshalJSON() ([]byte, error) { return json.Marshal(map[string]interface{}{ "asset": out.AssetID, "value": out.Amount, - "address": address.EncodeUint160(out.ScriptHash), + "address": address.Uint160ToString(out.ScriptHash), "n": out.Position, }) } diff --git a/pkg/core/transaction/register_test.go b/pkg/core/transaction/register_test.go index 37662381b..ba073bbf5 100644 --- a/pkg/core/transaction/register_test.go +++ b/pkg/core/transaction/register_test.go @@ -58,7 +58,7 @@ func TestDecodeRegisterTXFromRawString(t *testing.T) { assert.Equal(t, util.Fixed8FromInt64(100000000), txData.Amount) assert.Equal(t, uint8(0), txData.Precision) assert.Equal(t, keys.PublicKey{}, txData.Owner) - assert.Equal(t, "Abf2qMs1pzQb8kYk9RuxtUb9jtRKJVuBJt", address.EncodeUint160(txData.Admin)) + assert.Equal(t, "Abf2qMs1pzQb8kYk9RuxtUb9jtRKJVuBJt", address.Uint160ToString(txData.Admin)) assert.Equal(t, "c56f33fc6ecfcd0c225c4ab356fee59390af8560be0e930faebe74a6daff7c9b", tx.Hash().StringLE()) buf := io.NewBufBinWriter() diff --git a/pkg/core/transaction/transaction_test.go b/pkg/core/transaction/transaction_test.go index 5f8ea7e49..3403a0c6b 100644 --- a/pkg/core/transaction/transaction_test.go +++ b/pkg/core/transaction/transaction_test.go @@ -51,7 +51,7 @@ func TestDecodeEncodeClaimTX(t *testing.T) { assert.Equal(t, 0, len(tx.Attributes)) assert.Equal(t, 0, len(tx.Inputs)) assert.Equal(t, 1, len(tx.Outputs)) - assert.Equal(t, "AQJseD8iBmCD4sgfHRhMahmoi9zvopG6yz", address.EncodeUint160(tx.Outputs[0].ScriptHash)) + assert.Equal(t, "AQJseD8iBmCD4sgfHRhMahmoi9zvopG6yz", address.Uint160ToString(tx.Outputs[0].ScriptHash)) assert.Equal(t, "602c79718b16e442de58778e148d0b1084e3b2dffd5de6b7b16cee7969282de7", tx.Outputs[0].AssetID.StringLE()) assert.Equal(t, tx.Outputs[0].Amount.String(), "0.06247739") invoc := "40456349cec43053009accdb7781b0799c6b591c812768804ab0a0b56b5eae7a97694227fcd33e70899c075848b2cee8fae733faac6865b484d3f7df8949e2aadb" diff --git a/pkg/core/util_test.go b/pkg/core/util_test.go index 968a06fc7..b2222a56a 100644 --- a/pkg/core/util_test.go +++ b/pkg/core/util_test.go @@ -45,7 +45,7 @@ func TestGetConsensusAddressMainNet(t *testing.T) { } assert.Equal(t, consensusScript, script.String()) - assert.Equal(t, consensusAddr, address.EncodeUint160(script)) + assert.Equal(t, consensusAddr, address.Uint160ToString(script)) } func TestUtilityTokenTX(t *testing.T) { diff --git a/pkg/crypto/keys/publickey.go b/pkg/crypto/keys/publickey.go index 13aba0106..edf033998 100644 --- a/pkg/crypto/keys/publickey.go +++ b/pkg/crypto/keys/publickey.go @@ -244,7 +244,7 @@ func (p *PublicKey) Signature() []byte { func (p *PublicKey) Address() string { sig := hash.Hash160(p.GetVerificationScript()) - return address.EncodeUint160(sig) + return address.Uint160ToString(sig) } // Verify returns true if the signature is valid and corresponds diff --git a/pkg/encoding/address/address.go b/pkg/encoding/address/address.go index 02548d100..e02cca4ce 100644 --- a/pkg/encoding/address/address.go +++ b/pkg/encoding/address/address.go @@ -11,16 +11,16 @@ import ( // be changed and defaults to 23 (0x17), the standard NEO prefix. var Prefix = byte(0x17) -// EncodeUint160 returns the "NEO address" from the given Uint160. -func EncodeUint160(u util.Uint160) string { +// Uint160ToString returns the "NEO address" from the given Uint160. +func Uint160ToString(u util.Uint160) string { // Dont forget to prepend the Address version 0x17 (23) A b := append([]byte{Prefix}, u.BytesBE()...) return base58.CheckEncode(b) } -// DecodeUint160 attempts to decode the given NEO address string +// StringToUint160 attempts to decode the given NEO address string // into an Uint160. -func DecodeUint160(s string) (u util.Uint160, err error) { +func StringToUint160(s string) (u util.Uint160, err error) { b, err := base58.CheckDecode(s) if err != nil { return u, err diff --git a/pkg/encoding/address/address_test.go b/pkg/encoding/address/address_test.go index b9bf1f488..db7d24c03 100644 --- a/pkg/encoding/address/address_test.go +++ b/pkg/encoding/address/address_test.go @@ -14,18 +14,18 @@ func TestUint160DecodeEncodeAddress(t *testing.T) { "AMxkaxFVG8Q1BhnB4fjTA5ZmUTEnnTMJMa", } for _, addr := range addrs { - val, err := DecodeUint160(addr) + val, err := StringToUint160(addr) if err != nil { t.Fatal(err) } - assert.Equal(t, addr, EncodeUint160(val)) + assert.Equal(t, addr, Uint160ToString(val)) } } func TestUint160DecodeKnownAddress(t *testing.T) { address := "AJeAEsmeD6t279Dx4n2HWdUvUmmXQ4iJvP" - val, err := DecodeUint160(address) + val, err := StringToUint160(address) if err != nil { t.Fatal(err) } @@ -37,7 +37,7 @@ func TestUint160DecodeKnownAddress(t *testing.T) { func TestUint160DecodeBadBase58(t *testing.T) { address := "AJeAEsmeD6t279Dx4n2HWdUvUmmXQ4iJv@" - _, err := DecodeUint160(address) + _, err := StringToUint160(address) require.Error(t, err) } @@ -45,6 +45,6 @@ func TestUint160DecodeBadPrefix(t *testing.T) { // The same AJeAEsmeD6t279Dx4n2HWdUvUmmXQ4iJvP key encoded with 0x18 prefix. address := "AhymDz4vvHLtvaN36CMbzkki7H2U8ENb8F" - _, err := DecodeUint160(address) + _, err := StringToUint160(address) require.Error(t, err) } diff --git a/pkg/rpc/param.go b/pkg/rpc/param.go index af612e4a0..1dc57eb07 100644 --- a/pkg/rpc/param.go +++ b/pkg/rpc/param.go @@ -96,7 +96,7 @@ func (p Param) GetUint160FromAddress() (util.Uint160, error) { return util.Uint160{}, err } - return address.DecodeUint160(s) + return address.StringToUint160(s) } // GetFuncParam returns current parameter as a function call parameter. diff --git a/pkg/rpc/param_test.go b/pkg/rpc/param_test.go index fcd0450b8..58cc9b879 100644 --- a/pkg/rpc/param_test.go +++ b/pkg/rpc/param_test.go @@ -129,7 +129,7 @@ func TestParamGetUint160FromHex(t *testing.T) { func TestParamGetUint160FromAddress(t *testing.T) { in := "AK2nJJpJr6o664CWJKi1QRXjqeic2zRp8y" - u160, _ := address.DecodeUint160(in) + u160, _ := address.StringToUint160(in) p := Param{stringT, in} u, err := p.GetUint160FromAddress() assert.Equal(t, u160, u) diff --git a/pkg/rpc/txBuilder.go b/pkg/rpc/txBuilder.go index 111321a1d..6a31f29ec 100644 --- a/pkg/rpc/txBuilder.go +++ b/pkg/rpc/txBuilder.go @@ -31,11 +31,11 @@ func CreateRawContractTransaction(params ContractTxParams) (*transaction.Transac fromAddress = wif.PrivateKey.Address() - if fromAddressHash, err = address.DecodeUint160(fromAddress); err != nil { + if fromAddressHash, err = address.StringToUint160(fromAddress); err != nil { return nil, errs.Wrapf(err, "Failed to take script hash from address: %v", fromAddress) } - if toAddressHash, err = address.DecodeUint160(toAddress); err != nil { + if toAddressHash, err = address.StringToUint160(toAddress); err != nil { return nil, errs.Wrapf(err, "Failed to take script hash from address: %v", toAddress) } tx.Attributes = append(tx.Attributes, @@ -59,7 +59,7 @@ func CreateRawContractTransaction(params ContractTxParams) (*transaction.Transac // AddInputsAndUnspentsToTx adds inputs needed to transaction and one output // with change. func AddInputsAndUnspentsToTx(tx *transaction.Transaction, addr string, assetID util.Uint256, amount util.Fixed8, balancer BalanceGetter) error { - scriptHash, err := address.DecodeUint160(addr) + scriptHash, err := address.StringToUint160(addr) if err != nil { return errs.Wrapf(err, "failed to take script hash from address: %v", addr) } diff --git a/pkg/rpc/wrappers/asset_state.go b/pkg/rpc/wrappers/asset_state.go index a50d75045..5ee365a97 100644 --- a/pkg/rpc/wrappers/asset_state.go +++ b/pkg/rpc/wrappers/asset_state.go @@ -37,8 +37,8 @@ func NewAssetState(a *state.Asset) AssetState { FeeMode: a.FeeMode, FeeAddress: a.FeeAddress, Owner: a.Owner.String(), - Admin: address.EncodeUint160(a.Admin), - Issuer: address.EncodeUint160(a.Issuer), + Admin: address.Uint160ToString(a.Admin), + Issuer: address.Uint160ToString(a.Issuer), Expiration: a.Expiration, IsFrozen: a.IsFrozen, } diff --git a/pkg/rpc/wrappers/validate_address.go b/pkg/rpc/wrappers/validate_address.go index 5580719fe..dfde7b833 100644 --- a/pkg/rpc/wrappers/validate_address.go +++ b/pkg/rpc/wrappers/validate_address.go @@ -15,7 +15,7 @@ type ValidateAddressResponse struct { func ValidateAddress(addr interface{}) ValidateAddressResponse { resp := ValidateAddressResponse{Address: addr} if addr, ok := addr.(string); ok { - _, err := address.DecodeUint160(addr) + _, err := address.StringToUint160(addr) resp.IsValid = err == nil } return resp diff --git a/pkg/smartcontract/param_context.go b/pkg/smartcontract/param_context.go index d2bff6843..b53b0cadd 100644 --- a/pkg/smartcontract/param_context.go +++ b/pkg/smartcontract/param_context.go @@ -156,7 +156,7 @@ func adjustValToType(typ ParamType, val string) (interface{}, error) { case IntegerType: return strconv.Atoi(val) case Hash160Type: - u, err := address.DecodeUint160(val) + u, err := address.StringToUint160(val) if err == nil { return hex.EncodeToString(u.BytesBE()), nil } @@ -215,7 +215,7 @@ func inferParamType(val string) ParamType { return BoolType } - _, err = address.DecodeUint160(val) + _, err = address.StringToUint160(val) if err == nil { return Hash160Type }