File tree 3 files changed +485
-12
lines changed
3 files changed +485
-12
lines changed Original file line number Diff line number Diff line change @@ -90,6 +90,9 @@ func (account *Account) AccountAddress() AccountAddress {
90
90
return account .Address
91
91
}
92
92
93
+ // ErrAddressMissing0x is returned when an AccountAddress is missing the leading 0x
94
+ var ErrAddressMissing0x = errors .New ("AccountAddress missing 0x" )
95
+
93
96
// ErrAddressTooShort is returned when an AccountAddress is too short
94
97
var ErrAddressTooShort = errors .New ("AccountAddress too short" )
95
98
@@ -120,3 +123,28 @@ func (aa *AccountAddress) ParseStringRelaxed(x string) error {
120
123
121
124
return nil
122
125
}
126
+
127
+ // ParseStringWithPrefixRelaxed parses a string into an AccountAddress
128
+ func (aa * AccountAddress ) ParseStringWithPrefixRelaxed (x string ) error {
129
+ if ! strings .HasPrefix (x , "0x" ) {
130
+ return ErrAddressTooShort
131
+ }
132
+ x = x [2 :]
133
+ if len (x ) < 1 {
134
+ return ErrAddressTooShort
135
+ }
136
+ if len (x ) > 64 {
137
+ return ErrAddressTooLong
138
+ }
139
+ if len (x )% 2 != 0 {
140
+ x = "0" + x
141
+ }
142
+ bytes , err := hex .DecodeString (x )
143
+ if err != nil {
144
+ return err
145
+ }
146
+ // zero-prefix/right-align what bytes we got
147
+ copy ((* aa )[32 - len (bytes ):], bytes )
148
+
149
+ return nil
150
+ }
You can’t perform that action at this time.
0 commit comments