Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow any combination of input format and output format. #69

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 0 additions & 39 deletions example-scripts/bip39-to-sskr.sh

This file was deleted.

16 changes: 0 additions & 16 deletions example-scripts/sskr-to-bip39.sh

This file was deleted.

20 changes: 4 additions & 16 deletions src/format-sskr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -116,34 +116,22 @@ void FormatSSKR::process_output(Params* p) {
assert(gen_share_count == share_count);
assert(bytes_in_each_share == share_len);

if(p->is_ur_out) {
StringVector strings;
for (int i = 0; i < share_count; i++) {
uint8_t* bytes = shares_buffer + (i * bytes_in_each_share);
auto v = ByteVector(bytes, bytes + bytes_in_each_share);
ByteVector cbor;
ur::CborLite::encodeTagAndValue(cbor, ur::CborLite::Major::semantic, size_t(309));
ur::CborLite::encodeBytes(cbor, v);
if (p->is_ur_out) {
ur::UR ur("crypto-sskr", cbor);
auto s = ur::UREncoder::encode(ur);
strings.push_back(s);
}
auto all_strings = join(strings, "\n");
p->output = all_strings;
strings.push_back(ur::UREncoder::encode(ur));
} else {
StringVector strings;
for (int i = 0; i < share_count; i++) {
uint8_t* bytes = shares_buffer + (i * bytes_in_each_share);
auto v = ByteVector(bytes, bytes + bytes_in_each_share);
ByteVector cbor;
ur::CborLite::encodeTagAndValue(cbor, ur::CborLite::Major::semantic, size_t(309));
ur::CborLite::encodeBytes(cbor, v);
auto s = ur::Bytewords::encode(ur::Bytewords::standard, cbor);
strings.push_back(s);
strings.push_back(ur::Bytewords::encode(ur::Bytewords::standard, cbor));
}
}
auto all_strings = join(strings, "\n");
p->output = all_strings;
}
}

FormatSSKR::FormatSSKR()
Expand Down
41 changes: 0 additions & 41 deletions src/params.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -100,46 +100,6 @@ void Params::validate_output_format() {
}
}

void Params::validate_output_for_input() {
// Any input format works with hex output format.
if(is_hex(output_format)) {
return;
}

// Any input format works with any Bytewords output format.
if(is_bytewords_any(output_format)) {
return;
}

// Random input works with any output format.
if(is_random(input_format)) {
return;
}

// Hex input works with any output format.
if(is_hex(input_format)) {
return;
}

// Any Bytewords input works with any output format.
if(is_bytewords_any(input_format)) {
return;
}

// BIP39 UR input works with BIP39 output format.
if(is_ur_in && is_bip39(input_format) && is_bip39(output_format)) {
return;
}

// SSKR UR input works with SSKR output format.
if(is_ur_in && is_sskr(input_format) && is_sskr(output_format)) {
return;
}

argp_error(state, "Input format %s cannot be used with output format %s",
input_format->name.c_str(), output_format->name.c_str());
}

