/// <inheritdoc/>
        public byte[] EncodeSigned(Transaction transaction)
        {
            var rlpcollection = new List <byte[]>();

            var rlpSignatures = new List <byte[]>();

            for (Int32 i = 0; i < transaction.Inputs.Count; ++i)
            {
                if (transaction.Signatures[i] != null)
                {
                    rlpSignatures.Add(RLP.EncodeElement(transaction.Signatures[i]));
                }
                else
                {
                    // missing signature - cannot return valid encoded transaction
                    return(new byte[0]);
                }
            }

            rlpcollection.Add(RLP.EncodeList(rlpSignatures.ToArray()));

            var rlpInputs = new List <byte[]>();

            transaction.Inputs.ForEach(x => rlpInputs.Add(x.GetRLPEncoded()));
            rlpInputs.AddRange(Enumerable.Repeat(new TransactionInputData().GetRLPEncoded(), MAX_INPUTS - transaction.Inputs.Count));
            rlpcollection.Add(RLP.EncodeList(rlpInputs.ToArray()));

            var rlpOutputs = new List <byte[]>();

            transaction.Outputs.ForEach(x => rlpOutputs.Add(x.GetRLPEncoded()));
            rlpOutputs.AddRange(Enumerable.Repeat(new TransactionOutputData().GetRLPEncoded(), MAX_OUTPUTS - transaction.Outputs.Count));
            rlpcollection.Add(RLP.EncodeList(rlpOutputs.ToArray()));

            return(RLP.EncodeList(rlpcollection.ToArray()));
        }
Ejemplo n.º 2
0
 public byte[] ToBytes()
 {
     return(RLP.EncodeList(
                RLP.EncodeElement(PublicKey.Buffer.ToArray()),
                RLP.EncodeElement(ThresholdSignaturePublicKey)
                ));
 }
Ejemplo n.º 3
0
 public byte[] ToBytes()
 {
     return(RLP.EncodeList(
                RLP.EncodeElement(ContractAddress.ToBytes().ToArray()),
                RLP.EncodeElement(ByteCode)
                ));
 }
Ejemplo n.º 4
0
        public async Task SignTransaction()
        {
            HoardID to       = new HoardID("0x4bc1EF56d94c766A49153A102096E56fAE2004e1");
            var     nonce    = 324.ToBytesForRLPEncoding();
            var     gasPrice = 10000000000000.ToBytesForRLPEncoding();
            var     startGas = 21000.ToBytesForRLPEncoding();
            var     value    = 10000.ToBytesForRLPEncoding();
            var     data     = "".HexToByteArray();

            var txEncoded = new List <byte[]>();

            txEncoded.Add(RLP.EncodeElement(nonce));
            txEncoded.Add(RLP.EncodeElement(gasPrice));
            txEncoded.Add(RLP.EncodeElement(startGas));
            txEncoded.Add(RLP.EncodeElement(to.ToHexByteArray()));
            txEncoded.Add(RLP.EncodeElement(value));
            txEncoded.Add(RLP.EncodeElement(data));

            var rlpEncodedTransaction = RLP.EncodeList(txEncoded.ToArray());

            var user = await signer.RequestProfile(signer.Name + "\0");

            var signature = await user.SignTransaction(rlpEncodedTransaction);

            var account = Hoard.Utils.Helper.RecoverHoardIdFromTransaction(signature, rlpEncodedTransaction);

            Assert.Equal(user.ID, account);
        }
Ejemplo n.º 5
0
        /// <summary>
        /// Transforms this log into the log type used by consensus.
        ///
        /// TODO: Cache this value.
        /// </summary>
        public Log ToLog(IContractPrimitiveSerializer serializer)
        {
            (List <byte[]> topics, List <byte[]> fields) = this.Serialize(serializer);

            byte[] encodedFields = RLP.EncodeList(fields.Select(RLP.EncodeElement).ToArray());

            return(new Log(this.ContractAddress, topics, encodedFields));
        }
Ejemplo n.º 6
0
 public byte[] ToBytes()
 {
     return(RLP.EncodeList(
                RLP.EncodeElement(this.Hash.ToBytes()),
                RLP.EncodeElement(BitConverter.GetBytes(this.Nvout)),
                RLP.EncodeElement(BitConverter.GetBytes(this.Value))
                ));
 }
 public byte[] ToBytes()
 {
     return(RLP.EncodeList(
                RLP.EncodeElement(this.CodeHash ?? new byte[0]),
                RLP.EncodeElement(this.StateRoot ?? new byte[0]),
                RLP.EncodeElement(this.UnspentHash ?? new byte[0])
                ));
 }
        /// <summary>
        /// Returns rlp encoded transaction output data
        /// </summary>
        /// <returns></returns>
        public byte[] GetRLPEncoded()
        {
            var data = new byte[3][];

            data[0] = RLP.EncodeElement(Owner.HexToByteArray());
            data[1] = RLP.EncodeElement(Currency.HexToByteArray());
            data[2] = RLP.EncodeElement(Value);
            return(RLP.EncodeList(data));
        }
