Skip to content

Commit

Permalink
Merge pull request oasisprotocol/oasis-sdk#1038
Browse files Browse the repository at this point in the history
  • Loading branch information
matevz committed Jul 12, 2022
2 parents 8f0c690 + 319be9b commit 3dccbd1
Show file tree
Hide file tree
Showing 6 changed files with 134 additions and 15 deletions.
34 changes: 24 additions & 10 deletions cmd/accounts.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"github.com/oasisprotocol/oasis-sdk/client-sdk/go/helpers"
"github.com/oasisprotocol/oasis-sdk/client-sdk/go/modules/accounts"
"github.com/oasisprotocol/oasis-sdk/client-sdk/go/modules/consensusaccounts"
"github.com/oasisprotocol/oasis-sdk/client-sdk/go/testing"
"github.com/oasisprotocol/oasis-sdk/client-sdk/go/types"
)

Expand Down Expand Up @@ -65,7 +66,7 @@ var (
c, err := connection.Connect(ctx, npa.Network)
cobra.CheckErr(err)

addr, err := helpers.ResolveAddress(npa.Network, targetAddress)
addr, err := common.ResolveLocalAccountOrAddress(npa.Network, targetAddress)
cobra.CheckErr(err)

height, err := common.GetActualHeight(
Expand Down Expand Up @@ -234,7 +235,7 @@ var (
}

// Resolve beneficiary address.
benAddr, err := helpers.ResolveAddress(npa.Network, beneficiary)
benAddr, err := common.ResolveLocalAccountOrAddress(npa.Network, beneficiary)
cobra.CheckErr(err)

// Parse amount.
Expand Down Expand Up @@ -295,7 +296,7 @@ var (
var toAddr *types.Address
if to != "" {
var err error
toAddr, err = helpers.ResolveAddress(npa.Network, to)
toAddr, err = common.ResolveLocalAccountOrAddress(npa.Network, to)
cobra.CheckErr(err)
}

Expand Down Expand Up @@ -384,20 +385,33 @@ var (
cobra.CheckErr(err)
}

// Resolve destination address when specified.
// Resolve destination address when explicitly specified.
var toAddr *types.Address
var addrToCheck string
if to != "" {
var err error
toAddr, err = helpers.ResolveAddress(npa.Network, to)
toAddr, err = common.ResolveLocalAccountOrAddress(npa.Network, to)
cobra.CheckErr(err)
addrToCheck = toAddr.String()
} else {
// Destination address is implicit, but obtain it for safety check below nonetheless.
addr, err := helpers.ResolveAddress(npa.Network, npa.Account.Address)
cobra.CheckErr(err)
addrToCheck = addr.String()
}

// Safety check for withdrawals to known accounts that are not supported on the consensus layer.
for name, acc := range cliConfig.Global().Wallet.All {
if acc.Address == toAddr.String() && !acc.HasConsensusSigner() {
if acc.Address == addrToCheck && !acc.HasConsensusSigner() {
cobra.CheckErr(fmt.Errorf("account '%s' (%s) will not be able to sign transactions on consensus layer", name, acc.Address))
}
}
for name := range testing.TestAccounts {
testAcc, _ := common.LoadTestAccount(name)
if testAcc.Address().String() == addrToCheck && testAcc.ConsensusSigner() == nil {
cobra.CheckErr(fmt.Errorf("test account '%s' (%s) will not be able to sign transactions on consensus layer", name, testAcc.Address().String()))
}
}

// Parse amount.
// TODO: This should actually query the ParaTime (or config) to check what the consensus
Expand Down Expand Up @@ -479,7 +493,7 @@ var (
}

// Resolve destination address.
toAddr, err := helpers.ResolveAddress(npa.Network, to)
toAddr, err := common.ResolveLocalAccountOrAddress(npa.Network, to)
cobra.CheckErr(err)

acc := common.LoadAccount(cfg, npa.AccountName)
Expand Down Expand Up @@ -589,7 +603,7 @@ var (
}

// Resolve destination address.
toAddr, err := helpers.ResolveAddress(npa.Network, to)
toAddr, err := common.ResolveLocalAccountOrAddress(npa.Network, to)
cobra.CheckErr(err)

acc := common.LoadAccount(cfg, npa.AccountName)
Expand Down Expand Up @@ -642,7 +656,7 @@ var (
}

// Resolve destination address.
fromAddr, err := helpers.ResolveAddress(npa.Network, from)
fromAddr, err := common.ResolveLocalAccountOrAddress(npa.Network, from)
cobra.CheckErr(err)

acc := common.LoadAccount(cfg, npa.AccountName)
Expand Down Expand Up @@ -712,7 +726,7 @@ var (
now, err = conn.Consensus().Beacon().GetEpoch(ctx, height)
cobra.CheckErr(err)

addr, err := helpers.ResolveAddress(npa.Network, npa.Account.Address)
addr, err := common.ResolveLocalAccountOrAddress(npa.Network, npa.Account.Address)
cobra.CheckErr(err)

stakingConn := conn.Consensus().Staking()
Expand Down
13 changes: 10 additions & 3 deletions cmd/common/selector.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (

cliConfig "github.com/oasisprotocol/oasis-sdk/cli/config"
"github.com/oasisprotocol/oasis-sdk/client-sdk/go/config"
"github.com/oasisprotocol/oasis-sdk/client-sdk/go/helpers"
)

var (
Expand Down Expand Up @@ -66,9 +67,15 @@ func GetNPASelection(cfg *cliConfig.Config) *NPASelection {
s.AccountName = selectedAccount
}
if s.AccountName != "" {
s.Account = cfg.Wallet.All[s.AccountName]
if s.Account == nil {
cobra.CheckErr(fmt.Errorf("account '%s' does not exist in the wallet", s.AccountName))
if testName := helpers.ParseTestAccountAddress(s.AccountName); testName != "" {
testAcc, err := LoadTestAccountConfig(testName)
cobra.CheckErr(err)
s.Account = testAcc
} else {
s.Account = cfg.Wallet.All[s.AccountName]
if s.Account == nil {
cobra.CheckErr(fmt.Errorf("account '%s' does not exist in the wallet", s.AccountName))
}
}
}

Expand Down
46 changes: 46 additions & 0 deletions cmd/common/wallet.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,22 @@ import (

"github.com/oasisprotocol/oasis-sdk/cli/config"
"github.com/oasisprotocol/oasis-sdk/cli/wallet"
"github.com/oasisprotocol/oasis-sdk/cli/wallet/test"
configSdk "github.com/oasisprotocol/oasis-sdk/client-sdk/go/config"
"github.com/oasisprotocol/oasis-sdk/client-sdk/go/helpers"
"github.com/oasisprotocol/oasis-sdk/client-sdk/go/testing"
"github.com/oasisprotocol/oasis-sdk/client-sdk/go/types"
)

// LoadAccount loads the given named account.
func LoadAccount(cfg *config.Config, name string) wallet.Account {
// Check if the specified account is a test account.
if testName := helpers.ParseTestAccountAddress(name); testName != "" {
acc, err := LoadTestAccount(testName)
cobra.CheckErr(err)
return acc
}

// Early check for whether the account exists so that we don't ask for passphrase first.
var (
acfg *config.Account
Expand All @@ -38,3 +50,37 @@ func LoadAccount(cfg *config.Config, name string) wallet.Account {

return acc
}

// LoadTestAccount loads the given named test account.
func LoadTestAccount(name string) (wallet.Account, error) {
if testKey, ok := testing.TestAccounts[name]; ok {
return test.NewTestAccount(testKey)
}
return nil, fmt.Errorf("test account %s does not exist", name)
}

// LoadTestAccountConfig loads config for the given named test account.
func LoadTestAccountConfig(name string) (*config.Account, error) {
testAcc, err := LoadTestAccount(name)
if err != nil {
return nil, err
}

return &config.Account{
Description: "",
Kind: test.Kind,
Address: testAcc.Address().String(),
Config: nil,
}, nil
}

// ResolveLocalAccountOrAddress resolves a string address into the corresponding account address.
func ResolveLocalAccountOrAddress(net *configSdk.Network, address string) (*types.Address, error) {
// Check, if address is the account name in the wallet.
if acc, ok := config.Global().Wallet.All[address]; ok {
addr := acc.GetAddress()
return &addr, nil
}

return helpers.ResolveAddress(net, address)
}
2 changes: 1 addition & 1 deletion cmd/contracts.go
Original file line number Diff line number Diff line change
Expand Up @@ -331,7 +331,7 @@ func parsePolicy(net *config.Network, wallet *cliConfig.Account, policy string)
return &contracts.Policy{Address: &address}
case strings.HasPrefix(policy, "address:"):
policy = strings.TrimPrefix(policy, "address:")
address, err := helpers.ResolveAddress(net, policy)
address, err := common.ResolveLocalAccountOrAddress(net, policy)
if err != nil {
cobra.CheckErr(fmt.Errorf("malformed address in policy: %w", err))
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/wallet.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ var (
Run: func(cmd *cobra.Command, args []string) {
cfg := config.Global()
table := table.New()
table.SetHeader([]string{"Name", "Kind", "Address"})
table.SetHeader([]string{"Account", "Kind", "Address"})

var output [][]string
for name, acc := range cfg.Wallet.All {
Expand Down
52 changes: 52 additions & 0 deletions wallet/test/test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package test

import (
"encoding/base64"

coreSignature "github.com/oasisprotocol/oasis-core/go/common/crypto/signature"

"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/testing"
"github.com/oasisprotocol/oasis-sdk/client-sdk/go/types"
)

const (
// Kind is the account kind for the test accounts.
Kind = "test"
)

type testAccount struct {
testKey testing.TestKey
}

func NewTestAccount(testKey testing.TestKey) (wallet.Account, error) {
return &testAccount{testKey: testKey}, nil
}

func (a *testAccount) ConsensusSigner() coreSignature.Signer {
type wrappedSigner interface {
Unwrap() coreSignature.Signer
}

if ws, ok := a.testKey.Signer.(wrappedSigner); ok {
return ws.Unwrap()
}
return nil
}

func (a *testAccount) Signer() signature.Signer {
return a.testKey.Signer
}

func (a *testAccount) Address() types.Address {
return a.testKey.Address
}

func (a *testAccount) SignatureAddressSpec() types.SignatureAddressSpec {
return a.testKey.SigSpec
}

func (a *testAccount) UnsafeExport() string {
return base64.StdEncoding.EncodeToString(a.testKey.SecretKey)
}

0 comments on commit 3dccbd1

Please sign in to comment.