Ejemplo n.º 1
0
 public byte[] ToBytes()
 {
     return(RLP.EncodeList(
                RLP.EncodeElement(PublicKey.Buffer.ToArray()),
                RLP.EncodeElement(ThresholdSignaturePublicKey)
                ));
 }
Ejemplo n.º 2
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.º 3
0
        public void RlpEncode20000m()
        {
            var expected = "0x824e20".HexToByteArray();
            var actual   = RLP.EncodeElement(20000m.ToBigInteger().ToByteArrayUnsigned().TrimLeading());

            Assert.Equal(expected, actual);
        }
        /// <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.º 5
0
 public byte[] ToBytes()
 {
     return(RLP.EncodeList(
                RLP.EncodeElement(ContractAddress.ToBytes().ToArray()),
                RLP.EncodeElement(ByteCode)
                ));
 }
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])
                ));
 }
Ejemplo n.º 8
0
 public void ExecuteTransFinish()
 {
     foreach (TrieEntry entry in entry_list)
     {
         this.trie.Put(RLP.EncodeElement(entry.Key), entry.Data);
     }
     entry_list.Clear();
 }
Ejemplo n.º 9
0
        public AccountStateEntity GetAccount(byte[] key, byte[] root_hash)
        {
            Trie trie = new Trie(this, root_hash);

            byte[] value = trie.Get(RLP.EncodeElement(key));

            return(value.IsNotNullOrEmpty() ? AccountStateEntity.Parse(value) : null);
        }
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
        /// <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.º 12
0
        public void DeleteAccount(byte[] key)
        {
            if (!Execute())
            {
                return;
            }

            this.trie.Delete(RLP.EncodeElement(key));
        }
Ejemplo n.º 13
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.º 14
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.º 15
0
        private static byte[][] EncodeElementsBytes(params byte[][] bytes)
        {
            var encodeElements = new List <byte[]>();

            foreach (var byteElement in bytes)
            {
                encodeElements.Add(RLP.EncodeElement(byteElement));
            }
            return(encodeElements.ToArray());
        }
Ejemplo n.º 16
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.º 17
0
        private static void AssertIntEncoding(int test, string expected)
        {
            byte[] testBytes    = test.ToBytesForRLPEncoding();
            byte[] encoderesult = RLP.EncodeElement(testBytes);
            Assert.Equal(expected, encoderesult.ToHex());

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

            Assert.Equal(test, decodeResult.ToBigIntegerFromRLPDecoded());
        }
Ejemplo n.º 18
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.º 19
0
 static byte[] HexToByteArray(string value)
 {
     try
     {
         return(RLP.EncodeElement(HexToByteArrayInternal(value)));
     }
     catch (FormatException ex)
     {
         throw new FormatException(string.Format(
                                       "String '{0}' could not be converted to byte array (not hex?).", value), ex);
     }
 }
        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.º 21
0
        public void ShouldEncodeBigInteger()
        {
            BigInteger test     = "100102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f".HexToByteArray().ToBigIntegerFromRLPDecoded();
            string     expected = "a0100102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f";

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

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

            Assert.Equal(test, decodeResult.ToBigIntegerFromRLPDecoded());
        }
Ejemplo n.º 22
0
        public static byte[] EncodeLong(string number)
        {
            var numberWithoutX = number.StartsWith("0x") ? number.Substring(2) : number;
            var bigNumber      = BigInteger.Parse(numberWithoutX, NumberStyles.AllowHexSpecifier);
            var sizeThreshold  = BigInteger.Parse("00000000FFFFFFFF", NumberStyles.AllowHexSpecifier);

            if (bigNumber.CompareTo(sizeThreshold) < 0)
            {
                return(RLP.EncodeElement(number.HexToByteArray()));
            }

            return(RLP.EncodeElement(PadTo16Bytes(bigNumber.ToString("x")).HexToByteArray()));
        }
Ejemplo n.º 23
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.º 24
0
 private byte[] GetEncodedRaw()
 {
     return(CustomRLP.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())
                ));
 }
Ejemplo n.º 25
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.º 26
0
        public void ShouldEncodeEmptyString()
        {
            string test = "";

            byte[] testBytes = Encoding.UTF8.GetBytes(test);
            string expected  = "80";

            byte[] encoderesult = RLP.EncodeElement(testBytes);
            Assert.Equal(expected, encoderesult.ToHex());

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

            Assert.Equal(null, decodeResult);
        }
Ejemplo n.º 27
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.º 28
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.º 29
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.º 30
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()));
        }