Ejemplo n.º 9
0
        /// <summary>
        /// Returns rlp encoded transaction input data
        /// </summary>
        /// <returns></returns>
        public byte[] GetRLPEncoded()
        {
            var data = new byte[3][];

            data[0] = RLP.EncodeElement(new BigInteger(BlkNum).ToBytesForRLPEncoding());
            data[1] = RLP.EncodeElement(new BigInteger(TxIndex).ToBytesForRLPEncoding());
            data[2] = RLP.EncodeElement(new BigInteger(OIndex).ToBytesForRLPEncoding());
            return(RLP.EncodeList(data));
        }
Ejemplo n.º 10
0
 public byte[] ToBytes()
 {
     return(RLP.EncodeList(
                RLP.EncodeElement(this.CodeHash ?? new byte[0]),
                RLP.EncodeElement(this.StateRoot ?? new byte[0]),
                RLP.EncodeElement(this.UnspentHash ?? new byte[0]),
                RLP.EncodeElement(this.TypeName == null ? new byte[0] : Encoding.UTF8.GetBytes(this.TypeName))
                ));
 }
Ejemplo n.º 11
0
        public byte[] ToBytes()
        {
            var a = new List <byte[]> {
                TpkePublicKey
            };

            a.AddRange(Validators.Select(c => c.ToBytes()));
            return(RLP.EncodeList(a.Select(RLP.EncodeElement).ToArray()));
        }
Ejemplo n.º 12
0
        /// <summary>
        /// Compile and sign a .cs file before packaging it into the format it goes into a smart contract transaction in.
        /// </summary>
        public byte[] PackageSignedCSharpFile(Key privKey, string path)
        {
            (byte[] contractCode, byte[] signature)signed = this.SignCSharpFile(privKey, path);

            return(RLP.EncodeList(
                       RLP.EncodeElement(signed.contractCode),
                       RLP.EncodeElement(signed.signature)
                       ));
        }
Ejemplo n.º 13
0
        public byte[] ToBytesRlp()
        {
            IList <byte[]> encodedTopics = this.Topics.Select(x => RLP.EncodeElement(x)).ToList();

            return(RLP.EncodeList(
                       RLP.EncodeElement(this.Address.ToBytes()),
                       RLP.EncodeElement(RLP.EncodeList(encodedTopics.ToArray())),
                       RLP.EncodeElement(this.Data)
                       ));
        }
Ejemplo n.º 14
0
        /// <summary>
        /// Parse a receipt into the consensus data.
        /// </summary>
        public byte[] ToConsensusBytesRlp()
        {
            IList <byte[]> encodedLogs = this.Logs.Select(x => RLP.EncodeElement(x.ToBytesRlp())).ToList();

            return(RLP.EncodeList(
                       RLP.EncodeElement(this.PostState.ToBytes()),
                       RLP.EncodeElement(BitConverter.GetBytes(this.GasUsed)),
                       RLP.EncodeElement(this.Bloom.ToBytes()),
                       RLP.EncodeElement(RLP.EncodeList(encodedLogs.ToArray()))
                       ));
        }
Ejemplo n.º 15
0
        public void ShouldEncodeEmptyList()
        {
            byte[][] test     = new byte[0][];
            string   expected = "c0";

            byte[] encoderesult = RLP.EncodeList(test);
            Assert.Equal(expected, encoderesult.ToHex());

            var decodeResult = RLP.Decode(encoderesult)[0] as RLPCollection;

            Assert.True(decodeResult.Count == 0);
        }
Ejemplo n.º 16
0
        private static void AssertStringCollection(string[] test, string expected)
        {
            byte[] encoderesult = RLP.EncodeList(EncodeElementsBytes(test.ToBytesForRLPEncoding()));
            Assert.Equal(expected, encoderesult.ToHex());

            var decodeResult = RLP.Decode(encoderesult)[0] as RLPCollection;

            for (int i = 0; i < test.Length; i++)
            {
                Assert.Equal(test[i], decodeResult[i].RLPData.ToStringFromRLPDecoded());
            }
        }
        private byte[] SerializeArray(Array array)
        {
            List <byte[]> toEncode = new List <byte[]>();

            for (int i = 0; i < array.Length; i++)
            {
                object value      = array.GetValue(i);
                byte[] serialized = Serialize(value);
                toEncode.Add(RLP.EncodeElement(serialized));
            }

            return(RLP.EncodeList(toEncode.ToArray()));
        }
