1 import * as AsyncLRU from 'async-lru'
2 import * as jsonld from 'jsonld'
3 import { logger } from './logger'
6 'https://w3id.org/security/v1': {
11 dc: 'http://purl.org/dc/terms/',
12 sec: 'https://w3id.org/security#',
13 xsd: 'http://www.w3.org/2001/XMLSchema#',
15 EcdsaKoblitzSignature2016: 'sec:EcdsaKoblitzSignature2016',
16 Ed25519Signature2018: 'sec:Ed25519Signature2018',
17 EncryptedMessage: 'sec:EncryptedMessage',
18 GraphSignature2012: 'sec:GraphSignature2012',
19 LinkedDataSignature2015: 'sec:LinkedDataSignature2015',
20 LinkedDataSignature2016: 'sec:LinkedDataSignature2016',
21 CryptographicKey: 'sec:Key',
23 authenticationTag: 'sec:authenticationTag',
24 canonicalizationAlgorithm: 'sec:canonicalizationAlgorithm',
25 cipherAlgorithm: 'sec:cipherAlgorithm',
26 cipherData: 'sec:cipherData',
27 cipherKey: 'sec:cipherKey',
28 created: { '@id': 'dc:created', '@type': 'xsd:dateTime' },
29 creator: { '@id': 'dc:creator', '@type': '@id' },
30 digestAlgorithm: 'sec:digestAlgorithm',
31 digestValue: 'sec:digestValue',
33 encryptionKey: 'sec:encryptionKey',
34 expiration: { '@id': 'sec:expiration', '@type': 'xsd:dateTime' },
35 expires: { '@id': 'sec:expiration', '@type': 'xsd:dateTime' },
36 initializationVector: 'sec:initializationVector',
37 iterationCount: 'sec:iterationCount',
39 normalizationAlgorithm: 'sec:normalizationAlgorithm',
40 owner: { '@id': 'sec:owner', '@type': '@id' },
41 password: 'sec:password',
42 privateKey: { '@id': 'sec:privateKey', '@type': '@id' },
43 privateKeyPem: 'sec:privateKeyPem',
44 publicKey: { '@id': 'sec:publicKey', '@type': '@id' },
45 publicKeyBase58: 'sec:publicKeyBase58',
46 publicKeyPem: 'sec:publicKeyPem',
47 publicKeyWif: 'sec:publicKeyWif',
48 publicKeyService: { '@id': 'sec:publicKeyService', '@type': '@id' },
49 revoked: { '@id': 'sec:revoked', '@type': 'xsd:dateTime' },
51 signature: 'sec:signature',
52 signatureAlgorithm: 'sec:signingAlgorithm',
53 signatureValue: 'sec:signatureValue'
58 const nodeDocumentLoader = jsonld.documentLoaders.node()
60 const lru = new AsyncLRU({
63 if (CACHE[url] !== undefined) {
64 logger.debug('Using cache for JSON-LD %s.', url)
73 nodeDocumentLoader(url)
74 .then(value => cb(null, value))
75 .catch(err => cb(err))
79 jsonld.documentLoader = (url) => {
80 return new Promise((res, rej) => {
81 lru.get(url, (err, value) => {
82 if (err) return rej(err)