From d6009854c0fca8fb210f85b039a426fc8fa5d7c3 Mon Sep 17 00:00:00 2001 From: Francesco Ceccon Date: Sun, 28 Feb 2021 11:04:12 +0000 Subject: [PATCH 1/2] chore: small whitespace fix --- stellar-dotnet-sdk-test/ClaimPredicateTest.cs | 1 + 1 file changed, 1 insertion(+) diff --git a/stellar-dotnet-sdk-test/ClaimPredicateTest.cs b/stellar-dotnet-sdk-test/ClaimPredicateTest.cs index 7972bee9..e616d188 100644 --- a/stellar-dotnet-sdk-test/ClaimPredicateTest.cs +++ b/stellar-dotnet-sdk-test/ClaimPredicateTest.cs @@ -17,6 +17,7 @@ public void TestClaimPredicateBeforeAbsoluteTime() Assert.AreEqual(1600720493, parsed.DateTime.ToUnixTimeSeconds()); } + [TestMethod] public void TestClaimPredicateBeforeAbsoluteTimeMaxInt() { From c84c8b998c30ede1642f047adf182bd7af39cc74 Mon Sep 17 00:00:00 2001 From: Francesco Ceccon Date: Sun, 28 Feb 2021 11:21:16 +0000 Subject: [PATCH 2/2] feat: add IEquatable impl to KeyPair --- stellar-dotnet-sdk-test/ClaimPredicateTest.cs | 2 +- stellar-dotnet-sdk-test/KeyPairTest.cs | 34 +++++++++++++++++++ stellar-dotnet-sdk/KeyPair.cs | 10 +++++- 3 files changed, 44 insertions(+), 2 deletions(-) diff --git a/stellar-dotnet-sdk-test/ClaimPredicateTest.cs b/stellar-dotnet-sdk-test/ClaimPredicateTest.cs index e616d188..a139d114 100644 --- a/stellar-dotnet-sdk-test/ClaimPredicateTest.cs +++ b/stellar-dotnet-sdk-test/ClaimPredicateTest.cs @@ -17,7 +17,7 @@ public void TestClaimPredicateBeforeAbsoluteTime() Assert.AreEqual(1600720493, parsed.DateTime.ToUnixTimeSeconds()); } - + [TestMethod] public void TestClaimPredicateBeforeAbsoluteTimeMaxInt() { diff --git a/stellar-dotnet-sdk-test/KeyPairTest.cs b/stellar-dotnet-sdk-test/KeyPairTest.cs index cc96bea9..375cb3f9 100644 --- a/stellar-dotnet-sdk-test/KeyPairTest.cs +++ b/stellar-dotnet-sdk-test/KeyPairTest.cs @@ -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)); + } } } \ No newline at end of file diff --git a/stellar-dotnet-sdk/KeyPair.cs b/stellar-dotnet-sdk/KeyPair.cs index 2ab3276b..3b38565c 100644 --- a/stellar-dotnet-sdk/KeyPair.cs +++ b/stellar-dotnet-sdk/KeyPair.cs @@ -12,7 +12,7 @@ namespace stellar_dotnet_sdk /// represents public (and secret) keys of the account. /// Currently only supports ed25519 but in a future this class can be abstraction layer for other public-key signature systems. /// - public class KeyPair : IAccountId + public class KeyPair : IAccountId, IEquatable { private KeyPair(Key secretKey, byte[] seed) { @@ -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); + } } } \ No newline at end of file