@@ -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
8386type ByteSerializer func (b []byte ) interface {}
8487
88+ type AddressSerializer func (addr [20 ]byte ) interface {}
89+
8590func (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+
105115func (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+
161183func 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 :
0 commit comments