-
Notifications
You must be signed in to change notification settings - Fork 1
/
importexportldb.go
70 lines (58 loc) · 1.52 KB
/
importexportldb.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
// Copyright 2016 Factom Foundation
// Use of this source code is governed by the MIT
// license that can be found in the LICENSE file.
package wallet
import (
"fmt"
"os"
"github.com/FactomProject/factom"
"github.com/FactomProject/factomd/common/factoid"
)
// ImportWalletFromMnemonic creates a new wallet with a provided Mnemonic seed
// defined in bip-0039.
func ImportLDBWalletFromMnemonic(mnemonic, path string) (*Wallet, error) {
mnemonic, err := factom.ParseMnemonic(mnemonic)
if err != nil {
return nil, err
}
// check if the file exists
_, err = os.Stat(path)
if err == nil {
return nil, fmt.Errorf("%s: file already exists", path)
}
db, err := NewLevelDB(path)
if err != nil {
return nil, err
}
seed := new(DBSeed)
seed.MnemonicSeed = mnemonic
if err := db.InsertDBSeed(seed); err != nil {
return nil, err
}
w := new(Wallet)
w.transactions = make(map[string]*factoid.Transaction)
w.WalletDatabaseOverlay = db
return w, nil
}
// ExportLDBWallet writes all the secret/publilc key pairs from a wallet and the
// wallet seed in a pritable format.
func ExportLDBWallet(path string) (string, []*factom.FactoidAddress, []*factom.ECAddress, error) {
// check if the file exists
_, err := os.Stat(path)
if err != nil {
return "", nil, nil, err
}
w, err := NewOrOpenLevelDBWallet(path)
if err != nil {
return "", nil, nil, err
}
m, err := w.GetSeed()
if err != nil {
return "", nil, nil, err
}
fs, es, err := w.GetAllAddresses()
if err != nil {
return "", nil, nil, err
}
return m, fs, es, nil
}