Skip to content

Commit 221d03d

Browse files
Allow address to be formatted separately to bytes
Signed-off-by: Peter Broadhurst <peter.broadhurst@kaleido.io>
1 parent 3c690d6 commit 221d03d

File tree

2 files changed

+78
-6
lines changed

2 files changed

+78
-6
lines changed

pkg/abi/outputserialization.go

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import (
2727

2828
"github.com/hyperledger/firefly-common/pkg/i18n"
2929
"github.com/hyperledger/firefly-signer/internal/signermsgs"
30+
"github.com/hyperledger/firefly-signer/pkg/ethtypes"
3031
)
3132

3233
// Serializer contains a set of options for how to serialize an parsed
@@ -37,6 +38,7 @@ type Serializer struct {
3738
fs FloatSerializer
3839
bs ByteSerializer
3940
dn DefaultNameGenerator
41+
ad AddressSerializer
4042
pretty bool
4143
}
4244

@@ -52,6 +54,7 @@ func NewSerializer() *Serializer {
5254
fs: Base10StringFloatSerializer,
5355
bs: HexByteSerializer,
5456
dn: NumericDefaultNameGenerator,
57+
ad: nil, // we fall back to bytes serializer to preserve compatibility
5558
}
5659
}
5760

@@ -82,6 +85,8 @@ type FloatSerializer func(f *big.Float) interface{}
8285

8386
type ByteSerializer func(b []byte) interface{}
8487

88+
type AddressSerializer func(addr [20]byte) interface{}
89+
8590
func (s *Serializer) SetFormattingMode(ts FormattingMode) *Serializer {
8691
s.ts = ts
8792
return s
@@ -102,6 +107,11 @@ func (s *Serializer) SetByteSerializer(bs ByteSerializer) *Serializer {
102107
return s
103108
}
104109

110+
func (s *Serializer) SetAddressSerializer(ad AddressSerializer) *Serializer {
111+
s.ad = ad
112+
return s
113+
}
114+
105115
func (s *Serializer) SetDefaultNameGenerator(dn DefaultNameGenerator) *Serializer {
106116
s.dn = dn
107117
return s
@@ -158,6 +168,18 @@ func HexByteSerializer0xPrefix(b []byte) interface{} {
158168
return "0x" + hex.EncodeToString(b)
159169
}
160170

171+
func HexAddrSerializer0xPrefix(addr [20]byte) interface{} {
172+
return ethtypes.Address0xHex(addr).String()
173+
}
174+
175+
func HexAddrSerializerPlain(addr [20]byte) interface{} {
176+
return ethtypes.AddressPlainHex(addr).String()
177+
}
178+
179+
func ChecksumAddrSerializer(addr [20]byte) interface{} {
180+
return ethtypes.AddressWithChecksum(addr).String()
181+
}
182+
161183
func Base64ByteSerializer(b []byte) interface{} {
162184
return base64.StdEncoding.EncodeToString(b)
163185
}
@@ -210,9 +232,12 @@ func (s *Serializer) serializeElementaryType(ctx context.Context, breadcrumbs st
210232
case ElementaryTypeInt, ElementaryTypeUint:
211233
return s.is(cv.Value.(*big.Int)), nil
212234
case ElementaryTypeAddress:
213-
addr := make([]byte, 20)
214-
cv.Value.(*big.Int).FillBytes(addr)
215-
return s.bs(addr), nil
235+
var addr [20]byte
236+
cv.Value.(*big.Int).FillBytes(addr[:])
237+
if s.ad == nil {
238+
return s.bs(addr[:]), nil
239+
}
240+
return s.ad(addr), nil
216241
case ElementaryTypeBool:
217242
return (cv.Value.(*big.Int).Int64() == 1), nil
218243
case ElementaryTypeFixed, ElementaryTypeUfixed:

pkg/abi/outputserialization_test.go

Lines changed: 50 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -112,9 +112,6 @@ func TestJSONSerializationFormatsTuple(t *testing.T) {
112112

113113
func TestJSONSerializationNumbers(t *testing.T) {
114114

115-
abi := testABI(t, sampleABI1)
116-
assert.NotNil(t, abi)
117-
118115
v, err := (ParameterArray{{Type: "uint"}, {Type: "int"}}).ParseJSON([]byte(`[
119116
"123000000000000000000000112233",
120117
"-0x18d6f3720c92d9d437801b669"
@@ -144,6 +141,56 @@ func TestJSONSerializationNumbers(t *testing.T) {
144141

145142
}
146143

144+
func TestJSONSerializationAddresses(t *testing.T) {
145+
146+
v, err := (ParameterArray{{Type: "address"}}).ParseJSON([]byte(`[
147+
"0xC66A547171fdE5FbEfC546B58E66AaC300Cb6150"
148+
]`))
149+
assert.NoError(t, err)
150+
151+
j, err := v.JSON()
152+
assert.NoError(t, err)
153+
assert.JSONEq(t, `{
154+
"0": "c66a547171fde5fbefc546b58e66aac300cb6150"
155+
}`, string(j))
156+
157+
j, err = NewSerializer().
158+
SetByteSerializer(Base64ByteSerializer). // confirming no effect
159+
SerializeJSON(v)
160+
assert.NoError(t, err)
161+
assert.JSONEq(t, `{
162+
"0": "xmpUcXH95fvvxUa1jmaqwwDLYVA="
163+
}`, string(j))
164+
165+
j, err = NewSerializer().
166+
SetByteSerializer(Base64ByteSerializer). // confirming no effect
167+
SetAddressSerializer(HexAddrSerializer0xPrefix).
168+
SerializeJSON(v)
169+
assert.NoError(t, err)
170+
assert.JSONEq(t, `{
171+
"0": "0xc66a547171fde5fbefc546b58e66aac300cb6150"
172+
}`, string(j))
173+
174+
j, err = NewSerializer().
175+
SetByteSerializer(Base64ByteSerializer). // confirming no effect
176+
SetAddressSerializer(HexAddrSerializerPlain).
177+
SerializeJSON(v)
178+
assert.NoError(t, err)
179+
assert.JSONEq(t, `{
180+
"0": "c66a547171fde5fbefc546b58e66aac300cb6150"
181+
}`, string(j))
182+
183+
j, err = NewSerializer().
184+
SetByteSerializer(Base64ByteSerializer). // confirming no effect
185+
SetAddressSerializer(ChecksumAddrSerializer).
186+
SerializeJSON(v)
187+
assert.NoError(t, err)
188+
assert.JSONEq(t, `{
189+
"0": "0xC66A547171fdE5FbEfC546B58E66AaC300Cb6150"
190+
}`, string(j))
191+
192+
}
193+
147194
func TestJSONSerializationForTypes(t *testing.T) {
148195

149196
abi := testABI(t, sampleABI2)

0 commit comments

Comments
 (0)