Skip to content

Commit

Permalink
mobile interface ComputeECDHSharedSecret (ethereum#1219)
Browse files Browse the repository at this point in the history
* add computeECDHshared secret to the mobile native interface

* Fix type and docs for ComputeECDHSharedSecret

* unmarshal bytes into ecdh public key
  • Loading branch information
hbandura authored Nov 11, 2020
1 parent 15714df commit 416e518
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 0 deletions.
24 changes: 24 additions & 0 deletions accounts/keystore/keystore.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ import (
"github.com/ethereum/go-ethereum/crypto/ecies"
"github.com/ethereum/go-ethereum/event"
"github.com/ethereum/go-ethereum/log"
"github.com/wsddn/go-ecdh"
)

var (
Expand Down Expand Up @@ -624,3 +625,26 @@ func zeroKey(k *ecdsa.PrivateKey) {
b[i] = 0
}
}

// ComputeECDHSharedSecret computes an ECDH shared secret between the given account's
// private key and the public key provided. The account has to be unlocked first.
func (ks *KeyStore) ComputeECDHSharedSecret(a accounts.Account, public []byte) ([]byte, error) {
// Look up the key to sign with and abort if it cannot be found
ks.mu.RLock()
defer ks.mu.RUnlock()

unlockedKey, found := ks.unlocked[a.Address]
if !found {
return nil, ErrLocked
}
gen := ecdh.NewEllipticECDH(crypto.S256())
pubKey, ok := gen.Unmarshal(public)
if !ok {
return nil, errors.New("Could not unmarshal public key from bytes")
}
secret, err := gen.GenerateSharedSecret(unlockedKey.PrivateKey, pubKey)
if err != nil {
return nil, err
}
return secret, nil
}
Binary file modified geth-sources.jar
Binary file not shown.
6 changes: 6 additions & 0 deletions mobile/accounts.go
Original file line number Diff line number Diff line change
Expand Up @@ -224,3 +224,9 @@ func (ks *KeyStore) ImportPreSaleKey(keyJSON []byte, passphrase string) (ccount
}
return &Account{account}, nil
}

// ComputeECDHSharedSecret computes an ECDH shared secret between the given account's
// private key and the public key provided. The account has to be unlocked first.
func (ks *KeyStore) ComputeECDHSharedSecret(a Account, public []byte) ([]byte, error) {
return ks.keystore.ComputeECDHSharedSecret(a.account, public)
}

0 comments on commit 416e518

Please sign in to comment.