Exemplo n.º 1
0
        public static Gtx Decode(byte[] encodedMessage)
        {
            var gtx            = new Gtx();
            var gtxTransaction = new ASN1.AsnReader(encodedMessage);
            var gtxValue       = GTXValue.Decode(gtxTransaction);
            var gtxPayload     = gtxValue.Array[0];

            gtx.BlockchainID = PostchainUtil.ByteArrayToString(gtxPayload.Array[0].ByteArray);

            foreach (var opArr in gtxPayload.Array[1].Array)
            {
                var op = opArr.ToObjectArray();

                var opName = opArr.Array[0].String;
                var opArgs = opArr.Array[1].ToObjectArray();
                gtx.AddOperationToGtx(opName, opArgs);
            }

            foreach (var signer in gtxPayload.Array[2].Array)
            {
                gtx.AddSignerToGtx(signer.ByteArray);
            }

            foreach (var sig in gtxValue.Array[1].Array)
            {
                gtx.Signatures.Add(sig.ByteArray);
            }

            return(gtx);
        }
Exemplo n.º 2
0
        private byte[] CalculateHashOfValueInternal(GTXValue gtxValue)
        {
            var buf = new List <byte>()
            {
                (byte)HashPrefix.Leaf
            };

            buf.AddRange(gtxValue.Encode());

            return(HashingFun(buf.ToArray()));
        }
Exemplo n.º 3
0
        public GTXValue ToGtxValue()
        {
            var gtxValue = new GTXValue();

            gtxValue.Choice = GTXValueChoice.Array;
            gtxValue.Array  = new List <GTXValue>()
            {
                Gtx.ArgToGTXValue(this.OpName)
            };
            gtxValue.Array.AddRange(this.Args);

            return(gtxValue);
        }
Exemplo n.º 4
0
        public override bool Equals(object obj)
        {
            if ((obj == null) || !this.GetType().Equals(obj.GetType()))
            {
                return(false);
            }
            else
            {
                GTXValue gtxValue = (GTXValue)obj;

                return(this.Choice.Equals(gtxValue.Choice) &&
                       ((this.ByteArray == null || gtxValue.ByteArray == null) ? this.ByteArray == gtxValue.ByteArray : Enumerable.SequenceEqual(this.ByteArray, gtxValue.ByteArray)) &&
                       ((this.String == null || gtxValue.String == null) ? this.String == gtxValue.String : this.String.Equals(gtxValue.String)) &&
                       this.Integer.Equals(gtxValue.Integer) &&
                       ((this.Dict == null || gtxValue.Dict == null) ? this.Dict == gtxValue.Dict : Enumerable.SequenceEqual(this.Dict, gtxValue.Dict)) &&
                       ((this.Array == null || gtxValue.Array == null) ? this.Array == gtxValue.Array : Enumerable.SequenceEqual(this.Array, gtxValue.Array)));
            }
        }
Exemplo n.º 5
0
        public static GTXValue Decode(ASN1.AsnReader sequence)
        {
            var val = new GTXValue();

            byte choice = sequence.ReadChoice();

            sequence.ReadLength();
            switch (choice)
            {
            case ASN1.AsnUtil.TAG_NULL:
            {
                val.Choice = GTXValueChoice.Null;
                sequence.ReadChoice();
                sequence.ReadChoice();
                break;
            }

            case ASN1.AsnUtil.TAG_BYTE_ARRAY:
            {
                val.Choice    = GTXValueChoice.ByteArray;
                val.ByteArray = sequence.ReadOctetString();
                break;
            }

            case ASN1.AsnUtil.TAG_STRING:
            {
                val.Choice = GTXValueChoice.String;
                val.String = sequence.ReadUTF8String();
                break;
            }

            case ASN1.AsnUtil.TAG_INTEGER:
            {
                val.Choice  = GTXValueChoice.Integer;
                val.Integer = sequence.ReadInteger();
                break;
            }

            case ASN1.AsnUtil.TAG_ARRAY:
            {
                val.Choice = GTXValueChoice.Array;
                val.Array  = new List <GTXValue>();
                var innerSequence = sequence.ReadSequence();
                while (innerSequence.RemainingBytes > 0)
                {
                    val.Array.Add(GTXValue.Decode(innerSequence));
                }
                break;
            }

            case ASN1.AsnUtil.TAG_DICT:
            {
                val.Choice = GTXValueChoice.Dict;
                val.Dict   = new List <DictPair>();
                var innerSequence = sequence.ReadSequence();
                while (innerSequence.RemainingBytes > 0)
                {
                    val.Dict.Add(DictPair.Decode(innerSequence));
                }
                break;
            }

            default:
            {
                throw new System.Exception("Unknown choice tag: " + choice.ToString("X2"));
            }
            }

            return(val);
        }
Exemplo n.º 6
0
        public static GTXValue ArgToGTXValue(object arg)
        {
            var gtxValue = new GTXValue();

            if (arg is null)
            {
                gtxValue.Choice = GTXValueChoice.Null;
            }
            else if (PostchainUtil.IsNumericType(arg))
            {
                try
                {
                    gtxValue.Choice  = GTXValueChoice.Integer;
                    gtxValue.Integer = Convert.ToInt64(arg);
                }
                catch
                {
                    throw new System.Exception("Chromia.PostchainClient.GTX Gtx.ArgToGTXValue() Integer overflow.");
                }
            }
            else if (arg is byte[])
            {
                gtxValue.Choice    = GTXValueChoice.ByteArray;
                gtxValue.ByteArray = (byte[])arg;
            }
            else if (arg is string)
            {
                gtxValue.Choice = GTXValueChoice.String;
                gtxValue.String = (string)arg;
            }
            else if (arg is object[])
            {
                var array = (object[])arg;
                gtxValue.Choice = GTXValueChoice.Array;

                gtxValue.Array = new List <GTXValue>();
                foreach (var subArg in array)
                {
                    gtxValue.Array.Add(ArgToGTXValue((object)subArg));
                }
            }
            else if (arg is Dictionary <string, object> )
            {
                gtxValue.Choice = GTXValueChoice.Dict;

                var dict = (Dictionary <string, object>)arg;

                gtxValue.Dict = new List <DictPair>();
                foreach (var dictPair in dict)
                {
                    gtxValue.Dict.Add(new DictPair(dictPair.Key, ArgToGTXValue(dictPair.Value)));
                }
            }
            else if (arg is Operation)
            {
                return(((Operation)arg).ToGtxValue());
            }
            else
            {
                throw new System.Exception("Chromia.PostchainClient.GTX Gtx.ArgToGTXValue() Can't create GTXValue out of type " + arg.GetType());
            }


            return(gtxValue);
        }