account update: migrate or change password
* account.Update * KeyStore.Cleanup * fix dir rm for old format deleteKey
This commit is contained in:
@@ -72,16 +72,19 @@ func (ks keyStorePassphrase) GenerateNewKey(rand io.Reader, auth string) (key *K
|
||||
}
|
||||
|
||||
func (ks keyStorePassphrase) GetKey(keyAddr common.Address, auth string) (key *Key, err error) {
|
||||
keyBytes, keyId, err := decryptKeyFromFile(ks, keyAddr, auth)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
keyBytes, keyId, err := decryptKeyFromFile(ks.keysDirPath, keyAddr, auth)
|
||||
if err == nil {
|
||||
key = &Key{
|
||||
Id: uuid.UUID(keyId),
|
||||
Address: keyAddr,
|
||||
PrivateKey: ToECDSA(keyBytes),
|
||||
}
|
||||
}
|
||||
key = &Key{
|
||||
Id: uuid.UUID(keyId),
|
||||
Address: keyAddr,
|
||||
PrivateKey: ToECDSA(keyBytes),
|
||||
}
|
||||
return key, err
|
||||
return
|
||||
}
|
||||
|
||||
func (ks keyStorePassphrase) Cleanup(keyAddr common.Address) (err error) {
|
||||
return cleanup(ks.keysDirPath, keyAddr)
|
||||
}
|
||||
|
||||
func (ks keyStorePassphrase) GetKeyAddresses() (addresses []common.Address, err error) {
|
||||
@@ -142,7 +145,7 @@ func (ks keyStorePassphrase) StoreKey(key *Key, auth string) (err error) {
|
||||
|
||||
func (ks keyStorePassphrase) DeleteKey(keyAddr common.Address, auth string) (err error) {
|
||||
// only delete if correct passphrase is given
|
||||
_, _, err = decryptKeyFromFile(ks, keyAddr, auth)
|
||||
_, _, err = decryptKeyFromFile(ks.keysDirPath, keyAddr, auth)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -150,25 +153,25 @@ func (ks keyStorePassphrase) DeleteKey(keyAddr common.Address, auth string) (err
|
||||
return deleteKey(ks.keysDirPath, keyAddr)
|
||||
}
|
||||
|
||||
func decryptKeyFromFile(ks keyStorePassphrase, keyAddr common.Address, auth string) (keyBytes []byte, keyId []byte, err error) {
|
||||
func decryptKeyFromFile(keysDirPath string, keyAddr common.Address, auth string) (keyBytes []byte, keyId []byte, err error) {
|
||||
fmt.Printf("%v\n", keyAddr.Hex())
|
||||
m := make(map[string]interface{})
|
||||
err = getKey(ks.keysDirPath, keyAddr, &m)
|
||||
err = getKey(keysDirPath, keyAddr, &m)
|
||||
if err != nil {
|
||||
fmt.Printf("get key error: %v\n", err)
|
||||
return
|
||||
}
|
||||
|
||||
v := reflect.ValueOf(m["version"])
|
||||
if v.Kind() == reflect.String && v.String() == "1" {
|
||||
k := new(encryptedKeyJSONV1)
|
||||
getKey(ks.keysDirPath, keyAddr, &k)
|
||||
err = getKey(keysDirPath, keyAddr, &k)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
return decryptKeyV1(k, auth)
|
||||
} else {
|
||||
k := new(encryptedKeyJSONV3)
|
||||
getKey(ks.keysDirPath, keyAddr, &k)
|
||||
err = getKey(keysDirPath, keyAddr, &k)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
@@ -43,6 +43,7 @@ type KeyStore interface {
|
||||
GetKeyAddresses() ([]common.Address, error) // get all addresses
|
||||
StoreKey(*Key, string) error // store key optionally using auth string
|
||||
DeleteKey(common.Address, string) error // delete key by addr and auth string
|
||||
Cleanup(keyAddr common.Address) (err error)
|
||||
}
|
||||
|
||||
type keyStorePlain struct {
|
||||
@@ -86,6 +87,10 @@ func (ks keyStorePlain) GetKeyAddresses() (addresses []common.Address, err error
|
||||
return getKeyAddresses(ks.keysDirPath)
|
||||
}
|
||||
|
||||
func (ks keyStorePlain) Cleanup(keyAddr common.Address) (err error) {
|
||||
return cleanup(ks.keysDirPath, keyAddr)
|
||||
}
|
||||
|
||||
func (ks keyStorePlain) StoreKey(key *Key, auth string) (err error) {
|
||||
keyJSON, err := json.Marshal(key)
|
||||
if err != nil {
|
||||
@@ -100,10 +105,14 @@ func (ks keyStorePlain) DeleteKey(keyAddr common.Address, auth string) (err erro
|
||||
}
|
||||
|
||||
func deleteKey(keysDirPath string, keyAddr common.Address) (err error) {
|
||||
var keyFilePath string
|
||||
keyFilePath, err = getKeyFilePath(keysDirPath, keyAddr)
|
||||
var path string
|
||||
path, err = getKeyFilePath(keysDirPath, keyAddr)
|
||||
if err == nil {
|
||||
err = os.Remove(keyFilePath)
|
||||
addrHex := hex.EncodeToString(keyAddr[:])
|
||||
if path == filepath.Join(keysDirPath, addrHex, addrHex) {
|
||||
path = filepath.Join(keysDirPath, addrHex)
|
||||
}
|
||||
err = os.RemoveAll(path)
|
||||
}
|
||||
return
|
||||
}
|
||||
@@ -122,6 +131,36 @@ func getKeyFilePath(keysDirPath string, keyAddr common.Address) (keyFilePath str
|
||||
return
|
||||
}
|
||||
|
||||
func cleanup(keysDirPath string, keyAddr common.Address) (err error) {
|
||||
fileInfos, err := ioutil.ReadDir(keysDirPath)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
var paths []string
|
||||
account := hex.EncodeToString(keyAddr[:])
|
||||
for _, fileInfo := range fileInfos {
|
||||
path := filepath.Join(keysDirPath, fileInfo.Name())
|
||||
if len(path) >= 40 {
|
||||
addr := path[len(path)-40 : len(path)]
|
||||
if addr == account {
|
||||
if path == filepath.Join(keysDirPath, addr, addr) {
|
||||
path = filepath.Join(keysDirPath, addr)
|
||||
}
|
||||
paths = append(paths, path)
|
||||
}
|
||||
}
|
||||
}
|
||||
if len(paths) > 1 {
|
||||
for i := 0; err == nil && i < len(paths)-1; i++ {
|
||||
err = os.RemoveAll(paths[i])
|
||||
if err != nil {
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func getKeyFile(keysDirPath string, keyAddr common.Address) (fileContent []byte, err error) {
|
||||
var keyFilePath string
|
||||
keyFilePath, err = getKeyFilePath(keysDirPath, keyAddr)
|
||||
|
Reference in New Issue
Block a user