Last active
March 7, 2023 14:15
-
-
Save janpieterz/c6b22467da3a3a46374a43dbcef1f2a7 to your computer and use it in GitHub Desktop.
Trinsic Credential with File Attachment
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import {FieldType, TrinsicService} from "@trinsic/trinsic"; | |
import fs from "fs"; | |
import https from "https"; | |
let trinsic = new TrinsicService(); | |
trinsic.options.authToken = process.env.TRINSIC_AUTH_TOKEN; | |
//Just doing this dance so that I can detect the ecosystem and not keep re-creating the template | |
const info = await trinsic.provider().ecosystemInfo(); | |
const templateName = "graduation-certificate" | |
let templateId = `urn:template:${info.ecosystem.name}:${templateName}`; | |
const templateResponse = await trinsic.template().get({id: templateId}); | |
if(templateResponse === undefined || templateResponse.template === undefined) { | |
console.log(`Did not find template with id ${templateId} - creating it now...`); | |
const createTemplate = await trinsic.template().create({ | |
name: templateName, | |
fields: [ | |
{ | |
description: "Name", | |
type: FieldType.STRING, | |
optional: false | |
}, | |
{ | |
description: "Certificate", | |
type: FieldType.URI, | |
optional: false | |
} | |
] | |
}); | |
templateId = createTemplate.data?.id; | |
if(templateId === undefined) { | |
throw new Error("Template ID not found"); | |
} | |
console.log(`Created template with id ${templateId}`); | |
} | |
const fileBytes = fs.readFileSync('attachment.pdf'); | |
const fileMimeType = "application/pdf"; | |
console.log("Uploading attachment..."); | |
const uploadResponse = await trinsic.fileManagement().uploadFile( | |
{ | |
contents: fileBytes, | |
mimeType: fileMimeType | |
} | |
); | |
const fileUrl = uploadResponse.uploadedFile?.url; | |
if(fileUrl === undefined){ | |
throw new Error("Attachment not uploaded"); | |
} | |
console.log("Uploaded attachment, issuing credential...", fileUrl); | |
const credentialResponse = await trinsic.credential().issueFromTemplate({templateId: templateId, saveCopy: true, valuesJson: JSON.stringify({Name: "John Doe", Certificate: fileUrl})}); | |
const credential = JSON.parse(credentialResponse.documentJson); | |
console.log("Created credential", credential); | |
//You can choose to download credential.credentialSubject.Certificate to a file and verify the hash link. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"name": "file-credential", | |
"version": "1.0.0", | |
"description": "", | |
"main": "index.js", | |
"type": "module", | |
"scripts": { | |
"test": "echo \"Error: no test specified\" && exit 1" | |
}, | |
"author": "", | |
"license": "ISC", | |
"dependencies": { | |
"@trinsic/trinsic": "^1.10.0" | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment