mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-07-21 20:26:41 +00:00
Add accounts association and use address to refer to accounts
This commit is contained in:
parent
d73dff4d5b
commit
988a366a18
2 changed files with 49 additions and 40 deletions
|
|
@ -35,61 +35,65 @@ package accounts
|
||||||
import (
|
import (
|
||||||
crand "crypto/rand"
|
crand "crypto/rand"
|
||||||
"github.com/ethereum/go-ethereum/crypto"
|
"github.com/ethereum/go-ethereum/crypto"
|
||||||
|
"github.com/ethereum/go-ethereum/ethdb"
|
||||||
|
"github.com/syndtr/goleveldb/leveldb"
|
||||||
)
|
)
|
||||||
|
|
||||||
// TODO: better name for this struct?
|
/*
|
||||||
type Account struct {
|
type Account struct {
|
||||||
Address []byte
|
Address []byte
|
||||||
|
AssociatedAddress []byte // e.g. a wallet contract "owned" by the account
|
||||||
}
|
}
|
||||||
|
*/
|
||||||
|
|
||||||
type AccountManager struct {
|
type AccountManager struct {
|
||||||
keyStore crypto.KeyStore2
|
keyStore crypto.KeyStore2
|
||||||
|
accountsDb *ethdb.LDBDatabase
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: get key by addr - modify KeyStore2 GetKey to work with addr
|
// TODO: get key by addr - modify KeyStore2 GetKey to work with addr
|
||||||
|
|
||||||
// TODO: pass through passphrase for APIs which require access to private key?
|
// TODO: pass through passphrase for APIs which require access to private key?
|
||||||
func NewAccountManager(keyStore crypto.KeyStore2) AccountManager {
|
func NewAccountManager(keyStore crypto.KeyStore2) (AccountManager, error) {
|
||||||
am := &AccountManager{
|
db, err := ethdb.NewLDBDatabase("accounts")
|
||||||
keyStore: keyStore,
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
}
|
}
|
||||||
return *am
|
am := &AccountManager{
|
||||||
|
keyStore: keyStore,
|
||||||
|
accountsDb: db,
|
||||||
|
}
|
||||||
|
return *am, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (am *AccountManager) Sign(fromAccount *Account, keyAuth string, toSign []byte) (signature []byte, err error) {
|
func (am AccountManager) NewAccount(auth string) ([]byte, error) {
|
||||||
key, err := am.keyStore.GetKey(fromAccount.Address, keyAuth)
|
newKey, err := am.keyStore.GenerateNewKey(crand.Reader, auth)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
signature, err = crypto.Sign(toSign, key.PrivateKey)
|
return newKey.Address, err
|
||||||
|
}
|
||||||
|
|
||||||
|
func (am AccountManager) AssociateContract(ownerAddr []byte, associateAddr []byte) error {
|
||||||
|
am.accountsDb.Put(ownerAddr, associateAddr)
|
||||||
|
am.accountsDb.Put(associateAddr, ownerAddr)
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (am *AccountManager) GetAssociatedAddr(addr []byte) ([]byte, error) {
|
||||||
|
res, err := am.accountsDb.Get(addr)
|
||||||
|
if err == leveldb.ErrNotFound {
|
||||||
|
return []byte{}, nil
|
||||||
|
} else {
|
||||||
|
return res, nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (am *AccountManager) Sign(fromAddr []byte, keyAuth string, toSign []byte) ([]byte, error) {
|
||||||
|
key, err := am.keyStore.GetKey(fromAddr, keyAuth)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
signature, err := crypto.Sign(toSign, key.PrivateKey)
|
||||||
return signature, err
|
return signature, err
|
||||||
}
|
}
|
||||||
|
|
||||||
func (am AccountManager) NewAccount(auth string) (*Account, error) {
|
|
||||||
key, err := am.keyStore.GenerateNewKey(crand.Reader, auth)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
ua := &Account{
|
|
||||||
Address: key.Address,
|
|
||||||
}
|
|
||||||
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) {
|
|
||||||
addresses, err := am.keyStore.GetKeyAddresses()
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
accounts := make([]Account, len(addresses))
|
|
||||||
|
|
||||||
for i, addr := range addresses {
|
|
||||||
accounts[i] = Account{
|
|
||||||
Address: addr,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return accounts, err
|
|
||||||
}
|
|
||||||
|
|
|
||||||
|
|
@ -2,12 +2,17 @@ package accounts
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/ethereum/go-ethereum/crypto"
|
"github.com/ethereum/go-ethereum/crypto"
|
||||||
|
"github.com/ethereum/go-ethereum/ethutil"
|
||||||
"testing"
|
"testing"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
ethutil.ReadConfig("/tmp/ethtest", "/tmp", "ETH")
|
||||||
|
}
|
||||||
|
|
||||||
func TestAccountManager(t *testing.T) {
|
func TestAccountManager(t *testing.T) {
|
||||||
ks := crypto.NewKeyStorePlain(crypto.DefaultDataDir())
|
ks := crypto.NewKeyStorePlain(crypto.DefaultDataDir())
|
||||||
am := NewAccountManager(ks)
|
am, _ := NewAccountManager(ks)
|
||||||
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 := crypto.GetEntropyCSPRNG(32)
|
toSign := crypto.GetEntropyCSPRNG(32)
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue