2020-05-27 06:52:48 +00:00
|
|
|
|
using Base58Check;
|
2020-05-27 06:40:34 +00:00
|
|
|
|
|
|
|
|
|
namespace simplepack
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
public class SimplePack
|
|
|
|
|
{
|
|
|
|
|
private string head;
|
|
|
|
|
private string foot;
|
2020-05-27 08:00:18 +00:00
|
|
|
|
|
2020-05-27 06:40:34 +00:00
|
|
|
|
public SimplePack(string header, string footer){
|
2020-05-27 06:52:48 +00:00
|
|
|
|
// Specify human meaningful header/footer string
|
2020-05-27 06:40:34 +00:00
|
|
|
|
head = header;
|
|
|
|
|
foot = footer;
|
2020-05-27 06:52:48 +00:00
|
|
|
|
// header footer need to be >= 1 len
|
2020-05-27 06:40:34 +00:00
|
|
|
|
if (header.Length == 0 | footer.Length == 0){
|
|
|
|
|
throw new InvalidSimplePackHeader();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public byte[] decode(string data){
|
2020-05-27 06:52:48 +00:00
|
|
|
|
// Not valid if it doesn't have head/foot
|
2020-05-27 06:40:34 +00:00
|
|
|
|
if (! data.Contains(head) | ! data.Contains(foot)){ throw new InvalidSimplePackHeader();}
|
|
|
|
|
string base58Data = "";
|
2020-05-27 06:52:48 +00:00
|
|
|
|
|
|
|
|
|
// Extract the encoded part without header/footer
|
2020-05-27 06:40:34 +00:00
|
|
|
|
for (int i = head.Length; i < data.Length - foot.Length; i++){
|
|
|
|
|
base58Data += data[i];
|
|
|
|
|
}
|
2020-05-27 06:52:48 +00:00
|
|
|
|
// Will raise System.FormatException if checksum is not valid (or invalid footer/header)
|
2020-05-27 06:40:34 +00:00
|
|
|
|
return Base58CheckEncoding.Decode(base58Data);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public string encode(byte[] data){
|
|
|
|
|
// OutOfMemoryException may be thrown for large payloads
|
|
|
|
|
return head + Base58CheckEncoding.Encode(data) + foot;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
}
|