void Params::validate_ints_specific() {
auto f = dynamic_cast<FormatInts*>(output_format);
if(f != NULL) {
Expand Down Expand Up @@ -378,7 +338,6 @@ void Params::validate() {
validate_input();
validate_count_for_input_format();
validate_output_format();
validate_output_for_input();
validate_ints_specific();
validate_bip39_specific();
validate_sskr_specific();
Expand Down
1 change: 0 additions & 1 deletion src/params.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,6 @@ class Params {
void validate_input_format();
void validate_count_for_input_format();
void validate_output_format();
void validate_output_for_input();
void validate_ints_specific();
void validate_bip39_specific();
void validate_sskr_specific();
Expand Down
40 changes: 40 additions & 0 deletions src/test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,46 @@ testInSSKRUR()
"$(${SEEDTOOL} --in ur ur:crypto-sskr/taadecgomymwaeadaemdfnpldrprcmtpcnfwztcstaotfsbsadatkspyfg ur:crypto-sskr/taadecgomymwaeadaoglvdolfhemvogaiyksjpvabncphhmsmkjznbdefy)"
}

testBip39ToSskr()
{
assertEquals $'tuna acid epic hard data edge next acid acid able rust claw zaps curl zero exit wolf solo beta whiz jowl keys beta help nail flap data peck beta task flap blue grim holy warm curl ruin main cook twin exit dark when brag zero blue
tuna acid epic hard data edge next acid acid acid aqua solo legs away back poem inky cash view ruin nail stub puma duty hang inch edge rock lion taco iron puma legs knob many keno note work huts next meow kept pose view buzz item
tuna acid epic hard data edge next acid acid also guru onyx away junk also axis saga jolt scar item monk arch jade lung cook axis aunt logo arch swan body gala warm cook cyan many zest kiln vast gush king leaf horn bulb vibe meow
tuna acid epic hard data edge next acid brag able toil edge wall gala obey task body zinc cyan solo meow view roof free wall what tomb paid brew note judo owls chef kept stub deli down wall owls wave fair kept kept duty vows city
tuna acid epic hard data edge next acid brag acid runs lion note blue glow flux legs slot purr claw task quad roof list grim cash ruin what iris fizz cash mild slot cost bias wolf runs quiz ruin wave iced navy gear monk huts blue
tuna acid epic hard data edge next acid brag also bald down vibe foxy iced part wasp heat kiwi mint cyan kite kick redo wall fair judo zest chef edge fuel game legs poem apex keys lava oboe cats hard time ramp gift drum zoom hang
tuna acid epic hard data edge next acid brag apex huts nail memo cola luck data knob horn wasp foxy keys door kick kiln grim undo cats onyx iced warm crux keys hill silk slot plus bulb zoom acid hard love help jugs lion keno hill
tuna acid epic hard data edge next acid brag aqua heat maze heat nail draw luck toil solo cats twin view cats bulb exam frog arch flew ruby twin wand toys jury saga what exam aqua duty mint rust knob legs waxy beta quiz knob taco' \
"$(${SEEDTOOL} --in bip39 --out sskr --group-threshold 1 --group 2-of-3 --group 3-of-5 'slender horse coil sketch public wink nest point royal believe inform lyrics critic harbor scheme enrich burden glance song chicken grief first panic actor')"
}

testSskrToBip39()
{
assertEquals $'clump flavor moment wood resource mom usual solid fortune material document under' \
"$(${SEEDTOOL} --in sskr --out bip39 \
'tuna acid epic gyro edge next able acid able flux down eyes pool task cyan gyro kept exit paid wave high news bias zone zaps calm note poem aqua' \
'tuna acid epic gyro edge next able acid also wolf vows bald sets claw bald dull cost slot inky liar work note tied zest safe crux kiln work exit')"
}

testAllInputOutputCombinations()
{
# Tests all combinations of input and output formats. We don't want to
# hard-code these all, so we just test whether `hex -> A` is the same as `hex
# -> A -> B -> A`.
#
# The formats below are all the formats that can represent a full seed. There
# are others like "ints", but these don't represent a full seed with the
# default --count setting.
formats="hex bip39 sskr btw btwu btwm"
for dst_format in $formats; do
for src_format in $formats; do
output_orig="$(${SEEDTOOL} | ${SEEDTOOL} --in hex --out $dst_format)"
output_roundtrip="$(${SEEDTOOL} | ${SEEDTOOL} --in hex --out $dst_format | ${SEEDTOOL} --in $dst_format --out $src_format | ${SEEDTOOL} --in $src_format --out $dst_format)"
assertEquals "dst=$dst_format and src=$src_format must be compatible." "$output_orig" "$output_roundtrip"
done
done
}

# Eat all command-line arguments before calling shunit2.
shift $#
# TODO no shunit2 on windows/msys2/mingw64 platform
Expand Down