Skip to content

Commit 43b6c2c

Browse files
committed
Working on input formats.
1 parent b7f60ae commit 43b6c2c

19 files changed

+414
-102
lines changed

.vscode/launch.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
"type": "lldb",
88
"request": "launch",
99
"program": "${workspaceFolder}/src/seedtool",
10-
"args": [],
10+
"args": ["--in", "slip39", "foobar"],
1111
"cwd": "${workspaceFolder}/src",
1212
"preLaunchTask": "Build"
1313
}

src/Makefile.in

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -56,21 +56,21 @@ $(toolname): $(OBJS)
5656
seedtool.o: params.hpp format.hpp
5757
utils.o: utils.hpp
5858
params.o: params.hpp utils.hpp random.hpp formats-all.hpp
59-
random.o: random.hpp randombytes.h hkdf.h
59+
random.o: random.hpp randombytes.h hkdf.h utils.hpp
6060
randombytes.o: randombytes.h
6161
hkdf.o: hkdf.h
6262
format.o: format.hpp utils.hpp
6363
formats-all.hpp: format-base6.hpp format-base10.hpp format-bip39.hpp format-bits.hpp format-cards.hpp format-dice.hpp format-hex.hpp format-ints.hpp format-random.hpp format-slip39.hpp
64-
format-base6.o: format-base6.hpp format.hpp params.hpp
65-
format-base10.o: format-base10.hpp format.hpp params.hpp
66-
format-bip39.o: format-bip39.hpp format.hpp params.hpp
67-
format-bits.o: format-bits.hpp format.hpp params.hpp
68-
format-cards.o: format-cards.hpp format.hpp params.hpp
69-
format-dice.o: format-dice.hpp format.hpp params.hpp
70-
format-hex.o: format-hex.hpp format.hpp params.hpp
71-
format-ints.o: format-ints.hpp format.hpp params.hpp
72-
format-random.o: format-random.hpp format.hpp params.hpp
73-
format-slip39.o: format-slip39.hpp format.hpp params.hpp
64+
format-base6.o: format-base6.hpp format.hpp params.hpp random.hpp
65+
format-base10.o: format-base10.hpp format.hpp params.hpp random.hpp
66+
format-bip39.o: format-bip39.hpp format.hpp params.hpp random.hpp
67+
format-bits.o: format-bits.hpp format.hpp params.hpp random.hpp
68+
format-cards.o: format-cards.hpp format.hpp params.hpp random.hpp
69+
format-dice.o: format-dice.hpp format.hpp params.hpp random.hpp
70+
format-hex.o: format-hex.hpp format.hpp params.hpp random.hpp
71+
format-ints.o: format-ints.hpp format.hpp params.hpp random.hpp
72+
format-random.o: format-random.hpp format.hpp params.hpp random.hpp
73+
format-slip39.o: format-slip39.hpp format.hpp params.hpp random.hpp
7474

7575
bindir = $(DESTDIR)/$(prefix)/bin
7676

src/format-base10.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,17 @@
66
//
77

88
#include "format-base10.hpp"
9+
10+
#include <stdexcept>
11+
912
#include "params.hpp"
1013
#include "utils.hpp"
14+
#include "random.hpp"
1115

1216
void FormatBase10::process_input(Params* p) {
17+
auto input = p->get_one_argument();
18+
auto entropy = digits_to_data(input, 0, 9);
19+
p->seed = deterministic_random(entropy, p->count);
1320
}
1421

1522
void FormatBase10::process_output(Params* p) {

src/format-base6.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,17 @@
66
//
77

88
#include "format-base6.hpp"
9+
10+
#include <stdexcept>
11+
912
#include "params.hpp"
1013
#include "utils.hpp"
14+
#include "random.hpp"
1115

1216
void FormatBase6::process_input(Params* p) {
17+
auto input = p->get_one_argument();
18+
auto entropy = digits_to_data(input, 0, 5);
19+
p->seed = deterministic_random(entropy, p->count);
1320
}
1421

1522
void FormatBase6::process_output(Params* p) {

src/format-bip39.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,17 +9,29 @@
99

1010
#include <strings.h>
1111
#include <bc-bip39/bc-bip39.h>
12+
#include <stdexcept>
1213

1314
#include "params.hpp"
1415
#include "utils.hpp"
1516

17+
using namespace std;
18+
1619
bool FormatBIP39::is_seed_length_valid(size_t seed_len) {
1720
if(!(12 <= seed_len && seed_len <= 32)) { return false; }
1821
if(seed_len % 2 != 0) { return false; }
1922
return true;
2023
}
2124

2225
void FormatBIP39::process_input(Params* p) {
26+
auto input = p->get_combined_arguments();
27+
vector<uint8_t> buf;
28+
buf.resize(300);
29+
auto len = bip39_secret_from_mnemonics(input.c_str(), &buf[0], buf.size());
30+
if(len == 0) {
31+
throw runtime_error("Invalid BIP39 word sequence.");
32+
}
33+
buf.resize(len);
34+
p->seed = buf;
2335
}
2436

2537
void FormatBIP39::process_output(Params* p) {

src/format-bits.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,17 @@
66
//
77

88
#include "format-bits.hpp"
9+
10+
#include <stdexcept>
11+
912
#include "params.hpp"
1013
#include "utils.hpp"
14+
#include "random.hpp"
1115

1216
void FormatBits::process_input(Params* p) {
17+
auto input = p->get_one_argument();
18+
auto entropy = digits_to_data(input, 0, 1);
19+
p->seed = deterministic_random(entropy, p->count);
1320
}
1421

1522
void FormatBits::process_output(Params* p) {

src/format-cards.cpp

Lines changed: 65 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,23 +6,79 @@
66
//
77

88
#include "format-cards.hpp"
9+
10+
#include <stdexcept>
11+
#include <cctype>
12+
913
#include "params.hpp"
1014
#include "utils.hpp"
15+
#include "random.hpp"
16+
17+
using namespace std;
18+
19+
// https://en.wikipedia.org/wiki/High_card_by_suit
20+
static const vector<char> card_suits = { 'd', 'c', 'h', 's' };
21+
static const vector<char> card_ranks = { 'a', '2', '3', '4', '5', '6', '7', '8', '9', 't', 'j', 'q', 'k'};
22+
23+
static size_t parse_rank(char c) {
24+
c = tolower(c);
25+
for(size_t i = 0; i < card_ranks.size(); i++) {
26+
if(c == card_ranks[i]) {
27+
return i;
28+
}
29+
}
30+
throw runtime_error("Invalid card rank. Allowed: [A,2-9,T,J,Q,K]");
31+
}
32+
33+
static size_t parse_suit(char c) {
34+
c = tolower(c);
35+
for(size_t i = 0; i < card_suits.size(); i++) {
36+
if(c == card_suits[i]) {
37+
return i;
38+
}
39+
}
40+
throw runtime_error("Invalid card rank. Allowed: [D,C,H,S]");
41+
}
1142

12-
static const std::string card_suits[] = { "c", "d", "h", "s" };
13-
static const std::string card_ranks[] = { "a", "2", "3", "4", "5", "6", "7", "8", "9", "t", "j", "q", "k"};
43+
vector<uint8_t> cards_to_data(const string& cards) {
44+
vector<uint8_t> result;
1445

15-
static std::string to_card(size_t n) {
46+
auto len = cards.length();
47+
if(len % 2 != 0) {
48+
throw runtime_error("Cards string must have even number of characters.");
49+
}
50+
auto count = len / 2;
51+
result.reserve(count);
52+
for(auto i = 0; i < count; i++) {
53+
auto rank = parse_rank(cards[i * 2]);
54+
auto suit = parse_suit(cards[i * 2 + 1]);
55+
auto n = suit * 13 + rank;
56+
result.push_back(n);
57+
}
58+
59+
return result;
60+
}
61+
62+
void FormatCards::process_input(Params* p) {
63+
auto input = p->get_one_argument();
64+
auto entropy = cards_to_data(input);
65+
p->seed = deterministic_random(entropy, p->count);
66+
}
67+
68+
static string to_card(size_t n) {
1669
if(n > 51) { return NULL; }
17-
std::string buf;
70+
string buf;
1871
size_t rank = n % 13;
1972
size_t suit = n / 13;
20-
buf.append(card_ranks[rank]);
21-
buf.append(card_suits[suit]);
22-
return buf;
23-
}
73+
buf += card_ranks[rank];
74+
buf += card_suits[suit];
2475

25-
void FormatCards::process_input(Params* p) {
76+
// test value round trip
77+
auto v = cards_to_data(buf);
78+
assert(v.size() == 1);
79+
assert(v[0] == n);
80+
81+
return buf;
2682
}
2783

2884
void FormatCards::process_output(Params* p) {

src/format-dice.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,12 @@
88
#include "format-dice.hpp"
99
#include "params.hpp"
1010
#include "utils.hpp"
11+
#include "random.hpp"
1112

1213
void FormatDice::process_input(Params* p) {
14+
auto input = p->get_one_argument();
15+
auto entropy = digits_to_data(input, 1, 6);
16+
p->seed = deterministic_random(entropy, p->count);
1317
}
1418

1519
void FormatDice::process_output(Params* p) {

src/format-hex.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,15 @@
66
//
77

88
#include "format-hex.hpp"
9+
10+
#include <iostream>
11+
912
#include "params.hpp"
1013
#include "utils.hpp"
1114

1215
void FormatHex::process_input(Params* p) {
16+
auto input = p->get_one_argument();
17+
p->seed = hex_to_data(input);
1318
}
1419

1520
void FormatHex::process_output(Params* p) {

src/format-ints.cpp

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,48 @@
66
//
77

88
#include "format-ints.hpp"
9+
10+
#include <iostream>
11+
#include <sstream>
12+
#include <string>
13+
#include <stdexcept>
14+
915
#include "params.hpp"
1016
#include "utils.hpp"
17+
#include "random.hpp"
18+
19+
using namespace std;
1120

1221
FormatInts::FormatInts() : Format(Format::Key::ints, "ints") {
1322
low = 0;
1423
high = 9;
1524
}
1625

26+
static vector<uint8_t> parse_ints(const string& input) {
27+
vector<uint8_t> result;
28+
29+
istringstream iss(input);
30+
31+
while(!iss.eof()) {
32+
string s;
33+
iss >> s;
34+
int i;
35+
if(!(stringstream(s) >> i)) {
36+
throw runtime_error("Invalid integer. Allowed: [0-255]");
37+
}
38+
if(!(0 <= i && i <= 255)) {
39+
throw runtime_error("Integer out of range. Allowed: [0-255]");
40+
}
41+
result.push_back(i);
42+
}
43+
44+
return result;
45+
}
46+
1747
void FormatInts::process_input(Params* p) {
48+
auto input = p->get_combined_arguments();
49+
auto entropy = parse_ints(input);
50+
p->seed = deterministic_random(entropy, p->count);
1851
}
1952

2053
void FormatInts::process_output(Params* p) {

0 commit comments

Comments
 (0)