@@ -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).
52118func 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+ }
0 commit comments