aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/helpers/custom-jsonld-signature.ts
blob: a3bceb047700fc1027b0704f459ee88a88e12624 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
import * as AsyncLRU from 'async-lru'
import * as jsonld from 'jsonld'
import * as jsig from 'jsonld-signatures'
import { logger } from './logger'

const CACHE = {
  'https://w3id.org/security/v1': {
    '@context': {
      'id': '@id',
      'type': '@type',

      'dc': 'http://purl.org/dc/terms/',
      'sec': 'https://w3id.org/security#',
      'xsd': 'http://www.w3.org/2001/XMLSchema#',

      'EcdsaKoblitzSignature2016': 'sec:EcdsaKoblitzSignature2016',
      'Ed25519Signature2018': 'sec:Ed25519Signature2018',
      'EncryptedMessage': 'sec:EncryptedMessage',
      'GraphSignature2012': 'sec:GraphSignature2012',
      'LinkedDataSignature2015': 'sec:LinkedDataSignature2015',
      'LinkedDataSignature2016': 'sec:LinkedDataSignature2016',
      'CryptographicKey': 'sec:Key',

      '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'
    }
  }
}

const nodeDocumentLoader = jsonld.documentLoaders.node()

const lru = new AsyncLRU({
  max: 10,
  load: (url, cb) => {
    if (CACHE[ url ] !== undefined) {
      logger.debug('Using cache for JSON-LD %s.', url)

      return cb(null, {
        contextUrl: null,
        document: CACHE[ url ],
        documentUrl: url
      })
    }

    nodeDocumentLoader(url, cb)
  }
})

jsonld.documentLoader = (url, cb) => {
  lru.get(url, cb)
}

jsig.use('jsonld', jsonld)

export { jsig, jsonld }