Skip to content

Instantly share code, notes, and snippets.

@bjoerntx
Created August 2, 2019 13:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bjoerntx/edaa5ea7acd9b42ad45ae197aad68a63 to your computer and use it in GitHub Desktop.
Save bjoerntx/edaa5ea7acd9b42ad45ae197aad68a63 to your computer and use it in GitHub Desktop.
// this method stores the document hash in the blockchain
[System.Web.Http.HttpPost]
public ActionResult StoreDocument(
[FromBody] string Document,
[FromBody] string UniqueId,
[FromBody] string SignerName)
{
byte[] bPDF;
// create temporary ServerTextControl
using (TXTextControl.ServerTextControl tx = new TXTextControl.ServerTextControl())
{
tx.Create();
// load the document
tx.Load(Convert.FromBase64String(Document),
TXTextControl.BinaryStreamType.InternalUnicodeFormat);
TXTextControl.SaveSettings saveSettings = new TXTextControl.SaveSettings()
{
CreatorApplication = "TX Text Control Sample Application",
};
// save the document as PDF
tx.Save(out bPDF, TXTextControl.BinaryStreamType.AdobePDF, saveSettings);
}
// calculate the MD5 checksum of the binary data
// and store in SignedDocument object
SignedDocument document = new SignedDocument()
{
Checksum = Checksum.CalculateMD5(bPDF)
};
// define a Blockchain object
Blockchain bcDocument;
// if the blockchain exists, load it
if (System.IO.File.Exists(
Server.MapPath("~/App_Data/Blockchains/" + UniqueId + ".bc")))
{
string bc = System.IO.File.ReadAllText(
Server.MapPath("~/App_Data/Blockchains/" + UniqueId + ".bc"));
bcDocument = JsonConvert.DeserializeObject<Blockchain>(bc);
}
else
bcDocument = new Blockchain(true); // otherwise create a new blockchain
// add a new block to the blockchain and store the SignedDocument object
bcDocument.AddBlock(new Block(DateTime.Now, null, JsonConvert.SerializeObject(document)));
// store the blockchain as a file
System.IO.File.WriteAllText(
Server.MapPath("~/App_Data/Blockchains/" + UniqueId + ".bc"),
JsonConvert.SerializeObject(bcDocument));
// create and return a view model with the PDF and the unique document ID
StoredDocument storedDocument = new StoredDocument()
{
PDF = Convert.ToBase64String(bPDF),
DocumentId = UniqueId
};
return new JsonResult() { Data = storedDocument,
JsonRequestBehavior = JsonRequestBehavior.AllowGet };
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment