Skip to content

Commit 77f9d10

Browse files
authored
Merge branch 'main' into script-arg-example
2 parents d8d9701 + 214ab6c commit 77f9d10

File tree

3 files changed

+485
-12
lines changed

3 files changed

+485
-12
lines changed

internal/types/account.go

+28
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,9 @@ func (account *Account) AccountAddress() AccountAddress {
9090
return account.Address
9191
}
9292

93+
// ErrAddressMissing0x is returned when an AccountAddress is missing the leading 0x
94+
var ErrAddressMissing0x = errors.New("AccountAddress missing 0x")
95+
9396
// ErrAddressTooShort is returned when an AccountAddress is too short
9497
var ErrAddressTooShort = errors.New("AccountAddress too short")
9598

@@ -120,3 +123,28 @@ func (aa *AccountAddress) ParseStringRelaxed(x string) error {
120123

121124
return nil
122125
}
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+
}

0 commit comments

Comments
 (0)