2017-11-17 08:20:42 -06:00
|
|
|
import * as AsyncLRU from 'async-lru'
|
2018-10-23 04:38:48 -05:00
|
|
|
import * as jsonld from 'jsonld'
|
2019-04-15 03:49:46 -05:00
|
|
|
import { logger } from './logger'
|
|
|
|
|
|
|
|
const CACHE = {
|
|
|
|
'https://w3id.org/security/v1': {
|
|
|
|
'@context': {
|
2020-01-31 09:56:52 -06:00
|
|
|
id: '@id',
|
|
|
|
type: '@type',
|
2019-04-15 03:49:46 -05:00
|
|
|
|
2020-01-31 09:56:52 -06:00
|
|
|
dc: 'http://purl.org/dc/terms/',
|
|
|
|
sec: 'https://w3id.org/security#',
|
|
|
|
xsd: 'http://www.w3.org/2001/XMLSchema#',
|
2019-04-15 03:49:46 -05:00
|
|
|
|
2020-01-31 09:56:52 -06:00
|
|
|
EcdsaKoblitzSignature2016: 'sec:EcdsaKoblitzSignature2016',
|
|
|
|
Ed25519Signature2018: 'sec:Ed25519Signature2018',
|
|
|
|
EncryptedMessage: 'sec:EncryptedMessage',
|
|
|
|
GraphSignature2012: 'sec:GraphSignature2012',
|
|
|
|
LinkedDataSignature2015: 'sec:LinkedDataSignature2015',
|
|
|
|
LinkedDataSignature2016: 'sec:LinkedDataSignature2016',
|
|
|
|
CryptographicKey: 'sec:Key',
|
2019-04-15 03:49:46 -05:00
|
|
|
|
2020-01-31 09:56:52 -06:00
|
|
|
authenticationTag: 'sec:authenticationTag',
|
|
|
|
canonicalizationAlgorithm: 'sec:canonicalizationAlgorithm',
|
|
|
|
cipherAlgorithm: 'sec:cipherAlgorithm',
|
|
|
|
cipherData: 'sec:cipherData',
|
|
|
|
cipherKey: 'sec:cipherKey',
|
|
|
|
created: { '@id': 'dc:created', '@type': 'xsd:dateTime' },
|
|
|
|
creator: { '@id': 'dc:creator', '@type': '@id' },
|
|
|
|
digestAlgorithm: 'sec:digestAlgorithm',
|
|
|
|
digestValue: 'sec:digestValue',
|
|
|
|
domain: 'sec:domain',
|
|
|
|
encryptionKey: 'sec:encryptionKey',
|
|
|
|
expiration: { '@id': 'sec:expiration', '@type': 'xsd:dateTime' },
|
|
|
|
expires: { '@id': 'sec:expiration', '@type': 'xsd:dateTime' },
|
|
|
|
initializationVector: 'sec:initializationVector',
|
|
|
|
iterationCount: 'sec:iterationCount',
|
|
|
|
nonce: 'sec:nonce',
|
|
|
|
normalizationAlgorithm: 'sec:normalizationAlgorithm',
|
|
|
|
owner: { '@id': 'sec:owner', '@type': '@id' },
|
|
|
|
password: 'sec:password',
|
|
|
|
privateKey: { '@id': 'sec:privateKey', '@type': '@id' },
|
|
|
|
privateKeyPem: 'sec:privateKeyPem',
|
|
|
|
publicKey: { '@id': 'sec:publicKey', '@type': '@id' },
|
|
|
|
publicKeyBase58: 'sec:publicKeyBase58',
|
|
|
|
publicKeyPem: 'sec:publicKeyPem',
|
|
|
|
publicKeyWif: 'sec:publicKeyWif',
|
|
|
|
publicKeyService: { '@id': 'sec:publicKeyService', '@type': '@id' },
|
|
|
|
revoked: { '@id': 'sec:revoked', '@type': 'xsd:dateTime' },
|
|
|
|
salt: 'sec:salt',
|
|
|
|
signature: 'sec:signature',
|
|
|
|
signatureAlgorithm: 'sec:signingAlgorithm',
|
|
|
|
signatureValue: 'sec:signatureValue'
|
2019-04-15 03:49:46 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-11-17 08:20:42 -06:00
|
|
|
|
|
|
|
const nodeDocumentLoader = jsonld.documentLoaders.node()
|
|
|
|
|
|
|
|
const lru = new AsyncLRU({
|
|
|
|
max: 10,
|
2019-04-15 03:49:46 -05:00
|
|
|
load: (url, cb) => {
|
2020-01-31 09:56:52 -06:00
|
|
|
if (CACHE[url] !== undefined) {
|
2019-04-15 03:49:46 -05:00
|
|
|
logger.debug('Using cache for JSON-LD %s.', url)
|
|
|
|
|
|
|
|
return cb(null, {
|
|
|
|
contextUrl: null,
|
2020-01-31 09:56:52 -06:00
|
|
|
document: CACHE[url],
|
2019-04-15 03:49:46 -05:00
|
|
|
documentUrl: url
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-01-07 08:24:27 -06:00
|
|
|
nodeDocumentLoader(url)
|
|
|
|
.then(value => cb(null, value))
|
|
|
|
.catch(err => cb(err))
|
2017-11-17 08:20:42 -06:00
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2021-02-03 02:33:05 -06:00
|
|
|
/* eslint-disable no-import-assign */
|
2020-01-07 08:24:27 -06:00
|
|
|
jsonld.documentLoader = (url) => {
|
|
|
|
return new Promise((res, rej) => {
|
|
|
|
lru.get(url, (err, value) => {
|
|
|
|
if (err) return rej(err)
|
|
|
|
|
|
|
|
return res(value)
|
|
|
|
})
|
|
|
|
})
|
2017-11-17 08:20:42 -06:00
|
|
|
}
|
|
|
|
|
2019-08-29 09:15:41 -05:00
|
|
|
export { jsonld }
|