2016-09-08 21:13:10 +04:00
|
|
|
(**************************************************************************)
|
|
|
|
(* *)
|
|
|
|
(* Copyright (c) 2014 - 2016. *)
|
|
|
|
(* Dynamic Ledger Solutions, Inc. <contact@tezos.com> *)
|
|
|
|
(* *)
|
|
|
|
(* All rights reserved. No warranty, explicit or implicit, provided. *)
|
|
|
|
(* *)
|
|
|
|
(**************************************************************************)
|
|
|
|
|
2016-11-14 18:55:24 +04:00
|
|
|
module Ed25519 = struct
|
|
|
|
|
2017-02-19 21:22:32 +04:00
|
|
|
module Public_key_hash = Hash.Make_Blake2B(Base58)(struct
|
2016-11-14 18:55:24 +04:00
|
|
|
let name = "Ed25519.Public_key_hash"
|
|
|
|
let title = "An Ed25519 public key ID"
|
2017-02-19 21:22:32 +04:00
|
|
|
let b58check_prefix = Base58.Prefix.ed25519_public_key_hash
|
2016-11-25 22:46:50 +04:00
|
|
|
let size = Some 20
|
2016-11-14 18:55:24 +04:00
|
|
|
end)
|
|
|
|
|
2017-02-19 21:22:32 +04:00
|
|
|
let () =
|
|
|
|
Base58.check_encoded_prefix Public_key_hash.b58check_encoding "tz1" 36
|
|
|
|
|
2017-02-28 05:56:40 +04:00
|
|
|
module Public_key = struct
|
|
|
|
|
|
|
|
type t = Sodium.Sign.public_key
|
|
|
|
let compare = Sodium.Sign.compare_public_keys
|
|
|
|
let (=) xs ys = compare xs ys = 0
|
|
|
|
let (<>) xs ys = compare xs ys <> 0
|
|
|
|
let (<) xs ys = compare xs ys < 0
|
|
|
|
let (<=) xs ys = compare xs ys <= 0
|
|
|
|
let (>=) xs ys = compare xs ys >= 0
|
|
|
|
let (>) xs ys = compare xs ys > 0
|
|
|
|
let max x y = if x >= y then x else y
|
|
|
|
let min x y = if x <= y then x else y
|
|
|
|
|
|
|
|
type Base58.data +=
|
|
|
|
| Public_key of t
|
|
|
|
|
|
|
|
let b58check_encoding =
|
|
|
|
Base58.register_encoding
|
|
|
|
~prefix: Base58.Prefix.ed25519_public_key
|
|
|
|
~length:Sodium.Sign.public_key_size
|
|
|
|
~to_raw:(fun x -> Bytes.to_string (Sodium.Sign.Bytes.of_public_key x))
|
|
|
|
~of_raw:(fun x ->
|
|
|
|
try Some (Sodium.Sign.Bytes.to_public_key (Bytes.of_string x))
|
|
|
|
with _ -> None)
|
|
|
|
~wrap:(fun x -> Public_key x)
|
|
|
|
|
2017-04-05 11:54:21 +04:00
|
|
|
let of_b58check_opt s = Base58.simple_decode b58check_encoding s
|
|
|
|
let of_b58check_exn s =
|
2017-02-28 05:56:40 +04:00
|
|
|
match Base58.simple_decode b58check_encoding s with
|
|
|
|
| Some x -> x
|
|
|
|
| None -> Pervasives.failwith "Unexpected hash (ed25519 public key)"
|
2017-04-05 11:54:21 +04:00
|
|
|
let of_b58check s =
|
|
|
|
match Base58.simple_decode b58check_encoding s with
|
|
|
|
| Some x -> Ok x
|
|
|
|
| None -> generic_error "Unexpected hash (ed25519 public key)"
|
2017-02-28 05:56:40 +04:00
|
|
|
let to_b58check s = Base58.simple_encode b58check_encoding s
|
|
|
|
|
|
|
|
let of_bytes s = Sodium.Sign.Bytes.to_public_key s
|
|
|
|
|
2017-04-05 12:22:41 +04:00
|
|
|
let param ?(name="ed25519-public") ?(desc="Ed25519 public key (b58check-encoded)") t =
|
2017-09-27 11:55:20 +04:00
|
|
|
Cli_entries.(param ~name ~desc (parameter (fun _ str -> Lwt.return (of_b58check str))) t)
|
2017-04-05 12:22:41 +04:00
|
|
|
|
2017-02-28 05:56:40 +04:00
|
|
|
let () =
|
|
|
|
Base58.check_encoded_prefix b58check_encoding "edpk" 54
|
|
|
|
|
|
|
|
let encoding =
|
|
|
|
let open Data_encoding in
|
|
|
|
splitted
|
|
|
|
~json:
|
|
|
|
(describe
|
|
|
|
~title: "An Ed25519 public key (Base58Check encoded)" @@
|
|
|
|
conv
|
|
|
|
(fun s -> Base58.simple_encode b58check_encoding s)
|
|
|
|
(fun s ->
|
|
|
|
match Base58.simple_decode b58check_encoding s with
|
|
|
|
| Some x -> x
|
|
|
|
| None -> Data_encoding.Json.cannot_destruct
|
|
|
|
"Ed25519 public key: unexpected prefix.")
|
|
|
|
string)
|
|
|
|
~binary:
|
|
|
|
(conv
|
|
|
|
Sodium.Sign.Bigbytes.of_public_key
|
|
|
|
Sodium.Sign.Bigbytes.to_public_key
|
2017-04-10 23:14:17 +04:00
|
|
|
(Fixed.bytes Sodium.Sign.public_key_size))
|
2017-02-28 05:56:40 +04:00
|
|
|
|
|
|
|
let hash v =
|
|
|
|
Public_key_hash.hash_bytes
|
|
|
|
[ Sodium.Sign.Bigbytes.of_public_key v ]
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
module Secret_key = struct
|
|
|
|
|
|
|
|
type t = Sodium.Sign.secret_key
|
|
|
|
|
|
|
|
type Base58.data +=
|
|
|
|
| Secret_key of t
|
|
|
|
|
|
|
|
let b58check_encoding =
|
|
|
|
Base58.register_encoding
|
|
|
|
~prefix: Base58.Prefix.ed25519_secret_key
|
|
|
|
~length:Sodium.Sign.secret_key_size
|
|
|
|
~to_raw:(fun x -> Bytes.to_string (Sodium.Sign.Bytes.of_secret_key x))
|
|
|
|
~of_raw:(fun x ->
|
|
|
|
try Some (Sodium.Sign.Bytes.to_secret_key (Bytes.of_string x))
|
|
|
|
with _ -> None)
|
|
|
|
~wrap:(fun x -> Secret_key x)
|
|
|
|
|
2017-04-05 11:54:21 +04:00
|
|
|
let of_b58check_opt s = Base58.simple_decode b58check_encoding s
|
|
|
|
let of_b58check_exn s =
|
2017-02-28 05:56:40 +04:00
|
|
|
match Base58.simple_decode b58check_encoding s with
|
|
|
|
| Some x -> x
|
|
|
|
| None -> Pervasives.failwith "Unexpected hash (ed25519 secret key)"
|
2017-04-05 11:54:21 +04:00
|
|
|
let of_b58check s =
|
|
|
|
match Base58.simple_decode b58check_encoding s with
|
|
|
|
| Some x -> Ok x
|
|
|
|
| None -> generic_error "Unexpected hash (ed25519 public key)"
|
2017-02-28 05:56:40 +04:00
|
|
|
let to_b58check s = Base58.simple_encode b58check_encoding s
|
|
|
|
|
|
|
|
let of_bytes s = Sodium.Sign.Bytes.to_secret_key s
|
|
|
|
|
2017-04-05 12:22:41 +04:00
|
|
|
let param ?(name="ed25519-secret") ?(desc="Ed25519 secret key (b58check-encoded)") t =
|
2017-09-27 11:55:20 +04:00
|
|
|
Cli_entries.(param ~name ~desc (parameter (fun _ str -> Lwt.return (of_b58check str))) t)
|
2017-04-05 12:22:41 +04:00
|
|
|
|
2017-02-28 05:56:40 +04:00
|
|
|
let () =
|
|
|
|
Base58.check_encoded_prefix b58check_encoding "edsk" 98
|
|
|
|
|
|
|
|
let encoding =
|
|
|
|
let open Data_encoding in
|
|
|
|
splitted
|
|
|
|
~json:
|
|
|
|
(describe
|
|
|
|
~title: "An Ed25519 secret key (Base58Check encoded)" @@
|
|
|
|
conv
|
|
|
|
(fun s -> Base58.simple_encode b58check_encoding s)
|
|
|
|
(fun s ->
|
|
|
|
match Base58.simple_decode b58check_encoding s with
|
|
|
|
| Some x -> x
|
|
|
|
| None -> Data_encoding.Json.cannot_destruct
|
|
|
|
"Ed25519 secret key: unexpected prefix.")
|
|
|
|
string)
|
|
|
|
~binary:
|
|
|
|
(conv
|
|
|
|
Sodium.Sign.Bigbytes.of_secret_key
|
|
|
|
Sodium.Sign.Bigbytes.to_secret_key
|
2017-04-10 23:14:17 +04:00
|
|
|
(Fixed.bytes Sodium.Sign.secret_key_size))
|
2017-02-28 05:56:40 +04:00
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
let sign key msg =
|
|
|
|
Sodium.Sign.Bigbytes.(of_signature @@ sign_detached key msg)
|
|
|
|
|
|
|
|
module Signature = struct
|
|
|
|
|
|
|
|
type t = MBytes.t
|
|
|
|
|
|
|
|
type Base58.data +=
|
|
|
|
| Signature of t
|
|
|
|
|
|
|
|
let b58check_encoding =
|
|
|
|
Base58.register_encoding
|
|
|
|
~prefix: Base58.Prefix.ed25519_signature
|
|
|
|
~length:Sodium.Sign.signature_size
|
|
|
|
~to_raw:MBytes.to_string
|
|
|
|
~of_raw:(fun s -> Some (MBytes.of_string s))
|
|
|
|
~wrap:(fun x -> Signature x)
|
|
|
|
|
2017-04-05 11:54:21 +04:00
|
|
|
let of_b58check_opt s = Base58.simple_decode b58check_encoding s
|
|
|
|
let of_b58check_exn s =
|
2017-02-28 05:56:40 +04:00
|
|
|
match Base58.simple_decode b58check_encoding s with
|
|
|
|
| Some x -> x
|
|
|
|
| None -> Pervasives.failwith "Unexpected hash (ed25519 signature)"
|
2017-04-05 11:54:21 +04:00
|
|
|
let of_b58check s =
|
|
|
|
match Base58.simple_decode b58check_encoding s with
|
|
|
|
| Some x -> Ok x
|
|
|
|
| None -> generic_error "Unexpected hash (ed25519 public key)"
|
2017-02-28 05:56:40 +04:00
|
|
|
let to_b58check s = Base58.simple_encode b58check_encoding s
|
|
|
|
|
|
|
|
let of_bytes s = MBytes.of_string (Bytes.to_string s)
|
|
|
|
|
2017-04-05 12:22:41 +04:00
|
|
|
let param ?(name="signature") ?(desc="Signature (b58check-encoded)") t =
|
2017-09-27 11:55:20 +04:00
|
|
|
Cli_entries.(param ~name ~desc (parameter (fun _ str -> Lwt.return (of_b58check str))) t)
|
2017-04-05 12:22:41 +04:00
|
|
|
|
2017-02-28 05:56:40 +04:00
|
|
|
let () =
|
|
|
|
Base58.check_encoded_prefix b58check_encoding "edsig" 99
|
|
|
|
|
|
|
|
let encoding =
|
|
|
|
let open Data_encoding in
|
|
|
|
splitted
|
|
|
|
~json:
|
|
|
|
(describe
|
|
|
|
~title: "An Ed25519 signature (Base58Check encoded)" @@
|
|
|
|
conv
|
|
|
|
(fun s -> Base58.simple_encode b58check_encoding s)
|
|
|
|
(fun s ->
|
|
|
|
match Base58.simple_decode b58check_encoding s with
|
|
|
|
| Some x -> x
|
|
|
|
| None -> Data_encoding.Json.cannot_destruct
|
|
|
|
"Ed25519 signature: unexpected prefix.")
|
|
|
|
string)
|
2017-04-10 23:14:17 +04:00
|
|
|
~binary: (Fixed.bytes Sodium.Sign.signature_size)
|
2017-02-28 05:56:40 +04:00
|
|
|
|
|
|
|
let check public_key signature msg =
|
|
|
|
try
|
|
|
|
Sodium.Sign.Bigbytes.(verify public_key (to_signature signature) msg) ;
|
|
|
|
true
|
|
|
|
with _ -> false
|
|
|
|
|
|
|
|
let append key msg =
|
|
|
|
MBytes.concat msg (sign key msg)
|
|
|
|
|
|
|
|
end
|
2016-11-14 18:55:24 +04:00
|
|
|
|
|
|
|
let generate_key () =
|
|
|
|
let secret, pub = Sodium.Sign.random_keypair () in
|
2017-02-28 05:56:40 +04:00
|
|
|
(Public_key.hash pub, pub, secret)
|
2016-11-14 18:55:24 +04:00
|
|
|
|
|
|
|
end
|
2016-11-14 20:28:37 +04:00
|
|
|
|
|
|
|
module Make(Param : sig val name: string end)() = struct
|
|
|
|
|
|
|
|
include Pervasives
|
|
|
|
module Pervasives = Pervasives
|
|
|
|
module Compare = Compare
|
|
|
|
module Array = Array
|
|
|
|
module List = List
|
2016-11-17 05:06:50 +04:00
|
|
|
module Bytes = struct
|
|
|
|
include Bytes
|
|
|
|
include EndianBytes.BigEndian
|
|
|
|
module LE = EndianBytes.LittleEndian
|
|
|
|
end
|
|
|
|
module String = struct
|
|
|
|
include String
|
|
|
|
include EndianString.BigEndian
|
|
|
|
module LE = EndianString.LittleEndian
|
|
|
|
end
|
2016-11-14 20:28:37 +04:00
|
|
|
module Set = Set
|
|
|
|
module Map = Map
|
|
|
|
module Int32 = Int32
|
|
|
|
module Int64 = Int64
|
|
|
|
module Nativeint = Nativeint
|
|
|
|
module Buffer = Buffer
|
|
|
|
module Format = Format
|
|
|
|
module Hex_encode = Hex_encode
|
2017-05-23 17:04:31 +04:00
|
|
|
module Z = Z
|
2016-11-14 20:28:37 +04:00
|
|
|
module Lwt_sequence = Lwt_sequence
|
|
|
|
module Lwt = Lwt
|
|
|
|
module Lwt_list = Lwt_list
|
|
|
|
module MBytes = MBytes
|
|
|
|
module Uri = Uri
|
|
|
|
module Data_encoding = Data_encoding
|
|
|
|
module Time = Time
|
|
|
|
module Ed25519 = Ed25519
|
|
|
|
module Hash = Hash
|
2017-04-19 21:21:23 +04:00
|
|
|
module Tezos_data = Tezos_data
|
2016-11-14 20:28:37 +04:00
|
|
|
module Persist = Persist
|
|
|
|
module RPC = RPC
|
2017-09-29 18:20:10 +04:00
|
|
|
module Micheline = Micheline
|
2016-11-14 20:28:37 +04:00
|
|
|
module Fitness = Fitness
|
|
|
|
module Error_monad = struct
|
|
|
|
type error_category = [ `Branch | `Temporary | `Permanent ]
|
|
|
|
include Error_monad.Make()
|
|
|
|
end
|
2017-10-09 12:55:12 +04:00
|
|
|
module Updater = struct
|
|
|
|
include Updater
|
|
|
|
module type PROTOCOL =
|
|
|
|
RAW_PROTOCOL with type error := Error_monad.error
|
|
|
|
and type 'a tzresult := 'a Error_monad.tzresult
|
|
|
|
end
|
2016-11-14 20:28:37 +04:00
|
|
|
module Logging = Logging.Make(Param)
|
2017-02-19 21:22:32 +04:00
|
|
|
module Base58 = struct
|
|
|
|
include Base58
|
|
|
|
let simple_encode enc s = simple_encode enc s
|
|
|
|
let simple_decode enc s = simple_decode enc s
|
2016-11-14 20:28:37 +04:00
|
|
|
include Make(struct type context = Context.t end)
|
2017-02-19 21:22:32 +04:00
|
|
|
let decode s = decode s
|
2016-11-14 20:28:37 +04:00
|
|
|
end
|
|
|
|
module Context = struct
|
|
|
|
include Context
|
2017-02-19 21:22:32 +04:00
|
|
|
let register_resolver = Base58.register_resolver
|
|
|
|
let complete ctxt s = Base58.complete ctxt s
|
2016-11-14 20:28:37 +04:00
|
|
|
end
|
2017-10-27 20:53:07 +04:00
|
|
|
|
|
|
|
type error += Ecoproto_error of Error_monad.error list
|
|
|
|
|
|
|
|
let () =
|
|
|
|
let id = Format.asprintf "Ecoproto.%s" Param.name in
|
|
|
|
register_wrapped_error_kind
|
|
|
|
(fun ecoerrors -> Error_monad.classify_errors ecoerrors)
|
|
|
|
~id ~title:"Error returned by the protocol"
|
|
|
|
~description:"Wrapped error for the economic protocol."
|
|
|
|
~pp:(fun ppf ->
|
|
|
|
Format.fprintf ppf
|
|
|
|
"@[<v 2>Economic error:@ %a@]"
|
|
|
|
(Format.pp_print_list Error_monad.pp))
|
|
|
|
Data_encoding.(obj1 (req "ecoproto"
|
|
|
|
(list Error_monad.error_encoding)))
|
|
|
|
(function Ecoproto_error ecoerrors -> Some ecoerrors
|
|
|
|
| _ -> None )
|
|
|
|
(function ecoerrors -> Ecoproto_error ecoerrors)
|
|
|
|
|
|
|
|
let wrap_error = function
|
|
|
|
| Ok _ as ok -> ok
|
|
|
|
| Error errors -> Error [Ecoproto_error errors]
|
|
|
|
|
2016-09-08 21:13:10 +04:00
|
|
|
end
|