2020-06-05 04:16:05 +00:00
|
|
|
using System.IO;
|
|
|
|
using keyring;
|
2020-06-08 07:59:43 +00:00
|
|
|
using chestcrypto.identity;
|
2020-06-05 04:16:05 +00:00
|
|
|
|
|
|
|
namespace chestcrypto.profile{
|
|
|
|
|
|
|
|
public class RestoreKeyring{
|
|
|
|
|
|
|
|
private string profileDir;
|
|
|
|
|
|
|
|
public RestoreKeyring(string profileDirectory){
|
|
|
|
profileDir = profileDirectory;
|
|
|
|
|
|
|
|
if (! Directory.Exists(profileDir)){
|
|
|
|
Directory.CreateDirectory(profileDir); // Does not error if it exists already
|
|
|
|
}
|
2020-06-08 07:59:43 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
private PrivateIdentity getPrivateIdentityFromLine(string data){
|
|
|
|
var parts = data.Split(',');
|
|
|
|
int counter = 0;
|
|
|
|
|
|
|
|
DoublePrivateKey key = null;
|
|
|
|
string alias = "";
|
|
|
|
string note = "";
|
|
|
|
|
|
|
|
foreach (string part in parts){
|
|
|
|
switch(counter){
|
|
|
|
case 0:
|
|
|
|
key = new DoublePrivateKey(SimpleBase.Base85.Z85.Decode(part).ToArray());
|
|
|
|
break;
|
|
|
|
case 1:
|
|
|
|
alias = part;
|
|
|
|
break;
|
|
|
|
case 2:
|
|
|
|
note = part;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
throw new InvalidDataException();
|
|
|
|
}
|
|
|
|
counter += 1;
|
|
|
|
}
|
|
|
|
return new PrivateIdentity(key, alias, note);
|
2020-06-05 04:16:05 +00:00
|
|
|
}
|
2020-06-08 07:59:43 +00:00
|
|
|
private PublicIdentity getPublicIdentityFromLine(string data){
|
|
|
|
var parts = data.Split(',');
|
|
|
|
int counter = 0;
|
2020-06-05 04:16:05 +00:00
|
|
|
|
2020-06-08 07:59:43 +00:00
|
|
|
DoublePublicKey key = null;
|
|
|
|
string alias = "";
|
|
|
|
string note = "";
|
|
|
|
|
|
|
|
foreach (string part in parts){
|
|
|
|
switch(counter){
|
|
|
|
case 0:
|
|
|
|
key = new DoublePublicKey(SimpleBase.Base85.Z85.Decode(part).ToArray());
|
|
|
|
break;
|
|
|
|
case 1:
|
|
|
|
alias = part;
|
|
|
|
break;
|
|
|
|
case 2:
|
|
|
|
note = part;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
throw new InvalidDataException();
|
|
|
|
}
|
|
|
|
counter += 1;
|
|
|
|
}
|
|
|
|
return new PublicIdentity(key, alias, note);
|
|
|
|
}
|
|
|
|
public KeyRing getKeyring(){
|
2020-06-05 04:16:05 +00:00
|
|
|
KeyRing keyRing = new KeyRing();
|
2020-06-08 07:59:43 +00:00
|
|
|
string[] lines;
|
|
|
|
bool first;
|
|
|
|
try{
|
|
|
|
lines = System.IO.File.ReadAllLines(profileDir + "/private.keyring.csv");
|
|
|
|
first = true;
|
|
|
|
foreach (string line in lines){
|
|
|
|
if (first){
|
|
|
|
first = false;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (line.Length <= 1){
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
keyRing.addPrivateIdentity(getPrivateIdentityFromLine(line));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
catch(System.IO.FileNotFoundException){}
|
|
|
|
try{
|
|
|
|
lines = System.IO.File.ReadAllLines(profileDir + "/public.keyring.csv");
|
|
|
|
first = true;
|
|
|
|
foreach (string line in lines){
|
|
|
|
if (first){
|
|
|
|
first = false;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (line.Length <= 1){
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
keyRing.addPublicIdentity(getPublicIdentityFromLine(line));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
catch(System.IO.FileNotFoundException){}
|
|
|
|
|
|
|
|
return keyRing;
|
2020-06-05 04:16:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|