private static string GenerateWif(Key key, string password, Network network)
        {
            byte[] vch = key.ToBytes();
            //Compute the Bitcoin address (ASCII),
            byte[] addressBytes = Encoders.ASCII.DecodeData(key.PubKey.GetAddress(network).ToString());
            // and take the first four bytes of SHA256(SHA256()) of it. Let's call this "addresshash".
            byte[] addresshash = Hashes.Hash256(addressBytes).ToBytes().SafeSubarray(0, 4);

            byte[] derived = SCrypt.BitcoinComputeDerivedKey(Encoding.UTF8.GetBytes(password), addresshash);

            byte[] encrypted = EncryptKey(vch, derived);



            byte[] version  = network.GetVersionBytes(Base58Type.ENCRYPTED_SECRET_KEY_NO_EC, true);
            byte   flagByte = 0;

            flagByte |= 0x0C0;
            flagByte |= (key.IsCompressed ? (byte)0x20 : (byte)0x00);

            byte[] bytes = version
                           .Concat(new[] { flagByte })
                           .Concat(addresshash)
                           .Concat(encrypted).ToArray();
            return(Encoders.Base58Check.EncodeData(bytes));
        }
Ejemplo n.º 2
0
		private static string GenerateWif(Key key, string password, Network network)
		{
			var vch = key.ToBytes();
			//Compute the Bitcoin address (ASCII),
			var addressBytes = Encoders.ASCII.DecodeData(key.PubKey.GetAddress(network).ToWif());
			// and take the first four bytes of SHA256(SHA256()) of it. Let's call this "addresshash".
			var addresshash = Hashes.Hash256(addressBytes).ToBytes().SafeSubarray(0, 4);

			var derived = SCrypt.BitcoinComputeDerivedKey(Encoding.UTF8.GetBytes(password), addresshash);

			var encrypted = EncryptKey(vch, derived);



			var version = network.GetVersionBytes(Base58Type.ENCRYPTED_SECRET_KEY_NO_EC);
			byte flagByte = 0;
			flagByte |= 0x0C0;
			flagByte |= (key.IsCompressed ? (byte)0x20 : (byte)0x00);

			var bytes = version
							.Concat(new[] { flagByte })
							.Concat(addresshash)
							.Concat(encrypted).ToArray();
			return Encoders.Base58Check.EncodeData(bytes);
		}
Ejemplo n.º 3
0
		private static byte[] ToBytes(Key key)
		{
			var keyBytes = key.ToBytes();
			if(!key.IsCompressed)
				return keyBytes;
			else
				return keyBytes.Concat(new byte[] { 0x01 }).ToArray();
		}
Ejemplo n.º 4
0
        internal static byte[] GetStealthSharedSecret(Key priv, PubKey pub)
        {
            X9ECParameters curve = ECKey.Secp256k1;
            ECPoint        pubec = curve.Curve.DecodePoint(pub.ToBytes());
            ECPoint        p     = pubec.Multiply(new BigInteger(1, priv.ToBytes()));

            byte[] pBytes = new PubKey(p.GetEncoded()).Compress().ToBytes();
            byte[] hash   = Hashes.SHA256(pBytes);
            return(hash);
        }
Ejemplo n.º 5
0
        internal static byte[] GetStealthSharedSecret(Key priv, PubKey pub)
        {
            var curve  = ECKey.CreateCurve();
            var pubec  = curve.Curve.DecodePoint(pub.ToBytes());
            var p      = pubec.Multiply(new BigInteger(1, priv.ToBytes()));
            var pBytes = new PubKey(p.GetEncoded()).Compress().ToBytes();
            var hash   = Hashes.SHA256(pBytes);

            return(hash);
        }
Ejemplo n.º 6
0
        static byte[] ToBytes(Key key)
        {
            var keyBytes = key.ToBytes();

            if (!key.IsCompressed)
            {
                return(keyBytes);
            }
            return(keyBytes.Concat(new byte[] { 0x01 }).ToArray());
        }
Ejemplo n.º 7
0
        public NodeContext(IRandomNumberGenerator randomNumberGenerator)
        {
            _randomNumberGenerator = randomNumberGenerator;

            byte[] prv = new byte[32];

            _randomNumberGenerator.GetBytes(prv.AsSpan());

            var k = new NBitcoin.Key(prv); //TODO Dan

            // random data
            PrivateKey  = k.ToBytes();
            LocalPubKey = k.PubKey.ToHex();
        }
Ejemplo n.º 8
0
        public byte[] ToBytes()
        {
            var b = new byte[Length];
            int i = 0;

            b[i++] = nDepth;
            Array.Copy(parentFingerprint.ToBytes(), 0, b, i, 4);
            i += 4;
            Array.Copy(Utils.ToBytes(nChild, false), 0, b, i, 4);
            i += 4;
            Array.Copy(vchChainCode, 0, b, i, 32);
            i     += 32;
            b[i++] = 0;
            Array.Copy(key.ToBytes(), 0, b, i, 32);
            return(b);
        }
Ejemplo n.º 9
0
        internal static byte[] GetStealthSharedSecret(Key priv, PubKey pub)
        {
            if (priv == null)
            {
                throw new ArgumentNullException(nameof(priv));
            }
            if (pub == null)
            {
                throw new ArgumentNullException(nameof(pub));
            }
#if HAS_SPAN
            Span <byte> tmp = stackalloc byte[33];
            pub._ECKey.GetSharedPubkey(priv._ECKey).WriteToSpan(true, tmp, out _);
            return(Hashes.SHA256(tmp));
#else
            var curve  = ECKey.Secp256k1;
            var pubec  = curve.Curve.DecodePoint(pub.ToBytes());
            var p      = pubec.Multiply(new BigInteger(1, priv.ToBytes()));
            var pBytes = new PubKey(p.GetEncoded()).Compress().ToBytes();
            var hash   = Hashes.SHA256(pBytes);
            return(hash);
#endif
        }
Ejemplo n.º 10
0
 internal static byte[] GetStealthSharedSecret(Key priv, PubKey pub)
 {
     var curve = ECKey.CreateCurve();
     var pubec = curve.Curve.DecodePoint(pub.ToBytes());
     var p = pubec.Multiply(new BigInteger(1, priv.ToBytes()));
     var pBytes = new PubKey(p.GetEncoded()).Compress().ToBytes();
     var hash = Hashes.SHA256(pBytes);
     return hash;
 }