private void OpHash160(ParsedOpCode obj)
        {
            var value = MainStack.Pop();
            var hash  = HashUtil.Ripemd160(HashUtil.Blake256(value));

            MainStack.Push(hash);
        }
Exemple #2
0
        /// <summary>
        /// Maps a private key to a compressed public key + compressed public key hash
        /// </summary>
        /// <param name="privateKey"></param>
        /// <returns></returns>
        private (byte[] PrivateKey, byte[] PublicKey, byte[] PublicKeyHash) ExpandPrivateKey(byte[] privateKey)
        {
            var publicKey     = _securityService.GetPublicKey(privateKey, true);
            var publicKeyHash = HashUtil.Ripemd160(HashUtil.Blake256(publicKey));

            return(privateKey, publicKey, publicKeyHash);
        }
Exemple #3
0
        public byte[] GetFullHash()
        {
            var prefixHash  = GetHash(TxSerializeType.NoWitness);
            var witnessHash = GetHash(TxSerializeType.OnlyWitness);

            return(HashUtil.Blake256(prefixHash, witnessHash));
        }
Exemple #4
0
        private string GetPublicAddress(byte[] publicKey)
        {
            var prefix     = _network.AddressPrefix.PayToPublicKeyHash;
            var pubKeyHash = HashUtil.Ripemd160(HashUtil.Blake256(publicKey));

            return(new Base58Check().Encode(prefix, pubKeyHash, false));
        }
Exemple #5
0
 public MessageHeader(CurrencyNet currencyNet, MsgCommand command, byte[] payload)
 {
     CurrencyNetwork = currencyNet;
     Command         = command;
     PayloadLength   = payload.Length;
     Checksum        = HashUtil.Blake256(payload).Take(ChecksumLengthBytes).ToArray();
 }
Exemple #6
0
        public void PrivateKey_Sign_ReturnsExpectedSignatureDer()
        {
            var privateKey  = new ECPrivateSecurityService(_privateKey);
            var messageHash = HashUtil.Blake256(_message);
            var signature   = privateKey.Sign(messageHash).Serialize();

            Assert.Equal(_signature, signature);
        }
Exemple #7
0
        public string GetPublicAddress(Network network, PublicKeyFormat format)
        {
            var prefix        = network.AddressPrefix.PayToPublicKeyHash;
            var isCompressed  = format == PublicKeyFormat.PKFCompressed;
            var publicKeyHash = HashUtil.Ripemd160(HashUtil.Blake256(Bytes));

            return(new Base58Check(HashUtil.Blake256).Encode(prefix, publicKeyHash, isCompressed));
        }
Exemple #8
0
        public void GetRoot_GivenTwoHashes_ReturnsHashOfTwoElements()
        {
            var hashA = _hashes[0];
            var hashB = _hashes[1];

            var result = _subject.GetRoot(new[] { hashA, hashB });

            var expected = HashUtil.Blake256(hashA.Concat(hashB).ToArray());

            Assert.Equal(expected, (IEnumerable <byte>)result);
        }
Exemple #9
0
 public MerkleTreeTests()
 {
     _subject = new MerkleTree();
     _hashes  = new[]
     {
         HashUtil.Blake256(new byte[] { 0x01 }),
         HashUtil.Blake256(new byte[] { 0x02 }),
         HashUtil.Blake256(new byte[] { 0x03 }),
         HashUtil.Blake256(new byte[] { 0x04 }),
         HashUtil.Blake256(new byte[] { 0x05 }),
     };
 }
Exemple #10
0
        public void ECSecurityService_VerifySignature_VerifiesGeneratedSignatureAgainstPublicKey()
        {
            const bool isCompressed = false;
            var        messageHash  = HashUtil.Blake256(_message);
            var        signature    = new ECSignature(_signature);

            var privateSecurityService = new ECPrivateSecurityService(_privateKey);
            var publicKeyBytes         = privateSecurityService.GetPublicKey(isCompressed);
            var publicSecurityService  = new ECPublicSecurityService(publicKeyBytes);

            var hasValidSignature = publicSecurityService.VerifySignature(messageHash, signature);

            Assert.True(hasValidSignature);
        }
Exemple #11
0
        /// <summary>
        /// Calculates the hash of a transaction to be signed.
        /// </summary>
        /// <param name="transaction"></param>
        /// <returns></returns>
        private byte[] CalculateTxHash(MsgTx transaction)
        {
            var wbuf = new List <byte>(32 * 2 + 4);

            wbuf.AddRange(BitConverter.GetBytes((uint)1));

            var prefixHash  = transaction.GetHash(TxSerializeType.NoWitness);
            var witnessHash = transaction.GetHash(TxSerializeType.WitnessSigning);

            wbuf.AddRange(prefixHash);
            wbuf.AddRange(witnessHash);

            return(HashUtil.Blake256(wbuf.ToArray()));
        }
