slatepack: Add WORDS_PER_LINE (#420)

This commit is contained in:
j01tz 2020-05-27 14:54:23 -07:00 committed by GitHub
parent 03cb1097e0
commit 788d050bbe
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 8 additions and 7 deletions

View file

@ -32,6 +32,7 @@ use super::types::{Slatepack, SlatepackBin};
pub static HEADER: &str = "BEGINSLATEPACK."; pub static HEADER: &str = "BEGINSLATEPACK.";
static FOOTER: &str = ". ENDSLATEPACK."; static FOOTER: &str = ". ENDSLATEPACK.";
const WORD_LENGTH: usize = 15; const WORD_LENGTH: usize = 15;
const WORDS_PER_LINE: usize = 200;
lazy_static! { lazy_static! {
static ref HEADER_REGEX: Regex = static ref HEADER_REGEX: Regex =
@ -96,12 +97,12 @@ impl SlatepackArmor {
} }
/// Encode an armored slatepack /// Encode an armored slatepack
pub fn encode(slatepack: &Slatepack, num_cols: usize) -> Result<String, Error> { pub fn encode(slatepack: &Slatepack) -> Result<String, Error> {
let slatepack_bytes = byte_ser::to_bytes(&SlatepackBin(slatepack.clone())) let slatepack_bytes = byte_ser::to_bytes(&SlatepackBin(slatepack.clone()))
.map_err(|_| ErrorKind::SlatepackSer)?; .map_err(|_| ErrorKind::SlatepackSer)?;
let encoded_slatepack = base58check(&slatepack_bytes)?; let encoded_slatepack = base58check(&slatepack_bytes)?;
let formatted_slatepack = format_slatepack(&encoded_slatepack, num_cols)?; let formatted_slatepack = format_slatepack(&format!("{}{}", HEADER, encoded_slatepack))?;
Ok(format!("{}{}{}\n", HEADER, formatted_slatepack, FOOTER)) Ok(format!("{}{}\n", formatted_slatepack, FOOTER))
} }
} }
@ -154,13 +155,13 @@ fn base58check(slate: &[u8]) -> Result<String, Error> {
} }
// Adds human readable formatting to the slate payload for armoring // Adds human readable formatting to the slate payload for armoring
fn format_slatepack(slatepack: &str, num_cols: usize) -> Result<String, Error> { fn format_slatepack(slatepack: &str) -> Result<String, Error> {
let formatter = slatepack let formatter = slatepack
.chars() .chars()
.enumerate() .enumerate()
.flat_map(|(i, c)| { .flat_map(|(i, c)| {
if i != 0 && i % WORD_LENGTH == 0 { if i != 0 && i % WORD_LENGTH == 0 {
if num_cols != 0 && i % (WORD_LENGTH * num_cols) == WORD_LENGTH * 2 { if WORDS_PER_LINE != 0 && i % (WORD_LENGTH * WORDS_PER_LINE) == 0 {
Some('\n') Some('\n')
} else { } else {
Some(' ') Some(' ')

View file

@ -104,7 +104,7 @@ impl<'a> Slatepacker<'a> {
/// Armor a slatepack /// Armor a slatepack
pub fn armor_slatepack(&self, slatepack: &Slatepack) -> Result<String, Error> { pub fn armor_slatepack(&self, slatepack: &Slatepack) -> Result<String, Error> {
SlatepackArmor::encode(&slatepack, 3) SlatepackArmor::encode(&slatepack)
} }
/// Return/upgrade slate from slatepack /// Return/upgrade slate from slatepack