/
create.go
104 lines (89 loc) · 2.25 KB
/
create.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
package wallets
import (
"context"
"encoding/hex"
"fmt"
"github.com/brave-intl/bat-go/cmd"
"github.com/brave-intl/bat-go/utils/altcurrency"
appctx "github.com/brave-intl/bat-go/utils/context"
"github.com/brave-intl/bat-go/utils/httpsignature"
"github.com/brave-intl/bat-go/utils/logging"
"github.com/brave-intl/bat-go/utils/wallet"
"github.com/brave-intl/bat-go/utils/wallet/provider/uphold"
"github.com/spf13/cobra"
)
var (
// CreateCmd creates a wallet on uphold
CreateCmd = &cobra.Command{
Use: "create",
Short: "creates a wallet",
Run: cmd.Perform("create", Create),
}
)
func init() {
WalletsCmd.AddCommand(CreateCmd)
createBuilder := cmd.NewFlagBuilder(CreateCmd)
// name - the name of the new wallet
createBuilder.Flag().String("name", "",
"the name for the wallet").
Bind("name")
// provider - the provider to target
createBuilder.Flag().String("provider", "",
"the provider for the wallet").
Bind("provider")
}
// Create creates a wallet
func Create(cmd *cobra.Command, args []string) error {
provider, err := cmd.Flags().GetString("provider")
fmt.Println("provider", err)
if err != nil {
return err
}
switch provider {
case "uphold":
name, err := cmd.Flags().GetString("name")
if err != nil {
return err
}
return CreateOnUphold(
cmd.Context(),
name,
)
}
return nil
}
// CreateOnUphold creates a wallet on uphold
func CreateOnUphold(ctx context.Context, name string) error {
logger, lerr := appctx.GetLogger(ctx)
if lerr != nil {
_, logger = logging.SetupLogger(ctx)
}
publicKey, privateKey, err := httpsignature.GenerateEd25519Key(nil)
if err != nil {
return err
}
publicKeyHex := hex.EncodeToString([]byte(publicKey))
privateKeyHex := hex.EncodeToString([]byte(privateKey))
logger.Info().
Str("public_key", publicKeyHex).
Str("private_key", privateKeyHex).
Str("name", name).
Msg("key created")
var info wallet.Info
info.Provider = "uphold"
info.ProviderID = ""
{
tmp := altcurrency.BAT
info.AltCurrency = &tmp
}
info.PublicKey = publicKeyHex
wallet := &uphold.Wallet{Info: info, PrivKey: privateKey, PubKey: publicKey}
err = wallet.Register(name)
if err != nil {
return err
}
logger.Info().
Str("provider_id", wallet.Info.ProviderID).
Msg("Uphold card ID")
return nil
}