address: move into its own package

Doesn't really belong to the crypto.
This commit is contained in:
Roman Khimov 2019-12-25 15:22:02 +03:00
parent a025b9c42d
commit e685e9bf9a
15 changed files with 48 additions and 50 deletions

View file

@ -8,8 +8,8 @@ import (
"github.com/CityOfZion/neo-go/pkg/core"
"github.com/CityOfZion/neo-go/pkg/core/storage"
"github.com/CityOfZion/neo-go/pkg/core/transaction"
"github.com/CityOfZion/neo-go/pkg/crypto"
"github.com/CityOfZion/neo-go/pkg/crypto/keys"
"github.com/CityOfZion/neo-go/pkg/encoding/address"
"github.com/CityOfZion/neo-go/pkg/network"
"github.com/CityOfZion/neo-go/pkg/rpc"
"github.com/stretchr/testify/require"
@ -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 := crypto.Uint160DecodeAddress(fromAddress)
fromAddressHash, err := address.DecodeUint160(fromAddress)
require.NoError(t, err)
tx := &transaction.Transaction{

View file

@ -11,7 +11,7 @@ import (
"strconv"
"strings"
"github.com/CityOfZion/neo-go/pkg/crypto"
"github.com/CityOfZion/neo-go/pkg/encoding/address"
"github.com/CityOfZion/neo-go/pkg/io"
"github.com/CityOfZion/neo-go/pkg/vm/opcode"
)
@ -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 := crypto.Uint160DecodeAddress(addressStr)
uint160, err := address.DecodeUint160(addressStr)
if err != nil {
c.prog.Err = err
return

View file

@ -5,8 +5,8 @@ import (
"testing"
"github.com/CityOfZion/neo-go/pkg/core/transaction"
"github.com/CityOfZion/neo-go/pkg/crypto"
"github.com/CityOfZion/neo-go/pkg/crypto/hash"
"github.com/CityOfZion/neo-go/pkg/encoding/address"
"github.com/CityOfZion/neo-go/pkg/io"
"github.com/stretchr/testify/assert"
)
@ -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), crypto.AddressFromUint160(block.NextConsensus))
assert.Equal(t, data["nextconsensus"].(string), address.EncodeUint160(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 := crypto.AddressFromUint160(b.NextConsensus)
nextConsensus := address.EncodeUint160(b.NextConsensus)
assert.Equal(t, "APyEx5f4Zm4oCHwFWiSTaph1fPBxZacYVR", nextConsensus)
assert.Equal(t, "4012afae6df64195041e4764b57caa9e27fc2cfc596833163904136ec95816d104b44b3737d0e9f6b1b4445cd3b6a5cc80f6b0935675bc44dba44415eb309832b3404dc95bcf85e4635556a1d618e4ce947b26972992ed74788df5f9501b850ac0b40b7112d1ff30e4ade00369e16f0d13932d1ba76725e7682db072f8e2cd7752b840d12bb7dd45dd3b0e2098db5c67b6de55b7c40164937491fcaca1239b25860251224ead23ab232add78ccccd347239eae50ffc98f50b2a84c60ec5c3d284647a7406fabf6ca241b759af6b71080c0dfad7395632e989226a7e52f8cd2c133aeb2226e6e1aea47666fd81f578405a9f9bbd9d0bc523c3a44d7a5099ddc649feabe5f406188b8ee478731a89beeb76fdbd108eb0071b8f2b8678f40c5a1f387a491314336783255dee8cc5af4bf914dfeaacecc318fc13e02262658e39e8ce0631941b1", hex.EncodeToString(b.Script.InvocationScript))

View file

@ -3,7 +3,7 @@ package transaction
import (
"encoding/json"
"github.com/CityOfZion/neo-go/pkg/crypto"
"github.com/CityOfZion/neo-go/pkg/encoding/address"
"github.com/CityOfZion/neo-go/pkg/io"
"github.com/CityOfZion/neo-go/pkg/util"
)
@ -52,7 +52,7 @@ func (out *Output) MarshalJSON() ([]byte, error) {
return json.Marshal(map[string]interface{}{
"asset": out.AssetID,
"value": out.Amount,
"address": crypto.AddressFromUint160(out.ScriptHash),
"address": address.EncodeUint160(out.ScriptHash),
"n": out.Position,
})
}

View file

@ -4,8 +4,8 @@ import (
"encoding/hex"
"testing"
"github.com/CityOfZion/neo-go/pkg/crypto"
"github.com/CityOfZion/neo-go/pkg/crypto/keys"
"github.com/CityOfZion/neo-go/pkg/encoding/address"
"github.com/CityOfZion/neo-go/pkg/io"
"github.com/CityOfZion/neo-go/pkg/util"
"github.com/stretchr/testify/assert"
@ -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", crypto.AddressFromUint160(txData.Admin))
assert.Equal(t, "Abf2qMs1pzQb8kYk9RuxtUb9jtRKJVuBJt", address.EncodeUint160(txData.Admin))
assert.Equal(t, "c56f33fc6ecfcd0c225c4ab356fee59390af8560be0e930faebe74a6daff7c9b", tx.Hash().StringLE())
buf := io.NewBufBinWriter()

View file

@ -4,7 +4,7 @@ import (
"encoding/hex"
"testing"
"github.com/CityOfZion/neo-go/pkg/crypto"
"github.com/CityOfZion/neo-go/pkg/encoding/address"
"github.com/CityOfZion/neo-go/pkg/io"
"github.com/CityOfZion/neo-go/pkg/smartcontract"
"github.com/CityOfZion/neo-go/pkg/util"
@ -51,8 +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))
address := crypto.AddressFromUint160(tx.Outputs[0].ScriptHash)
assert.Equal(t, "AQJseD8iBmCD4sgfHRhMahmoi9zvopG6yz", address)
assert.Equal(t, "AQJseD8iBmCD4sgfHRhMahmoi9zvopG6yz", address.EncodeUint160(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"

View file

@ -4,7 +4,7 @@ import (
"testing"
"github.com/CityOfZion/neo-go/config"
"github.com/CityOfZion/neo-go/pkg/crypto"
"github.com/CityOfZion/neo-go/pkg/encoding/address"
"github.com/stretchr/testify/assert"
)
@ -45,7 +45,7 @@ func TestGetConsensusAddressMainNet(t *testing.T) {
}
assert.Equal(t, consensusScript, script.String())
assert.Equal(t, consensusAddr, crypto.AddressFromUint160(script))
assert.Equal(t, consensusAddr, address.EncodeUint160(script))
}
func TestUtilityTokenTX(t *testing.T) {

View file

@ -1,21 +1,20 @@
package crypto
package address
import (
"github.com/CityOfZion/neo-go/pkg/encoding/base58"
"github.com/CityOfZion/neo-go/pkg/util"
)
// AddressFromUint160 returns the "NEO address" from the given
// Uint160.
func AddressFromUint160(u util.Uint160) string {
// EncodeUint160 returns the "NEO address" from the given Uint160.
func EncodeUint160(u util.Uint160) string {
// Dont forget to prepend the Address version 0x17 (23) A
b := append([]byte{0x17}, u.BytesBE()...)
return base58.CheckEncode(b)
}
// Uint160DecodeAddress attempts to decode the given NEO address string
// DecodeUint160 attempts to decode the given NEO address string
// into an Uint160.
func Uint160DecodeAddress(s string) (u util.Uint160, err error) {
func DecodeUint160(s string) (u util.Uint160, err error) {
b, err := base58.CheckDecode(s)
if err != nil {
return u, err

View file

@ -1,4 +1,4 @@
package crypto
package address
import (
"testing"
@ -13,18 +13,18 @@ func TestUint160DecodeEncodeAddress(t *testing.T) {
"AMxkaxFVG8Q1BhnB4fjTA5ZmUTEnnTMJMa",
}
for _, addr := range addrs {
val, err := Uint160DecodeAddress(addr)
val, err := DecodeUint160(addr)
if err != nil {
t.Fatal(err)
}
assert.Equal(t, addr, AddressFromUint160(val))
assert.Equal(t, addr, EncodeUint160(val))
}
}
func TestUint160DecodeKnownAddress(t *testing.T) {
address := "AJeAEsmeD6t279Dx4n2HWdUvUmmXQ4iJvP"
val, err := Uint160DecodeAddress(address)
val, err := DecodeUint160(address)
if err != nil {
t.Fatal(err)
}

View file

@ -6,7 +6,7 @@ import (
"encoding/json"
"fmt"
"github.com/CityOfZion/neo-go/pkg/crypto"
"github.com/CityOfZion/neo-go/pkg/encoding/address"
"github.com/CityOfZion/neo-go/pkg/util"
"github.com/pkg/errors"
)
@ -96,7 +96,7 @@ func (p Param) GetUint160FromAddress() (util.Uint160, error) {
return util.Uint160{}, err
}
return crypto.Uint160DecodeAddress(s)
return address.DecodeUint160(s)
}
// GetFuncParam returns current parameter as a function call parameter.

View file

@ -5,7 +5,7 @@ import (
"encoding/json"
"testing"
"github.com/CityOfZion/neo-go/pkg/crypto"
"github.com/CityOfZion/neo-go/pkg/encoding/address"
"github.com/CityOfZion/neo-go/pkg/util"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
@ -129,7 +129,7 @@ func TestParamGetUint160FromHex(t *testing.T) {
func TestParamGetUint160FromAddress(t *testing.T) {
in := "AK2nJJpJr6o664CWJKi1QRXjqeic2zRp8y"
u160, _ := crypto.Uint160DecodeAddress(in)
u160, _ := address.DecodeUint160(in)
p := Param{stringT, in}
u, err := p.GetUint160FromAddress()
assert.Equal(t, u160, u)

View file

@ -7,8 +7,8 @@ import (
"strconv"
"github.com/CityOfZion/neo-go/pkg/core/transaction"
"github.com/CityOfZion/neo-go/pkg/crypto"
"github.com/CityOfZion/neo-go/pkg/crypto/keys"
"github.com/CityOfZion/neo-go/pkg/encoding/address"
"github.com/CityOfZion/neo-go/pkg/io"
"github.com/CityOfZion/neo-go/pkg/smartcontract"
"github.com/CityOfZion/neo-go/pkg/util"
@ -26,17 +26,17 @@ func CreateRawContractTransaction(params ContractTxParams) (*transaction.Transac
fromAddress string
receiverOutput *transaction.Output
wif, assetID, address, amount, balancer = params.wif, params.assetID, params.address, params.value, params.balancer
wif, assetID, toAddress, amount, balancer = params.wif, params.assetID, params.address, params.value, params.balancer
)
fromAddress = wif.PrivateKey.Address()
if fromAddressHash, err = crypto.Uint160DecodeAddress(fromAddress); err != nil {
if fromAddressHash, err = address.DecodeUint160(fromAddress); err != nil {
return nil, errs.Wrapf(err, "Failed to take script hash from address: %v", fromAddress)
}
if toAddressHash, err = crypto.Uint160DecodeAddress(address); err != nil {
return nil, errs.Wrapf(err, "Failed to take script hash from address: %v", address)
if toAddressHash, err = address.DecodeUint160(toAddress); err != nil {
return nil, errs.Wrapf(err, "Failed to take script hash from address: %v", toAddress)
}
tx.Attributes = append(tx.Attributes,
transaction.Attribute{
@ -58,12 +58,12 @@ func CreateRawContractTransaction(params ContractTxParams) (*transaction.Transac
// AddInputsAndUnspentsToTx adds inputs needed to transaction and one output
// with change.
func AddInputsAndUnspentsToTx(tx *transaction.Transaction, address string, assetID util.Uint256, amount util.Fixed8, balancer BalanceGetter) error {
scriptHash, err := crypto.Uint160DecodeAddress(address)
func AddInputsAndUnspentsToTx(tx *transaction.Transaction, addr string, assetID util.Uint256, amount util.Fixed8, balancer BalanceGetter) error {
scriptHash, err := address.DecodeUint160(addr)
if err != nil {
return errs.Wrapf(err, "failed to take script hash from address: %v", address)
return errs.Wrapf(err, "failed to take script hash from address: %v", addr)
}
inputs, spent, err := balancer.CalculateInputs(address, assetID, amount)
inputs, spent, err := balancer.CalculateInputs(addr, assetID, amount)
if err != nil {
return errs.Wrap(err, "failed to get inputs")
}

View file

@ -3,7 +3,7 @@ package wrappers
import (
"github.com/CityOfZion/neo-go/pkg/core/state"
"github.com/CityOfZion/neo-go/pkg/core/transaction"
"github.com/CityOfZion/neo-go/pkg/crypto"
"github.com/CityOfZion/neo-go/pkg/encoding/address"
"github.com/CityOfZion/neo-go/pkg/util"
)
@ -37,8 +37,8 @@ func NewAssetState(a *state.Asset) AssetState {
FeeMode: a.FeeMode,
FeeAddress: a.FeeAddress,
Owner: a.Owner.String(),
Admin: crypto.AddressFromUint160(a.Admin),
Issuer: crypto.AddressFromUint160(a.Issuer),
Admin: address.EncodeUint160(a.Admin),
Issuer: address.EncodeUint160(a.Issuer),
Expiration: a.Expiration,
IsFrozen: a.IsFrozen,
}

View file

@ -1,7 +1,7 @@
package wrappers
import (
"github.com/CityOfZion/neo-go/pkg/crypto"
"github.com/CityOfZion/neo-go/pkg/encoding/address"
)
// ValidateAddressResponse represents response to validate address call.
@ -12,10 +12,10 @@ type ValidateAddressResponse struct {
// ValidateAddress verifies that the address is a correct NEO address
// see https://docs.neo.org/en-us/node/cli/2.9.4/api/validateaddress.html
func ValidateAddress(address interface{}) ValidateAddressResponse {
resp := ValidateAddressResponse{Address: address}
if address, ok := address.(string); ok {
_, err := crypto.Uint160DecodeAddress(address)
func ValidateAddress(addr interface{}) ValidateAddressResponse {
resp := ValidateAddressResponse{Address: addr}
if addr, ok := addr.(string); ok {
_, err := address.DecodeUint160(addr)
resp.IsValid = err == nil
}
return resp

View file

@ -7,8 +7,8 @@ import (
"strings"
"unicode/utf8"
"github.com/CityOfZion/neo-go/pkg/crypto"
"github.com/CityOfZion/neo-go/pkg/crypto/keys"
"github.com/CityOfZion/neo-go/pkg/encoding/address"
"github.com/CityOfZion/neo-go/pkg/io"
"github.com/CityOfZion/neo-go/pkg/util"
)
@ -156,7 +156,7 @@ func adjustValToType(typ ParamType, val string) (interface{}, error) {
case IntegerType:
return strconv.Atoi(val)
case Hash160Type:
u, err := crypto.Uint160DecodeAddress(val)
u, err := address.DecodeUint160(val)
if err == nil {
return hex.EncodeToString(u.BytesBE()), nil
}
@ -215,7 +215,7 @@ func inferParamType(val string) ParamType {
return BoolType
}
_, err = crypto.Uint160DecodeAddress(val)
_, err = address.DecodeUint160(val)
if err == nil {
return Hash160Type
}