Skip to content
This repository has been archived by the owner on May 24, 2024. It is now read-only.

Commit

Permalink
Merge pull request #310 from fracek/comparable-keypair
Browse files Browse the repository at this point in the history
Equatable KeyPair
  • Loading branch information
fracek authored Feb 28, 2021
2 parents 50a067a + c84c8b9 commit 5ff3242
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 1 deletion.
1 change: 1 addition & 0 deletions stellar-dotnet-sdk-test/ClaimPredicateTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ public void TestClaimPredicateBeforeAbsoluteTime()

Assert.AreEqual(1600720493, parsed.DateTime.ToUnixTimeSeconds());
}

[TestMethod]
public void TestClaimPredicateBeforeAbsoluteTimeMaxInt()
{
Expand Down
34 changes: 34 additions & 0 deletions stellar-dotnet-sdk-test/KeyPairTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -98,5 +98,39 @@ public void TestSignWithoutSecret()
throw;
}
}

[TestMethod]
public void TestEqualityWithNullIsFalse()
{
var keyPair = KeyPair.FromAccountId("GDEAOZWTVHQZGGJY6KG4NAGJQ6DXATXAJO3AMW7C4IXLKMPWWB4FDNFZ");
Assert.IsFalse(keyPair.Equals(null));
}

[TestMethod]
public void TestEqualityWithKeyWithSecretKeyAndWithout()
{
var keyPair = KeyPair.FromSecretSeed("SDJHRQF4GCMIIKAAAQ6IHY42X73FQFLHUULAPSKKD4DFDM7UXWWCRHBE");
var otherKeyPair = KeyPair.FromAccountId(keyPair.AccountId);
Assert.IsFalse(keyPair.Equals(otherKeyPair));
Assert.IsFalse(otherKeyPair.Equals(keyPair));
}

[TestMethod]
public void TestEqualityWithKeyWithSecretKey()
{
var keyPair = KeyPair.FromSecretSeed("SDJHRQF4GCMIIKAAAQ6IHY42X73FQFLHUULAPSKKD4DFDM7UXWWCRHBE");
var otherKeyPair = KeyPair.FromSecretSeed(keyPair.SecretSeed);
Assert.IsTrue(keyPair.Equals(otherKeyPair));
Assert.IsTrue(otherKeyPair.Equals(keyPair));
}

[TestMethod]
public void TestEqualityWithOnlyPublicKey()
{
var keyPair = KeyPair.FromAccountId("GDEAOZWTVHQZGGJY6KG4NAGJQ6DXATXAJO3AMW7C4IXLKMPWWB4FDNFZ");
var otherKeyPair = KeyPair.FromAccountId(keyPair.AccountId);

Assert.IsTrue(keyPair.Equals(otherKeyPair));
}
}
}
10 changes: 9 additions & 1 deletion stellar-dotnet-sdk/KeyPair.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ namespace stellar_dotnet_sdk
/// <see cref="KeyPair"/> represents public (and secret) keys of the account.
/// Currently <see cref="KeyPair"/> only supports ed25519 but in a future this class can be abstraction layer for other public-key signature systems.
/// </summary>
public class KeyPair : IAccountId
public class KeyPair : IAccountId, IEquatable<KeyPair>
{
private KeyPair(Key secretKey, byte[] seed)
{
Expand Down Expand Up @@ -338,5 +338,13 @@ public bool Verify(byte[] data, xdr.Signature signature)
{
return Verify(data, signature.InnerValue);
}

public bool Equals(KeyPair other)
{
if (other == null) return false;
if (SeedBytes != null && other.SeedBytes == null) return false;
if (SeedBytes == null && other.SeedBytes != null) return false;
return _publicKey.Equals(other._publicKey);
}
}
}

0 comments on commit 5ff3242

Please sign in to comment.