Change prefix to xdc

This commit is contained in:
AnilChinchawale 2019-03-17 16:14:04 +05:30
parent 239f9702ce
commit f272b5d8cb
4 changed files with 47 additions and 19 deletions

View file

@ -244,7 +244,7 @@ func (w *wizard) readPassword() string {
func (w *wizard) readAddress() *common.Address {
for {
// Read the address from the user
fmt.Printf("> 0x")
fmt.Printf("> xdc")
text, err := w.in.ReadString('\n')
if err != nil {
log.Crit("Failed to read user input", "err", err)
@ -269,7 +269,7 @@ func (w *wizard) readAddress() *common.Address {
func (w *wizard) readDefaultAddress(def common.Address) common.Address {
for {
// Read the address from the user
fmt.Printf("> 0x")
fmt.Printf("> xdc")
text, err := w.in.ReadString('\n')
if err != nil {
log.Crit("Failed to read user input", "err", err)

View file

@ -28,11 +28,16 @@ func ToHex(b []byte) string {
return "0x" + hex
}
// FromHex returns the bytes represented by the hexadecimal string s.
// s may be prefixed with "0x".
func FromHex(s string) []byte {
if len(s) > 1 {
if s[0:2] == "0x" || s[0:2] == "0X" {
s = s[2:]
}
if (s[0] == 'x' || s[0] == 'X') && (s[1] == 'd' || s[1] == 'D') && (s[2] == 'c' || s[2] == 'C') {
s = s[3:]
}
}
if len(s)%2 == 1 {
s = "0" + s
@ -53,6 +58,10 @@ func CopyBytes(b []byte) (copiedBytes []byte) {
return
}
func hasXDCPrefix(str string) bool {
return len(str) >= 3 && (str[0] == 'x' || str[0] == 'X') && (str[1] == 'd' || str[1] == 'D') && (str[2] == 'c' || str[2] == 'C')
}
func hasHexPrefix(str string) bool {
return len(str) >= 2 && str[0] == '0' && (str[1] == 'x' || str[1] == 'X')
}

View file

@ -44,6 +44,14 @@ func (b Bytes) MarshalText() ([]byte, error) {
return result, nil
}
// MarshalXDCText implements encoding.TextMarshaler
func (b Bytes) MarshalXDCText() ([]byte, error) {
result := make([]byte, len(b)*2+3)
copy(result, `xdc`)
hex.Encode(result[3:], b)
return result, nil
}
// UnmarshalJSON implements json.Unmarshaler.
func (b *Bytes) UnmarshalJSON(input []byte) error {
if !isString(input) {
@ -277,12 +285,18 @@ func bytesHave0xPrefix(input []byte) bool {
return len(input) >= 2 && input[0] == '0' && (input[1] == 'x' || input[1] == 'X')
}
func bytesHaveXDCPrefix(input []byte) bool {
return len(input) >= 3 && (input[0] == 'x' || input[0] == 'X') && (input[1] == 'd' || input[1] == 'D') && (input[2] == 'c' || input[2] == 'C')
}
func checkText(input []byte, wantPrefix bool) ([]byte, error) {
if len(input) == 0 {
return nil, nil // empty strings are allowed
}
if bytesHave0xPrefix(input) {
input = input[2:]
if bytesHaveXDCPrefix(input) {
input = input[3:]
} else if bytesHave0xPrefix(input) {
input = input[2:]
} else if wantPrefix {
return nil, ErrMissingPrefix
}

View file

@ -40,15 +40,15 @@ func TestIsHexAddress(t *testing.T) {
str string
exp bool
}{
{"0x5aaeb6053f3e94c9b9a09f33669435e7ef1beaed", true},
{"xdc5aaeb6053f3e94c9b9a09f33669435e7ef1beaed", true},
{"5aaeb6053f3e94c9b9a09f33669435e7ef1beaed", true},
{"0X5aaeb6053f3e94c9b9a09f33669435e7ef1beaed", true},
{"0XAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA", true},
{"0xAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA", true},
{"0x5aaeb6053f3e94c9b9a09f33669435e7ef1beaed1", false},
{"0x5aaeb6053f3e94c9b9a09f33669435e7ef1beae", false},
{"XDC5aaeb6053f3e94c9b9a09f33669435e7ef1beaed", true},
{"XdcAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA", true},
{"xdcAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA", true},
{"xdc5aaeb6053f3e94c9b9a09f33669435e7ef1beaed1", false},
{"xdc5aaeb6053f3e94c9b9a09f33669435e7ef1beae", false},
{"5aaeb6053f3e94c9b9a09f33669435e7ef1beaed11", false},
{"0xxaaeb6053f3e94c9b9a09f33669435e7ef1beaed", false},
{"xdcxaaeb6053f3e94c9b9a09f33669435e7ef1beaed", false},
}
for _, test := range tests {
@ -101,6 +101,11 @@ func TestAddressUnmarshalJSON(t *testing.T) {
{`"0xG000000000000000000000000000000000000000"`, true, nil},
{`"0x0000000000000000000000000000000000000000"`, false, big.NewInt(0)},
{`"0x0000000000000000000000000000000000000010"`, false, big.NewInt(16)},
{`"xdc"`, true, nil},
{`"xdc00"`, true, nil},
{`"xdcG000000000000000000000000000000000000000"`, true, nil},
{`"xdc0000000000000000000000000000000000000000"`, false, big.NewInt(0)},
{`"xdc0000000000000000000000000000000000000010"`, false, big.NewInt(16)},
}
for i, test := range tests {
var v Address
@ -125,15 +130,15 @@ func TestAddressHexChecksum(t *testing.T) {
Output string
}{
// Test cases from https://github.com/ethereum/EIPs/blob/master/EIPS/eip-55.md#specification
{"0x5aaeb6053f3e94c9b9a09f33669435e7ef1beaed", "0x5aAeb6053F3E94C9b9A09f33669435E7Ef1BeAed"},
{"0xfb6916095ca1df60bb79ce92ce3ea74c37c5d359", "0xfB6916095ca1df60bB79Ce92cE3Ea74c37c5d359"},
{"0xdbf03b407c01e7cd3cbea99509d93f8dddc8c6fb", "0xdbF03B407c01E7cD3CBea99509d93f8DDDC8C6FB"},
{"0xd1220a0cf47c7b9be7a2e6ba89f429762e7b9adb", "0xD1220A0cf47c7B9Be7A2E6BA89F429762e7b9aDb"},
{"xdc5aaeb6053f3e94c9b9a09f33669435e7ef1beaed", "xdc5aAeb6053F3E94C9b9A09f33669435E7Ef1BeAed"},
{"xdcfb6916095ca1df60bb79ce92ce3ea74c37c5d359", "xdcfB6916095ca1df60bB79Ce92cE3Ea74c37c5d359"},
{"xdcdbf03b407c01e7cd3cbea99509d93f8dddc8c6fb", "xdcdbF03B407c01E7cD3CBea99509d93f8DDDC8C6FB"},
{"xdcd1220a0cf47c7b9be7a2e6ba89f429762e7b9adb", "xdcD1220A0cf47c7B9Be7A2E6BA89F429762e7b9aDb"},
// Ensure that non-standard length input values are handled correctly
{"0xa", "0x000000000000000000000000000000000000000A"},
{"0x0a", "0x000000000000000000000000000000000000000A"},
{"0x00a", "0x000000000000000000000000000000000000000A"},
{"0x000000000000000000000000000000000000000a", "0x000000000000000000000000000000000000000A"},
{"0xa", "xdc000000000000000000000000000000000000000A"},
{"0x0a", "xdc000000000000000000000000000000000000000A"},
{"0x00a", "xdc000000000000000000000000000000000000000A"},
{"0x000000000000000000000000000000000000000a", "xdc000000000000000000000000000000000000000A"},
}
for i, test := range tests {
output := HexToAddress(test.Input).Hex()