Skip to content

Commit

Permalink
Merge branch 'feature/notary' into feature/old_vm
Browse files Browse the repository at this point in the history
  • Loading branch information
borjantrajanoski authored Sep 28, 2018
2 parents b936d95 + 3048611 commit 452f71d
Show file tree
Hide file tree
Showing 9 changed files with 2,009 additions and 19 deletions.
383 changes: 375 additions & 8 deletions Gopkg.lock

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion Gopkg.toml
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@

[[constraint]]
name = "github.com/ethereum/go-ethereum"
version = "1.8.11"
version = "1.8.15"

[[constraint]]
branch = "master"
Expand Down
5 changes: 2 additions & 3 deletions api/dapp.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,14 +66,13 @@ func (a *DAppApi) SendEthereumTransaction(value, to, data string) (string, error
objTx := map[string]interface{}{
"nonce": ethTx.Nonce,
"gasPrice": ethTx.GasPrice,
"gasLimit": ethTx.GasLimit,
"gas": ethTx.GasLimit,
"to": ethTx.To,
"value": ethTx.Value,
"data": ethTx.Data,
"input": ethTx.Data,
"v": ethTx.V,
"r": ethTx.R,
"s": ethTx.S,
"chainId": ethTx.ChainID,
"from": ethTx.From,
"hash": ethTx.Hash,
}
Expand Down
1 change: 1 addition & 0 deletions db/bolt_to_storm.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"errors"
"time"


aes "github.com/Bit-Nation/panthalassa/crypto/aes"
keyManager "github.com/Bit-Nation/panthalassa/keyManager"
queue "github.com/Bit-Nation/panthalassa/queue"
Expand Down
100 changes: 100 additions & 0 deletions documents/call.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@ import (
"encoding/base64"
"errors"
"time"

keyManager "github.com/Bit-Nation/panthalassa/keyManager"
bind "github.com/ethereum/go-ethereum/accounts/abi/bind"
common "github.com/ethereum/go-ethereum/common"
cid "github.com/ipfs/go-cid"
mh "github.com/multiformats/go-multihash"
)

type DocumentCreateCall struct {
Expand Down Expand Up @@ -97,6 +103,9 @@ func (c *DocumentAllCall) Handle(data map[string]interface{}) (map[string]interf
"mime_type": d.MimeType,
"description": d.Description,
"title": d.Title,
"hash": d.CID,
"signature": d.Signature,
"tx_hash": d.TransactionHash,
})
}
return map[string]interface{}{
Expand Down Expand Up @@ -183,3 +192,94 @@ func (d *DocumentDeleteCall) Handle(data map[string]interface{}) (map[string]int
return map[string]interface{}{}, d.s.db.DeleteStruct(&doc)

}

type DocumentSubmitCall struct {
s *Storage
km *keyManager.KeyManager
n *NotaryMulti
}

func NewDocumentNotariseCall(s *Storage, km *keyManager.KeyManager, n *NotaryMulti) *DocumentSubmitCall {
return &DocumentSubmitCall{
s: s,
km: km,
n: n,
}
}

func (d *DocumentSubmitCall) CallID() string {
return "DOCUMENT:NOTARISE"
}

func (d *DocumentSubmitCall) Validate(map[string]interface{}) error {
return nil
}

func (d *DocumentSubmitCall) Handle(data map[string]interface{}) (map[string]interface{}, error) {

docID, k := data["doc_id"].(float64)
if !k {
return map[string]interface{}{}, errors.New("expect doc_id to be an integer")
}

var doc Document
if err := d.s.db.One("ID", int(docID), &doc); err != nil {
return map[string]interface{}{}, err
}

// decrypt document content
docContent, err := d.km.AESDecrypt(doc.EncryptedContent)
if err != nil {
return map[string]interface{}{}, err
}

// assign plain document content
doc.Content = docContent


// hash document
docHash, err := mh.Sum(doc.Content, mh.SHA2_256, -1)
if err != nil {
return map[string]interface{}{}, err
}

docContentCID := cid.NewCidV1(cid.Raw, docHash).Bytes()

// attach cid to document
doc.CID = docContentCID

// sign cid
cidSignature, err := d.km.IdentitySign(docContentCID)
if err != nil {
return map[string]interface{}{}, err
}

// attach signature to doc
doc.Signature = cidSignature

ethAddr, err := d.km.GetEthereumAddress()
if err != nil {
return map[string]interface{}{}, err
}

// fetch the notary fee
notaryFee, err := d.n.NotaryFee(nil)
if err != nil {
return nil, err
}

// submit tx to chain
tx, err := d.n.NotarizeTwo(&bind.TransactOpts{
From: common.HexToAddress(ethAddr),
Signer: d.km.SignEthTx,
Value: notaryFee,
}, docContentCID, cidSignature)
if err != nil {
return map[string]interface{}{}, err
}

doc.TransactionHash = tx.Hash().Hex()

return map[string]interface{}{}, d.s.Save(&doc)

}
1,388 changes: 1,388 additions & 0 deletions documents/notary_contract.go

Large diffs are not rendered by default.

9 changes: 6 additions & 3 deletions documents/storage.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package documents

import (
"github.com/Bit-Nation/panthalassa/crypto/aes"
"github.com/Bit-Nation/panthalassa/keyManager"
"github.com/asdine/storm"
aes "github.com/Bit-Nation/panthalassa/crypto/aes"
keyManager "github.com/Bit-Nation/panthalassa/keyManager"
storm "github.com/asdine/storm"
)

type Document struct {
Expand All @@ -14,6 +14,9 @@ type Document struct {
Content []byte `json:"-"`
EncryptedContent aes.CipherText
CreatedAt int64
CID []byte
Signature []byte
TransactionHash string
}

type Storage struct {
Expand Down
97 changes: 97 additions & 0 deletions keyManager/keyManager.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ import (
"encoding/hex"
"encoding/json"
"errors"
"strconv"

api "github.com/Bit-Nation/panthalassa/api"
aes "github.com/Bit-Nation/panthalassa/crypto/aes"
scrypt "github.com/Bit-Nation/panthalassa/crypto/scrypt"
ks "github.com/Bit-Nation/panthalassa/keyStore"
Expand All @@ -14,6 +16,8 @@ import (
identity "github.com/Bit-Nation/panthalassa/keyStore/migration/identity"
mnemonic "github.com/Bit-Nation/panthalassa/mnemonic"
x3dh "github.com/Bit-Nation/x3dh"
common "github.com/ethereum/go-ethereum/common"
types "github.com/ethereum/go-ethereum/core/types"
ethCrypto "github.com/ethereum/go-ethereum/crypto"
lp2pCrypto "github.com/libp2p/go-libp2p-crypto"
ed25519 "golang.org/x/crypto/ed25519"
Expand All @@ -22,6 +26,7 @@ import (
type KeyManager struct {
keyStore ks.Store
account Store
Api *api.API
}

type Store struct {
Expand Down Expand Up @@ -277,6 +282,98 @@ func (km KeyManager) AESEncrypt(plainText aes.PlainText) (aes.CipherText, error)
return aes.CTREncrypt(plainText, aesSecret)
}

func (km KeyManager) SignEthTx(signer types.Signer, addresses common.Address, tx *types.Transaction) (*types.Transaction, error) {

submittedTx, err := km.Api.SendEthereumTransaction(
tx.Value().String(),
tx.To().String(),
hex.EncodeToString(tx.Data()),
)
if err != nil {
return nil, err
}

// convert to ethereum hex string
numToHex := func(txData map[string]interface{}, toTransform string) (string, error) {

gasPriceStr, ok := txData["gasPrice"].(string)
if ok {
return "", errors.New("gas price must be a string")
}
gasPrice, err := strconv.Atoi(gasPriceStr)
if err != nil {
return "", err
}

return "0x" + strconv.FormatInt(int64(gasPrice), 16), nil

}

// unmarshal tx
var txMap map[string]interface{}
if err := json.Unmarshal([]byte(submittedTx), &txMap); err != nil {
return nil, err
}

// convert nonce
txMap["nonce"], err = numToHex(txMap, "nonce")
if err != nil {
return nil, err
}

// convert gas price
txMap["gasPrice"], err = numToHex(txMap, "gasPrice")
if err != nil {
return nil, err
}

// convert gas
txMap["gas"], err = numToHex(txMap, "gas")
if err != nil {
return nil, err
}

// convert value
txMap["value"], err = numToHex(txMap, "value")
if err != nil {
return nil, err
}

// map input
txMap["input"] = txMap["data"]

// convert signature v
txMap["v"], err = numToHex(txMap, "v")
if err != nil {
return nil, err
}

// convert signature r
txMap["r"], err = numToHex(txMap, "r")
if err != nil {
return nil, err
}

// convert signature s
txMap["s"], err = numToHex(txMap, "s")
if err != nil {
return nil, err
}

// turn mutated transaction into encoded json
txJson, err := json.Marshal(txMap)
if err != nil {
return nil, err
}

signedTx := &types.Transaction{}
if err := signedTx.UnmarshalJSON(txJson); err != nil {
return nil, err
}

return signedTx, nil
}

func (km KeyManager) ChatIdKeyPair() (x3dh.KeyPair, error) {

strPriv, err := km.keyStore.GetKey(chatMigration.MigrationPrivPrefix)
Expand Down
43 changes: 39 additions & 4 deletions mobile_interface.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package panthalassa

import (
"context"
"encoding/base64"
"encoding/hex"
"encoding/json"
Expand All @@ -22,7 +23,9 @@ import (
profile "github.com/Bit-Nation/panthalassa/profile"
queue "github.com/Bit-Nation/panthalassa/queue"
uiapi "github.com/Bit-Nation/panthalassa/uiapi"
"github.com/asdine/storm"
storm "github.com/asdine/storm"
common "github.com/ethereum/go-ethereum/common"
ethclient "github.com/ethereum/go-ethereum/ethclient"
proto "github.com/golang/protobuf/proto"
log "github.com/ipfs/go-log"
ma "github.com/multiformats/go-multiaddr"
Expand Down Expand Up @@ -59,6 +62,7 @@ func start(dbDir string, km *keyManager.KeyManager, config StartConfig, client,

// device api
deviceApi := api.New(client)
km.Api = deviceApi

// create p2p network
p2pNetwork, err := p2p.New()
Expand Down Expand Up @@ -139,10 +143,42 @@ func start(dbDir string, km *keyManager.KeyManager, config StartConfig, client,
// dyncall registry
dcr := dyncall.New()

if err := RegisterDocumentCalls(dcr, dbInstance, km); err != nil {
// register document related calls
docStorage := documents.NewStorage(dbInstance, km)
if err := RegisterDocumentCalls(dcr, docStorage, km); err != nil {
return err
}

ethClient, err := ethclient.Dial(config.EthWsEndpoint)
if err != nil {
return err
}

var notaryMultiAddr common.Address

networkID, err := ethClient.NetworkID(context.Background())
if err != nil {
return err
}

// make sure network is correct
if networkID.Int64() != int64(4) {
return errors.New("there is only a notary for the rinkeby testnet")
}

// rinkeby addresses
notaryMultiAddr = common.HexToAddress("0xe4d2032fdda10d4e6f483e2dea6857abc0e3cbf8")

notaryContract, err := documents.NewNotaryMulti(notaryMultiAddr, ethClient)
if err != nil {
return err
}
notariseCall := documents.NewDocumentNotariseCall(docStorage, km, notaryContract)
if err := dcr.Register(notariseCall); err != nil {
return err
}

// register contract calls
if err := RegisterContactCalls(dcr, dbInstance); err != nil {
return err
}
Expand All @@ -163,8 +199,7 @@ func start(dbDir string, km *keyManager.KeyManager, config StartConfig, client,
return nil
}

func RegisterDocumentCalls(dcr *dyncall.Registry, dbInstance *storm.DB, km *keyManager.KeyManager) error {
docStorage := documents.NewStorage(dbInstance, km)
func RegisterDocumentCalls(dcr *dyncall.Registry, docStorage *documents.Storage, km *keyManager.KeyManager) error {

// register document all call
allCall := documents.NewDocumentAllCall(docStorage)
Expand Down

0 comments on commit 452f71d

Please sign in to comment.