Skip to content

Commit

Permalink
Merge pull request #4 from MysteriumNetwork/improvement/registry-code…
Browse files Browse the repository at this point in the history
…-refactor

Registry code refactored - signing and public key providing separated…
  • Loading branch information
tadovas authored Jul 17, 2018
2 parents 48d0c17 + 9ea60c2 commit 0d3ef8e
Show file tree
Hide file tree
Showing 13 changed files with 13,603 additions and 33 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
**/generated
**/testoutput
**/geth
**/keystore
**/geth.ipc
Expand Down
5 changes: 5 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,11 @@ before_deploy:
deploy:
- provider: releases
file:
#MystToken for testnet
- "build/src/MystToken.sol"
- "build/abi/MystToken.abi"
- "build/bin/MystToken.bin"
#Payments
- "build/src/IdentityPromises.sol"
- "build/abi/IdentityPromises.abi"
- "build/bin/IdentityPromises.bin"
Expand Down
1,849 changes: 1,849 additions & 0 deletions balances/generated/IdentityBalances.go

Large diffs are not rendered by default.

4,768 changes: 4,768 additions & 0 deletions mysttoken/generated/mysttoken.go

Large diffs are not rendered by default.

4,241 changes: 4,241 additions & 0 deletions promises/generated/IdentityPromises.go

Large diffs are not rendered by default.

2,192 changes: 2,192 additions & 0 deletions registry/generated/registry.go

Large diffs are not rendered by default.

51 changes: 51 additions & 0 deletions registry/identity_signature.go
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
}
38 changes: 38 additions & 0 deletions registry/keystore_signature.go
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},
}
}
58 changes: 58 additions & 0 deletions registry/keystore_signature_test.go
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)

}
25 changes: 25 additions & 0 deletions registry/private_key_signature.go
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,
}
}
36 changes: 4 additions & 32 deletions registry/registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package registry
import (
"crypto/ecdsa"
"errors"
"fmt"
"github.com/MysteriumNetwork/payments/registry/generated"
"github.com/ethereum/go-ethereum/accounts/abi/bind"
"github.com/ethereum/go-ethereum/common"
Expand All @@ -21,6 +20,7 @@ func init() {
}

type MystIdentity struct {
*privateKeyHolder
PrivateKey *ecdsa.PrivateKey
PublicKey *ecdsa.PublicKey
Address common.Address
Expand All @@ -32,43 +32,15 @@ func NewMystIdentity() (*MystIdentity, error) {
return nil, err
}
return &MystIdentity{
&privateKeyHolder{
privateKey: key,
},
key,
&key.PublicKey,
crypto.PubkeyToAddress(key.PublicKey),
}, nil
}

func (identity *MystIdentity) PubKeyToBytes() []byte {
pubKeyBytes := crypto.FromECDSAPub(identity.PublicKey)
return pubKeyBytes[1:] //drop first byte as it's encoded curve type - we are not interested in as so does ethereum EVM
}

type ProofOfIdentity struct {
Data []byte
Signature *DecomposedSignature
}

func (proof *ProofOfIdentity) String() string {
return fmt.Sprintf("Proof: %+v", *proof)
}

func CreateProofOfIdentity(identity *MystIdentity) (*ProofOfIdentity, error) {
signature, err := crypto.Sign(crypto.Keccak256([]byte("Register prefix:"), identity.PubKeyToBytes()), identity.PrivateKey)
if err != nil {
return nil, err
}

decSig, err := DecomposeSignature(signature)
if err != nil {
return nil, err
}

return &ProofOfIdentity{
identity.PubKeyToBytes(),
decSig,
}, nil
}

type Registry struct {
generated.IdentityRegistrySession
Address common.Address
Expand Down
4 changes: 4 additions & 0 deletions scripts/release.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,9 @@
set -e
mkdir -p build/src
./scripts/package_sol.sh ./contracts build/src
#MystToken for testnet purposes only
./scripts/generate_abi.sh build/src/MystToken.sol build/abi
./scripts/generate_bin.sh build/src/MystToken.sol build/bin
#Payments
./scripts/generate_abi.sh build/src/IdentityPromises.sol build/abi
./scripts/generate_bin.sh build/src/IdentityPromises.sol build/bin
Loading

0 comments on commit 0d3ef8e

Please sign in to comment.