-
Notifications
You must be signed in to change notification settings - Fork 3.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(schema/indexer): add address codec param (#21361)
- Loading branch information
Showing
5 changed files
with
97 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package addressutil | ||
|
||
type AddressCodec interface { | ||
// StringToBytes decodes text to bytes | ||
StringToBytes(text string) ([]byte, error) | ||
// BytesToString encodes bytes to text | ||
BytesToString(bz []byte) (string, error) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package addressutil | ||
|
||
import ( | ||
"encoding/hex" | ||
"fmt" | ||
) | ||
|
||
// HexAddressCodec is a basic address codec that encodes and decodes addresses as hex strings. | ||
// It is intended to be used as a fallback codec when no other codec is provided. | ||
type HexAddressCodec struct{} | ||
|
||
func (h HexAddressCodec) StringToBytes(text string) ([]byte, error) { | ||
if len(text) < 2 || text[:2] != "0x" { | ||
return nil, fmt.Errorf("invalid hex address: %s", text) | ||
} | ||
|
||
return hex.DecodeString(text[2:]) | ||
} | ||
|
||
func (h HexAddressCodec) BytesToString(bz []byte) (string, error) { | ||
return fmt.Sprintf("0x%x", bz), nil | ||
} | ||
|
||
var _ AddressCodec = HexAddressCodec{} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
package addressutil | ||
|
||
import ( | ||
"bytes" | ||
"testing" | ||
) | ||
|
||
func TestHexAddressCodec(t *testing.T) { | ||
tt := []struct { | ||
text string | ||
bz []byte | ||
err bool | ||
}{ | ||
{ | ||
text: "0x1234", | ||
bz: []byte{0x12, 0x34}, | ||
}, | ||
{ | ||
text: "0x", | ||
bz: []byte{}, | ||
}, | ||
{ | ||
text: "0x123", | ||
err: true, | ||
}, | ||
{ | ||
text: "1234", | ||
err: true, | ||
}, | ||
} | ||
|
||
h := HexAddressCodec{} | ||
for _, tc := range tt { | ||
bz, err := h.StringToBytes(tc.text) | ||
if tc.err && err == nil { | ||
t.Fatalf("expected error, got none") | ||
} | ||
if !tc.err && err != nil { | ||
t.Fatalf("unexpected error: %v", err) | ||
} | ||
if !tc.err && !bytes.Equal(bz, tc.bz) { | ||
t.Fatalf("expected %v, got %v", tc.bz, bz) | ||
} | ||
|
||
// check address rendering if no error | ||
if !tc.err { | ||
if str, err := h.BytesToString(tc.bz); err != nil { | ||
t.Fatalf("unexpected error: %v", err) | ||
} else if str != tc.text { | ||
t.Fatalf("expected %s, got %s", tc.text, str) | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters