@blocklet/did-space-js 0.5.58
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 +77 -0
- package/dist/client/index.d.ts +40 -0
- package/dist/client/index.js +110 -0
- package/dist/commands/backup/backup-blocklet.d.ts +14 -0
- package/dist/commands/backup/backup-blocklet.js +83 -0
- package/dist/commands/backup/index.d.ts +3 -0
- package/dist/commands/backup/index.js +19 -0
- package/dist/commands/backup/post-app-backup.d.ts +9 -0
- package/dist/commands/backup/post-app-backup.js +49 -0
- package/dist/commands/backup/put-app-backup.d.ts +9 -0
- package/dist/commands/backup/put-app-backup.js +49 -0
- package/dist/commands/base.d.ts +21 -0
- package/dist/commands/base.js +47 -0
- package/dist/commands/incremental-backup/backup-blocklet.d.ts +14 -0
- package/dist/commands/incremental-backup/backup-blocklet.js +90 -0
- package/dist/commands/incremental-backup/index.d.ts +1 -0
- package/dist/commands/incremental-backup/index.js +17 -0
- package/dist/commands/incremental-sync/index.d.ts +2 -0
- package/dist/commands/incremental-sync/index.js +18 -0
- package/dist/commands/incremental-sync/sync-base.d.ts +76 -0
- package/dist/commands/incremental-sync/sync-base.js +236 -0
- package/dist/commands/incremental-sync/sync-push.d.ts +30 -0
- package/dist/commands/incremental-sync/sync-push.js +202 -0
- package/dist/commands/index.d.ts +9 -0
- package/dist/commands/index.js +25 -0
- package/dist/commands/nft/index.d.ts +1 -0
- package/dist/commands/nft/index.js +17 -0
- package/dist/commands/nft/put-nft-object.d.ts +20 -0
- package/dist/commands/nft/put-nft-object.js +143 -0
- package/dist/commands/object/delete-object.d.ts +7 -0
- package/dist/commands/object/delete-object.js +38 -0
- package/dist/commands/object/get-object.d.ts +7 -0
- package/dist/commands/object/get-object.js +43 -0
- package/dist/commands/object/index.d.ts +5 -0
- package/dist/commands/object/index.js +21 -0
- package/dist/commands/object/list-object.d.ts +7 -0
- package/dist/commands/object/list-object.js +31 -0
- package/dist/commands/object/list-objects.d.ts +7 -0
- package/dist/commands/object/list-objects.js +44 -0
- package/dist/commands/object/put-object.d.ts +7 -0
- package/dist/commands/object/put-object.js +61 -0
- package/dist/commands/restore/index.d.ts +3 -0
- package/dist/commands/restore/index.js +19 -0
- package/dist/commands/restore/post-app-restore.d.ts +7 -0
- package/dist/commands/restore/post-app-restore.js +45 -0
- package/dist/commands/restore/put-app-restore.d.ts +9 -0
- package/dist/commands/restore/put-app-restore.js +49 -0
- package/dist/commands/restore/restore-blocklet.d.ts +14 -0
- package/dist/commands/restore/restore-blocklet.js +80 -0
- package/dist/commands/space/head.d.ts +8 -0
- package/dist/commands/space/head.js +52 -0
- package/dist/commands/space/index.d.ts +1 -0
- package/dist/commands/space/index.js +17 -0
- package/dist/commands/sync/index.d.ts +3 -0
- package/dist/commands/sync/index.js +19 -0
- package/dist/commands/sync/sync-folder-base.d.ts +105 -0
- package/dist/commands/sync/sync-folder-base.js +319 -0
- package/dist/commands/sync/sync-folder-pull.d.ts +13 -0
- package/dist/commands/sync/sync-folder-pull.js +151 -0
- package/dist/commands/sync/sync-folder-push.d.ts +20 -0
- package/dist/commands/sync/sync-folder-push.js +166 -0
- package/dist/did-document/did-document.d.ts +29 -0
- package/dist/did-document/did-document.js +2 -0
- package/dist/did-document/index.d.ts +3 -0
- package/dist/did-document/index.js +19 -0
- package/dist/did-document/sign-document.d.ts +3 -0
- package/dist/did-document/sign-document.js +13 -0
- package/dist/did-document/verify-document.d.ts +10 -0
- package/dist/did-document/verify-document.js +35 -0
- package/dist/index.d.ts +7 -0
- package/dist/index.js +23 -0
- package/dist/libs/api.d.ts +3 -0
- package/dist/libs/api.js +65 -0
- package/dist/libs/did.d.ts +9 -0
- package/dist/libs/did.js +34 -0
- package/dist/libs/encoding.d.ts +7 -0
- package/dist/libs/encoding.js +17 -0
- package/dist/libs/error.d.ts +4 -0
- package/dist/libs/error.js +27 -0
- package/dist/libs/hash.d.ts +1 -0
- package/dist/libs/hash.js +5 -0
- package/dist/libs/index.d.ts +11 -0
- package/dist/libs/index.js +27 -0
- package/dist/libs/logger.d.ts +2 -0
- package/dist/libs/logger.js +12 -0
- package/dist/libs/object-integrality.d.ts +1 -0
- package/dist/libs/object-integrality.js +13 -0
- package/dist/libs/performance.d.ts +7 -0
- package/dist/libs/performance.js +13 -0
- package/dist/libs/sleep.d.ts +1 -0
- package/dist/libs/sleep.js +9 -0
- package/dist/libs/space.d.ts +16 -0
- package/dist/libs/space.js +56 -0
- package/dist/libs/stream.d.ts +3 -0
- package/dist/libs/stream.js +10 -0
- package/dist/meta/backup/backup-blocklet-command.d.ts +50 -0
- package/dist/meta/backup/backup-blocklet-command.js +2 -0
- package/dist/meta/backup/index.d.ts +3 -0
- package/dist/meta/backup/index.js +19 -0
- package/dist/meta/backup/post-app-backup-command.d.ts +8 -0
- package/dist/meta/backup/post-app-backup-command.js +16 -0
- package/dist/meta/backup/put-app-backup-command.d.ts +7 -0
- package/dist/meta/backup/put-app-backup-command.js +18 -0
- package/dist/meta/incremental-backup/backup-blocklet-command.d.ts +50 -0
- package/dist/meta/incremental-backup/backup-blocklet-command.js +2 -0
- package/dist/meta/incremental-backup/index.d.ts +1 -0
- package/dist/meta/incremental-backup/index.js +17 -0
- package/dist/meta/index.d.ts +7 -0
- package/dist/meta/index.js +23 -0
- package/dist/meta/nft/index.d.ts +1 -0
- package/dist/meta/nft/index.js +17 -0
- package/dist/meta/nft/put-nft-object-command.d.ts +51 -0
- package/dist/meta/nft/put-nft-object-command.js +2 -0
- package/dist/meta/object/delete-object-command.d.ts +6 -0
- package/dist/meta/object/delete-object-command.js +2 -0
- package/dist/meta/object/get-object-command.d.ts +10 -0
- package/dist/meta/object/get-object-command.js +2 -0
- package/dist/meta/object/head-object-command.d.ts +13 -0
- package/dist/meta/object/head-object-command.js +2 -0
- package/dist/meta/object/index.d.ts +5 -0
- package/dist/meta/object/index.js +21 -0
- package/dist/meta/object/list-object-command.d.ts +6 -0
- package/dist/meta/object/list-object-command.js +2 -0
- package/dist/meta/object/list-objects-command.d.ts +8 -0
- package/dist/meta/object/list-objects-command.js +2 -0
- package/dist/meta/object/put-object-command.d.ts +29 -0
- package/dist/meta/object/put-object-command.js +2 -0
- package/dist/meta/restore/index.d.ts +3 -0
- package/dist/meta/restore/index.js +19 -0
- package/dist/meta/restore/post-app-restore-command.d.ts +8 -0
- package/dist/meta/restore/post-app-restore-command.js +13 -0
- package/dist/meta/restore/put-app-restore-command.d.ts +7 -0
- package/dist/meta/restore/put-app-restore-command.js +18 -0
- package/dist/meta/restore/restore-blocklet-command.d.ts +28 -0
- package/dist/meta/restore/restore-blocklet-command.js +2 -0
- package/dist/meta/spaces-client-options.d.ts +5 -0
- package/dist/meta/spaces-client-options.js +2 -0
- package/dist/meta/sync/incremental-sync-base-command.d.ts +7 -0
- package/dist/meta/sync/incremental-sync-base-command.js +2 -0
- package/dist/meta/sync/incremental-sync-push-command.d.ts +8 -0
- package/dist/meta/sync/incremental-sync-push-command.js +2 -0
- package/dist/meta/sync/index.d.ts +6 -0
- package/dist/meta/sync/index.js +22 -0
- package/dist/meta/sync/sync-base-command.d.ts +121 -0
- package/dist/meta/sync/sync-base-command.js +2 -0
- package/dist/meta/sync/sync-folder-base-command.d.ts +12 -0
- package/dist/meta/sync/sync-folder-base-command.js +2 -0
- package/dist/meta/sync/sync-folder-pull-command.d.ts +5 -0
- package/dist/meta/sync/sync-folder-pull-command.js +2 -0
- package/dist/meta/sync/sync-folder-push-command.d.ts +7 -0
- package/dist/meta/sync/sync-folder-push-command.js +2 -0
- package/dist/protocol/command.protocol.d.ts +68 -0
- package/dist/protocol/command.protocol.js +2 -0
- package/dist/protocol/index.d.ts +1 -0
- package/dist/protocol/index.js +17 -0
- package/dist/security/index.d.ts +1 -0
- package/dist/security/index.js +17 -0
- package/dist/security/request-signature.d.ts +32 -0
- package/dist/security/request-signature.js +140 -0
- package/package.json +78 -0
|
@@ -0,0 +1,140 @@
|
|
|
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.verifyDelegation = exports.verifyRequest = exports.signRequest = void 0;
|
|
7
|
+
const jwt_1 = require("@arcblock/jwt");
|
|
8
|
+
const isEmpty_1 = __importDefault(require("lodash/isEmpty"));
|
|
9
|
+
const isObject_1 = __importDefault(require("lodash/isObject"));
|
|
10
|
+
const isString_1 = __importDefault(require("lodash/isString"));
|
|
11
|
+
const isUndefined_1 = __importDefault(require("lodash/isUndefined"));
|
|
12
|
+
const json_stable_stringify_1 = __importDefault(require("json-stable-stringify"));
|
|
13
|
+
const did_1 = require("@arcblock/did");
|
|
14
|
+
const mcrypto_1 = require("@ocap/mcrypto");
|
|
15
|
+
const wallet_1 = require("@ocap/wallet");
|
|
16
|
+
const is_url_1 = __importDefault(require("is-url"));
|
|
17
|
+
const debug_1 = __importDefault(require("debug"));
|
|
18
|
+
const logger_1 = require("../libs/logger");
|
|
19
|
+
const debug = (0, debug_1.default)('request-signature');
|
|
20
|
+
// verifyDelegation to get actual appDid
|
|
21
|
+
function verifyDelegation(delegation, delegatee) {
|
|
22
|
+
const { from, to, userPk, permissions } = (0, jwt_1.decode)(delegation);
|
|
23
|
+
if (!(0, jwt_1.verify)(delegation, userPk)) {
|
|
24
|
+
throw new Error('delegation is not valid');
|
|
25
|
+
}
|
|
26
|
+
if (!(0, did_1.isFromPublicKey)(from, userPk)) {
|
|
27
|
+
throw new Error(`delegation.userPk(${userPk}) does not match with delegator(${from})`);
|
|
28
|
+
}
|
|
29
|
+
if (to !== delegatee.address) {
|
|
30
|
+
throw new Error(`delegation.to(${to}) is not delegatee(${delegatee.address})`);
|
|
31
|
+
}
|
|
32
|
+
if (!Array.isArray(permissions) || permissions.length === 0) {
|
|
33
|
+
throw new Error('delegation has no permissions');
|
|
34
|
+
}
|
|
35
|
+
if (!permissions.some((item) => item.role === 'DIDSpaceAgent')) {
|
|
36
|
+
throw new Error('delegation has no DIDSpaceAgent permission');
|
|
37
|
+
}
|
|
38
|
+
return from;
|
|
39
|
+
}
|
|
40
|
+
exports.verifyDelegation = verifyDelegation;
|
|
41
|
+
/**
|
|
42
|
+
* @FIXME 这还不是最终的解决方案,目前需要完善边界的情况,需要更多的时间 @jianchao
|
|
43
|
+
* @description
|
|
44
|
+
* @param {{ url: string }} { url }
|
|
45
|
+
* @return {string} {string}
|
|
46
|
+
*/
|
|
47
|
+
function getSignUrl({ url }) {
|
|
48
|
+
let signUrl = url;
|
|
49
|
+
if ((0, is_url_1.default)(encodeURI(url))) {
|
|
50
|
+
const Url = new URL(url);
|
|
51
|
+
signUrl = `${Url.pathname}${Url.search}`;
|
|
52
|
+
debug('getSignUrl', { signUrl });
|
|
53
|
+
}
|
|
54
|
+
return signUrl;
|
|
55
|
+
}
|
|
56
|
+
function signRequest({ url, method, data, headers, wallet, delegation, }) {
|
|
57
|
+
if ((0, isEmpty_1.default)(url)) {
|
|
58
|
+
throw new Error(`url(${url}) cannot be empty`);
|
|
59
|
+
}
|
|
60
|
+
if (!(0, isObject_1.default)(data)) {
|
|
61
|
+
throw new Error(`data(${data}) is not an object`);
|
|
62
|
+
}
|
|
63
|
+
if (delegation) {
|
|
64
|
+
verifyDelegation(delegation, wallet);
|
|
65
|
+
}
|
|
66
|
+
const signUrl = getSignUrl({ url });
|
|
67
|
+
// @ts-expect-error
|
|
68
|
+
const signData = (0, isString_1.default)(data?.data) ? data : {};
|
|
69
|
+
const token = (0, jwt_1.sign)(wallet.address, wallet.secretKey, {
|
|
70
|
+
digest: mcrypto_1.Hasher.SHA3.hash256((0, json_stable_stringify_1.default)({
|
|
71
|
+
url: signUrl,
|
|
72
|
+
method,
|
|
73
|
+
data: signData,
|
|
74
|
+
})),
|
|
75
|
+
// 请求在一个小时内都是不过期的
|
|
76
|
+
exp: String(Math.floor(Date.now() / 1000) + 60 * 60),
|
|
77
|
+
});
|
|
78
|
+
return {
|
|
79
|
+
url,
|
|
80
|
+
method,
|
|
81
|
+
data,
|
|
82
|
+
headers: Object.assign(headers ?? {}, {
|
|
83
|
+
'x-app-did': wallet.address,
|
|
84
|
+
'x-app-pk': wallet.publicKey,
|
|
85
|
+
'x-app-token': token,
|
|
86
|
+
'x-app-delegation': delegation || '',
|
|
87
|
+
}),
|
|
88
|
+
};
|
|
89
|
+
}
|
|
90
|
+
exports.signRequest = signRequest;
|
|
91
|
+
function verifyRequest({ url, method, data, headers, }) {
|
|
92
|
+
let appDid = headers['x-app-did'];
|
|
93
|
+
const appPk = headers['x-app-pk'];
|
|
94
|
+
const appToken = headers['x-app-token'];
|
|
95
|
+
const delegation = headers['x-app-delegation'];
|
|
96
|
+
if ((0, isUndefined_1.default)(appDid)) {
|
|
97
|
+
throw new Error('x-app-did must be present in request headers');
|
|
98
|
+
}
|
|
99
|
+
if ((0, isUndefined_1.default)(appPk)) {
|
|
100
|
+
throw new Error('x-app-pk must be present in request headers');
|
|
101
|
+
}
|
|
102
|
+
if ((0, isUndefined_1.default)(appToken)) {
|
|
103
|
+
throw new Error('x-app-token must be present in request headers');
|
|
104
|
+
}
|
|
105
|
+
if (!(0, did_1.isValid)(appDid)) {
|
|
106
|
+
throw new Error(`appDid(${appDid}) must be a valid did`);
|
|
107
|
+
}
|
|
108
|
+
if (!(0, did_1.isFromPublicKey)(appDid, appPk)) {
|
|
109
|
+
throw new Error(`appDid(${appDid}) and appPk(${appPk}) do not match`);
|
|
110
|
+
}
|
|
111
|
+
if (!(0, jwt_1.verify)(appToken, appPk)) {
|
|
112
|
+
throw new Error(`appToken(${appToken}) is invalid`);
|
|
113
|
+
}
|
|
114
|
+
if (delegation) {
|
|
115
|
+
appDid = verifyDelegation(delegation, (0, wallet_1.fromPublicKey)(appPk, (0, did_1.toTypeInfo)(appDid)));
|
|
116
|
+
}
|
|
117
|
+
const payload = (0, jwt_1.decode)(appToken, true);
|
|
118
|
+
if ((0, isUndefined_1.default)(payload.digest)) {
|
|
119
|
+
throw new Error('payload.digest must exists');
|
|
120
|
+
}
|
|
121
|
+
const verifyData = (0, isString_1.default)(data?.data) ? data : {};
|
|
122
|
+
const signUrl = url;
|
|
123
|
+
const expectedDigest = mcrypto_1.Hasher.SHA3.hash256((0, json_stable_stringify_1.default)({
|
|
124
|
+
url: signUrl,
|
|
125
|
+
method,
|
|
126
|
+
data: verifyData,
|
|
127
|
+
}));
|
|
128
|
+
if (payload.digest !== expectedDigest) {
|
|
129
|
+
logger_1.logger.info('verifyRequest', {
|
|
130
|
+
url,
|
|
131
|
+
signUrl,
|
|
132
|
+
method,
|
|
133
|
+
data,
|
|
134
|
+
headers,
|
|
135
|
+
});
|
|
136
|
+
throw new Error('payload.digest do not match');
|
|
137
|
+
}
|
|
138
|
+
return appDid;
|
|
139
|
+
}
|
|
140
|
+
exports.verifyRequest = verifyRequest;
|
package/package.json
ADDED
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@blocklet/did-space-js",
|
|
3
|
+
"version": "0.5.58",
|
|
4
|
+
"publishConfig": {
|
|
5
|
+
"access": "public"
|
|
6
|
+
},
|
|
7
|
+
"description": "Decentralized file space managed by DID",
|
|
8
|
+
"author": {
|
|
9
|
+
"name": "arcblock",
|
|
10
|
+
"email": "blocklet@arcblock.io",
|
|
11
|
+
"url": "https://github.com/blocklet"
|
|
12
|
+
},
|
|
13
|
+
"homepage": "",
|
|
14
|
+
"license": "ISC",
|
|
15
|
+
"main": "dist/index.js",
|
|
16
|
+
"typings": "dist/index.d.ts",
|
|
17
|
+
"files": [
|
|
18
|
+
"dist"
|
|
19
|
+
],
|
|
20
|
+
"scripts": {
|
|
21
|
+
"lint": "eslint src tests",
|
|
22
|
+
"lint:fix": "npm run lint -- --fix",
|
|
23
|
+
"test": "jest --forceExit --detectOpenHandles",
|
|
24
|
+
"coverage": "npm run test -- --coverage",
|
|
25
|
+
"prebuild": "rm -fr dist",
|
|
26
|
+
"build": "tsc",
|
|
27
|
+
"watch": "npm run build -- --watch",
|
|
28
|
+
"verify": "npm run lint && npm run test && npm run build",
|
|
29
|
+
"link": "yarn link"
|
|
30
|
+
},
|
|
31
|
+
"dependencies": {
|
|
32
|
+
"@arcblock/did": "^1.18.136",
|
|
33
|
+
"@arcblock/jwt": "^1.18.136",
|
|
34
|
+
"@arcblock/validator": "^1.18.136",
|
|
35
|
+
"@blocklet/env": "1.16.32",
|
|
36
|
+
"@did-space/core": "0.5.58",
|
|
37
|
+
"@ocap/mcrypto": "^1.18.136",
|
|
38
|
+
"@ocap/util": "^1.18.136",
|
|
39
|
+
"@ocap/wallet": "^1.18.136",
|
|
40
|
+
"agentkeepalive": "^4.5.0",
|
|
41
|
+
"axios": "^1.7.7",
|
|
42
|
+
"base64-url": "^2.3.3",
|
|
43
|
+
"dayjs": "^1.11.13",
|
|
44
|
+
"debug": "^4.3.7",
|
|
45
|
+
"filehound": "^1.17.6",
|
|
46
|
+
"form-data": "^4.0.1",
|
|
47
|
+
"fs-extra": "^11.2.0",
|
|
48
|
+
"got": "11.8.6",
|
|
49
|
+
"hasha": "^5.2.2",
|
|
50
|
+
"is-url": "^1.2.4",
|
|
51
|
+
"joi": "^17.13.3",
|
|
52
|
+
"json-stable-stringify": "^1.1.1",
|
|
53
|
+
"lodash": "^4.17.21",
|
|
54
|
+
"mime-types": "^2.1.35",
|
|
55
|
+
"p-all": "3.0.0",
|
|
56
|
+
"p-queue": "6.6.2",
|
|
57
|
+
"ufo": "^1.5.4",
|
|
58
|
+
"xbytes": "^1.9.1"
|
|
59
|
+
},
|
|
60
|
+
"devDependencies": {
|
|
61
|
+
"@arcblock/eslint-config-ts": "^0.3.3",
|
|
62
|
+
"@types/fs-extra": "^9.0.13",
|
|
63
|
+
"@types/jest": "^28.1.8",
|
|
64
|
+
"@types/js-yaml": "^4.0.9",
|
|
65
|
+
"@types/json-stable-stringify": "^1.1.0",
|
|
66
|
+
"@types/lodash": "^4.17.12",
|
|
67
|
+
"@types/mime-types": "^2.1.4",
|
|
68
|
+
"@types/node": "18.19.31",
|
|
69
|
+
"@types/valid-url": "^1.0.7",
|
|
70
|
+
"@typescript-eslint/eslint-plugin": "^7.18.0",
|
|
71
|
+
"eslint": "^8.57.1",
|
|
72
|
+
"jest": "^28.1.3",
|
|
73
|
+
"lint-staged": "^13.3.0",
|
|
74
|
+
"ts-jest": "^28.0.8",
|
|
75
|
+
"typescript": "^4.9.5"
|
|
76
|
+
},
|
|
77
|
+
"gitHead": "3c6d028ee695f85769ce2d257687bbf24d6f9de8"
|
|
78
|
+
}
|