2023-07-18 08:47:04 +00:00
|
|
|
package accounts
|
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/ecdsa"
|
|
|
|
|
|
|
|
libcommon "github.com/ledgerwatch/erigon-lib/common"
|
|
|
|
"github.com/ledgerwatch/erigon/core"
|
|
|
|
"github.com/ledgerwatch/erigon/crypto"
|
|
|
|
)
|
|
|
|
|
|
|
|
const DevAddress = "0x67b1d87101671b127f5f8714789C7192f7ad340e"
|
|
|
|
|
|
|
|
type Account struct {
|
|
|
|
Name string
|
|
|
|
Address libcommon.Address
|
|
|
|
sigKey *ecdsa.PrivateKey
|
|
|
|
}
|
|
|
|
|
|
|
|
func init() {
|
2023-07-20 22:10:18 +00:00
|
|
|
core.DevnetSignKey = func(addr libcommon.Address) *ecdsa.PrivateKey {
|
|
|
|
return SigKey(addr)
|
|
|
|
}
|
2023-11-01 10:08:47 +00:00
|
|
|
|
|
|
|
devnetEtherbaseAccount := &Account{
|
|
|
|
"DevnetEtherbase",
|
|
|
|
core.DevnetEtherbase,
|
|
|
|
core.DevnetSignPrivateKey,
|
|
|
|
}
|
|
|
|
accountsByAddress[core.DevnetEtherbase] = devnetEtherbaseAccount
|
|
|
|
accountsByName[devnetEtherbaseAccount.Name] = devnetEtherbaseAccount
|
2023-07-18 08:47:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var accountsByAddress = map[libcommon.Address]*Account{}
|
|
|
|
var accountsByName = map[string]*Account{}
|
|
|
|
|
|
|
|
func NewAccount(name string) *Account {
|
|
|
|
if account, ok := accountsByName[name]; ok {
|
|
|
|
return account
|
|
|
|
}
|
|
|
|
|
|
|
|
sigKey, _ := crypto.GenerateKey()
|
|
|
|
|
|
|
|
account := &Account{
|
|
|
|
Name: name,
|
|
|
|
Address: crypto.PubkeyToAddress(sigKey.PublicKey),
|
|
|
|
sigKey: sigKey,
|
|
|
|
}
|
|
|
|
|
|
|
|
accountsByAddress[account.Address] = account
|
|
|
|
accountsByName[name] = account
|
|
|
|
|
|
|
|
return account
|
|
|
|
}
|
|
|
|
|
2023-07-28 13:03:32 +00:00
|
|
|
func (a *Account) SigKey() *ecdsa.PrivateKey {
|
|
|
|
return a.sigKey
|
|
|
|
}
|
|
|
|
|
2023-07-20 22:10:18 +00:00
|
|
|
func GetAccount(account string) *Account {
|
|
|
|
if account, ok := accountsByName[account]; ok {
|
|
|
|
return account
|
|
|
|
}
|
|
|
|
|
|
|
|
if account, ok := accountsByAddress[libcommon.HexToAddress(account)]; ok {
|
|
|
|
return account
|
2023-07-18 08:47:04 +00:00
|
|
|
}
|
|
|
|
|
2023-07-20 22:10:18 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func SigKey(source interface{}) *ecdsa.PrivateKey {
|
|
|
|
switch source := source.(type) {
|
|
|
|
case libcommon.Address:
|
|
|
|
if account, ok := accountsByAddress[source]; ok {
|
|
|
|
return account.sigKey
|
|
|
|
}
|
|
|
|
|
|
|
|
if source == core.DevnetEtherbase {
|
|
|
|
return core.DevnetSignPrivateKey
|
|
|
|
}
|
|
|
|
case string:
|
|
|
|
if account := GetAccount(source); account != nil {
|
|
|
|
return account.sigKey
|
|
|
|
}
|
2023-07-18 08:47:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|