-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathleader.go.bckp
112 lines (98 loc) · 2.86 KB
/
leader.go.bckp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
package main
import (
"errors"
"github.com/LefKok/Coin/BitCoSi"
"github.com/LefKok/Coin/blkparser"
"github.com/dedis/cothority/lib/app"
"github.com/dedis/cothority/lib/coconet"
"github.com/dedis/cothority/lib/conode"
dbg "github.com/dedis/cothority/lib/debug_lvl"
"github.com/dedis/cothority/proto/sign"
"github.com/dedis/crypto/abstract"
"log"
"net"
)
var suite abstract.Suite
type Node struct {
IP net.IP
PublicKey string
Last_Block string
transaction_pool []blkparser.Tx
}
func main() {
Current := new(Node)
Magic := [4]byte{0xF9, 0xBE, 0xB4, 0xD9}
Current.IP = net.IPv4(0, 1, 2, 3)
Current.PublicKey = "my_cool_key"
Current.Last_Block = "0"
Parser, _ := BitCoSi.NewParser("/home/lefteris/hi/blocks", Magic)
Current.transaction_pool = Parser.Parse(10, 100)
var err error
var trblock BitCoSi.TrBlock
for len(Current.transaction_pool) > 0 {
trblock, err = getblock(Current, 15)
suite = app.GetSuite("25519")
sig := CoSi(trblock.HeaderHash)
dbg.Lvlf1("Signature is: %+v", sig)
if err != nil {
log.Println(err)
} else {
trblock.Print()
}
}
}
func CoSi(myHash string) sign.SignatureBroadcastMessage {
//server := "profeda.org:2001"
server := "localhost:2011"
dbg.Lvl2("Connecting to", server)
conn := coconet.NewTCPConn(server)
err := conn.Connect()
if err != nil {
dbg.Fatal("Error when getting the connection to the host:", err)
}
dbg.Lvl1("Connected to ", server)
msg := &conode.TimeStampMessage{
Type: conode.StampRequestType,
ReqNo: 0,
Sreq: &conode.StampRequest{Val: []byte(myHash)}}
err = conn.PutData(msg)
if err != nil {
dbg.Fatal("Couldn't send hash-message to server: ", err)
}
dbg.Lvl1("Sent signature request")
// Wait for the signed message
tsm := &conode.TimeStampMessage{}
tsm.Srep = &conode.StampReply{}
tsm.Srep.SuiteStr = suite.String()
err = conn.GetData(tsm)
if err != nil {
dbg.Fatal("Error while receiving signature:", err)
}
dbg.Lvl1("Got signature response")
// Asking to close the connection
err = conn.PutData(&conode.TimeStampMessage{
ReqNo: 1,
Type: conode.StampClose,
})
conn.Close()
dbg.Lvl2("Connection closed with server")
// Verify if what we received is correct
if !conode.VerifySignature(suite, tsm.Srep, tsm.Srep.SigBroad.X0_hat, []byte(myHash)) {
dbg.Fatal("Verification of signature failed")
} else {
dbg.Lvl1("Verification OK")
}
return tsm.Srep.SigBroad
}
func getblock(l *Node, n int) (_ BitCoSi.TrBlock, _ error) {
if len(l.transaction_pool) > 0 {
trlist := BitCoSi.NewTransactionList(l.transaction_pool, n)
header := BitCoSi.NewHeader(trlist, l.Last_Block, l.IP, l.PublicKey)
trblock := BitCoSi.NewTrBlock(trlist, header)
l.transaction_pool = l.transaction_pool[trblock.TransactionList.TxCnt:]
l.Last_Block = trblock.HeaderHash
return trblock, nil
} else {
return *new(BitCoSi.TrBlock), errors.New("no transaction available")
}
}