|
| 1 | +// Copyright 2016 Factom Foundation |
| 2 | +// Use of this source code is governed by the MIT |
| 3 | +// license that can be found in the LICENSE file. |
| 4 | + |
| 5 | +package wallet |
| 6 | + |
| 7 | +import ( |
| 8 | + "fmt" |
| 9 | + "sync" |
| 10 | + |
| 11 | + "github.com/FactomProject/factom" |
| 12 | + "github.com/FactomProject/factomd/common/factoid" |
| 13 | +) |
| 14 | + |
| 15 | +// Wallet is a connection to a Factom Wallet Database |
| 16 | +type Wallet struct { |
| 17 | + *WalletDatabaseOverlay |
| 18 | + Encrypted bool |
| 19 | + DBPath string |
| 20 | + txlock sync.Mutex |
| 21 | + transactions map[string]*factoid.Transaction |
| 22 | + txdb *TXDatabaseOverlay |
| 23 | +} |
| 24 | + |
| 25 | +func (w *Wallet) InitWallet() error { |
| 26 | + dbSeed, err := w.GetOrCreateDBSeed() |
| 27 | + if err != nil { |
| 28 | + return err |
| 29 | + } |
| 30 | + if dbSeed == nil { |
| 31 | + return fmt.Errorf("dbSeed not present in DB") |
| 32 | + } |
| 33 | + return nil |
| 34 | +} |
| 35 | + |
| 36 | +func NewOrOpenLevelDBWallet(path string) (*Wallet, error) { |
| 37 | + w := new(Wallet) |
| 38 | + w.transactions = make(map[string]*factoid.Transaction) |
| 39 | + |
| 40 | + db, err := NewLevelDB(path) |
| 41 | + if err != nil { |
| 42 | + return nil, err |
| 43 | + } |
| 44 | + w.WalletDatabaseOverlay = db |
| 45 | + |
| 46 | + if err = w.InitWallet(); err != nil { |
| 47 | + return nil, err |
| 48 | + } |
| 49 | + |
| 50 | + return w, nil |
| 51 | +} |
| 52 | + |
| 53 | +func NewOrOpenBoltDBWallet(path string) (*Wallet, error) { |
| 54 | + w := new(Wallet) |
| 55 | + w.transactions = make(map[string]*factoid.Transaction) |
| 56 | + |
| 57 | + db, err := NewBoltDB(path) |
| 58 | + if err != nil { |
| 59 | + return nil, err |
| 60 | + } |
| 61 | + w.WalletDatabaseOverlay = db |
| 62 | + |
| 63 | + if err = w.InitWallet(); err != nil { |
| 64 | + return nil, err |
| 65 | + } |
| 66 | + |
| 67 | + return w, nil |
| 68 | +} |
| 69 | + |
| 70 | +func NewEncryptedBoltDBWallet(path, password string) (*Wallet, error) { |
| 71 | + w := new(Wallet) |
| 72 | + w.transactions = make(map[string]*factoid.Transaction) |
| 73 | + |
| 74 | + db, err := NewEncryptedBoltDB(path, password) |
| 75 | + if err != nil { |
| 76 | + return nil, err |
| 77 | + } |
| 78 | + w.WalletDatabaseOverlay = db |
| 79 | + |
| 80 | + if err = w.InitWallet(); err != nil { |
| 81 | + return nil, err |
| 82 | + } |
| 83 | + |
| 84 | + return w, nil |
| 85 | +} |
| 86 | + |
| 87 | +func NewEncryptedBoltDBWalletAwaitingPassphrase(path string) (*Wallet, error) { |
| 88 | + w := new(Wallet) |
| 89 | + w.transactions = make(map[string]*factoid.Transaction) |
| 90 | + w.Encrypted = true |
| 91 | + w.DBPath = path |
| 92 | + return w, nil |
| 93 | +} |
| 94 | + |
| 95 | +func NewMapDBWallet() (*Wallet, error) { |
| 96 | + w := new(Wallet) |
| 97 | + w.transactions = make(map[string]*factoid.Transaction) |
| 98 | + w.WalletDatabaseOverlay = NewMapDB() |
| 99 | + |
| 100 | + if err := w.InitWallet(); err != nil { |
| 101 | + return nil, err |
| 102 | + } |
| 103 | + |
| 104 | + return w, nil |
| 105 | +} |
| 106 | + |
| 107 | +// Close closes a Factom Wallet Database |
| 108 | +func (w *Wallet) Close() error { |
| 109 | + if w.WalletDatabaseOverlay == nil { |
| 110 | + return nil |
| 111 | + } |
| 112 | + return w.DBO.Close() |
| 113 | +} |
| 114 | + |
| 115 | +// AddTXDB allows the wallet api to read from a local transaction cashe. |
| 116 | +func (w *Wallet) AddTXDB(t *TXDatabaseOverlay) { |
| 117 | + w.txdb = t |
| 118 | +} |
| 119 | + |
| 120 | +// TXDB returns a handle for the Transaction Database. |
| 121 | +func (w *Wallet) TXDB() *TXDatabaseOverlay { |
| 122 | + return w.txdb |
| 123 | +} |
| 124 | + |
| 125 | +// GenerateECAddress creates and stores a new Entry Credit Address in the |
| 126 | +// Wallet. The address can be reproduced in the future using the Wallet Seed. |
| 127 | +func (w *Wallet) GenerateECAddress() (*factom.ECAddress, error) { |
| 128 | + return w.GetNextECAddress() |
| 129 | +} |
| 130 | + |
| 131 | +// GenerateFCTAddress creates and stores a new Factoid Address in the Wallet. |
| 132 | +// The address can be reproduced in the future using the Wallet Seed. |
| 133 | +func (w *Wallet) GenerateFCTAddress() (*factom.FactoidAddress, error) { |
| 134 | + return w.GetNextFCTAddress() |
| 135 | +} |
| 136 | + |
| 137 | +// GenerateIdentityKey creates and stores a new Identity Key in the Wallet. |
| 138 | +func (w *Wallet) GenerateIdentityKey() (*factom.IdentityKey, error) { |
| 139 | + return w.GetNextIdentityKey() |
| 140 | +} |
| 141 | + |
| 142 | +// GetAllAddresses retrieves all Entry Credit and Factoid Addresses from the |
| 143 | +// Wallet Database. |
| 144 | +func (w *Wallet) GetAllAddresses() ([]*factom.FactoidAddress, []*factom.ECAddress, error) { |
| 145 | + fcs, err := w.GetAllFCTAddresses() |
| 146 | + if err != nil { |
| 147 | + return nil, nil, err |
| 148 | + } |
| 149 | + |
| 150 | + ecs, err := w.GetAllECAddresses() |
| 151 | + if err != nil { |
| 152 | + return nil, nil, err |
| 153 | + } |
| 154 | + |
| 155 | + return fcs, ecs, nil |
| 156 | +} |
| 157 | + |
| 158 | +// GetSeed returns the string representaion of the Wallet Seed. The Wallet Seed |
| 159 | +// can be used to regenerate the Factoid and Entry Credit Addresses previously |
| 160 | +// generated by the wallet. Note that Addresses that are imported into the |
| 161 | +// Wallet cannot be regenerated using the Wallet Seed. |
| 162 | +func (w *Wallet) GetSeed() (string, error) { |
| 163 | + seed, err := w.GetDBSeed() |
| 164 | + if err != nil { |
| 165 | + return "", err |
| 166 | + } |
| 167 | + |
| 168 | + return seed.MnemonicSeed, nil |
| 169 | +} |
| 170 | + |
| 171 | +func (w *Wallet) GetVersion() string { |
| 172 | + return WalletVersion |
| 173 | +} |
| 174 | + |
| 175 | +func (w *Wallet) GetApiVersion() string { |
| 176 | + return ApiVersion |
| 177 | +} |
0 commit comments