github.com/theQRL/go-zond@v0.1.1/accounts/keystore/keystore.go (about) 1 // Copyright 2017 The go-ethereum Authors 2 // This file is part of the go-ethereum library. 3 // 4 // The go-ethereum library is free software: you can redistribute it and/or modify 5 // it under the terms of the GNU Lesser General Public License as published by 6 // the Free Software Foundation, either version 3 of the License, or 7 // (at your option) any later version. 8 // 9 // The go-ethereum library is distributed in the hope that it will be useful, 10 // but WITHOUT ANY WARRANTY; without even the implied warranty of 11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 // GNU Lesser General Public License for more details. 13 // 14 // You should have received a copy of the GNU Lesser General Public License 15 // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>. 16 17 // Package keystore implements encrypted storage of secp256k1 private keys. 18 // 19 // Keys are stored as encrypted JSON files according to the Web3 Secret Storage specification. 20 // See https://github.com/ethereum/wiki/wiki/Web3-Secret-Storage-Definition for more information. 21 package keystore 22 23 import ( 24 "errors" 25 "math/big" 26 "os" 27 "path/filepath" 28 "reflect" 29 "runtime" 30 "sync" 31 "time" 32 33 "github.com/theQRL/go-qrllib/dilithium" 34 "github.com/theQRL/go-zond/accounts" 35 "github.com/theQRL/go-zond/common" 36 "github.com/theQRL/go-zond/core/types" 37 "github.com/theQRL/go-zond/event" 38 "github.com/theQRL/go-zond/pqcrypto" 39 ) 40 41 var ( 42 ErrLocked = accounts.NewAuthNeededError("password or unlock") 43 ErrNoMatch = errors.New("no key for given address or file") 44 ErrDecrypt = errors.New("could not decrypt key with given password") 45 46 // ErrAccountAlreadyExists is returned if an account attempted to import is 47 // already present in the keystore. 48 ErrAccountAlreadyExists = errors.New("account already exists") 49 ) 50 51 // KeyStoreType is the reflect type of a keystore backend. 52 var KeyStoreType = reflect.TypeOf(&KeyStore{}) 53 54 // KeyStoreScheme is the protocol scheme prefixing account and wallet URLs. 55 const KeyStoreScheme = "keystore" 56 57 // Maximum time between wallet refreshes (if filesystem notifications don't work). 58 const walletRefreshCycle = 3 * time.Second 59 60 // KeyStore manages a key storage directory on disk. 61 type KeyStore struct { 62 storage keyStore // Storage backend, might be cleartext or encrypted 63 cache *accountCache // In-memory account cache over the filesystem storage 64 changes chan struct{} // Channel receiving change notifications from the cache 65 unlocked map[common.Address]*unlocked // Currently unlocked account (decrypted private keys) 66 67 wallets []accounts.Wallet // Wallet wrappers around the individual key files 68 updateFeed event.Feed // Event feed to notify wallet additions/removals 69 updateScope event.SubscriptionScope // Subscription scope tracking current live listeners 70 updating bool // Whether the event notification loop is running 71 72 mu sync.RWMutex 73 importMu sync.Mutex // Import Mutex locks the import to prevent two insertions from racing 74 } 75 76 type unlocked struct { 77 *Key 78 abort chan struct{} 79 } 80 81 // NewKeyStore creates a keystore for the given directory. 82 func NewKeyStore(keydir string, scryptN, scryptP int) *KeyStore { 83 keydir, _ = filepath.Abs(keydir) 84 ks := &KeyStore{storage: &keyStorePassphrase{keydir, scryptN, scryptP, false}} 85 ks.init(keydir) 86 return ks 87 } 88 89 // NewPlaintextKeyStore creates a keystore for the given directory. 90 // Deprecated: Use NewKeyStore. 91 func NewPlaintextKeyStore(keydir string) *KeyStore { 92 keydir, _ = filepath.Abs(keydir) 93 ks := &KeyStore{storage: &keyStorePlain{keydir}} 94 ks.init(keydir) 95 return ks 96 } 97 98 func (ks *KeyStore) init(keydir string) { 99 // Lock the mutex since the account cache might call back with events 100 ks.mu.Lock() 101 defer ks.mu.Unlock() 102 103 // Initialize the set of unlocked keys and the account cache 104 ks.unlocked = make(map[common.Address]*unlocked) 105 ks.cache, ks.changes = newAccountCache(keydir) 106 107 // TODO: In order for this finalizer to work, there must be no references 108 // to ks. addressCache doesn't keep a reference but unlocked keys do, 109 // so the finalizer will not trigger until all timed unlocks have expired. 110 runtime.SetFinalizer(ks, func(m *KeyStore) { 111 m.cache.close() 112 }) 113 // Create the initial list of wallets from the cache 114 accs := ks.cache.accounts() 115 ks.wallets = make([]accounts.Wallet, len(accs)) 116 for i := 0; i < len(accs); i++ { 117 ks.wallets[i] = &keystoreWallet{account: accs[i], keystore: ks} 118 } 119 } 120 121 // Wallets implements accounts.Backend, returning all single-key wallets from the 122 // keystore directory. 123 func (ks *KeyStore) Wallets() []accounts.Wallet { 124 // Make sure the list of wallets is in sync with the account cache 125 ks.refreshWallets() 126 127 ks.mu.RLock() 128 defer ks.mu.RUnlock() 129 130 cpy := make([]accounts.Wallet, len(ks.wallets)) 131 copy(cpy, ks.wallets) 132 return cpy 133 } 134 135 // refreshWallets retrieves the current account list and based on that does any 136 // necessary wallet refreshes. 137 func (ks *KeyStore) refreshWallets() { 138 // Retrieve the current list of accounts 139 ks.mu.Lock() 140 accs := ks.cache.accounts() 141 142 // Transform the current list of wallets into the new one 143 var ( 144 wallets = make([]accounts.Wallet, 0, len(accs)) 145 events []accounts.WalletEvent 146 ) 147 148 for _, account := range accs { 149 // Drop wallets while they were in front of the next account 150 for len(ks.wallets) > 0 && ks.wallets[0].URL().Cmp(account.URL) < 0 { 151 events = append(events, accounts.WalletEvent{Wallet: ks.wallets[0], Kind: accounts.WalletDropped}) 152 ks.wallets = ks.wallets[1:] 153 } 154 // If there are no more wallets or the account is before the next, wrap new wallet 155 if len(ks.wallets) == 0 || ks.wallets[0].URL().Cmp(account.URL) > 0 { 156 wallet := &keystoreWallet{account: account, keystore: ks} 157 158 events = append(events, accounts.WalletEvent{Wallet: wallet, Kind: accounts.WalletArrived}) 159 wallets = append(wallets, wallet) 160 continue 161 } 162 // If the account is the same as the first wallet, keep it 163 if ks.wallets[0].Accounts()[0] == account { 164 wallets = append(wallets, ks.wallets[0]) 165 ks.wallets = ks.wallets[1:] 166 continue 167 } 168 } 169 // Drop any leftover wallets and set the new batch 170 for _, wallet := range ks.wallets { 171 events = append(events, accounts.WalletEvent{Wallet: wallet, Kind: accounts.WalletDropped}) 172 } 173 ks.wallets = wallets 174 ks.mu.Unlock() 175 176 // Fire all wallet events and return 177 for _, event := range events { 178 ks.updateFeed.Send(event) 179 } 180 } 181 182 // Subscribe implements accounts.Backend, creating an async subscription to 183 // receive notifications on the addition or removal of keystore wallets. 184 func (ks *KeyStore) Subscribe(sink chan<- accounts.WalletEvent) event.Subscription { 185 // We need the mutex to reliably start/stop the update loop 186 ks.mu.Lock() 187 defer ks.mu.Unlock() 188 189 // Subscribe the caller and track the subscriber count 190 sub := ks.updateScope.Track(ks.updateFeed.Subscribe(sink)) 191 192 // Subscribers require an active notification loop, start it 193 if !ks.updating { 194 ks.updating = true 195 go ks.updater() 196 } 197 return sub 198 } 199 200 // updater is responsible for maintaining an up-to-date list of wallets stored in 201 // the keystore, and for firing wallet addition/removal events. It listens for 202 // account change events from the underlying account cache, and also periodically 203 // forces a manual refresh (only triggers for systems where the filesystem notifier 204 // is not running). 205 func (ks *KeyStore) updater() { 206 for { 207 // Wait for an account update or a refresh timeout 208 select { 209 case <-ks.changes: 210 case <-time.After(walletRefreshCycle): 211 } 212 // Run the wallet refresher 213 ks.refreshWallets() 214 215 // If all our subscribers left, stop the updater 216 ks.mu.Lock() 217 if ks.updateScope.Count() == 0 { 218 ks.updating = false 219 ks.mu.Unlock() 220 return 221 } 222 ks.mu.Unlock() 223 } 224 } 225 226 // HasAddress reports whether a key with the given address is present. 227 func (ks *KeyStore) HasAddress(addr common.Address) bool { 228 return ks.cache.hasAddress(addr) 229 } 230 231 // Accounts returns all key files present in the directory. 232 func (ks *KeyStore) Accounts() []accounts.Account { 233 return ks.cache.accounts() 234 } 235 236 // Delete deletes the key matched by account if the passphrase is correct. 237 // If the account contains no filename, the address must match a unique key. 238 func (ks *KeyStore) Delete(a accounts.Account, passphrase string) error { 239 // Decrypting the key isn't really necessary, but we do 240 // it anyway to check the password and zero out the key 241 // immediately afterwards. 242 a, key, err := ks.getDecryptedKey(a, passphrase) 243 if key != nil { 244 key.Dilithium = nil 245 } 246 if err != nil { 247 return err 248 } 249 // The order is crucial here. The key is dropped from the 250 // cache after the file is gone so that a reload happening in 251 // between won't insert it into the cache again. 252 err = os.Remove(a.URL.Path) 253 if err == nil { 254 ks.cache.delete(a) 255 ks.refreshWallets() 256 } 257 return err 258 } 259 260 // SignHash returns the Dilithium signature for the given hash. 261 func (ks *KeyStore) SignHash(a accounts.Account, hash []byte) ([]byte, error) { 262 // Look up the key to sign with and abort if it cannot be found 263 ks.mu.RLock() 264 defer ks.mu.RUnlock() 265 266 unlockedKey, found := ks.unlocked[a.Address] 267 if !found { 268 return nil, ErrLocked 269 } 270 271 signature, err := unlockedKey.Dilithium.Sign(hash) 272 273 return signature[:], err 274 } 275 276 func (ks *KeyStore) GetPublicKey(a accounts.Account) ([]byte, error) { 277 // Look up the key to sign with and abort if it cannot be found 278 ks.mu.RLock() 279 defer ks.mu.RUnlock() 280 281 unlockedKey, found := ks.unlocked[a.Address] 282 if !found { 283 return nil, ErrLocked 284 } 285 286 pk := unlockedKey.Dilithium.GetPK() 287 288 return pk[:], nil 289 } 290 291 // SignTx signs the given transaction with the requested account. 292 func (ks *KeyStore) SignTx(a accounts.Account, tx *types.Transaction, chainID *big.Int) (*types.Transaction, error) { 293 // Look up the key to sign with and abort if it cannot be found 294 ks.mu.RLock() 295 defer ks.mu.RUnlock() 296 297 unlockedKey, found := ks.unlocked[a.Address] 298 if !found { 299 return nil, ErrLocked 300 } 301 // Depending on the presence of the chain ID, sign with 2718 or homestead 302 signer := types.LatestSignerForChainID(chainID) 303 return types.SignTx(tx, signer, unlockedKey.Dilithium) 304 } 305 306 // SignHashWithPassphrase signs hash if the private key matching the given address 307 // can be decrypted with the given passphrase. The produced signature is in the 308 // [R || S || V] format where V is 0 or 1. 309 func (ks *KeyStore) SignHashWithPassphrase(a accounts.Account, passphrase string, hash []byte) (signature []byte, err error) { 310 _, key, err := ks.getDecryptedKey(a, passphrase) 311 if err != nil { 312 return nil, err 313 } 314 defer zeroKey(&key.Dilithium) 315 return pqcrypto.Sign(hash, key.Dilithium) 316 } 317 318 // SignTxWithPassphrase signs the transaction if the private key matching the 319 // given address can be decrypted with the given passphrase. 320 func (ks *KeyStore) SignTxWithPassphrase(a accounts.Account, passphrase string, tx *types.Transaction, chainID *big.Int) (*types.Transaction, error) { 321 _, key, err := ks.getDecryptedKey(a, passphrase) 322 if err != nil { 323 return nil, err 324 } 325 defer zeroKey(&key.Dilithium) 326 // Depending on the presence of the chain ID, sign with or without replay protection. 327 signer := types.LatestSignerForChainID(chainID) 328 return types.SignTx(tx, signer, key.Dilithium) 329 } 330 331 // Unlock unlocks the given account indefinitely. 332 func (ks *KeyStore) Unlock(a accounts.Account, passphrase string) error { 333 return ks.TimedUnlock(a, passphrase, 0) 334 } 335 336 // Lock removes the private key with the given address from memory. 337 func (ks *KeyStore) Lock(addr common.Address) error { 338 ks.mu.Lock() 339 if unl, found := ks.unlocked[addr]; found { 340 ks.mu.Unlock() 341 ks.expire(addr, unl, time.Duration(0)*time.Nanosecond) 342 } else { 343 ks.mu.Unlock() 344 } 345 return nil 346 } 347 348 // TimedUnlock unlocks the given account with the passphrase. The account 349 // stays unlocked for the duration of timeout. A timeout of 0 unlocks the account 350 // until the program exits. The account must match a unique key file. 351 // 352 // If the account address is already unlocked for a duration, TimedUnlock extends or 353 // shortens the active unlock timeout. If the address was previously unlocked 354 // indefinitely the timeout is not altered. 355 func (ks *KeyStore) TimedUnlock(a accounts.Account, passphrase string, timeout time.Duration) error { 356 a, key, err := ks.getDecryptedKey(a, passphrase) 357 if err != nil { 358 return err 359 } 360 361 ks.mu.Lock() 362 defer ks.mu.Unlock() 363 u, found := ks.unlocked[a.Address] 364 if found { 365 if u.abort == nil { 366 // The address was unlocked indefinitely, so unlocking 367 // it with a timeout would be confusing. 368 zeroKey(&key.Dilithium) 369 return nil 370 } 371 // Terminate the expire goroutine and replace it below. 372 close(u.abort) 373 } 374 if timeout > 0 { 375 u = &unlocked{Key: key, abort: make(chan struct{})} 376 go ks.expire(a.Address, u, timeout) 377 } else { 378 u = &unlocked{Key: key} 379 } 380 ks.unlocked[a.Address] = u 381 return nil 382 } 383 384 // Find resolves the given account into a unique entry in the keystore. 385 func (ks *KeyStore) Find(a accounts.Account) (accounts.Account, error) { 386 ks.cache.maybeReload() 387 ks.cache.mu.Lock() 388 a, err := ks.cache.find(a) 389 ks.cache.mu.Unlock() 390 return a, err 391 } 392 393 func (ks *KeyStore) getDecryptedKey(a accounts.Account, auth string) (accounts.Account, *Key, error) { 394 a, err := ks.Find(a) 395 if err != nil { 396 return a, nil, err 397 } 398 key, err := ks.storage.GetKey(a.Address, a.URL.Path, auth) 399 return a, key, err 400 } 401 402 func (ks *KeyStore) expire(addr common.Address, u *unlocked, timeout time.Duration) { 403 t := time.NewTimer(timeout) 404 defer t.Stop() 405 select { 406 case <-u.abort: 407 // just quit 408 case <-t.C: 409 ks.mu.Lock() 410 // only drop if it's still the same key instance that dropLater 411 // was launched with. we can check that using pointer equality 412 // because the map stores a new pointer every time the key is 413 // unlocked. 414 if ks.unlocked[addr] == u { 415 zeroKey(&u.Dilithium) 416 delete(ks.unlocked, addr) 417 } 418 ks.mu.Unlock() 419 } 420 } 421 422 // NewAccount generates a new key and stores it into the key directory, 423 // encrypting it with the passphrase. 424 func (ks *KeyStore) NewAccount(passphrase string) (accounts.Account, error) { 425 _, account, err := storeNewKey(ks.storage, passphrase) 426 if err != nil { 427 return accounts.Account{}, err 428 } 429 // Add the account to the cache immediately rather 430 // than waiting for file system notifications to pick it up. 431 ks.cache.add(account) 432 ks.refreshWallets() 433 return account, nil 434 } 435 436 // Export exports as a JSON key, encrypted with newPassphrase. 437 func (ks *KeyStore) Export(a accounts.Account, passphrase, newPassphrase string) (keyJSON []byte, err error) { 438 _, key, err := ks.getDecryptedKey(a, passphrase) 439 if err != nil { 440 return nil, err 441 } 442 var N, P int 443 if store, ok := ks.storage.(*keyStorePassphrase); ok { 444 N, P = store.scryptN, store.scryptP 445 } else { 446 N, P = StandardScryptN, StandardScryptP 447 } 448 return EncryptKey(key, newPassphrase, N, P) 449 } 450 451 // Import stores the given encrypted JSON key into the key directory. 452 func (ks *KeyStore) Import(keyJSON []byte, passphrase, newPassphrase string) (accounts.Account, error) { 453 key, err := DecryptKey(keyJSON, passphrase) 454 if key != nil && key.Dilithium != nil { 455 defer zeroKey(&key.Dilithium) 456 } 457 if err != nil { 458 return accounts.Account{}, err 459 } 460 ks.importMu.Lock() 461 defer ks.importMu.Unlock() 462 463 if ks.cache.hasAddress(key.Address) { 464 return accounts.Account{ 465 Address: key.Address, 466 }, ErrAccountAlreadyExists 467 } 468 return ks.importKey(key, newPassphrase) 469 } 470 471 // ImportDilithium stores the given key into the key directory, encrypting it with the passphrase. 472 func (ks *KeyStore) ImportDilithium(d *dilithium.Dilithium, passphrase string) (accounts.Account, error) { 473 ks.importMu.Lock() 474 defer ks.importMu.Unlock() 475 476 key := newKeyFromDilithium(d) 477 if ks.cache.hasAddress(key.Address) { 478 return accounts.Account{ 479 Address: key.Address, 480 }, ErrAccountAlreadyExists 481 } 482 return ks.importKey(key, passphrase) 483 } 484 485 func (ks *KeyStore) importKey(key *Key, passphrase string) (accounts.Account, error) { 486 a := accounts.Account{Address: key.Address, URL: accounts.URL{Scheme: KeyStoreScheme, Path: ks.storage.JoinPath(keyFileName(key.Address))}} 487 if err := ks.storage.StoreKey(a.URL.Path, key, passphrase); err != nil { 488 return accounts.Account{}, err 489 } 490 ks.cache.add(a) 491 ks.refreshWallets() 492 return a, nil 493 } 494 495 // Update changes the passphrase of an existing account. 496 func (ks *KeyStore) Update(a accounts.Account, passphrase, newPassphrase string) error { 497 a, key, err := ks.getDecryptedKey(a, passphrase) 498 if err != nil { 499 return err 500 } 501 return ks.storage.StoreKey(a.URL.Path, key, newPassphrase) 502 } 503 504 // ImportPreSaleKey decrypts the given Ethereum presale wallet and stores 505 // a key file in the key directory. The key file is encrypted with the same passphrase. 506 func (ks *KeyStore) ImportPreSaleKey(keyJSON []byte, passphrase string) (accounts.Account, error) { 507 a, _, err := importPreSaleKey(ks.storage, keyJSON, passphrase) 508 if err != nil { 509 return a, err 510 } 511 ks.cache.add(a) 512 ks.refreshWallets() 513 return a, nil 514 } 515 516 // isUpdating returns whether the event notification loop is running. 517 // This method is mainly meant for tests. 518 func (ks *KeyStore) isUpdating() bool { 519 ks.mu.RLock() 520 defer ks.mu.RUnlock() 521 return ks.updating 522 } 523 524 // zeroKey nil to dilithium key in memory. 525 func zeroKey(k **dilithium.Dilithium) { 526 *k = nil 527 }