Skip to content

Commit

Permalink
core/capabilities/ccip/ccipaptos/addresscodec.go: dont use aptos-go-sdk
Browse files Browse the repository at this point in the history
  • Loading branch information
cfal committed Feb 26, 2025
1 parent e9a6559 commit 064ff71
Showing 1 changed file with 22 additions and 21 deletions.
43 changes: 22 additions & 21 deletions core/capabilities/ccip/ccipaptos/addresscodec.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package ccipaptos

import (
"encoding/hex"
"fmt"

"github.com/aptos-labs/aptos-go-sdk"
"strings"
)

type AddressCodec struct{}
Expand All @@ -21,26 +21,29 @@ func addressBytesToString(addr []byte) (string, error) {
return "", fmt.Errorf("invalid Aptos address length, expected 32, got %d", len(addr))
}

accAddress := aptos.AccountAddress(addr)
return accAddress.String(), nil
return fmt.Sprintf("0x%064x", addr), nil
}

func addressStringToBytes(addr string) ([]byte, error) {
var accAddress aptos.AccountAddress
err := accAddress.ParseStringRelaxed(addr)
if err != nil {
return nil, fmt.Errorf("failed to decode Aptos address '%s': %w", addr, err)
a := addr
if strings.HasPrefix(a, "0x") {
a = a[2:]
}
if len(a) == 0 {
return nil, fmt.Errorf("invalid Aptos address length, expected at least 1 char: %s", addr)
}
if len(a) > 64 {
return nil, fmt.Errorf("invalid Aptos address length, expected at most 64 chars: %s", addr)
}
for len(a) < 64 {
a = "0" + a
}
return accAddress[:], nil
}

func addressStringToBytes32(addr string) ([32]byte, error) {
var accAddress aptos.AccountAddress
err := accAddress.ParseStringRelaxed(addr)
bytes, err := hex.DecodeString(a)
if err != nil {
return accAddress, fmt.Errorf("failed to decode Aptos address '%s': %w", addr, err)
return nil, fmt.Errorf("failed to decode Aptos address '%s': %w", addr, err)
}
return accAddress, nil
return bytes, nil
}

func addressBytesToBytes32(addr []byte) ([32]byte, error) {
Expand All @@ -53,14 +56,12 @@ func addressBytesToBytes32(addr []byte) ([32]byte, error) {
return result, nil
}

// takes a valid Aptos address string and converts it into canonical format.
func addressStringToString(addr string) (string, error) {
var accAddress aptos.AccountAddress
err := accAddress.ParseStringRelaxed(addr)
func addressStringToBytes32(addr string) ([32]byte, error) {
bytes, err := addressStringToBytes(addr)
if err != nil {
return "", fmt.Errorf("failed to decode Aptos address '%s': %w", addr, err)
return [32]byte{}, err
}
return accAddress.String(), nil
return addressBytesToBytes32(bytes)
}

func addressIsValid(addr string) bool {
Expand Down

0 comments on commit 064ff71

Please sign in to comment.