@blocklet/sdk 1.5.0 → 1.5.4

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.
@@ -4,6 +4,11 @@ const { didExtension } = require('@blocklet/meta/lib/extension');
4
4
 
5
5
  const Joi = JOI.extend(didExtension);
6
6
 
7
+ const TYPES = {
8
+ NOTIFICATION: 'notification',
9
+ CONNECT: 'connect',
10
+ };
11
+
7
12
  const assetSchema = Joi.object({
8
13
  did: Joi.DID().trim().required(),
9
14
  chainHost: Joi.string().uri().required(),
@@ -39,13 +44,22 @@ const attachmentSchema = Joi.object({
39
44
  .concat(Joi.object().when('type', { is: 'token', then: tokenSchema })),
40
45
  }).required();
41
46
 
42
- const notificationSchema = Joi.object({
47
+ const notificationTypeSchema = Joi.object({
48
+ type: Joi.string().valid(TYPES.NOTIFICATION),
43
49
  title: Joi.string(),
44
50
  body: Joi.string(),
45
51
  attachments: Joi.array().items(attachmentSchema).default([]),
46
52
  actions: Joi.array().items(actionSchema).default([]),
47
53
  }).required();
48
54
 
55
+ const connectTypeSchema = Joi.object({
56
+ type: Joi.string().valid(TYPES.CONNECT),
57
+ url: Joi.string().uri().required(),
58
+ checkUrl: Joi.string().uri(),
59
+ }).required();
60
+
61
+ const notificationSchema = Joi.alternatives().try(notificationTypeSchema, connectTypeSchema).required();
62
+
49
63
  const receiverSchema = Joi.DID().trim().required();
50
64
 
51
65
  const inputNotificationSchema = Joi.alternatives()
@@ -0,0 +1,85 @@
1
+ const get = require('lodash/get');
2
+ const { WalletHandlers: Handler } = require('@arcblock/did-auth');
3
+ const Notification = require('./service/notification');
4
+
5
+ const noop = () => ({});
6
+
7
+ const CONNECTED_DID_KEY = 'headers[x-connected-did]';
8
+
9
+ // whether app web page is in mobile DID wallet
10
+ const inMobileWallet = (abtwallet) => {
11
+ return abtwallet && ['ios', 'android'].includes(abtwallet.os);
12
+ };
13
+
14
+ class WalletHandlers extends Handler {
15
+ /**
16
+ * @param {boolean} autoConnect enable auto connect to wallet (wallet does not need to scan qr code)
17
+ * @param {boolean} connectedDidOnly only current login did or connected did can connect
18
+ */
19
+ constructor({ autoConnect = true, connectedDidOnly = false, options = {}, ...opts }) {
20
+ if (autoConnect && connectedDidOnly) {
21
+ options.sessionDidKey = CONNECTED_DID_KEY;
22
+ }
23
+
24
+ super({ options, ...opts });
25
+
26
+ this.enableConnect = !!autoConnect;
27
+ }
28
+
29
+ getConnectedDid = ({ req, abtwallet }) => {
30
+ if (!this.enableConnect) {
31
+ return null;
32
+ }
33
+
34
+ if (inMobileWallet(abtwallet)) {
35
+ return null;
36
+ }
37
+
38
+ return get(req, CONNECTED_DID_KEY);
39
+ };
40
+
41
+ attach({ onStart = noop, ...opts }) {
42
+ const _onStart = async (params) => {
43
+ const extra = (await onStart(params)) || {};
44
+
45
+ const connectedDid = this.getConnectedDid(params);
46
+
47
+ // fill extra
48
+ extra.connectedDid = connectedDid || '';
49
+ extra.saveConnect = this.enableConnect;
50
+
51
+ // send notification to wallet to trigger wallet to auto connect
52
+ if (connectedDid) {
53
+ // wallet use check url to check status of the session
54
+ let checkUrl = '';
55
+ try {
56
+ checkUrl = new URL(decodeURIComponent(new URL(params.deepLink).searchParams.get('url')));
57
+ checkUrl.pathname = checkUrl.pathname.replace(/auth$/, 'status');
58
+ } catch (e) {
59
+ checkUrl = '';
60
+ console.error(e);
61
+ }
62
+
63
+ const message = {
64
+ type: 'connect',
65
+ url: params.deepLink,
66
+ };
67
+
68
+ if (checkUrl) {
69
+ message.checkUrl = checkUrl.href;
70
+ }
71
+
72
+ Notification.sendToUser(connectedDid, message).catch((err) => console.error(err));
73
+ }
74
+
75
+ return extra;
76
+ };
77
+
78
+ super.attach({
79
+ onStart: _onStart,
80
+ ...opts,
81
+ });
82
+ }
83
+ }
84
+
85
+ module.exports = WalletHandlers;
package/package.json CHANGED
@@ -3,7 +3,7 @@
3
3
  "publishConfig": {
4
4
  "access": "public"
5
5
  },
6
- "version": "1.5.0",
6
+ "version": "1.5.4",
7
7
  "description": "graphql client to read/write data on abt node",
8
8
  "main": "lib/index.js",
9
9
  "files": [
@@ -19,12 +19,12 @@
19
19
  "author": "linchen1987 <linchen.1987@foxmail.com> (http://github.com/linchen1987)",
20
20
  "license": "MIT",
21
21
  "dependencies": {
22
- "@abtnode/client": "1.5.0",
23
- "@abtnode/constant": "1.5.0",
24
- "@arcblock/did-auth": "^1.13.16",
25
- "@blocklet/meta": "1.5.0",
26
- "@ocap/mcrypto": "^1.13.16",
27
- "@ocap/wallet": "^1.13.16",
22
+ "@abtnode/client": "1.5.4",
23
+ "@abtnode/constant": "1.5.4",
24
+ "@arcblock/did-auth": "^1.13.28",
25
+ "@blocklet/meta": "1.5.4",
26
+ "@ocap/mcrypto": "^1.13.28",
27
+ "@ocap/wallet": "^1.13.28",
28
28
  "axios": "^0.21.4",
29
29
  "joi": "^17.4.0",
30
30
  "lodash": "^4.17.21",
@@ -34,5 +34,5 @@
34
34
  "detect-port": "^1.3.0",
35
35
  "jest": "^26.4.2"
36
36
  },
37
- "gitHead": "bc78f2a10de0ccc7ae23b6b6f259c05606bad8f5"
37
+ "gitHead": "8856a7eae8ebd3e09ca11214892f57fb522f9b45"
38
38
  }