Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[TypeTag] Add parsing for strings to TypeTag #137

Merged
merged 1 commit into from
Mar 17, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions internal/types/account.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,9 @@ func (account *Account) AccountAddress() AccountAddress {
return account.Address
}

// ErrAddressMissing0x is returned when an AccountAddress is missing the leading 0x
var ErrAddressMissing0x = errors.New("AccountAddress missing 0x")

// ErrAddressTooShort is returned when an AccountAddress is too short
var ErrAddressTooShort = errors.New("AccountAddress too short")

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

return nil
}

// ParseStringWithPrefixRelaxed parses a string into an AccountAddress
func (aa *AccountAddress) ParseStringWithPrefixRelaxed(x string) error {
if !strings.HasPrefix(x, "0x") {
return ErrAddressTooShort
}
x = x[2:]
if len(x) < 1 {
return ErrAddressTooShort
}
if len(x) > 64 {
return ErrAddressTooLong
}
if len(x)%2 != 0 {
x = "0" + x
}
bytes, err := hex.DecodeString(x)
if err != nil {
return err
}
// zero-prefix/right-align what bytes we got
copy((*aa)[32-len(bytes):], bytes)

return nil
}
Loading