Advertisement
Guest User

BlockChainGO

a guest
Nov 14th, 2018
136
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Go 2.13 KB | None | 0 0
  1. package main
  2.  
  3. import (
  4.     "fmt"
  5.     "crypto/sha256"
  6.     "encoding/hex"
  7.     "encoding/json"
  8.     "fmt"
  9.     "log"
  10.     "net/http"
  11.     "net"
  12.     "time"
  13.     "math/rand"
  14. )
  15.  
  16. type Block struct{
  17.     Hash string
  18.     PrevHash string
  19.     Id uint64
  20.     Timestamp uint64
  21.     Data interface{}
  22. }
  23.  
  24. func (block *Block) GetHash() string{
  25.     hash := sha256.Sum256([]byte(block.ToString()))
  26.     return hex.Decode(sha25)
  27. }
  28.  
  29. func (block *Block) ToString() string{
  30.     data, _ := json.MarshalIndent(block.Data, '\n', '\t')
  31.     return fmt.Sprintf(`
  32.         {
  33.             "id": %d,
  34.             "prevHash":"%s",
  35.             "timestamp":%d,
  36.             "data":%v,
  37.         }
  38.     `, block.Id, block.PrevHash, block.Timestamp, data)
  39. }
  40.  
  41. type BlockChain struct{
  42.     Blocks []*Block
  43.     LastID uint64
  44. }
  45.  
  46. func New () *BlockChain{
  47.     chain := new(BlockChain)
  48.     chain.LastID = 0
  49.     chain.Blocks = make([]*Block, 0)
  50.     return chain
  51. }
  52.  
  53. func (BlockChain *ch) NewBlock(data interface{}) string{
  54.     ch.LastID++
  55.     block := new (Block)
  56.     block.Data = data
  57.     block.Id = ch.LastID
  58.     if ch.Length() == 0{
  59.         block.PrevHash = ToSha256(rand.Uint64())       
  60.     }else{
  61.         block.PrevHash = ch.LastBlock.GetHash()
  62.     }
  63.     ch.Blocks = append(ch.Blocks, block)
  64.     return block.GetHash()
  65. }
  66.  
  67. func (BlockChain *ch) Length() int{
  68.     return len(ch.Blocks)
  69. }
  70.  
  71. func (BlockChain *ch) LastBlock() *Block{
  72.     return ch.Blocks[len(ch.Blocks) - 1]
  73. }
  74.  
  75. func (BlockChain *ch) GetBlock(hash string) *Block{
  76.     for _, block := range ch.Blocks{
  77.         if block.Hash == hash{
  78.             return block
  79.         }
  80.     }
  81.     return nil
  82. }
  83.  
  84. func ToSha256(data interface{}) string{
  85.     hash := sha256.Sum256([]byte(data))
  86.     return hex.Decode(hash)
  87. }
  88.  
  89. func Exists(r http.Request, form string){
  90.     _, ok := r.Form[form]
  91.     return ok
  92. }
  93.  
  94. func main() {
  95.     http.HandleFunc("/block/new", func (w *http.Writer, r http.Request){
  96.         if r.ParseForm() != nil{
  97.             log.Fataln("Cannot Parse Form")
  98.         }
  99.        
  100.         if Exists(r, "data"){
  101.  
  102.         }
  103.     }) 
  104.  
  105.     http.HandleFunc("/block/list", func(w *http.Writer, r http.Request){
  106.         if r.ParseForm() != nil{
  107.             log.Fataln("Cannot Parse Form")
  108.         }
  109.  
  110.         if Exists(r, "hash"){
  111.             hash := r.FormValue("hash")
  112.            
  113.         }else{
  114.  
  115.         }
  116.     })
  117.  
  118.     log.Println("Starting on :80")
  119.     log.Println(http.ServeAndListen(":80", nil))
  120. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement