Skip to content

Commit

Permalink
Merge pull request #22 from wangdayong228/dev-get-address-type
Browse files Browse the repository at this point in the history
Dev get address type
  • Loading branch information
Pana authored Sep 28, 2020
2 parents 428efa7 + 4976da0 commit f65a685
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 2 deletions.
2 changes: 1 addition & 1 deletion example/context/config.toml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
NodeURL = "http://101.201.127.86:12537"
NodeURL = "http://testnet-jsonrpc.conflux-chain.org:12537"
BlockHash = "0x2dc741dfef6261a938c644ba2251b1202a2fdc125f76a9078a3b23b55f396ad2"
TransactionHash = "0x3e4e363d84ee71d3d5152973e05c329813306458734be83af40ea6436fe214a7"
ERC20Address = "0x860ccec253c3aa2a74433a75817d4eaf543119b6"
33 changes: 33 additions & 0 deletions types/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,39 @@ func (address *Address) IsZero() bool {
return *tmp == constants.ZeroAddress
}

type AddressType string

const (
NormalAddress AddressType = "Normal"
CustomContractAddress AddressType = "CustomContract"
InternalContractAddress AddressType = "InternalContract"
InvalidAddress AddressType = "Invalid"
)

// GetAddressType returuns the address type,
// address with prefix "0x1" is normal address and "0x8" is contract address
func (address *Address) GetAddressType() AddressType {
if address == nil {
return InvalidAddress
}

addrBytes := address.ToCommonAddress().Bytes()

flag := addrBytes[0] >> 4
if flag == 0x1 {
return NormalAddress
}
if flag == 0x8 {
return CustomContractAddress
}

if addrBytes[0] == 0x08 && addrBytes[1] == 0x88 {
return InternalContractAddress
}

return InvalidAddress
}

// Hash represents the 32 byte Keccak256 hash of arbitrary data in HEX format.
type Hash string

Expand Down
31 changes: 30 additions & 1 deletion types/types_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package types

import "testing"
import (
"testing"
)

func TestAddressIsZero(t *testing.T) {
zeroAddrs := []Address{Address("0x0000000000000000000000000000000000000000"), Address("0X0000000000000000000000000000000000000000")}
Expand All @@ -21,3 +23,30 @@ func TestAddressIsZero(t *testing.T) {
t.Errorf("expect %+v be zero address", &normalAddr)
}
}

func TestGetAddressType(t *testing.T) {
normalAddr := Address("0x1200000000fa000d00e000000000000000000000")
customContractAddr := Address("0x8300000000fa000d00e000000000000000000000")
internalContractAddr := Address("0x0888000000fa000d00e000000000000000000002")
invalidAddr := Address("0x3300000000fa000d00e000000000000000000000")

addressType := normalAddr.GetAddressType()
if addressType != NormalAddress {
t.Errorf("expect %+v be normal address, actual is %v", normalAddr, addressType)
}

addressType = customContractAddr.GetAddressType()
if addressType != CustomContractAddress {
t.Errorf("expect %+v be contract address, actual is %v", customContractAddr, addressType)
}

addressType = internalContractAddr.GetAddressType()
if addressType != InternalContractAddress {
t.Errorf("expect %+v be contract address, actual is %v", internalContractAddr, addressType)
}

addressType = invalidAddr.GetAddressType()
if addressType != InvalidAddress {
t.Errorf("expect %+v be unknown address,actual is %v", invalidAddr, addressType)
}
}

0 comments on commit f65a685

Please sign in to comment.