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	"strings"
11
12	"github.com/urfave/cli"
13
14	"github.com/bitmark-inc/bitmarkd/account"
15	"github.com/bitmark-inc/bitmarkd/command/bitmark-cli/configuration"
16)
17
18// merge the account string to private data
19type seedResult struct {
20	*configuration.Private
21	Name    string `json:"name"`
22	Account string `json:"account"`
23	Phrase  string `json:"recovery_phrase"`
24}
25
26// to decrypt and show the secret data
27func runSeed(c *cli.Context) error {
28
29	m := c.App.Metadata["config"].(*metadata)
30
31	name, owner, err := checkOwnerWithPasswordPrompt(c.GlobalString("identity"), m.config, c)
32	if nil != err {
33		return err
34	}
35
36	phrase, err := account.Base58EncodedSeedToPhrase(owner.Seed)
37	if nil != err {
38		return err
39	}
40
41	if c.Bool("recovery") {
42		fmt.Printf("recovery phrase:\n%s", prettyPhrase(phrase))
43		return nil
44	}
45
46	result := seedResult{
47		Private: owner,
48		Name:    name,
49		Account: owner.PrivateKey.Account().String(),
50		Phrase:  strings.Join(phrase, " "),
51	}
52
53	printJson(m.w, result)
54	return nil
55}
56
57// convert slice of string phrase to pretty string format
58func prettyPhrase(phrase []string) string {
59	var builder strings.Builder
60
61	for i, p := range phrase {
62		builder.WriteString(p)
63		breakline := (i+1)%6 == 0
64		if breakline {
65			builder.WriteString("\n")
66		} else if i < len(phrase)-1 {
67			builder.WriteString(" ")
68		}
69	}
70
71	return builder.String()
72}
73