Skip to content

Commit

Permalink
Merge pull request #21 from Bit-Nation/develop
Browse files Browse the repository at this point in the history
update master with latest development
  • Loading branch information
florianlenz authored May 21, 2018
2 parents db02bfd + 8533c58 commit dad2848
Show file tree
Hide file tree
Showing 8 changed files with 257 additions and 20 deletions.
69 changes: 54 additions & 15 deletions Readme.md
Original file line number Diff line number Diff line change
@@ -1,24 +1,63 @@
# Panthalassa
> Bitnation's backend - contains the mesh and some utils
# panthalassa

[![standard-readme compliant](https://img.shields.io/badge/standard--readme-OK-green.svg?style=flat-square)](https://github.com/RichardLitt/standard-readme)
[![Build Status](https://semaphoreci.com/api/v1/florianlenz/panthalassa/branches/develop/badge.svg)](https://semaphoreci.com/florianlenz/panthalassa) (Develop)
[![Build Status](https://semaphoreci.com/api/v1/florianlenz/panthalassa/branches/master/badge.svg)](https://semaphoreci.com/florianlenz/panthalassa) (Master)

> Backend for Pangea
## Development
TODO: Fill out this long description.

1. Clone the project into `$GOPATH/src/github.com/Bit-Nation/panthalassa`
2. Run `make` to see all available commands
## Table of Contents

### Install
1. Run `make deps` to get the needed dependencies
2. Run `make install` to install the gx dependencies
3. Run `make deps_hack` to rewrite the import paths
- [Security](#security)
- [Background](#background)
- [Install](#install)
- [Usage](#usage)
- [API](#api)
- [Maintainers](#maintainers)
- [Contribute](#contribute)
- [License](#license)

### Build for ios
- Follow the install section first
- Run `make ios` to build for ios
## Security
If you find a bug / vulnerability please DO NOT open an issue. Write to `[email protected]` PLEASE use [this](security-bitnation.co.key.pub) PGP key to encrypt your report / email.

### Build for android
- Follow the install section first
- Run `make android` to build for android
## Background
[Pangea](https://github.com/Bit-Nation/BITNATION-Pangea-mobile) is the mobile interface to our blockchain jurisdiction. While smart contract's are "onchain" (on a blockchain like Ethereum) communication happens offchain.
Since current chat systems like WhatsApp and Telegram are hevaly centralized, we are using a p2p system to send messages between peers so that bitnaiton doesn't become a central point of failure.
We are using [libp2p](https://github.com/libp2p) for the p2p network, which is a great project.

## Install

First clone the project. You can run the commands from the `Usage` section.

## Usage
We are using GX as the dependency manager since libp2p (and almost all go projects from [Protocol Labs](https://protocol.ai/)) use it as the dependecny manager.
However, you don't need to pay attention to it, since you just have to use the make file. The following commands are available:
- `make list` (or just `make`) will list all commands from the Makefile.
- `make deps` will fetch tools that you need in order to work with the project.
- `make install` will install all dependencies needed in order to work with the project.
- `make deps_hack` will "hack" your dependencies. GX rewrites your import paths `github.com/libp2p/go-libp2p` e.g. becomes `gx/ipfs/QmNh1kGFFdsPu79KNSaL4NUKUPb4Eiz4KHdMtFY6664RDp/go-libp2p`. You need this in order to work with the package versions specified in the package.json.
- `make deps_hack_revert` will undo `make deps_hack`. We never want to commit the GX import paths.
- `make deps_mobile` will install some tools needed to build panthalassa for mobile. You need to run this before you can build for ios and android.
- `make ios` will build panthalassa for ios and place it in the `build` folder.
- `make android` will build panthalassa for android and place it in the build folder.
- `make test` will format the code and run all tests.
- `make test_coverage` will test the code and open the coverage report.

## API
> TODO - add link to godoc.org
## Maintainers

[@florianlenz](https://github.com/florianlenz)

## Contribute

Pull requests are accepted.

Small note: If editing the README, please conform to the [standard-readme](https://github.com/RichardLitt/standard-readme) specifications.

## License

MIT © 2018 [email protected]
43 changes: 43 additions & 0 deletions keyManager/keyManager.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
package keyManager

import (
"encoding/hex"
"encoding/json"
"errors"

scrypt "github.com/Bit-Nation/panthalassa/crypto/scrypt"
ks "github.com/Bit-Nation/panthalassa/keyStore"
ethereumMigration "github.com/Bit-Nation/panthalassa/keyStore/migration/ethereum"
identity "github.com/Bit-Nation/panthalassa/keyStore/migration/identity"
"github.com/Bit-Nation/panthalassa/mnemonic"
ethCrypto "github.com/ethereum/go-ethereum/crypto"
lp2pCrypto "github.com/libp2p/go-libp2p-crypto"
)

type KeyManager struct {
Expand Down Expand Up @@ -126,6 +130,45 @@ func (km KeyManager) GetEthereumAddress() (string, error) {
return ethCrypto.PubkeyToAddress(priv.PublicKey).String(), nil
}

func (km KeyManager) IdentityPrivateKey() (string, error) {
return km.keyStore.GetKey(identity.Ed25519PrivateKey)
}

func (km KeyManager) IdentityPublicKey() (string, error) {
return km.keyStore.GetKey(identity.Ed25519PublicKey)
}

func (km KeyManager) GetMnemonic() mnemonic.Mnemonic {
return km.keyStore.GetMnemonic()
}

//Get the Mesh network private key (which is the identity ed25519 private key)
func (km KeyManager) MeshPrivateKey() (lp2pCrypto.PrivKey, error) {

//Fetch private key
priv, err := km.IdentityPrivateKey()
if err != nil {
return nil, err
}
privBytes, err := hex.DecodeString(priv)
if err != nil {
return nil, err
}

//Fetch public key
pub, err := km.IdentityPublicKey()
if err != nil {
return nil, err
}
pubBytes, err := hex.DecodeString(pub)
if err != nil {
return nil, err
}

return lp2pCrypto.UnmarshalEd25519PrivateKey(append(privBytes, pubBytes...))

}

//Did the keystore change (happen after migration)
func (km KeyManager) WasMigrated() bool {
return km.keyStore.WasMigrated()
Expand Down
61 changes: 56 additions & 5 deletions keyManager/keyManager_test.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
package keyManager

import (
keyStore "github.com/Bit-Nation/panthalassa/keyStore"
"github.com/Bit-Nation/panthalassa/mnemonic"
"github.com/stretchr/testify/require"
"encoding/hex"
"testing"

keyStore "github.com/Bit-Nation/panthalassa/keyStore"
identity "github.com/Bit-Nation/panthalassa/keyStore/migration/identity"
mnemonic "github.com/Bit-Nation/panthalassa/mnemonic"
require "github.com/stretchr/testify/require"
)

//Test the Create from function
Expand All @@ -26,7 +29,7 @@ func TestCreateFromKeyStore(t *testing.T) {
func TestExportFunction(t *testing.T) {

//create key storage
jsonKeyStore := `{"mnemonic":"differ destroy head candy imitate barely wine ranch roof barrel sheriff blame umbrella visit sell green dress embark ramp cement rotate crawl session broom","keys":{"ethereum_private_key":"eba47c97d7a6688d03e41b145d26090216c4468231bb46677553141f75222d5c"},"version":1}`
jsonKeyStore := `{"mnemonic":"differ destroy head candy imitate barely wine ranch roof barrel sheriff blame umbrella visit sell green dress embark ramp cement rotate crawl session broom","keys":{"ed_25519_private_key":"9d426d0eb4170529672df197454bc77cc36cb341c872bcee0bece79ac893b34a8c5de2e7d099b881ed6214f8add6cbba2a84f57546b7f0a6d39197c904529f3f","ed_25519_public_key":"8c5de2e7d099b881ed6214f8add6cbba2a84f57546b7f0a6d39197c904529f3f","ethereum_private_key":"eba47c97d7a6688d03e41b145d26090216c4468231bb46677553141f75222d5c"},"version":1}`
ks, err := keyStore.UnmarshalStore(jsonKeyStore)
require.Nil(t, err)

Expand All @@ -51,7 +54,7 @@ func TestExportFunction(t *testing.T) {
func TestOpenWithMnemonic(t *testing.T) {

//create key storage
jsonKeyStore := `{"mnemonic":"differ destroy head candy imitate barely wine ranch roof barrel sheriff blame umbrella visit sell green dress embark ramp cement rotate crawl session broom","keys":{"ethereum_private_key":"eba47c97d7a6688d03e41b145d26090216c4468231bb46677553141f75222d5c"},"version":1}`
jsonKeyStore := `{"mnemonic":"differ destroy head candy imitate barely wine ranch roof barrel sheriff blame umbrella visit sell green dress embark ramp cement rotate crawl session broom","keys":{"ed_25519_private_key":"9d426d0eb4170529672df197454bc77cc36cb341c872bcee0bece79ac893b34a8c5de2e7d099b881ed6214f8add6cbba2a84f57546b7f0a6d39197c904529f3f","ed_25519_public_key":"8c5de2e7d099b881ed6214f8add6cbba2a84f57546b7f0a6d39197c904529f3f","ethereum_private_key":"eba47c97d7a6688d03e41b145d26090216c4468231bb46677553141f75222d5c"},"version":1}`
ks, err := keyStore.UnmarshalStore(jsonKeyStore)
require.Nil(t, err)

Expand All @@ -74,6 +77,22 @@ func TestOpenWithMnemonic(t *testing.T) {

}

func TestGetMnemonic(t *testing.T) {

//create key storage
jsonKeyStore := `{"mnemonic":"differ destroy head candy imitate barely wine ranch roof barrel sheriff blame umbrella visit sell green dress embark ramp cement rotate crawl session broom","keys":{"ethereum_private_key":"eba47c97d7a6688d03e41b145d26090216c4468231bb46677553141f75222d5c"},"version":1}`
ks, err := keyStore.UnmarshalStore(jsonKeyStore)
require.Nil(t, err)

//key manager
km := CreateFromKeyStore(ks)

//Get address
mne := km.GetMnemonic()
require.Equal(t, "differ destroy head candy imitate barely wine ranch roof barrel sheriff blame umbrella visit sell green dress embark ramp cement rotate crawl session broom", mne.String())

}

func TestGetAddressFromPrivateKey(t *testing.T) {

//create key storage
Expand All @@ -90,3 +109,35 @@ func TestGetAddressFromPrivateKey(t *testing.T) {
require.Equal(t, "0x748A6536dE0a8b1902f808233DD75ec4451cdFC6", addr)

}

func TestGetLibP2PPrivateKey(t *testing.T) {

//create key storage
jsonKeyStore := `{"mnemonic":"differ destroy head candy imitate barely wine ranch roof barrel sheriff blame umbrella visit sell green dress embark ramp cement rotate crawl session broom","keys":{"ethereum_private_key":"eba47c97d7a6688d03e41b145d26090216c4468231bb46677553141f75222d5c"},"version":1}`
ks, err := keyStore.UnmarshalStore(jsonKeyStore)
require.Nil(t, err)

//key manager
km := CreateFromKeyStore(ks)

meshPriv, err := km.MeshPrivateKey()
require.Nil(t, err)

edPriv, err := ks.GetKey(identity.Ed25519PrivateKey)
require.Nil(t, err)
edPrivBytes, err := hex.DecodeString(edPriv)
require.Nil(t, err)

edPub, err := ks.GetKey(identity.Ed25519PublicKey)
require.Nil(t, err)
edPubBytes, err := hex.DecodeString(edPub)
require.Nil(t, err)

//Check if private key does match
meshPrivBytes, err := meshPriv.Bytes()
require.Nil(t, err)
combinedKey := append(edPrivBytes, edPubBytes...)
preFix, err := hex.DecodeString("08011260")
require.Nil(t, err)
require.Equal(t, hex.EncodeToString(append(preFix, combinedKey...)), hex.EncodeToString(meshPrivBytes))
}
2 changes: 2 additions & 0 deletions keyStore/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,14 @@ import (

migration "github.com/Bit-Nation/panthalassa/keyStore/migration"
ethereumMigration "github.com/Bit-Nation/panthalassa/keyStore/migration/ethereum"
ed25519Migration "github.com/Bit-Nation/panthalassa/keyStore/migration/identity/ed25519"
mnemonic "github.com/Bit-Nation/panthalassa/mnemonic"
)

//Migrations to run
var migrations = []migration.Migration{
ethereumMigration.Migration{},
ed25519Migration.Migration{},
}

type Store struct {
Expand Down
32 changes: 32 additions & 0 deletions mesh/mesh.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,37 @@
package mesh

import (
"context"
lp2p "github.com/libp2p/go-libp2p"
lp2pCrypto "github.com/libp2p/go-libp2p-crypto"
host "github.com/libp2p/go-libp2p-host"
)

func New(meshPk *lp2pCrypto.Ed25519PrivateKey) (*Network, error) {

//Create host
h, err := lp2p.New(context.Background(), func(cfg *lp2p.Config) error {

cfg.PeerKey = meshPk
cfg.DisableSecio = false

return lp2p.Defaults(cfg)

})
if err != nil {
return nil, err
}

return &Network{
host: h,
}, nil

}

type Network struct {
host host.Host
}

/*
@todo there is an problem with the bootstrapping bundle. Since we don't need the mesh network now we will comment it
import (
Expand Down
12 changes: 12 additions & 0 deletions mobile/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,18 @@ func ExportAccountStore(pw, pwConfirm string) (string, error) {

}

// get mnemonic
func GetMnemonic() (string, error) {

// exit if not started
if panthalassaInstance == nil {
return "", errors.New("you have to start panthalassa in order to stop it")
}

return panthalassaInstance.km.GetMnemonic().String(), nil

}

//Stop panthalassa
func Stop() error {

Expand Down
6 changes: 6 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,12 @@
"hash": "Qmdu32Lzvuof9HXogmHQZ8JJuhaWyADBwrRSmpXjGFX8HM",
"name": "go-libp2p-bootstrap",
"version": "3.0.2"
},
{
"author": "whyrusleeping",
"hash": "QmaPbCnUMBohSGo3KnxEa2bHqyJVVeEEcwtqJAYxerieBo",
"name": "go-libp2p-crypto",
"version": "1.5.0"
}
],
"gxVersion": "0.12.1",
Expand Down
52 changes: 52 additions & 0 deletions security-bitnation.co.key.pub
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
-----BEGIN PGP PUBLIC KEY BLOCK-----

mQINBFoxGfcBEADCq6ZyJYeF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