-
Notifications
You must be signed in to change notification settings - Fork 1
/
test.go
145 lines (111 loc) · 3.71 KB
/
test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
package test
import (
"encoding/base64"
"encoding/hex"
"fmt"
"github.com/AlecAivazis/survey/v2"
ethCommon "github.com/ethereum/go-ethereum/common"
flag "github.com/spf13/pflag"
coreSignature "github.com/oasisprotocol/oasis-core/go/common/crypto/signature"
"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"
"github.com/oasisprotocol/cli/wallet"
)
const (
// Kind is the account kind for the test accounts.
Kind = "test"
)
type testAccountFactory struct{}
func (af *testAccountFactory) Kind() string {
return Kind
}
func (af *testAccountFactory) PrettyKind(rawCfg map[string]interface{}) string {
var cfg wallet.AccountConfig
if err := cfg.UnmarshalMap(rawCfg); err != nil {
return ""
}
return fmt.Sprintf("test (%s)", cfg.Algorithm)
}
func (af *testAccountFactory) Flags() *flag.FlagSet {
return nil
}
func (af *testAccountFactory) GetConfigFromFlags() (map[string]interface{}, error) {
return nil, nil
}
func (af *testAccountFactory) GetConfigFromSurvey(_ *wallet.ImportKind) (map[string]interface{}, error) {
return nil, fmt.Errorf("test: account is built-in")
}
func (af *testAccountFactory) DataPrompt(_ wallet.ImportKind, _ map[string]interface{}) survey.Prompt {
return nil
}
func (af *testAccountFactory) DataValidator(_ wallet.ImportKind, _ map[string]interface{}) survey.Validator {
return nil
}
func (af *testAccountFactory) RequiresPassphrase() bool {
return false
}
func (af *testAccountFactory) SupportedImportKinds() []wallet.ImportKind {
return []wallet.ImportKind{}
}
func (af *testAccountFactory) HasConsensusSigner(rawCfg map[string]interface{}) bool {
var cfg wallet.AccountConfig
if err := cfg.UnmarshalMap(rawCfg); err != nil {
return false
}
return cfg.Algorithm == wallet.AlgorithmEd25519Raw
}
func (af *testAccountFactory) Migrate(_ map[string]interface{}) bool {
return false
}
func (af *testAccountFactory) Create(_ string, _ string, _ map[string]interface{}) (wallet.Account, error) {
return nil, fmt.Errorf("test: account is built-in")
}
func (af *testAccountFactory) Load(_ string, _ string, _ map[string]interface{}) (wallet.Account, error) {
return nil, fmt.Errorf("test: account is built-in")
}
func (af *testAccountFactory) Remove(_ string, _ map[string]interface{}) error {
return fmt.Errorf("test: account is built-in")
}
func (af *testAccountFactory) Rename(_, _ string, _ map[string]interface{}) error {
return fmt.Errorf("test: account is built-in")
}
func (af *testAccountFactory) Import(_ string, _ string, _ map[string]interface{}, _ *wallet.ImportSource) (wallet.Account, error) {
return nil, fmt.Errorf("test: import not supported")
}
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) EthAddress() *ethCommon.Address {
return a.testKey.EthAddress
}
func (a *testAccount) SignatureAddressSpec() types.SignatureAddressSpec {
return a.testKey.SigSpec
}
func (a *testAccount) UnsafeExport() (string, string) {
if a.testKey.SigSpec.Secp256k1Eth != nil {
return hex.EncodeToString(a.testKey.SecretKey), ""
}
return base64.StdEncoding.EncodeToString(a.testKey.SecretKey), ""
}
func init() {
wallet.Register(&testAccountFactory{})
}