void LoadCore(Stream stream) { JObject obj = JObject.Load(new JsonTextReader(new StreamReader(stream)) { DateParseHandling = DateParseHandling.DateTimeOffset }); _Parameters = WalletCreation.FromJson((JObject)obj["Parameters"]); _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); } } }
static internal WalletCreation FromJson(JObject obj) { WalletCreation creation = new WalletCreation(); creation.Network = null; JToken unused; if (obj.TryGetValue("Name", out unused)) { creation.Name = (string)obj["Name"]; } else { creation.Name = null; } if (obj.Property("PurgeConnectionOnFilterChange") != null) { creation.PurgeConnectionOnFilterChange = (bool)obj["PurgeConnectionOnFilterChange"]; } JToken network; if (obj.TryGetValue("Network", out network)) { creation.Network = Network.GetNetwork((string)network); } creation.SignatureRequired = (int)(long)obj["SignatureRequired"]; creation.DerivationPath = KeyPath.Parse((string)obj["DerivationPath"]); creation.UseP2SH = (bool)obj["UseP2SH"]; var array = (JArray)obj["RootKeys"]; var keys = array.Select(i => new BitcoinExtPubKey((string)i)).ToArray(); creation.Network = creation.Network ?? keys[0].Network; creation.RootKeys = keys.Select(k => k.ExtPubKey).ToArray(); return(creation); }
/// <summary> /// Create a new wallet /// </summary> /// <param name="creation">Creation parameters</param> /// <param name="keyPoolSize">The number of keys which will be pre-created</param> public Wallet(WalletCreation creation, int keyPoolSize = 500) { if (creation == null) { throw new ArgumentNullException("creation"); } _Parameters = creation; _ScanLocation = new BlockLocator(); _ScanLocation.Blocks.Add(creation.Network.GetGenesis().GetHash()); _KeyPoolSize = keyPoolSize; Created = DateTimeOffset.UtcNow; }