1// SPDX-License-Identifier: ISC
2// Copyright (c) 2014-2020 Bitmark Inc.
3// Use of this source code is governed by an ISC
4// license that can be found in the LICENSE file.
5
6package main
7
8import (
9	"fmt"
10
11	"github.com/urfave/cli"
12
13	"github.com/bitmark-inc/bitmarkd/fault"
14)
15
16func runAdd(c *cli.Context) error {
17
18	m := c.App.Metadata["config"].(*metadata)
19
20	name, err := checkName(c.GlobalString("identity"))
21	if nil != err {
22		return err
23	}
24
25	description, err := checkDescription(c.String("description"))
26	if nil != err {
27		return err
28	}
29
30	// blank or a valid seed
31	seed := c.String("seed")
32	new := c.Bool("new")
33	acc := c.String("account")
34
35	if m.verbose {
36		fmt.Fprintf(m.e, "identity: %s\n", name)
37		fmt.Fprintf(m.e, "description: %s\n", description)
38		fmt.Fprintf(m.e, "seed: %s\n", seed)
39		fmt.Fprintf(m.e, "account: %s\n", acc)
40		fmt.Fprintf(m.e, "new: %t\n", new)
41	}
42
43	if "" == acc {
44		seed, err = checkSeed(seed, new, m.testnet)
45		if nil != err {
46			return err
47		}
48
49		password := c.GlobalString("password")
50		if "" == password {
51			password, err = promptNewPassword()
52			if nil != err {
53				return err
54			}
55		}
56
57		err = m.config.AddIdentity(name, description, seed, password)
58		if nil != err {
59			return err
60		}
61
62	} else if "" == seed && "" != acc && !new {
63		err = m.config.AddReceiveOnlyIdentity(name, description, acc)
64		if nil != err {
65			return err
66		}
67
68	} else {
69		return fault.IncompatibleOptions
70	}
71
72	// require configuration update
73	m.save = true
74	return nil
75}
76