/
account.go
301 lines (263 loc) · 7.58 KB
/
account.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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
package cmd
import (
"bytes"
"context"
"crypto/ed25519"
"encoding/json"
"fmt"
"io/ioutil"
"log"
url2 "github.com/AccumulateNetwork/accumulate/internal/url"
"github.com/AccumulateNetwork/accumulate/protocol"
"github.com/AccumulateNetwork/accumulate/types"
acmeapi "github.com/AccumulateNetwork/accumulate/types/api"
"github.com/mdp/qrterminal"
"github.com/spf13/cobra"
)
var accountCmd = &cobra.Command{
Use: "account",
Short: "Create and get token accounts",
Run: func(cmd *cobra.Command, args []string) {
var out string
var err error
if len(args) > 0 {
switch arg := args[0]; arg {
case "get":
if len(args) > 1 {
out, err = GetAccount(args[1])
} else {
fmt.Println("Usage:")
PrintAccountGet()
}
case "create":
if len(args) > 4 {
switch args[1] {
case "token":
out, err = CreateAccount(args[2], args[3:])
case "data":
out, err = CreateDataAccount(args[2], args[3:])
default:
fmt.Printf("Deprecation Warning!\nTo create a token account, in future please specify either \"token\" or \"data\"\n\n")
//this will be removed in future release and replaced with usage: PrintAccountCreate()
out, err = CreateAccount(args[1], args[2:])
}
} else {
fmt.Println("Usage:")
PrintAccountCreate()
}
case "qr":
if len(args) > 1 {
out, err = QrAccount(args[1])
} else {
fmt.Println("Usage:")
PrintAccountQr()
}
case "generate":
out, err = GenerateAccount()
case "list":
out, err = ListAccounts()
case "restore":
out, err = RestoreAccounts()
default:
fmt.Println("Usage:")
PrintAccount()
}
} else {
fmt.Println("Usage:")
PrintAccount()
}
printOutput(cmd, out, err)
},
}
func PrintAccountGet() {
fmt.Println(" accumulate account get [url] Get lite token account by URL")
}
func PrintAccountQr() {
fmt.Println(" accumulate account qr [url] Display QR code for lite account URL")
}
func PrintAccountGenerate() {
fmt.Println(" accumulate account generate Generate random lite token account")
}
func PrintAccountList() {
fmt.Println(" accumulate account list Display all lite token accounts")
}
func PrintAccountRestore() {
fmt.Println(" accumulate account restore Restore old lite token accounts")
}
func PrintAccountCreate() {
fmt.Println(" accumulate account create token [actor adi] [signing key name] [key index (optional)] [key height (optional)] [new token account url] [tokenUrl] [keyBookUrl] Create a token account for an ADI")
fmt.Println(" accumulate account create data [actor adi] [signing key name] [key index (optional)] [key height (optional)] [new data account url] [keyBookUrl] Create a data account under an ADI")
}
func PrintAccountImport() {
fmt.Println(" accumulate account import [private-key] Import lite token account from private key hex")
}
func PrintAccountExport() {
fmt.Println(" accumulate account export [url] Export private key hex of lite token account")
}
func PrintAccount() {
PrintAccountGet()
PrintAccountQr()
PrintAccountGenerate()
PrintAccountList()
PrintAccountRestore()
PrintAccountCreate()
PrintAccountImport()
PrintAccountExport()
}
func GetAccount(url string) (string, error) {
var res acmeapi.APIDataResponse
params := acmeapi.APIRequestURL{}
params.URL = types.String(url)
if err := Client.Request(context.Background(), "token-account", params, &res); err != nil {
return PrintJsonRpcError(err)
}
return PrintQueryResponse(&res)
}
func QrAccount(s string) (string, error) {
u, err := url2.Parse(s)
if err != nil {
return "", fmt.Errorf("%q is not a valid Accumulate URL: %v\n", s, err)
}
b := bytes.NewBufferString("")
qrterminal.GenerateWithConfig(u.String(), qrterminal.Config{
Level: qrterminal.M,
Writer: b,
HalfBlocks: true,
BlackChar: qrterminal.BLACK_BLACK,
BlackWhiteChar: qrterminal.BLACK_WHITE,
WhiteChar: qrterminal.WHITE_WHITE,
WhiteBlackChar: qrterminal.WHITE_BLACK,
QuietZone: 2,
})
r, err := ioutil.ReadAll(b)
return string(r), err
}
//account create adiActor labelOrPubKeyHex height index tokenUrl keyBookUrl
func CreateAccount(url string, args []string) (string, error) {
actor, err := url2.Parse(url)
if err != nil {
PrintAccountCreate()
return "", err
}
args, si, privKey, err := prepareSigner(actor, args)
if len(args) < 3 {
PrintAccountCreate()
return "", fmt.Errorf("insufficient number of command line arguments")
}
accountUrl, err := url2.Parse(args[0])
if err != nil {
PrintAccountCreate()
return "", fmt.Errorf("invalid account url %s", args[0])
}
if actor.Authority != accountUrl.Authority {
return "", fmt.Errorf("account url to create (%s) doesn't match the authority adi (%s)", accountUrl.Authority, actor.Authority)
}
tok, err := url2.Parse(args[1])
if err != nil {
return "", fmt.Errorf("invalid token url")
}
var keybook string
if len(args) > 2 {
kbu, err := url2.Parse(args[2])
if err != nil {
return "", fmt.Errorf("invalid key book url")
}
keybook = kbu.String()
}
//make sure this is a valid token account
tokenJson, err := Get(tok.String())
if err != nil {
return "", err
}
token := protocol.TokenIssuer{}
err = json.Unmarshal([]byte(tokenJson), &token)
if err != nil {
PrintAccountCreate()
return "", fmt.Errorf("invalid token type %v", err)
}
tac := &protocol.TokenAccountCreate{}
tac.Url = accountUrl.String()
tac.TokenUrl = tok.String()
tac.KeyBookUrl = keybook
binaryData, err := tac.MarshalBinary()
if err != nil {
return "", err
}
jsonData, err := json.Marshal(&tac)
if err != nil {
return "", err
}
nonce := nonceFromTimeNow()
params, err := prepareGenTx(jsonData, binaryData, actor, si, privKey, nonce)
if err != nil {
return "", err
}
var res acmeapi.APIDataResponse
if err := Client.Request(context.Background(), "token-account-create", params, &res); err != nil {
//todo: if we fail, then we need to remove the adi from storage or keep it and try again later...
return "", err
}
ar := ActionResponse{}
err = json.Unmarshal(*res.Data, &ar)
if err != nil {
return "", fmt.Errorf("error unmarshalling account create result")
}
return ar.Print()
}
func GenerateAccount() (string, error) {
return GenerateKey("")
}
func ListAccounts() (string, error) {
b, err := Db.GetBucket(BucketLabel)
if err != nil {
//no accounts so nothing to do...
return "", err
}
var out string
for _, v := range b.KeyValueList {
lt, err := protocol.LiteAddress(v.Value, protocol.AcmeUrl().String())
if err != nil {
continue
}
if lt.String() == string(v.Key) {
out += fmt.Sprintf("%s\n", v.Key)
}
}
//TODO: this probably should also list out adi accounts as well
return out, nil
}
func RestoreAccounts() (out string, err error) {
anon, err := Db.GetBucket(BucketAnon)
if err != nil {
//no anon accounts so nothing to do...
return
}
for _, v := range anon.KeyValueList {
u, err := url2.Parse(string(v.Key))
if err != nil {
out += fmt.Sprintf("%q is not a valid URL\n", v.Key)
}
key, _, err := protocol.ParseLiteAddress(u)
if err != nil {
out += fmt.Sprintf("%q is not a valid lite account: %v\n", v.Key, err)
} else if key == nil {
out += fmt.Sprintf("%q is not a lite account\n", v.Key)
}
privKey := ed25519.PrivateKey(v.Value)
pubKey := privKey.Public().(ed25519.PublicKey)
out += fmt.Sprintf("Converting %s : %x\n", v.Key, pubKey)
err = Db.Put(BucketLabel, v.Key, pubKey)
if err != nil {
log.Fatal(err)
}
err = Db.Put(BucketKeys, pubKey, privKey)
if err != nil {
return "", err
}
err = Db.DeleteBucket(BucketAnon)
if err != nil {
return "", err
}
}
return out, nil
}