| 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::ListFile; | ||||||||||||||
| 19 | 14 | our $VERSION = '0.2.0'; | |||||||||||||
| 20 | |||||||||||||||
| 21 | 14 | ▶ | use v5.34; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 22 | 14 | ▶ | use warnings; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 23 | 14 | ▶ | use experimental 'signatures'; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 24 | 14 | ▶ | no feature qw(indirect multidimensional bareword_filehandles); | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 25 | |||||||||||||||
| 26 | 14 | ▶ | use App::FuguSeed::List (); | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 27 | 14 | ▶ | use Digest::SHA (); | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 28 | 14 | ▶ | use Fugu::File (); | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 29 | 14 | ▶ | use Fugu::Log (); | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 30 | |||||||||||||||
| 31 | # App::FuguSeed::ListFile - read a word list file and prove its | ||||||||||||||
| 32 | # digest (LIST-SHARE-3). | ||||||||||||||
| 33 | # | ||||||||||||||
| 34 | # Both verbs of fuguseed-words take a list file. This module is the | ||||||||||||||
| 35 | # one reader of that file. It computes the SHA-256 of the bytes and | ||||||||||||||
| 36 | # it refuses every digest but the one of the source list, which | ||||||||||||||
| 37 | # App::FuguSeed::List pins. | ||||||||||||||
| 38 | # | ||||||||||||||
| 39 | # The digest proves the whole file, so the module needs no other | ||||||||||||||
| 40 | # check: the source list holds 2048 lines, and each line holds one | ||||||||||||||
| 41 | # word and one line feed (LIST-SOURCE-2). | ||||||||||||||
| 42 | # | ||||||||||||||
| 43 | # A recoverable failure returns undef and reports through the log, | ||||||||||||||
| 44 | # because a class-method module has no error accessor to hold the | ||||||||||||||
| 45 | # reason. | ||||||||||||||
| 46 | |||||||||||||||
| 47 | # $class->read($path): | ||||||||||||||
| 48 | # Read the word list file $path. The result is a hash reference | ||||||||||||||
| 49 | # with the 2048 words under "words" and the SHA-256 of the file | ||||||||||||||
| 50 | # under "digest". | ||||||||||||||
| 51 | # | ||||||||||||||
| 52 | # The method returns undef for a file that it cannot read, and | ||||||||||||||
| 53 | # for a file with another digest than the source list. | ||||||||||||||
| 54 | 7 | ▶ | sub read ( $, $path ) | ||||||||||||
|
Subroutine
read: called CC=3 SCAR=14.2
Pod
documented
| |||||||||||||||
| 55 | 0 | ▶ | { | ||||||||||||
|
Subroutine
read: not called CC=3 SCAR=14.2
Pod
documented
| |||||||||||||||
| 56 | 0 | my $log = Fugu::Log->default; | |||||||||||||
| 57 | 0 | my $text = Fugu::File->read($path); | |||||||||||||
| 58 | 0 | ▶ | unless ( defined $text ) { | ||||||||||||
|
Branch: 2 branches
| |||||||||||||||
| 59 | 0 | $log->error( 'cannot read the word list %s', $path ); | |||||||||||||
| 60 | 0 | return; | |||||||||||||
| 61 | } | ||||||||||||||
| 62 | |||||||||||||||
| 63 | 0 | my $digest = Digest::SHA::sha256_hex($text); | |||||||||||||
| 64 | 0 | ▶ | unless ( $digest eq App::FuguSeed::List::DIGEST() ) { | ||||||||||||
|
Branch: 2 branches
| |||||||||||||||
| 65 | 0 | $log->error( | |||||||||||||
| 66 | '%s is not the English word list of BIP39: ' | ||||||||||||||
| 67 | . 'its SHA-256 is %s', | ||||||||||||||
| 68 | $path, $digest | ||||||||||||||
| 69 | ); | ||||||||||||||
| 70 | 0 | return; | |||||||||||||
| 71 | } | ||||||||||||||
| 72 | |||||||||||||||
| 73 | # The digest pins the bytes, so the split gives the 2048 words | ||||||||||||||
| 74 | # of the source list. | ||||||||||||||
| 75 | 0 | my @words = split /\n/, $text; | |||||||||||||
| 76 | |||||||||||||||
| 77 | 0 | return { words => \@words, digest => $digest }; | |||||||||||||
| 78 | } | ||||||||||||||
| 79 | |||||||||||||||
| 80 | 14 | 1; | |||||||||||||