aboutsummaryrefslogtreecommitdiffstats
path: root/common/types.go
diff options
context:
space:
mode:
Diffstat (limited to 'common/types.go')
-rw-r--r--common/types.go62
1 files changed, 62 insertions, 0 deletions
diff --git a/common/types.go b/common/types.go
index 4ea2d56a6..76e7be58f 100644
--- a/common/types.go
+++ b/common/types.go
@@ -23,8 +23,10 @@ import (
"math/rand"
"reflect"
+ "encoding/json"
"github.com/ethereum/go-ethereum/common/hexutil"
"github.com/ethereum/go-ethereum/crypto/sha3"
+ "strings"
)
const (
@@ -238,3 +240,63 @@ func (a *UnprefixedAddress) UnmarshalText(input []byte) error {
func (a UnprefixedAddress) MarshalText() ([]byte, error) {
return []byte(hex.EncodeToString(a[:])), nil
}
+
+// MixedcaseAddress retains the original string, which may or may not be
+// correctly checksummed
+type MixedcaseAddress struct {
+ addr Address
+ original string
+}
+
+// NewMixedcaseAddress constructor (mainly for testing)
+func NewMixedcaseAddress(addr Address) MixedcaseAddress {
+ return MixedcaseAddress{addr: addr, original: addr.Hex()}
+}
+
+// NewMixedcaseAddressFromString is mainly meant for unit-testing
+func NewMixedcaseAddressFromString(hexaddr string) (*MixedcaseAddress, error) {
+ if !IsHexAddress(hexaddr) {
+ return nil, fmt.Errorf("Invalid address")
+ }
+ a := FromHex(hexaddr)
+ return &MixedcaseAddress{addr: BytesToAddress(a), original: hexaddr}, nil
+}
+
+// UnmarshalJSON parses MixedcaseAddress
+func (ma *MixedcaseAddress) UnmarshalJSON(input []byte) error {
+ if err := hexutil.UnmarshalFixedJSON(addressT, input, ma.addr[:]); err != nil {
+ return err
+ }
+ return json.Unmarshal(input, &ma.original)
+}
+
+// MarshalJSON marshals the original value
+func (ma *MixedcaseAddress) MarshalJSON() ([]byte, error) {
+ if strings.HasPrefix(ma.original, "0x") || strings.HasPrefix(ma.original, "0X") {
+ return json.Marshal(fmt.Sprintf("0x%s", ma.original[2:]))
+ }
+ return json.Marshal(fmt.Sprintf("0x%s", ma.original))
+}
+
+// Address returns the address
+func (ma *MixedcaseAddress) Address() Address {
+ return ma.addr
+}
+
+// String implements fmt.Stringer
+func (ma *MixedcaseAddress) String() string {
+ if ma.ValidChecksum() {
+ return fmt.Sprintf("%s [chksum ok]", ma.original)
+ }
+ return fmt.Sprintf("%s [chksum INVALID]", ma.original)
+}
+
+// ValidChecksum returns true if the address has valid checksum
+func (ma *MixedcaseAddress) ValidChecksum() bool {
+ return ma.original == ma.addr.Hex()
+}
+
+// Original returns the mixed-case input string
+func (ma *MixedcaseAddress) Original() string {
+ return ma.original
+}