custom-jsonld-signature.ts 2.8 KB

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