• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

opengovsg / formsg-javascript-sdk / 6902743362

17 Nov 2023 10:22AM UTC coverage: 97.567%. First build
6902743362

Pull #96

github

web-flow
Update ci.yml to use node-version 18
Pull Request #96: Update ci.yml

91 of 100 branches covered (0.0%)

Branch coverage included in aggregate %.

310 of 311 relevant lines covered (99.68%)

6.36 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

100.0
/src/util/parser.ts
1
// The constituents of the X-FormSG-Signature
2
export type HeaderSignature = {
3
  // The ed25519 signature
4
  v1: string
5
  // The epoch used for signing, number of milliseconds since Jan 1, 1970
6
  t: number
7
  // The submission ID, usually the MongoDB submission ObjectId
8
  s: string
9
  // The form ID, usually the MongoDB form ObjectId
10
  f: string
11
}
12

13
// The constituents of the verification signature
14
// Note that even though this is similar to HeaderSignature, the keys do not
15
// mean the same thing.
16
export type VerificationSignature = {
17
  // The transaction id.
18
  v: string
19
  // The epoch used for signing, number of milliseconds since Jan 1, 1970
20
  t: number
21
  // The signature component.
22
  s: string
23
  // The form ID, usually the MongoDB form ObjectId
24
  f: string
25
}
26

27
/**
28
 * Helper function to retrieve keys-values in a signature.
29
 * @param signature The signature to convert to a keymap
30
 * @returns The key-value map of the signature
31
 */
32
const signatureToKeyMap = (signature: string) => {
3✔
33
  return signature
12✔
34
    .split(',')
35
    .map((kv) => kv.split(/=(.*)/))
42✔
36
    .reduce((acc: Record<string, string>, [k, v]) => {
42✔
37
      acc[k] = v
42✔
38
      return acc
42✔
39
    }, {})
40
}
41

42
/**
43
 * Parses the X-FormSG-Signature header into its constituents
44
 * @param header The X-FormSG-Signature header
45
 * @returns The signature header constituents
46
 */
47
export const parseSignatureHeader = (header: string): HeaderSignature => {
3✔
48
  const parsedSignature = signatureToKeyMap(header) as Record<
8✔
49
    string,
50
    string | number
51
  >
52

53
  parsedSignature.t = Number(parsedSignature.t)
8✔
54

55
  return parsedSignature as HeaderSignature
8✔
56
}
57

58
/**
59
 * Parses the verification signature into its constituent
60
 * @param signature The verification signature
61
 * @returns The verification signature constituents
62
 */
63
export const parseVerificationSignature = (
3✔
64
  signature: string
65
): VerificationSignature => {
66
  const parsedSignature = signatureToKeyMap(signature) as Record<
4✔
67
    string,
68
    string | number
69
  >
70

71
  parsedSignature.t = Number(parsedSignature.t)
4✔
72

73
  return parsedSignature as VerificationSignature
4✔
74
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc