@abtnode/util 1.8.48 → 1.8.50

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/lib/base32.js ADDED
@@ -0,0 +1,14 @@
1
+ const { fromBase58 } = require('@ocap/util');
2
+ const { base32 } = require('multiformats/bases/base32');
3
+
4
+ const encode = (did) => {
5
+ if (!did) {
6
+ return did;
7
+ }
8
+
9
+ const buffer = fromBase58(did);
10
+
11
+ return base32.encode(buffer);
12
+ };
13
+
14
+ module.exports = { encode };
@@ -1,28 +1,24 @@
1
- const get = require('lodash/get');
2
1
  const stringify = require('json-stable-stringify');
3
2
  const { toBase64, toBase58 } = require('@ocap/util');
4
3
  const joinUrl = require('url-join');
5
4
  const pRetry = require('p-retry');
6
5
  const debug = require('debug')('@abtnode/util:did-document');
7
6
  const axios = require('./axios');
7
+ const { encode: encodeBase32 } = require('./base32');
8
8
 
9
- const update = async ({ records, didRegistryUrl, wallet }) => {
9
+ const getDID = (address) => `did:abt:${address}`;
10
+
11
+ const update = async ({ services, didRegistryUrl, wallet }) => {
10
12
  debug('update did document', { didRegistryUrl });
11
13
 
12
- const did = `did:abt:${wallet.address}`;
14
+ const did = getDID(wallet.address);
13
15
  const time = new Date().toISOString();
14
16
 
15
17
  const document = {
16
18
  '@context': 'https://www.w3.org/ns/did/v1',
17
19
  id: did,
18
20
  controller: did,
19
- service: [
20
- {
21
- id: `${did}#dns`,
22
- type: 'DNSRecords',
23
- records,
24
- },
25
- ],
21
+ service: services,
26
22
  verificationMethod: [
27
23
  {
28
24
  id: `${did}#key-1`,
@@ -50,30 +46,66 @@ const update = async ({ records, didRegistryUrl, wallet }) => {
50
46
 
51
47
  const updateWithRetry = async (...args) => pRetry(() => update(...args), { retries: 3 });
52
48
 
53
- const updateDaemon = async ({ ips, wallet, didRegistryUrl, domain }) => {
54
- try {
55
- const filteredIps = (ips || []).filter(Boolean);
56
- if (filteredIps.length === 0) {
57
- return {
58
- status: 'warn',
59
- message: 'No DID Document to update',
60
- };
61
- }
62
-
63
- const records = filteredIps.map((ip) => {
64
- return {
65
- type: 'A',
66
- rr: wallet.address.toLowerCase(),
67
- value: ip,
68
- domain,
69
- };
70
- });
71
-
72
- await updateWithRetry({ records, didRegistryUrl, wallet });
73
- return { status: 'success' };
74
- } catch (err) {
75
- return { status: 'error', message: get(err, 'response.data') || err.message };
49
+ const getServerServices = ({ ips, wallet, domain }) => {
50
+ const records = ips.map((ip) => ({
51
+ type: 'A',
52
+ rr: encodeBase32(wallet.address),
53
+ value: ip,
54
+ domain,
55
+ }));
56
+
57
+ const services = [
58
+ {
59
+ id: getDID(wallet.address),
60
+ type: 'DNSRecords',
61
+ records,
62
+ },
63
+ ];
64
+
65
+ return services;
66
+ };
67
+
68
+ const getBlockletServices = ({ blockletAppDid, daemonDidDomain, domain }) => {
69
+ return [
70
+ {
71
+ id: getDID(blockletAppDid),
72
+ type: 'DNSRecords',
73
+ records: [
74
+ {
75
+ type: 'CNAME',
76
+ rr: encodeBase32(blockletAppDid),
77
+ value: daemonDidDomain,
78
+ domain,
79
+ },
80
+ ],
81
+ },
82
+ ];
83
+ };
84
+
85
+ const updateServerDocument = async ({ ips, wallet, didRegistryUrl, domain }) => {
86
+ const filteredIps = (ips || []).filter(Boolean);
87
+ if (filteredIps.length === 0) {
88
+ throw new Error('No DID Document to update');
76
89
  }
90
+
91
+ const services = getServerServices({ ips: filteredIps, domain, wallet });
92
+
93
+ return updateWithRetry({ services, didRegistryUrl, wallet });
94
+ };
95
+
96
+ const updateBlockletDocument = async ({ wallet, didRegistryUrl, domain, daemonDidDomain, blockletAppDid }) => {
97
+ const services = getBlockletServices({ blockletAppDid, daemonDidDomain, domain });
98
+
99
+ return updateWithRetry({ services, didRegistryUrl, wallet });
77
100
  };
78
101
 
79
- module.exports = { updateDaemon };
102
+ const disableDNS = async ({ wallet, didRegistryUrl }) => updateWithRetry({ services: [], didRegistryUrl, wallet });
103
+
104
+ module.exports = {
105
+ updateServerDocument,
106
+ updateBlockletDocument,
107
+ disableDNS,
108
+ getDID,
109
+ getServerServices,
110
+ getBlockletServices,
111
+ };
@@ -0,0 +1,136 @@
1
+ const fs = require('fs');
2
+ const path = require('path');
3
+ const get = require('lodash/get');
4
+
5
+ const logger = require('@abtnode/logger')('@abtnode/util:logo-middleware');
6
+
7
+ const ensureBlockletExist = async (req, res, next) => {
8
+ const { blocklet } = req;
9
+
10
+ if (!blocklet || !get(blocklet, 'env.appDir')) {
11
+ res.sendFallbackLogo();
12
+ return;
13
+ }
14
+
15
+ next();
16
+ };
17
+
18
+ const ensureCustomSquareLogo = async (req, res, next) => {
19
+ const { blocklet, sendOptions } = req;
20
+
21
+ const customLogoSquare = get(blocklet, 'environmentObj.BLOCKLET_APP_LOGO_SQUARE');
22
+
23
+ if (customLogoSquare) {
24
+ if (customLogoSquare.startsWith('http')) {
25
+ res.redirect(customLogoSquare);
26
+ return;
27
+ }
28
+
29
+ const logoFile = path.join(get(blocklet, 'env.appDir'), customLogoSquare);
30
+ if (fs.existsSync(logoFile)) {
31
+ res.sendFile(logoFile, sendOptions);
32
+ return;
33
+ }
34
+ }
35
+
36
+ next();
37
+ };
38
+
39
+ const ensureBundleLogo = async (req, res, next) => {
40
+ const { blocklet, sendOptions } = req;
41
+
42
+ if (blocklet.meta.logo) {
43
+ const logoFile = path.join(get(blocklet, 'env.appDir'), blocklet.meta.logo);
44
+
45
+ if (fs.existsSync(logoFile)) {
46
+ res.sendFile(logoFile, sendOptions);
47
+ return;
48
+ }
49
+ }
50
+
51
+ next();
52
+ };
53
+
54
+ const fallbackLogo = async (req, res) => {
55
+ res.sendFallbackLogo();
56
+ };
57
+
58
+ // eslint-disable-next-line no-unused-vars
59
+ const cacheError = async (err, req, res, next) => {
60
+ logger.error('failed to send blocklet logo', { url: req.url, error: err });
61
+ res.sendFallbackLogo();
62
+ };
63
+
64
+ const ensureDefaultLogo = async (req, res, next) => {
65
+ const { blocklet, sendOptions } = req;
66
+
67
+ if (blocklet && get(blocklet, 'env.dataDir')) {
68
+ get(blocklet, 'env.dataDir');
69
+
70
+ const logoSvgFile = path.join(get(blocklet, 'env.dataDir'), 'logo.svg');
71
+ if (fs.existsSync(logoSvgFile)) {
72
+ res.sendFile(logoSvgFile, sendOptions);
73
+ return;
74
+ }
75
+
76
+ const logoPngFile = path.join(get(blocklet, 'env.dataDir'), 'logo.png');
77
+ if (fs.existsSync(logoPngFile)) {
78
+ res.sendFile(logoPngFile, sendOptions);
79
+ return;
80
+ }
81
+ }
82
+
83
+ next();
84
+ };
85
+
86
+ const ensureCustomFavicon = async (req, res, next) => {
87
+ const { blocklet, sendOptions } = req;
88
+
89
+ const customLogoFavicon = get(blocklet, 'environmentObj.BLOCKLET_APP_LOGO_FAVICON');
90
+ if (customLogoFavicon) {
91
+ if (customLogoFavicon.startsWith('http')) {
92
+ res.redirect(customLogoFavicon);
93
+ return;
94
+ }
95
+
96
+ const logoFile = path.join(get(blocklet, 'env.appDir'), customLogoFavicon);
97
+ if (fs.existsSync(logoFile)) {
98
+ res.sendFile(logoFile, sendOptions);
99
+ return;
100
+ }
101
+ }
102
+
103
+ next();
104
+ };
105
+
106
+ const attachSendLogoContext =
107
+ ({ onSendFallbackLogo, onGetBlocklet }) =>
108
+ async (req, res, next) => {
109
+ const sendOptions = { maxAge: '1d' };
110
+
111
+ req.sendOptions = sendOptions;
112
+
113
+ try {
114
+ req.blocklet = await onGetBlocklet({ req });
115
+ } catch (error) {
116
+ logger.error('failed to get blocklet', { url: req.url, error });
117
+ req.blocklet = null;
118
+ }
119
+
120
+ res.sendFallbackLogo = () => {
121
+ onSendFallbackLogo({ res, sendOptions });
122
+ };
123
+
124
+ next();
125
+ };
126
+
127
+ module.exports = {
128
+ attachSendLogoContext,
129
+ ensureBlockletExist,
130
+ ensureCustomSquareLogo,
131
+ ensureBundleLogo,
132
+ ensureCustomFavicon,
133
+ ensureDefaultLogo,
134
+ fallbackLogo,
135
+ cacheError,
136
+ };
package/lib/security.js CHANGED
@@ -30,8 +30,16 @@ const formatEnv = (raw, stringifyObject = true) => {
30
30
  return value;
31
31
  };
32
32
 
33
+ // https://github.com/joaquimserafim/base64-url/blob/54d9c9ede66a8724f280cf24fd18c38b9a53915f/index.js#L10
34
+ const escape = (str) => str.replace(/\+/g, '-').replace(/\//g, '_').replace(/=/g, '');
35
+ const encodeEncryptionKey = (key) => escape(Buffer.from(key).toString('base64'));
36
+ const unescape = (str) => (str + '==='.slice((str.length + 3) % 4)).replace(/-/g, '+').replace(/_/g, '/');
37
+ const decodeEncryptionKey = (str) => new Uint8Array(Buffer.from(unescape(str), 'base64'));
38
+
33
39
  module.exports = {
34
40
  encrypt,
35
41
  decrypt,
36
42
  formatEnv,
43
+ encodeEncryptionKey,
44
+ decodeEncryptionKey,
37
45
  };
package/package.json CHANGED
@@ -3,7 +3,7 @@
3
3
  "publishConfig": {
4
4
  "access": "public"
5
5
  },
6
- "version": "1.8.48",
6
+ "version": "1.8.50",
7
7
  "description": "ArcBlock's JavaScript utility",
8
8
  "main": "lib/index.js",
9
9
  "files": [
@@ -18,13 +18,13 @@
18
18
  "author": "polunzh <polunzh@gmail.com> (http://github.com/polunzh)",
19
19
  "license": "MIT",
20
20
  "dependencies": {
21
- "@abtnode/constant": "1.8.48",
22
- "@abtnode/logger": "1.8.48",
23
- "@arcblock/jwt": "^1.18.31",
24
- "@blocklet/constant": "1.8.48",
25
- "@ocap/mcrypto": "1.18.31",
26
- "@ocap/util": "1.18.31",
27
- "@ocap/wallet": "1.18.31",
21
+ "@abtnode/constant": "1.8.50",
22
+ "@abtnode/logger": "1.8.50",
23
+ "@arcblock/jwt": "^1.18.32",
24
+ "@blocklet/constant": "1.8.50",
25
+ "@ocap/mcrypto": "1.18.32",
26
+ "@ocap/util": "1.18.32",
27
+ "@ocap/wallet": "1.18.32",
28
28
  "axios": "^0.27.2",
29
29
  "axios-mock-adapter": "^1.21.2",
30
30
  "axon": "^2.0.3",
@@ -41,6 +41,7 @@
41
41
  "is-docker": "^2.2.1",
42
42
  "json-stable-stringify": "^1.0.1",
43
43
  "lodash": "^4.17.21",
44
+ "multiformats": "9.9.0",
44
45
  "p-retry": "4.6.1",
45
46
  "parallel-transform": "^1.2.0",
46
47
  "public-ip": "^4.0.4",
@@ -63,5 +64,5 @@
63
64
  "fs-extra": "^10.1.0",
64
65
  "jest": "^27.5.1"
65
66
  },
66
- "gitHead": "39979d7a059d5f97442ac7d46604391e6656ac8d"
67
+ "gitHead": "7f3ccb957659a6791b93b6fc4fbcbbd2c42f1efd"
67
68
  }