Example #1
0
        void LoadCore(Stream stream)
        {
            JObject obj = JObject.Load(new JsonTextReader(new StreamReader(stream))
            {
                DateParseHandling = DateParseHandling.DateTimeOffset
            });

#pragma warning disable CS0612                        // Type or member is obsolete
            _Parameters = WalletCreation.FromJson((JObject)obj["Parameters"]);
#pragma warning restore CS0612                        // Type or member is obsolete
            _PathStates = new Dictionary <KeyPath, PathState>();
            if (obj.Property("CurrentIndex") != null) //legacy
            {
                var idx        = (int)(long)obj["CurrentIndex"];
                var loadedKeys = (int)(long)obj["LoadedKeys"];
                _PathStates.Add(_Parameters.DerivationPath.Derive(0), new PathState()
                {
                    Next   = idx,
                    Loaded = loadedKeys
                });
                _PathStates.Add(_Parameters.DerivationPath.Derive(1), new PathState()
                {
                    Next   = idx,
                    Loaded = loadedKeys
                });
            }

            var indices = obj["Indices"] as JArray;
            if (indices != null)
            {
                foreach (var indice in indices.OfType <JObject>())
                {
                    _PathStates.Add(KeyPath.Parse((string)indice["KeyPath"]), new PathState()
                    {
                        Next   = (int)(long)indice["Next"],
                        Loaded = (int)(long)indice["Loaded"]
                    });
                }
            }
            _KeyPoolSize  = (int)(long)obj["KeyPoolSize"];
            Created       = (DateTimeOffset)obj["Created"];
            _ScanLocation = new BlockLocator();
            _ScanLocation.FromBytes(Encoders.Hex.DecodeData((string)obj["Location"]));
            _KnownScripts.Clear();
            var knownScripts = (JArray)obj["KnownScripts"];
            foreach (var known in knownScripts.OfType <JObject>())
            {
                Script script = Script.FromBytesUnsafe(Encoders.Hex.DecodeData((string)known["ScriptPubKey"]));
                if (known["KeyPath"] != null)                //Legacy data
                {
                    KeyPath keypath = KeyPath.Parse((string)known["KeyPath"]);
                    _KnownScripts.Add(script, _Parameters.DerivationPath.Derive(keypath));
                }
                if (known["AbsoluteKeyPath"] != null)
                {
                    KeyPath keypath = KeyPath.Parse((string)known["AbsoluteKeyPath"]);
                    _KnownScripts.Add(script, keypath);
                }
            }
        }