Skip to content
Open
Show file tree
Hide file tree
Changes from 3 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
9 changes: 9 additions & 0 deletions pdp/contract/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@ package contract
import (
"context"
"crypto/ecdsa"
"encoding/hex"
"fmt"
mbig "math/big"
"strings"
"time"

"github.com/ethereum/go-ethereum"
Expand Down Expand Up @@ -109,6 +111,13 @@ func OfferingToCapabilities(offering PDPOfferingData, additionalCaps map[string]
// Add custom capabilities
for k, v := range additionalCaps {
keys = append(keys, k)
// try hexadecimal
if len(v)%2 == 0 && len(v) > 3 && strings.HasPrefix(v, "0x") {
if decoded, err := hex.DecodeString(v[2:]); err == nil {
values = append(values, decoded)
continue
}
}
values = append(values, []byte(v))
}

Expand Down
48 changes: 48 additions & 0 deletions pdp/contract/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package contract
import (
"bytes"
"encoding/hex"
"math/big"
"testing"

"github.com/ethereum/go-ethereum/common"
Expand Down Expand Up @@ -100,3 +101,50 @@ func TestDecodeAddressCapability_PadLeft(t *testing.T) {
}
}
}

func TestOfferingToCapabilities_AdditionalHex(t *testing.T) {
offering := PDPOfferingData{
"https://pdp.example.com",
big.NewInt(32),
big.NewInt(0x1000000000000000),
false,
false,
[]byte{1, 1, 1, 1, 1, 1, 1, 1, 1},
big.NewInt(6000),
big.NewInt(30),
"narnia",
common.HexToAddress("0x0000000000004946c0e9F43F4Dee607b0eF1fA1c"),
}
additionalCaps := make(map[string]string)
additionalCaps["coolEndorsement"] = "0xccccaaaaddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddaaaacccc"
additionalCaps["owner"] = "0x4a6f6B9fF1fc974096f9063a45Fd12bD5B928AD1"
keys, values, err := OfferingToCapabilities(offering, additionalCaps)
if err != nil {
t.Errorf("OfferingToCapabilities returned error %s", err)
}
if len(keys) != len(values) {
t.Errorf("length mismatch: %d keys for %d values", len(keys), len(values))
}
capabilities := make(map[string][]byte)
for i := range len(keys) {
if len(keys[i]) == 0 {
t.Errorf("Got empty key at index %d", i)
}
if values[i] == nil {
t.Errorf("Got nil value for key %s", keys[i])
}
if _, contains := capabilities[keys[i]]; contains {
t.Errorf("Got duplicate key %s", keys[i])
}
capabilities[keys[i]] = values[i]
}
for key, valueStr := range additionalCaps {
if value, contains := capabilities[key]; !contains {
t.Errorf("keys: Missing '%s' key from additionalCaps", key)
} else if expectedLength := (len(valueStr) - 2) / 2; expectedLength != len(value) {
t.Errorf("Wrong length for hex capability '%s': expected %d actual %d", key, expectedLength, len(value))
} else if !bytes.Equal(value, mustHex(additionalCaps[key])) {
t.Errorf("Mismatching value for key '%s': expected %s actual 0x%x", key, valueStr, value)
}
}
}