Skip to content

Commit

Permalink
Merge pull request #2 from mitmedialab/only-production
Browse files Browse the repository at this point in the history
Patient and Provider Authentication
  • Loading branch information
Firescar96 authored Jun 28, 2018
2 parents dd05d9d + 81d17ac commit 133da97
Show file tree
Hide file tree
Showing 105 changed files with 5,182 additions and 852,836 deletions.
116 changes: 116 additions & 0 deletions DatabaseManager/common/common.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
package common

import (
"encoding/hex"
"log"
"os"
"os/exec"
"runtime"
"strings"

"github.com/ethereum/go-ethereum/rpc"
"github.com/syndtr/goleveldb/leveldb"
)

type Document struct {
DocumentID int `json:"DocumentID"`
PatientID string `json:"patientid"`
PracticeID string `json:"practiceid"`
RecvdDateTime string `json:"recvddatetime"`
DocDateTime string `json:"docdatetime"`
}

type Documents []Document

type Patient struct {
PatientID int `json:"patientid"`
EthAddress string `json:"ethaddress"`
}

type Patients []Patient

type NoArgs struct {
// nothin
}

func PrefixPatientUID(input string) string {
return "patient-uid-" + strings.ToLower(input)
}

// NodeExec calls node and abstracts away figuring out where the node binary is located
func NodeExec(args ...string) *exec.Cmd {
cmd := []string{"node"}
cmd = append(cmd, args...)
cmd = append(cmd, "||")
cmd = append(cmd, "node")
cmd = append(cmd, args...)
return exec.Command("/bin/bash", []string{"-c", strings.Join(cmd, " ")}...)
}

var WalletPassword string

//recover takes a message and returns the address of the sender
func ECRecover(msg string, signature string) (string, error) {
msgHex := "0x" + hex.EncodeToString([]byte(msg))
rpcClient, _ := GetEthereumRPCConn()

var result string
err := rpcClient.Call(&result, "personal_ecRecover", msgHex, signature)
return result, err
}

//Sign takes a hashed message and signs it
func Sign(msg string, account string) (string, error) {
msgHex := "0x" + hex.EncodeToString([]byte(msg))

rpcClient, _ := GetEthereumRPCConn()

var result string
err := rpcClient.Call(&result, "personal_sign", msgHex, account, "")
return result, err
}

func GetEthereumRPCConn() (*rpc.Client, error) {
//create a connection over json rpc to the ethereum client
rpcClient, err := rpc.Dial("http://localhost:8545")
if err != nil {
log.Fatalf("Failed to connect to the Ethereum client: %v", err)
}
return rpcClient, err
}

//instantiates the lookup table
func InstantiateLookupTable() *leveldb.DB {
var home string

if runtime.GOOS == "windows" {
home = os.Getenv("APPDATA") + "/MedRec"
} else if runtime.GOOS == "darwin" {
log.Println("darwmin")
home = os.Getenv("HOME") + "/Library/Preferences"
} else {
home = os.Getenv("HOME") + "/.medrec"
}

tab, err := leveldb.OpenFile(home+"/lookupTable", nil)
if err != nil {
log.Fatal(err)
}

return tab
}

func GetKeystorePath(username string) string {
var home string

if runtime.GOOS == "windows" {
home = os.Getenv("APPDATA") + "/MedRec"
} else if runtime.GOOS == "darwin" {
log.Println("darwmin")
home = os.Getenv("HOME") + "/Library/Preferences"
} else {
home = os.Getenv("HOME") + "/.medrec"
}

return home + "/" + username + "-keystore.keystore"
}
567 changes: 0 additions & 567 deletions DatabaseManager/ethereum/Agent.go

This file was deleted.

327 changes: 0 additions & 327 deletions DatabaseManager/ethereum/AgentGroup.go

This file was deleted.

55 changes: 51 additions & 4 deletions DatabaseManager/ethereum/AgentRegistryDaemon.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,21 @@ import (
"time"
)

// AddSigner watches the AgentRegistry for the AddSigner event and upon detecting one votes
// in the signer via the clique protocol
func AddSigner() {
//sha3 hash of the "AddSigner(address)" event
//sha3 (keccack-256) hash of the "AddSigner(address)" event
const addSignerHash = "0x637c77a2d598a51d085d4a2413332c45a235a25ee855bf3dfcdc2c8fcf02860f"

rpcClient := GetRPCConn()
rpcClient, _ := GetEthereumRPCConn()

var events []map[string]interface{}
var lastBlockNumber int64

lastBlockNumber = 1
hexBlockNumber := "0x" + fmt.Sprintf("%x", lastBlockNumber)
txObject := map[string]string{"fromBlock": hexBlockNumber, "toBlock": "latest", "topic": addSignerHash}
ticker := time.NewTicker(time.Millisecond * 500)
ticker := time.NewTicker(time.Second * 5)
go func() {
for range ticker.C {
err := rpcClient.Call(&events, "eth_getLogs", txObject)
Expand All @@ -37,7 +39,52 @@ func AddSigner() {

err = rpcClient.Call(&events, "clique_propose", newSigner, true)
if err != nil {
log.Printf("Non fatal err when proposing new signer to the clique, liquely due to using ganache-cli")
log.Printf("Non fatal err when proposing new signer to the clique, likely due to using ganache-cli")
}

if blockNumber > lastBlockNumber {
lastBlockNumber = blockNumber
hexBlockNumber := "0x" + fmt.Sprintf("%x", lastBlockNumber+1)
txObject["fromBlock"] = hexBlockNumber
}
}
}
}()
}

// RemoveSigner watches the AgentRegistry for the RemoveSigner event and upon detecting one votes
// to kick a signer via the clique protocol
func RemoveSigner() {
//sha3 (keccack-256) hash of the "RemoveSigner(address)" event
const addSignerHash = "0x1803740ef72fc16e647c10fe2d31cf61a1578081960c2e3fb7f5aa957e82f550"

rpcClient, _ := GetEthereumRPCConn()

var events []map[string]interface{}
var lastBlockNumber int64

lastBlockNumber = 1
hexBlockNumber := "0x" + fmt.Sprintf("%x", lastBlockNumber)
txObject := map[string]string{"fromBlock": hexBlockNumber, "toBlock": "latest", "topic": addSignerHash}
ticker := time.NewTicker(time.Second * 5)
go func() {
for range ticker.C {
err := rpcClient.Call(&events, "eth_getLogs", txObject)
if err != nil {
log.Printf("Failed to get the filter logs: %v", err)
continue
}

for _, event := range events {
removeSignerTopic := event["topics"].([]interface{})
hexBlockNumber := event["blockNumber"].(string)
blockNumber, _ := strconv.ParseInt(hexBlockNumber, 0, 64)
kickedSigner := "0x" + removeSignerTopic[1].(string)[26:]
log.Printf("Signer removed %s at block %d\n", kickedSigner, blockNumber)

err = rpcClient.Call(&events, "clique_propose", kickedSigner, false)
if err != nil {
log.Printf("Non fatal err when proposing new signer to the clique, likely due to using ganache-cli")
}

if blockNumber > lastBlockNumber {
Expand Down
3 changes: 3 additions & 0 deletions DatabaseManager/ethereum/README.md
Original file line number Diff line number Diff line change
@@ -1 +1,4 @@
Run `go generate` to generate the smart contract libraries.


You will need to have `solc`, the solidity compiler installed
Loading

0 comments on commit 133da97

Please sign in to comment.