Ejemplo n.º 18
0
        private byte[] SerializeStruct(object o)
        {
            List <byte[]> toEncode = new List <byte[]>();

            foreach (FieldInfo field in o.GetType().GetFields())
            {
                object value      = field.GetValue(o);
                byte[] serialized = this.Serialize(value);
                toEncode.Add(RLP.EncodeElement(serialized));
            }

            return(RLP.EncodeList(toEncode.ToArray()));
        }
Ejemplo n.º 19
0
        public byte[] Encode()
        {
            List <byte[]> datas = new List <byte[]>();

            foreach (IRlpItem item in this)
            {
                if (item != null)
                {
                    datas.Add(item.Encode());
                }
            }
            return(RLP.EncodeList(datas.ToArray()));
        }
Ejemplo n.º 20
0
 private byte[] Encode(int depth, bool forceHash)
 {
     if (!this.Dirty)
     {
         return(this.Hash != null?RLP.EncodeElement(this.Hash) : this.rlp);
     }
     else
     {
         NodeType type = this.NodeType;
         byte[]   ret;
         if (type == NodeType.BranchNode)
         {
             byte[][] encoded = new byte[17][];
             for (int i = 0; i < 16; i++)
             {
                 Node child = this.BranchNodeGetChild(i);
                 encoded[i] = child == null ? PatriciaTrie.EmptyElementRlp : child.Encode(depth + 1, false);
             }
             byte[] value = this.BranchNodeGetValue();
             encoded[16] = RLP.EncodeElement(value);
             ret         = RLP.EncodeList(encoded);
         }
         else if (type == NodeType.KeyValueNodeNode)
         {
             ret = RLP.EncodeList(RLP.EncodeElement(this.KvNodeGetKey().ToPacked()), this.KvNodeGetChildNode().Encode(depth + 1, false));
         }
         else
         {
             byte[] value = this.KvNodeGetValue();
             ret = RLP.EncodeList(RLP.EncodeElement(this.KvNodeGetKey().ToPacked()),
                                  RLP.EncodeElement(value ?? PatriciaTrie.EmptyByteArray));
         }
         if (this.Hash != null)
         {
             this.trie.TrieKvStore.Delete(this.Hash);
         }
         this.Dirty = false;
         if (ret.Length < 32 && !forceHash)
         {
             this.rlp = ret;
             return(ret);
         }
         else
         {
             this.Hash = this.trie.Hasher.Hash(ret);
             this.trie.TrieKvStore.Put(this.Hash, ret);
             return(RLP.EncodeElement(this.Hash));
         }
     }
 }
Ejemplo n.º 21
0
 public string Serialize()
 {
     return("0x" + AionUtils.ByteToHex(RLP.EncodeList(
                                           RLP.EncodeElement(Nonce.HexToByteArray()),
                                           RLP.EncodeElement(To.HexToByteArray()),
                                           RLP.EncodeElement(Value.HexToByteArray()),
                                           RLP.EncodeElement(Data.HexToByteArray()),
                                           RLP.EncodeElement(Timestamp.HexToByteArray()),
                                           CustomRLP.EncodeLong(Gas),
                                           CustomRLP.EncodeLong(GasPrice),
                                           RLP.EncodeElement(Type.HexToByteArray()),
                                           RLP.EncodeElement(Signature)
                                           )));
 }
Ejemplo n.º 22
0
        public void SanityTest()
        {
            // https://github.com/Nethereum/Nethereum/issues/510

            var item1 = new byte[] { 0x01 };
            var item2 = new byte[] { 0x01, 0x02 };

            byte[] encoded = RLP.EncodeList(RLP.EncodeElement(item1), RLP.EncodeElement(item2));

            RLPCollection decoded = (RLPCollection)RLP.Decode(encoded);

            // The actual list used to be at decoded[0]. Previously, these asserts would fail.
            Assert.Equal(item1, decoded[0].RLPData);
            Assert.Equal(item2, decoded[1].RLPData);
        }
Ejemplo n.º 23
0
 public byte[][] GetRlpDataElements()
 {
     return(new[]
     {
         RLP.EncodeByte((byte)chainTag),
         RLP.EncodeElement(blockRef.ToBigEndianBytes().TrimLeading()),
         RLP.EncodeElement(((long)expiration).ToBytesForRLPEncoding().TrimLeading()),
         RLP.EncodeList(clauses.Select(c => c.RLPData).ToArray()),
         RLP.EncodeByte(gasPriceCoef),
         RLP.EncodeElement(gas.ToBigEndianBytes().TrimLeading()),
         RLP.EncodeElement(dependsOn == "" ? null : dependsOn?.HexToByteArray().TrimLeading()),
         RLP.EncodeElement(nonce.ToBigEndianBytes().TrimLeading()),
         Reserved,
         RLP.EncodeElement(signature?.HexToByteArray())
     });
 }
Ejemplo n.º 24
0
        public byte[] Serialize <T>(T s) where T : struct
        {
            var toEncode = new List <byte[]>();

            foreach (FieldInfo field in s.GetType().GetFields())
            {
                object value = field.GetValue(s);

                byte[] serialized = value != null
                    ? this.primitiveSerializer.Serialize(value) ?? new byte[0]
                    : new byte[0];

                toEncode.Add(RLP.EncodeElement(serialized));
            }

            return(RLP.EncodeList(toEncode.ToArray()));
        }
Ejemplo n.º 25
0
        /// <summary>
        /// Serialize a receipt into bytes to be stored.
        /// </summary>
        public byte[] ToStorageBytesRlp()
        {
            IList <byte[]> encodedLogs = this.Logs.Select(x => RLP.EncodeElement(x.ToBytesRlp())).ToList();

            return(RLP.EncodeList(
                       RLP.EncodeElement(this.PostState.ToBytes()),
                       RLP.EncodeElement(BitConverter.GetBytes(this.GasUsed)),
                       RLP.EncodeElement(this.Bloom.ToBytes()),
                       RLP.EncodeElement(RLP.EncodeList(encodedLogs.ToArray())),
                       RLP.EncodeElement(this.TransactionHash.ToBytes()),
                       RLP.EncodeElement(this.BlockHash.ToBytes()),
                       RLP.EncodeElement(this.From.ToBytes()),
                       RLP.EncodeElement(this.To?.ToBytes()),
                       RLP.EncodeElement(this.NewContractAddress?.ToBytes()),
                       RLP.EncodeElement(BitConverter.GetBytes(this.Success))
                       ));
        }
Ejemplo n.º 26
0
        private byte[] EncodeRlpListTaskResult(params object[] list)
        {
            byte[][] vals = new byte[list.Length][];
            for (int i = 0; i < list.Length; i++)
            {
                if (list[i] is Task <byte[]> )
                {
                    vals[i] = ((Task <byte[]>)list[i]).Result;
                }
                else
                {
                    vals[i] = (byte[])list[i];
                }
            }

            return(RLP.EncodeList(vals));
        }
Ejemplo n.º 27
0
        private byte[] SerializeCreateContract(ContractTxData contractTxData)
        {
            var rlpBytes = new List <byte[]>();

            rlpBytes.Add(contractTxData.ContractExecutionCode);

            this.AddMethodParams(rlpBytes, contractTxData.MethodParameters);

            var encoded = RLP.EncodeList(rlpBytes.Select(RLP.EncodeElement).ToArray());

            var bytes = new byte[PrefixSize + encoded.Length];

            this.SerializePrefix(bytes, contractTxData);

            encoded.CopyTo(bytes, PrefixSize);

            return(bytes);
        }
        public byte[] Serialize(object[] methodParameters)
        {
            if (methodParameters == null)
            {
                throw new ArgumentNullException(nameof(methodParameters));
            }

            var result = new List <byte[]>();

            foreach (object param in methodParameters)
            {
                byte[] encoded = this.Encode(param);

                result.Add(encoded);
            }

            return(RLP.EncodeList(result.Select(RLP.EncodeElement).ToArray()));
        }
Ejemplo n.º 29
0
        /// <inheritdoc/>
        public byte[] EncodeRaw(Transaction transaction)
        {
            var rlpcollection = new List <byte[]>();

            var rlpInputs = new List <byte[]>();

            transaction.Inputs.ForEach(x => rlpInputs.Add(x.GetRLPEncoded()));
            rlpInputs.AddRange(Enumerable.Repeat(new TransactionInputData().GetRLPEncoded(), MAX_INPUTS - transaction.Inputs.Count));
            rlpcollection.Add(RLP.EncodeList(rlpInputs.ToArray()));

            var rlpOutputs = new List <byte[]>();

            transaction.Outputs.ForEach(x => rlpOutputs.Add(x.GetRLPEncoded()));
            rlpOutputs.AddRange(Enumerable.Repeat(new TransactionOutputData().GetRLPEncoded(), MAX_OUTPUTS - transaction.Outputs.Count));
            rlpcollection.Add(RLP.EncodeList(rlpOutputs.ToArray()));

            return(RLP.EncodeList(rlpcollection.ToArray()));
        }
Ejemplo n.º 30
0
        private byte[] Serialize(Array array)
        {
            // Edge case, serializing nonsensical
            if (array is byte[] a)
            {
                return(a);
            }

            List <byte[]> toEncode = new List <byte[]>();

            for (int i = 0; i < array.Length; i++)
            {
                object value      = array.GetValue(i);
                byte[] serialized = this.Serialize(value);
                toEncode.Add(RLP.EncodeElement(serialized));
            }

            return(RLP.EncodeList(toEncode.ToArray()));
        }