Advertisement
1lann

Untitled

Aug 27th, 2017
174
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Go 2.19 KB | None | 0 0
  1. /**
  2. Hello this is Liugnum
  3. we're gonna write a program
  4.  
  5. cleaned by 1lann
  6.  
  7. @author Lignum
  8. @date 27/08/2017
  9. */
  10. package main
  11.  
  12. import (
  13.     "bufio"
  14.     "bytes"
  15.     "encoding/json"
  16.     "fmt"
  17.     "io"
  18.     "net/http"
  19.     "os"
  20.     "strings"
  21. )
  22.  
  23. const Header = "KCH"
  24. const Password = "dljkqwndw"
  25.  
  26. func printBanner() {
  27.     fmt.Println("KristChatGo booting up like afuckign rocket flies into spice...")
  28. }
  29.  
  30. func readMessageLoop() {
  31.     type TransactionResponse struct {
  32.         Ok           bool
  33.         Transactions []struct {
  34.             From     string
  35.             To       string
  36.             Value    int
  37.             Metadata string
  38.         }
  39.     }
  40.  
  41.     resp, err := http.Get("https://krist.ceriat.net/transactions/latest")
  42.     if err != nil {
  43.         panic(err)
  44.     }
  45.  
  46.     defer resp.Body.Close()
  47.  
  48.     var response TransactionResponse
  49.     if err := json.NewDecoder(resp.Body).Decode(&response); err != nil {
  50.         panic(err)
  51.     }
  52.  
  53.     for _, tr := range response.Transactions {
  54.         if strings.HasPrefix(tr.Metadata, Header) {
  55.             fmt.Println(strings.TrimPrefix(tr.Metadata, Header))
  56.         }
  57.     }
  58. }
  59.  
  60. func messageSend() {
  61.     type SendTransaction struct {
  62.         PrivateKey string `json:"privatekey"`
  63.         To         string `json:"to"`
  64.         Amount     int    `json:"amount"`
  65.         Metadata   string `json:"metadata"`
  66.     }
  67.  
  68.     fmt.Println("address")
  69.     var address string
  70.     if _, err := fmt.Scanf("%s\n", &address); err != nil {
  71.         panic("ok bye")
  72.     }
  73.  
  74.     fmt.Println("message")
  75.     var message string
  76.     if _, err := fmt.Scanf("%s\n", &message); err != nil {
  77.         panic("ok bye")
  78.     }
  79.  
  80.     txJSON, _ := json.Marshal(SendTransaction{
  81.         To:         address,
  82.         PrivateKey: Password,
  83.         Amount:     1,
  84.         Metadata:   Header + message,
  85.     })
  86.     fmt.Println(string(txJSON))
  87.  
  88.     resp, err := http.Post("https://krist.ceriat.net/transactions",
  89.         "application/json", bytes.NewReader(txJSON))
  90.     if err != nil {
  91.         panic(err)
  92.     }
  93.  
  94.     defer resp.Body.Close()
  95.  
  96.     if _, err := io.Copy(os.Stdout, resp.Body); err != nil {
  97.         panic(err)
  98.     }
  99.     os.Stdout.Write([]byte{'\n'})
  100. }
  101.  
  102. func main() {
  103.     printBanner()
  104.  
  105.     scanner := bufio.NewScanner(os.Stdin)
  106.     for scanner.Scan() {
  107.         fmt.Println("type read or send")
  108.  
  109.         switch scanner.Text() {
  110.         case "read":
  111.             readMessageLoop()
  112.         case "send":
  113.             messageSend()
  114.         default:
  115.             panic("panic")
  116.         }
  117.     }
  118. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement