@tdxvolt/volt-client-grpc 0.1.1 → 0.1.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/src/utils.js CHANGED
@@ -3,103 +3,103 @@ import _ from "lodash";
3
3
  import forge from "node-forge";
4
4
  import bs58 from "bs58";
5
5
 
6
- import {constants} from "./constants.js";
6
+ import { constants } from "./constants.js";
7
7
 
8
- const {pki} = forge;
8
+ const { pki } = forge;
9
9
 
10
10
  export const createSecureContextOptions = (cryptoOptions) => {
11
- const tlsOptions = {
12
- cert: Buffer.from(cryptoOptions.cert),
13
- ca: Buffer.from(cryptoOptions.ca),
14
- key: Buffer.from(cryptoOptions.key),
15
- };
16
- return tlsOptions;
11
+ const tlsOptions = {
12
+ cert: Buffer.from(cryptoOptions.cert),
13
+ ca: Buffer.from(cryptoOptions.ca),
14
+ key: Buffer.from(cryptoOptions.key),
15
+ };
16
+ return tlsOptions;
17
17
  };
18
18
 
19
19
  export const getServiceAddress = (mdnsService) => {
20
- let address;
21
-
22
- if (mdnsService.txtRecord && mdnsService.txtRecord.ip) {
23
- // Use the advertised IP address if present.
24
- address = `${mdnsService.txtRecord.ip}:${mdnsService.port}`;
25
- } else {
26
- // Find a valid ipv4 address.
27
- const ipv4 = _.find(mdnsService.addresses, (addy) => ip.isV4Format(addy));
28
- const serviceDetails = {
29
- host: ipv4,
30
- port: mdnsService.port,
31
- };
32
-
33
- address = `${serviceDetails.host}:${serviceDetails.port}`;
34
- }
35
-
36
- return address;
20
+ let address;
21
+
22
+ if (mdnsService.txtRecord?.ip) {
23
+ // Use the advertised IP address if present.
24
+ address = `${mdnsService.txtRecord.ip}:${mdnsService.port}`;
25
+ } else {
26
+ // Find a valid ipv4 address.
27
+ const ipv4 = _.find(mdnsService.addresses, (addy) => ip.isV4Format(addy));
28
+ const serviceDetails = {
29
+ host: ipv4,
30
+ port: mdnsService.port,
31
+ };
32
+
33
+ address = `${serviceDetails.host}:${serviceDetails.port}`;
34
+ }
35
+
36
+ return address;
37
37
  };
38
38
 
39
39
  export const privateKeyToPem = (keyPair) => {
40
- return pki.privateKeyToPem(keyPair.privateKey);
40
+ return pki.privateKeyToPem(keyPair.privateKey);
41
41
  };
42
42
 
43
43
  export const publicKeyToPem = (keyPair) => {
44
- return pki.publicKeyToPem(keyPair.publicKey);
44
+ return pki.publicKeyToPem(keyPair.publicKey);
45
45
  };
46
46
 
47
47
  export const deserialisePEM = (pem) => {
48
- return pem.replace(/\\r\\n/g, "\r\n");
48
+ return pem.replace(/\\r\\n/g, "\r\n");
49
49
  };
50
50
 
51
51
  export const removeCarriageReturn = (inp) => {
52
- return inp.replace(/\r\n/g, "\n");
52
+ return inp.replace(/\r\n/g, "\n");
53
53
  };
54
54
 
55
55
  export const createBase58Hash = (inp) => {
56
- const md = forge.md.sha256.create();
57
- md.update(inp);
58
- const hashBuf = Buffer.from(md.digest().getBytes(), "binary");
59
- return bs58.encode(hashBuf);
56
+ const md = forge.md.sha256.create();
57
+ md.update(inp);
58
+ const hashBuf = Buffer.from(md.digest().getBytes(), "binary");
59
+ return bs58.encode(hashBuf);
60
60
  };
61
61
 
62
62
  export const createPublicKeyFingerprint = (key) => {
63
- // Remove carriage returns before creating hash to bring into line with openSSL / volt format.
64
- return createBase58Hash(removeCarriageReturn(key));
63
+ // Remove carriage returns before creating hash to bring into line with openSSL / volt format.
64
+ return createBase58Hash(removeCarriageReturn(key));
65
65
  };
66
66
 
67
67
  export const signBase64 = (key, msg) => {
68
- const md = forge.md.sha256.create();
69
- md.update(msg);
70
- const signature = key.sign(md);
71
- return forge.util.encode64(signature);
68
+ const md = forge.md.sha256.create();
69
+ md.update(msg);
70
+ const signature = key.sign(md);
71
+ return forge.util.encode64(signature);
72
72
  };
73
73
 
74
74
  export const verifyBase64 = (key, msg, signature) => {
75
- const md = forge.md.sha256.create();
76
- md.update(msg);
77
- return key.verify(md.digest().bytes(), forge.util.decode64(signature));
75
+ const md = forge.md.sha256.create();
76
+ md.update(msg);
77
+ return key.verify(md.digest().bytes(), forge.util.decode64(signature));
78
78
  };
79
79
 
80
80
  export const getDIDResolutionURL = (didIn) => {
81
- const did = didIn.toLowerCase().split(":");
82
- if (did.length < 3) {
83
- return "";
84
- }
85
-
86
- if (did[0] !== "did") {
87
- return "";
88
- }
89
-
90
- if (did[1] !== "tdx") {
91
- return "";
92
- }
93
-
94
- let hostName;
95
- let didGUID;
96
- if (did.length === 3) {
97
- hostName = constants.defaultDIDHostName;
98
- didGUID = did[2];
99
- } else {
100
- hostName = did[2];
101
- didGUID = did[3];
102
- }
103
-
104
- return `https://${hostName}/api/identity/${didGUID}`;
81
+ const did = didIn.toLowerCase().split(":");
82
+ if (did.length < 3) {
83
+ return "";
84
+ }
85
+
86
+ if (did[0] !== "did") {
87
+ return "";
88
+ }
89
+
90
+ if (did[1] !== "tdx") {
91
+ return "";
92
+ }
93
+
94
+ let hostName;
95
+ let didGUID;
96
+ if (did.length === 3) {
97
+ hostName = constants.defaultDIDHostName;
98
+ didGUID = did[2];
99
+ } else {
100
+ hostName = did[2];
101
+ didGUID = did[3];
102
+ }
103
+
104
+ return `https://${hostName}/api/identity/${didGUID}`;
105
105
  };