work on loading/saving state

This commit is contained in:
Kevin Froman 2020-06-04 23:16:05 -05:00
parent d2d59c21f3
commit 7ee829b94d
6 changed files with 123 additions and 27 deletions

45
tests/keyringTest.cs Normal file
View File

@ -0,0 +1,45 @@
using NUnit.Framework;
using System;
using chestcrypto;
using keyring;
using chestcrypto.identity;
using Sodium;
namespace keyringTests
{
public class Tests
{
[SetUp]
public void Setup()
{
}
[Test]
public void TestKeyringAddPublic()
{
DoublePublicKey key = new DoublePublicKey(PublicKeyAuth.GenerateKeyPair().PublicKey, PublicKeyBox.GenerateKeyPair().PublicKey);
PublicIdentity iden = new PublicIdentity(key, "bob");
DoublePublicKey key2 = new DoublePublicKey(PublicKeyAuth.GenerateKeyPair().PublicKey, PublicKeyBox.GenerateKeyPair().PublicKey);
PublicIdentity iden2 = new PublicIdentity(key2, "alice");
KeyRing ring = new KeyRing();
ring.addPublicIdentity(iden);
Assert.IsTrue(ring.publicIdentities.Contains(iden));
Assert.IsFalse(ring.publicIdentities.Contains(iden2));
}
[Test]
public void TestKeyringAddPrivate(){
DoublePrivateKey key = new DoublePrivateKey(PublicKeyAuth.GenerateKeyPair().PrivateKey, PublicKeyBox.GenerateKeyPair().PrivateKey);
PrivateIdentity iden = new PrivateIdentity(key, "bob");
DoublePrivateKey key2 = new DoublePrivateKey(PublicKeyAuth.GenerateKeyPair().PrivateKey, PublicKeyBox.GenerateKeyPair().PrivateKey);
PrivateIdentity iden2 = new PrivateIdentity(key2, "alice");
KeyRing ring = new KeyRing();
ring.addPrivateIdentity(iden);
Assert.IsTrue(ring.privateIdentities.Contains(iden));
Assert.IsFalse(ring.privateIdentities.Contains(iden2));
}
}
}

25
tests/profile/testLoad.cs Normal file
View File

@ -0,0 +1,25 @@
using NUnit.Framework;
using chestcrypto.identity;
using chestcrypto;
using chestcrypto.profile;
using System;
using System.IO;
using System.Linq;
namespace testProfileLoad
{
public class Tests
{
[SetUp]
public void Setup()
{
}
[Test]
public void TestLoad(){
RestoreKeyring restore = new RestoreKeyring("test.profile");
}
}
}

View File

@ -12,14 +12,14 @@ namespace chestcrypto.identity
private string name;
private string comment; // human's note
public PublicIdentity(DoublePublicKey doublePrivateKey, string alias){
key = doublePrivateKey;
public PublicIdentity(DoublePublicKey doublePublicKey, string alias){
key = doublePublicKey;
name = alias;
comment = "";
}
public PublicIdentity(DoublePublicKey doublePrivateKey, string alias, string note){
key = doublePrivateKey;
public PublicIdentity(DoublePublicKey doublePublicKey, string alias, string note){
key = doublePublicKey;
name = alias;
comment = note;
}

View File

@ -1,18 +0,0 @@
using chestcrypto;
namespace keyring{
internal class KeyRingFile
{
private string storageFile = null;
internal KeyRingFile(string filePath){
storageFile = filePath;
}
internal void write(byte[] data){
}
}
}

View File

@ -1,19 +1,38 @@
using chestcrypto;
using chestcrypto.exceptions;
using chestcrypto.identity;
using chestcrypto.exceptions;
using System.Collections.Generic;
using System.Linq;
namespace keyring{
public class KeyRing
{
//private List<PublicIdentity> publicIdentities;
private List<PrivateIdentity> privateIdentities;
public List<PublicIdentity> publicIdentities { get; set; }
public List<PrivateIdentity> privateIdentities { get; set; }
public KeyRing(){
//publicIdentities = new List<PublicIdentity>();
publicIdentities = new List<PublicIdentity>();
privateIdentities = new List<PrivateIdentity>();
}
public void addPublicIdentity(PublicIdentity newIden){
foreach(PublicIdentity iden in publicIdentities){
if (Enumerable.ReferenceEquals(iden.getPublicKey(), iden)){
throw new DuplicateIdentityException();
}
}
publicIdentities.Add(newIden);
}
public void addPrivateIdentity(PrivateIdentity newIden){
foreach(PrivateIdentity iden in privateIdentities){
if (Enumerable.ReferenceEquals(iden.getPrivateKey(), iden)){
throw new DuplicateIdentityException();
}
}
privateIdentities.Add(newIden);
}
}
}

View File

@ -0,0 +1,25 @@
using System.IO;
using keyring;
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
}
}
private void getKeyring(){
KeyRing keyRing = new KeyRing();
}
}
}