@@ -87,18 +87,38 @@ func NewBaseUnits(amount quantity.Quantity, denomination Denomination) BaseUnits
8787 }
8888}
8989
90+ // FormatNamedAddress returns a human-friendly representation of an address:
91+ // it prints the name (if known) followed by the preferred form of the address
92+ // in parentheses. If an Ethereum hex address mapping is provided for the native
93+ // address, it is used; otherwise the native Bech32 address is used.
94+ func FormatNamedAddress (ctx context.Context , addr Address ) string {
95+ name := ""
96+ if an , ok := ctx .Value (ContextKeyAccountNames ).(AccountNames ); ok && an != nil {
97+ if n , exists := an [addr .String ()]; exists {
98+ name = n
99+ }
100+ }
101+ if name == "" {
102+ // Unknown address; return native Bech32 form.
103+ return addr .String ()
104+ }
105+
106+ // Prefer Ethereum hex if available.
107+ if ethMap , ok := ctx .Value (ContextKeyAccountEthMap ).(map [string ]string ); ok && ethMap != nil {
108+ if ethHex , has := ethMap [addr .String ()]; has && ethHex != "" {
109+ return fmt .Sprintf ("%s (%s)" , name , ethHex )
110+ }
111+ }
112+ return fmt .Sprintf ("%s (%s)" , name , addr .String ())
113+ }
114+
90115// PrettyPrintToAmount is a helper for printing To-Amount transaction bodies (e.g. transfer, deposit, withdraw).
91116func PrettyPrintToAmount (ctx context.Context , prefix string , w io.Writer , to * Address , amount BaseUnits ) {
92117 toStr := "Self"
93118 if to != nil {
94- toStr = to .String ()
95- an , ok := ctx .Value (ContextKeyAccountNames ).(AccountNames )
96- if ok {
97- if name , ok := an [to .String ()]; ok {
98- toStr = fmt .Sprintf ("%s (%s)" , name , to )
99- }
100- }
119+ toStr = FormatNamedAddress (ctx , * to )
101120 }
121+
102122 fmt .Fprintf (w , "%sTo: %s\n " , prefix , toStr )
103123 fmt .Fprintf (w , "%sAmount: " , prefix )
104124 amount .PrettyPrint (ctx , prefix , w )
0 commit comments