Exemplo n.º 1
0
        /// <summary>
        /// Loads identity from folder.
        /// </summary>
        private static async Task <PrivateIdentity> LoadIdentity(string folder)
        {
            var file = Path.Combine(folder, "node.identity");

            // If file doesn't exist, create and save a new identity.
            if (!File.Exists(file))
            {
                var identity = PrivateIdentity.Create();
                var bytes    = Transcoder.HexFromBytes(PrivateIdentity.Export(identity));
                await File.WriteAllTextAsync(file, bytes);

                Console.WriteLine($"No identity found at {file}, so identity {identity.ID} was created.");
                return(identity);
            }

            // Otherwise, attempt to load the identity from the file.
            var hex = await File.ReadAllTextAsync(file);

            var importedIdentity = PrivateIdentity.Import(Transcoder.BytesFromHex(hex));

            if (importedIdentity.IsError)
            {
                Console.WriteLine($"Failed to load identity at {file}.");
                throw new Exception(importedIdentity.Error.GetType().ToString());
            }
            Console.WriteLine($"Found identity {importedIdentity.Value.ID} at {file}, loaded successfully.");
            return(importedIdentity.Value);
        }
Exemplo n.º 2
0
        public async void OnConnect(string folder, IPEndPoint endpoint)
        {
            var             file = Path.Combine(folder, "client.identity");
            PrivateIdentity identity;

            if (!Env.CheckFile(file))
            {
                if (!Env.Confirm($"No identity was found at {file}. Do you want to create a new one?"))
                {
                    return;
                }
                identity = PrivateIdentity.Create();
                var hex = Transcoder.HexFromBytes(PrivateIdentity.Export(identity));
                await Env.WriteFile(file, hex);
            }
            else
            {
                var hex    = Transcoder.BytesFromHex(await Env.ReadFile(file));
                var result = PrivateIdentity.Import(hex);
                if (result.IsError)
                {
                    Env.Alert("Could not load identity!!");
                    return;
                }
                identity = result.Value;
            }

            APITranslatorClient nodeConnection;

            try
            {
                nodeConnection = await APITranslatorClient.CreateAndConnect(new APITranslatorClient.ConnectionArgs
                {
                    Self     = identity,
                    ServerID = identity.ID,
                    Address  = endpoint.Address,
                    Port     = endpoint.Port
                });
            }
            catch
            {
                Env.Alert("Unable to connect to your node!");
                return;
            }

            Resources = new ResourceManager(identity, nodeConnection, OnDisconnect);
            VM        = new ConnectedViewModel(OnDisconnect);
        }
Exemplo n.º 3
0
        public void TestImportExport()
        {
            var newIdentity = PrivateIdentity.Import(PrivateIdentity.Export(privateIdentity)).Value;

            Assert.AreEqual(privateIdentity.ID, newIdentity.ID);
        }