Exemple #1
0
        /// <summary>
        /// Gets P2PKH Bitcoin Address from a key
        /// - Uses the compressed public key format for address generation
        /// </summary>
        /// <param name="publicKey">uncompressed public key (64 bytes)</param>
        /// <param name="dsa">ECDsa algorithm interface</param>
        /// <param name="version">Version bytes of bitcoin network (0x00 for mainnet)</param>
        /// <param name="context">optional context</param>
        /// <returns>Base58check bitcoin address</returns>
        public static string GetP2PKHAddress(Span <byte> publicKey, IECDsa dsa, Span <byte> version, DisposableContext?context = null)
        {
            if (dsa == null)
            {
                throw new ArgumentException($"Invalid input parameters to '{nameof(GetP2PKHAddress)}'. '{nameof(dsa)}' cannot be null.");
            }

            var compressedPublicKey = dsa.GetSECCompressedPublicKey(publicKey, context);

            return(compressedPublicKey.Hash().RIPEMD160().ToP2PKHAddress(version));
        }
Exemple #2
0
        private Signature TestSign(IECDsa signer)
        {
            Key key = signer.CreateKey();

            using var rnd = RandomNumberGenerator.Create();

            var data = new byte[256];

            rnd.GetBytes(data);
            var signature = signer.SignData(data, key);

            if (signer.VerifyData(data, signature, key))
            {
                return(signature);
            }
            throw new Exception();
        }
Exemple #3
0
        private void TestRSAccessors(IECDsa signer)
        {
            Key key = signer.CreateKey();

            using var rnd = RandomNumberGenerator.Create();
            var data = new byte[32];

            rnd.GetBytes(data);
            var signature = signer.SignData(data, key);
            var R         = signature.GetR();
            var S         = signature.GetS();

            Signature RS = new Signature {
                Bytes = new byte[64]
            };

            RS.Set(R, S);

            Assert.Equal(signature.Bytes.ToArray(), RS.Bytes.ToArray());
        }
Exemple #4
0
        public static Span <byte> CreatePrivateKey(IECDsa ecdsa, int keyLength = 32)
        {
            if (ecdsa == null)
            {
                throw new CryptoException($"Invalid input to '{nameof(CreatePrivateKey)}'. '{nameof(ecdsa)}' should not be NULL.");
            }

            using (var rnd = RandomNumberGenerator.Create())
            {
                var privateKey = new byte[keyLength];
                Key key;
                do
                {
                    rnd.GetBytes(privateKey);
                    key = new Key
                    {
                        PrivateKey = privateKey,
                        PublicKey  = null
                    };
                }while (!ecdsa.IsPrivateKeyValid(key));

                return(privateKey);
            }
        }
Exemple #5
0
 /// <summary>
 /// Gets P2PKH Bitcoin Address from a key
 /// - Uses the compressed public key format for address generation
 /// </summary>
 /// <param name="key">key containing the public key</param>
 /// <param name="dsa">ECDsa algorithm interface</param>
 /// <param name="version">Version bytes of bitcoin network (0x00 for mainnet)</param>
 /// <param name="context">optional context</param>
 /// <returns>Base58check bitcoin address</returns>
 public static string GetP2PKHAddress(Key key, IECDsa dsa, Span <byte> version, DisposableContext?context = null)
 {
     return(GetP2PKHAddress(key.PublicKey, dsa, version, context));
 }