| 1 | # ex:ts=8 sw=4: | ||||||||||||||||||||||||||||
| 2 | # $OpenBSD$ | ||||||||||||||||||||||||||||
| 3 | # | ||||||||||||||||||||||||||||
| 4 | # Copyright (c) 2026 Dick Olsson <hi@senzilla.io> | ||||||||||||||||||||||||||||
| 5 | # | ||||||||||||||||||||||||||||
| 6 | # Permission to use, copy, modify, and distribute this software for any | ||||||||||||||||||||||||||||
| 7 | # purpose with or without fee is hereby granted, provided that the above | ||||||||||||||||||||||||||||
| 8 | # copyright notice and this permission notice appear in all copies. | ||||||||||||||||||||||||||||
| 9 | # | ||||||||||||||||||||||||||||
| 10 | # THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES | ||||||||||||||||||||||||||||
| 11 | # WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF | ||||||||||||||||||||||||||||
| 12 | # MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR | ||||||||||||||||||||||||||||
| 13 | # ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES | ||||||||||||||||||||||||||||
| 14 | # WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN | ||||||||||||||||||||||||||||
| 15 | # ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF | ||||||||||||||||||||||||||||
| 16 | # OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. | ||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||
| 18 | package App::FuguSeed::QR; | ||||||||||||||||||||||||||||
| 19 | 2 | our $VERSION = '0.2.0'; | |||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||
| 21 | 2 | ▶ | use v5.34; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 22 | 2 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 23 | 2 | ▶ | use experimental 'signatures'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 24 | 2 | ▶ | no feature qw(indirect multidimensional bareword_filehandles); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||
| 26 | 2 | ▶ | use App::FuguSeed::Codewords (); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 27 | 2 | ▶ | use App::FuguSeed::Matrix (); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 28 | 2 | ▶ | use App::FuguSeed::Mnemonic (); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 29 | 2 | ▶ | use App::FuguSeed::Text (); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||
| 31 | # App::FuguSeed::QR - the flow of fuguseed-qr. | ||||||||||||||||||||||||||||
| 32 | # | ||||||||||||||||||||||||||||
| 33 | # The module reads the 12 words from standard input, and it writes | ||||||||||||||||||||||||||||
| 34 | # the SeedQR to standard output (QR-PROGRAM-3, QR-PROGRAM-4). It is | ||||||||||||||||||||||||||||
| 35 | # the one module of the program that touches a stream, and the three | ||||||||||||||||||||||||||||
| 36 | # standard streams are the one contact of the program with the | ||||||||||||||||||||||||||||
| 37 | # computer (SEC-TRUST-3). It also holds the two parts of the words | ||||||||||||||||||||||||||||
| 38 | # that fuguseed-last does not call: the checksum check and the digit | ||||||||||||||||||||||||||||
| 39 | # string (QR-PROGRAM-6). | ||||||||||||||||||||||||||||
| 40 | # | ||||||||||||||||||||||||||||
| 41 | # A failure line can name a word position or a count, never a word | ||||||||||||||||||||||||||||
| 42 | # (SEC-CHANNELS-2). | ||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||
| 44 | # NAME and USAGE: | ||||||||||||||||||||||||||||
| 45 | # The name of the program in a failure line, and the one usage | ||||||||||||||||||||||||||||
| 46 | # line of QR-PROGRAM-2. The program takes no option and no | ||||||||||||||||||||||||||||
| 47 | # argument (D-12). | ||||||||||||||||||||||||||||
| 48 | 2 | ▶ | use constant NAME => 'fuguseed-qr'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 49 | 2 | ▶ | use constant USAGE => "usage: fuguseed-qr\n"; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||
| 51 | # SUCCESS, FAILURE and USAGE_ERROR: | ||||||||||||||||||||||||||||
| 52 | # The three exit codes of the program (QR-PROGRAM-2, | ||||||||||||||||||||||||||||
| 53 | # QR-PROGRAM-4). | ||||||||||||||||||||||||||||
| 54 | 2 | ▶ | use constant SUCCESS => 0; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 55 | 2 | ▶ | use constant FAILURE => 1; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 56 | 2 | ▶ | use constant USAGE_ERROR => 2; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||
| 58 | # COUNT: | ||||||||||||||||||||||||||||
| 59 | # The word count of one seed (D-04). | ||||||||||||||||||||||||||||
| 60 | 2 | ▶ | use constant COUNT => 12; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||
| 62 | # DIGITS: | ||||||||||||||||||||||||||||
| 63 | # The decimal digits of one index in the digit string | ||||||||||||||||||||||||||||
| 64 | # (QR-MNEMONIC-3). | ||||||||||||||||||||||||||||
| 65 | 2 | ▶ | use constant DIGITS => 4; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||
| 67 | # CHECKSUM_FAULT: | ||||||||||||||||||||||||||||
| 68 | # The failure line of a wrong checksum (QR-MNEMONIC-4). A typed | ||||||||||||||||||||||||||||
| 69 | # word of any position can change the checksum, so the line | ||||||||||||||||||||||||||||
| 70 | # names no position. | ||||||||||||||||||||||||||||
| 71 | 2 | ▶ | use constant CHECKSUM_FAULT => 'the checksum of the 12 words fails'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||
| 73 | # $class->valid($words): | ||||||||||||||||||||||||||||
| 74 | # True when the low 4 bits of index 12 are the checksum that | ||||||||||||||||||||||||||||
| 75 | # BIP39 requires (QR-MNEMONIC-2). The caller proves the words | ||||||||||||||||||||||||||||
| 76 | # with the fault method of App::FuguSeed::Mnemonic first. | ||||||||||||||||||||||||||||
| 77 | 193 | ▶ | sub valid ( $, $words ) | ||||||||||||||||||||||||||
|
Subroutine
valid: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 78 | { | ||||||||||||||||||||||||||||
| 79 | 193 | my @index = App::FuguSeed::Mnemonic->indexes($words); | |||||||||||||||||||||||||||
| 80 | 193 | my $typed = $index[-1] % 2**App::FuguSeed::Mnemonic->CHECKSUM_BITS; | |||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||
| 82 | 193 | return $typed == App::FuguSeed::Mnemonic->checksum(@index); | |||||||||||||||||||||||||||
| 83 | } | ||||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||
| 85 | # $class->digits($words): | ||||||||||||||||||||||||||||
| 86 | # The digit string of QR-MNEMONIC-3: the 12 indexes, 0-based, | ||||||||||||||||||||||||||||
| 87 | # each as 4 decimal digits with leading zeros, in word order. | ||||||||||||||||||||||||||||
| 88 | # The caller proves the words first. | ||||||||||||||||||||||||||||
| 89 | 6 | ▶ | sub digits ( $, $words ) | ||||||||||||||||||||||||||
|
Subroutine
digits: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 90 | { | ||||||||||||||||||||||||||||
| 91 | return join q{}, | ||||||||||||||||||||||||||||
| 92 | 6 | map { sprintf '%0' . DIGITS . 'd', $_ } | |||||||||||||||||||||||||||
| 93 | App::FuguSeed::Mnemonic->indexes($words); | ||||||||||||||||||||||||||||
| 94 | } | ||||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||
| 96 | # $class->run(@argument): | ||||||||||||||||||||||||||||
| 97 | # Read the words, print the result, and return the exit code. | ||||||||||||||||||||||||||||
| 98 | 31 | ▶ | sub run ( $class, @argument ) | ||||||||||||||||||||||||||
|
Subroutine
run: called CC=8 SCAR=21.1
Pod
documented
| |||||||||||||||||||||||||||||
| 99 | { | ||||||||||||||||||||||||||||
| 100 | 31 | ▶ | if (@argument) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 101 | 0 | print {*STDERR} USAGE; | |||||||||||||||||||||||||||
| 102 | 0 | return USAGE_ERROR; | |||||||||||||||||||||||||||
| 103 | } | ||||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||
| 105 | # The pause of QR-TEXT-5 waits for a person, so each view | ||||||||||||||||||||||||||||
| 106 | # must leave the buffer before the read. | ||||||||||||||||||||||||||||
| 107 | 31 | local $| = 1; | |||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||
| 109 | 31 | my $line = readline STDIN; | |||||||||||||||||||||||||||
| 110 | 31 | ▶ | my @words = split q{ }, $line // q{}; | ||||||||||||||||||||||||||
|
Condition50%1/2
$line // ""
MC/DC0%0/1
$line // ""
$line
Truth table50%1/2
A
A= $line
| |||||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||
| 112 | # A wrong checksum is a failure, and the program prints no | ||||||||||||||||||||||||||||
| 113 | # SeedQR (QR-MNEMONIC-4). | ||||||||||||||||||||||||||||
| 114 | 31 | my $fault = App::FuguSeed::Mnemonic->fault( \@words, COUNT ); | |||||||||||||||||||||||||||
| 115 | 31 | ▶ | $fault = CHECKSUM_FAULT | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
defined $fault or $class->valid(\@words)
MC/DC50%1/2
defined $fault or $class->valid(\@words)
defined $fault
$class->valid(\@words)
Truth table66%2/3
A or B
A= defined $faultB= $class->valid(\@words)
| |||||||||||||||||||||||||||||
| 116 | if !defined $fault && !$class->valid( \@words ); | ||||||||||||||||||||||||||||
| 117 | 31 | ▶ | if ( defined $fault ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 118 | 30 | print {*STDERR} NAME . ": $fault\n"; | |||||||||||||||||||||||||||
| 119 | 30 | return FAILURE; | |||||||||||||||||||||||||||
| 120 | } | ||||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||
| 122 | # A class name after print is a bareword filehandle on perl | ||||||||||||||||||||||||||||
| 123 | # v5.34, so each result reaches a variable first. | ||||||||||||||||||||||||||||
| 124 | 1 | my $digits = $class->digits( \@words ); | |||||||||||||||||||||||||||
| 125 | 1 | my @codewords = App::FuguSeed::Codewords->encode($digits); | |||||||||||||||||||||||||||
| 126 | 1 | my $matrix = App::FuguSeed::Matrix->build( \@codewords ); | |||||||||||||||||||||||||||
| 127 | 1 | my $grid = App::FuguSeed::Text->grid($matrix); | |||||||||||||||||||||||||||
| 128 | |||||||||||||||||||||||||||||
| 129 | 1 | print "$digits\n"; | |||||||||||||||||||||||||||
| 130 | 1 | print $grid; | |||||||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||||||
| 132 | 1 | my @views = App::FuguSeed::Text->zones($matrix); | |||||||||||||||||||||||||||
| 133 | 1 | ▶ | while ( defined( my $view = shift @views ) ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 134 | 25 | print $view; | |||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||
| 136 | # One line of standard input between two zones. At | ||||||||||||||||||||||||||||
| 137 | # the end of the input, the remaining zones print | ||||||||||||||||||||||||||||
| 138 | # without a pause (QR-TEXT-5). | ||||||||||||||||||||||||||||
| 139 | 25 | ▶ | readline STDIN if @views; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 140 | } | ||||||||||||||||||||||||||||
| 141 | |||||||||||||||||||||||||||||
| 142 | 1 | return SUCCESS; | |||||||||||||||||||||||||||
| 143 | } | ||||||||||||||||||||||||||||
| 144 | |||||||||||||||||||||||||||||
| 145 | 2 | 1; | |||||||||||||||||||||||||||