Skip to content

Commit 0068b35

Browse files
feat: format known addresses
1 parent 968a561 commit 0068b35

File tree

9 files changed

+102
-16
lines changed

9 files changed

+102
-16
lines changed

cmd/account/show/allowances.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ import (
1010
"github.com/oasisprotocol/oasis-sdk/client-sdk/go/config"
1111
"github.com/oasisprotocol/oasis-sdk/client-sdk/go/helpers"
1212
"github.com/oasisprotocol/oasis-sdk/client-sdk/go/types"
13+
14+
"github.com/oasisprotocol/cli/cmd/common"
1315
)
1416

1517
// allowanceDescription is a description of an allowance.
@@ -61,7 +63,8 @@ func prettyPrintAllowanceDescriptions(
6163
lenLongest := lenLongestString(beneficiaryFieldName, amountFieldName)
6264

6365
for _, desc := range allowDescriptions {
64-
fmt.Fprintf(w, "%s - %-*s %s", prefix, lenLongest, beneficiaryFieldName, desc.beneficiary)
66+
ppAddr := common.PrettyAddress(network, types.NewAddressFromConsensus(desc.beneficiary))
67+
fmt.Fprintf(w, "%s - %-*s %s", prefix, lenLongest, beneficiaryFieldName, ppAddr)
6568
if desc.self {
6669
fmt.Fprintf(w, " (self)")
6770
}

cmd/account/show/delegations.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,8 @@ func prettyPrintDelegationDescriptions(
105105
}
106106

107107
for _, desc := range delDescriptions {
108-
fmt.Fprintf(w, "%s - %-*s %s", prefix, lenLongest, addressFieldName, desc.address)
108+
ppAddr := common.PrettyAddress(network, types.NewAddressFromConsensus(desc.address))
109+
fmt.Fprintf(w, "%s - %-*s %s", prefix, lenLongest, addressFieldName, ppAddr)
109110
if desc.self {
110111
fmt.Fprintf(w, " (self)")
111112
}

cmd/account/show/show.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,9 @@ var (
5757
nativeAddr, ethAddr, err := common.ResolveLocalAccountOrAddress(npa.Network, targetAddress)
5858
cobra.CheckErr(err)
5959

60-
if name := common.FindAccountName(nativeAddr.String()); name != "" {
61-
fmt.Printf("Name: %s\n", name)
60+
if name := common.FindAccountNameForNetwork(npa.Network, nativeAddr.String()); name != "" {
61+
// Show name along with preferred address (Ethereum first if available).
62+
fmt.Printf("Name: %s\n", common.PrettyAddress(npa.Network, *nativeAddr))
6263
}
6364

6465
height, err := common.GetActualHeight(

cmd/common/helpers.go

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,13 @@ import (
55

66
"github.com/spf13/cobra"
77

8+
staking "github.com/oasisprotocol/oasis-core/go/staking/api"
9+
configSdk "github.com/oasisprotocol/oasis-sdk/client-sdk/go/config"
10+
sdkhelpers "github.com/oasisprotocol/oasis-sdk/client-sdk/go/helpers"
811
"github.com/oasisprotocol/oasis-sdk/client-sdk/go/testing"
912
"github.com/oasisprotocol/oasis-sdk/client-sdk/go/types"
1013

14+
buildRoflProvider "github.com/oasisprotocol/cli/build/rofl/provider"
1115
"github.com/oasisprotocol/cli/config"
1216
)
1317

@@ -48,8 +52,76 @@ func GenAccountNames() types.AccountNames {
4852
return an
4953
}
5054

55+
func GenAccountNamesForNetwork(net *configSdk.Network) types.AccountNames {
56+
an := GenAccountNames()
57+
58+
// Include ParaTime native addresses as paratime:<name> for the selected network.
59+
if net != nil {
60+
for ptName, pt := range net.ParaTimes.All {
61+
rtAddr := types.NewAddressFromConsensus(staking.NewRuntimeAddress(pt.Namespace()))
62+
if _, exists := an[rtAddr.String()]; !exists {
63+
an[rtAddr.String()] = fmt.Sprintf("paratime:%s", ptName)
64+
}
65+
}
66+
67+
// Include ROFL default provider addresses as rofl:provider:<paratime>.
68+
for ptName, pt := range net.ParaTimes.All {
69+
if svc, ok := buildRoflProvider.DefaultRoflServices[pt.ID]; ok {
70+
if svc.Provider != "" {
71+
if a, _, err := sdkhelpers.ResolveEthOrOasisAddress(svc.Provider); err == nil && a != nil {
72+
if _, exists := an[a.String()]; !exists {
73+
an[a.String()] = fmt.Sprintf("rofl:provider:%s", ptName)
74+
}
75+
}
76+
}
77+
}
78+
}
79+
}
80+
81+
return an
82+
}
83+
84+
func GenAccountEthMap(_ *configSdk.Network) map[string]string {
85+
em := make(map[string]string)
86+
87+
// Address book entries which may carry an Ethereum address.
88+
for _, entry := range config.Global().AddressBook.All {
89+
if ea := entry.GetEthAddress(); ea != nil {
90+
em[entry.GetAddress().String()] = ea.Hex()
91+
}
92+
}
93+
94+
// Built-in test accounts (many have both forms).
95+
for _, acc := range testing.TestAccounts {
96+
if acc.EthAddress != nil {
97+
em[acc.Address.String()] = acc.EthAddress.Hex()
98+
}
99+
}
100+
101+
return em
102+
}
103+
104+
func PrettyAddress(net *configSdk.Network, addr types.Address) string {
105+
name := GenAccountNamesForNetwork(net)[addr.String()]
106+
if name == "" {
107+
// Unknown address; return the native form as-is.
108+
return addr.String()
109+
}
110+
111+
if eth := GenAccountEthMap(net)[addr.String()]; eth != "" {
112+
return fmt.Sprintf("%s (%s)", name, eth)
113+
}
114+
return fmt.Sprintf("%s (%s)", name, addr.String())
115+
}
116+
51117
// FindAccountName finds account's name (if exists).
52118
func FindAccountName(address string) string {
53119
an := GenAccountNames()
54120
return an[address]
55121
}
122+
123+
// FindAccountNameForNetwork finds account's name in the context of a specific network.
124+
func FindAccountNameForNetwork(net *configSdk.Network, address string) string {
125+
an := GenAccountNamesForNetwork(net)
126+
return an[address]
127+
}

cmd/common/json.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,10 @@ func PrettyPrint(npa *NPASelection, prefix string, blob interface{}) string {
135135
ctx = context.WithValue(ctx, config.ContextKeyParaTimeCfg, npa.ParaTime)
136136
}
137137
ctx = context.WithValue(ctx, signature.ContextKeySigContext, &sigCtx)
138-
ctx = context.WithValue(ctx, types.ContextKeyAccountNames, GenAccountNames())
138+
139+
// Provide names (network-aware) and native->ETH mapping for Ethereum-preferred parentheses.
140+
ctx = context.WithValue(ctx, types.ContextKeyAccountNames, GenAccountNamesForNetwork(npa.Network))
141+
ctx = context.WithValue(ctx, types.ContextKeyAccountEthMap, GenAccountEthMap(npa.Network))
139142

140143
// Set up chain context for signature verification during pretty-printing.
141144
coreSignature.UnsafeResetChainContext()

cmd/rofl/deploy.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ var (
119119
cobra.CheckErr(fmt.Sprintf("Invalid provider address: %s", err))
120120
}
121121

122-
fmt.Printf("Using provider: %s (%s)\n", machine.Provider, providerAddr)
122+
fmt.Printf("Using provider: %s\n", common.PrettyAddress(npa.Network, *providerAddr))
123123

124124
if deployShowOffers {
125125
// Display all offers supported by the provider.

cmd/rofl/machine/mgmt.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ var (
8282
cobra.CheckErr(err)
8383
}
8484

85-
fmt.Printf("Using provider: %s (%s)\n", machine.Provider, providerAddr)
85+
fmt.Printf("Using provider: %s\n", common.PrettyAddress(npa.Network, *providerAddr))
8686
fmt.Printf("Canceling machine: %s [%s]\n", machineName, machine.ID)
8787
fmt.Printf("WARNING: Canceling a machine will permanently destroy it including any persistent storage!\n")
8888

@@ -157,18 +157,18 @@ var (
157157
cobra.CheckErr(err)
158158
}
159159

160-
fmt.Printf("Provider: %s (%s)\n", machine.Provider, providerAddr)
160+
fmt.Printf("Provider: %s\n", common.PrettyAddress(npa.Network, *providerAddr))
161161
fmt.Printf("Machine: %s [%s]\n", machineName, machine.ID)
162162

163163
// Resolve old admin in online mode.
164164
if !txCfg.Offline {
165165
insDsc, err := conn.Runtime(npa.ParaTime).ROFLMarket.Instance(ctx, client.RoundLatest, *providerAddr, machineID)
166166
cobra.CheckErr(err)
167167

168-
fmt.Printf("Old admin: %s\n", insDsc.Admin)
168+
fmt.Printf("Old admin: %s\n", common.PrettyAddress(npa.Network, insDsc.Admin))
169169
}
170170

171-
fmt.Printf("New admin: %s\n", newAdminAddr)
171+
fmt.Printf("New admin: %s\n", common.PrettyAddress(npa.Network, *newAdminAddr))
172172

173173
// Prepare transaction.
174174
tx := roflmarket.NewInstanceChangeAdmin(nil, &roflmarket.InstanceChangeAdmin{
@@ -226,7 +226,7 @@ var (
226226
cobra.CheckErr(err)
227227
}
228228

229-
fmt.Printf("Using provider: %s (%s)\n", machine.Provider, providerAddr)
229+
fmt.Printf("Using provider: %s\n", common.PrettyAddress(npa.Network, *providerAddr))
230230
fmt.Printf("Top-up machine: %s [%s]\n", machineName, machine.ID)
231231
fmt.Printf("Top-up term: %d x %s\n", roflCommon.TermCount, roflCommon.Term)
232232

@@ -312,7 +312,7 @@ func queueCommand(cliArgs []string, method string, args any, msgAfter string) {
312312
cobra.CheckErr(err)
313313
}
314314

315-
fmt.Printf("Using provider: %s (%s)\n", machine.Provider, providerAddr)
315+
fmt.Printf("Using provider: %s\n", common.PrettyAddress(npa.Network, *providerAddr))
316316
fmt.Printf("Machine: %s [%s]\n", machineName, machine.ID)
317317
fmt.Printf("Command: %s\n", method)
318318
fmt.Printf("Args:\n")

cmd/rofl/machine/show.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -69,12 +69,12 @@ var showCmd = &cobra.Command{
6969
}
7070

7171
fmt.Printf("Name: %s\n", machineName)
72-
fmt.Printf("Provider: %s\n", insDsc.Provider)
72+
fmt.Printf("Provider: %s\n", common.PrettyAddress(npa.Network, insDsc.Provider))
7373
fmt.Printf("ID: %s\n", insDsc.ID)
7474
fmt.Printf("Offer: %s\n", insDsc.Offer)
7575
fmt.Printf("Status: %s\n", insDsc.Status)
76-
fmt.Printf("Creator: %s\n", insDsc.Creator)
77-
fmt.Printf("Admin: %s\n", insDsc.Admin)
76+
fmt.Printf("Creator: %s\n", common.PrettyAddress(npa.Network, insDsc.Creator))
77+
fmt.Printf("Admin: %s\n", common.PrettyAddress(npa.Network, insDsc.Admin))
7878
switch insDsc.NodeID {
7979
case nil:
8080
fmt.Printf("Node ID: <none>\n")

cmd/wallet/show.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,13 @@ func showPublicWalletInfo(name string, wallet wallet.Account, accCfg *config.Acc
3030
kind = accCfg.PrettyKind()
3131
}
3232

33-
fmt.Printf("Name: %s\n", name)
33+
// Prefer Ethereum address in parentheses when available (Ticket #523).
34+
preferred := wallet.Address().String()
35+
if eth := wallet.EthAddress(); eth != nil {
36+
preferred = eth.Hex()
37+
}
38+
39+
fmt.Printf("Name: %s (%s)\n", name, preferred)
3440
fmt.Printf("Kind: %s\n", kind)
3541
if signer := wallet.Signer(); signer != nil {
3642
fmt.Printf("Public Key: %s\n", signer.Public())

0 commit comments

Comments
 (0)