-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
cli: Add support for storing oasis and ethereum addresses
- Loading branch information
Showing
11 changed files
with
310 additions
and
69 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,184 @@ | ||
package address | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/AlecAivazis/survey/v2" | ||
"github.com/mitchellh/mapstructure" | ||
"github.com/oasisprotocol/oasis-sdk/cli/wallet" | ||
"github.com/oasisprotocol/oasis-sdk/client-sdk/go/crypto/signature" | ||
"github.com/oasisprotocol/oasis-sdk/client-sdk/go/helpers" | ||
"github.com/oasisprotocol/oasis-sdk/client-sdk/go/types" | ||
"github.com/spf13/cobra" | ||
flag "github.com/spf13/pflag" | ||
|
||
ethCommon "github.com/ethereum/go-ethereum/common" | ||
coreSignature "github.com/oasisprotocol/oasis-core/go/common/crypto/signature" | ||
) | ||
|
||
const ( | ||
// Kind is the account kind for the test accounts. | ||
Kind = "address" | ||
|
||
// ethAddressCfgKey is an optional key passed in the config files. | ||
ethAddressCfgKey = "eth_address" | ||
) | ||
|
||
type accountConfig struct { | ||
EthAddress string `mapstructure:"eth_address,omitempty"` | ||
} | ||
|
||
type addressAccount struct { | ||
address types.Address | ||
cfg *accountConfig | ||
} | ||
|
||
func newAccount(address types.Address, cfg *accountConfig) (wallet.Account, error) { | ||
return &addressAccount{address: address, cfg: cfg}, nil | ||
} | ||
|
||
type addressAccountFactory struct{} | ||
|
||
func (a *addressAccountFactory) HasConsensusSigner(cfg map[string]interface{}) bool { | ||
return false | ||
} | ||
|
||
func (a *addressAccountFactory) unmarshalConfig(raw map[string]interface{}) (*accountConfig, error) { | ||
if raw == nil { | ||
// No eth_address defined. | ||
return &accountConfig{}, nil | ||
} | ||
|
||
var cfg accountConfig | ||
if err := mapstructure.Decode(raw, &cfg); err != nil { | ||
return nil, err | ||
} | ||
return &cfg, nil | ||
} | ||
|
||
func (a *addressAccountFactory) Create(name string, passphrase string, cfg map[string]interface{}) (wallet.Account, error) { | ||
return nil, fmt.Errorf("address: create not supported") | ||
} | ||
|
||
func (a *addressAccountFactory) Load(_ string, _ string, rawCfg map[string]interface{}, rawAddr string) (wallet.Account, error) { | ||
cfg, err := a.unmarshalConfig(rawCfg) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
addr, _, err := helpers.ResolveEthOrOasisAddress(rawAddr) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return newAccount(*addr, cfg) | ||
} | ||
|
||
func (a *addressAccountFactory) Remove(name string, cfg map[string]interface{}) error { | ||
return nil | ||
} | ||
|
||
func (a *addressAccountFactory) Rename(old, new string, cfg map[string]interface{}) error { | ||
return nil | ||
} | ||
|
||
func (a *addressAccountFactory) Import(_ string, _ string, cfg map[string]interface{}, src *wallet.ImportSource) (wallet.Account, error) { | ||
addr, ethAddr, err := helpers.ResolveEthOrOasisAddress(src.Data) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
ethAddrHex := "" | ||
if ethAddr != nil { | ||
ethAddrHex = ethAddr.Hex() | ||
cfg[ethAddressCfgKey] = ethAddrHex | ||
} | ||
|
||
return newAccount(*addr, &accountConfig{ | ||
EthAddress: ethAddrHex, | ||
}) | ||
} | ||
|
||
func (a *addressAccountFactory) Kind() string { | ||
return Kind | ||
} | ||
|
||
func (a *addressAccountFactory) PrettyKind(rawCfg map[string]interface{}) string { | ||
if _, ok := rawCfg[ethAddressCfgKey]; ok { | ||
return "address (ethereum)" | ||
} | ||
return "address (native)" | ||
} | ||
|
||
func (a *addressAccountFactory) Flags() *flag.FlagSet { | ||
return nil | ||
} | ||
|
||
func (a *addressAccountFactory) GetConfigFromFlags() (map[string]interface{}, error) { | ||
return map[string]interface{}{}, nil | ||
} | ||
|
||
func (a *addressAccountFactory) GetConfigFromSurvey(_ *wallet.ImportKind) (map[string]interface{}, error) { | ||
return map[string]interface{}{}, nil | ||
} | ||
|
||
func (a *addressAccountFactory) DataPrompt(_ wallet.ImportKind, _ map[string]interface{}) survey.Prompt { | ||
return &survey.Multiline{Message: "Address (bech32 or hex-encoded):"} | ||
} | ||
|
||
func (a *addressAccountFactory) DataValidator(_ wallet.ImportKind, _ map[string]interface{}) survey.Validator { | ||
return func(ans interface{}) error { | ||
if addr, _, err := helpers.ResolveEthOrOasisAddress(ans.(string)); addr == nil { | ||
if err != nil { | ||
return err | ||
} | ||
return fmt.Errorf("unsupported address format") | ||
} | ||
return nil | ||
} | ||
} | ||
|
||
func (a *addressAccountFactory) RequiresPassphrase() bool { | ||
// Address accounts do not require a passphrase. | ||
return false | ||
} | ||
|
||
func (a *addressAccountFactory) SupportedImportKinds() []wallet.ImportKind { | ||
return []wallet.ImportKind{ | ||
wallet.ImportKindAddress, | ||
} | ||
} | ||
|
||
func (a *addressAccount) ConsensusSigner() coreSignature.Signer { | ||
return nil | ||
} | ||
|
||
func (a *addressAccount) Signer() signature.Signer { | ||
return nil | ||
} | ||
|
||
func (a *addressAccount) Address() types.Address { | ||
return a.address | ||
} | ||
|
||
func (a *addressAccount) EthAddress() *ethCommon.Address { | ||
if a.cfg.EthAddress != "" { | ||
_, ethAddr, err := helpers.ResolveEthOrOasisAddress(a.cfg.EthAddress) | ||
cobra.CheckErr(err) | ||
|
||
return ethAddr | ||
} | ||
return nil | ||
} | ||
|
||
func (a *addressAccount) SignatureAddressSpec() types.SignatureAddressSpec { | ||
return types.SignatureAddressSpec{} | ||
} | ||
|
||
func (a *addressAccount) UnsafeExport() string { | ||
return "N/A" | ||
} | ||
|
||
func init() { | ||
wallet.Register(&addressAccountFactory{}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.