Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
G
Geth-Modification
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
张蕾
Geth-Modification
Commits
cda91ee1
Commit
cda91ee1
authored
Feb 16, 2016
by
Péter Szilágyi
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
crypto: expose key decryption method to parse a string direclty
parent
66b148dd
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
45 additions
and
36 deletions
+45
-36
key_store_passphrase.go
crypto/key_store_passphrase.go
+37
-26
key_store_plain.go
crypto/key_store_plain.go
+8
-10
No files found.
crypto/key_store_passphrase.go
View file @
cda91ee1
...
@@ -75,15 +75,7 @@ func (ks keyStorePassphrase) GenerateNewKey(rand io.Reader, auth string) (key *K
...
@@ -75,15 +75,7 @@ func (ks keyStorePassphrase) GenerateNewKey(rand io.Reader, auth string) (key *K
}
}
func
(
ks
keyStorePassphrase
)
GetKey
(
keyAddr
common
.
Address
,
auth
string
)
(
key
*
Key
,
err
error
)
{
func
(
ks
keyStorePassphrase
)
GetKey
(
keyAddr
common
.
Address
,
auth
string
)
(
key
*
Key
,
err
error
)
{
keyBytes
,
keyId
,
err
:=
decryptKeyFromFile
(
ks
.
keysDirPath
,
keyAddr
,
auth
)
return
decryptKeyFromFile
(
ks
.
keysDirPath
,
keyAddr
,
auth
)
if
err
==
nil
{
key
=
&
Key
{
Id
:
uuid
.
UUID
(
keyId
),
Address
:
keyAddr
,
PrivateKey
:
ToECDSA
(
keyBytes
),
}
}
return
}
}
func
(
ks
keyStorePassphrase
)
Cleanup
(
keyAddr
common
.
Address
)
(
err
error
)
{
func
(
ks
keyStorePassphrase
)
Cleanup
(
keyAddr
common
.
Address
)
(
err
error
)
{
...
@@ -145,39 +137,58 @@ func (ks keyStorePassphrase) StoreKey(key *Key, auth string) (err error) {
...
@@ -145,39 +137,58 @@ func (ks keyStorePassphrase) StoreKey(key *Key, auth string) (err error) {
return
writeKeyFile
(
key
.
Address
,
ks
.
keysDirPath
,
keyJSON
)
return
writeKeyFile
(
key
.
Address
,
ks
.
keysDirPath
,
keyJSON
)
}
}
func
(
ks
keyStorePassphrase
)
DeleteKey
(
keyAddr
common
.
Address
,
auth
string
)
(
err
error
)
{
func
(
ks
keyStorePassphrase
)
DeleteKey
(
keyAddr
common
.
Address
,
auth
string
)
error
{
// only delete if correct passphrase is given
// only delete if correct passphrase is given
_
,
_
,
err
=
decryptKeyFromFile
(
ks
.
keysDirPath
,
keyAddr
,
auth
)
if
_
,
err
:=
decryptKeyFromFile
(
ks
.
keysDirPath
,
keyAddr
,
auth
);
err
!=
nil
{
if
err
!=
nil
{
return
err
return
err
}
}
return
deleteKey
(
ks
.
keysDirPath
,
keyAddr
)
return
deleteKey
(
ks
.
keysDirPath
,
keyAddr
)
}
}
func
decryptKeyFromFile
(
keysDirPath
string
,
keyAddr
common
.
Address
,
auth
string
)
(
keyBytes
[]
byte
,
keyId
[]
byte
,
err
error
)
{
// DecryptKey decrypts a key from a json blob, returning the private key itself.
func
DecryptKey
(
keyjson
[]
byte
,
auth
string
)
(
*
Key
,
error
)
{
// Parse the json into a simple map to fetch the key version
m
:=
make
(
map
[
string
]
interface
{})
m
:=
make
(
map
[
string
]
interface
{})
err
=
getKey
(
keysDirPath
,
keyAddr
,
&
m
)
if
err
:=
json
.
Unmarshal
(
keyjson
,
&
m
);
err
!=
nil
{
if
err
!=
nil
{
return
nil
,
err
return
}
}
// Depending on the version try to parse one way or another
var
(
keyBytes
,
keyId
[]
byte
err
error
)
v
:=
reflect
.
ValueOf
(
m
[
"version"
])
v
:=
reflect
.
ValueOf
(
m
[
"version"
])
if
v
.
Kind
()
==
reflect
.
String
&&
v
.
String
()
==
"1"
{
if
v
.
Kind
()
==
reflect
.
String
&&
v
.
String
()
==
"1"
{
k
:=
new
(
encryptedKeyJSONV1
)
k
:=
new
(
encryptedKeyJSONV1
)
err
=
getKey
(
keysDirPath
,
keyAddr
,
&
k
)
if
err
:=
json
.
Unmarshal
(
keyjson
,
k
);
err
!=
nil
{
if
err
!=
nil
{
return
nil
,
err
return
}
}
return
decryptKeyV1
(
k
,
auth
)
keyBytes
,
keyId
,
err
=
decryptKeyV1
(
k
,
auth
)
}
else
{
}
else
{
k
:=
new
(
encryptedKeyJSONV3
)
k
:=
new
(
encryptedKeyJSONV3
)
err
=
getKey
(
keysDirPath
,
keyAddr
,
&
k
)
if
err
:=
json
.
Unmarshal
(
keyjson
,
k
);
err
!=
nil
{
if
err
!=
nil
{
return
nil
,
err
return
}
}
return
decryptKeyV3
(
k
,
auth
)
keyBytes
,
keyId
,
err
=
decryptKeyV3
(
k
,
auth
)
}
// Handle any decryption errors and return the key
if
err
!=
nil
{
return
nil
,
err
}
key
:=
ToECDSA
(
keyBytes
)
return
&
Key
{
Id
:
uuid
.
UUID
(
keyId
),
Address
:
PubkeyToAddress
(
key
.
PublicKey
),
PrivateKey
:
key
,
},
nil
}
func
decryptKeyFromFile
(
keysDirPath
string
,
keyAddr
common
.
Address
,
auth
string
)
(
key
*
Key
,
err
error
)
{
keyjson
,
err
:=
getKeyFile
(
keysDirPath
,
keyAddr
)
if
err
!=
nil
{
return
nil
,
err
}
}
return
DecryptKey
(
keyjson
,
auth
)
}
}
func
decryptKeyV3
(
keyProtected
*
encryptedKeyJSONV3
,
auth
string
)
(
keyBytes
[]
byte
,
keyId
[]
byte
,
err
error
)
{
func
decryptKeyV3
(
keyProtected
*
encryptedKeyJSONV3
,
auth
string
)
(
keyBytes
[]
byte
,
keyId
[]
byte
,
err
error
)
{
...
...
crypto/key_store_plain.go
View file @
cda91ee1
...
@@ -62,18 +62,16 @@ func GenerateNewKeyDefault(ks KeyStore, rand io.Reader, auth string) (key *Key,
...
@@ -62,18 +62,16 @@ func GenerateNewKeyDefault(ks KeyStore, rand io.Reader, auth string) (key *Key,
return
key
,
err
return
key
,
err
}
}
func
(
ks
keyStorePlain
)
GetKey
(
keyAddr
common
.
Address
,
auth
string
)
(
key
*
Key
,
err
error
)
{
func
(
ks
keyStorePlain
)
GetKey
(
keyAddr
common
.
Address
,
auth
string
)
(
*
Key
,
error
)
{
key
=
new
(
Key
)
keyjson
,
err
:=
getKeyFile
(
ks
.
keysDirPath
,
keyAddr
)
err
=
getKey
(
ks
.
keysDirPath
,
keyAddr
,
key
)
return
}
func
getKey
(
keysDirPath
string
,
keyAddr
common
.
Address
,
content
interface
{})
(
err
error
)
{
fileContent
,
err
:=
getKeyFile
(
keysDirPath
,
keyAddr
)
if
err
!=
nil
{
if
err
!=
nil
{
return
return
nil
,
err
}
key
:=
new
(
Key
)
if
err
:=
json
.
Unmarshal
(
keyjson
,
key
);
err
!=
nil
{
return
nil
,
err
}
}
return
json
.
Unmarshal
(
fileContent
,
content
)
return
key
,
nil
}
}
func
(
ks
keyStorePlain
)
GetKeyAddresses
()
(
addresses
[]
common
.
Address
,
err
error
)
{
func
(
ks
keyStorePlain
)
GetKeyAddresses
()
(
addresses
[]
common
.
Address
,
err
error
)
{
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment