|
|
@ -35,12 +35,11 @@ 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/crypto/secp256k1" |
|
|
|
|
|
|
|
) |
|
|
|
) |
|
|
|
|
|
|
|
|
|
|
|
// TODO: better name for this struct?
|
|
|
|
// TODO: better name for this struct?
|
|
|
|
type UserAccount struct { |
|
|
|
type Account struct { |
|
|
|
Addr []byte |
|
|
|
Address []byte |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
type AccountManager struct { |
|
|
|
type AccountManager struct { |
|
|
@ -57,43 +56,40 @@ func NewAccountManager(keyStore crypto.KeyStore2) AccountManager { |
|
|
|
return *am |
|
|
|
return *am |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
func (am *AccountManager) Sign(fromAddr []byte, keyAuth string, toSign []byte) (signature []byte, err error) { |
|
|
|
func (am *AccountManager) Sign(fromAccount *Account, keyAuth string, toSign []byte) (signature []byte, err error) { |
|
|
|
key, err := am.keyStore.GetKey(fromAddr, keyAuth) |
|
|
|
key, err := am.keyStore.GetKey(fromAccount.Address, keyAuth) |
|
|
|
if err != nil { |
|
|
|
if err != nil { |
|
|
|
return nil, err |
|
|
|
return nil, err |
|
|
|
} |
|
|
|
} |
|
|
|
privKey := crypto.FromECDSA(key.PrivateKey) |
|
|
|
signature, err = crypto.Sign(toSign, key.PrivateKey) |
|
|
|
// TODO: what is second value?
|
|
|
|
|
|
|
|
signature, err = secp256k1.Sign(toSign, privKey) |
|
|
|
|
|
|
|
return signature, err |
|
|
|
return signature, err |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
func (am AccountManager) NewAccount(auth string) (*UserAccount, error) { |
|
|
|
func (am AccountManager) NewAccount(auth string) (*Account, error) { |
|
|
|
key, err := am.keyStore.GenerateNewKey(crand.Reader, auth) |
|
|
|
key, err := am.keyStore.GenerateNewKey(crand.Reader, auth) |
|
|
|
if err != nil { |
|
|
|
if err != nil { |
|
|
|
return nil, err |
|
|
|
return nil, err |
|
|
|
} |
|
|
|
} |
|
|
|
ua := &UserAccount{ |
|
|
|
ua := &Account{ |
|
|
|
Addr: key.Address, |
|
|
|
Address: key.Address, |
|
|
|
} |
|
|
|
} |
|
|
|
return ua, err |
|
|
|
return ua, err |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// set of accounts == set of keys in given key store
|
|
|
|
// set of accounts == set of keys in given key store
|
|
|
|
// TODO: do we need persistence of accounts as well?
|
|
|
|
// TODO: do we need persistence of accounts as well?
|
|
|
|
func (am *AccountManager) Accounts() ([]UserAccount, error) { |
|
|
|
func (am *AccountManager) Accounts() ([]Account, error) { |
|
|
|
addresses, err := am.keyStore.GetKeyAddresses() |
|
|
|
addresses, err := am.keyStore.GetKeyAddresses() |
|
|
|
if err != nil { |
|
|
|
if err != nil { |
|
|
|
return nil, err |
|
|
|
return nil, err |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
accounts := make([]UserAccount, len(addresses)) |
|
|
|
accounts := make([]Account, len(addresses)) |
|
|
|
|
|
|
|
|
|
|
|
for i, addr := range addresses { |
|
|
|
for i, addr := range addresses { |
|
|
|
ua := &UserAccount{ |
|
|
|
accounts[i] = Account{ |
|
|
|
Addr: addr, |
|
|
|
Address: addr, |
|
|
|
} |
|
|
|
} |
|
|
|
accounts[i] = *ua |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
return accounts, err |
|
|
|
return accounts, err |
|
|
|
} |
|
|
|
} |
|
|
|