Exemple #12
0
        /// <summary>
        ///     Calculates the BLAKE256 hash of the current instance.  Witness data is not serialized.
        /// </summary>
        /// <returns>The hash as a byte[] with length 32</returns>
        public byte[] GetHash(TxSerializeType serializationType = TxSerializeType.NoWitness)
        {
            byte[] bytes;

            using (var ms = new MemoryStream())
                using (var bw = new BinaryWriter(ms))
                {
                    Encode(bw, serializationType);
                    bw.Flush();
                    bytes = ms.ToArray();
                }

            return(HashUtil.Blake256(bytes));
        }
Exemple #13
0
        public void GetRoot_GivenThreeHashes_ReturnsExpectedMerkleRoot()
        {
            var hashA = _hashes[0];
            var hashB = _hashes[1];
            var hashC = _hashes[3];

            var result = _subject.GetRoot(new[] { hashA, hashB, hashC });

            // Since the number of elements is not balanced, we should make sure that
            // the logic hashes C with itself as the tree is build.

            var hashAB   = HashUtil.Blake256(hashA, hashB);
            var hashCC   = HashUtil.Blake256(hashC, hashC);
            var expected = HashUtil.Blake256(hashAB, hashCC);

            Assert.Equal(expected, (IEnumerable <byte>)result);
        }
Exemple #14
0
        /// <summary>
        /// Calculates the merkle root given an array of hashes of the leaf nodes.
        /// </summary>
        /// <param name="hashes"></param>
        /// <returns></returns>
        public byte[] GetRoot(byte[][] hashes)
        {
            if (hashes == null || hashes.Length == 0)
            {
                return(new byte[32]);
            }

            var input = ExtendCollectionSize(hashes);
            var queue = new Queue <byte[]>(input);

            while (queue.Count > 1)
            {
                var a = queue.Dequeue();
                var b = queue.Dequeue() ?? a;

                // If both elements are null, return null.
                // Otherwise, return the hash of both concatenanted
                var parent = a == null && b == null ? null : HashUtil.Blake256(a, b);
                queue.Enqueue(parent);
            }

            // The last element is the merkle root.
            return(queue.Dequeue());
        }
        public static byte[] CalculateSignatureHash(ParsedOpCode[] subScript, SignatureHashType hashType, MsgTx transaction, int index)
        {
            const SignatureHashType mask = (SignatureHashType)0x1f;

            if ((hashType & mask) == SignatureHashType.Single && index >= transaction.TxOut.Length)
            {
                throw new InvalidSignatureException("SignatureHashType.Single index out of range");
            }

            // Clear out signature scripts for input transactions not at index
            // transactionIndex
            for (var i = 0; i < transaction.TxIn.Length; i++)
            {
                transaction.TxIn[i].SignatureScript =
                    i == index?
                    subScript.SelectMany(s => s.Serialize()).ToArray()
                        : new byte[0];
            }

            switch (hashType & mask)
            {
            case SignatureHashType.None:
                transaction.TxOut = new TxOut[0];
                for (var i = 0; i < transaction.TxIn.Length; i++)
                {
                    if (i != index)
                    {
                        transaction.TxIn[i].Sequence = 0;
                    }
                }
                break;

            case SignatureHashType.Single:
                transaction.TxOut = new TxOut[index];

                for (var i = 0; i < index; i++)
                {
                    transaction.TxOut[i].Value    = -1;
                    transaction.TxOut[i].PkScript = null;
                }

                for (var i = 0; i < transaction.TxIn.Length; i++)
                {
                    if (i != index)
                    {
                        transaction.TxIn[i].Sequence = 0;
                    }
                }
                break;

            case SignatureHashType.Old:
                break;

            case SignatureHashType.All:
                break;

            case SignatureHashType.AllValue:
                break;

            case SignatureHashType.AnyOneCanPay:
                break;

            default:
                throw new ArgumentOutOfRangeException();
            }

            if ((hashType & SignatureHashType.AnyOneCanPay) != 0)
            {
                transaction.TxIn = transaction.TxIn
                                   .Skip(index)
                                   .Take(1)
                                   .ToArray();
            }

            var wbuf = new List <byte>(32 * 2 + 4);

            wbuf.AddRange(BitConverter.GetBytes((uint)hashType));

            var prefixHash  = transaction.GetHash(TxSerializeType.NoWitness);
            var witnessHash = transaction.GetHash(
                (hashType & mask) != SignatureHashType.All ?
                TxSerializeType.WitnessSigning :
                TxSerializeType.WitnessValueSigning
                );

            wbuf.AddRange(prefixHash);
            wbuf.AddRange(witnessHash);

            return(HashUtil.Blake256(wbuf.ToArray()));
        }
        private void OpBlake256(ParsedOpCode obj)
        {
            var value = MainStack.Pop();

            MainStack.Push(HashUtil.Blake256(value));
        }
Exemple #17
0
 public byte[] GetHash()
 {
     return(HashUtil.Blake256(Encode()));
 }