Exemple #1
0
    public void ConverterTest()
    {
        // Get a compressed private key.
        string        base58        = "Kwr371tjA9u2rFSMZjTNun2PXXP3WPZu2afRHTcta6KxEUdm1vEw";
        BitcoinSecret bitcoinSecret = Network.Main.CreateBitcoinSecret(base58);
        Key           privateKey    = bitcoinSecret.PrivateKey;

        Assert.True(privateKey.IsCompressed);

        uint256          hashMsg           = Hashes.DoubleSHA256(Encoding.ASCII.GetBytes("compact hashing test"));
        CompactSignature expectedSignature = privateKey.SignCompact(hashMsg);

        Assert.NotNull(expectedSignature);

        byte[] sigBytes = CompactSignatureJsonConverter.ToBytes(expectedSignature);
        string hex      = ByteHelpers.ToHex(sigBytes);

        Assert.Equal("1F71932FFF735FA6A57787191A296717F71270B2B7E1D90008B7147117F250DBDE012359EED51D28682B1AAB686A8FD8A411A8D07F1EB4D7CDAC5B7EBE73F260A0", hex);

        CompactSignatureJsonConverter converter = new();
        string json = JsonConvert.SerializeObject(expectedSignature, converter);

        Assert.Equal("\"H3GTL/9zX6ald4cZGilnF/cScLK34dkACLcUcRfyUNveASNZ7tUdKGgrGqtoao/YpBGo0H8etNfNrFt+vnPyYKA=\"", json);

        CompactSignature?actual = JsonConvert.DeserializeObject <CompactSignature>(json, converter);

        Assert.NotNull(actual);

        Assert.Equal(actual !.RecoveryId, expectedSignature.RecoveryId);
        Assert.Equal(actual.Signature, expectedSignature.Signature);
    }
Exemple #2
0
        /// <summary>
        /// Verify the Dogecoin message signed by some dogecoin private key
        /// </summary>
        /// <param name="message">input original message</param>
        /// <param name="signature">signature made by some dogecoin address</param>
        /// <param name="address">Dogecoin address</param>
        /// <returns></returns>
        public static async Task <(bool, string)> VerifyDogeMessage(string message, string signature, string address, bool messageIsAlreadyHash = false)
        {
            if (string.IsNullOrEmpty(message) || string.IsNullOrEmpty(signature) || string.IsNullOrEmpty(address))
            {
                return(false, "Input parameters cannot be empty or null.");
            }

            try
            {
                var add = BitcoinAddress.Create(address, DogeTransactionHelpers.Network);
                //var vadd = (add as IPubkeyHashUsable);

                var split = signature.Split('@');
                if (split.Length > 1)
                {
                    var sg         = Convert.FromBase64String(split[1]);
                    var recoveryId = Convert.ToInt32(split[0]);

                    var sgs = new CompactSignature(recoveryId, sg);
                    Console.WriteLine("Signature loaded.");
                    PubKey recoveredPubKey = null;
                    if (!messageIsAlreadyHash)
                    {
                        uint256 hash = NBitcoin.Crypto.Hashes.DoubleSHA256(Encoding.UTF8.GetBytes(message));
                        recoveredPubKey = PubKey.RecoverCompact(hash, sgs);
                    }
                    else
                    {
                        recoveredPubKey = PubKey.RecoverCompact(uint256.Parse(message), sgs);
                    }

                    var pk = recoveredPubKey.GetAddress(ScriptPubKeyType.Legacy, DogeTransactionHelpers.Network);
                    if (pk.ToString() == add.ToString())
                    {
                        return(true, "Verified.");
                    }
                }
            }
            catch
            {
                return(false, "Wrong input. Cannot verify the message signature.");
            }
            return(false, "Not verified.");
        }
Exemple #3
0
    public void SignatureTest()
    {
        // Get a compressed private key.
        string        base58        = "Kwr371tjA9u2rFSMZjTNun2PXXP3WPZu2afRHTcta6KxEUdm1vEw";
        BitcoinSecret bitcoinSecret = Network.Main.CreateBitcoinSecret(base58);
        Key           privateKey    = bitcoinSecret.PrivateKey;

        Assert.True(privateKey.IsCompressed);

        uint256          hashMsg          = Hashes.DoubleSHA256(Encoding.ASCII.GetBytes("compact hashing test"));
        CompactSignature compactSignature = privateKey.SignCompact(hashMsg);

        Assert.NotNull(compactSignature);

        byte[] sigBytes = CompactSignatureJsonConverter.ToBytes(compactSignature);
        string hex      = ByteHelpers.ToHex(sigBytes);

        Assert.Equal("1F71932FFF735FA6A57787191A296717F71270B2B7E1D90008B7147117F250DBDE012359EED51D28682B1AAB686A8FD8A411A8D07F1EB4D7CDAC5B7EBE73F260A0", hex);
    }
