Example #1
0
 public void Write(BitcoinStreamWriter writer)
 {
     previousOutput.Write(writer);
     writer.WriteCompact((ulong) signatureScript.Length);
     writer.Write(signatureScript);
     writer.Write(sequence);
 }
 public void Write(BitcoinStreamWriter writer)
 {
     writer.WriteCompact((ulong) inventory.Count);
     foreach (InventoryVector vector in inventory)
     {
         vector.Write(writer);
     }
 }
 public void Write(BitcoinStreamWriter writer)
 {
     writer.Write(protocolVersion);
     writer.WriteCompact((ulong) locatorHashes.Count);
     foreach (byte[] hash in locatorHashes)
     {
         writer.Write(hash);
     }
     writer.Write(hashStop);
 }
Example #4
0
 public void Write(BitcoinStreamWriter writer)
 {
     writer.Write(value);
     writer.WriteCompact((ulong) pubkeyScript.Length);
     writer.Write(pubkeyScript);
 }
        private bool CheckSig(byte[] script)
        {
            if (dataStack.Count < 2)
            {
                valid = false;
                return false;
            }

            byte[] pubKey = PopData();
            byte[] signatureBlock = PopData();

            //todo: validate length first
            byte hashtype = signatureBlock[signatureBlock.Length - 1];
            byte[] signature = new byte[signatureBlock.Length - 1];
            Array.Copy(signatureBlock, 0, signature, 0, signatureBlock.Length - 1);

            //todo: process hashtype

            MemoryStream mem = new MemoryStream();

            // todo: remove signatures from script
            // todo: also remove all OP_CODESEPARATORs from script
            byte[] subScript = script;
            if (lastCodeSeparator >= 0)
            {
                subScript = new byte[script.Length - lastCodeSeparator - 1];
                Array.Copy(script, lastCodeSeparator + 1, subScript, 0, script.Length - lastCodeSeparator - 1);
            }

            using (BitcoinStreamWriter writer = new BitcoinStreamWriter(mem))
            {
                //todo: check if transaction exists
                writer.Write(transaction.Version);
                writer.WriteCompact((ulong) transaction.Inputs.Length);
                for (int i = 0; i < transaction.Inputs.Length; i++)
                {
                    TxIn input = transaction.Inputs[i];
                    input.PreviousOutput.Write(writer);
                    if (transactionInputNumber == i)
                    {
                        writer.WriteCompact((ulong) subScript.Length);
                        writer.Write(subScript);
                    }
                    else
                    {
                        writer.WriteCompact(0);
                    }
                    writer.Write(input.Sequence);
                }
                writer.WriteArray(transaction.Outputs, (w, v) => v.Write(writer));
                writer.Write(transaction.LockTime);
                writer.Write((uint) hashtype);
            }

            byte[] signedData = mem.ToArray();

            return SignatureUtils.Verify(signedData, pubKey, signature);
        }
        private static byte[] GetMessageHash(string message)
        {
            byte[] messagePrefix = Encoding.ASCII.GetBytes("Bitcoin Signed Message:\n");
            byte[] messageBytes = Encoding.UTF8.GetBytes(message);

            MemoryStream mem = new MemoryStream();
            using (BitcoinStreamWriter writer = new BitcoinStreamWriter(mem))
            {
                writer.WriteCompact((ulong) messagePrefix.Length);
                writer.Write(messagePrefix, 0, messagePrefix.Length);

                writer.WriteCompact((ulong) messageBytes.Length);
                writer.Write(messageBytes, 0, messageBytes.Length);
            }

            byte[] text = mem.ToArray();
            return CryptoUtils.DoubleSha256(text);
        }
 public void Write(BitcoinStreamWriter writer)
 {
     blockHeader.Write(writer);
     writer.Write(totalTransactions);
     writer.WriteCompact((ulong) hashes.Count);
     foreach (byte[] hash in hashes)
     {
         writer.Write(hash);
     }
     writer.WriteCompact((ulong) flags.Length);
     writer.Write(flags);
 }