Source file p2p_identity.ml
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
type t = {
peer_id : P2p_peer.Id.t;
public_key : Tezos_crypto.Crypto_box.public_key;
secret_key : Tezos_crypto.Crypto_box.secret_key;
proof_of_work_stamp : Tezos_crypto.Crypto_box.nonce;
}
let encoding =
let open Data_encoding in
def
"p2p_identity"
~description:
"The identity of a peer. This includes cryptographic keys as well as a \
proof-of-work."
@@ conv
(fun {peer_id; public_key; secret_key; proof_of_work_stamp} ->
(Some peer_id, public_key, secret_key, proof_of_work_stamp))
(fun (peer_id_opt, public_key, secret_key, proof_of_work_stamp) ->
let peer_id =
match peer_id_opt with
| Some peer_id -> peer_id
| None -> Tezos_crypto.Crypto_box.hash public_key
in
{peer_id; public_key; secret_key; proof_of_work_stamp})
(obj4
(opt "peer_id" P2p_peer_id.encoding)
(req "public_key" Tezos_crypto.Crypto_box.public_key_encoding)
(req "secret_key" Tezos_crypto.Crypto_box.secret_key_encoding)
(req "proof_of_work_stamp" Tezos_crypto.Crypto_box.nonce_encoding))
let generate_with_bound ?yield_every ?max pow_target =
let open Error_monad.Lwt_syntax in
let secret_key, public_key, peer_id =
Tezos_crypto.Crypto_box.random_keypair ()
in
let+ proof_of_work_stamp =
Tezos_crypto.Crypto_box.generate_proof_of_work
?yield_every
?max
public_key
pow_target
in
{peer_id; public_key; secret_key; proof_of_work_stamp}
let generate ?yield_every pow_target =
generate_with_bound ?yield_every pow_target
let generate_with_pow_target_0 () =
let secret_key, public_key, peer_id =
Tezos_crypto.Crypto_box.random_keypair ()
in
let proof_of_work_stamp =
Tezos_crypto.Crypto_box.generate_proof_of_work_with_target_0 public_key
in
{peer_id; public_key; secret_key; proof_of_work_stamp}
let () = Data_encoding.Registration.register encoding