Browse Source

Created basic blockchain, created basic server, renamed from "micah.mp/paperchain/client" to "micah.mp/paperchain" due to realising I dont really need a separate server cause it's blockchain

pull/3/head
gryffyn 7 months ago
parent
commit
df99b1fff0
Signed by: gryffyn GPG Key ID: 6948DD6514D02BEF
  1. 3
      .gitignore
  2. 69
      chain/chain.go
  3. 3
      go.mod
  4. 2
      go.sum
  5. 33
      paperchain.go
  6. 65
      server/server.go

3
.gitignore

@ -88,4 +88,5 @@ fabric.properties
# Android studio 3.1+ serialized cache file
.idea/caches/build_file_checksums.ser
client
paperchain
.env

69
chain/chain.go

@ -0,0 +1,69 @@
package chain
import (
"encoding/hex"
"lukechampine.com/blake3"
"time"
)
type _CASE struct {
ID string
Name string
}
type _EXAMINER struct {
ID string
Name string
Phone string
Email string
Org string
}
type _DATA struct {
Case _CASE
Examiner _EXAMINER
}
type Block struct {
Index int
Timestamp time.Time
Data string
Hash string
PrevHash string
}
var Blockchain []Block
func calculateHash(block Block) string {
record := string(rune(block.Index)) + block.Timestamp.String() + block.Data + block.PrevHash
h := blake3.New(384, nil)
_, _ = h.Write([]byte(record))
hashed := h.Sum(nil)
return hex.EncodeToString(hashed)
}
func GenerateBlock(oldBlock Block, data string) (Block, error) {
t := time.Now()
var newBlock Block
newBlock = Block{oldBlock.Index + 1, t, data, oldBlock.Hash, calculateHash(newBlock)}
return newBlock, nil
}
func IsBlockValid(newBlock, oldBlock Block) bool {
if oldBlock.Index+1 != newBlock.Index {
return false
}
if oldBlock.Hash != newBlock.PrevHash {
return false
}
if calculateHash(newBlock) != newBlock.Hash {
return false
}
return true
}
func ReplaceChain(newBlocks []Block) {
if len(newBlocks) > len(Blockchain) {
Blockchain = newBlocks
}
}

3
go.mod

@ -1,9 +1,10 @@
module micah.mp/paperchain/client
module micah.mp/paperchain
go 1.15
require (
github.com/andskur/argon2-hashing v0.1.3
github.com/joho/godotenv v1.3.0
github.com/rivo/tview v0.0.0-20200915114512-42866ecf6ca6
golang.org/x/crypto v0.0.0-20201012173705-84dcc777aaee // indirect
lukechampine.com/blake3 v1.1.4

2
go.sum

@ -5,6 +5,8 @@ github.com/gdamore/encoding v1.0.0 h1:+7OoQ1Bc6eTm5niUzBa0Ctsh6JbMW6Ra+YNuAtDBdk
github.com/gdamore/encoding v1.0.0/go.mod h1:alR0ol34c49FCSBLjhosxzcPHQbf2trDkoo5dl+VrEg=
github.com/gdamore/tcell v1.3.0 h1:r35w0JBADPZCVQijYebl6YMWWtHRqVEGt7kL2eBADRM=
github.com/gdamore/tcell v1.3.0/go.mod h1:Hjvr+Ofd+gLglo7RYKxxnzCBmev3BzsS67MebKS4zMM=
github.com/joho/godotenv v1.3.0 h1:Zjp+RcGpHhGlrMbJzXTrZZPrWj+1vfm90La1wgB6Bhc=
github.com/joho/godotenv v1.3.0/go.mod h1:7hK45KPybAkOC6peb+G5yklZfMxEjkZhHbwpqxOKXbg=
github.com/klauspost/cpuid v1.3.1 h1:5JNjFYYQrZeKRJ0734q51WCEEn2huer72Dc7K+R/b6s=
github.com/klauspost/cpuid v1.3.1/go.mod h1:bYW4mA6ZgKPob1/Dlai2LviZJO7KGI3uoWLd42rAQw4=
github.com/lucasb-eyer/go-colorful v1.0.2/go.mod h1:0MS4r+7BZKSJ5mw4/S5MPN+qHFF1fYclkSPilDOKW0s=

33
paperchain.go

@ -1,15 +1,32 @@
package main
import (
_ "encoding/hex"
"fmt"
_ "lukechampine.com/blake3"
"github.com/joho/godotenv"
"log"
"micah.mp/paperchain/chain"
"micah.mp/paperchain/server"
"time"
)
func main() {
loginUi()
if authLogin() {
fmt.Println("Valid login.")
func loadEnv() map[string]string {
var env map[string]string
env, err := godotenv.Read()
if err != nil {
log.Fatal("Error loading .env file")
}
return env
}
func main() {
env := loadEnv()
go func() {
t := time.Now()
genesisBlock := chain.Block{0, t, "", "", ""}
chain.Blockchain = append(chain.Blockchain, genesisBlock)
}()
server.Serve(env["PAPERCHAIN_ADDR"], env["PAPERCHAIN_PORT"])
// loginUi()
//if authLogin() {
// fmt.Println("Valid login.")
//}
}

65
server/server.go

@ -0,0 +1,65 @@
package server
import (
"bufio"
"fmt"
"io"
"log"
"micah.mp/paperchain/chain"
"net"
)
var bcServer chan []chain.Block
func Serve(addr string, port string) {
server, err := net.Listen("tcp", addr+":"+port)
if server == nil {
panic("couldn't start listening: " + err.Error())
}
defer server.Close()
conns := clientConns(server)
for {
go handleConn(<-conns)
}
}
func clientConns(listener net.Listener) chan net.Conn {
ch := make(chan net.Conn)
i := 0
go func() {
for {
client, err := listener.Accept()
if client == nil {
fmt.Printf("couldn't accept: " + err.Error())
continue
}
i++
fmt.Printf("%d: %v <-> %v\n", i, client.LocalAddr(), client.RemoteAddr())
ch <- client
}
}()
return ch
}
func handleConn(conn net.Conn) {
defer conn.Close()
_, _ = io.WriteString(conn, "Enter a new note:")
scanner := bufio.NewScanner(conn)
go func() {
for scanner.Scan() {
note := scanner.Text()
newBlock, err := chain.GenerateBlock(chain.Blockchain[len(chain.Blockchain)-1], note)
if err != nil {
log.Println(err)
continue
}
if chain.IsBlockValid(newBlock, chain.Blockchain[len(chain.Blockchain)-1]) {
newBlockchain := append(chain.Blockchain, newBlock)
chain.ReplaceChain(newBlockchain)
}
bcServer <- chain.Blockchain
_, _ = io.WriteString(conn, "\nEnter a new note:")
}
}()
}
Loading…
Cancel
Save