/
gen_wallet.go
58 lines (49 loc) · 1.46 KB
/
gen_wallet.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
/*
Copyright © 2022 NAME HERE <EMAIL ADDRESS>
*/
package cmd
import (
"encoding/json"
"fmt"
"github.com/10gic/arweaveutil/util"
"github.com/spf13/cobra"
"os"
)
// genWalletCmd represents the genWallet command
var genWalletCmd = &cobra.Command{
Use: "gen-wallet",
Short: "Generate a wallet (jwk file)",
Long: `Generate a wallet, save the generated jwk file in current directory`,
Run: func(cmd *cobra.Command, args []string) {
key, err := util.GenerateRsaJwk()
if err != nil {
fmt.Printf(`failed to generate RSA jwk: %v\n`, err)
return
}
bytes, _ := json.Marshal(key)
// Compute the Arweave address
address, err := util.JwkToAddress(bytes)
if err != nil {
fmt.Printf(`failed to get address from jwk: %v\n`, err)
return
}
// Save keyfile
fileName := "arweave-keyfile-" + address + ".json"
err = os.WriteFile(fileName, bytes, 0644)
if err != nil {
fmt.Printf(`failed to write file %s: %v\n`, fileName, err)
return
}
fmt.Printf("keyfile %v saved\n", fileName)
},
}
func init() {
rootCmd.AddCommand(genWalletCmd)
// Here you will define your flags and configuration settings.
// Cobra supports Persistent Flags which will work for this command
// and all subcommands, e.g.:
// genWalletCmd.PersistentFlags().String("foo", "", "A help for foo")
// Cobra supports local flags which will only run when this command
// is called directly, e.g.:
// genWalletCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
}