custom-jsonld-signature.ts 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. import AsyncLRU from 'async-lru'
  2. import { logger } from './logger'
  3. import jsonld = require('jsonld')
  4. const CACHE = {
  5. 'https://w3id.org/security/v1': {
  6. '@context': {
  7. id: '@id',
  8. type: '@type',
  9. dc: 'http://purl.org/dc/terms/',
  10. sec: 'https://w3id.org/security#',
  11. xsd: 'http://www.w3.org/2001/XMLSchema#',
  12. EcdsaKoblitzSignature2016: 'sec:EcdsaKoblitzSignature2016',
  13. Ed25519Signature2018: 'sec:Ed25519Signature2018',
  14. EncryptedMessage: 'sec:EncryptedMessage',
  15. GraphSignature2012: 'sec:GraphSignature2012',
  16. LinkedDataSignature2015: 'sec:LinkedDataSignature2015',
  17. LinkedDataSignature2016: 'sec:LinkedDataSignature2016',
  18. CryptographicKey: 'sec:Key',
  19. authenticationTag: 'sec:authenticationTag',
  20. canonicalizationAlgorithm: 'sec:canonicalizationAlgorithm',
  21. cipherAlgorithm: 'sec:cipherAlgorithm',
  22. cipherData: 'sec:cipherData',
  23. cipherKey: 'sec:cipherKey',
  24. created: { '@id': 'dc:created', '@type': 'xsd:dateTime' },
  25. creator: { '@id': 'dc:creator', '@type': '@id' },
  26. digestAlgorithm: 'sec:digestAlgorithm',
  27. digestValue: 'sec:digestValue',
  28. domain: 'sec:domain',
  29. encryptionKey: 'sec:encryptionKey',
  30. expiration: { '@id': 'sec:expiration', '@type': 'xsd:dateTime' },
  31. expires: { '@id': 'sec:expiration', '@type': 'xsd:dateTime' },
  32. initializationVector: 'sec:initializationVector',
  33. iterationCount: 'sec:iterationCount',
  34. nonce: 'sec:nonce',
  35. normalizationAlgorithm: 'sec:normalizationAlgorithm',
  36. owner: { '@id': 'sec:owner', '@type': '@id' },
  37. password: 'sec:password',
  38. privateKey: { '@id': 'sec:privateKey', '@type': '@id' },
  39. privateKeyPem: 'sec:privateKeyPem',
  40. publicKey: { '@id': 'sec:publicKey', '@type': '@id' },
  41. publicKeyBase58: 'sec:publicKeyBase58',
  42. publicKeyPem: 'sec:publicKeyPem',
  43. publicKeyWif: 'sec:publicKeyWif',
  44. publicKeyService: { '@id': 'sec:publicKeyService', '@type': '@id' },
  45. revoked: { '@id': 'sec:revoked', '@type': 'xsd:dateTime' },
  46. salt: 'sec:salt',
  47. signature: 'sec:signature',
  48. signatureAlgorithm: 'sec:signingAlgorithm',
  49. signatureValue: 'sec:signatureValue'
  50. }
  51. }
  52. }
  53. const nodeDocumentLoader = jsonld.documentLoaders.node()
  54. const lru = new AsyncLRU({
  55. max: 10,
  56. load: (url, cb) => {
  57. if (CACHE[url] !== undefined) {
  58. logger.debug('Using cache for JSON-LD %s.', url)
  59. return cb(null, {
  60. contextUrl: null,
  61. document: CACHE[url],
  62. documentUrl: url
  63. })
  64. }
  65. nodeDocumentLoader(url)
  66. .then(value => cb(null, value))
  67. .catch(err => cb(err))
  68. }
  69. })
  70. /* eslint-disable no-import-assign */
  71. jsonld.documentLoader = (url) => {
  72. return new Promise((res, rej) => {
  73. lru.get(url, (err, value) => {
  74. if (err) return rej(err)
  75. return res(value)
  76. })
  77. })
  78. }
  79. export { jsonld }