Example #1
0
 public static PublicKey RecoverPubKeyFromMessage(ReadOnlyByteSpan message, ReadOnlyByteSpan signature)
 => PublicKey.FromRecoverCompact(GetMessageHash(message), signature);
Example #2
0
        public void Base58PrivateKeyTests()
        {
            var bsecret1  = new Base58PrivateKey();
            var bsecret2  = new Base58PrivateKey();
            var bsecret1C = new Base58PrivateKey();
            var bsecret2C = new Base58PrivateKey();
            var baddress1 = new Base58PrivateKey();

            Assert.True(bsecret1.SetString(StrSecret1));
            Assert.True(bsecret2.SetString(StrSecret2));
            Assert.True(bsecret1C.SetString(StrSecret1C));
            Assert.True(bsecret2C.SetString(StrSecret2C));
            Assert.False(baddress1.SetString(StrAddressBad));

            var key1 = bsecret1.GetKey();

            Assert.False(key1.IsCompressed);
            var key2 = bsecret2.GetKey();

            Assert.False(key2.IsCompressed);
            var key1C = bsecret1C.GetKey();

            Assert.True(key1C.IsCompressed);
            var key2C = bsecret2C.GetKey();

            Assert.True(key2C.IsCompressed);

            var pubkey1  = key1.CreatePublicKey();
            var pubkey2  = key2.CreatePublicKey();
            var pubkey1C = key1C.CreatePublicKey();
            var pubkey2C = key2C.CreatePublicKey();

            var a = pubkey1.ToAddress();

            Assert.True(key1.VerifyPubKey(pubkey1));
            Assert.False(key1.VerifyPubKey(pubkey1C));
            Assert.False(key1.VerifyPubKey(pubkey2));
            Assert.False(key1.VerifyPubKey(pubkey2C));

            Assert.False(key1C.VerifyPubKey(pubkey1));
            Assert.True(key1C.VerifyPubKey(pubkey1C));
            Assert.False(key1C.VerifyPubKey(pubkey2));
            Assert.False(key1C.VerifyPubKey(pubkey2C));

            Assert.False(key2.VerifyPubKey(pubkey1));
            Assert.False(key2.VerifyPubKey(pubkey1C));
            Assert.True(key2.VerifyPubKey(pubkey2));
            Assert.False(key2.VerifyPubKey(pubkey2C));

            Assert.False(key2C.VerifyPubKey(pubkey1));
            Assert.False(key2C.VerifyPubKey(pubkey1C));
            Assert.False(key2C.VerifyPubKey(pubkey2));
            Assert.True(key2C.VerifyPubKey(pubkey2C));

            for (var n = 0; n < 16; n++)
            {
                var strMsg  = $"Very secret message {n}: 11";
                var hashMsg = Hashes.Hash256(strMsg.AsciiToBytes());

                // normal signatures

                var sign1  = key1.SignMessage(hashMsg);
                var sign1C = key1C.SignMessage(hashMsg);
                var sign2  = key2.SignMessage(hashMsg);
                var sign2C = key2C.SignMessage(hashMsg);
                Assert.NotNull(sign1);
                Assert.NotNull(sign1C);
                Assert.NotNull(sign2);
                Assert.NotNull(sign2C);

                Assert.True(pubkey1.Verify(hashMsg, sign1));
                Assert.True(pubkey1.Verify(hashMsg, sign1C));
                Assert.False(pubkey1.Verify(hashMsg, sign2));
                Assert.False(pubkey1.Verify(hashMsg, sign2C));

                Assert.True(pubkey1C.Verify(hashMsg, sign1));
                Assert.True(pubkey1C.Verify(hashMsg, sign1C));
                Assert.False(pubkey1C.Verify(hashMsg, sign2));
                Assert.False(pubkey1C.Verify(hashMsg, sign2C));

                Assert.False(pubkey2.Verify(hashMsg, sign1));
                Assert.False(pubkey2.Verify(hashMsg, sign1C));
                Assert.True(pubkey2.Verify(hashMsg, sign2));
                Assert.True(pubkey2.Verify(hashMsg, sign2C));

                Assert.False(pubkey2C.Verify(hashMsg, sign1));
                Assert.False(pubkey2C.Verify(hashMsg, sign1C));
                Assert.True(pubkey2C.Verify(hashMsg, sign2));
                Assert.True(pubkey2C.Verify(hashMsg, sign2C));

                // compact signatures (with key recovery)

                var compact1  = key1.CreateCompactSignature(hashMsg);
                var compact1C = key1C.CreateCompactSignature(hashMsg);
                var compact2  = key2.CreateCompactSignature(hashMsg);
                var compact2C = key2C.CreateCompactSignature(hashMsg);
                Assert.NotNull(compact1);
                Assert.NotNull(compact1C);
                Assert.NotNull(compact2);
                Assert.NotNull(compact2C);

                var rkey1  = PublicKey.FromRecoverCompact(hashMsg, compact1);
                var rkey2  = PublicKey.FromRecoverCompact(hashMsg, compact2);
                var rkey1C = PublicKey.FromRecoverCompact(hashMsg, compact1C);
                var rkey2C = PublicKey.FromRecoverCompact(hashMsg, compact2C);
                Assert.NotNull(rkey1);
                Assert.NotNull(rkey2);
                Assert.NotNull(rkey1C);
                Assert.NotNull(rkey2C);

                Assert.True(rkey1 == pubkey1);
                Assert.True(rkey2 == pubkey2);
                Assert.True(rkey1C == pubkey1C);
                Assert.True(rkey2C == pubkey2C);
            }
        }