Skip to content

Instantly share code, notes, and snippets.

@lautarodragan
Last active September 28, 2018 08:46
Show Gist options
  • Save lautarodragan/b63f6fa979703542add37e660ac5761a to your computer and use it in GitHub Desktop.
Save lautarodragan/b63f6fa979703542add37e660ac5761a to your computer and use it in GitHub Desktop.
Using jsonld as a factory rather than a singleton
const JSONLD = require('jsonld')
const jsonld = JSONLD // try commenting this line
// const jsonld = JSONLD() // and uncommenting this one
const DefaultClaimContext = {
cred: 'https://w3id.org/credentials#',
schema: 'http://schema.org/',
sec: 'https://w3id.org/security#',
issuer: 'cred:issuer',
issuanceDate: 'cred:issued',
type: 'http://schema.org/additionalType',
claim: 'http://schema.org/Thing', // The most generic definition in schema.org,
}
const DefaultWorkClaimContext = {
author: 'schema:author',
claim: 'http://schema.org/CreativeWork',
dateCreated: 'schema:dateCreated',
datePublished: 'schema:datePublished',
name: 'schema:name',
keywords: 'schema:keywords',
archiveUrl: 'schema:url',
hash: 'sec:digestValue',
}
const DefaultIdentityClaimContext = {
publicKey: 'sec:publicKeyBase58',
profileUrl: 'sec:owner',
}
const canonizeClaim = async (document) => {
const contextualClaim = {
'@context': {
...DefaultClaimContext,
...claimTypeDefaults[document.type],
...document['@context'],
},
type: document.type,
issuer: document.issuer,
issuanceDate: document.issuanceDate,
claim: document.claim,
}
return jsonld.canonize(contextualClaim)
}
const claimTypeDefaults = {
Work: DefaultWorkClaimContext,
Identity: DefaultIdentityClaimContext,
}
const testPublicKeyEd25519Base58 = 'JAi9YoyDdgBQLenyVzoXWH4C26wKMzHrjertxVrjLWTe'
const testOwnerUrl = `data:,${testPublicKeyEd25519Base58}`
const DataParser = jsonld => {
const nodeDocumentLoader = jsonld.documentLoaders.node({ usePromise: true })
const customLoader = async (url, callback) => {
const parsedData = ParseDataUrl(url)
if (parsedData) {
const publicKey = {
id: url,
type: 'Ed25519VerificationKey2018',
owner: url,
publicKeyBase58: parsedData.data,
}
return callback(null, {
contextUrl: ['https://w3id.org/security/v2'],
document: {
owner: {
id: url,
publicKey: [publicKey],
},
publicKey,
},
})
}
nodeDocumentLoader(url, callback)
}
jsonld.documentLoader = customLoader
}
DataParser(jsonld)
const TheRaven = {
id: '996efc4bd089f62e596f3c2c15bda3002d45540481df3be2c11192a6963ee8a7',
type: 'Work',
issuer: testOwnerUrl,
issuanceDate: '2017-11-13T15:00:00.000Z',
claim: {
name: 'The Raven',
author: 'Edgar Allan Poe',
keywords: 'poem',
dateCreated: '',
datePublished: '1845-01-29T03:00:00.000Z',
hash: 'de1c818f9be211a78dff90a03b9e297bbb61b3c292f1c1bbc3a5283e9b203cb1',
archiveUrl: 'ipfs:/theRaven',
},
'https://w3id.org/security#proof': {
'@graph': {
'@type': 'https://w3id.org/security#Ed25519Signature2018',
created: {
'@type': 'http://www.w3.org/2001/XMLSchema#dateTime',
'@value': '2018-09-05T20:19:20Z',
},
'http://purl.org/dc/terms/creator': {
'@id': testOwnerUrl,
},
'https://w3id.org/security#jws':
'eyJhbGciOiJFZERTQSIsImI2NCI6ZmFsc2UsImNyaXQiOlsiYjY0Il19..TSHkMOwbWZvIp8Hd-MyebaMgItf4Iyl3dgUSlHBBlnidw' +
'gzo084pGpKmbOewYFrXfmAVhXnC4UPzaPUjaU9BDw',
},
},
}
async function main(claim) {
console.log('claim', claim)
const canonizedClaim = await canonizeClaim(claim)
console.log('canonizedClaim', canonizedClaim)
}
main(TheRaven).catch(console.error)
/*********
In both cases the output is:
$ node index.js
claim { id:
'996efc4bd089f62e596f3c2c15bda3002d45540481df3be2c11192a6963ee8a7',
type: 'Work',
issuer: 'data:,JAi9YoyDdgBQLenyVzoXWH4C26wKMzHrjertxVrjLWTe',
issuanceDate: '2017-11-13T15:00:00.000Z',
claim:
{ name: 'The Raven',
author: 'Edgar Allan Poe',
keywords: 'poem',
dateCreated: '',
datePublished: '1845-01-29T03:00:00.000Z',
hash:
'de1c818f9be211a78dff90a03b9e297bbb61b3c292f1c1bbc3a5283e9b203cb1',
archiveUrl: 'ipfs:/theRaven' },
'https://w3id.org/security#proof':
{ '@graph':
{ '@type': 'https://w3id.org/security#Ed25519Signature2018',
created: [Object],
'http://purl.org/dc/terms/creator': [Object],
'https://w3id.org/security#jws':
'eyJhbGciOiJFZERTQSIsImI2NCI6ZmFsc2UsImNyaXQiOlsiYjY0Il19..TSHkMOwbWZvIp8Hd-MyebaMgItf4Iyl3dgUSlHBBlnidwgzo084pGpKmbOewYFrXfmAVhXnC4UPzaPUjaU9BDw' } } }
canonizedClaim _:c14n0 <http://schema.org/CreativeWork> _:c14n1 .
_:c14n0 <http://schema.org/additionalType> "Work" .
_:c14n0 <https://w3id.org/credentials#issued> "2017-11-13T15:00:00.000Z" .
_:c14n0 <https://w3id.org/credentials#issuer> "data:,JAi9YoyDdgBQLenyVzoXWH4C26wKMzHrjertxVrjLWTe" .
_:c14n1 <http://schema.org/author> "Edgar Allan Poe" .
_:c14n1 <http://schema.org/dateCreated> "" .
_:c14n1 <http://schema.org/datePublished> "1845-01-29T03:00:00.000Z" .
_:c14n1 <http://schema.org/keywords> "poem" .
_:c14n1 <http://schema.org/name> "The Raven" .
_:c14n1 <http://schema.org/url> "ipfs:/theRaven" .
_:c14n1 <https://w3id.org/security#digestValue> "de1c818f9be211a78dff90a03b9e297bbb61b3c292f1c1bbc3a5283e9b203cb1" .
$ node index.js
claim { id:
'996efc4bd089f62e596f3c2c15bda3002d45540481df3be2c11192a6963ee8a7',
type: 'Work',
issuer: 'data:,JAi9YoyDdgBQLenyVzoXWH4C26wKMzHrjertxVrjLWTe',
issuanceDate: '2017-11-13T15:00:00.000Z',
claim:
{ name: 'The Raven',
author: 'Edgar Allan Poe',
keywords: 'poem',
dateCreated: '',
datePublished: '1845-01-29T03:00:00.000Z',
hash:
'de1c818f9be211a78dff90a03b9e297bbb61b3c292f1c1bbc3a5283e9b203cb1',
archiveUrl: 'ipfs:/theRaven' },
'https://w3id.org/security#proof':
{ '@graph':
{ '@type': 'https://w3id.org/security#Ed25519Signature2018',
created: [Object],
'http://purl.org/dc/terms/creator': [Object],
'https://w3id.org/security#jws':
'eyJhbGciOiJFZERTQSIsImI2NCI6ZmFsc2UsImNyaXQiOlsiYjY0Il19..TSHkMOwbWZvIp8Hd-MyebaMgItf4Iyl3dgUSlHBBlnidwgzo084pGpKmbOewYFrXfmAVhXnC4UPzaPUjaU9BDw' } } }
canonizedClaim _:c14n0 <http://schema.org/CreativeWork> _:c14n1 .
_:c14n0 <http://schema.org/additionalType> "Work" .
_:c14n0 <https://w3id.org/credentials#issued> "2017-11-13T15:00:00.000Z" .
_:c14n0 <https://w3id.org/credentials#issuer> "data:,JAi9YoyDdgBQLenyVzoXWH4C26wKMzHrjertxVrjLWTe" .
_:c14n1 <http://schema.org/author> "Edgar Allan Poe" .
_:c14n1 <http://schema.org/dateCreated> "" .
_:c14n1 <http://schema.org/datePublished> "1845-01-29T03:00:00.000Z" .
_:c14n1 <http://schema.org/keywords> "poem" .
_:c14n1 <http://schema.org/name> "The Raven" .
_:c14n1 <http://schema.org/url> "ipfs:/theRaven" .
_:c14n1 <https://w3id.org/security#digestValue> "de1c818f9be211a78dff90a03b9e297bbb61b3c292f1c1bbc3a5283e9b203cb1" .
The two outputs are identical, so it seems to be working.
*****************/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment