From 4f2b2f8ecee494ec4694075c57224dca9f525fb8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gr=C3=A9goire=20Henry?= Date: Thu, 5 Apr 2018 16:19:26 +0200 Subject: [PATCH] Alpha: use Base58 for displaying commitment --- src/lib_crypto/s.ml | 3 -- src/lib_protocol_environment/sigs/v1/s.mli | 3 -- .../tezos_protocol_environment.ml | 3 -- .../src/blinded_public_key_hash.ml | 30 ++++++++----------- .../src/blinded_public_key_hash.mli | 7 +---- .../lib_protocol/src/commitment_storage.ml | 26 ++++++++-------- .../src/unclaimed_public_key_hash.ml | 27 +++++++++++++---- .../src/unclaimed_public_key_hash.mli | 4 ++- .../lib_protocol/test/test_activation.ml | 4 +-- 9 files changed, 53 insertions(+), 54 deletions(-) diff --git a/src/lib_crypto/s.ml b/src/lib_crypto/s.ml index a577f9d15..629bedbf2 100644 --- a/src/lib_crypto/s.ml +++ b/src/lib_crypto/s.ml @@ -156,9 +156,6 @@ module type SIGNATURE = sig type t - val hash_bytes: ?key:MBytes.t -> MBytes.t list -> t - val hash_string: ?key:string -> string list -> t - val pp: Format.formatter -> t -> unit val pp_short: Format.formatter -> t -> unit include Compare.S with type t := t diff --git a/src/lib_protocol_environment/sigs/v1/s.mli b/src/lib_protocol_environment/sigs/v1/s.mli index b3e8c5212..793ec4f30 100644 --- a/src/lib_protocol_environment/sigs/v1/s.mli +++ b/src/lib_protocol_environment/sigs/v1/s.mli @@ -209,9 +209,6 @@ module type SIGNATURE = sig type t - val hash_bytes: ?key:MBytes.t -> MBytes.t list -> t - val hash_string: ?key:string -> string list -> t - val pp: Format.formatter -> t -> unit val pp_short: Format.formatter -> t -> unit include Compare.S with type t := t diff --git a/src/lib_protocol_environment/tezos_protocol_environment.ml b/src/lib_protocol_environment/tezos_protocol_environment.ml index 12ab93b2c..59f5a91eb 100644 --- a/src/lib_protocol_environment/tezos_protocol_environment.ml +++ b/src/lib_protocol_environment/tezos_protocol_environment.ml @@ -269,9 +269,6 @@ module Make (Context : CONTEXT) = struct type t - val hash_bytes: ?key:MBytes.t -> MBytes.t list -> t - val hash_string: ?key:string -> string list -> t - val pp: Format.formatter -> t -> unit val pp_short: Format.formatter -> t -> unit include Compare.S with type t := t diff --git a/src/proto_alpha/lib_protocol/src/blinded_public_key_hash.ml b/src/proto_alpha/lib_protocol/src/blinded_public_key_hash.ml index 77c238d64..20899cbf6 100644 --- a/src/proto_alpha/lib_protocol/src/blinded_public_key_hash.ml +++ b/src/proto_alpha/lib_protocol/src/blinded_public_key_hash.ml @@ -7,30 +7,24 @@ (* *) (**************************************************************************) -type t = MBytes.t +include Blake2B.Make(Base58)(struct + let name = "Blinded public key hash" + let title = "A blinded public key hash" + let b58check_prefix = "\001\002\049\223" + let size = Some Ed25519.Public_key_hash.size + end) + +let () = + Base58.check_encoded_prefix b58check_encoding "btz1" 37 + +let of_ed25519_pkh secret pkh = + hash_bytes ~key:secret [ Ed25519.Public_key_hash.to_bytes pkh ] type secret = MBytes.t -let size = Ed25519.Public_key_hash.size let secret_size = Ed25519.Public_key_hash.size - -let encoding = Data_encoding.Fixed.bytes size let secret_encoding = Data_encoding.Fixed.bytes secret_size -let of_ed25519_pkh secret pkh = - Ed25519.Public_key_hash.to_bytes @@ - Ed25519.Public_key_hash.hash_bytes - ~key:secret - [ Ed25519.Public_key_hash.to_bytes pkh ] - -let compare = MBytes.compare -let (=) = MBytes.(=) - -let of_hex h = - if Compare.Int.(String.length h <> size * 2) then - invalid_arg "Blinded_public_key_hash.of_hex" ; - MBytes.of_hex (`Hex h) - let secret_of_hex h = if Compare.Int.(String.length h <> secret_size * 2) then invalid_arg "Blinded_public_key_hash.secret_of_hex" ; diff --git a/src/proto_alpha/lib_protocol/src/blinded_public_key_hash.mli b/src/proto_alpha/lib_protocol/src/blinded_public_key_hash.mli index 87b220fcc..a873918e5 100644 --- a/src/proto_alpha/lib_protocol/src/blinded_public_key_hash.mli +++ b/src/proto_alpha/lib_protocol/src/blinded_public_key_hash.mli @@ -7,16 +7,11 @@ (* *) (**************************************************************************) -type t -val encoding : t Data_encoding.t +include S.HASH type secret val secret_encoding : secret Data_encoding.t val of_ed25519_pkh : secret -> Ed25519.Public_key_hash.t -> t -val of_hex : string -> t val secret_of_hex : string -> secret - -val compare : t -> t -> int -val (=) : t -> t -> bool diff --git a/src/proto_alpha/lib_protocol/src/commitment_storage.ml b/src/proto_alpha/lib_protocol/src/commitment_storage.ml index c9ca007a3..6f2d7c706 100644 --- a/src/proto_alpha/lib_protocol/src/commitment_storage.ml +++ b/src/proto_alpha/lib_protocol/src/commitment_storage.ml @@ -10,22 +10,22 @@ open Commitment_repr let test_commitments = [ - "dca88243fece75e9c22e", "4a6af2f5c466bf0a7a1001a1e9468cbfca82cef6", 1868898542104130027; - "c736bfb7074f69bee133", "32d7b02afc218623b4e2fd85b84b06f0a1d202d2", 517697389496079974; - "e07bb6ba71082141eae0", "17a0241048b13857abe19db7fa11ac63de3eda5e", 962290491831710023; - "17efe5e32c28126c4e94", "7484c711d0cbd8ba6e7f9965311a4903ea17b80a", 1233665184704419921; - "5c742b7e335b265cfa82", "3ca39ae8ddd026030af633561382d4e16c8c2cae", 131959324067470008; - "40196c01a502608d7f22", "ebb81e0f79c568c0181e9db9cdafde7a8db65f82", 112378240876120002; - "d7abb1cd59a66ce3fc42", "f8e91e7adba8cc32ce848a43e440de7c3e4f0866", 1060667014046690017; - "f7a1e97970689cce7291", "4f964fbe29971a85c7152541519b1dbb8e436184", 71300478465380003; - "f59ee7283f7eda5b3c03", "591e167977f9c4739ab17cc9d40a672737b45fa1", 283380756728119992; - "076b7feedde492164ca3", "8a5bb1da65cbbc7f3acdc3a3dae22b43364d80a6", 1357762577679880028 + "tz1fkmDXEQdua3u71JBVh4eGUGqK4t1G8xhZ", "btz1T77Ly5U1bWNBR5KzDSgNFST5Bh5F1eB6g", 1868898542104130027L ; + "tz1doNkK6RKaRswsuKJV4erT6HauYSm9fuHi", "btz1QxTPgszARgWioEor3eMehxW3osfhw3KoJ", 517697389496079974L ; + "tz1g6zFsci4YR8p1MJrkyc6wAKizR6mqJmyQ", "btz1NUYc1tV5VBksMNuQG4AuZF9Xudh1sDJni", 962290491831710023L ; + "tz1MpbcwGFWVBBWoxwm6iQH5Hzh9mCXbnETJ", "btz1Wwifd8vbQqnbuzSbQLvJEjQ9FUoxVJm68", 1233665184704419921L ; + "tz1U4t2PmX5cZVUui4BNaiRVokLa6AxB5G9Z", "btz1RrFkp9GmnypoNGRSyURkBQNUs4PPYG8SR", 131959324067470008L ; + "tz1RUxPjviua4XJM78XjSKGUCAS9R3y8Bdof", "btz1hozabzP9HdRakJddzyea7DgPHzJ5PB37N", 112378240876120002L ; + "tz1fJPeueQKJrTj2SFV2PmyCinLYJKXCEMf4", "btz1j1k4nrZB4r8RpTYiy8zbi3Fappopkb8ZF", 1060667014046690017L ; + "tz1iDPZLxcGf5CqCNpTuuMdtu3zKpJ6HvvFR", "btz1TaSfoSNhFoqwqbPC9iC19rN24KJtB7skD", 71300478465380003L ; + "tz1i2kbtVu65dP739qLGRpJNujRM8pdpkH3p", "btz1USqQRuvASPXwseXkGTWeWv4dn3VwMVPEk", 283380756728119992L ; + "tz1LKGCg6ESJLDviHkT8Jc7tUwjw4h3d9MaF", "btz1YwCKMbBLRL1qkBjAHGCwjbWDqiTAEFpbw", 1357762577679880028L ; ] let init_commitment ctxt (hpkh, blind, amount) = - let half_public_key_hash = Unclaimed_public_key_hash.of_hex hpkh in - let blinded_public_key_hash = Blinded_public_key_hash.of_hex blind in - let amount = Tez_repr.of_mutez_exn (Int64.of_int amount) in + let half_public_key_hash = Unclaimed_public_key_hash.of_b58check_exn hpkh in + let blinded_public_key_hash = Blinded_public_key_hash.of_b58check_exn blind in + let amount = Tez_repr.of_mutez_exn amount in Storage.Commitments.init ctxt half_public_key_hash { blinded_public_key_hash ; amount } diff --git a/src/proto_alpha/lib_protocol/src/unclaimed_public_key_hash.ml b/src/proto_alpha/lib_protocol/src/unclaimed_public_key_hash.ml index b561c079e..fdf37a34b 100644 --- a/src/proto_alpha/lib_protocol/src/unclaimed_public_key_hash.ml +++ b/src/proto_alpha/lib_protocol/src/unclaimed_public_key_hash.ml @@ -14,12 +14,29 @@ let size = Ed25519.Public_key_hash.size / 2 let of_ed25519_pkh pkh = MBytes.sub (Ed25519.Public_key_hash.to_bytes pkh) 0 size -let encoding = Data_encoding.Fixed.bytes size +let zero = MBytes.of_string (String.make size '\000') -let of_hex h = - if Compare.Int.(String.length h <> size * 2) then - invalid_arg "Blinded_public_key_hash.of_hex" ; - MBytes.of_hex (`Hex h) +let to_b58check s = + Ed25519.Public_key_hash.to_b58check + (Ed25519.Public_key_hash.of_bytes_exn + (MBytes.concat s zero)) + +let of_b58check_exn s = + let pkh = Ed25519.Public_key_hash.of_b58check_exn s in + let padding = MBytes.sub (Ed25519.Public_key_hash.to_bytes pkh) size size in + if MBytes.(<>) zero padding then + failwith "invalid Base58Check-encoded unclaimed public-key hash" ; + of_ed25519_pkh pkh + +let encoding = + let open Data_encoding in + splitted + ~binary:(Fixed.bytes size) + ~json: + (conv + to_b58check + of_b58check_exn + string) module Index = struct diff --git a/src/proto_alpha/lib_protocol/src/unclaimed_public_key_hash.mli b/src/proto_alpha/lib_protocol/src/unclaimed_public_key_hash.mli index fe5658e77..701d86ee3 100644 --- a/src/proto_alpha/lib_protocol/src/unclaimed_public_key_hash.mli +++ b/src/proto_alpha/lib_protocol/src/unclaimed_public_key_hash.mli @@ -12,7 +12,9 @@ type t val encoding : t Data_encoding.t val of_ed25519_pkh : Ed25519.Public_key_hash.t -> t -val of_hex : string -> t + +val to_b58check : t -> string +val of_b58check_exn : string -> t module Index : sig type nonrec t = t diff --git a/src/proto_alpha/lib_protocol/test/test_activation.ml b/src/proto_alpha/lib_protocol/test/test_activation.ml index 919f95b86..4af103a78 100644 --- a/src/proto_alpha/lib_protocol/test/test_activation.ml +++ b/src/proto_alpha/lib_protocol/test/test_activation.ml @@ -46,8 +46,8 @@ let given_secret = "c5422e3864b9e6c5260e2aac76ea0f3d28d4fff7" let expected_blinded_pkh = - Blinded_public_key_hash.of_hex - "4a6af2f5c466bf0a7a1001a1e9468cbfca82cef6" + Blinded_public_key_hash.of_b58check_exn + "btz1T77Ly5U1bWNBR5KzDSgNFST5Bh5F1eB6g" let expected_amount = match Tez.of_mutez 1868898542104130027L with