good morning!!!!
Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
B
bor
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Iterations
Wiki
Requirements
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Test cases
Artifacts
Deploy
Releases
Package registry
Container registry
Harbor Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Issue analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
GitLab community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
open
bor
Commits
46ce66a8
Commit
46ce66a8
authored
Feb 25, 2015
by
Jeffrey Wilcke
Browse files
Options
Downloads
Plain Diff
Merge pull request #385 from Gustav-Simonsson/account_manager_key_locking
Add automatic locking / unlocking of accounts
parents
b155b9d8
23f26580
Branches
Branches containing commit
Tags
Tags containing commit
No related merge requests found
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
accounts/account_manager.go
+40
-9
40 additions, 9 deletions
accounts/account_manager.go
accounts/accounts_test.go
+51
-2
51 additions, 2 deletions
accounts/accounts_test.go
with
91 additions
and
11 deletions
accounts/account_manager.go
+
40
−
9
View file @
46ce66a8
...
@@ -34,9 +34,14 @@ package accounts
...
@@ -34,9 +34,14 @@ package accounts
import
(
import
(
crand
"crypto/rand"
crand
"crypto/rand"
"errors"
"github.com/ethereum/go-ethereum/crypto"
"github.com/ethereum/go-ethereum/crypto"
"sync"
"time"
)
)
var
ErrLocked
=
errors
.
New
(
"account is locked; please request passphrase"
)
// TODO: better name for this struct?
// TODO: better name for this struct?
type
Account
struct
{
type
Account
struct
{
Address
[]
byte
Address
[]
byte
...
@@ -44,14 +49,17 @@ type Account struct {
...
@@ -44,14 +49,17 @@ type Account struct {
type
AccountManager
struct
{
type
AccountManager
struct
{
keyStore
crypto
.
KeyStore2
keyStore
crypto
.
KeyStore2
unlockedKeys
map
[
string
]
crypto
.
Key
unlockMilliseconds
time
.
Duration
mutex
sync
.
RWMutex
}
}
// TODO: get key by addr - modify KeyStore2 GetKey to work with addr
func
NewAccountManager
(
keyStore
crypto
.
KeyStore2
,
unlockMilliseconds
time
.
Duration
)
AccountManager
{
keysMap
:=
make
(
map
[
string
]
crypto
.
Key
)
// TODO: pass through passphrase for APIs which require access to private key?
func
NewAccountManager
(
keyStore
crypto
.
KeyStore2
)
AccountManager
{
am
:=
&
AccountManager
{
am
:=
&
AccountManager
{
keyStore
:
keyStore
,
keyStore
:
keyStore
,
unlockedKeys
:
keysMap
,
unlockMilliseconds
:
unlockMilliseconds
,
}
}
return
*
am
return
*
am
}
}
...
@@ -60,11 +68,26 @@ func (am AccountManager) DeleteAccount(address []byte, auth string) error {
...
@@ -60,11 +68,26 @@ func (am AccountManager) DeleteAccount(address []byte, auth string) error {
return
am
.
keyStore
.
DeleteKey
(
address
,
auth
)
return
am
.
keyStore
.
DeleteKey
(
address
,
auth
)
}
}
func
(
am
*
AccountManager
)
Sign
(
fromAccount
*
Account
,
keyAuth
string
,
toSign
[]
byte
)
(
signature
[]
byte
,
err
error
)
{
func
(
am
*
AccountManager
)
Sign
(
fromAccount
*
Account
,
toSign
[]
byte
)
(
signature
[]
byte
,
err
error
)
{
am
.
mutex
.
RLock
()
unlockedKey
:=
am
.
unlockedKeys
[
string
(
fromAccount
.
Address
)]
am
.
mutex
.
RUnlock
()
if
unlockedKey
.
Address
==
nil
{
return
nil
,
ErrLocked
}
signature
,
err
=
crypto
.
Sign
(
toSign
,
unlockedKey
.
PrivateKey
)
return
signature
,
err
}
func
(
am
*
AccountManager
)
SignLocked
(
fromAccount
*
Account
,
keyAuth
string
,
toSign
[]
byte
)
(
signature
[]
byte
,
err
error
)
{
key
,
err
:=
am
.
keyStore
.
GetKey
(
fromAccount
.
Address
,
keyAuth
)
key
,
err
:=
am
.
keyStore
.
GetKey
(
fromAccount
.
Address
,
keyAuth
)
if
err
!=
nil
{
if
err
!=
nil
{
return
nil
,
err
return
nil
,
err
}
}
am
.
mutex
.
RLock
()
am
.
unlockedKeys
[
string
(
fromAccount
.
Address
)]
=
*
key
am
.
mutex
.
RUnlock
()
go
unlockLater
(
am
,
fromAccount
.
Address
)
signature
,
err
=
crypto
.
Sign
(
toSign
,
key
.
PrivateKey
)
signature
,
err
=
crypto
.
Sign
(
toSign
,
key
.
PrivateKey
)
return
signature
,
err
return
signature
,
err
}
}
...
@@ -80,8 +103,6 @@ func (am AccountManager) NewAccount(auth string) (*Account, error) {
...
@@ -80,8 +103,6 @@ func (am AccountManager) NewAccount(auth string) (*Account, error) {
return
ua
,
err
return
ua
,
err
}
}
// set of accounts == set of keys in given key store
// TODO: do we need persistence of accounts as well?
func
(
am
*
AccountManager
)
Accounts
()
([]
Account
,
error
)
{
func
(
am
*
AccountManager
)
Accounts
()
([]
Account
,
error
)
{
addresses
,
err
:=
am
.
keyStore
.
GetKeyAddresses
()
addresses
,
err
:=
am
.
keyStore
.
GetKeyAddresses
()
if
err
!=
nil
{
if
err
!=
nil
{
...
@@ -97,3 +118,13 @@ func (am *AccountManager) Accounts() ([]Account, error) {
...
@@ -97,3 +118,13 @@ func (am *AccountManager) Accounts() ([]Account, error) {
}
}
return
accounts
,
err
return
accounts
,
err
}
}
func
unlockLater
(
am
*
AccountManager
,
addr
[]
byte
)
{
select
{
case
<-
time
.
After
(
time
.
Millisecond
*
am
.
unlockMilliseconds
)
:
}
am
.
mutex
.
RLock
()
// TODO: how do we know the key is actually gone from memory?
delete
(
am
.
unlockedKeys
,
string
(
addr
))
am
.
mutex
.
RUnlock
()
}
This diff is collapsed.
Click to expand it.
accounts/accounts_test.go
+
51
−
2
View file @
46ce66a8
...
@@ -6,19 +6,68 @@ import (
...
@@ -6,19 +6,68 @@ import (
"github.com/ethereum/go-ethereum/crypto"
"github.com/ethereum/go-ethereum/crypto"
"github.com/ethereum/go-ethereum/crypto/randentropy"
"github.com/ethereum/go-ethereum/crypto/randentropy"
"github.com/ethereum/go-ethereum/ethutil"
"github.com/ethereum/go-ethereum/ethutil"
"time"
)
)
func
TestAccountManager
(
t
*
testing
.
T
)
{
func
TestAccountManager
(
t
*
testing
.
T
)
{
ks
:=
crypto
.
NewKeyStorePlain
(
ethutil
.
DefaultDataDir
()
+
"/testaccounts"
)
ks
:=
crypto
.
NewKeyStorePlain
(
ethutil
.
DefaultDataDir
()
+
"/testaccounts"
)
am
:=
NewAccountManager
(
ks
)
am
:=
NewAccountManager
(
ks
,
100
)
pass
:=
""
// not used but required by API
pass
:=
""
// not used but required by API
a1
,
err
:=
am
.
NewAccount
(
pass
)
a1
,
err
:=
am
.
NewAccount
(
pass
)
toSign
:=
randentropy
.
GetEntropyCSPRNG
(
32
)
toSign
:=
randentropy
.
GetEntropyCSPRNG
(
32
)
_
,
err
=
am
.
Sign
(
a1
,
pass
,
toSign
)
_
,
err
=
am
.
Sign
Locked
(
a1
,
pass
,
toSign
)
if
err
!=
nil
{
if
err
!=
nil
{
t
.
Fatal
(
err
)
t
.
Fatal
(
err
)
}
}
// Cleanup
time
.
Sleep
(
time
.
Millisecond
*
150
)
// wait for locking
accounts
,
err
:=
am
.
Accounts
()
if
err
!=
nil
{
t
.
Fatal
(
err
)
}
for
_
,
account
:=
range
accounts
{
err
:=
am
.
DeleteAccount
(
account
.
Address
,
pass
)
if
err
!=
nil
{
t
.
Fatal
(
err
)
}
}
}
func
TestAccountManagerLocking
(
t
*
testing
.
T
)
{
ks
:=
crypto
.
NewKeyStorePassphrase
(
ethutil
.
DefaultDataDir
()
+
"/testaccounts"
)
am
:=
NewAccountManager
(
ks
,
200
)
pass
:=
"foo"
a1
,
err
:=
am
.
NewAccount
(
pass
)
toSign
:=
randentropy
.
GetEntropyCSPRNG
(
32
)
// Signing without passphrase fails because account is locked
_
,
err
=
am
.
Sign
(
a1
,
toSign
)
if
err
!=
ErrLocked
{
t
.
Fatal
(
err
)
}
// Signing with passphrase works
_
,
err
=
am
.
SignLocked
(
a1
,
pass
,
toSign
)
if
err
!=
nil
{
t
.
Fatal
(
err
)
}
// Signing without passphrase works because account is temp unlocked
_
,
err
=
am
.
Sign
(
a1
,
toSign
)
if
err
!=
nil
{
t
.
Fatal
(
err
)
}
// Signing without passphrase fails after automatic locking
time
.
Sleep
(
time
.
Millisecond
*
time
.
Duration
(
250
))
_
,
err
=
am
.
Sign
(
a1
,
toSign
)
if
err
!=
ErrLocked
{
t
.
Fatal
(
err
)
}
// Cleanup
// Cleanup
accounts
,
err
:=
am
.
Accounts
()
accounts
,
err
:=
am
.
Accounts
()
if
err
!=
nil
{
if
err
!=
nil
{
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment