static void Main(string[] args) { if (args.Length != 2) { Console.WriteLine("Invalid args - should only be the datakey and the file containing the data you want to put"); return; } SiaSkynet.SiaSkynetClient c = new SiaSkynetClient(); bool seedFileExists = File.Exists(@".\seed.txt"); string seed = ""; if (seedFileExists) { Console.WriteLine("seed.txt found, generating keys off it"); seed = System.IO.File.ReadAllText(@".\seed.txt"); } else { string generatedSeed = GetUniqueString(64); System.IO.File.WriteAllText(@".\seed.txt", generatedSeed); seed = generatedSeed; Console.WriteLine("seed.txt not found, generating a seed randomly and writing it to seed.txt"); } var keys = SiaSkynetClient.GenerateKeys(seed).Result; Console.WriteLine("Updating registry at " + args[0] + " with content in " + args[1]); string content = System.IO.File.ReadAllText(args[1]); bool r = c.SkyDbSet(keys.privateKey, keys.publicKey, args[0], content).Result; Console.WriteLine("Registry now contains:"); string actualContent = c.SkyDbGetAsString(keys.publicKey, args[0]).Result; Console.WriteLine(actualContent); }
private void InitKeys() { var key = SiaSkynetClient.GenerateKeys(seedPhrase); privateKey = key.privateKey; publicKey = key.publicKey; }
public async Task <MetaMaskLogin?> GetStoredhash(string address) { string encryptedStoredLogin = await GetEncryptedMetamaskHash(); if (!string.IsNullOrEmpty(encryptedStoredLogin)) { try { var key = SiaSkynetClient.GenerateKeys(address); var cipherData = Utils.HexStringToByteArray(encryptedStoredLogin); var decrypt = Utils.Decrypt(cipherData, key.privateKey); var dString = System.Text.Encoding.UTF8.GetString(decrypt); MetaMaskLogin?storedLogin = JsonSerializer.Deserialize <MetaMaskLogin>(dString); if (storedLogin?.address != address) { return(null); } return(storedLogin); } catch (Exception ex) { Console.WriteLine(ex); } } return(null); }
public void TestKeys() { var key = SiaSkynetClient.GenerateKeys(_testSeed); var key2 = SiaSkynetClient.GenerateKeys(_testSeed); Assert.IsNotNull(key.publicKey); }
/// <summary> /// Get document from SkyDB based on ID as key /// </summary> /// <param name="id"></param> /// <returns></returns> private async Task <Document?> GetDocument(DocumentSummary sum) { try { Error = null; Console.WriteLine("Loading document"); if (IsDfinityNetwork || sum.StorageSource == StorageSource.Dfinity) { string?json = await dfinityService.GetValue(sum.Id.ToString()); if (string.IsNullOrEmpty(json)) { return(new Document()); } var document = JsonSerializer.Deserialize <Document>(json) ?? new Document(); sum.Title = document.Title; sum.PreviewImage = document.PreviewImage; sum.CreatedDate = document.CreatedDate; sum.ModifiedDate = document.ModifiedDate; return(document); } else { var encryptedData = await client.SkyDbGet(sum.PublicKey, new RegistryKey(sum.Id.ToString()), TimeSpan.FromSeconds(10)); if (!encryptedData.HasValue) { return(new Document()); } else { //Decrypt data var key = SiaSkynetClient.GenerateKeys(sum.ContentSeed); var jsonBytes = Utils.Decrypt(encryptedData.Value.file, key.privateKey); var json = Encoding.UTF8.GetString(jsonBytes); var document = JsonSerializer.Deserialize <Document>(json) ?? new Document(); sum.Title = document.Title; sum.PreviewImage = document.PreviewImage; sum.CreatedDate = document.CreatedDate; sum.ModifiedDate = document.ModifiedDate; document.Revision = encryptedData.Value.registryEntry?.Revision ?? 0; return(document); } } } catch { Error = $"Unable to load document from {CurrentNetwork}. Please try again."; } return(null); }
/// <summary> /// Login with username/password /// </summary> /// <param name="username"></param> /// <param name="password"></param> public void Login(string username, string password) { string seedPhrase = $"{username}-{password}-{salt}"; var key = SiaSkynetClient.GenerateKeys(seedPhrase); privateKey = key.privateKey; publicKey = key.publicKey; IsLoggedIn = true; }
public async Task TestGetRegistry() { var key = SiaSkynetClient.GenerateKeys(_testSeed); RegistryKey dataKey = new RegistryKey("t3"); var result = await _client.GetRegistry(key.publicKey, dataKey); Assert.IsNotNull(result); Assert.AreEqual("IADUs8d9CQjUO34LmdaaNPK_STuZo24rpKVfYW3wPPM2uQ", result.GetDataAsString()); }
public async Task TestSkyDbUpdateSimple() { string newData = Guid.NewGuid().ToString(); var key = SiaSkynetClient.GenerateKeys("my private key seed"); RegistryKey dataKey = new RegistryKey("datakey"); var success = await _client.SkyDbSetAsString(key.privateKey, key.publicKey, dataKey, newData); string result = await _client.SkyDbGetAsString(key.publicKey, dataKey); Assert.IsTrue(success); Assert.AreEqual(newData, result); }
public async Task TestSetRegistry() { string dataKey = Guid.NewGuid().ToString(); int revision = 0; string data = "IADUs8d9CQjUO34LmdaaNPK_STuZo24rpKVfYW3wPPM2uQ"; //Sia logo var key = SiaSkynetClient.GenerateKeys(_testSeed); RegistryEntry reg = new RegistryEntry(new RegistryKey(dataKey)); reg.SetData(data); reg.Revision = revision; var success = await _client.SetRegistry(key.privateKey, key.publicKey, reg); Assert.IsTrue(success); }
public async Task SaveStoredHash(MetaMaskLogin storedLogin) { try { var key = SiaSkynetClient.GenerateKeys(storedLogin.address); string jsonString = JsonSerializer.Serialize(storedLogin); var encrypted = Utils.Encrypt(System.Text.Encoding.UTF8.GetBytes(jsonString), key.privateKey); var hexString = BitConverter.ToString(encrypted).Replace("-", ""); await localStorageService.SetItemAsStringAsync(MetaMaskLocalStorageKey, hexString); } catch (Exception ex) { Console.WriteLine(ex); } }
public void BasicEncryptionTest() { var key = SiaSkynetClient.GenerateKeys("test"); var key2 = SiaSkynetClient.GenerateKeys("bar"); var testValue = "this is not encrypted"; var encrypted = Utils.Encrypt(System.Text.Encoding.UTF8.GetBytes(testValue), key.privateKey); var eString = System.Text.Encoding.UTF8.GetString(encrypted); Assert.AreNotEqual(testValue, eString); var decrypt = Utils.Decrypt(encrypted, key.privateKey); var dString = System.Text.Encoding.UTF8.GetString(decrypt); Assert.AreEqual(testValue, dString); }
public async Task TestForcedRevision() { string data = Guid.NewGuid().ToString(); var key = SiaSkynetClient.GenerateKeys("my private key seed"); RegistryKey dataKey = new RegistryKey("datakey" + Guid.NewGuid()); int revision = 5; var success = await _client.SkyDbSet(key.privateKey, key.publicKey, dataKey, Encoding.UTF8.GetBytes(data), revision); var result = await _client.SkyDbGet(key.publicKey, dataKey); Assert.IsTrue(success); Assert.AreEqual(revision, result.Value.registryEntry.Revision); var stringResult = Encoding.UTF8.GetString(result.Value.file); Assert.AreEqual(data, stringResult); }
public async Task <string> StoreMessage([FromBody] AddMessageRequest req) { string address = req.Address.ToLowerInvariant(); string seed = $"{secretConfig.SkynetSeed}-{address}"; var key = SiaSkynetClient.GenerateKeys(seed); var encrypted = Utils.Encrypt(System.Text.Encoding.UTF8.GetBytes(req.Message), key.privateKey); using (Stream stream = new MemoryStream(encrypted)) { //Save data to Skynet file var response = await client.UploadFileAsync("secret.dat", stream); var link = response.Skylink; return(link); } }
public async Task TestRegistryUpdate() { RegistryKey dataKey = new RegistryKey("regtest"); var key = SiaSkynetClient.GenerateKeys(_testSeed); var success = await _client.UpdateRegistry(key.privateKey, key.publicKey, dataKey, "update1"); await Task.Delay(TimeSpan.FromSeconds(5)); var success2 = await _client.UpdateRegistry(key.privateKey, key.publicKey, dataKey, "update2"); await Task.Delay(TimeSpan.FromSeconds(5)); var result = await _client.GetRegistry(key.publicKey, dataKey); Assert.IsTrue(success); Assert.IsTrue(success2); Assert.AreEqual("update2", result.GetDataAsString()); }
public async Task TestSkyDbUpdate() { RegistryKey dataKey = new RegistryKey("skydbtest-" + Guid.NewGuid()); var key = SiaSkynetClient.GenerateKeys(_testSeed); var success = await _client.SkyDbSetAsString(key.privateKey, key.publicKey, dataKey, "update1"); Assert.IsTrue(success); await Task.Delay(TimeSpan.FromSeconds(5)); var success2 = await _client.SkyDbSetAsString(key.privateKey, key.publicKey, dataKey, "update2"); await Task.Delay(TimeSpan.FromSeconds(5)); string result = await _client.SkyDbGetAsString(key.publicKey, dataKey); Assert.IsTrue(success); Assert.IsTrue(success2); Assert.AreEqual("update2", result); }
static void Main(string[] args) { Console.WriteLine("Hello World!"); var sky = new SiaSkynetClient(); //var upload = sky.UploadFileAsync("next.tar", File.OpenRead("next.tar")); //var skyResult = upload.GetAwaiter().GetResult(); //Console.WriteLine(skyResult.Skylink); //Console.WriteLine(skyResult.Merkleroot); //Console.WriteLine(skyResult.Bitfield); //var download = sky.DownloadFileAsStringAsync("AAC0uO43g64ULpyrW0zO3bjEknSFbAhm8c-RFP21EQlmSQ"); //Console.WriteLine(download.Result.file); var key = SiaSkynetClient.GenerateKeys("milkey"); var dbGet = sky.SkyDbGetAsString(key.publicKey, "blazor-sample"); Console.WriteLine(dbGet.Result); var dbSet = sky.SkyDbSet(key.privateKey, key.publicKey, "blazor-sample", "hello"); Console.WriteLine(dbSet.Result); }
/// <summary> /// Save document to SkyDB /// </summary> /// <param name="doc"></param> /// <returns></returns> private async Task <bool> SaveDocument(Document doc, DocumentSummary sum) { //Only allowed to save if you have a private key for this document if (sum.PrivateKey == null) { return(false); } var json = JsonSerializer.Serialize(doc); bool success = false; try { if (IsDfinityNetwork || sum.StorageSource == StorageSource.Dfinity) { await dfinityService.SetValue(doc.Id.ToString(), json); success = true; } else { //Encrypt with ContentSeed var key = SiaSkynetClient.GenerateKeys(sum.ContentSeed); var data = Encoding.UTF8.GetBytes(json); var encryptedData = Utils.Encrypt(data, key.privateKey); success = await client.SkyDbSet(sum.PrivateKey, sum.PublicKey, new RegistryKey(doc.Id.ToString()), encryptedData, doc.Revision + 1); } } catch (Exception ex) { Console.WriteLine(ex); success = false; } return(success); }
public CmsItemStorageService(IOptions <SkynetConfig> skynetConfig, IOptions <CmsConfiguration> cmsConfiguration) { this.skynetConfig = skynetConfig.Value; this.cmsConfiguration = cmsConfiguration.Value; if (!string.IsNullOrEmpty(skynetConfig.Value.BaseUrl)) { _client = new SiaSkynetClient(skynetConfig.Value.BaseUrl); } else { _client = new SiaSkynetClient(); } if (string.IsNullOrWhiteSpace(this.skynetConfig.Secret)) { throw new ArgumentNullException("SkynetConfig.Seed should contain a seed value to generate a private/public key pair.", nameof(skynetConfig)); } var keypair = SiaSkynetClient.GenerateKeys(this.skynetConfig.Secret); privateKey = keypair.privateKey; publicKey = keypair.publicKey; }
public async Task TestSetRegistry_With_DiscoverableBucket() { var path = $"crqa.hns/snew.hns/newcontent/{Guid.NewGuid().ToString()}/index.json"; var bucket = new DiscoverableBucket(path); var dataKey = new RegistryKey(bucket); int revision = 0; string data = "IADUs8d9CQjUO34LmdaaNPK_STuZo24rpKVfYW3wPPM2uQ"; //Sia logo var key = SiaSkynetClient.GenerateKeys(_testSeed); RegistryEntry reg = new RegistryEntry(dataKey); reg.SetData(data); reg.Revision = revision; var success = await _client.SetRegistry(key.privateKey, key.publicKey, reg); Assert.IsTrue(success); var result = await _client.GetRegistry(key.publicKey, dataKey); Assert.AreEqual(data, result.GetDataAsString()); }
public async Task <string> GetMessage([FromBody] GetMessageRequest req) { //Check hash var hashKey = SiaSkynetClient.GenerateKeys(req.Address); var cipherData = Utils.HexStringToByteArray(req.SecretHash); var decrypt = Utils.Decrypt(cipherData, hashKey.privateKey); if (decrypt == null) { throw new Exception("Invalid hash."); } string address = req.Address.ToLowerInvariant(); string seed = $"{secretConfig.SkynetSeed}-{address}"; var key = SiaSkynetClient.GenerateKeys(seed); var encryptedData = await client.DownloadFileAsByteArrayAsync(req.Skylink); var data = Utils.Decrypt(encryptedData.file, key.privateKey); return(Encoding.UTF8.GetString(data)); }
/// <summary> /// Save current document /// </summary> /// <param name="fallbackTitle"></param> /// <returns></returns> public async Task SaveCurrentDocument(string fallbackTitle, byte[] img) { Error = null; if (CurrentDocument != null) { var sum = CurrentSum; if (sum != null && sum.PrivateKey == null) { DocumentList.Remove(sum); sum = null; CurrentDocument.Id = Guid.NewGuid(); } if (sum == null) { string contentSeed = Guid.NewGuid().ToString(); string fileSeed = Guid.NewGuid().ToString(); string seedPhrase = $"{fileSeed}-{salt}"; var key = SiaSkynetClient.GenerateKeys(seedPhrase); sum = new DocumentSummary() { Id = CurrentDocument.Id, Title = CurrentDocument.Title, CreatedDate = DateTimeOffset.UtcNow, ModifiedDate = DateTimeOffset.UtcNow, ContentSeed = contentSeed, PrivateKey = key.privateKey, PublicKey = key.publicKey, StorageSource = this.IsDfinityLogin ? StorageSource.Dfinity : StorageSource.Skynet }; DocumentList.Add(sum); } if (sum.PrivateKey == null) { } //Fix title if there is no title var title = CurrentDocument.Title; if (string.IsNullOrWhiteSpace(title)) { title = fallbackTitle; } if (string.IsNullOrWhiteSpace(title)) { title = "Untitled document " + sum.CreatedDate; } CurrentDocument.Title = title; sum.Title = title; CurrentDocument.ModifiedDate = DateTimeOffset.UtcNow; sum.ModifiedDate = DateTimeOffset.UtcNow; //Save image string?imgLink = null; try { imgLink = await SaveDocumentImage(img); sum.PreviewImage = imgLink; CurrentDocument.PreviewImage = imgLink; } catch { } //Save document bool success = await SaveDocument(CurrentDocument, sum); if (success) { Console.WriteLine("Document saved"); //Save updated document list await SaveDocumentList(DocumentList); Console.WriteLine("Document list saved"); } if (!success) { Error = "Error saving document. Please try again"; } else { CurrentDocument = null; } } }