Skip to content

Commit

Permalink
Merge pull request #5 from MysteriumNetwork/improvement/integration-f…
Browse files Browse the repository at this point in the history
…eedback

Improvement/integration feedback
  • Loading branch information
tadovas authored Jul 23, 2018
2 parents 0d3ef8e + 64a40d1 commit 668aca9
Show file tree
Hide file tree
Showing 38 changed files with 387 additions and 273 deletions.
3 changes: 2 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,14 @@ before_install:
- sudo apt-get -y install solc ethereum nodejs npm
- curl https://raw.githubusercontent.com/golang/dep/master/install.sh | sh
- npm install -g soljitsu
- go get golang.org/x/tools/cmd/goimports

install:
- scripts/deps.sh ensure

script:
- scripts/test.sh -v
- scripts/check_go_fmt.sh
- scripts/check_goimports.sh

before_deploy:
- scripts/release.sh
Expand Down
14 changes: 7 additions & 7 deletions balances/balances.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
package balances

import (
"math/big"

"github.com/MysteriumNetwork/payments/balances/generated"
"github.com/MysteriumNetwork/payments/registry"
"github.com/MysteriumNetwork/payments/identity"
"github.com/ethereum/go-ethereum/accounts/abi"
"github.com/ethereum/go-ethereum/accounts/abi/bind"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/crypto"
"github.com/ethereum/go-ethereum/event"
"math/big"
)

//go:generate abigen --sol ../contracts/IdentityBalances.sol --pkg generated --out generated/IdentityBalances.go
Expand Down Expand Up @@ -46,19 +46,19 @@ func (balances *IdentityBalances) BindForWithdrawEvents(sink chan<- *generated.I

type WithdrawRequest struct {
Amount *big.Int
Signature *registry.DecomposedSignature
Signature *identity.DecomposedSignature
}

const withDrawPrefix = "Withdraw request:"

func NewWithdrawRequest(identity *registry.MystIdentity, amount int64) (*WithdrawRequest, error) {
func NewWithdrawRequest(signer identity.Signer, amount int64) (*WithdrawRequest, error) {
bigAmount := big.NewInt(amount)
amountBytes := abi.U256(bigAmount)
signature, err := crypto.Sign(crypto.Keccak256([]byte(withDrawPrefix), amountBytes), identity.PrivateKey)
signature, err := signer.Sign([]byte(withDrawPrefix), amountBytes)
if err != nil {
return nil, err
}
decomposed, err := registry.DecomposeSignature(signature)
decomposed, err := identity.DecomposeSignature(signature)
if err != nil {
return nil, err
}
Expand Down
12 changes: 6 additions & 6 deletions balances/balances_test.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
package balances

import (
"math/big"
"testing"
"time"

"github.com/MysteriumNetwork/payments/balances/generated"
"github.com/MysteriumNetwork/payments/mysttoken"
generated2 "github.com/MysteriumNetwork/payments/mysttoken/generated"
"github.com/MysteriumNetwork/payments/registry"
"github.com/MysteriumNetwork/payments/test_utils"
"github.com/stretchr/testify/assert"
"math/big"
"testing"
"time"
)

var abiMap = test_utils.AbiMap{
Expand Down Expand Up @@ -41,7 +41,7 @@ func TestTopUpActionAddsMystToBalanceAndEmitsToppedUpEvent(t *testing.T) {
assert.NoError(t, err)
simulator.Commit()

mystIdentity, err := registry.NewMystIdentity()
mystIdentity, err := test_utils.NewMystIdentity()
assert.NoError(t, err)

initialBalance, err := identityBalances.Balances(mystIdentity.Address)
Expand Down Expand Up @@ -93,7 +93,7 @@ func TestWithdrawActionRemovesMystFromBalanceAndEmitsWithdrawnEvent(t *testing.T
assert.NoError(t, err)
simulator.Commit()

mystIdentity, err := registry.NewMystIdentity()
mystIdentity, err := test_utils.NewMystIdentity()
assert.NoError(t, err)

withdrawChan := make(chan *generated.IdentityBalancesWithdrawn, 10)
Expand Down
5 changes: 3 additions & 2 deletions cli/deployer/bin.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
package main

import (
"io/ioutil"
"strings"

"github.com/ethereum/go-ethereum/accounts/abi"
"github.com/ethereum/go-ethereum/accounts/abi/bind"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/types"
"io/ioutil"
"strings"
)

func DeploySmartContractFile(opts *bind.TransactOpts, path string, abiPath string, backend bind.ContractBackend, params ...interface{}) (common.Address, *types.Transaction, error) {
Expand Down
16 changes: 11 additions & 5 deletions cli/deployer/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,20 @@ import (
"errors"
"flag"
"fmt"
"github.com/MysteriumNetwork/payments/cli/helpers"
"github.com/ethereum/go-ethereum/common"
"math/big"
"os"

"github.com/MysteriumNetwork/payments/cli/helpers"
"github.com/ethereum/go-ethereum/common"
)

var cmd = flag.String("cmd", "help", "Command to execute")
var contractName = flag.String("contract.name", "settlement", "Name of contract to deply (settlement or testerc20(testnet only!))")
var tokenCount = flag.Int64("mysttoken.amount", 1000000, "Initial token amount to deploy - can always be minted later")
var registrationFee = flag.Int64("payments.registrationFee", 100, "Registration fee for identity. Can be changed later")
var erc20address = flag.String("payments.erc20address", "", "ERC20 token address for payments. In hex (0x...) format")
var contractPath = flag.String("payments.contractPath", "", "Path to bin file of payments contract")
var abiPath = flag.String("payments.abiPath", "", "Path to ABI file of payments contract")
var contractPath = flag.String("contract.binPath", "", "Path to bin file of payments contract")
var abiPath = flag.String("contract.abiPath", "", "Path to ABI file of payments contract")

func main() {
flag.Parse()
Expand Down Expand Up @@ -89,7 +90,12 @@ func deployContract() (err error) {
}
return DeployPromises(transactor, client, common.HexToAddress(*erc20address), *registrationFee)
case "testerc20":
return DeployTestErc20(transactor, client, *tokenCount)
addr, _, err := DeploySmartContractFile(transactor, *contractPath, *abiPath, client)
if err != nil {
return err
}
fmt.Println("Address of deployed contract: ", addr.String())
return nil
case "payments2":
addr, _, err := DeploySmartContractFile(transactor, *contractPath, *abiPath, client, common.HexToAddress(*erc20address), big.NewInt(*registrationFee))
if err != nil {
Expand Down
1 change: 1 addition & 0 deletions cli/deployer/promises.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package main

import (
"fmt"

"github.com/MysteriumNetwork/payments/mysttoken/generated"
"github.com/MysteriumNetwork/payments/promises"
"github.com/ethereum/go-ethereum/accounts/abi/bind"
Expand Down
1 change: 1 addition & 0 deletions cli/deployer/testerc20.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package main

import (
"fmt"

"github.com/MysteriumNetwork/payments/mysttoken"
"github.com/ethereum/go-ethereum/accounts/abi/bind"
)
Expand Down
3 changes: 2 additions & 1 deletion cli/helpers/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@ import (
"context"
"flag"
"fmt"
"time"

"github.com/cheggaaa/pb"
"github.com/ethereum/go-ethereum"
"github.com/ethereum/go-ethereum/ethclient"
"time"
)

var GethUrl = flag.String("geth.url", "", "URL value of started geth to connect")
Expand Down
1 change: 1 addition & 0 deletions cli/helpers/keystore.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"errors"
"flag"
"fmt"

"github.com/ethereum/go-ethereum/accounts"
"github.com/ethereum/go-ethereum/accounts/abi/bind"
"github.com/ethereum/go-ethereum/accounts/keystore"
Expand Down
5 changes: 3 additions & 2 deletions cli/minter/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,12 @@ import (
"context"
"flag"
"fmt"
"math/big"
"os"

"github.com/MysteriumNetwork/payments/cli/helpers"
"github.com/MysteriumNetwork/payments/mysttoken/generated"
"github.com/ethereum/go-ethereum/common"
"math/big"
"os"
)

var erc20contract = flag.String("erc20.address", "", "Address of ERC20 mintable token")
Expand Down
27 changes: 27 additions & 0 deletions identity/keystore_signer.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package identity

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 KeystoreSigner struct {
keystore *keystore.KeyStore
account accounts.Account
}

func NewKeystoreSigner(ks *keystore.KeyStore, identity common.Address) *KeystoreSigner {
return &KeystoreSigner{
keystore: ks,
account: accounts.Account{
Address: identity,
},
}
}

func (ki *KeystoreSigner) Sign(data ...[]byte) ([]byte, error) {
hash := crypto.Keccak256(data...)
return ki.keystore.SignHash(ki.account, hash)
}
38 changes: 38 additions & 0 deletions identity/keystore_signer_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package identity

import (
"os"
"testing"

"github.com/ethereum/go-ethereum/accounts/keystore"
"github.com/ethereum/go-ethereum/crypto"
"github.com/stretchr/testify/assert"
)

func TestKeyStoreSignerProvidesValidSignature(t *testing.T) {

assert.NoError(t, os.RemoveAll("testoutput"))

sampleKey, err := crypto.GenerateKey()
assert.NoError(t, err)

ks := keystore.NewKeyStore("testoutput", keystore.StandardScryptN, keystore.StandardScryptP)
acc, err := ks.ImportECDSA(sampleKey, "")
assert.NoError(t, err)

assert.NoError(t, ks.Unlock(acc, ""))
signer := NewKeystoreSigner(ks, acc.Address)

signData := []byte("Testing signature")

extractedSignature, err := signer.Sign(signData)
assert.NoError(t, err)

pubKeyBytes, err := crypto.Ecrecover(crypto.Keccak256(signData), extractedSignature)
assert.NoError(t, err)

recoveredIdentity := crypto.PubkeyToAddress(*crypto.ToECDSAPub(pubKeyBytes))

assert.Equal(t, acc.Address, recoveredIdentity)

}
2 changes: 1 addition & 1 deletion registry/signature.go → identity/signature.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package registry
package identity

import (
"errors"
Expand Down
5 changes: 3 additions & 2 deletions registry/signature_test.go → identity/signature_test.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
package registry
package identity

import (
"testing"

"github.com/ethereum/go-ethereum/common"
"github.com/stretchr/testify/assert"
"testing"
)

var testSignature = common.FromHex(
Expand Down
5 changes: 5 additions & 0 deletions identity/signer.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package identity

type Signer interface {
Sign(data ...[]byte) ([]byte, error)
}
3 changes: 2 additions & 1 deletion mysttoken/mysttoken.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
package mysttoken

import (
"math/big"

"github.com/MysteriumNetwork/payments/mysttoken/generated"
"github.com/ethereum/go-ethereum/accounts/abi/bind"
"github.com/ethereum/go-ethereum/common"
"math/big"
)

//go:generate abigen --sol ../contracts/MystToken.sol --pkg generated --out generated/mysttoken.go
Expand Down
5 changes: 3 additions & 2 deletions mysttoken/mysttoken_test.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
package mysttoken

import (
"math/big"
"testing"

"github.com/MysteriumNetwork/payments/mysttoken/generated"
"github.com/MysteriumNetwork/payments/test_utils"
"github.com/stretchr/testify/assert"
"math/big"
"testing"
)

var abiMap = test_utils.AbiMap{
Expand Down
18 changes: 10 additions & 8 deletions promises/clearing.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
package promises

import (
"math/big"

"github.com/MysteriumNetwork/payments/identity"
"github.com/MysteriumNetwork/payments/promises/generated"
"github.com/MysteriumNetwork/payments/registry"
"github.com/ethereum/go-ethereum/accounts/abi/bind"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/event"
"math/big"
)

//go:generate abigen --sol ../contracts/IdentityPromises.sol --exc contract/registry.sol:IdentityRegistry --pkg generated --out generated/IdentityPromises.go
Expand Down Expand Up @@ -36,17 +38,17 @@ func NewPromiseClearer(transactOpts *bind.TransactOpts, contract *generated.Iden
}
}

func (pc *PromiseClearing) RegisterIdentities(identities ...registry.MystIdentity) error {
func (pc *PromiseClearing) RegisterIdentities(identities ...registry.IdentityHolder) error {
for _, identity := range identities {
proof, err := registry.CreateProofOfIdentity(&identity)
registrationData, err := registry.CreateRegistrationData(identity)
if err != nil {
return err
}
sig := proof.Signature
sig := registrationData.Signature
var pubKeyPart1 [32]byte
var pubKeyPart2 [32]byte
copy(pubKeyPart1[:], proof.Data[0:32])
copy(pubKeyPart2[:], proof.Data[32:64])
copy(pubKeyPart1[:], registrationData.PublicKey.Part1)
copy(pubKeyPart2[:], registrationData.PublicKey.Part2)
_, err = pc.RegisterIdentity(pubKeyPart1, pubKeyPart2, sig.V, sig.R, sig.S)
if err != nil {
return err
Expand All @@ -56,11 +58,11 @@ func (pc *PromiseClearing) RegisterIdentities(identities ...registry.MystIdentit
}

func (pc *PromiseClearing) ClearReceivedPromise(promise *ReceivedPromise) error {
issuerSig, err := registry.DecomposeSignature(promise.IssuerSignature)
issuerSig, err := identity.DecomposeSignature(promise.IssuerSignature)
if err != nil {
return err
}
receiverSig, err := registry.DecomposeSignature(promise.ReceiverSignature)
receiverSig, err := identity.DecomposeSignature(promise.ReceiverSignature)
if err != nil {
return err
}
Expand Down
Loading

0 comments on commit 668aca9

Please sign in to comment.