diff --git a/accounts/account_manager.go b/accounts/account_manager.go index e9eb8f8165..6cbd23c4e9 100644 --- a/accounts/account_manager.go +++ b/accounts/account_manager.go @@ -33,7 +33,6 @@ and accounts persistence is derived from stored keys' addresses package accounts import ( - "bytes" "crypto/ecdsa" crand "crypto/rand" "errors" @@ -41,6 +40,7 @@ import ( "sync" "time" + "github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/crypto" ) @@ -50,12 +50,12 @@ var ( ) type Account struct { - Address []byte + Address common.Address } type Manager struct { keyStore crypto.KeyStore2 - unlocked map[string]*unlocked + unlocked map[common.Address]*unlocked mutex sync.RWMutex } @@ -67,40 +67,40 @@ type unlocked struct { func NewManager(keyStore crypto.KeyStore2) *Manager { return &Manager{ keyStore: keyStore, - unlocked: make(map[string]*unlocked), + unlocked: make(map[common.Address]*unlocked), } } -func (am *Manager) HasAccount(addr []byte) bool { +func (am *Manager) HasAccount(addr common.Address) bool { accounts, _ := am.Accounts() for _, acct := range accounts { - if bytes.Compare(acct.Address, addr) == 0 { + if acct.Address == addr { return true } } return false } -func (am *Manager) Primary() (addr []byte, err error) { +func (am *Manager) Primary() (addr common.Address, err error) { addrs, err := am.keyStore.GetKeyAddresses() if os.IsNotExist(err) { - return nil, ErrNoKeys + return common.Address{}, ErrNoKeys } else if err != nil { - return nil, err + return common.Address{}, err } if len(addrs) == 0 { - return nil, ErrNoKeys + return common.Address{}, ErrNoKeys } return addrs[0], nil } -func (am *Manager) DeleteAccount(address []byte, auth string) error { +func (am *Manager) DeleteAccount(address common.Address, auth string) error { return am.keyStore.DeleteKey(address, auth) } func (am *Manager) Sign(a Account, toSign []byte) (signature []byte, err error) { am.mutex.RLock() - unlockedKey, found := am.unlocked[string(a.Address)] + unlockedKey, found := am.unlocked[a.Address] am.mutex.RUnlock() if !found { return nil, ErrLocked @@ -111,7 +111,7 @@ func (am *Manager) Sign(a Account, toSign []byte) (signature []byte, err error) // TimedUnlock unlocks the account with the given address. // When timeout has passed, the account will be locked again. -func (am *Manager) TimedUnlock(addr []byte, keyAuth string, timeout time.Duration) error { +func (am *Manager) TimedUnlock(addr common.Address, keyAuth string, timeout time.Duration) error { key, err := am.keyStore.GetKey(addr, keyAuth) if err != nil { return err @@ -124,7 +124,7 @@ func (am *Manager) TimedUnlock(addr []byte, keyAuth string, timeout time.Duratio // Unlock unlocks the account with the given address. The account // stays unlocked until the program exits or until a TimedUnlock // timeout (started after the call to Unlock) expires. -func (am *Manager) Unlock(addr []byte, keyAuth string) error { +func (am *Manager) Unlock(addr common.Address, keyAuth string) error { key, err := am.keyStore.GetKey(addr, keyAuth) if err != nil { return err @@ -157,10 +157,10 @@ func (am *Manager) Accounts() ([]Account, error) { return accounts, err } -func (am *Manager) addUnlocked(addr []byte, key *crypto.Key) *unlocked { +func (am *Manager) addUnlocked(addr common.Address, key *crypto.Key) *unlocked { u := &unlocked{Key: key, abort: make(chan struct{})} am.mutex.Lock() - prev, found := am.unlocked[string(addr)] + prev, found := am.unlocked[addr] if found { // terminate dropLater for this key to avoid unexpected drops. close(prev.abort) @@ -169,12 +169,12 @@ func (am *Manager) addUnlocked(addr []byte, key *crypto.Key) *unlocked { // key, i.e. when Unlock was used. zeroKey(prev.PrivateKey) } - am.unlocked[string(addr)] = u + am.unlocked[addr] = u am.mutex.Unlock() return u } -func (am *Manager) dropLater(addr []byte, u *unlocked, timeout time.Duration) { +func (am *Manager) dropLater(addr common.Address, u *unlocked, timeout time.Duration) { t := time.NewTimer(timeout) defer t.Stop() select { @@ -186,9 +186,9 @@ func (am *Manager) dropLater(addr []byte, u *unlocked, timeout time.Duration) { // was launched with. we can check that using pointer equality // because the map stores a new pointer every time the key is // unlocked. - if am.unlocked[string(addr)] == u { + if am.unlocked[addr] == u { zeroKey(u.PrivateKey) - delete(am.unlocked, string(addr)) + delete(am.unlocked, addr) } am.mutex.Unlock() } @@ -204,7 +204,7 @@ func zeroKey(k *ecdsa.PrivateKey) { // USE WITH CAUTION = this will save an unencrypted private key on disk // no cli or js interface -func (am *Manager) Export(path string, addr []byte, keyAuth string) error { +func (am *Manager) Export(path string, addr common.Address, keyAuth string) error { key, err := am.keyStore.GetKey(addr, keyAuth) if err != nil { return err diff --git a/cmd/geth/admin.go b/cmd/geth/admin.go index b0cb7507ac..949a7bde08 100644 --- a/cmd/geth/admin.go +++ b/cmd/geth/admin.go @@ -391,7 +391,7 @@ func (js *jsre) unlock(call otto.FunctionCall) otto.Value { } } am := js.ethereum.AccountManager() - err = am.TimedUnlock(common.FromHex(addr), passphrase, time.Duration(seconds)*time.Second) + err = am.TimedUnlock(common.HexToAddress(addr), passphrase, time.Duration(seconds)*time.Second) if err != nil { fmt.Printf("Unlock account failed '%v'\n", err) return otto.FalseValue() @@ -433,7 +433,7 @@ func (js *jsre) newAccount(call otto.FunctionCall) otto.Value { fmt.Printf("Could not create the account: %v", err) return otto.UndefinedValue() } - return js.re.ToVal(common.ToHex(acct.Address)) + return js.re.ToVal(acct.Address.Hex()) } func (js *jsre) nodeInfo(call otto.FunctionCall) otto.Value { diff --git a/cmd/geth/js.go b/cmd/geth/js.go index 61e97433a9..4ddb3bd9ca 100644 --- a/cmd/geth/js.go +++ b/cmd/geth/js.go @@ -26,6 +26,7 @@ import ( "strings" "github.com/ethereum/go-ethereum/cmd/utils" + "github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/common/docserver" "github.com/ethereum/go-ethereum/common/natspec" "github.com/ethereum/go-ethereum/eth" @@ -164,7 +165,7 @@ func (self *jsre) UnlockAccount(addr []byte) bool { return false } // TODO: allow retry - if err := self.ethereum.AccountManager().Unlock(addr, pass); err != nil { + if err := self.ethereum.AccountManager().Unlock(common.BytesToAddress(addr), pass); err != nil { return false } else { fmt.Println("Account is now unlocked for this session.") diff --git a/cmd/geth/main.go b/cmd/geth/main.go index 5fe83a2a0d..dbcfe81758 100644 --- a/cmd/geth/main.go +++ b/cmd/geth/main.go @@ -365,11 +365,10 @@ func unlockAccount(ctx *cli.Context, am *accounts.Manager, account string) (pass // Load startup keys. XXX we are going to need a different format // Attempt to unlock the account passphrase = getPassPhrase(ctx, "", false) - accbytes := common.FromHex(account) - if len(accbytes) == 0 { + if len(account) == 0 { utils.Fatalf("Invalid account address '%s'", account) } - err = am.Unlock(accbytes, passphrase) + err = am.Unlock(common.StringToAddress(account), passphrase) if err != nil { utils.Fatalf("Unlock account failed '%v'", err) } @@ -385,11 +384,11 @@ func startEth(ctx *cli.Context, eth *eth.Ethereum) { account := ctx.GlobalString(utils.UnlockedAccountFlag.Name) if len(account) > 0 { if account == "primary" { - accbytes, err := am.Primary() + primaryAcc, err := am.Primary() if err != nil { utils.Fatalf("no primary account: %v", err) } - account = common.ToHex(accbytes) + account = primaryAcc.Hex() } unlockAccount(ctx, am, account) } diff --git a/cmd/mist/gui.go b/cmd/mist/gui.go index 53194ae50a..f443bacbcc 100644 --- a/cmd/mist/gui.go +++ b/cmd/mist/gui.go @@ -232,7 +232,7 @@ func (self *Gui) loadMergedMiningOptions() { func (gui *Gui) insertTransaction(window string, tx *types.Transaction) { var inout string from, _ := tx.From() - if gui.eth.AccountManager().HasAccount(common.Hex2Bytes(from.Hex())) { + if gui.eth.AccountManager().HasAccount(from) { inout = "send" } else { inout = "recv" diff --git a/crypto/crypto.go b/crypto/crypto.go index 6fc5bfd363..2b1628124e 100644 --- a/crypto/crypto.go +++ b/crypto/crypto.go @@ -231,13 +231,13 @@ func decryptPreSaleKey(fileContent []byte, password string) (key *Key, err error ecKey := ToECDSA(ethPriv) key = &Key{ Id: nil, - Address: PubkeyToAddress(ecKey.PublicKey), + Address: common.BytesToAddress(PubkeyToAddress(ecKey.PublicKey)), PrivateKey: ecKey, } - derivedAddr := common.Bytes2Hex(key.Address) + derivedAddr := hex.EncodeToString(key.Address.Bytes()) // needed because .Hex() gives leading "0x" expectedAddr := preSaleKeyStruct.EthAddr if derivedAddr != expectedAddr { - err = errors.New("decrypted addr not equal to expected addr") + err = errors.New(fmt.Sprintf("decrypted addr not equal to expected addr ", derivedAddr, expectedAddr)) } return key, err } diff --git a/crypto/key.go b/crypto/key.go index 654d9e83da..5e1f3637ea 100644 --- a/crypto/key.go +++ b/crypto/key.go @@ -30,12 +30,13 @@ import ( "io" "code.google.com/p/go-uuid/uuid" + "github.com/ethereum/go-ethereum/common" ) type Key struct { Id uuid.UUID // Version 4 "random" for unique id not derived from key data // to simplify lookups we also store the address - Address []byte + Address common.Address // we only store privkey as pubkey/address can be derived from it // privkey in this struct is always in plaintext PrivateKey *ecdsa.PrivateKey @@ -63,7 +64,7 @@ type encryptedKeyJSON struct { func (k *Key) MarshalJSON() (j []byte, err error) { jStruct := plainKeyJSON{ k.Id, - k.Address, + k.Address.Bytes(), FromECDSA(k.PrivateKey), } j, err = json.Marshal(jStruct) @@ -80,7 +81,7 @@ func (k *Key) UnmarshalJSON(j []byte) (err error) { u := new(uuid.UUID) *u = keyJSON.Id k.Id = *u - k.Address = keyJSON.Address + k.Address = common.BytesToAddress(keyJSON.Address) k.PrivateKey = ToECDSA(keyJSON.PrivateKey) return err @@ -90,7 +91,7 @@ func NewKeyFromECDSA(privateKeyECDSA *ecdsa.PrivateKey) *Key { id := uuid.NewRandom() key := &Key{ Id: id, - Address: PubkeyToAddress(privateKeyECDSA.PublicKey), + Address: common.BytesToAddress(PubkeyToAddress(privateKeyECDSA.PublicKey)), PrivateKey: privateKeyECDSA, } return key diff --git a/crypto/key_store_passphrase.go b/crypto/key_store_passphrase.go index 4cfb1851b3..739483d9fd 100644 --- a/crypto/key_store_passphrase.go +++ b/crypto/key_store_passphrase.go @@ -68,7 +68,6 @@ import ( "bytes" "crypto/aes" "crypto/cipher" - "encoding/hex" "encoding/json" "errors" "io" @@ -76,6 +75,7 @@ import ( "path/filepath" "code.google.com/p/go-uuid/uuid" + "github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/crypto/randentropy" "golang.org/x/crypto/scrypt" ) @@ -100,7 +100,7 @@ func (ks keyStorePassphrase) GenerateNewKey(rand io.Reader, auth string) (key *K return GenerateNewKeyDefault(ks, rand, auth) } -func (ks keyStorePassphrase) GetKey(keyAddr []byte, auth string) (key *Key, err error) { +func (ks keyStorePassphrase) GetKey(keyAddr common.Address, auth string) (key *Key, err error) { keyBytes, keyId, err := DecryptKey(ks, keyAddr, auth) if err != nil { return nil, err @@ -113,7 +113,7 @@ func (ks keyStorePassphrase) GetKey(keyAddr []byte, auth string) (key *Key, err return key, err } -func (ks keyStorePassphrase) GetKeyAddresses() (addresses [][]byte, err error) { +func (ks keyStorePassphrase) GetKeyAddresses() (addresses []common.Address, err error) { return GetKeyAddresses(ks.keysDirPath) } @@ -150,7 +150,7 @@ func (ks keyStorePassphrase) StoreKey(key *Key, auth string) (err error) { } keyStruct := encryptedKeyJSON{ key.Id, - key.Address, + key.Address.Bytes(), cipherStruct, } keyJSON, err := json.Marshal(keyStruct) @@ -161,18 +161,18 @@ func (ks keyStorePassphrase) StoreKey(key *Key, auth string) (err error) { return WriteKeyFile(key.Address, ks.keysDirPath, keyJSON) } -func (ks keyStorePassphrase) DeleteKey(keyAddr []byte, auth string) (err error) { +func (ks keyStorePassphrase) DeleteKey(keyAddr common.Address, auth string) (err error) { // only delete if correct passphrase is given _, _, err = DecryptKey(ks, keyAddr, auth) if err != nil { return err } - keyDirPath := filepath.Join(ks.keysDirPath, hex.EncodeToString(keyAddr)) + keyDirPath := filepath.Join(ks.keysDirPath, keyAddr.Hex()) return os.RemoveAll(keyDirPath) } -func DecryptKey(ks keyStorePassphrase, keyAddr []byte, auth string) (keyBytes []byte, keyId []byte, err error) { +func DecryptKey(ks keyStorePassphrase, keyAddr common.Address, auth string) (keyBytes []byte, keyId []byte, err error) { fileContent, err := GetKeyFile(ks.keysDirPath, keyAddr) if err != nil { return nil, nil, err diff --git a/crypto/key_store_plain.go b/crypto/key_store_plain.go index 581968d7c9..2ade119852 100644 --- a/crypto/key_store_plain.go +++ b/crypto/key_store_plain.go @@ -27,6 +27,7 @@ import ( "encoding/hex" "encoding/json" "fmt" + "github.com/ethereum/go-ethereum/common" "io" "io/ioutil" "os" @@ -37,10 +38,10 @@ import ( type KeyStore2 interface { // create new key using io.Reader entropy source and optionally using auth string GenerateNewKey(io.Reader, string) (*Key, error) - GetKey([]byte, string) (*Key, error) // key from addr and auth string - GetKeyAddresses() ([][]byte, error) // get all addresses - StoreKey(*Key, string) error // store key optionally using auth string - DeleteKey([]byte, string) error // delete key by addr and auth string + GetKey(common.Address, string) (*Key, error) // key from addr and auth string + GetKeyAddresses() ([]common.Address, error) // get all addresses + StoreKey(*Key, string) error // store key optionally using auth string + DeleteKey(common.Address, string) error // delete key by addr and auth string } type keyStorePlain struct { @@ -66,7 +67,7 @@ func GenerateNewKeyDefault(ks KeyStore2, rand io.Reader, auth string) (key *Key, return key, err } -func (ks keyStorePlain) GetKey(keyAddr []byte, auth string) (key *Key, err error) { +func (ks keyStorePlain) GetKey(keyAddr common.Address, auth string) (key *Key, err error) { fileContent, err := GetKeyFile(ks.keysDirPath, keyAddr) if err != nil { return nil, err @@ -77,7 +78,7 @@ func (ks keyStorePlain) GetKey(keyAddr []byte, auth string) (key *Key, err error return key, err } -func (ks keyStorePlain) GetKeyAddresses() (addresses [][]byte, err error) { +func (ks keyStorePlain) GetKeyAddresses() (addresses []common.Address, err error) { return GetKeyAddresses(ks.keysDirPath) } @@ -90,19 +91,19 @@ func (ks keyStorePlain) StoreKey(key *Key, auth string) (err error) { return err } -func (ks keyStorePlain) DeleteKey(keyAddr []byte, auth string) (err error) { - keyDirPath := filepath.Join(ks.keysDirPath, hex.EncodeToString(keyAddr)) +func (ks keyStorePlain) DeleteKey(keyAddr common.Address, auth string) (err error) { + keyDirPath := filepath.Join(ks.keysDirPath, keyAddr.Hex()) err = os.RemoveAll(keyDirPath) return err } -func GetKeyFile(keysDirPath string, keyAddr []byte) (fileContent []byte, err error) { - fileName := hex.EncodeToString(keyAddr) +func GetKeyFile(keysDirPath string, keyAddr common.Address) (fileContent []byte, err error) { + fileName := keyAddr.Hex() return ioutil.ReadFile(filepath.Join(keysDirPath, fileName, fileName)) } -func WriteKeyFile(addr []byte, keysDirPath string, content []byte) (err error) { - addrHex := hex.EncodeToString(addr) +func WriteKeyFile(addr common.Address, keysDirPath string, content []byte) (err error) { + addrHex := addr.Hex() keyDirPath := filepath.Join(keysDirPath, addrHex) keyFilePath := filepath.Join(keyDirPath, addrHex) err = os.MkdirAll(keyDirPath, 0700) // read, write and dir search for user @@ -112,7 +113,7 @@ func WriteKeyFile(addr []byte, keysDirPath string, content []byte) (err error) { return ioutil.WriteFile(keyFilePath, content, 0600) // read, write for user } -func GetKeyAddresses(keysDirPath string) (addresses [][]byte, err error) { +func GetKeyAddresses(keysDirPath string) (addresses []common.Address, err error) { fileInfos, err := ioutil.ReadDir(keysDirPath) if err != nil { return nil, err @@ -122,7 +123,7 @@ func GetKeyAddresses(keysDirPath string) (addresses [][]byte, err error) { if err != nil { continue } - addresses = append(addresses, address) + addresses = append(addresses, common.BytesToAddress(address)) } return addresses, err } diff --git a/eth/backend.go b/eth/backend.go index 7960a0e61d..65b096d49c 100644 --- a/eth/backend.go +++ b/eth/backend.go @@ -386,14 +386,17 @@ func (s *Ethereum) StartMining(threads int) error { func (s *Ethereum) Etherbase() (eb common.Address, err error) { eb = s.etherbase if (eb == common.Address{}) { - var ebbytes []byte - ebbytes, err = s.accountManager.Primary() - eb = common.BytesToAddress(ebbytes) - if (eb == common.Address{}) { - err = fmt.Errorf("no accounts found") + primary, err := s.accountManager.Primary() + if err != nil { + return eb, err } + if (primary == common.Address{}) { + err = fmt.Errorf("no accounts found") + return eb, err + } + eb = primary } - return + return eb, nil } func (s *Ethereum) StopMining() { s.miner.Stop() } diff --git a/xeth/xeth.go b/xeth/xeth.go index 11dc506b85..74110c4ec1 100644 --- a/xeth/xeth.go +++ b/xeth/xeth.go @@ -365,7 +365,7 @@ func (self *XEth) Accounts() []string { accounts, _ := self.backend.AccountManager().Accounts() accountAddresses := make([]string, len(accounts)) for i, ac := range accounts { - accountAddresses[i] = common.ToHex(ac.Address) + accountAddresses[i] = ac.Address.Str() } return accountAddresses }