Exemplo n.º 1
0
Arquivo: RLP.cs Projeto: zutobg/Meadow
        private static byte[] EncodeByteArray(RLPByteArray rlpBytes)
        {
            // If it's null
            if (rlpBytes.Data.Length == 0)
            {
                return(new byte[] { 0x80 });
            }

            // If it's a single byte less than/equal to 128, the encoding is it's own value.
            if (rlpBytes.Data.Length == 1 && rlpBytes.Data.Span[0] <= 0x7f)
            {
                return(rlpBytes.Data.ToArray());
            }

            // If it's between 0 and 55 bytes, we add a prefix that denotes length.
            if (rlpBytes.Data.Length < 55)
            {
                return(new byte[] { (byte)(0x80 + rlpBytes.Data.Length) }.Concat(rlpBytes.Data.ToArray()));
            }

            // If it's more, we'll want to get the length of the data (as a possibly large integer)
            int length = rlpBytes.Data.Length;

            byte[] lengthData = EncodeLength(length);

            // Return an array with our data
            return(new[] { (byte)(0xb7 + lengthData.Length) }.Concat(lengthData, rlpBytes.Data.ToArray()));
        }
Exemplo n.º 2
0
Arquivo: RLP.cs Projeto: zutobg/Meadow
        public static BigInteger ToInteger(RLPByteArray rlpByteArray, int byteCount = 32, bool signed = false)
        {
            // If our data is null or empty, the result is 0.
            if (rlpByteArray.Data.Length == 0)
            {
                return(0);
            }

            // Obtain our integer
            return(BigIntegerConverter.GetBigInteger(rlpByteArray.Data.Span, signed, byteCount));
        }