-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4 from MysteriumNetwork/improvement/registry-code…
…-refactor Registry code refactored - signing and public key providing separated…
- Loading branch information
Showing
13 changed files
with
13,603 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
**/generated | ||
**/testoutput | ||
**/geth | ||
**/keystore | ||
**/geth.ipc | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
package registry | ||
|
||
import ( | ||
"fmt" | ||
) | ||
|
||
const registerPrefix = "Register prefix:" | ||
|
||
type Signer interface { | ||
Sign(data ...[]byte) ([]byte, error) | ||
} | ||
|
||
type PublicKeyHolder interface { | ||
GetPublicKey() ([]byte, error) | ||
} | ||
|
||
type IdentityHolder interface { | ||
Signer | ||
PublicKeyHolder | ||
} | ||
|
||
type ProofOfIdentity struct { | ||
Data []byte | ||
Signature *DecomposedSignature | ||
} | ||
|
||
func (proof *ProofOfIdentity) String() string { | ||
return fmt.Sprintf("Proof: %+v", *proof) | ||
} | ||
|
||
func CreateProofOfIdentity(identityHolder IdentityHolder) (*ProofOfIdentity, error) { | ||
pubKeyBytes, err := identityHolder.GetPublicKey() | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
signature, err := identityHolder.Sign([]byte(registerPrefix), pubKeyBytes) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
decSig, err := DecomposeSignature(signature) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return &ProofOfIdentity{ | ||
pubKeyBytes, | ||
decSig, | ||
}, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package registry | ||
|
||
import ( | ||
"github.com/ethereum/go-ethereum/accounts" | ||
"github.com/ethereum/go-ethereum/accounts/keystore" | ||
"github.com/ethereum/go-ethereum/common" | ||
"github.com/ethereum/go-ethereum/crypto" | ||
) | ||
|
||
type keystoreIdentity struct { | ||
keystore *keystore.KeyStore | ||
account accounts.Account | ||
} | ||
|
||
func (ki *keystoreIdentity) Sign(data ...[]byte) ([]byte, error) { | ||
hash := crypto.Keccak256(data...) | ||
return ki.keystore.SignHash(ki.account, hash) | ||
} | ||
|
||
func (ki *keystoreIdentity) GetPublicKey() ([]byte, error) { | ||
hash := crypto.Keccak256([]byte("public key recover")) | ||
sig, err := ki.keystore.SignHash(ki.account, hash) | ||
if err != nil { | ||
return nil, err | ||
} | ||
pubKeyBytes, err := crypto.Ecrecover(hash, sig) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return pubKeyBytes[1:], nil | ||
} | ||
|
||
func FromKeystoreIdentity(keystore *keystore.KeyStore, identity common.Address) IdentityHolder { | ||
return &keystoreIdentity{ | ||
keystore: keystore, | ||
account: accounts.Account{Address: identity}, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
package registry | ||
|
||
import ( | ||
"github.com/ethereum/go-ethereum/accounts/keystore" | ||
"github.com/ethereum/go-ethereum/crypto" | ||
"github.com/stretchr/testify/assert" | ||
"os" | ||
"testing" | ||
) | ||
|
||
func TestKeystoreSignerProvidesValidPublicKey(t *testing.T) { | ||
assert.NoError(t, os.RemoveAll("testoutput")) | ||
|
||
sampleKey, err := crypto.GenerateKey() | ||
assert.NoError(t, err) | ||
keyHolder := IdentityFromPrivateKey(sampleKey) | ||
|
||
ks := keystore.NewKeyStore("testoutput", keystore.StandardScryptN, keystore.StandardScryptP) | ||
acc, err := ks.ImportECDSA(sampleKey, "") | ||
assert.NoError(t, err) | ||
|
||
assert.NoError(t, ks.Unlock(acc, "")) | ||
identityHolder := FromKeystoreIdentity(ks, acc.Address) | ||
extractedPubKey, err := identityHolder.GetPublicKey() | ||
assert.NoError(t, err) | ||
|
||
expectedPubKey, err := keyHolder.GetPublicKey() | ||
assert.NoError(t, err) | ||
|
||
assert.Equal(t, expectedPubKey, extractedPubKey) | ||
} | ||
|
||
func TestKeyStoreSignerProvidesValidSignature(t *testing.T) { | ||
|
||
assert.NoError(t, os.RemoveAll("testoutput")) | ||
|
||
sampleKey, err := crypto.GenerateKey() | ||
assert.NoError(t, err) | ||
keyHolder := IdentityFromPrivateKey(sampleKey) | ||
|
||
ks := keystore.NewKeyStore("testoutput", keystore.StandardScryptN, keystore.StandardScryptP) | ||
acc, err := ks.ImportECDSA(sampleKey, "") | ||
assert.NoError(t, err) | ||
|
||
assert.NoError(t, ks.Unlock(acc, "")) | ||
identityHolder := FromKeystoreIdentity(ks, acc.Address) | ||
|
||
signData := []byte("Testing signature") | ||
|
||
extractedSignature, err := identityHolder.Sign(signData) | ||
assert.NoError(t, err) | ||
|
||
expectedSignature, err := keyHolder.Sign(signData) | ||
assert.NoError(t, err) | ||
|
||
assert.Equal(t, expectedSignature, extractedSignature) | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package registry | ||
|
||
import ( | ||
"crypto/ecdsa" | ||
"github.com/ethereum/go-ethereum/crypto" | ||
) | ||
|
||
type privateKeyHolder struct { | ||
privateKey *ecdsa.PrivateKey | ||
} | ||
|
||
func (pkh *privateKeyHolder) Sign(data ...[]byte) ([]byte, error) { | ||
return crypto.Sign(crypto.Keccak256(data...), pkh.privateKey) | ||
} | ||
|
||
func (pkh *privateKeyHolder) GetPublicKey() ([]byte, error) { | ||
pubKeyBytes := crypto.FromECDSAPub(&pkh.privateKey.PublicKey) | ||
return pubKeyBytes[1:], nil //drop first byte as it's encoded curve type - we are not interested in as so does ethereum EVM | ||
} | ||
|
||
func IdentityFromPrivateKey(privateKey *ecdsa.PrivateKey) IdentityHolder { | ||
return &privateKeyHolder{ | ||
privateKey: privateKey, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.