Exemple #4
0
        private static ISignature GetCompactSignature(IEnumerable <CodeGraph.Parameter[]> overloads)
        {
            List <Parameter[]> parameterOverloads = new List <Parameter[]>();

            foreach (CodeGraph.Parameter[] overload in overloads)
            {
                // Input parameters may be optional. In these cases, create multiple overloads.
                for (int index = overload.Length - 1;
                     index >= 0 && overload[index] is InParameter && ((InParameter)overload[index]).IsOptional;
                     index--)
                {
                    parameterOverloads.Add(ConvertOverload(overload.Take(index)).ToArray());
                }
                parameterOverloads.Add(ConvertOverload(overload).ToArray());
            }

            return(parameterOverloads.Any()
                ? CompactSignature.FromOverloads(parameterOverloads.ToArray())
                : new Sequence());
        }
Exemple #5
0
        public void key_test1()
        {
            BitcoinSecret bsecret1  = Network.Main.CreateBitcoinSecret(strSecret1);
            BitcoinSecret bsecret2  = Network.Main.CreateBitcoinSecret(strSecret2);
            BitcoinSecret bsecret1C = Network.Main.CreateBitcoinSecret(strSecret1C);
            BitcoinSecret bsecret2C = Network.Main.CreateBitcoinSecret(strSecret2C);

            Assert.Throws <FormatException>(() => Network.Main.CreateBitcoinSecret(strAddressBad));

            Key key1 = bsecret1.PrivateKey;

            Assert.True(key1.IsCompressed == false);
            Assert.True(bsecret1.Copy(true).PrivateKey.IsCompressed == true);
            Assert.True(bsecret1.Copy(true).Copy(false).IsCompressed == false);
            Assert.True(bsecret1.Copy(true).Copy(false).ToString() == bsecret1.ToString());
            Key key2 = bsecret2.PrivateKey;

            Assert.True(key2.IsCompressed == false);
            Key key1C = bsecret1C.PrivateKey;

            Assert.True(key1C.IsCompressed == true);
            Key key2C = bsecret2C.PrivateKey;

            Assert.True(key1C.IsCompressed == true);

            PubKey pubkey1  = key1.PubKey;
            PubKey pubkey2  = key2.PubKey;
            PubKey pubkey1C = key1C.PubKey;
            PubKey pubkey2C = key2C.PubKey;

            Assert.True(addr1.Hash == pubkey1.Hash);
            Assert.True(addr2.Hash == pubkey2.Hash);
            Assert.True(addr1C.Hash == pubkey1C.Hash);
            Assert.True(addr2C.Hash == pubkey2C.Hash);


            for (int n = 0; n < 16; n++)
            {
                string strMsg = String.Format("Very secret message {0}: 11", n);
                if (n == 10)
                {
                    //Test one long message
                    strMsg = String.Join(",", Enumerable.Range(0, 2000).Select(i => i.ToString()).ToArray());
                }
                uint256 hashMsg = Hashes.DoubleSHA256(TestUtils.ToBytes(strMsg));

                // normal signatures

                ECDSASignature sign1 = null, sign2 = null, sign1C = null, sign2C = null;
                List <Task>    tasks = new List <Task>();
                sign1  = key1.Sign(hashMsg);
                sign2  = key2.Sign(hashMsg);
                sign1C = key1C.Sign(hashMsg);
                sign2C = key2C.Sign(hashMsg);

                for (int i = 0; i < 30; i++)
                {
                    Assert.True(pubkey1.Verify(hashMsg, sign1));
                    Assert.True(pubkey2.Verify(hashMsg, sign2));
                    Assert.True(pubkey1C.Verify(hashMsg, sign1C));
                    Assert.True(pubkey2C.Verify(hashMsg, sign2C));

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

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

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

                    Assert.True(!pubkey2C.Verify(hashMsg, sign1));
                    Assert.True(pubkey2C.Verify(hashMsg, sign2));
                    Assert.True(!pubkey2C.Verify(hashMsg, sign1C));
                    Assert.True(pubkey2C.Verify(hashMsg, sign2C));
                }
                // compact signatures (with key recovery)

                CompactSignature csign1 = null, csign2 = null, csign1C = null, csign2C = null;

                if (key1.IsCompressed)
                {
                    csign1  = key1.SignCompact(hashMsg);
                    csign2  = key2.SignCompact(hashMsg);
                    csign1C = key1C.SignCompact(hashMsg);
                    csign2C = key2C.SignCompact(hashMsg);

                    PubKey rkey1 = null, rkey2 = null, rkey1C = null, rkey2C = null;
                    rkey1  = PubKey.RecoverCompact(hashMsg, csign1);
                    rkey2  = PubKey.RecoverCompact(hashMsg, csign2);
                    rkey1C = PubKey.RecoverCompact(hashMsg, csign1C);
                    rkey2C = PubKey.RecoverCompact(hashMsg, csign2C);

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

                    Assert.True(sign1.IsLowR && sign1.ToDER().Length <= 70);
                    Assert.True(sign2.IsLowR && sign2.ToDER().Length <= 70);
                    Assert.True(sign1C.IsLowR && sign1C.ToDER().Length <= 70);
                    Assert.True(sign2C.IsLowR && sign2C.ToDER().Length <= 70);
                }
            }
        }