Răsfoiți Sursa

Update key store to new spec but keep address field for now

* Also fix address types post-rebase
Gustav Simonsson 10 ani în urmă
părinte
comite
2c1b0ff17e
7 a modificat fișierele cu 59 adăugiri și 72 ștergeri
  1. 1 1
      cmd/geth/admin.go
  2. 1 1
      crypto/crypto.go
  3. 25 25
      crypto/key.go
  4. 29 42
      crypto/key_store_passphrase.go
  5. 1 1
      miner/worker.go
  6. 1 1
      tests/block_test_util.go
  7. 1 1
      xeth/xeth.go

+ 1 - 1
cmd/geth/admin.go

@@ -126,7 +126,7 @@ func (js *jsre) pendingTransactions(call otto.FunctionCall) otto.Value {
 	// Add the accouns to a new set
 	accountSet := set.New()
 	for _, account := range accounts {
-		accountSet.Add(common.BytesToAddress(account.Address))
+		accountSet.Add(account.Address)
 	}
 
 	//ltxs := make([]*tx, len(txs))

+ 1 - 1
crypto/crypto.go

@@ -185,7 +185,7 @@ func ImportBlockTestKey(privKeyBytes []byte) error {
 	ecKey := ToECDSA(privKeyBytes)
 	key := &Key{
 		Id:         uuid.NewRandom(),
-		Address:    PubkeyToAddress(ecKey.PublicKey),
+		Address:    common.BytesToAddress(PubkeyToAddress(ecKey.PublicKey)),
 		PrivateKey: ecKey,
 	}
 	err := ks.StoreKey(key, "")

+ 25 - 25
crypto/key.go

@@ -48,47 +48,47 @@ type Key struct {
 }
 
 type plainKeyJSON struct {
-	Version    string
-	Id         string
-	Address    string
-	PrivateKey string
+	Address    string `json:"address"`
+	PrivateKey string `json:"privatekey"`
+	Id         string `json:"id"`
+	Version    string `json:"version"`
 }
 
 type encryptedKeyJSON struct {
-	Version string
-	Id      string
-	Address string
-	Crypto  cipherJSON
+	Address string `json:"address"`
+	Crypto  cryptoJSON
+	Id      string `json:"id"`
+	Version string `json:"version"`
 }
 
-type cipherJSON struct {
-	MAC        string
-	Salt       string
-	IV         string
-	KeyHeader  keyHeaderJSON
-	CipherText string
+type cryptoJSON struct {
+	Cipher       string           `json:"cipher"`
+	CipherText   string           `json:"ciphertext"`
+	CipherParams cipherparamsJSON `json:"cipherparams"`
+	KDF          string           `json:"kdf"`
+	KDFParams    scryptParamsJSON `json:"kdfparams"`
+	MAC          string           `json:"mac"`
+	Version      string           `json:"version"`
 }
 
-type keyHeaderJSON struct {
-	Version   string
-	Kdf       string
-	KdfParams scryptParamsJSON
+type cipherparamsJSON struct {
+	IV string `json:"iv"`
 }
 
 type scryptParamsJSON struct {
-	N       int
-	R       int
-	P       int
-	DkLen   int
-	SaltLen int
+	N     int    `json:"n"`
+	R     int    `json:"r"`
+	P     int    `json:"p"`
+	DkLen int    `json:"dklen"`
+	Salt  string `json:"salt"`
 }
 
 func (k *Key) MarshalJSON() (j []byte, err error) {
 	jStruct := plainKeyJSON{
-		version,
-		k.Id.String(),
 		hex.EncodeToString(k.Address[:]),
 		hex.EncodeToString(FromECDSA(k.PrivateKey)),
+		k.Id.String(),
+		version,
 	}
 	j, err = json.Marshal(jStruct)
 	return j, err

+ 29 - 42
crypto/key_store_passphrase.go

@@ -143,41 +143,36 @@ func (ks keyStorePassphrase) StoreKey(key *Key, auth string) (err error) {
 	cipherText := make([]byte, len(toEncrypt))
 	AES128CBCEncrypter.CryptBlocks(cipherText, toEncrypt)
 
-	paramsJSON := scryptParamsJSON{
-		N:       scryptN,
-		R:       scryptr,
-		P:       scryptp,
-		DkLen:   scryptdkLen,
-		SaltLen: 32,
-	}
+	mac := Sha3(derivedKey[16:32], cipherText)
 
-	keyHeaderJSON := keyHeaderJSON{
-		Version:   keyHeaderVersion,
-		Kdf:       keyHeaderKDF,
-		KdfParams: paramsJSON,
+	scryptParamsJSON := scryptParamsJSON{
+		N:     scryptN,
+		R:     scryptr,
+		P:     scryptp,
+		DkLen: scryptdkLen,
+		Salt:  hex.EncodeToString(salt),
 	}
 
-	keyHeaderJSONStr, err := json.Marshal(keyHeaderJSON)
-	if err != nil {
-		return err
+	cipherParamsJSON := cipherparamsJSON{
+		IV: hex.EncodeToString(iv),
 	}
 
-	mac := Sha3(keyHeaderJSONStr, derivedKey[16:32], cipherText)
-
-	cipherStruct := cipherJSON{
-		hex.EncodeToString(mac),
-		hex.EncodeToString(salt),
-		hex.EncodeToString(iv),
-		keyHeaderJSON,
-		hex.EncodeToString(cipherText),
+	cryptoStruct := cryptoJSON{
+		Cipher:       "aes-128-cbc",
+		CipherText:   hex.EncodeToString(cipherText),
+		CipherParams: cipherParamsJSON,
+		KDF:          "scrypt",
+		KDFParams:    scryptParamsJSON,
+		MAC:          hex.EncodeToString(mac),
+		Version:      "1",
 	}
-	keyStruct := encryptedKeyJSON{
-		version,
-		key.Id.String(),
+	encryptedKeyJSON := encryptedKeyJSON{
 		hex.EncodeToString(key.Address[:]),
-		cipherStruct,
+		cryptoStruct,
+		key.Id.String(),
+		version,
 	}
-	keyJSON, err := json.Marshal(keyStruct)
+	keyJSON, err := json.Marshal(encryptedKeyJSON)
 	if err != nil {
 		return err
 	}
@@ -212,33 +207,25 @@ func DecryptKey(ks keyStorePassphrase, keyAddr common.Address, auth string) (key
 		return nil, nil, err
 	}
 
-	salt, err := hex.DecodeString(keyProtected.Crypto.Salt)
-	if err != nil {
-		return nil, nil, err
-	}
-
-	iv, err := hex.DecodeString(keyProtected.Crypto.IV)
+	iv, err := hex.DecodeString(keyProtected.Crypto.CipherParams.IV)
 	if err != nil {
 		return nil, nil, err
 	}
 
-	keyHeader := keyProtected.Crypto.KeyHeader
 	cipherText, err := hex.DecodeString(keyProtected.Crypto.CipherText)
 	if err != nil {
 		return nil, nil, err
 	}
 
-	// used in MAC
-	keyHeaderJSONStr, err := json.Marshal(keyHeader)
+	salt, err := hex.DecodeString(keyProtected.Crypto.KDFParams.Salt)
 	if err != nil {
 		return nil, nil, err
 	}
 
-	// TODO: make this more generic when we support different KDF params / key versions
-	n := keyHeader.KdfParams.N
-	r := keyHeader.KdfParams.R
-	p := keyHeader.KdfParams.P
-	dkLen := keyHeader.KdfParams.DkLen
+	n := keyProtected.Crypto.KDFParams.N
+	r := keyProtected.Crypto.KDFParams.R
+	p := keyProtected.Crypto.KDFParams.P
+	dkLen := keyProtected.Crypto.KDFParams.DkLen
 
 	authArray := []byte(auth)
 	derivedKey, err := scrypt.Key(authArray, salt, n, r, p, dkLen)
@@ -246,7 +233,7 @@ func DecryptKey(ks keyStorePassphrase, keyAddr common.Address, auth string) (key
 		return nil, nil, err
 	}
 
-	calculatedMAC := Sha3(keyHeaderJSONStr, derivedKey[16:32], cipherText)
+	calculatedMAC := Sha3(derivedKey[16:32], cipherText)
 	if !bytes.Equal(calculatedMAC, mac) {
 		err = errors.New("Decryption failed: MAC mismatch")
 		return nil, nil, err

+ 1 - 1
miner/worker.go

@@ -474,7 +474,7 @@ func gasprice(price *big.Int, pct int64) *big.Int {
 func accountAddressesSet(accounts []accounts.Account) *set.Set {
 	accountSet := set.New()
 	for _, account := range accounts {
-		accountSet.Add(common.BytesToAddress(account.Address))
+		accountSet.Add(account.Address)
 	}
 	return accountSet
 }

+ 1 - 1
tests/block_test_util.go

@@ -113,7 +113,7 @@ func (t *BlockTest) InsertPreState(ethereum *eth.Ethereum) (*state.StateDB, erro
 		if acct.PrivateKey != "" {
 			privkey, err := hex.DecodeString(strings.TrimPrefix(acct.PrivateKey, "0x"))
 			err = crypto.ImportBlockTestKey(privkey)
-			err = ethereum.AccountManager().TimedUnlock(addr, "", 999999*time.Second)
+			err = ethereum.AccountManager().TimedUnlock(common.BytesToAddress(addr), "", 999999*time.Second)
 			if err != nil {
 				return nil, err
 			}

+ 1 - 1
xeth/xeth.go

@@ -817,7 +817,7 @@ func (self *XEth) ConfirmTransaction(tx string) bool {
 }
 
 func (self *XEth) doSign(from common.Address, hash common.Hash, didUnlock bool) ([]byte, error) {
-	sig, err := self.backend.AccountManager().Sign(accounts.Account{Address: from.Bytes()}, hash.Bytes())
+	sig, err := self.backend.AccountManager().Sign(accounts.Account{Address: from}, hash.Bytes())
 	if err == accounts.ErrLocked {
 		if didUnlock {
 			return nil, fmt.Errorf("signer account still locked after successful unlock")