Commit 2497c4bf authored by Ryo33's avatar Ryo33

Add BlockBody

parent f4cf2953
type t = {
version : string;
previous_hash : Hash.t option;
hash_algorithm : HashAlgorithm.t;
expires_at : ExpirationDateTime.t;
minimum_required_power_for_next_block : Power.t;
commands : Command.t array;
signatures : Signature.t array }
type t = { body : BlockBody.t; signatures : Signature.t array }
type t
type t = { body : BlockBody.t; signatures : Signature.t array }
module YayakaHash = Hash
module YayakaCommand = Command
open Core
type t = {
version : string;
layer : int;
previous_hash : YayakaHash.t option;
hash_algorithm : HashAlgorithm.t;
fingerprint_algorithm : HashAlgorithm.t;
expires_at : ExpirationDateTime.t;
minimum_required_power : Power.t;
commands : YayakaCommand.t list; }
let format {
version; layer; previous_hash; hash_algorithm; fingerprint_algorithm;
expires_at; minimum_required_power; commands } =
Format.sprintf "YAYAKA %s\n%d\n%s\n%s\n%s\n%d\n%s\n%d\n%s"
version
layer
(Option.value_map previous_hash ~f:YayakaHash.format ~default:"\n")
(HashAlgorithm.to_string hash_algorithm)
(ExpirationDateTime.to_string expires_at)
(Power.to_int minimum_required_power)
(HashAlgorithm.to_string fingerprint_algorithm)
(List.length commands)
(commands |> List.map ~f:YayakaCommand.format |> String.concat ~sep:"\n")
type t = {
version : string;
layer : int;
previous_hash : Hash.t option;
hash_algorithm : HashAlgorithm.t;
fingerprint_algorithm : HashAlgorithm.t;
expires_at : ExpirationDateTime.t;
minimum_required_power : Power.t;
commands : Command.t list; }
val format : t -> string
......@@ -18,30 +18,36 @@ type t =
let format = function
| AddPublicKey { algorithm; power; key } ->
Format.sprintf "\
%d\n\
add\n\
%s\n\
%d\n\
%s"
(4 + String.count key ~f:(( = ) '\n'))
(PublicKeyAlgorithm.to_string algorithm) (Power.to_int power) key
| RemovePublicKey { fingerprint } ->
Format.sprintf "\
2\n\
remove\n\
%s"
fingerprint
| UpdatePower { fingerprint; power; } ->
Format.sprintf "\
3\n\
update\n\
%s\n\
%d"
fingerprint (Power.to_int power)
| Halt ->
"halt"
| Halt -> "\
1\n\
halt"
| UserCommand { protocol; body } ->
Format.sprintf "\
%d\n\
user\n\
%s\n\
%s"
protocol body
(3 + String.count body ~f:(( = ) '\n')) protocol body
let combine3 r1 r2 r3 ~ok ~err =
Result.combine
......
......@@ -8,7 +8,7 @@ let hash algorithm text =
let unwrap = function
| Ok result ->
let hex = String.chop_prefix_exn ~prefix:"(stdin)= " result in
Hex.to_string (`Hex hex)
Hex.to_string (`Hex hex) |> Base64.encode_exn
| _ -> raise (Failure "") in
match algorithm with
| Sha1 ->
......@@ -21,4 +21,4 @@ let hash algorithm text =
{ algorithm = algorithm; hash = unwrap result }
let format { algorithm; hash } =
HashAlgorithm.to_string algorithm, Base64.encode_exn hash
HashAlgorithm.to_string algorithm ^ "\n" ^ hash
......@@ -3,5 +3,5 @@ type t = { algorithm : HashAlgorithm.t; hash : string }
val hash : HashAlgorithm.t -> string -> t
(** computes a hash for the given string. *)
val format : t -> ( string * string )
val format : t -> string
(** formats the given hash for block. *)
......@@ -6,4 +6,4 @@ type t = {
let fingerprint { fingerprint_algorithm; key; _ } =
let Hash.{ hash; _ } = Hash.hash fingerprint_algorithm key in
Base64.encode_exn hash
hash
open Core
open YayakaChain
module Test = Alcotest
let a = Power.of_int 1
let expiration_date_time_of_string str =
str
|> ExpirationDateTime.of_string
|> Result.map_error ~f:(fun a -> Failure a)
|> Result.ok_exn
let test_format () =
Test.(check string) "with commands"
"\
YAYAKA 0.1\n\
1\n\
sha1\n\
hvfkN/qlp/zhXR3cuerq6jd2Z7g=\n\
sha256\n\
20190207120915\n\
3\n\
sha1\n\
2\n\
7\n\
add\n\
rsa\n\
2\n\
-----BEGIN PUBLIC KEY-----\n\
MB4wDQYJKoZIhvcNAQEBBQADDQAwCgIDAOCHAgMBAAE=\n\
-----END PUBLIC KEY-----\n\
\n\
3\n\
update\n\
qZk+NkcGgWq6PiVxeFDCbJzQ2J0=\n\
3"
(BlockBody.format BlockBody.{
version = "0.1";
layer = 1;
previous_hash = Some Hash.{
algorithm = HashAlgorithm.Sha1;
hash = "hvfkN/qlp/zhXR3cuerq6jd2Z7g="};
hash_algorithm = HashAlgorithm.Sha256;
fingerprint_algorithm = HashAlgorithm.Sha1;
expires_at = expiration_date_time_of_string "20190207120915";
minimum_required_power = Option.value_exn (Power.of_int 3);
commands = [
Command.AddPublicKey {
algorithm = PublicKeyAlgorithm.RSA;
power = Option.value_exn (Power.of_int 2);
key = "\
-----BEGIN PUBLIC KEY-----\n\
MB4wDQYJKoZIhvcNAQEBBQADDQAwCgIDAOCHAgMBAAE=\n\
-----END PUBLIC KEY-----\n"
};
Command.UpdatePower {
fingerprint = "qZk+NkcGgWq6PiVxeFDCbJzQ2J0=";
power = Option.value_exn (Power.of_int 3)
};
]
})
let block_body_tests = [
"format", `Quick, test_format;
]
let () =
Test.run "BlockBody" [
"BlockBody", block_body_tests;
]
......@@ -4,6 +4,7 @@ module Test = Alcotest
let test_format () =
Test.(check string) "add" "\
7\n\
add\n\
rsa\n\
2\n\
......@@ -19,12 +20,14 @@ let test_format () =
-----END PUBLIC KEY-----\n" })));
Test.(check string) "remove" "\
2\n\
remove\n\
+gf0YWKHMzO0/mCFlB9AivKRlD0="
(Command.(format (RemovePublicKey {
fingerprint = "+gf0YWKHMzO0/mCFlB9AivKRlD0=" })));
Test.(check string) "update" "\
3\n\
update\n\
+gf0YWKHMzO0/mCFlB9AivKRlD0=\n\
3"
......@@ -33,10 +36,12 @@ let test_format () =
power = Option.value_exn (Power.of_int 3) })));
Test.(check string) "halt" "\
1\n\
halt"
(Command.(format (Halt)));
Test.(check string) "user" "\
4\n\
user\n\
some-protocol\n\
some-command\n\
......
......@@ -6,5 +6,6 @@
publicKey_test
publicKeyAlgorithm_test
expirationDateTime_test
command_test)
command_test
blockBody_test)
(libraries alcotest yayakaChain))
......@@ -7,7 +7,7 @@ let pp ppf = function
let hash = Test.testable pp ( = )
let h hex = Hex.to_string (`Hex hex)
let h hex = Hex.to_string (`Hex hex) |> Base64.encode_exn
let test_hash () =
Test.(check hash) "a with sha1" {
......@@ -28,13 +28,14 @@ let test_hash () =
(Hash.hash Sha256 "abc")
let test_format () =
Test.(check (pair string string)) "sha1"
("sha1", "qZk+NkcGgWq6PiVxeFDCbJzQ2J0=")
Test.(check string) "sha1"
"sha1\nqZk+NkcGgWq6PiVxeFDCbJzQ2J0="
(Hash.format {
algorithm = Sha1;
hash = (h "a9993e364706816aba3e25717850c26c9cd0d89d") });
Test.(check (pair string string)) "sha256"
("sha256", "ungWv48Bz+pBQUDeXa4iI7ADYaOWF3qctBD/YfIAFa0=")
Test.(check string) "sha256"
"sha256\nungWv48Bz+pBQUDeXa4iI7ADYaOWF3qctBD/YfIAFa0="
(Hash.format {
algorithm = Sha256;
hash = (h "ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad") })
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment