-
Notifications
You must be signed in to change notification settings - Fork 10
/
create.go
93 lines (77 loc) · 2.62 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
package main
import (
"bufio"
"fmt"
"os"
"github.com/sedracoin/sedrad/cmd/sedrawallet/libsedrawallet"
"github.com/sedracoin/sedrad/cmd/sedrawallet/libsedrawallet/bip32"
"github.com/sedracoin/sedrad/cmd/sedrawallet/utils"
"github.com/pkg/errors"
"github.com/sedracoin/sedrad/cmd/sedrawallet/keys"
)
func create(conf *createConfig) error {
var encryptedMnemonics []*keys.EncryptedMnemonic
var signerExtendedPublicKeys []string
var err error
isMultisig := conf.NumPublicKeys > 1
if !conf.Import {
encryptedMnemonics, signerExtendedPublicKeys, err = keys.CreateMnemonics(conf.NetParams(), conf.NumPrivateKeys, conf.Password, isMultisig)
} else {
encryptedMnemonics, signerExtendedPublicKeys, err = keys.ImportMnemonics(conf.NetParams(), conf.NumPrivateKeys, conf.Password, isMultisig)
}
if err != nil {
return err
}
for i, extendedPublicKey := range signerExtendedPublicKeys {
fmt.Printf("Extended public key of mnemonic #%d:\n%s\n\n", i+1, extendedPublicKey)
}
fmt.Printf("Notice the above is neither a secret key to your wallet " +
"(use \"sedrawallet dump-unencrypted-data\" to see a secret seed phrase) " +
"nor a wallet public address (use \"sedrawallet new-address\" to create and see one)\n\n")
extendedPublicKeys := make([]string, conf.NumPrivateKeys, conf.NumPublicKeys)
copy(extendedPublicKeys, signerExtendedPublicKeys)
reader := bufio.NewReader(os.Stdin)
for i := conf.NumPrivateKeys; i < conf.NumPublicKeys; i++ {
fmt.Printf("Enter public key #%d here:\n", i+1)
extendedPublicKey, err := utils.ReadLine(reader)
if err != nil {
return err
}
_, err = bip32.DeserializeExtendedKey(string(extendedPublicKey))
if err != nil {
return errors.Wrapf(err, "%s is invalid extended public key", string(extendedPublicKey))
}
fmt.Println()
extendedPublicKeys = append(extendedPublicKeys, string(extendedPublicKey))
}
// For a read only wallet the cosigner index is 0
cosignerIndex := uint32(0)
if len(signerExtendedPublicKeys) > 0 {
cosignerIndex, err = libsedrawallet.MinimumCosignerIndex(signerExtendedPublicKeys, extendedPublicKeys)
if err != nil {
return err
}
}
file := keys.File{
Version: keys.LastVersion,
EncryptedMnemonics: encryptedMnemonics,
ExtendedPublicKeys: extendedPublicKeys,
MinimumSignatures: conf.MinimumSignatures,
CosignerIndex: cosignerIndex,
ECDSA: conf.ECDSA,
}
err = file.SetPath(conf.NetParams(), conf.KeysFile, conf.Yes)
if err != nil {
return err
}
err = file.TryLock()
if err != nil {
return err
}
err = file.Save()
if err != nil {
return err
}
fmt.Printf("Wrote the keys into %s\n", file.Path())
return nil
}