-
Notifications
You must be signed in to change notification settings - Fork 1
/
v1conversion.go
154 lines (138 loc) · 3.8 KB
/
v1conversion.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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
// 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"
"github.com/FactomProject/factoid"
"github.com/FactomProject/factoid/state/stateinit"
"github.com/FactomProject/factoid/wallet"
"github.com/FactomProject/factom"
)
// This file is a dirty hack to to get the keys out of a version 1 wallet.
// ImportV1Wallet takes a version 1 wallet bolt.db file and imports all of its
// addresses into a factom wallet.
func ImportV1Wallet(v1path, v2path string) (*Wallet, error) {
w, err := NewOrOpenBoltDBWallet(v2path)
if err != nil {
return nil, err
}
fstate := stateinit.NewFactoidState(v1path)
_, values := fstate.GetDB().GetKeysValues([]byte(factoid.W_NAME))
for _, v := range values {
we, ok := v.(wallet.IWalletEntry)
if !ok {
w.Close()
return nil, fmt.Errorf("Cannot retrieve addresses from version 1 database")
}
switch we.GetType() {
case "fct":
f, err := factom.MakeFactoidAddress(we.GetPrivKey(0)[:32])
if err != nil {
w.Close()
return nil, err
}
if err := w.InsertFCTAddress(f); err != nil {
w.Close()
return nil, err
}
case "ec":
e, err := factom.MakeECAddress(we.GetPrivKey(0)[:32])
if err != nil {
w.Close()
return nil, err
}
if err := w.InsertECAddress(e); err != nil {
w.Close()
return nil, err
}
default:
return nil, fmt.Errorf("version 1 database returned unknown address type %s %#v", we.GetType(), we)
}
}
return w, err
}
// ImportV1WalletToLDB takes a version 1 wallet bolt.db file and imports all of its
// addresses into a factom wallet.
func ImportV1WalletToLDB(v1path, v2path string) (*Wallet, error) {
w, err := NewOrOpenBoltDBWallet(v2path)
if err != nil {
return nil, err
}
fstate := stateinit.NewFactoidState(v1path)
_, values := fstate.GetDB().GetKeysValues([]byte(factoid.W_NAME))
for _, v := range values {
we, ok := v.(wallet.IWalletEntry)
if !ok {
w.Close()
return nil, fmt.Errorf("Cannot retrieve addresses from version 1 database")
}
switch we.GetType() {
case "fct":
f, err := factom.MakeFactoidAddress(we.GetPrivKey(0)[:32])
if err != nil {
w.Close()
return nil, err
}
if err := w.InsertFCTAddress(f); err != nil {
w.Close()
return nil, err
}
case "ec":
e, err := factom.MakeECAddress(we.GetPrivKey(0)[:32])
if err != nil {
w.Close()
return nil, err
}
if err := w.InsertECAddress(e); err != nil {
w.Close()
return nil, err
}
default:
return nil, fmt.Errorf("version 1 database returned unknown address type %s %#v", we.GetType(), we)
}
}
return w, err
}
// ImportV1WalletToEncryptedDB takes a version 1 wallet bolt.db file and imports all of its
// addresses into a factom wallet.
func ImportV1WalletToEncryptedDB(v1path, v2path, password string) (*Wallet, error) {
w, err := NewEncryptedBoltDBWallet(v2path, password)
if err != nil {
return nil, err
}
fstate := stateinit.NewFactoidState(v1path)
_, values := fstate.GetDB().GetKeysValues([]byte(factoid.W_NAME))
for _, v := range values {
we, ok := v.(wallet.IWalletEntry)
if !ok {
w.Close()
return nil, fmt.Errorf("Cannot retrieve addresses from version 1 database")
}
switch we.GetType() {
case "fct":
f, err := factom.MakeFactoidAddress(we.GetPrivKey(0)[:32])
if err != nil {
w.Close()
return nil, err
}
if err := w.InsertFCTAddress(f); err != nil {
w.Close()
return nil, err
}
case "ec":
e, err := factom.MakeECAddress(we.GetPrivKey(0)[:32])
if err != nil {
w.Close()
return nil, err
}
if err := w.InsertECAddress(e); err != nil {
w.Close()
return nil, err
}
default:
return nil, fmt.Errorf("version 1 database returned unknown address type %s %#v", we.GetType(), we)
}
}
return w, err
}