Commit 669ce573 authored by Janoš Guljaš's avatar Janoš Guljaš Committed by GitHub

Overlay from eth addr (#281)

parent 8d746790
......@@ -18,10 +18,10 @@ import (
// NewOverlayAddress constructs a Swarm Address from ECDSA private key.
func NewOverlayAddress(p ecdsa.PublicKey, networkID uint64) swarm.Address {
data := make([]byte, 28)
copy(data, elliptic.Marshal(btcec.S256(), p.X, p.Y)[12:32])
binary.LittleEndian.PutUint64(data[20:28], networkID)
h := sha3.Sum256(data)
ethAddr := NewEthereumAddress(p)
netIDBytes := make([]byte, 8)
binary.LittleEndian.PutUint64(netIDBytes, networkID)
h := sha3.Sum256(append(ethAddr, netIDBytes...))
return swarm.NewAddress(h[:])
}
......@@ -44,3 +44,17 @@ func DecodeSecp256k1PrivateKey(data []byte) (*ecdsa.PrivateKey, error) {
privk, _ := btcec.PrivKeyFromBytes(btcec.S256(), data)
return (*ecdsa.PrivateKey)(privk), nil
}
// NewEthereumAddress returns a binary representation of ethereum blockchain address.
// This function is based on github.com/ethereum/go-ethereum/crypto.PubkeyToAddress.
func NewEthereumAddress(p ecdsa.PublicKey) []byte {
if p.X == nil || p.Y == nil {
return nil
}
pubBytes := elliptic.Marshal(btcec.S256(), p.X, p.Y)
return legacyKeccak256(pubBytes[1:])[12:]
}
func legacyKeccak256(data []byte) []byte {
return sha3.NewLegacyKeccak256().Sum(data)
}
......@@ -25,7 +25,7 @@ var _ keystore.Service = (*Service)(nil)
const (
keyHeaderKDF = "scrypt"
keyVersion = 1
keyVersion = 3
scryptN = 1 << 15
scryptR = 8
......@@ -33,7 +33,9 @@ const (
scryptDKLen = 32
)
// This format is compatible with Ethereum JSON v3 key file format.
type encryptedKey struct {
Address string `json:"address"`
Crypto keyCripto `json:"crypto"`
Version int `json:"version"`
}
......@@ -66,6 +68,7 @@ func encryptKey(k *ecdsa.PrivateKey, password string) ([]byte, error) {
return nil, err
}
return json.Marshal(encryptedKey{
Address: hex.EncodeToString(crypto.NewEthereumAddress(k.PublicKey)),
Crypto: *kc,
Version: keyVersion,
})
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment