Skip to content

Commit

Permalink
Merge pull request #49 from persistenceOne/avkr003/v0.6.0
Browse files Browse the repository at this point in the history
Merging audit report changes, direct staking feature, test cases improvement and bug fixes
  • Loading branch information
avkr003 authored Jan 7, 2022
2 parents cc56762 + 56c686a commit 2b1154e
Show file tree
Hide file tree
Showing 110 changed files with 6,617 additions and 1,538 deletions.
62 changes: 58 additions & 4 deletions .script/compileSC.sh
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,7 +1,61 @@
#!/bin/bash

solc --abi --bin contracts/LiquidStakingV2.sol -o build
abigen --bin ./build/LiquidStakingV2.bin --abi ./build/LiquidStakingV2.abi --pkg=liquidStaking --out=liquidStaking.go
OPENZEPPELIN_DIR_NAME="@openzeppelin"

solc --abi --bin contracts/TokenWrapperV2.sol -o build --overwrite
abigen --bin ./build/TokenWrapperV2.bin --abi ./build/TokenWrapperV2.abi --pkg=tokenWrapper --out=TokenWrapper.go
rm -rf pStake-smartContracts
rm -rf "$OPENZEPPELIN_DIR_NAME"
rm -rf ./ethereum/abi/liquidStaking/liquidStaking.go
rm -rf ./ethereum/abi/tokenWrapper/tokenWrapper.go

echo "Downloading pStake-smartContracts..."
git clone [email protected]:persistenceOne/pStake-smartContracts.git
# shellcheck disable=SC2164
cd pStake-smartContracts
git checkout "$SMART_CONTRACT_VERSION"
# shellcheck disable=SC2103
cd ..

echo "Downloading openzeppelin..."
mkdir "$OPENZEPPELIN_DIR_NAME"
# shellcheck disable=SC2164
cd "$OPENZEPPELIN_DIR_NAME"

echo "Downloading openzeppelin-contracts..."
git clone https://github.com/OpenZeppelin/openzeppelin-contracts.git
# shellcheck disable=SC2164
cd openzeppelin-contracts
git checkout "$OPENZEPPELIN_VERSION"
cd ..
mv openzeppelin-contracts/contracts ./
rm -rf openzeppelin-contracts

echo "Downloading openzeppelin-contract-upgradeable..."
git clone https://github.com/OpenZeppelin/openzeppelin-contracts-upgradeable.git
# shellcheck disable=SC2164
cd openzeppelin-contracts-upgradeable
git checkout "$OPENZEPPELIN_VERSION"
cd ..
mv openzeppelin-contracts-upgradeable/contracts ./contracts-upgradeable
rm -rf openzeppelin-contracts-upgradeable

cd ..
mv "$OPENZEPPELIN_DIR_NAME" ./pStake-smartContracts/
# shellcheck disable=SC2164
cd pStake-smartContracts

echo "Compiling smart contracts..."
# shellcheck disable=SC2046
solc --abi --bin contracts/"$LIQUID_STAKING".sol -o build @openzeppelin/=$(pwd)/@openzeppelin/
abigen --bin ./build/"$LIQUID_STAKING".bin --abi ./build/"$LIQUID_STAKING".abi --pkg=liquidStaking --out=liquidStaking.go

# shellcheck disable=SC2046
solc --abi --bin contracts/"$TOKEN_WRAPPER".sol -o build --overwrite @openzeppelin/=$(pwd)/@openzeppelin/
abigen --bin ./build/"$TOKEN_WRAPPER".bin --abi ./build/"$TOKEN_WRAPPER".abi --pkg=tokenWrapper --out=tokenWrapper.go

cd ..
mv pStake-smartContracts/liquidStaking.go ./ethereum/abi/liquidStaking
mv pStake-smartContracts/tokenWrapper.go ./ethereum/abi/tokenWrapper

rm -rf pStake-smartContracts

echo "Compilation done."
5 changes: 2 additions & 3 deletions .script/startup.sh
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
#!/bin/bash


KAFKA_VERSION=2.8.0
KAFKA_VERSION=2.8.1
KAFKA_FOLDER=kafka_2.13-"$KAFKA_VERSION"

if [ -d $KAFKA_FOLDER ]; then
echo "File exists"
else
pwd
echo "File does not exist, downloading $KAFKA_VERSION"
wget http://mirrors.estointernet.in/apache/kafka/"$KAFKA_VERSION"/"$KAFKA_FOLDER".tgz
wget https://dlcdn.apache.org/kafka/"$KAFKA_VERSION"/"$KAFKA_FOLDER".tgz
tar -xzf "$KAFKA_FOLDER".tgz
rm "$KAFKA_FOLDER".tgz
fi
Expand Down
32 changes: 23 additions & 9 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
export GO111MODULE=on

VERSION := $(shell echo $(shell git describe --tags) | sed 's/^v//')
TM_VERSION := $(shell go list -m github.com/tendermint/tendermint | sed 's:.* ::')
COMMIT := $(shell git rev-parse --short HEAD)
export SMART_CONTRACT_VERSION=fbda08567aac3acd004e36dfe670a44253453907
export LIQUID_STAKING=LiquidStakingV3
export TOKEN_WRAPPER=TokenWrapperV3
export OPENZEPPELIN_VERSION=v3.4.2

VERSION := $(shell echo $(shell git describe --always) | sed 's/^v//')

build_tags = netgo

Expand All @@ -13,12 +16,7 @@ whitespace := $(subst ,, )
comma := ,
build_tags_comma_sep := $(subst $(whitespace),$(comma),$(build_tags))

ldflags = -X github.com/cosmos/cosmos-sdk/version.Name=persistenceBridge \
-X github.com/cosmos/cosmos-sdk/version.AppName=persistenceBridge \
-X github.com/cosmos/cosmos-sdk/version.Version=$(VERSION) \
-X github.com/cosmos/cosmos-sdk/version.Commit=$(COMMIT) \
-X github.com/tendermint/tendermint/version.TMCoreSemVer=$(TM_VERSION) \
-X github.com/cosmos/cosmos-sdk/version.BuildTags=$(build_tags_comma_sep)
ldflags = -X github.com/persistenceOne/persistenceBridge/application/commands.Version=$(VERSION)

ifeq (cleveldb,$(findstring cleveldb,$(build_tags)))
ldflags += -X github.com/cosmos/cosmos-sdk/types.DBBackend=cleveldb
Expand All @@ -41,6 +39,7 @@ GOOS = $(shell go env GOOS)
all: verify install

install:
.script/compileSC.sh
ifeq (${OS},Windows_NT)
go build -mod=readonly ${BUILD_FLAGS} -o ${GOBIN}/persistenceBridge.exe ./orchestrator

Expand All @@ -50,6 +49,7 @@ else
endif

build:
.script/compileSC.sh
ifeq (${OS},Windows_NT)
go build ${BUILD_FLAGS} -o build/${GOOS}/${GOARCH}/persistenceBridge.exe ./orchestrator

Expand All @@ -71,3 +71,17 @@ else
endif


test:
@go test ./application/casp
@go test ./application/commands
@go test ./application/configuration
@go test ./application/db
@go test ./application/outgoingTx
@go test ./application/rest
@go test ./application/rpc
@go test ./application/shutdown

@go test ./ethereum/contracts
@go test ./ethereum

@go test ./tendermint
8 changes: 6 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ relays transactions between a Cosmos-SDK chain and ethereum.

## Installation Steps

> Prerequisite: go1.15+ required. [ref](https://golang.org/doc/install)
> Prerequisite: go1.16+ required. [ref](https://golang.org/doc/install)
> Prerequisite: git. [ref](https://github.com/git/git)
Expand Down Expand Up @@ -59,6 +59,7 @@ checking from last checked height + 1
```shell
persistenceBridge init
```
Start the `kafka` server. The starting script is available at `.script/startup.sh`

This generates a `config.toml` file in `$HOME/.persistenceBridge/` with empty and default values. Update this file as per configuration (Telegram configuration is not compulsory).

Expand All @@ -67,10 +68,13 @@ Add participating validators:
persistenceBridge add [validator_address] [validator_name]
```

Before starting the bridge, the `wrap address` (on cosmos chain) and `bridge admin` (on eth chain) should have some token balance otherwise bridge won't be able to do tx. Both the address can be queried (after all the configuration has been filled) by:
`persistenceBridge show`

Now when starting for the first time:

```shell
persistenceBridge start --tmStart 1 --ethStart 4772131
persistenceBridge start --tmStart [START_HEIGHT_ON_COSMOS_CHAIN] --ethStart [START_HEIGHT_ON_ETHEREUM_CHAIN]
```

When starting next time
Expand Down
8 changes: 4 additions & 4 deletions application/casp/address.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ func GetEthAddress() (common.Address, error) {
if err != nil {
return common.Address{}, err
}
if len(uncompressedPublicKeys.Items) == 0 {
return common.Address{}, fmt.Errorf("no eth public keys got from casp")
if len(uncompressedPublicKeys.Items) != 1 {
return common.Address{}, fmt.Errorf("no or more than 1 eth public keys got from casp")
}
publicKey := GetEthPubKey(uncompressedPublicKeys.Items[0])
return crypto.PubkeyToAddress(publicKey), nil
Expand All @@ -30,8 +30,8 @@ func GetTendermintAddress() (sdk.AccAddress, error) {
if err != nil {
return nil, err
}
if len(uncompressedPublicKeys.Items) == 0 {
return nil, fmt.Errorf("no tendermint public keys got from casp")
if len(uncompressedPublicKeys.Items) != 1 {
return nil, fmt.Errorf("no or more than 1 tendermint public keys got from casp")
}
tmPublicKey := GetTMPubKey(uncompressedPublicKeys.Items[0])
return sdk.AccAddress(tmPublicKey.Address()), nil
Expand Down
7 changes: 2 additions & 5 deletions application/casp/address_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,14 @@
package casp

import (
"github.com/persistenceOne/persistenceBridge/application/configuration"
test "github.com/persistenceOne/persistenceBridge/utilities/testing"
"github.com/stretchr/testify/require"
"regexp"
"testing"
)

func TestGetEthAddress(t *testing.T) {
configuration.InitConfig()
configuration.SetConfig(test.GetCmdWithConfig())
test.SetTestConfig()
ethAddress, err := GetEthAddress()
re := regexp.MustCompile(`^0x[0-9a-fA-F]{40}$`)
require.Nil(t, err)
Expand All @@ -25,8 +23,7 @@ func TestGetEthAddress(t *testing.T) {
}

func TestGetTendermintAddress(t *testing.T) {
configuration.InitConfig()
configuration.SetConfig(test.GetCmdWithConfig())
test.SetTestConfig()
tenderMintAddress, errTMA := GetTendermintAddress()
re := regexp.MustCompile(`^cosmos[0-9a-zA-Z]{39}$`)
require.Nil(t, errTMA, "Error Getting Tendermint address")
Expand Down
33 changes: 21 additions & 12 deletions application/casp/publicKey.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,48 +8,57 @@ package casp
import (
"crypto/ecdsa"
"encoding/hex"
"fmt"
"math/big"
"strings"

"github.com/btcsuite/btcd/btcec"
"github.com/cosmos/cosmos-sdk/crypto/keys/secp256k1"
cryptotypes "github.com/cosmos/cosmos-sdk/crypto/types"
"github.com/ethereum/go-ethereum/crypto"
"github.com/persistenceOne/persistenceBridge/utilities/logging"
"math/big"
)

// GetTMPubKey Should include prefix "04"
// GetTMPubKey caspPubKey should include prefix "04"
func GetTMPubKey(caspPubKey string) cryptotypes.PubKey {
x, y := getXY(caspPubKey)

pubKey := ecdsa.PublicKey{
Curve: btcec.S256(),
X: &x,
Y: &y,
X: x,
Y: y,
}
pubkeyObject := (*btcec.PublicKey)(&pubKey)
pk := pubkeyObject.SerializeCompressed()
return &secp256k1.PubKey{Key: pk}
}

// GetEthPubKey Should include prefix "04"
// GetEthPubKey caspPubKey should include prefix "04"
func GetEthPubKey(caspPubKey string) ecdsa.PublicKey {
x, y := getXY(caspPubKey)
publicKey := ecdsa.PublicKey{
Curve: crypto.S256(),
X: &x,
Y: &y,
X: x,
Y: y,
}
return publicKey
}

// getXY Should include prefix "04"
func getXY(caspPubKey string) (big.Int, big.Int) {
// getXY caspPubKey should include prefix "04"
func getXY(caspPubKey string) (x, y *big.Int) {
s := strings.Split(caspPubKey, "")
if s[0] != "0" && s[1] != "4" {
logging.Fatal("invalid casp public key")
}

pubKeyBytes, err := hex.DecodeString(string([]rune(caspPubKey)[2:])) // uncompressed pubkey
if err != nil {
logging.Fatal(err)
}
var x big.Int
if len(pubKeyBytes) != 64 {
logging.Fatal(fmt.Sprintf("invalid casp public key, length (%v) not equal to 64", len(pubKeyBytes)))
}
x.SetBytes(pubKeyBytes[0:32])
var y big.Int
y.SetBytes(pubKeyBytes[32:])
return x, y
return
}
37 changes: 11 additions & 26 deletions application/casp/publicKey_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,59 +6,44 @@
package casp

import (
"crypto/ecdsa"
"github.com/cosmos/cosmos-sdk/crypto/types"
"github.com/ethereum/go-ethereum/crypto"
"github.com/persistenceOne/persistenceBridge/application/configuration"
caspQueries "github.com/persistenceOne/persistenceBridge/application/rest/casp"
test "github.com/persistenceOne/persistenceBridge/utilities/testing"
"github.com/stretchr/testify/require"
"math/big"
"reflect"
"regexp"
"testing"
)

func TestGetTMPubKey(t *testing.T) {
configuration.InitConfig()
configuration.SetConfig(test.GetCmdWithConfig())
test.SetTestConfig()

uncompressedPublicKeys, err := caspQueries.GetUncompressedEthPublicKeys()
uncompressedPublicKeys, err := caspQueries.GetUncompressedTMPublicKeys()
require.Nil(t, err, "Failed to get casp Response")
tmpKey := GetTMPubKey(uncompressedPublicKeys.PublicKeys[0])
tmpKey := GetTMPubKey(uncompressedPublicKeys.Items[0])
re := regexp.MustCompile(`^PubKeySecp256k1{+[0-9a-fA-F]+}$`)
require.Equal(t, 20, len(tmpKey.Address().Bytes()))
require.Equal(t, reflect.TypeOf(types.Address{}), reflect.TypeOf(tmpKey.Address()))
require.Equal(t, true, re.MatchString(tmpKey.String()), "TM Public Key regex not matching")
require.NotNil(t, tmpKey)
}

func TestGetEthPubKey(t *testing.T) {
configuration.InitConfig()
configuration.SetConfig(test.GetCmdWithConfig())
test.SetTestConfig()

uncompressedPublicKeys, err := caspQueries.GetUncompressedEthPublicKeys()
require.Nil(t, err, "Failed to get casp Response")
ethPubliKey := uncompressedPublicKeys.PublicKeys[0]
ethKey := GetEthPubKey(ethPubliKey)
require.Equal(t, 20, len(crypto.PubkeyToAddress(ethKey)))
require.Equal(t, reflect.TypeOf(ecdsa.PublicKey{}), reflect.TypeOf(ethKey))
require.Equal(t, reflect.TypeOf(&big.Int{}), reflect.TypeOf(ethKey.X))
require.Equal(t, reflect.TypeOf(&big.Int{}), reflect.TypeOf(ethKey.Y))
require.NotNil(t, ethKey)
ethPublicKey := GetEthPubKey(uncompressedPublicKeys.Items[0])
require.Equal(t, 20, len(crypto.PubkeyToAddress(ethPublicKey)))
require.NotNil(t, ethPublicKey)
}

func TestGetXY(t *testing.T) {
configuration.InitConfig()
configuration.SetConfig(test.GetCmdWithConfig())
test.SetTestConfig()

uncompressedPublicKeys, err := caspQueries.GetUncompressedEthPublicKeys()
require.Nil(t, err, "Failed to get casp Response")
x, y := getXY(uncompressedPublicKeys.PublicKeys[0])
require.Equal(t, 32, len(y.Bytes()))
require.Equal(t, 32, len(y.Bytes()))
require.Equal(t, reflect.TypeOf(big.Int{}), reflect.TypeOf(x))
require.Equal(t, reflect.TypeOf(big.Int{}), reflect.TypeOf(y))
x, y := getXY(uncompressedPublicKeys.Items[0])
require.NotNil(t, x)
require.NotNil(t, y)
require.LessOrEqual(t, 32, len(x.Bytes()))
require.LessOrEqual(t, 32, len(y.Bytes()))
}
Loading

0 comments on commit 2b1154e

Please sign in to comment.