internal static DocumentSummary ParseDocumentationCommentXml(string xml) { var result = new DocumentSummary(); // Currently, XmlReader is the fastest way to parse XML in C#. using (var reader = XmlReader.Create(new StringReader(xml))) { while (reader.Read()) { if (reader.NodeType == XmlNodeType.Element) { var elementName = reader.Name; if (elementName == "summary") { result.HasSummary = true; } else if (elementName == "typeparam") { result.TypeParameterNames.Add(reader.GetAttribute("name")); } else if (elementName == "param") { result.ParameterNames.Add(reader.GetAttribute("name")); } else if (elementName == "returns") { result.HasReturns = true; } } } } return result; }
public async Task <string?> StoreShareMessage(string ethAddress, DocumentSummary currentSum) { string url = baseUrl + "/share/add"; ShareModel shareMsg = new ShareModel() { Sum = currentSum }; string json = JsonSerializer.Serialize(shareMsg); AddMessageRequest reqModel = new AddMessageRequest() { Address = ethAddress, Message = json }; HttpClient client = new HttpClient(); var result = await client.PostAsJsonAsync(url, reqModel); if (result.IsSuccessStatusCode) { var skylink = await result.Content.ReadAsStringAsync(); return("sia://" + skylink); } return(null); }
/// <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); }
private TreeNode BuildDocumentTreeNode(DocumentSummary documentSummary) { var treeNodes = new List <TreeNode>(); PopulateTreeNodeChildrenWithObjectProperties(documentSummary, treeNodes); var documentNode = new TreeNode($"{documentSummary.MatterReference}: {documentSummary.Title}", treeNodes.ToArray()) { Tag = documentSummary }; return(documentNode); }
public void HashCode_Is_HashCode_Of_Uid() { // arrange var summary = new DocumentSummary { Uid = nameof(DocSummaryTests) }; // act var expected = summary.Uid.GetHashCode(); var actual = summary.GetHashCode(); // assert Assert.Equal(expected, actual); }
public void Equality_Based_On_Type_And_Id( string uid, object target, bool areEqual) { // arrange var summary = new DocumentSummary { Uid = uid }; // act var equals = summary.Equals(target); // assert Assert.Equal(areEqual, equals); }
public void Author_ToString_Includes_Alias_And_Docs_Count( string alias, int expectedDocs) { // arrange var author = new Author { Alias = alias }; for (var idx = 0; idx < expectedDocs; idx++) { var summary = new DocumentSummary { AuthorAlias = alias, Title = Guid.NewGuid().ToString(), }; summary.Uid = summary.Title.Split('-')[^ 1];
public void ToString_Includes_Uid_Alias_And_Title() { // arrange var summary = new DocumentSummary { Uid = nameof(EqualityTests), Title = "Something great", AuthorAlias = "test" }; // act var str = summary.ToString(); // assert Assert.Contains(summary.Uid, str); Assert.Contains(summary.Title, str); Assert.Contains(summary.AuthorAlias, str); }
public void Properties_Match_Doc( Func <Document, string> resolver, Func <DocumentSummary, string> summaryResolver) { // arrange var doc = new Document { Uid = nameof(DocSummaryTests), AuthorAlias = "system", Title = "Document title" }; // act var summary = new DocumentSummary(doc); var expected = resolver(doc); var actual = summaryResolver(summary); // assert Assert.Equal(expected, actual); }
internal static DocumentSummary CreateDocumentSummary(IMethodSymbol method) { var result = new DocumentSummary(); foreach (var item in method.TypeParameters) { result.TypeParameterNames.Add(item.Name); } foreach (var item in method.Parameters) { //skip in editing. if (!string.IsNullOrEmpty(item.Name)) { result.ParameterNames.Add(item.Name); } } result.HasReturns = !method.ReturnsVoid; return result; }
public override int GetHashCode() { unchecked { int hash = 17; hash = hash * 23 + (ChangeNumber == default(int) ? 0 : ChangeNumber.GetHashCode()); hash = hash * 23 + (Document1 == null ? 0 : Document1.GetHashCode()); hash = hash * 23 + (DocumentLevel == null ? 0 : DocumentLevel.GetHashCode()); hash = hash * 23 + (DocumentSummary == null ? 0 : DocumentSummary.GetHashCode()); hash = hash * 23 + (FileExtension == null ? 0 : FileExtension.GetHashCode()); hash = hash * 23 + (FileName == null ? 0 : FileName.GetHashCode()); hash = hash * 23 + (FolderFlag == default(bool) ? 0 : FolderFlag.GetHashCode()); hash = hash * 23 + (ModifiedDate == default(DateTime) ? 0 : ModifiedDate.GetHashCode()); hash = hash * 23 + (Owner == default(int) ? 0 : Owner.GetHashCode()); hash = hash * 23 + (Revision == null ? 0 : Revision.GetHashCode()); hash = hash * 23 + (Rowguid == default(Guid) ? 0 : Rowguid.GetHashCode()); hash = hash * 23 + (Status == default(byte) ? 0 : Status.GetHashCode()); hash = hash * 23 + (Title == null ? 0 : Title.GetHashCode()); return(hash); } }
public string GetShareUrl(DocumentSummary sum, bool readOnly) { var pubString = BitConverter.ToString(sum.PublicKey).Replace("-", ""); var privString = sum.PrivateKey != null?BitConverter.ToString(sum.PrivateKey).Replace("-", "") : null; var query = new Dictionary <string, string> { { "id", sum.Id.ToString() }, { "pub", pubString }, { "c", sum.ContentSeed }, { "s", sum.StorageSource.ToString() }, }; if (!string.IsNullOrEmpty(privString) && !readOnly) { query.Add("priv", privString); } var shareUrl = QueryHelpers.AddQueryString(navigationManager.ToAbsoluteUri("/").ToString(), query); return(shareUrl); }
public static IEnumerable <object[]> EqualityTests() { var uid = Guid.NewGuid().ToString(); var sameUid = new DocumentSummary { Uid = uid }; var differentUid = new DocumentSummary { Uid = Guid.NewGuid().ToString() }; var anonymous = new { Uid = uid }; yield return(new object[] { uid, sameUid, true }); yield return(new object[] { uid, differentUid, false }); yield return(new object[] { uid, anonymous, false }); }
public void AddDocumentSummary(Guid docId, byte[] pubKey, byte[]?privKey, string contentSeed, StorageSource storageSource) { var existing = DocumentList.Where(x => x.Id == docId).FirstOrDefault(); if (existing != null) { return; } DocumentSummary sum = new DocumentSummary() { Id = docId, PublicKey = pubKey, PrivateKey = privKey, ContentSeed = contentSeed, CreatedDate = DateTimeOffset.UtcNow, ModifiedDate = DateTimeOffset.UtcNow, Title = "Shared document", StorageSource = storageSource }; DocumentList.Add(sum); }
/// <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); }
/// <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 existing = DocumentList.Where(x => x.Id == CurrentDocument.Id).FirstOrDefault(); if (existing != null) { DocumentList.Remove(existing); } var created = existing?.CreatedDate ?? DateTimeOffset.UtcNow; //Fix title if there is no title var title = CurrentDocument.Title; if (string.IsNullOrWhiteSpace(title)) { title = fallbackTitle; } if (string.IsNullOrWhiteSpace(title)) { title = "Untitled document " + created; } CurrentDocument.Title = title; DocumentSummary sum = new DocumentSummary() { Id = CurrentDocument.Id, Title = CurrentDocument.Title, CreatedDate = created, ModifiedDate = DateTimeOffset.UtcNow }; DocumentList.Add(sum); string?imgLink = null; if (img != null) { using (Stream stream = new MemoryStream(img)) { //Save preview image to Skynet file var response = await client.UploadFileAsync("document.jpg", stream); imgLink = response.Skylink; } } sum.PreviewImage = imgLink; bool success = await SaveDocument(CurrentDocument); if (success) { CurrentDocument = null; } else { Error = "Unable to save document to Skynet. Please try again."; } //Save updated document list await SaveDocumentList(DocumentList); } }
/// <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; } } }
private async Task OnMetaMaskShare() { Error = null; var chain = await MetaMaskService.GetSelectedChain(); if (chain.chain != MetaMask.Blazor.Enums.Chain.Kovan) { Error = "Please select the Kovan network in MetaMask. Sharing currently only works on the Kovan testnet."; Progress = null; return; } if (SkyDocsService.CurrentSum == null) { Progress = null; return; } //Store data to share and get URL Progress = "Saving sharing secrets to Skynet..."; StateHasChanged(); try { var existing = SkyDocsService.CurrentSum; DocumentSummary shareSum = new DocumentSummary() { ContentSeed = existing.ContentSeed, CreatedDate = existing.CreatedDate, Id = existing.Id, ShareOrigin = existing.ShareOrigin, Title = existing.Title, PublicKey = existing.PublicKey, ModifiedDate = existing.ModifiedDate, PreviewImage = existing.PreviewImage, PrivateKey = ShareReadOnly ? null : existing.PrivateKey, StorageSource = SkyDocsService.IsDfinityLogin ? StorageSource.Dfinity : StorageSource.Skynet }; string?url = await ShareService.StoreShareMessage(ShareFormModel.EthAddress, shareSum); if (string.IsNullOrEmpty(url)) { Error = "Error storing shared data. Please try again"; } else { Console.WriteLine(url); } //Smart contract has a function called "share" FunctionABI function = new FunctionABI("share", false); //With 4 inputs var inputsParameters = new[] { new Parameter("address", "receiver"), new Parameter("string", "appId"), new Parameter("string", "shareType"), new Parameter("string", "data") }; function.InputParameters = inputsParameters; var functionCallEncoder = new FunctionCallEncoder(); var data = functionCallEncoder.EncodeRequest(function.Sha3Signature, inputsParameters, ShareFormModel.EthAddress, "SkyDocs", string.Empty, url); //Using The Share It Network: https://github.com/michielpost/TheShareItNetwork string address = "0x6E8c5AFd3CFf5f6Ec85c032B68eF2997323a00FD"; BigInteger weiValue = 0; data = data[2..]; //Remove the 0x from the generated string
public static void AreEqual(Document expected, DocumentSummary actual) { Assert.AreEqual(expected.Id, actual.Id); Assert.AreEqual(expected.Name, actual.Name); }