-
Notifications
You must be signed in to change notification settings - Fork 0
/
wallet.go
66 lines (55 loc) · 1.61 KB
/
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
59
60
61
62
63
64
65
66
package cmd
import (
"fmt"
"log"
"os"
"strings"
"github.com/erxonxi/coin/wallet"
"github.com/spf13/cobra"
)
var walletCmd = &cobra.Command{
Use: "wallet",
Short: "Command to manage your wallets.",
Long: `Command to manage your wallets.`,
Run: walletFunc,
}
func init() {
rootCmd.AddCommand(walletCmd)
walletCmd.Flags().BoolVarP((&create), "create", "c", false, "Create new wallet")
walletCmd.Flags().StringVarP((&address), "address", "a", "", "Address to get information about")
}
func walletFunc(cmd *cobra.Command, args []string) {
defer os.Exit(0)
nodeID := os.Getenv("NODE_ID")
if nodeID == "" {
log.Panic("Please provide a NODE_ID")
}
if create == true {
wallets, _ := wallet.CreateWallets(nodeID)
address := wallets.AddWallet()
wallets.SaveFile(nodeID)
fmt.Printf("New address is: %s\n", address)
} else {
if address != "" {
wallets, _ := wallet.InitializeWallets(nodeID)
if !wallet.ValidateAddress(address) {
log.Panic("Invalid address")
}
w := wallets.GetWallet(address)
PrintWalletAddress(address, w)
} else {
wallets, _ := wallet.CreateWallets(nodeID)
addresses := wallets.GetAllAddresses()
for _, address := range addresses {
fmt.Println(address)
}
}
}
}
func PrintWalletAddress(address string, w wallet.Wallet) {
var lines []string
lines = append(lines, fmt.Sprintf("======ADDRESS:======\n %s ", address))
lines = append(lines, fmt.Sprintf("======PUBLIC KEY:======\n %x", w.PublicKey))
lines = append(lines, fmt.Sprintf("======PRIVATE KEY:======\n %x", wallet.DecodePriveteKey(w.PrivateKey).D.Bytes()))
fmt.Println(strings.Join(lines, "\n"))
}