@ -1 +1 @@ |
|||
{"genesisblock":"YKWAWY6GM5xrLhYrfi6hAmPmsECUH83n7xVcgaguLF4=","lastupdate":"0001-01-01T00:00:00Z","blocks":[{"hash":"YKWAWY6GM5xrLhYrfi6hAmPmsECUH83n7xVcgaguLF4=","height":0,"date":"2017-11-29T10:42:55.112675187+01:00","previoushash":"","nexthash":"cSN48a9BwgJQZdz2NwBRPJqCtC4Qtv-l-RYeGl9eBZs=","data":[{"address":"firstaddress"}],"emitter":"VOnL-15rFsUiCnRoyGFksKvWKcwNBRz5iarRem0Ilvo="},{"hash":"cSN48a9BwgJQZdz2NwBRPJqCtC4Qtv-l-RYeGl9eBZs=","height":1,"date":"2017-11-29T23:07:43.169204719+01:00","previoushash":"YKWAWY6GM5xrLhYrfi6hAmPmsECUH83n7xVcgaguLF4=","nexthash":"","data":[{"address":"firstaddress"}],"emitter":"VOnL-15rFsUiCnRoyGFksKvWKcwNBRz5iarRem0Ilvo="}]} |
|||
{"genesisblock":"YKWAWY6GM5xrLhYrfi6hAmPmsECUH83n7xVcgaguLF4=","lastupdate":"0001-01-01T00:00:00Z","blocks":[{"hash":"YKWAWY6GM5xrLhYrfi6hAmPmsECUH83n7xVcgaguLF4=","height":0,"date":"2017-11-29T10:42:55.112675187+01:00","previoushash":"","nexthash":"cSN48a9BwgJQZdz2NwBRPJqCtC4Qtv-l-RYeGl9eBZs=","data":[{"address":"firstaddress"}],"emitter":"VOnL-15rFsUiCnRoyGFksKvWKcwNBRz5iarRem0Ilvo="},{"hash":"cSN48a9BwgJQZdz2NwBRPJqCtC4Qtv-l-RYeGl9eBZs=","height":1,"date":"2017-11-29T23:07:43.169204719+01:00","previoushash":"YKWAWY6GM5xrLhYrfi6hAmPmsECUH83n7xVcgaguLF4=","nexthash":"Ql0MH8WVaCOXzRWmR5xUyAQXaQ5Ovt6PoN3HrkHmYmk=","data":[{"address":"firstaddress"}],"emitter":"VOnL-15rFsUiCnRoyGFksKvWKcwNBRz5iarRem0Ilvo="},{"hash":"Ql0MH8WVaCOXzRWmR5xUyAQXaQ5Ovt6PoN3HrkHmYmk=","height":2,"date":"2017-12-26T14:38:41.446523607+01:00","previoushash":"cSN48a9BwgJQZdz2NwBRPJqCtC4Qtv-l-RYeGl9eBZs=","nexthash":"64bzp4j5jJjCcvKYcsytJOKD5dFyMHUjtNY1S5TD8Sk=","data":[{"address":"firstaddress"}],"emitter":"VOnL-15rFsUiCnRoyGFksKvWKcwNBRz5iarRem0Ilvo="},{"hash":"64bzp4j5jJjCcvKYcsytJOKD5dFyMHUjtNY1S5TD8Sk=","height":3,"date":"2017-12-26T18:05:05.541862456+01:00","previoushash":"Ql0MH8WVaCOXzRWmR5xUyAQXaQ5Ovt6PoN3HrkHmYmk=","nexthash":"N-w5F0pRA0kpZx7JDS3RPBedUSq1Kyk13Pc1guhY7Ws=","data":[{"address":"firstaddress"}],"emitter":"VOnL-15rFsUiCnRoyGFksKvWKcwNBRz5iarRem0Ilvo="},{"hash":"N-w5F0pRA0kpZx7JDS3RPBedUSq1Kyk13Pc1guhY7Ws=","height":4,"date":"2017-12-26T18:06:13.953399725+01:00","previoushash":"64bzp4j5jJjCcvKYcsytJOKD5dFyMHUjtNY1S5TD8Sk=","nexthash":"45Otq5P8WR97T4SxvaFk_0icNdUcIzO-mTjn6wrxJd4=","data":[{"address":"firstaddress"}],"emitter":"VOnL-15rFsUiCnRoyGFksKvWKcwNBRz5iarRem0Ilvo="},{"hash":"45Otq5P8WR97T4SxvaFk_0icNdUcIzO-mTjn6wrxJd4=","height":5,"date":"2017-12-26T18:06:57.93092362+01:00","previoushash":"N-w5F0pRA0kpZx7JDS3RPBedUSq1Kyk13Pc1guhY7Ws=","nexthash":"","data":[{"address":"firstaddress"}],"emitter":"VOnL-15rFsUiCnRoyGFksKvWKcwNBRz5iarRem0Ilvo="}]} |
@ -0,0 +1,18 @@ |
|||
SESSION='peersTest' |
|||
|
|||
tmux new-session -d -s $SESSION |
|||
tmux split-window -d -t 0 -v |
|||
tmux split-window -d -t 0 -h |
|||
tmux split-window -d -t 0 -v |
|||
tmux split-window -d -t 2 -v |
|||
|
|||
tmux send-keys -t 0 'cd peer && go run *.go server 3001 3002' enter |
|||
sleep 2 |
|||
tmux send-keys -t 1 "curl -X POST http://127.0.0.1:3002/register -d '{\"address\": \"firstaddress\"}'" enter |
|||
sleep 1 |
|||
tmux send-keys -t 1 'cd peer && go run *.go client 3003 3004' enter |
|||
tmux send-keys -t 2 'cd peer && go run *.go client 3005 3006' enter |
|||
tmux send-keys -t 3 'cd peer && go run *.go client 3007 3008' enter |
|||
tmux send-keys -t 4 'cd serverCA && go run *.go' enter |
|||
|
|||
tmux attach |
@ -0,0 +1,32 @@ |
|||
package main |
|||
|
|||
import ( |
|||
"encoding/json" |
|||
"fmt" |
|||
"net/http" |
|||
) |
|||
|
|||
type Address struct { |
|||
Address string `json:"address"` //the pubK of the user, to perform logins
|
|||
} |
|||
|
|||
func Index(w http.ResponseWriter, r *http.Request) { |
|||
fmt.Fprintln(w, "CA") |
|||
} |
|||
func GetPeers(w http.ResponseWriter, r *http.Request) { |
|||
getPeers() |
|||
|
|||
jResp, err := json.Marshal(peersList) |
|||
check(err) |
|||
fmt.Fprintln(w, string(jResp)) |
|||
} |
|||
|
|||
func GetBlockchain(w http.ResponseWriter, r *http.Request) { |
|||
fmt.Print("aaaaa: ") |
|||
fmt.Println(blockchain.Blocks[len(blockchain.Blocks)-1].Hash) |
|||
reconstructBlockchainFromBlock("http://"+config.IP+":"+config.ServerRESTPort, blockchain.Blocks[len(blockchain.Blocks)-1].Hash) |
|||
|
|||
jResp, err := json.Marshal(blockchain) |
|||
check(err) |
|||
fmt.Fprintln(w, string(jResp)) |
|||
} |
@ -0,0 +1,26 @@ |
|||
package main |
|||
|
|||
import ( |
|||
mgo "gopkg.in/mgo.v2" |
|||
) |
|||
|
|||
func getSession() (*mgo.Session, error) { |
|||
session, err := mgo.Dial("mongodb://" + config.Mongodb.IP) |
|||
if err != nil { |
|||
panic(err) |
|||
} |
|||
//defer session.Close()
|
|||
|
|||
// Optional. Switch the session to a monotonic behavior.
|
|||
session.SetMode(mgo.Monotonic, true) |
|||
|
|||
// Optional. Switch the session to a monotonic behavior.
|
|||
session.SetMode(mgo.Monotonic, true) |
|||
|
|||
return session, err |
|||
} |
|||
func getCollection(session *mgo.Session, collection string) *mgo.Collection { |
|||
|
|||
c := session.DB(config.Mongodb.Database).C(collection) |
|||
return c |
|||
} |
@ -0,0 +1,3 @@ |
|||
curl -X POST http://127.0.0.1:3030/signup -d '{"email": "user1@e.com", "password": "user1"}' |
|||
|
|||
curl -X POST http://127.0.0.1:3030/login -d '{"email": "user1@e.com", "password": "user1"}' |
@ -0,0 +1,49 @@ |
|||
package main |
|||
|
|||
import ( |
|||
"fmt" |
|||
"time" |
|||
|
|||
jwt "github.com/dgrijalva/jwt-go" |
|||
) |
|||
|
|||
const ( |
|||
signingKey = "this is the secret signing key" |
|||
) |
|||
|
|||
var createdToken string |
|||
|
|||
func initializeToken() { |
|||
var err error |
|||
createdToken, err = newToken() |
|||
if err != nil { |
|||
fmt.Println("Creating token failed") |
|||
} |
|||
} |
|||
|
|||
func newToken() (string, error) { |
|||
signingKeyB := []byte(signingKey) |
|||
// Create the token
|
|||
token := jwt.New(jwt.SigningMethodHS256) |
|||
// Set some claims
|
|||
claims := make(jwt.MapClaims) |
|||
claims["foo"] = "bar" |
|||
claims["exp"] = time.Now().Add(time.Hour * 72).Unix() |
|||
token.Claims = claims |
|||
|
|||
// Sign and get the complete encoded token as a string
|
|||
tokenString, err := token.SignedString(signingKeyB) |
|||
return tokenString, err |
|||
} |
|||
|
|||
func parseToken(myToken string, myKey string) { |
|||
token, err := jwt.Parse(myToken, func(token *jwt.Token) (interface{}, error) { |
|||
return []byte(myKey), nil |
|||
}) |
|||
|
|||
if err == nil && token.Valid { |
|||
fmt.Println("Your token is valid. I like your style.") |
|||
} else { |
|||
fmt.Println("This token is terrible! I cannot accept this.") |
|||
} |
|||
} |
@ -0,0 +1,127 @@ |
|||
package main |
|||
|
|||
import ( |
|||
"encoding/json" |
|||
"fmt" |
|||
"net/http" |
|||
|
|||
"gopkg.in/mgo.v2/bson" |
|||
) |
|||
|
|||
type User struct { |
|||
Id bson.ObjectId `json:"id" bson:"_id,omitempty"` |
|||
Email string `json:"email"` |
|||
Password string `json:"password"` |
|||
Token string `json:"token"` |
|||
} |
|||
|
|||
func Signup(w http.ResponseWriter, r *http.Request) { |
|||
//ipFilter(w, r)
|
|||
|
|||
decoder := json.NewDecoder(r.Body) |
|||
var user User |
|||
err := decoder.Decode(&user) |
|||
if err != nil { |
|||
panic(err) |
|||
} |
|||
defer r.Body.Close() |
|||
|
|||
fmt.Print("user signup: ") |
|||
fmt.Println(user) |
|||
|
|||
//save the new project to mongodb
|
|||
rUser := User{} |
|||
err = userCollection.Find(bson.M{"email": user.Email}).One(&rUser) |
|||
if err != nil { |
|||
//user not exists
|
|||
err = userCollection.Insert(user) //TODO find a way to get the object result when inserting in one line, without need of the two mgo petitions
|
|||
err = userCollection.Find(bson.M{"email": user.Email}).One(&user) |
|||
} else { |
|||
//user exists
|
|||
fmt.Fprintln(w, "User already registered") |
|||
return |
|||
} |
|||
|
|||
jResp, err := json.Marshal(user) |
|||
if err != nil { |
|||
panic(err) |
|||
} |
|||
fmt.Fprintln(w, string(jResp)) |
|||
} |
|||
|
|||
func Login(w http.ResponseWriter, r *http.Request) { |
|||
//ipFilter(w, r)
|
|||
|
|||
decoder := json.NewDecoder(r.Body) |
|||
var user User |
|||
err := decoder.Decode(&user) |
|||
if err != nil { |
|||
panic(err) |
|||
} |
|||
defer r.Body.Close() |
|||
|
|||
fmt.Print("user login: ") |
|||
fmt.Println(user) |
|||
token, err := newToken() |
|||
check(err) |
|||
user.Token = token |
|||
|
|||
//save the new project to mongodb
|
|||
rUser := User{} |
|||
err = userCollection.Find(bson.M{"email": user.Email}).One(&rUser) |
|||
if err != nil { |
|||
} else { |
|||
//user exists, update with the token
|
|||
err = userCollection.Update(bson.M{"_id": rUser.Id}, user) |
|||
check(err) |
|||
} |
|||
//generate the token
|
|||
//add the token to the user
|
|||
//save the user with the new token
|
|||
|
|||
jResp, err := json.Marshal(user) |
|||
if err != nil { |
|||
panic(err) |
|||
} |
|||
fmt.Fprintln(w, string(jResp)) |
|||
} |
|||
|
|||
type Sign struct { |
|||
M string `json:"m"` |
|||
C string `json:"c"` |
|||
} |
|||
|
|||
func BlindSign(w http.ResponseWriter, r *http.Request) { |
|||
//ipFilter(w, r)
|
|||
|
|||
decoder := json.NewDecoder(r.Body) |
|||
var user User |
|||
err := decoder.Decode(&user) |
|||
if err != nil { |
|||
panic(err) |
|||
} |
|||
defer r.Body.Close() |
|||
|
|||
jResp, err := json.Marshal(user) |
|||
if err != nil { |
|||
panic(err) |
|||
} |
|||
fmt.Fprintln(w, string(jResp)) |
|||
} |
|||
func VerifySign(w http.ResponseWriter, r *http.Request) { |
|||
//ipFilter(w, r)
|
|||
|
|||
decoder := json.NewDecoder(r.Body) |
|||
var user User |
|||
err := decoder.Decode(&user) |
|||
if err != nil { |
|||
panic(err) |
|||
} |
|||
defer r.Body.Close() |
|||
|
|||
jResp, err := json.Marshal(user) |
|||
if err != nil { |
|||
panic(err) |
|||
} |
|||
fmt.Fprintln(w, string(jResp)) |
|||
} |