Skip to content

Commit c6e493b

Browse files
feat: format known addresses for consensus txes
1 parent 24194a3 commit c6e493b

3 files changed

Lines changed: 34 additions & 9 deletions

File tree

client-sdk/go/modules/consensusaccounts/types.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,8 +65,8 @@ type Undelegate struct {
6565
}
6666

6767
// PrettyPrint writes a pretty-printed representation of the transaction to the given writer.
68-
func (ud *Undelegate) PrettyPrint(_ context.Context, prefix string, w io.Writer) {
69-
fmt.Fprintf(w, "%sFrom: %s\n", prefix, ud.From)
68+
func (ud *Undelegate) PrettyPrint(ctx context.Context, prefix string, w io.Writer) {
69+
fmt.Fprintf(w, "%sFrom: %s\n", prefix, types.FormatNamedAddress(ctx, ud.From))
7070
fmt.Fprintf(w, "%sShares: %s\n", prefix, ud.Shares)
7171
}
7272

client-sdk/go/types/address.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,11 @@ type contextKey string
4444
// ContextKeyAccountNames is the key to retrieve the public key to account name map from context.
4545
const ContextKeyAccountNames = contextKey("runtime/account-names")
4646

47+
// ContextKeyAccountEthMap is the key to retrieve a mapping from native (Bech32)
48+
// Oasis addresses to their corresponding Ethereum hex addresses for pretty printing.
49+
// When present, Ethereum addresses take precedence inside parentheses.
50+
const ContextKeyAccountEthMap = contextKey("runtime/account-eth-map")
51+
4752
// AccountNames maps public key or address to user-defined account name for pretty printing.
4853
type AccountNames map[string]string
4954

client-sdk/go/types/token.go

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -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).
91116
func 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

Comments
 (0)