Skip to content
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
21 changes: 21 additions & 0 deletions pdp/contract/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -495,3 +495,24 @@ func FSDeregisterProvider(ctx context.Context, db *harmonydb.DB, ethClient *ethc

return signedTx.Hash().String(), nil
}

func DecodeAddressCapability(input []byte) common.Address {
// If input is longer than 32 bytes → return zero
if len(input) > 32 {
return common.Address{}
}

// 32-byte big-endian buffer
var buf [32]byte

if len(input) == 32 {
// Exact fit
copy(buf[:], input)
} else {
// Left pad if shorter
copy(buf[32-len(input):], input)
}

// Lowest 20 bytes are the address
return common.BytesToAddress(buf[12:])
}
102 changes: 102 additions & 0 deletions pdp/contract/utils_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
package contract

import (
"bytes"
"encoding/hex"
"testing"

"github.com/ethereum/go-ethereum/common"
)

// helper: decode hex string "0x...." to []byte
func mustHex(s string) []byte {
if len(s) < 2 {
return []byte{}
}
b, err := hex.DecodeString(s[2:])
if err != nil {
panic(err)
}
return b
}

func TestDecodeAddressCapability_Unmodified(t *testing.T) {
tests := []string{
"0x000000000004444c5dc75cb358380d2e3de08a90",
"0x4a6f6B9fF1fc974096f9063a45Fd12bD5B928AD1",
}

for _, input := range tests {
got := DecodeAddressCapability(mustHex(input))
want := common.HexToAddress(input)

if !bytes.Equal(got.Bytes(), want.Bytes()) {
t.Errorf("input %s → expected %s, got %s", input, want.Hex(), got.Hex())
}
}
}

func TestDecodeAddressCapability_TooLongGivesZero(t *testing.T) {
input := "0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff"

got := DecodeAddressCapability(mustHex(input))
want := common.HexToAddress("0x0000000000000000000000000000000000000000")

if !bytes.Equal(got.Bytes(), want.Bytes()) {
t.Errorf("expected %s, got %s", want.Hex(), got.Hex())
}
}

func TestDecodeAddressCapability_FromLowBytes(t *testing.T) {
tests := []struct {
input string
want string
}{
{
"0x1234ffffffffffffffff6789ffffffffffffffffffffffffffffffffffffff0f",
"0xffffffffffffffffffffffffffffffffffffff0f",
},
{
"0x1234eeeeeeeeeeeeee6789eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee0e",
"0xeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee0e",
},
{
"0x12dddddddddddddddddddddddddddddddddddddd0d",
"0xdddddddddddddddddddddddddddddddddddddd0d",
},
}

for _, tc := range tests {
got := DecodeAddressCapability(mustHex(tc.input))
want := common.HexToAddress(tc.want)

if !bytes.Equal(got.Bytes(), want.Bytes()) {
t.Errorf("input %s → expected %s, got %s", tc.input, want.Hex(), got.Hex())
}
}
}

func TestDecodeAddressCapability_PadLeft(t *testing.T) {
tests := []struct {
input string
want string
}{
{
"0x04444c5dc75cb358380d2e3de08a90",
"0x000000000004444c5dc75cb358380d2e3de08a90",
},
{
"0x",
"0x0000000000000000000000000000000000000000",
},
}

for _, tc := range tests {
got := DecodeAddressCapability(mustHex(tc.input))
want := common.HexToAddress(tc.want)

if !bytes.Equal(got.Bytes(), want.Bytes()) {
t.Errorf("input %s → expected %s, got %s", tc.input, want.Hex(), got.Hex())
}
}
}
4 changes: 1 addition & 3 deletions web/api/webrpc/pdp.go
Original file line number Diff line number Diff line change
Expand Up @@ -290,9 +290,7 @@ func capabilitiesToOffering(keys []string, values [][]byte) (*FSPDPOffering, map
case contract.CapLocation:
offering.Location = string(value)
case contract.CapPaymentToken:
if len(value) >= 20 {
offering.PaymentTokenAddress = common.BytesToAddress(value[len(value)-20:]).Hex()
}
offering.PaymentTokenAddress = contract.DecodeAddressCapability(value).Hex()
default:
// Custom capability
customCaps[key] = string(value)
Expand Down