/// <summary>
        /// 从指定的json对象中解析出签名上下文
        /// </summary>
        /// <param name="json">json对象</param>
        /// <returns>返回上下文</returns>
        public static SignatureContext FromJson(JObject json)
        {
            ISignable signable = typeof(SignatureContext).GetTypeInfo().Assembly.CreateInstance(json["type"].AsString()) as ISignable;

            using (MemoryStream ms = new MemoryStream(json["hex"].AsString().HexToBytes(), false))
                using (BinaryReader reader = new BinaryReader(ms, Encoding.UTF8))
                {
                    signable.DeserializeUnsigned(reader);
                }
            SignatureContext context = new SignatureContext(signable);
            JArray           scripts = (JArray)json["scripts"];

            for (int i = 0; i < scripts.Count; i++)
            {
                if (scripts[i] != null)
                {
                    context.redeemScripts[i] = scripts[i]["redeem_script"].AsString().HexToBytes();
                    JArray ps = (JArray)scripts[i]["parameters"];
                    context.parameters[i] = new byte[ps.Count][];
                    for (int j = 0; j < ps.Count; j++)
                    {
                        context.parameters[i][j] = ps[j]?.AsString().HexToBytes();
                    }
                    context.temp[i] = scripts[i]["temp"];
                }
            }
            return(context);
        }
Beispiel #2
0
        public static SignatureContext Parse(string value)
        {
            JObject   json     = JObject.Parse(value);
            string    typename = string.Format("{0}.{1}", typeof(SignatureContext).Namespace, json["type"].AsString());
            ISignable signable = Assembly.GetExecutingAssembly().CreateInstance(typename) as ISignable;

            using (MemoryStream ms = new MemoryStream(json["hex"].AsString().HexToBytes(), false))
                using (BinaryReader reader = new BinaryReader(ms, Encoding.UTF8))
                {
                    signable.DeserializeUnsigned(reader);
                }
            SignatureContext context = new SignatureContext(signable);
            JArray           scripts = (JArray)json["scripts"];

            for (int i = 0; i < scripts.Count; i++)
            {
                if (scripts[i] != null)
                {
                    context.redeemScripts[i] = scripts[i]["redeem_script"].AsString().HexToBytes();
                    context.signatures[i]    = new Dictionary <ECPoint, byte[]>();
                    JArray sigs = (JArray)scripts[i]["signatures"];
                    for (int j = 0; j < sigs.Count; j++)
                    {
                        ECPoint pubkey    = ECPoint.DecodePoint(sigs[j]["pubkey"].AsString().HexToBytes(), ECCurve.Secp256r1);
                        byte[]  signature = sigs[j]["signature"].AsString().HexToBytes();
                        context.signatures[i].Add(pubkey, signature);
                    }
                }
            }
            return(context);
        }