@blocklet/sdk 1.15.17 → 1.16.0-beta-b16cb035
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/README.md +1 -198
- package/lib/component/index.d.ts +35 -0
- package/lib/component/index.js +95 -0
- package/lib/config.d.ts +33 -0
- package/lib/config.js +17 -0
- package/lib/connect/authenticator.d.ts +5 -0
- package/lib/connect/authenticator.js +18 -0
- package/lib/connect/handler.d.ts +2 -0
- package/lib/connect/handler.js +90 -0
- package/lib/connect/shared.d.ts +19 -0
- package/lib/connect/shared.js +81 -0
- package/lib/database/index.d.ts +13 -0
- package/lib/database/index.js +46 -0
- package/lib/embed/adapters/express.d.ts +3 -0
- package/lib/embed/adapters/express.js +13 -0
- package/lib/embed/generate.d.ts +6 -0
- package/lib/embed/generate.js +30 -0
- package/lib/embed/get-embed-url.d.ts +1 -0
- package/lib/embed/get-embed-url.js +55 -0
- package/lib/embed/get-embed.d.ts +1 -0
- package/lib/embed/get-embed.js +32 -0
- package/lib/embed/index.d.ts +10 -0
- package/lib/embed/index.js +13 -0
- package/lib/embed/message.d.ts +26 -0
- package/lib/embed/message.js +134 -0
- package/lib/env.d.ts +17 -0
- package/lib/env.js +3 -0
- package/lib/error-handler.d.ts +0 -0
- package/lib/error-handler.js +5 -5
- package/lib/index.d.ts +53 -0
- package/lib/index.js +49 -12
- package/lib/middlewares/auth.d.ts +8 -0
- package/lib/middlewares/auth.js +54 -0
- package/lib/middlewares/component.d.ts +7 -0
- package/lib/middlewares/component.js +32 -0
- package/lib/middlewares/index.d.ts +24 -0
- package/lib/middlewares/index.js +17 -0
- package/lib/middlewares/user.d.ts +10 -0
- package/lib/middlewares/user.js +12 -0
- package/lib/security/index.d.ts +9 -0
- package/lib/security/index.js +27 -0
- package/lib/service/auth.d.ts +31 -0
- package/lib/service/auth.js +129 -136
- package/lib/service/notification.d.ts +47 -0
- package/lib/service/notification.js +182 -18
- package/lib/types/notification.d.ts +113 -0
- package/lib/types/notification.js +3 -0
- package/lib/util/check-blocklet-env.d.ts +2 -0
- package/lib/util/check-blocklet-env.js +20 -17
- package/lib/util/constants.d.ts +5 -0
- package/lib/util/constants.js +6 -2
- package/lib/util/env.d.ts +19 -0
- package/lib/util/env.js +33 -0
- package/lib/util/send-notification.d.ts +26 -0
- package/lib/util/send-notification.js +149 -38
- package/lib/validators/index.d.ts +12 -0
- package/lib/validators/index.js +13 -5
- package/lib/validators/notification.d.ts +74 -0
- package/lib/validators/notification.js +227 -69
- package/lib/version.d.ts +6 -0
- package/lib/version.js +6 -0
- package/lib/wallet-authenticator.d.ts +5 -0
- package/lib/wallet-authenticator.js +16 -51
- package/lib/wallet-handler.d.ts +19 -0
- package/lib/wallet-handler.js +109 -87
- package/lib/wallet.d.ts +3 -0
- package/lib/wallet.js +10 -0
- package/package.json +52 -19
- package/lib/database.js +0 -98
package/README.md
CHANGED
|
@@ -2,201 +2,4 @@
|
|
|
2
2
|
|
|
3
3
|
Blocklet SDK for blocklet developer
|
|
4
4
|
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
```shell
|
|
8
|
-
yarn add @blocklet/sdk
|
|
9
|
-
```
|
|
10
|
-
|
|
11
|
-
or
|
|
12
|
-
|
|
13
|
-
```shell
|
|
14
|
-
npm install @blocklet/sdk
|
|
15
|
-
```
|
|
16
|
-
|
|
17
|
-
## Auth SDK
|
|
18
|
-
|
|
19
|
-
### Usage
|
|
20
|
-
|
|
21
|
-
```javascript
|
|
22
|
-
const Auth = require('@blocklet/sdk/service/auth');
|
|
23
|
-
|
|
24
|
-
const client = new Auth();
|
|
25
|
-
|
|
26
|
-
const userDid = 'xxxxxxxx';
|
|
27
|
-
|
|
28
|
-
const { user } = await client.getUser(userDid);
|
|
29
|
-
```
|
|
30
|
-
|
|
31
|
-
### Api
|
|
32
|
-
|
|
33
|
-
#### client.getUser(did)
|
|
34
|
-
|
|
35
|
-
Get user by user did
|
|
36
|
-
|
|
37
|
-
- **did** `string`
|
|
38
|
-
|
|
39
|
-
#### client.getUsers()
|
|
40
|
-
|
|
41
|
-
Get all users of the team
|
|
42
|
-
|
|
43
|
-
#### client.updateUserRole(did, role)
|
|
44
|
-
|
|
45
|
-
- **did** `string`
|
|
46
|
-
- **role** `string`
|
|
47
|
-
|
|
48
|
-
#### client.getPermissionsByRole(role)
|
|
49
|
-
|
|
50
|
-
Get all permissions of a role
|
|
51
|
-
|
|
52
|
-
- **role** `string`
|
|
53
|
-
|
|
54
|
-
#### client.getRoles()
|
|
55
|
-
|
|
56
|
-
Get all roles of the team
|
|
57
|
-
|
|
58
|
-
#### client.getPermissions()
|
|
59
|
-
|
|
60
|
-
Get all permissions of the team
|
|
61
|
-
|
|
62
|
-
## Notification SDK
|
|
63
|
-
|
|
64
|
-
### Usage
|
|
65
|
-
|
|
66
|
-
```javascript
|
|
67
|
-
const Notification = require('@blocklet/sdk/service/notification');
|
|
68
|
-
|
|
69
|
-
const userDid = 'xxxxxxxx';
|
|
70
|
-
|
|
71
|
-
const notification = {
|
|
72
|
-
title: 'xxx',
|
|
73
|
-
body: 'xxx',
|
|
74
|
-
attachments: [
|
|
75
|
-
{
|
|
76
|
-
type: 'asset',
|
|
77
|
-
data: {
|
|
78
|
-
did: 'xxx',
|
|
79
|
-
chainHost: 'https://chainhost',
|
|
80
|
-
},
|
|
81
|
-
},
|
|
82
|
-
],
|
|
83
|
-
actions: [
|
|
84
|
-
{
|
|
85
|
-
name: 'xxx',
|
|
86
|
-
title: 'Go To Website',
|
|
87
|
-
link: 'https://arcblock.io',
|
|
88
|
-
},
|
|
89
|
-
],
|
|
90
|
-
};
|
|
91
|
-
|
|
92
|
-
const content = { message: 'this is a message' };
|
|
93
|
-
const actions = [];
|
|
94
|
-
|
|
95
|
-
await Notification.sendToUser(userDid, notification);
|
|
96
|
-
|
|
97
|
-
await Notification.sendToUser(userDid, [notification, anotherNotification]);
|
|
98
|
-
await Notification.sendToUser([userDid, anotherUserDid], notification);
|
|
99
|
-
await Notification.sendToUser([userDid, anotherUserDid], [notification, anotherNotification]);
|
|
100
|
-
```
|
|
101
|
-
|
|
102
|
-
### Api
|
|
103
|
-
|
|
104
|
-
#### notification.sendToUser(receiver, notification)
|
|
105
|
-
|
|
106
|
-
Send notification to an account
|
|
107
|
-
|
|
108
|
-
- **receiver** `string | array<string>` required
|
|
109
|
-
- **notification** `object | array<object>` required
|
|
110
|
-
- **notification.title** `string`
|
|
111
|
-
- **notification.body** `string`
|
|
112
|
-
- **notification.attachments** `array<object>`
|
|
113
|
-
- **attachment.type** `enum` 'asset', 'vc', 'token' required
|
|
114
|
-
- **attachment.data** `object`
|
|
115
|
-
- _type: text_
|
|
116
|
-
- **type** `string`
|
|
117
|
-
- **message** `string`
|
|
118
|
-
- _type: asset_
|
|
119
|
-
- **did** `string` did
|
|
120
|
-
- **chainHost** `string` uri
|
|
121
|
-
- _type: vc_
|
|
122
|
-
- **credential** `object`
|
|
123
|
-
- **tag** `string`
|
|
124
|
-
- _type: token_
|
|
125
|
-
- **address** `string` did
|
|
126
|
-
- **amount** `string`
|
|
127
|
-
- **symbol** `string`
|
|
128
|
-
- **senderDid** `string` did
|
|
129
|
-
- **chainHost** `string`
|
|
130
|
-
- **decimal** `integer`
|
|
131
|
-
- **notification.actions** `array<object>`
|
|
132
|
-
- **name** `string` required
|
|
133
|
-
- **title** `string`
|
|
134
|
-
- **color** `string`
|
|
135
|
-
- **bgColor** `string`
|
|
136
|
-
- **link** `string` uri
|
|
137
|
-
|
|
138
|
-
## WalletAuthenticator SDK
|
|
139
|
-
|
|
140
|
-
### Usage
|
|
141
|
-
|
|
142
|
-
```javascript
|
|
143
|
-
const { WalletAuthenticator } = require('@blocklet/sdk');
|
|
144
|
-
|
|
145
|
-
const authenticator = new WalletAuthenticator();
|
|
146
|
-
```
|
|
147
|
-
|
|
148
|
-
## WalletHandler SDK
|
|
149
|
-
|
|
150
|
-
### Usage
|
|
151
|
-
|
|
152
|
-
```javascript
|
|
153
|
-
const AuthStorage = require('@arcblock/did-auth-storage-nedb');
|
|
154
|
-
const { WalletAuthenticator, WalletHandlers } = require('@blocklet/sdk');
|
|
155
|
-
|
|
156
|
-
const authenticator = new WalletAuthenticator();
|
|
157
|
-
|
|
158
|
-
const handlers = new WalletHandlers({
|
|
159
|
-
authenticator,
|
|
160
|
-
tokenGenerator: () => Date.now().toString(),
|
|
161
|
-
tokenStorage: new AuthStorage({
|
|
162
|
-
dbPath: path.join(process.env.BLOCKLET_DATA_DIR, 'auth.db'),
|
|
163
|
-
onload: (err) => {
|
|
164
|
-
if (err) {
|
|
165
|
-
// eslint-disable-next-line no-console
|
|
166
|
-
console.error(`Failed to load database from ${path.join(process.env.BLOCKLET_DATA_DIR, 'auth.db')}`, err);
|
|
167
|
-
}
|
|
168
|
-
},
|
|
169
|
-
}),
|
|
170
|
-
});
|
|
171
|
-
```
|
|
172
|
-
|
|
173
|
-
## Database SDK
|
|
174
|
-
|
|
175
|
-
A database library for develop blocklet, it's a wrapper of [nedb](https://www.github.com/Arcblock/nedb).
|
|
176
|
-
Supply a simpler way to use nedb. Just use `new Database([dbName])`, or you can pass a object option as second parameter to create a database as origin nedb way `new Database([dbName], [options])`
|
|
177
|
-
|
|
178
|
-
Supply full-promise support.
|
|
179
|
-
|
|
180
|
-
### Usage
|
|
181
|
-
|
|
182
|
-
```javascript
|
|
183
|
-
const { Database } = require('@blocklet/sdk');
|
|
184
|
-
|
|
185
|
-
(async () => {
|
|
186
|
-
const db1 = new Database('db1');
|
|
187
|
-
const data1 = await db1.find().skip(1).limit(10);
|
|
188
|
-
|
|
189
|
-
class MyDatabase extends Database {
|
|
190
|
-
constructor(name) {
|
|
191
|
-
super(name);
|
|
192
|
-
}
|
|
193
|
-
|
|
194
|
-
async extraFn() {
|
|
195
|
-
return 'extra';
|
|
196
|
-
}
|
|
197
|
-
}
|
|
198
|
-
const db2 = new MyDatabase('db2');
|
|
199
|
-
const data2 = await db2.find().paginate(1, 10);
|
|
200
|
-
const data2Extra = await db2.extraFn();
|
|
201
|
-
})();
|
|
202
|
-
```
|
|
5
|
+
See [https://developer.blocklet.io/docs/](https://developer.blocklet.io/docs/)
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import { AxiosResponse } from 'axios';
|
|
2
|
+
export type MountPoint = {
|
|
3
|
+
did: string;
|
|
4
|
+
name: string;
|
|
5
|
+
title: string;
|
|
6
|
+
port: number;
|
|
7
|
+
webEndpoint: string;
|
|
8
|
+
mountPoint: string;
|
|
9
|
+
};
|
|
10
|
+
declare const getChildWebEndpoint: (name: string) => string;
|
|
11
|
+
declare const getParentWebEndpoint: () => string;
|
|
12
|
+
declare const getComponentWebEndpoint: (keyword: string) => string;
|
|
13
|
+
declare const call: ({ name, path: _path, data }: {
|
|
14
|
+
name?: string;
|
|
15
|
+
path: string;
|
|
16
|
+
data: any;
|
|
17
|
+
}) => Promise<AxiosResponse<any, any>>;
|
|
18
|
+
declare const getComponentMountPoint: (keyword: string) => string;
|
|
19
|
+
export { call };
|
|
20
|
+
export { getComponentMountPoint };
|
|
21
|
+
export { getComponentWebEndpoint };
|
|
22
|
+
export { getChildWebEndpoint };
|
|
23
|
+
export { getParentWebEndpoint };
|
|
24
|
+
declare const _default: {
|
|
25
|
+
call: ({ name, path: _path, data }: {
|
|
26
|
+
name?: string;
|
|
27
|
+
path: string;
|
|
28
|
+
data: any;
|
|
29
|
+
}) => Promise<AxiosResponse<any, any>>;
|
|
30
|
+
getComponentMountPoint: (keyword: string) => string;
|
|
31
|
+
getComponentWebEndpoint: (keyword: string) => string;
|
|
32
|
+
getChildWebEndpoint: (name: string) => string;
|
|
33
|
+
getParentWebEndpoint: () => string;
|
|
34
|
+
};
|
|
35
|
+
export default _default;
|
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.getParentWebEndpoint = exports.getChildWebEndpoint = exports.getComponentWebEndpoint = exports.getComponentMountPoint = exports.call = void 0;
|
|
7
|
+
const axios_1 = __importDefault(require("axios"));
|
|
8
|
+
const get_1 = __importDefault(require("lodash/get"));
|
|
9
|
+
const url_join_1 = __importDefault(require("url-join"));
|
|
10
|
+
const json_stable_stringify_1 = __importDefault(require("json-stable-stringify"));
|
|
11
|
+
const util_1 = require("@blocklet/meta/lib/util");
|
|
12
|
+
const wallet_1 = __importDefault(require("../wallet"));
|
|
13
|
+
const config_1 = require("../config");
|
|
14
|
+
const parseMountPoints = () => {
|
|
15
|
+
const mountPoints = JSON.parse(process.env.BLOCKLET_MOUNT_POINTS) || [];
|
|
16
|
+
mountPoints.forEach((x) => {
|
|
17
|
+
if (x.port) {
|
|
18
|
+
x.webEndpoint = `http://127.0.0.1:${x.port}`;
|
|
19
|
+
}
|
|
20
|
+
});
|
|
21
|
+
return mountPoints;
|
|
22
|
+
};
|
|
23
|
+
const sign = (data) => {
|
|
24
|
+
const wallet = (0, wallet_1.default)();
|
|
25
|
+
const signData = typeof data === 'undefined' ? {} : data;
|
|
26
|
+
return { 'x-component-sig': wallet.sign((0, json_stable_stringify_1.default)(signData)) };
|
|
27
|
+
};
|
|
28
|
+
const doCall = async ({ url, data }) => {
|
|
29
|
+
try {
|
|
30
|
+
const resp = await axios_1.default.post(url, data, { headers: sign(data), timeout: 60000 });
|
|
31
|
+
config_1.logger.info(`call ${url} api success`);
|
|
32
|
+
return resp;
|
|
33
|
+
}
|
|
34
|
+
catch (error) {
|
|
35
|
+
config_1.logger.error(`call ${url} api failed`, {
|
|
36
|
+
url,
|
|
37
|
+
responseStatus: (0, get_1.default)(error, 'response.status'),
|
|
38
|
+
responseData: (0, get_1.default)(error, 'response.data'),
|
|
39
|
+
error,
|
|
40
|
+
});
|
|
41
|
+
throw new Error(`call ${url} api failed`);
|
|
42
|
+
}
|
|
43
|
+
};
|
|
44
|
+
const parsePorts = () => JSON.parse(process.env.BLOCKLET_WEB_PORTS);
|
|
45
|
+
const getWebEndpoint = (name) => {
|
|
46
|
+
const ports = parsePorts();
|
|
47
|
+
if (ports[name]) {
|
|
48
|
+
return `http://127.0.0.1:${ports[name]}`;
|
|
49
|
+
}
|
|
50
|
+
return '';
|
|
51
|
+
};
|
|
52
|
+
const getChildWebEndpoint = (name) => {
|
|
53
|
+
const fullName = `${process.env.BLOCKLET_REAL_NAME}/${name}`;
|
|
54
|
+
return getWebEndpoint(fullName);
|
|
55
|
+
};
|
|
56
|
+
exports.getChildWebEndpoint = getChildWebEndpoint;
|
|
57
|
+
const getParentWebEndpoint = () => {
|
|
58
|
+
const parentName = (0, util_1.getParentComponentName)(process.env.BLOCKLET_REAL_NAME);
|
|
59
|
+
return getWebEndpoint(parentName);
|
|
60
|
+
};
|
|
61
|
+
exports.getParentWebEndpoint = getParentWebEndpoint;
|
|
62
|
+
const getComponentWebEndpoint = (keyword) => {
|
|
63
|
+
const mountPoints = parseMountPoints();
|
|
64
|
+
const item = mountPoints.find((x) => [x.title, x.name, x.did].includes(keyword));
|
|
65
|
+
return item ? item.webEndpoint : '';
|
|
66
|
+
};
|
|
67
|
+
exports.getComponentWebEndpoint = getComponentWebEndpoint;
|
|
68
|
+
const call = async ({ name, path: _path, data }) => {
|
|
69
|
+
let baseURL;
|
|
70
|
+
if (name) {
|
|
71
|
+
baseURL = getComponentWebEndpoint(name) || getChildWebEndpoint(name);
|
|
72
|
+
}
|
|
73
|
+
else {
|
|
74
|
+
baseURL = getParentWebEndpoint();
|
|
75
|
+
}
|
|
76
|
+
if (!baseURL) {
|
|
77
|
+
throw new Error(`can not find web endpoint for ${name}`);
|
|
78
|
+
}
|
|
79
|
+
const url = (0, url_join_1.default)(baseURL, _path);
|
|
80
|
+
return doCall({ url, data });
|
|
81
|
+
};
|
|
82
|
+
exports.call = call;
|
|
83
|
+
const getComponentMountPoint = (keyword) => {
|
|
84
|
+
const mountPoints = parseMountPoints();
|
|
85
|
+
const item = mountPoints.find((x) => [x.title, x.name, x.did].includes(keyword));
|
|
86
|
+
return item ? item.mountPoint : '';
|
|
87
|
+
};
|
|
88
|
+
exports.getComponentMountPoint = getComponentMountPoint;
|
|
89
|
+
exports.default = {
|
|
90
|
+
call,
|
|
91
|
+
getComponentMountPoint,
|
|
92
|
+
getComponentWebEndpoint,
|
|
93
|
+
getChildWebEndpoint,
|
|
94
|
+
getParentWebEndpoint,
|
|
95
|
+
};
|
package/lib/config.d.ts
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
export declare const logger: {
|
|
2
|
+
info: {
|
|
3
|
+
(...data: any[]): void;
|
|
4
|
+
(message?: any, ...optionalParams: any[]): void;
|
|
5
|
+
};
|
|
6
|
+
debug: (...args: any[]) => void;
|
|
7
|
+
warn: {
|
|
8
|
+
(...data: any[]): void;
|
|
9
|
+
(message?: any, ...optionalParams: any[]): void;
|
|
10
|
+
};
|
|
11
|
+
error: {
|
|
12
|
+
(...data: any[]): void;
|
|
13
|
+
(message?: any, ...optionalParams: any[]): void;
|
|
14
|
+
};
|
|
15
|
+
};
|
|
16
|
+
declare const _default: {
|
|
17
|
+
logger: {
|
|
18
|
+
info: {
|
|
19
|
+
(...data: any[]): void;
|
|
20
|
+
(message?: any, ...optionalParams: any[]): void;
|
|
21
|
+
};
|
|
22
|
+
debug: (...args: any[]) => void;
|
|
23
|
+
warn: {
|
|
24
|
+
(...data: any[]): void;
|
|
25
|
+
(message?: any, ...optionalParams: any[]): void;
|
|
26
|
+
};
|
|
27
|
+
error: {
|
|
28
|
+
(...data: any[]): void;
|
|
29
|
+
(message?: any, ...optionalParams: any[]): void;
|
|
30
|
+
};
|
|
31
|
+
};
|
|
32
|
+
};
|
|
33
|
+
export default _default;
|
package/lib/config.js
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.logger = void 0;
|
|
4
|
+
/* eslint-disable no-console */
|
|
5
|
+
exports.logger = {
|
|
6
|
+
info: console.log,
|
|
7
|
+
debug: (...args) => {
|
|
8
|
+
if (process.env.NODE_ENV === 'development') {
|
|
9
|
+
console.debug(...args);
|
|
10
|
+
}
|
|
11
|
+
},
|
|
12
|
+
warn: console.warn,
|
|
13
|
+
error: console.error,
|
|
14
|
+
};
|
|
15
|
+
exports.default = {
|
|
16
|
+
logger: exports.logger,
|
|
17
|
+
};
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
const authenticator_1 = require("@did-connect/authenticator");
|
|
6
|
+
const wallet_1 = __importDefault(require("../wallet"));
|
|
7
|
+
const check_blocklet_env_1 = __importDefault(require("../util/check-blocklet-env"));
|
|
8
|
+
const shared_1 = require("./shared");
|
|
9
|
+
class BlockletAuthenticator extends authenticator_1.Authenticator {
|
|
10
|
+
constructor(options = {}) {
|
|
11
|
+
(0, check_blocklet_env_1.default)();
|
|
12
|
+
super({
|
|
13
|
+
wallet: (0, wallet_1.default)(),
|
|
14
|
+
...(0, shared_1.getAuthenticatorProps)(options),
|
|
15
|
+
});
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
module.exports = BlockletAuthenticator;
|
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
+
}) : function(o, v) {
|
|
16
|
+
o["default"] = v;
|
|
17
|
+
});
|
|
18
|
+
var __importStar = (this && this.__importStar) || function (mod) {
|
|
19
|
+
if (mod && mod.__esModule) return mod;
|
|
20
|
+
var result = {};
|
|
21
|
+
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
|
22
|
+
__setModuleDefault(result, mod);
|
|
23
|
+
return result;
|
|
24
|
+
};
|
|
25
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
26
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
27
|
+
};
|
|
28
|
+
const get_1 = __importDefault(require("lodash/get"));
|
|
29
|
+
const handler_1 = require("@did-connect/handler");
|
|
30
|
+
const notification_1 = __importStar(require("../service/notification"));
|
|
31
|
+
const noop = () => ({});
|
|
32
|
+
// whether app web page is in mobile DID wallet
|
|
33
|
+
const inMobileWallet = (didwallet) => {
|
|
34
|
+
return didwallet && ['ios', 'android'].includes(didwallet.os);
|
|
35
|
+
};
|
|
36
|
+
const consoleLogger = { info: noop, error: console.error, warn: console.warn, debug: noop };
|
|
37
|
+
const getConnectedDid = (session) => {
|
|
38
|
+
if (session.autoConnect === false) {
|
|
39
|
+
return '';
|
|
40
|
+
}
|
|
41
|
+
if (inMobileWallet(session.didwallet)) {
|
|
42
|
+
return '';
|
|
43
|
+
}
|
|
44
|
+
return (0, get_1.default)(session, 'previousConnected.userDid', '');
|
|
45
|
+
};
|
|
46
|
+
const createConnectHandlers = ({ authenticator, storage, logger = consoleLogger, socketPathname, sendNotificationFn, }) => {
|
|
47
|
+
const handlers = (0, handler_1.createHandlers)({
|
|
48
|
+
storage,
|
|
49
|
+
authenticator,
|
|
50
|
+
logger,
|
|
51
|
+
socketPathname,
|
|
52
|
+
});
|
|
53
|
+
const originCreateHandler = handlers.handleSessionCreate;
|
|
54
|
+
handlers.handleSessionCreate = async (context) => {
|
|
55
|
+
const session = await originCreateHandler(context);
|
|
56
|
+
const connectedDid = getConnectedDid(session);
|
|
57
|
+
// send notification to wallet to trigger wallet to auto connect
|
|
58
|
+
if (connectedDid) {
|
|
59
|
+
// wallet use check url to check status of the session
|
|
60
|
+
let checkUrl = '';
|
|
61
|
+
try {
|
|
62
|
+
// @ts-expect-error TS(2322) FIXME: Type 'URL' is not assignable to type 'string'.
|
|
63
|
+
checkUrl = new URL(session.authUrl);
|
|
64
|
+
checkUrl.pathname = checkUrl.pathname.replace(/\/auth/, '/session');
|
|
65
|
+
}
|
|
66
|
+
catch (e) {
|
|
67
|
+
checkUrl = '';
|
|
68
|
+
console.error(e);
|
|
69
|
+
}
|
|
70
|
+
const deepLink = new URL('https://abtwallet.io/i/');
|
|
71
|
+
deepLink.searchParams.set('action', 'requestAuth');
|
|
72
|
+
deepLink.searchParams.set('url', encodeURIComponent(session.authUrl));
|
|
73
|
+
const message = {
|
|
74
|
+
type: 'connect',
|
|
75
|
+
url: deepLink.href,
|
|
76
|
+
};
|
|
77
|
+
if (checkUrl) {
|
|
78
|
+
message.checkUrl = checkUrl.href;
|
|
79
|
+
}
|
|
80
|
+
// sendNotificationFn maybe custom function so we need params
|
|
81
|
+
const sendFn = sendNotificationFn || notification_1.sendToUser.bind(notification_1.default);
|
|
82
|
+
sendFn(connectedDid, message, { ...context, session }).catch((err) => {
|
|
83
|
+
console.error(err);
|
|
84
|
+
});
|
|
85
|
+
}
|
|
86
|
+
return session;
|
|
87
|
+
};
|
|
88
|
+
return handlers;
|
|
89
|
+
};
|
|
90
|
+
module.exports = createConnectHandlers;
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { WalletObject } from '@ocap/wallet';
|
|
2
|
+
declare const getDelegator: () => WalletObject<string>;
|
|
3
|
+
declare const getDelegatee: () => WalletObject<string>;
|
|
4
|
+
declare const getDelegation: (delegator: WalletObject, delegatee: WalletObject) => string;
|
|
5
|
+
declare const getAuthenticatorProps: (options?: {}) => {
|
|
6
|
+
appInfo: (...args: any[]) => Promise<{
|
|
7
|
+
name: string | undefined;
|
|
8
|
+
description: string | undefined;
|
|
9
|
+
link: string;
|
|
10
|
+
icon: string;
|
|
11
|
+
updateSubEndpoint: true;
|
|
12
|
+
subscriptionEndpoint: string;
|
|
13
|
+
nodeDid: string | undefined;
|
|
14
|
+
}>;
|
|
15
|
+
delegator: () => WalletObject<string>;
|
|
16
|
+
delegation: () => string;
|
|
17
|
+
chainInfo: () => Record<string, string>;
|
|
18
|
+
};
|
|
19
|
+
export { getDelegation, getDelegator, getDelegatee, getAuthenticatorProps };
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.getAuthenticatorProps = exports.getDelegatee = exports.getDelegator = exports.getDelegation = void 0;
|
|
7
|
+
const url_join_1 = __importDefault(require("url-join"));
|
|
8
|
+
const wallet_1 = __importDefault(require("@blocklet/meta/lib/wallet"));
|
|
9
|
+
const jwt_1 = require("@arcblock/jwt");
|
|
10
|
+
const util_1 = require("@blocklet/meta/lib/util");
|
|
11
|
+
const constants_1 = require("../util/constants");
|
|
12
|
+
// wraps value in closure or returns closure
|
|
13
|
+
const closure = (value) => (typeof value === 'function' ? value : () => value);
|
|
14
|
+
const getDelegator = () => {
|
|
15
|
+
const { BLOCKLET_APP_SK, BLOCKLET_APP_PSK, BLOCKLET_WALLET_TYPE } = process.env;
|
|
16
|
+
if (BLOCKLET_APP_SK && BLOCKLET_APP_PSK && BLOCKLET_APP_SK !== BLOCKLET_APP_PSK) {
|
|
17
|
+
return (0, wallet_1.default)(BLOCKLET_APP_PSK, undefined, BLOCKLET_WALLET_TYPE);
|
|
18
|
+
}
|
|
19
|
+
return null;
|
|
20
|
+
};
|
|
21
|
+
exports.getDelegator = getDelegator;
|
|
22
|
+
const getDelegatee = () => {
|
|
23
|
+
const { BLOCKLET_APP_SK, BLOCKLET_WALLET_TYPE } = process.env;
|
|
24
|
+
return (0, wallet_1.default)(BLOCKLET_APP_SK, undefined, BLOCKLET_WALLET_TYPE);
|
|
25
|
+
};
|
|
26
|
+
exports.getDelegatee = getDelegatee;
|
|
27
|
+
const getDelegation = (delegator, delegatee) => {
|
|
28
|
+
return (0, jwt_1.signV2)(delegator.address, delegator.secretKey, {
|
|
29
|
+
agentDid: `did:abt:${delegatee.address}`,
|
|
30
|
+
permissions: [
|
|
31
|
+
{
|
|
32
|
+
role: 'DIDConnectAgent',
|
|
33
|
+
claims: [
|
|
34
|
+
'authPrincipal',
|
|
35
|
+
'profile',
|
|
36
|
+
'signature',
|
|
37
|
+
'prepareTx',
|
|
38
|
+
'agreement',
|
|
39
|
+
'verifiableCredential',
|
|
40
|
+
'asset',
|
|
41
|
+
'keyPair',
|
|
42
|
+
'encryptionKey',
|
|
43
|
+
],
|
|
44
|
+
},
|
|
45
|
+
],
|
|
46
|
+
exp: Math.floor(new Date().getTime() / 1000) + 60 * 60, // valid for 1 hour
|
|
47
|
+
});
|
|
48
|
+
};
|
|
49
|
+
exports.getDelegation = getDelegation;
|
|
50
|
+
const getAuthenticatorProps = (options = {}) => ({
|
|
51
|
+
chainInfo: () => (0, util_1.getChainInfo)(process.env),
|
|
52
|
+
...options,
|
|
53
|
+
appInfo: async (...args) => {
|
|
54
|
+
const info = await closure(options.appInfo)(...args);
|
|
55
|
+
const { request, baseUrl } = args[0];
|
|
56
|
+
const groupPathPrefix = request.headers['x-group-path-prefix'] || '/';
|
|
57
|
+
return {
|
|
58
|
+
name: process.env.BLOCKLET_APP_NAME,
|
|
59
|
+
description: process.env.BLOCKLET_APP_DESCRIPTION,
|
|
60
|
+
...(info || {}),
|
|
61
|
+
link: (0, util_1.getConnectAppUrl)(args[0]),
|
|
62
|
+
icon: (0, url_join_1.default)(baseUrl, constants_1.SERVICE_PREFIX, `/blocklet/logo?v=${process.env.BLOCKLET_APP_VERSION || ''}`),
|
|
63
|
+
updateSubEndpoint: true,
|
|
64
|
+
subscriptionEndpoint: (0, url_join_1.default)(groupPathPrefix, constants_1.SERVICE_PREFIX, 'websocket'),
|
|
65
|
+
nodeDid: process.env.ABT_NODE_DID,
|
|
66
|
+
};
|
|
67
|
+
},
|
|
68
|
+
delegator: () => {
|
|
69
|
+
const delegator = getDelegator();
|
|
70
|
+
return delegator;
|
|
71
|
+
},
|
|
72
|
+
delegation: () => {
|
|
73
|
+
const delegator = getDelegator();
|
|
74
|
+
if (delegator) {
|
|
75
|
+
const delegatee = getDelegatee();
|
|
76
|
+
return getDelegation(delegator, delegatee);
|
|
77
|
+
}
|
|
78
|
+
return null;
|
|
79
|
+
},
|
|
80
|
+
});
|
|
81
|
+
exports.getAuthenticatorProps = getAuthenticatorProps;
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { DataStore as BaseStore, DataStoreOptions } from '@nedb/core';
|
|
2
|
+
declare class DataBase extends BaseStore {
|
|
3
|
+
constructor(name: string, options?: DataStoreOptions);
|
|
4
|
+
exists(...args: any[]): Promise<boolean>;
|
|
5
|
+
paginate({ condition, sort, page, size, projection }: {
|
|
6
|
+
condition?: {};
|
|
7
|
+
sort?: {};
|
|
8
|
+
page?: number;
|
|
9
|
+
size?: number;
|
|
10
|
+
projection?: {};
|
|
11
|
+
}): Promise<import("@nedb/core").Row<import("@nedb/core").AnyObject>[]>;
|
|
12
|
+
}
|
|
13
|
+
export = DataBase;
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
const fs_extra_1 = __importDefault(require("fs-extra"));
|
|
6
|
+
const path_1 = __importDefault(require("path"));
|
|
7
|
+
const core_1 = require("@nedb/core");
|
|
8
|
+
class DataBase extends core_1.DataStore {
|
|
9
|
+
constructor(name, options = {}) {
|
|
10
|
+
if (!process.env.BLOCKLET_DATA_DIR) {
|
|
11
|
+
throw new Error('Blocklet SDK must be used in blocklet runtime');
|
|
12
|
+
}
|
|
13
|
+
const DB_DIR = path_1.default.join(process.env.BLOCKLET_DATA_DIR, 'db');
|
|
14
|
+
fs_extra_1.default.ensureDirSync(DB_DIR);
|
|
15
|
+
const filename = options.filename || path_1.default.join(DB_DIR, `${name}.db`);
|
|
16
|
+
super({
|
|
17
|
+
filename,
|
|
18
|
+
autoload: true,
|
|
19
|
+
timestampData: true,
|
|
20
|
+
onload: (err) => {
|
|
21
|
+
if (err) {
|
|
22
|
+
console.error(`failed to load disk database ${filename}`, err);
|
|
23
|
+
}
|
|
24
|
+
},
|
|
25
|
+
...options,
|
|
26
|
+
});
|
|
27
|
+
// @ts-ignore
|
|
28
|
+
const compactInterval = typeof options.compactInterval === 'undefined' ? 60 * 60 * 1000 : +options.compactInterval;
|
|
29
|
+
if (compactInterval > 0) {
|
|
30
|
+
this.persistence.setAutoCompactionInterval(compactInterval, console.warn);
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
async exists(...args) {
|
|
34
|
+
const doc = await this.findOne(...args);
|
|
35
|
+
return !!doc;
|
|
36
|
+
}
|
|
37
|
+
async paginate({ condition = {}, sort = {}, page = 1, size = 100, projection = {} }) {
|
|
38
|
+
return this.cursor(condition)
|
|
39
|
+
.sort(sort)
|
|
40
|
+
.skip(Math.max(page * size - size, 0))
|
|
41
|
+
.limit(Math.max(size, 1))
|
|
42
|
+
.projection(projection)
|
|
43
|
+
.exec();
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
module.exports = DataBase;
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
const get_embed_1 = __importDefault(require("../get-embed"));
|
|
7
|
+
async function expressEmbed(req, res) {
|
|
8
|
+
const { query = {} } = req;
|
|
9
|
+
const { url = '' } = query;
|
|
10
|
+
const data = await (0, get_embed_1.default)(url);
|
|
11
|
+
res.json(data);
|
|
12
|
+
}
|
|
13
|
+
exports.default = expressEmbed;
|