예제 #1
0
        public void Sign(byte[] privKey, byte[] pubKey)
        {
            byte[] bufferToSign = this.GetBufferToSign();
            var    signature    = PostchainUtil.Sign(bufferToSign, privKey);

            this.AddSignature(pubKey, signature);
        }
예제 #2
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);
        }
예제 #3
0
 private BinaryTreeElement InnerHandleLeaf(object leaf, PathSet paths)
 {
     if (leaf is null)
     {
         return(this.HandlePrimitiveLeaf(leaf, paths));
     }
     else if (leaf is byte[])
     {
         return(this.HandlePrimitiveLeaf(leaf, paths));
     }
     else if (leaf is string)
     {
         return(this.HandlePrimitiveLeaf(leaf, paths));
     }
     else if (PostchainUtil.IsNumericType(leaf))
     {
         return(this.HandlePrimitiveLeaf(leaf, paths));
     }
     else if (leaf is object[])
     {
         return(this.BuildFromArray((object[])leaf, paths));
     }
     else if (leaf is Dictionary <string, object> )
     {
         return(this.BuildFromDictionary((Dictionary <string, object>)leaf, paths));
     }
     else
     {
         throw new System.Exception("Unsupporting data type: " + leaf.GetType());
     }
 }
예제 #4
0
        private object[] GetGtvTxBody()
        {
            var body = new List <object>();

            body.Add(PostchainUtil.HexStringToBuffer(this.BlockchainID));
            body.Add((from Operation op in this.Operations select op.Raw()).ToArray());
            body.Add(this.Signers.ToArray());

            return(body.ToArray());
        }
        /**
         * @param content to sign. It will be digested before signing.
         * @param privKey The private key to sign the content with
         *
         * @return the signature
         */
        public static byte[] Sign(byte[] content, byte[] privKey)
        {
            if (privKey == null)
            {
                throw new Exception("Programmer error, missing privKey");
            }

            if (privKey.Length != 32)
            {
                throw new Exception("Programmer error. Invalid key length. Expected 32, but got " + privKey.Length);
            }

            return(PostchainUtil.SignDigest(content, privKey));
        }
예제 #6
0
        public override string ToString()
        {
            switch (Choice)
            {
            case (GTXValueChoice.Null):
            {
                return("null");
            }

            case (GTXValueChoice.ByteArray):
            {
                return("0x" + PostchainUtil.ByteArrayToString(ByteArray));
            }

            case (GTXValueChoice.String):
            {
                return(String);
            }

            case (GTXValueChoice.Integer):
            {
                return(Integer.ToString());
            }

            case (GTXValueChoice.Array):
            {
                string ret = "[";
                if (Array.Count == 0)
                {
                    return(ret + "]");
                }

                foreach (var elm in Array)
                {
                    ret += elm.ToString() + ", ";
                }

                return(ret.Remove(ret.Length - 2) + "]");
            }

            case (GTXValueChoice.Dict):
            {
                string ret = "[";
                if (Dict.Count == 0)
                {
                    return(ret + "]");
                }

                foreach (var elm in Dict)
                {
                    ret += @"{{""" + elm.Name + @""": " + elm.Value.ToString() + "}, ";
                }

                return(ret.Remove(ret.Length - 2) + "]");
            }

            default:
            {
                return("");
            }
            }
        }
예제 #7
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);
        }
예제 #8
0
 public string Serialize()
 {
     return(PostchainUtil.ByteArrayToString(Encode()));
 }
 private string GetTxRID()
 {
     return(PostchainUtil.ByteArrayToString(this.GetBufferToSign()));
 }