-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprotocol.nim
More file actions
44 lines (34 loc) · 1.11 KB
/
protocol.nim
File metadata and controls
44 lines (34 loc) · 1.11 KB
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
import net, strformat
import base58/bitcoin
const
protocolName* = "ShitP2P"
protocolVersion* = "0.0.1"
protocolPort* = Port(2005)
type
BlockChainNode* = object
ip*: string
Command* = enum
SYNC = "sync"
BROADCAST = "broadcast"
TRANSACTION = "transaction"
Block* = object
blockId*: int
sender*: string
receiver*: string
amount*: int
nonce*: int
previousHash*: string
blockHash*: string
proc isSupportedVersion*(version: string): bool =
if version == protocolVersion:
return true
else: return false
proc toBlockChainAddress*(pk: string): string =
result = encode(pk)
proc toPublicKey*(address: string): string =
result = decode(address)
func prepareHeader(command: Command, msgLength: int): string =
result = &"{protocolName} {protocolVersion} {command} {msgLength}\n"
template transactionHeader*(len: int): string = prepareHeader(TRANSACTION, len)
template broadcastHeader*(len: int): string = prepareHeader(BROADCAST, len)
template syncHeader*(len: int): string = prepareHeader(SYNC, len)