@blocklet/sdk 1.8.2 → 1.8.5

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.
@@ -0,0 +1,16 @@
1
+ const { Authenticator } = require('@did-connect/authenticator');
2
+
3
+ const getWallet = require('../wallet');
4
+ const checkBlockletEnv = require('../util/check-blocklet-env');
5
+ const getShared = require('./shared');
6
+
7
+ module.exports = class BlockletAuthenticator extends Authenticator {
8
+ constructor(options = {}) {
9
+ checkBlockletEnv();
10
+
11
+ super({
12
+ wallet: getWallet(),
13
+ ...getShared(options),
14
+ });
15
+ }
16
+ };
@@ -0,0 +1,73 @@
1
+ const get = require('lodash/get');
2
+ const { createHandlers } = require('@did-connect/handler');
3
+ const Notification = require('../service/notification');
4
+
5
+ const noop = () => ({});
6
+
7
+ // whether app web page is in mobile DID wallet
8
+ const inMobileWallet = (didwallet) => {
9
+ return didwallet && ['ios', 'android'].includes(didwallet.os);
10
+ };
11
+
12
+ const consoleLogger = { info: noop, error: console.error, warn: console.warn, debug: noop };
13
+ const getConnectedDid = (session) => {
14
+ if (session.autoConnect === false) {
15
+ return '';
16
+ }
17
+
18
+ if (inMobileWallet(session.didwallet)) {
19
+ return '';
20
+ }
21
+
22
+ return get(session, 'previousConnected.userDid', '');
23
+ };
24
+
25
+ module.exports = ({ authenticator, storage, logger = consoleLogger, socketPathname, sendNotificationFn }) => {
26
+ const handlers = createHandlers({
27
+ storage,
28
+ authenticator,
29
+ logger,
30
+ socketPathname,
31
+ });
32
+
33
+ const originCreateHandler = handlers.handleSessionCreate;
34
+ handlers.handleSessionCreate = async (context) => {
35
+ const session = await originCreateHandler(context);
36
+ const connectedDid = getConnectedDid(session);
37
+
38
+ // send notification to wallet to trigger wallet to auto connect
39
+ if (connectedDid) {
40
+ // wallet use check url to check status of the session
41
+ let checkUrl = '';
42
+ try {
43
+ checkUrl = new URL(session.authUrl);
44
+ checkUrl.pathname = checkUrl.pathname.replace(/\/auth/, '/session');
45
+ } catch (e) {
46
+ checkUrl = '';
47
+ console.error(e);
48
+ }
49
+
50
+ const deepLink = new URL('https://abtwallet.io/i/');
51
+ deepLink.searchParams.set('action', 'requestAuth');
52
+ deepLink.searchParams.set('url', encodeURIComponent(session.authUrl));
53
+ const message = {
54
+ type: 'connect',
55
+ url: deepLink.href,
56
+ };
57
+
58
+ if (checkUrl) {
59
+ message.checkUrl = checkUrl.href;
60
+ }
61
+
62
+ // sendNotificationFn maybe custom function so we need params
63
+ const sendFn = sendNotificationFn || Notification.sendToUser.bind(Notification);
64
+ sendFn(connectedDid, message, { ...context, session }).catch((err) => {
65
+ console.error(err);
66
+ });
67
+ }
68
+
69
+ return session;
70
+ };
71
+
72
+ return handlers;
73
+ };
@@ -0,0 +1,34 @@
1
+ const joinUrl = require('url-join');
2
+ const { getConnectAppUrl } = require('@blocklet/meta/lib/util');
3
+ const { SERVICE_PREFIX } = require('../util/constants');
4
+
5
+ // wraps value in closure or returns closure
6
+ const closure = (value) => (typeof value === 'function' ? value : () => value);
7
+
8
+ module.exports = (options = {}) => ({
9
+ chainInfo: () => {
10
+ const chainHost = process.env.CHAIN_HOST;
11
+ return chainHost ? { host: chainHost, id: 'chain' } : { host: 'none', id: 'none' };
12
+ },
13
+
14
+ ...options,
15
+
16
+ appInfo: async (...args) => {
17
+ const info = await closure(options.appInfo)(...args);
18
+
19
+ const { request, baseUrl } = args[0];
20
+ const groupPathPrefix = request.headers['x-group-path-prefix'] || '/';
21
+ const blockletDid = request.headers['x-blocklet-did'];
22
+
23
+ return {
24
+ name: process.env.BLOCKLET_APP_NAME,
25
+ description: process.env.BLOCKLET_APP_DESCRIPTION,
26
+ ...(info || {}),
27
+ link: getConnectAppUrl(args[0]),
28
+ icon: joinUrl(baseUrl, SERVICE_PREFIX, `/blocklet/logo/${blockletDid}`),
29
+ updateSubEndpoint: true,
30
+ subscriptionEndpoint: joinUrl(groupPathPrefix, SERVICE_PREFIX, 'websocket'),
31
+ nodeDid: process.env.ABT_NODE_DID,
32
+ };
33
+ },
34
+ });
package/lib/env.js CHANGED
@@ -1,3 +1,24 @@
1
+ const { getParentComponentName } = require('@blocklet/meta/lib/util');
2
+
3
+ const parsePorts = () => JSON.parse(process.env.BLOCKLET_WEB_PORTS);
4
+
5
+ const getWebEndpoint = (name) => {
6
+ const ports = parsePorts();
7
+ if (ports[name]) {
8
+ return `http://127.0.0.1:${ports[name]}`;
9
+ }
10
+ return '';
11
+ };
12
+
13
+ const getChildWebEndpoint = (name) => {
14
+ const fullName = `${process.env.BLOCKLET_REAL_NAME}/${name}`;
15
+ return getWebEndpoint(fullName);
16
+ };
17
+ const getParentWebEndpoint = () => {
18
+ const parentName = getParentComponentName(process.env.BLOCKLET_REAL_NAME);
19
+ return getWebEndpoint(parentName);
20
+ };
21
+
1
22
  module.exports = Object.freeze({
2
23
  appId: process.env.BLOCKLET_APP_ID,
3
24
  appName: process.env.BLOCKLET_APP_NAME,
@@ -6,4 +27,7 @@ module.exports = Object.freeze({
6
27
  isComponent: process.env.BLOCKLET_DID !== process.env.BLOCKLET_REAL_DID,
7
28
  dataDir: process.env.BLOCKLET_DATA_DIR,
8
29
  cacheDir: process.env.BLOCKLET_CACHE_DIR,
30
+ getWebEndpoint,
31
+ getChildWebEndpoint,
32
+ getParentWebEndpoint,
9
33
  });
package/lib/index.js CHANGED
@@ -2,6 +2,8 @@ const AuthService = require('./service/auth');
2
2
  const NotificationService = require('./service/notification');
3
3
  const WalletAuthenticator = require('./wallet-authenticator');
4
4
  const WalletHandlers = require('./wallet-handler');
5
+ const BlockletAuthenticator = require('./connect/authenticator');
6
+ const createConnectHandlers = require('./connect/handler');
5
7
  const Database = require('./database');
6
8
  const env = require('./env');
7
9
  const middlewares = require('./middlewares');
@@ -12,6 +14,8 @@ module.exports = {
12
14
  NotificationService,
13
15
  WalletHandlers,
14
16
  WalletAuthenticator,
17
+ BlockletAuthenticator,
18
+ createConnectHandlers,
15
19
  Database,
16
20
  getWallet,
17
21
  env,
@@ -1,14 +1,8 @@
1
- const joinUrl = require('url-join');
2
1
  const { WalletAuthenticator: Authenticator } = require('@arcblock/did-auth');
3
2
 
4
3
  const getWallet = require('./wallet');
5
4
  const checkBlockletEnv = require('./util/check-blocklet-env');
6
- const { SERVICE_PREFIX } = require('./util/constants');
7
-
8
- const env = require('./env');
9
-
10
- // wraps value in closure or returns closure
11
- const closure = (value) => (typeof value === 'function' ? value : () => value);
5
+ const getShared = require('./connect/shared');
12
6
 
13
7
  class WalletAuthenticator extends Authenticator {
14
8
  constructor(options = {}) {
@@ -16,39 +10,7 @@ class WalletAuthenticator extends Authenticator {
16
10
 
17
11
  super({
18
12
  wallet: getWallet().toJSON(),
19
-
20
- chainInfo: () => {
21
- const chainHost = process.env.CHAIN_HOST;
22
- return chainHost ? { host: chainHost, id: 'chain' } : { host: 'none', id: 'none' };
23
- },
24
-
25
- ...options,
26
-
27
- appInfo: async (...args) => {
28
- const info = await closure(options.appInfo)(...args);
29
-
30
- const { request, baseUrl } = args[0];
31
- const pathPrefix = request.headers['x-path-prefix'] || '/';
32
- const groupPathPrefix = request.headers['x-group-path-prefix'] || '/';
33
- const blockletDid = request.headers['x-blocklet-did'];
34
-
35
- // Child blocklets should set appInfo.link to exactly the same path as the root blocklet
36
- let appUrl = baseUrl;
37
- if (env.isComponent && pathPrefix !== groupPathPrefix) {
38
- appUrl = joinUrl(joinUrl(appUrl, '/').replace(pathPrefix, groupPathPrefix), '');
39
- }
40
-
41
- return {
42
- name: process.env.BLOCKLET_APP_NAME,
43
- description: process.env.BLOCKLET_APP_DESCRIPTION,
44
- ...(info || {}),
45
- link: appUrl,
46
- icon: joinUrl(baseUrl, SERVICE_PREFIX, `/blocklet/logo/${blockletDid}`),
47
- updateSubEndpoint: true,
48
- subscriptionEndpoint: joinUrl(groupPathPrefix, SERVICE_PREFIX, 'websocket'),
49
- nodeDid: process.env.ABT_NODE_DID,
50
- };
51
- },
13
+ ...getShared(options),
52
14
  });
53
15
  }
54
16
  }
package/package.json CHANGED
@@ -3,7 +3,7 @@
3
3
  "publishConfig": {
4
4
  "access": "public"
5
5
  },
6
- "version": "1.8.2",
6
+ "version": "1.8.5",
7
7
  "description": "graphql client to read/write data on abt node",
8
8
  "main": "lib/index.js",
9
9
  "files": [
@@ -19,15 +19,17 @@
19
19
  "author": "linchen1987 <linchen.1987@foxmail.com> (http://github.com/linchen1987)",
20
20
  "license": "MIT",
21
21
  "dependencies": {
22
- "@abtnode/client": "1.8.2",
23
- "@abtnode/constant": "1.8.2",
24
- "@arcblock/did-auth": "1.17.2",
25
- "@arcblock/jwt": "1.17.2",
26
- "@arcblock/ws": "1.17.2",
27
- "@blocklet/meta": "1.8.2",
28
- "@nedb/core": "^1.3.1",
29
- "@ocap/mcrypto": "1.17.2",
30
- "@ocap/wallet": "1.17.2",
22
+ "@abtnode/client": "1.8.5",
23
+ "@abtnode/constant": "1.8.5",
24
+ "@arcblock/did-auth": "1.17.6",
25
+ "@arcblock/jwt": "1.17.6",
26
+ "@arcblock/ws": "1.17.6",
27
+ "@blocklet/meta": "1.8.5",
28
+ "@did-connect/authenticator": "^2.0.18",
29
+ "@did-connect/handler": "^2.0.18",
30
+ "@nedb/core": "^1.3.2",
31
+ "@ocap/mcrypto": "1.17.6",
32
+ "@ocap/wallet": "1.17.6",
31
33
  "axios": "^0.27.2",
32
34
  "fs-extra": "^10.0.1",
33
35
  "joi": "^17.6.0",
@@ -39,5 +41,5 @@
39
41
  "detect-port": "^1.3.0",
40
42
  "jest": "^27.4.5"
41
43
  },
42
- "gitHead": "fcbe3c97f3825c507ee16714f49bbf8f58c5b59f"
44
+ "gitHead": "58da88581cdf1dd45fd50e56db08a055be82626e"
43
45
  }