76 lines
2.6 KiB
Perl
76 lines
2.6 KiB
Perl
|
|
#!/usr/bin/env perl
|
||
|
|
|
||
|
|
use strict;
|
||
|
|
use warnings;
|
||
|
|
use Digest::SHA qw(sha256);
|
||
|
|
use FindBin qw($Bin);
|
||
|
|
use File::Path qw(make_path);
|
||
|
|
use JSON::PP;
|
||
|
|
use lib "$Bin";
|
||
|
|
use LXMF::MessagePack qw(pack_array pack_bin pack_float64 pack_map);
|
||
|
|
|
||
|
|
my $output_dir = "$Bin/../examples";
|
||
|
|
make_path($output_dir);
|
||
|
|
|
||
|
|
my $destination_hash = pack("C*", 0x00 .. 0x0f);
|
||
|
|
my $source_hash = pack("C*", 0x10 .. 0x1f);
|
||
|
|
my $signature = pack("C*", 0x20 .. 0x5f);
|
||
|
|
my $stamp = pack("C*", 0xa0 .. 0xbf);
|
||
|
|
|
||
|
|
my $timestamp = 1_700_000_000.25;
|
||
|
|
my $title = "Test title";
|
||
|
|
my $content = "Deterministic LXMF body";
|
||
|
|
my $fields = pack_map();
|
||
|
|
|
||
|
|
my @required_items = (
|
||
|
|
pack_float64($timestamp),
|
||
|
|
pack_bin($title),
|
||
|
|
pack_bin($content),
|
||
|
|
$fields,
|
||
|
|
);
|
||
|
|
|
||
|
|
my $payload = pack_array(@required_items);
|
||
|
|
my $stamped_payload = pack_array(@required_items, pack_bin($stamp));
|
||
|
|
my $message_id = sha256($destination_hash . $source_hash . $payload);
|
||
|
|
my $signed_part = $destination_hash . $source_hash . $payload . $message_id;
|
||
|
|
|
||
|
|
my $minimal = $destination_hash . $source_hash . $signature . $payload;
|
||
|
|
my $stamped = $destination_hash . $source_hash . $signature . $stamped_payload;
|
||
|
|
|
||
|
|
write_hex("$output_dir/lxmf_message_minimal.hex", $minimal);
|
||
|
|
write_hex("$output_dir/lxmf_message_stamped.hex", $stamped);
|
||
|
|
|
||
|
|
my $manifest = {
|
||
|
|
description => "Deterministic LXMessage packing vectors; signature bytes are placeholders",
|
||
|
|
format => "destination_hash || source_hash || signature || msgpack_payload",
|
||
|
|
payload_order => [qw(timestamp title content fields optional_stamp)],
|
||
|
|
destination_hash_hex => unpack("H*", $destination_hash),
|
||
|
|
source_hash_hex => unpack("H*", $source_hash),
|
||
|
|
signature_hex => unpack("H*", $signature),
|
||
|
|
signature_valid => JSON::PP::false,
|
||
|
|
timestamp => $timestamp,
|
||
|
|
title_hex => unpack("H*", $title),
|
||
|
|
content_hex => unpack("H*", $content),
|
||
|
|
fields => {},
|
||
|
|
stamp_hex => unpack("H*", $stamp),
|
||
|
|
payload_without_stamp_hex => unpack("H*", $payload),
|
||
|
|
payload_with_stamp_hex => unpack("H*", $stamped_payload),
|
||
|
|
message_id_hex => unpack("H*", $message_id),
|
||
|
|
signed_part_hex => unpack("H*", $signed_part),
|
||
|
|
minimal_lxmf_hex => unpack("H*", $minimal),
|
||
|
|
stamped_lxmf_hex => unpack("H*", $stamped),
|
||
|
|
};
|
||
|
|
|
||
|
|
open my $json_fh, ">:raw", "$output_dir/lxmf_message_vectors.json"
|
||
|
|
or die "cannot write vector manifest: $!\n";
|
||
|
|
print {$json_fh} JSON::PP->new->canonical->pretty->encode($manifest);
|
||
|
|
close $json_fh;
|
||
|
|
|
||
|
|
print "Generated deterministic LXMessage vectors in $output_dir\n";
|
||
|
|
|
||
|
|
sub write_hex {
|
||
|
|
my ($path, $bytes) = @_;
|
||
|
|
open my $fh, ">:raw", $path or die "cannot write $path: $!\n";
|
||
|
|
print {$fh} unpack("H*", $bytes), "\n";
|
||
|
|
close $fh;
|
||
|
|
}
|