-
Notifications
You must be signed in to change notification settings - Fork 13
/
run-seed.go
72 lines (57 loc) · 1.5 KB
/
run-seed.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
// SPDX-License-Identifier: ISC
// Copyright (c) 2014-2019 Bitmark Inc.
// Use of this source code is governed by an ISC
// license that can be found in the LICENSE file.
package main
import (
"fmt"
"strings"
"github.com/urfave/cli"
"github.com/bitmark-inc/bitmarkd/account"
"github.com/bitmark-inc/bitmarkd/command/bitmark-cli/configuration"
)
// merge the account string to private data
type seedResult struct {
*configuration.Private
Name string `json:"name"`
Account string `json:"account"`
Phrase string `json:"recovery_phrase"`
}
// to decrypt and show the secret data
func runSeed(c *cli.Context) error {
m := c.App.Metadata["config"].(*metadata)
name, owner, err := checkOwnerWithPasswordPrompt(c.GlobalString("identity"), m.config, c)
if nil != err {
return err
}
phrase, err := account.Base58EncodedSeedToPhrase(owner.Seed)
if nil != err {
return err
}
if c.Bool("recovery") {
fmt.Printf("recovery phrase:\n%s", prettyPhrase(phrase))
return nil
}
result := seedResult{
Private: owner,
Name: name,
Account: owner.PrivateKey.Account().String(),
Phrase: strings.Join(phrase, " "),
}
printJson(m.w, result)
return nil
}
// convert slice of string phrase to pretty string format
func prettyPhrase(phrase []string) string {
var builder strings.Builder
for i, p := range phrase {
builder.WriteString(p)
breakline := (i+1)%6 == 0
if breakline {
builder.WriteString("\n")
} else if i < len(phrase)-1 {
builder.WriteString(" ")
}
}
return builder.String()
}