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
|
|
|
|
|
|
|
|
type secret_key = Sodium.Sign.secret_key
|
|
|
|
type public_key = Sodium.Sign.public_key
|
|
|
|
type signature = MBytes.t
|
|
|
|
|
|
|
|
let sign key msg =
|
|
|
|
Sodium.Sign.Bigbytes.(of_signature @@ sign_detached key msg)
|
|
|
|
|
|
|
|
let check_signature public_key signature msg =
|
|
|
|
try
|
|
|
|
Sodium.Sign.Bigbytes.(verify public_key (to_signature signature) msg) ;
|
|
|
|
true
|
|
|
|
with _ -> false
|
|
|
|
|
|
|
|
let append_signature key msg =
|
|
|
|
MBytes.concat msg (sign key msg)
|
|
|
|
|
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
|
|
|
|
|
2016-11-14 18:55:24 +04:00
|
|
|
let hash v =
|
|
|
|
Public_key_hash.hash_bytes
|
|
|
|
[ Sodium.Sign.Bigbytes.of_public_key v ]
|
|
|
|
|
|
|
|
let generate_key () =
|
|
|
|
let secret, pub = Sodium.Sign.random_keypair () in
|
|
|
|
(hash pub, pub, secret)
|
|
|
|
|
2017-02-19 21:22:32 +04:00
|
|
|
type Base58.data +=
|
2016-11-14 18:55:24 +04:00
|
|
|
| Public_key of public_key
|
|
|
|
| Secret_key of secret_key
|
|
|
|
| Signature of signature
|
|
|
|
|
2017-02-19 21:22:32 +04:00
|
|
|
let b58check_public_key_encoding =
|
|
|
|
Base58.register_encoding
|
|
|
|
~prefix: Base58.Prefix.ed25519_public_key
|
|
|
|
~length:Sodium.Sign.public_key_size
|
2016-11-14 18:55:24 +04:00
|
|
|
~to_raw:(fun x -> Bytes.to_string (Sodium.Sign.Bytes.of_public_key x))
|
2016-11-14 19:26:34 +04:00
|
|
|
~of_raw:(fun x ->
|
|
|
|
try Some (Sodium.Sign.Bytes.to_public_key (Bytes.of_string x))
|
|
|
|
with _ -> None)
|
2016-11-14 18:55:24 +04:00
|
|
|
~wrap:(fun x -> Public_key x)
|
|
|
|
|
2017-02-19 21:22:32 +04:00
|
|
|
let b58check_secret_key_encoding =
|
|
|
|
Base58.register_encoding
|
|
|
|
~prefix: Base58.Prefix.ed25519_secret_key
|
|
|
|
~length:Sodium.Sign.secret_key_size
|
2016-11-14 18:55:24 +04:00
|
|
|
~to_raw:(fun x -> Bytes.to_string (Sodium.Sign.Bytes.of_secret_key x))
|
2016-11-14 19:26:34 +04:00
|
|
|
~of_raw:(fun x ->
|
|
|
|
try Some (Sodium.Sign.Bytes.to_secret_key (Bytes.of_string x))
|
|
|
|
with _ -> None)
|
2016-11-14 18:55:24 +04:00
|
|
|
~wrap:(fun x -> Secret_key x)
|
|
|
|
|
2017-02-19 21:22:32 +04:00
|
|
|
let b58check_signature_encoding =
|
|
|
|
Base58.register_encoding
|
|
|
|
~prefix: Base58.Prefix.ed25519_signature
|
|
|
|
~length:Sodium.Sign.signature_size
|
2016-11-14 18:55:24 +04:00
|
|
|
~to_raw:MBytes.to_string
|
2016-11-14 19:26:34 +04:00
|
|
|
~of_raw:(fun s -> Some (MBytes.of_string s))
|
2016-11-14 18:55:24 +04:00
|
|
|
~wrap:(fun x -> Signature x)
|
|
|
|
|
2017-02-19 21:22:32 +04:00
|
|
|
let () =
|
|
|
|
Base58.check_encoded_prefix b58check_public_key_encoding "edpk" 54 ;
|
|
|
|
Base58.check_encoded_prefix b58check_secret_key_encoding "edsk" 98 ;
|
|
|
|
Base58.check_encoded_prefix b58check_signature_encoding "edsig" 99
|
|
|
|
|
2016-11-14 18:55:24 +04:00
|
|
|
let public_key_encoding =
|
|
|
|
let open Data_encoding in
|
|
|
|
splitted
|
|
|
|
~json:
|
|
|
|
(describe
|
2017-02-19 21:22:32 +04:00
|
|
|
~title: "An Ed25519 public key (Base58Check encoded)" @@
|
2016-11-14 18:55:24 +04:00
|
|
|
conv
|
2017-02-19 21:22:32 +04:00
|
|
|
(fun s -> Base58.simple_encode b58check_public_key_encoding s)
|
2016-11-14 18:55:24 +04:00
|
|
|
(fun s ->
|
2017-02-19 21:22:32 +04:00
|
|
|
match Base58.simple_decode b58check_public_key_encoding s with
|
2016-11-14 18:55:24 +04:00
|
|
|
| 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
|
|
|
|
bytes)
|
|
|
|
|
|
|
|
let secret_key_encoding =
|
|
|
|
let open Data_encoding in
|
|
|
|
splitted
|
|
|
|
~json:
|
|
|
|
(describe
|
2017-02-19 21:22:32 +04:00
|
|
|
~title: "An Ed25519 secret key (Base58Check encoded)" @@
|
2016-11-14 18:55:24 +04:00
|
|
|
conv
|
2017-02-19 21:22:32 +04:00
|
|
|
(fun s -> Base58.simple_encode b58check_secret_key_encoding s)
|
2016-11-14 18:55:24 +04:00
|
|
|
(fun s ->
|
2017-02-19 21:22:32 +04:00
|
|
|
match Base58.simple_decode b58check_secret_key_encoding s with
|
2016-11-14 18:55:24 +04:00
|
|
|
| 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
|
|
|
|
bytes)
|
|
|
|
|
|
|
|
let signature_encoding =
|
|
|
|
let open Data_encoding in
|
|
|
|
splitted
|
|
|
|
~json:
|
|
|
|
(describe
|
2017-02-19 21:22:32 +04:00
|
|
|
~title: "An Ed25519 signature (Base58Check encoded)" @@
|
2016-11-14 18:55:24 +04:00
|
|
|
conv
|
2017-02-19 21:22:32 +04:00
|
|
|
(fun s -> Base58.simple_encode b58check_signature_encoding s)
|
2016-11-14 18:55:24 +04:00
|
|
|
(fun s ->
|
2017-02-19 21:22:32 +04:00
|
|
|
match Base58.simple_decode b58check_signature_encoding s with
|
2016-11-14 18:55:24 +04:00
|
|
|
| Some x -> x
|
|
|
|
| None -> Data_encoding.Json.cannot_destruct
|
|
|
|
"Ed25519 signature: unexpected prefix.")
|
|
|
|
string)
|
|
|
|
~binary: (Fixed.bytes 64)
|
|
|
|
|
|
|
|
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
|
|
|
|
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
|
|
|
|
module Persist = Persist
|
|
|
|
module RPC = RPC
|
|
|
|
module Fitness = Fitness
|
|
|
|
module Updater = Updater
|
|
|
|
module Error_monad = struct
|
|
|
|
type error_category = [ `Branch | `Temporary | `Permanent ]
|
|
|
|
include Error_monad.Make()
|
|
|
|
end
|
|
|
|
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
|
|
|
|
|
|
|
|
module type PACKED_PROTOCOL = sig
|
|
|
|
val hash : Protocol_hash.t
|
|
|
|
include Updater.PROTOCOL
|
|
|
|
val error_encoding : error Data_encoding.t
|
|
|
|
val classify_errors : error list -> [ `Branch | `Temporary | `Permanent ]
|
|
|
|
val pp : Format.formatter -> error -> unit
|
2017-02-19 21:22:32 +04:00
|
|
|
val complete_b58prefix : Context.t -> string -> string list Lwt.t
|
2016-11-14 20:28:37 +04:00
|
|
|
end
|
|
|
|
|
2016-09-08 21:13:10 +04:00
|
|
|
end
|