Skip to content

Commit 5ab3d00

Browse files
cfalfriedemannf
andauthored
relayer/txm/bcs_util.go: support base64 encoded addresses (#140)
* relayer/txm/bcs_util.go: support base64 encoded addresses * Fix return stmt and add test --------- Co-authored-by: Friedemann Fürst <59653747+friedemannf@users.noreply.github.com>
1 parent bb7e84a commit 5ab3d00

File tree

2 files changed

+50
-6
lines changed

2 files changed

+50
-6
lines changed

relayer/txm/bcs_util.go

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package txm
22

33
import (
4+
"encoding/base64"
45
"encoding/json"
56
"errors"
67
"fmt"
@@ -288,13 +289,21 @@ func serializeArg(argVal any, argType aptos.TypeTag, serializer *bcs.Serializer)
288289
return nil
289290
}
290291
if v, ok := argVal.(string); ok {
291-
address := &aptos.AccountAddress{}
292-
err := address.ParseStringRelaxed(v)
293-
if err != nil {
294-
return err
292+
// first, check if it's base64
293+
decoded, err := base64.StdEncoding.DecodeString(v)
294+
if err == nil && len(decoded) == 32 {
295+
address := aptos.AccountAddress(decoded)
296+
address.MarshalBCS(serializer)
297+
return nil
298+
} else {
299+
address := &aptos.AccountAddress{}
300+
err := address.ParseStringRelaxed(v)
301+
if err != nil {
302+
return err
303+
}
304+
address.MarshalBCS(serializer)
305+
return nil
295306
}
296-
address.MarshalBCS(serializer)
297-
return nil
298307
}
299308
case aptos.TypeTagVector:
300309
itemType := argType.Value.(*aptos.VectorTag).TypeParam

relayer/txm/bcs_util_test.go

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,48 @@
11
package txm
22

33
import (
4+
"encoding/json"
45
"math/big"
56
"testing"
67

78
"github.com/aptos-labs/aptos-go-sdk"
9+
"github.com/aptos-labs/aptos-go-sdk/bcs"
810
"github.com/stretchr/testify/require"
911
)
1012

13+
func TestCreateBcsValue(t *testing.T) {
14+
t.Parallel()
15+
t.Run("", func(t *testing.T) {
16+
address := &aptos.AccountAddress{}
17+
_ = address.ParseStringRelaxed("0x3b17dad1bdd88f337712cc2f6187bb741d56da467320373fd9198262cc93de76")
18+
stringAddress := address.StringLong()
19+
byteAddress, err := bcs.Serialize(address)
20+
require.NoError(t, err)
21+
typeTag, err := CreateTypeTag("address")
22+
require.NoError(t, err)
23+
24+
// Test serializing a hex string value
25+
serialized, err := CreateBcsValue(typeTag, stringAddress)
26+
require.NoError(t, err)
27+
require.Equal(t, byteAddress, serialized)
28+
29+
// Test serializing a base64 string
30+
// When marshalling using JSON, the bytearray will be serialized as a base64 string,
31+
// unmarshalling this string into an any will result in it being treated as a string, not a bytearray.
32+
// CreateBcsValue is supposed to account for this by first testing if the value can be decoded using base64
33+
marshaled, err := json.Marshal(struct {
34+
Address []byte `json:"address"`
35+
}{Address: byteAddress})
36+
require.NoError(t, err)
37+
result := make(map[string]interface{})
38+
err = json.Unmarshal(marshaled, &result)
39+
require.NoError(t, err)
40+
serialized, err = CreateBcsValue(typeTag, result["address"].(string))
41+
require.NoError(t, err)
42+
require.Equal(t, byteAddress, serialized)
43+
})
44+
}
45+
1146
func TestGetBcsValues(t *testing.T) {
1247
t.Parallel()
1348
t.Run("uint32,uint64", func(t *testing.T) {

0 commit comments

Comments
 (0)