Skip to content

Commit e34e6ef

Browse files
refactor: unify address formatting context generation
1 parent f0e7240 commit e34e6ef

20 files changed

Lines changed: 140 additions & 78 deletions

File tree

cmd/account/show/allowances.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,8 @@ func prettyPrintAllowanceDescriptions(
6262
// element so we can align all values.
6363
lenLongest := lenLongestString(beneficiaryFieldName, amountFieldName)
6464

65-
// Precompute address formatting context for efficiency (network-aware for paratime names).
66-
addrCtx := common.GenAddressFormatContextForNetwork(network)
65+
// Precompute address formatting context for efficiency.
66+
addrCtx := common.GenAddressFormatContext()
6767

6868
for _, desc := range allowDescriptions {
6969
prettyAddr := common.PrettyAddressWith(addrCtx, desc.beneficiary.String())

cmd/account/show/delegations.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -110,8 +110,8 @@ func prettyPrintDelegationDescriptions(
110110
lenLongest = lenLongestString(addressFieldName, amountFieldName, endTimeFieldName)
111111
}
112112

113-
// Precompute address formatting context for efficiency (network-aware for paratime names).
114-
addrCtx := common.GenAddressFormatContextForNetwork(network)
113+
// Precompute address formatting context for efficiency.
114+
addrCtx := common.GenAddressFormatContext()
115115

116116
for _, desc := range delDescriptions {
117117
prettyAddr := common.PrettyAddressWith(addrCtx, desc.address.String())

cmd/account/show/show.go

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -91,20 +91,23 @@ var (
9191
nativeAddr, ethAddr, err := common.ResolveLocalAccountOrAddress(npa.Network, targetAddress)
9292
cobra.CheckErr(err)
9393
out.NativeAddress = nativeAddr
94-
out.Name = common.FindAccountNameForNetwork(npa.Network, nativeAddr.String())
94+
addrCtx := common.GenAddressFormatContext()
95+
out.Name = addrCtx.Names[nativeAddr.String()]
9596

96-
// If eth address is not available, try to get it from wallet config (no unlock required).
97+
// If eth address is not available, try to get it from locally-known mappings
98+
// (wallet/addressbook/test accounts). No unlock required.
9799
if ethAddr == nil {
98-
for _, walletCfg := range cfg.Wallet.All {
99-
if walletCfg.Address == nativeAddr.String() {
100-
ethAddr = walletCfg.GetEthAddress()
101-
break
102-
}
100+
if ethHex := addrCtx.Eth[nativeAddr.String()]; ethHex != "" && ethCommon.IsHexAddress(ethHex) {
101+
eth := ethCommon.HexToAddress(ethHex)
102+
ethAddr = &eth
103103
}
104104
}
105105

106106
// If eth address is still not available and the user selected a wallet account,
107107
// load it (may require passphrase) to derive and persist eth_address metadata.
108+
//
109+
// NOTE: We only do this for an explicitly selected wallet account to avoid surprising
110+
// interactive prompts when a user provides an arbitrary address.
108111
if ethAddr == nil && walletNameForEth != "" {
109112
if walletCfg, ok := cfg.Wallet.All[walletNameForEth]; ok && walletCfg.SupportsEthAddress() {
110113
// Avoid prompting in non-interactive contexts (e.g. piping output).

cmd/common/helpers.go

Lines changed: 36 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,12 @@ package common
33
import (
44
"fmt"
55
"os"
6+
"sort"
67

78
ethCommon "github.com/ethereum/go-ethereum/common"
89
"github.com/spf13/cobra"
910

1011
staking "github.com/oasisprotocol/oasis-core/go/staking/api"
11-
configSdk "github.com/oasisprotocol/oasis-sdk/client-sdk/go/config"
1212
"github.com/oasisprotocol/oasis-sdk/client-sdk/go/helpers"
1313
"github.com/oasisprotocol/oasis-sdk/client-sdk/go/testing"
1414
"github.com/oasisprotocol/oasis-sdk/client-sdk/go/types"
@@ -46,17 +46,12 @@ func CheckForceErr(err interface{}) {
4646
cobra.CheckErr(errMsg)
4747
}
4848

49-
// GenAccountNames generates a map of all addresses -> account name for pretty printing.
50-
// Priority order (later entries overwrite earlier): test accounts < addressbook < wallet.
51-
// This ensures wallet identity takes precedence over addressbook labels for the same address.
49+
// GenAccountNames generates a map of all known native addresses -> account name for pretty printing.
50+
// It includes test accounts, configured networks (paratimes/ROFL defaults), addressbook and wallet.
51+
//
52+
// Priority order (later entries overwrite earlier):
53+
// test accounts < network entries < addressbook < wallet.
5254
func GenAccountNames() types.AccountNames {
53-
return GenAccountNamesForNetwork(nil)
54-
}
55-
56-
// GenAccountNamesForNetwork generates a map of all addresses -> account name for pretty printing,
57-
// including network-specific entries (paratimes, ROFL providers) when a network is provided.
58-
// Priority order (later entries overwrite earlier): test accounts < network entries < addressbook < wallet.
59-
func GenAccountNamesForNetwork(net *configSdk.Network) types.AccountNames {
6055
an := types.AccountNames{}
6156

6257
// Test accounts have lowest priority.
@@ -65,15 +60,34 @@ func GenAccountNamesForNetwork(net *configSdk.Network) types.AccountNames {
6560
}
6661

6762
// Network-derived entries (paratimes, ROFL providers) have second-lowest priority.
68-
if net != nil {
63+
cfg := config.Global()
64+
netNames := make([]string, 0, len(cfg.Networks.All))
65+
for name := range cfg.Networks.All {
66+
netNames = append(netNames, name)
67+
}
68+
sort.Strings(netNames)
69+
for _, netName := range netNames {
70+
net := cfg.Networks.All[netName]
71+
if net == nil {
72+
continue
73+
}
74+
6975
// Include ParaTime runtime addresses as paratime:<name>.
70-
for ptName, pt := range net.ParaTimes.All {
76+
ptNames := make([]string, 0, len(net.ParaTimes.All))
77+
for ptName := range net.ParaTimes.All {
78+
ptNames = append(ptNames, ptName)
79+
}
80+
sort.Strings(ptNames)
81+
for _, ptName := range ptNames {
82+
pt := net.ParaTimes.All[ptName]
83+
if pt == nil {
84+
continue
85+
}
86+
7187
rtAddr := types.NewAddressFromConsensus(staking.NewRuntimeAddress(pt.Namespace()))
7288
an[rtAddr.String()] = fmt.Sprintf("paratime:%s", ptName)
73-
}
7489

75-
// Include ROFL default provider addresses as rofl:provider:<paratime>.
76-
for ptName, pt := range net.ParaTimes.All {
90+
// Include ROFL default provider addresses as rofl:provider:<paratime>.
7791
if svc, ok := buildRoflProvider.DefaultRoflServices[pt.ID]; ok {
7892
if svc.Provider != "" {
7993
if a, _, err := helpers.ResolveEthOrOasisAddress(svc.Provider); err == nil && a != nil {
@@ -85,12 +99,12 @@ func GenAccountNamesForNetwork(net *configSdk.Network) types.AccountNames {
8599
}
86100

87101
// Addressbook entries have medium priority.
88-
for name, acc := range config.Global().AddressBook.All {
102+
for name, acc := range cfg.AddressBook.All {
89103
an[acc.GetAddress().String()] = name
90104
}
91105

92106
// Wallet entries have highest priority.
93-
for name, acc := range config.Global().Wallet.All {
107+
for name, acc := range cfg.Wallet.All {
94108
an[acc.GetAddress().String()] = name
95109
}
96110

@@ -103,27 +117,17 @@ func FindAccountName(address string) string {
103117
return an[address]
104118
}
105119

106-
// FindAccountNameForNetwork finds account's name in the context of a specific network.
107-
func FindAccountNameForNetwork(net *configSdk.Network, address string) string {
108-
an := GenAccountNamesForNetwork(net)
109-
return an[address]
110-
}
111-
112120
// AddressFormatContext contains precomputed maps for address formatting.
113121
type AddressFormatContext struct {
114122
// Names maps native address string to account name.
115123
Names types.AccountNames
116-
// Eth maps native address string to Ethereum hex address string (if known).
124+
// Eth maps native address string to Ethereum hex address string, if known.
125+
// This is optional metadata coming from wallet/addressbook/test accounts (and never derived from chain state).
117126
Eth map[string]string
118127
}
119128

120129
// GenAccountEthMap generates a map of native address string -> eth hex address (if known).
121130
func GenAccountEthMap() map[string]string {
122-
return GenAccountEthMapForNetwork(nil)
123-
}
124-
125-
// GenAccountEthMapForNetwork generates a map of native address string -> eth hex address (if known).
126-
func GenAccountEthMapForNetwork(_ *configSdk.Network) map[string]string {
127131
eth := make(map[string]string)
128132

129133
// From test accounts.
@@ -152,15 +156,9 @@ func GenAccountEthMapForNetwork(_ *configSdk.Network) map[string]string {
152156

153157
// GenAddressFormatContext builds both name and eth address maps for formatting.
154158
func GenAddressFormatContext() AddressFormatContext {
155-
return GenAddressFormatContextForNetwork(nil)
156-
}
157-
158-
// GenAddressFormatContextForNetwork builds both name and eth address maps for formatting,
159-
// including network-specific entries when a network is provided.
160-
func GenAddressFormatContextForNetwork(net *configSdk.Network) AddressFormatContext {
161159
return AddressFormatContext{
162-
Names: GenAccountNamesForNetwork(net),
163-
Eth: GenAccountEthMapForNetwork(net),
160+
Names: GenAccountNames(),
161+
Eth: GenAccountEthMap(),
164162
}
165163
}
166164

@@ -233,10 +231,3 @@ func PrettyResolvedAddressWith(ctx AddressFormatContext, nativeAddr *types.Addre
233231
func PrettyAddress(addr string) string {
234232
return PrettyAddressWith(GenAddressFormatContext(), addr)
235233
}
236-
237-
// PrettyAddressForNetwork formats an address for display with network context.
238-
// This includes network-derived names like paratime addresses and ROFL providers.
239-
func PrettyAddressForNetwork(net *configSdk.Network, addr types.Address) string {
240-
ctx := GenAddressFormatContextForNetwork(net)
241-
return PrettyAddressWith(ctx, addr.String())
242-
}

cmd/common/json.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -173,8 +173,8 @@ func PrettyPrint(npa *NPASelection, prefix string, blob interface{}) string {
173173
}
174174
ctx = context.WithValue(ctx, signature.ContextKeySigContext, &sigCtx)
175175

176-
// Provide network-aware names and native->ETH mapping for address formatting.
177-
addrCtx := GenAddressFormatContextForNetwork(npa.Network)
176+
// Provide locally-known names and native->ETH mapping for address formatting.
177+
addrCtx := GenAddressFormatContext()
178178
ctx = context.WithValue(ctx, types.ContextKeyAccountNames, addrCtx.Names)
179179
ctx = context.WithValue(ctx, types.ContextKeyAccountEthMap, addrCtx.Eth)
180180

cmd/contract.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ var (
5959
inst, err := conn.Runtime(npa.ParaTime).Contracts.Instance(ctx, client.RoundLatest, contracts.InstanceID(instanceID))
6060
cobra.CheckErr(err)
6161

62-
addrCtx := common.GenAddressFormatContextForNetwork(npa.Network)
62+
addrCtx := common.GenAddressFormatContext()
6363

6464
fmt.Printf("ID: %d\n", inst.ID)
6565
fmt.Printf("Code ID: %d\n", inst.CodeID)
@@ -89,7 +89,7 @@ var (
8989
code, err := conn.Runtime(npa.ParaTime).Contracts.Code(ctx, client.RoundLatest, contracts.CodeID(codeID))
9090
cobra.CheckErr(err)
9191

92-
addrCtx := common.GenAddressFormatContextForNetwork(npa.Network)
92+
addrCtx := common.GenAddressFormatContext()
9393

9494
fmt.Printf("ID: %d\n", code.ID)
9595
fmt.Printf("Hash: %s\n", code.Hash)

cmd/network/governance/list.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ var govListCmd = &cobra.Command{
2727
conn, err := connection.Connect(ctx, npa.Network)
2828
cobra.CheckErr(err)
2929

30-
addrCtx := common.GenAddressFormatContextForNetwork(npa.Network)
30+
addrCtx := common.GenAddressFormatContext()
3131

3232
table := table.New()
3333
table.Header("ID", "Kind", "Submitter", "Created At", "Closes At", "State")

cmd/network/governance/show.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ var (
5454
Run: func(_ *cobra.Command, args []string) {
5555
cfg := cliConfig.Global()
5656
npa := common.GetNPASelection(cfg)
57-
addrCtx := common.GenAddressFormatContextForNetwork(npa.Network)
57+
addrCtx := common.GenAddressFormatContext()
5858

5959
// Determine the proposal ID to query.
6060
proposalID, err := strconv.ParseUint(args[0], 10, 64)

cmd/network/show.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ func prettyPrintEntityNodes(ctx context.Context, npa *common.NPASelection, staki
4949
return err
5050
}
5151

52-
addrCtx := common.GenAddressFormatContextForNetwork(npa.Network)
52+
addrCtx := common.GenAddressFormatContext()
5353

5454
fmt.Printf("=== ENTITY ===\n")
5555

cmd/paratime/show.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,7 @@ var (
203203
fmt.Printf("Type: %d\n", ethTx.Type())
204204
toStr := "<contract creation>"
205205
if to := ethTx.To(); to != nil {
206-
addrCtx := common.GenAddressFormatContextForNetwork(npa.Network)
206+
addrCtx := common.GenAddressFormatContext()
207207
toStr = common.PrettyAddressWith(addrCtx, to.Hex())
208208
}
209209
fmt.Printf("To: %s\n", toStr)

0 commit comments

Comments
 (0)