@blocklet/sdk 1.16.54-beta-20251016-050817-2fc632b8 → 1.16.54-beta-20251021-070951-25e3083c
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/config.js +5 -5
- package/lib/connect/authenticator.d.ts +1 -1
- package/lib/connect/authenticator.js +9 -11
- package/lib/connect/handler.d.ts +1 -1
- package/lib/connect/handler.js +3 -1
- package/lib/connect/shared.d.ts +1 -1
- package/lib/connect/shared.js +17 -6
- package/lib/database/index.d.ts +2 -2
- package/lib/database/index.js +4 -2
- package/lib/did.d.ts +2 -2
- package/lib/did.js +4 -7
- package/lib/env.d.ts +2 -2
- package/lib/env.js +4 -5
- package/lib/index.d.ts +9 -9
- package/lib/index.js +18 -18
- package/lib/middlewares/auth.d.ts +3 -3
- package/lib/middlewares/auth.js +7 -8
- package/lib/middlewares/blocklet.d.ts +2 -2
- package/lib/middlewares/blocklet.js +2 -2
- package/lib/middlewares/csrf.js +2 -4
- package/lib/middlewares/fallback.d.ts +1 -1
- package/lib/middlewares/fallback.js +3 -1
- package/lib/middlewares/index.d.ts +5 -5
- package/lib/middlewares/index.js +15 -15
- package/lib/middlewares/session.d.ts +1 -1
- package/lib/middlewares/session.js +3 -1
- package/lib/middlewares/sitemap.d.ts +1 -1
- package/lib/middlewares/sitemap.js +3 -1
- package/lib/middlewares/user.d.ts +1 -1
- package/lib/middlewares/user.js +3 -1
- package/lib/security/index.d.ts +2 -2
- package/lib/security/index.js +3 -3
- package/lib/service/blocklet.d.ts +1 -1
- package/lib/service/blocklet.js +10 -9
- package/lib/service/eventbus.d.ts +2 -2
- package/lib/service/eventbus.js +5 -8
- package/lib/service/notification.d.ts +7 -6
- package/lib/service/notification.js +33 -29
- package/lib/service/signature.d.ts +21 -0
- package/lib/service/signature.js +78 -0
- package/lib/util/app-info.js +2 -2
- package/lib/util/check-blocklet-env.d.ts +1 -1
- package/lib/util/check-blocklet-env.js +4 -2
- package/lib/util/component-api.js +8 -4
- package/lib/util/csrf.d.ts +5 -0
- package/lib/util/csrf.js +9 -0
- package/lib/util/jest-setup.js +2 -2
- package/lib/util/jest-teardown.js +2 -2
- package/lib/util/send-notification.d.ts +13 -10
- package/lib/util/send-notification.js +42 -47
- package/lib/util/service-api.js +8 -4
- package/lib/util/verify-session.js +10 -7
- package/lib/util/verify-sign.d.ts +8 -7
- package/lib/util/verify-sign.js +11 -42
- package/lib/wallet-authenticator.d.ts +1 -1
- package/lib/wallet-authenticator.js +9 -10
- package/lib/wallet-handler.d.ts +1 -1
- package/lib/wallet-handler.js +3 -1
- package/lib/wallet.d.ts +31 -5
- package/lib/wallet.js +129 -29
- package/package.json +20 -19
package/lib/wallet.js
CHANGED
|
@@ -1,55 +1,155 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.getAccessWallet = exports.getEthereumWallet = exports.getPermanentWallet = exports.deriveWallet = exports.getWallet = exports.createRemoteWallet = exports.getPkWallet = exports.cacheWallet = void 0;
|
|
2
4
|
const wallet_1 = require("@ocap/wallet");
|
|
3
5
|
const mcrypto_1 = require("@ocap/mcrypto");
|
|
4
6
|
const did_1 = require("@arcblock/did");
|
|
5
7
|
const lru_cache_1 = require("lru-cache");
|
|
8
|
+
const did_ext_1 = require("@arcblock/did-ext");
|
|
9
|
+
const signature_1 = require("./service/signature");
|
|
6
10
|
// NOTICE: 1 个应用的 sdk 运行时最多 4 个钱包
|
|
7
11
|
// 此处 cache 只是优化性能的手段,即使 miss 也不会造成任何错误,最终决定设置 size 为 4
|
|
8
|
-
|
|
12
|
+
exports.cacheWallet = new lru_cache_1.LRUCache({ max: 4, ttl: 60 * 1000 });
|
|
13
|
+
const REMOTE_CACHE_PLACEHOLDER = 'REMOTE_SIGN';
|
|
9
14
|
/**
|
|
10
|
-
*
|
|
11
|
-
*
|
|
12
|
-
* @return {WalletObject} {WalletObject}
|
|
15
|
+
* Create a wallet from public key
|
|
16
|
+
* Internal helper function used by both createRemoteWallet and getWallet.getPkWallet
|
|
13
17
|
*/
|
|
14
|
-
const
|
|
18
|
+
const getPkWallet = (type, appPk = process.env.BLOCKLET_APP_PK) => {
|
|
15
19
|
let t;
|
|
16
|
-
let sk;
|
|
17
20
|
// BLOCKLET_WALLET_TYPE is for backward compatibility
|
|
18
21
|
// eslint-disable-next-line no-param-reassign
|
|
19
22
|
type = type || process.env.CHAIN_TYPE || process.env.BLOCKLET_WALLET_TYPE;
|
|
20
|
-
const cacheKey = [type, appSk].join('_');
|
|
21
|
-
const cache = cacheWallet.get(cacheKey);
|
|
22
|
-
if (cache)
|
|
23
|
-
return cache;
|
|
24
23
|
if ((0, did_1.isEthereumType)((0, did_1.DidType)(type))) {
|
|
25
|
-
sk = appSk.slice(0, 66);
|
|
26
24
|
t = (0, wallet_1.WalletType)(type);
|
|
27
25
|
}
|
|
28
26
|
else {
|
|
29
|
-
sk = appSk;
|
|
30
27
|
t = (0, wallet_1.WalletType)({ role: mcrypto_1.types.RoleType.ROLE_APPLICATION, pk: mcrypto_1.types.KeyType.ED25519, hash: mcrypto_1.types.HashType.SHA3 });
|
|
31
28
|
}
|
|
32
|
-
|
|
33
|
-
cacheWallet.set(cacheKey, currentWallet);
|
|
34
|
-
return currentWallet;
|
|
29
|
+
return (0, wallet_1.fromPublicKey)(appPk, t);
|
|
35
30
|
};
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
31
|
+
exports.getPkWallet = getPkWallet;
|
|
32
|
+
/**
|
|
33
|
+
* Create a remote wallet with sign and signJWT methods that call blocklet-service
|
|
34
|
+
* @param publicKey - The public key to create the wallet from
|
|
35
|
+
* @param type - The wallet type
|
|
36
|
+
* @returns Wallet object with remote sign and signJWT methods
|
|
37
|
+
*/
|
|
38
|
+
const createRemoteWallet = (publicKey, type) => {
|
|
39
|
+
if (!publicKey) {
|
|
40
|
+
throw new Error('Missing publicKey for creating remote wallet');
|
|
41
|
+
}
|
|
42
|
+
// Create base wallet from public key
|
|
43
|
+
const baseWallet = (0, wallet_1.fromPublicKey)(publicKey, type);
|
|
44
|
+
const remoteWallet = Object.create(baseWallet);
|
|
45
|
+
// Add remote sign method
|
|
46
|
+
remoteWallet.sign = async (payload) => {
|
|
47
|
+
try {
|
|
48
|
+
const { signature } = await (0, signature_1.remoteSign)(payload);
|
|
49
|
+
if (!signature) {
|
|
50
|
+
throw new Error('Empty signature returned from blocklet-service');
|
|
51
|
+
}
|
|
52
|
+
return signature;
|
|
53
|
+
}
|
|
54
|
+
catch (error) {
|
|
55
|
+
throw new Error(`Remote signing failed: ${error.message || 'unknown error'}. Ensure blocklet-service signing API is available.`);
|
|
56
|
+
}
|
|
57
|
+
};
|
|
58
|
+
// Add remote signJWT method
|
|
59
|
+
// Match the signature: signJWT(payload?, doSign?, version?)
|
|
60
|
+
remoteWallet.signJWT = async (payload, doSign, version) => {
|
|
61
|
+
try {
|
|
62
|
+
const { token } = await (0, signature_1.remoteSignJWT)(payload, { doSign, version });
|
|
63
|
+
if (!token) {
|
|
64
|
+
throw new Error('Empty JWT token returned from blocklet-service');
|
|
65
|
+
}
|
|
66
|
+
return token;
|
|
67
|
+
}
|
|
68
|
+
catch (error) {
|
|
69
|
+
throw new Error(`Remote JWT signing failed: ${error.message || 'unknown error'}. Ensure blocklet-service signing API is available.`);
|
|
70
|
+
}
|
|
71
|
+
};
|
|
72
|
+
// Add remote signETH method
|
|
73
|
+
remoteWallet.signETH = async (data, hashBeforeSign) => {
|
|
74
|
+
try {
|
|
75
|
+
const { signature } = await (0, signature_1.remoteSignETH)(data, hashBeforeSign);
|
|
76
|
+
if (!signature) {
|
|
77
|
+
throw new Error('Empty signature returned from blocklet-service');
|
|
78
|
+
}
|
|
79
|
+
return signature;
|
|
80
|
+
}
|
|
81
|
+
catch (error) {
|
|
82
|
+
throw new Error(`Remote ETH signing failed: ${error.message || 'unknown error'}. Ensure blocklet-service signing API is available.`);
|
|
83
|
+
}
|
|
84
|
+
};
|
|
85
|
+
return remoteWallet;
|
|
86
|
+
};
|
|
87
|
+
exports.createRemoteWallet = createRemoteWallet;
|
|
88
|
+
/**
|
|
89
|
+
* @param {string} [type=process.env.CHAIN_TYPE] can only be 'eth|ethereum' or 'default|arcblock'
|
|
90
|
+
* @param {string} [appSk=process.env.BLOCKLET_APP_SK] must be hex
|
|
91
|
+
* @return {WalletObject} {WalletObject}
|
|
92
|
+
*/
|
|
93
|
+
const getWallet = (type, appSk = process.env.BLOCKLET_APP_SK) => {
|
|
42
94
|
// BLOCKLET_WALLET_TYPE is for backward compatibility
|
|
43
95
|
// eslint-disable-next-line no-param-reassign
|
|
44
96
|
type = type || process.env.CHAIN_TYPE || process.env.BLOCKLET_WALLET_TYPE;
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
97
|
+
const cacheKey = [type, appSk || REMOTE_CACHE_PLACEHOLDER].join('_');
|
|
98
|
+
const cache = exports.cacheWallet.get(cacheKey);
|
|
99
|
+
if (cache)
|
|
100
|
+
return cache;
|
|
101
|
+
const walletType = (0, did_1.isEthereumType)((0, did_1.DidType)(type))
|
|
102
|
+
? (0, wallet_1.WalletType)(type)
|
|
103
|
+
: (0, wallet_1.WalletType)({ role: mcrypto_1.types.RoleType.ROLE_APPLICATION, pk: mcrypto_1.types.KeyType.ED25519, hash: mcrypto_1.types.HashType.SHA3 });
|
|
104
|
+
if (!appSk) {
|
|
105
|
+
const appPk = process.env.BLOCKLET_APP_PK;
|
|
106
|
+
const currentWallet = (0, exports.createRemoteWallet)(appPk, walletType);
|
|
107
|
+
exports.cacheWallet.set(cacheKey, currentWallet);
|
|
108
|
+
return currentWallet;
|
|
48
109
|
}
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
110
|
+
const sk = (0, did_1.isEthereumType)((0, did_1.DidType)(type)) ? appSk.slice(0, 66) : appSk;
|
|
111
|
+
const currentWallet = (0, wallet_1.fromSecretKey)(sk, walletType);
|
|
112
|
+
exports.cacheWallet.set(cacheKey, currentWallet);
|
|
113
|
+
return currentWallet;
|
|
114
|
+
};
|
|
115
|
+
exports.getWallet = getWallet;
|
|
116
|
+
/**
|
|
117
|
+
* Create wallet from app DID with automatic fallback to remote signing
|
|
118
|
+
* @param sub - Subject identifier (e.g., 'email|user@example.com')
|
|
119
|
+
* @param type - DID type shortcut (e.g., 'ethereum')
|
|
120
|
+
* @param index - Index for deriving wallet (default: 0)
|
|
121
|
+
* @returns Wallet object with sign and signJWT methods
|
|
122
|
+
*/
|
|
123
|
+
const deriveWallet = async (sub, type, index) => {
|
|
124
|
+
const appSk = process.env.BLOCKLET_APP_SK;
|
|
125
|
+
// If we have a secret key, use local fromAppDid
|
|
126
|
+
if (appSk) {
|
|
127
|
+
return (0, did_ext_1.fromAppDid)(sub, appSk, type, index);
|
|
128
|
+
}
|
|
129
|
+
// No secret key available, use remote fromAppDid
|
|
130
|
+
const { address, publicKey, type: walletType } = await (0, signature_1.remoteDeriveWallet)(sub, type, index);
|
|
131
|
+
if (!address || !publicKey) {
|
|
132
|
+
throw new Error('Invalid response from remote fromAppDid: missing address or publicKey');
|
|
52
133
|
}
|
|
53
|
-
|
|
134
|
+
// Create remote wallet with the returned public key
|
|
135
|
+
return (0, exports.createRemoteWallet)(publicKey, walletType);
|
|
54
136
|
};
|
|
55
|
-
|
|
137
|
+
exports.deriveWallet = deriveWallet;
|
|
138
|
+
// BLOCKLET_WALLET_TYPE is for backward compatibility
|
|
139
|
+
const getPermanentWallet = () => (0, exports.getWallet)(process.env.CHAIN_TYPE || process.env.BLOCKLET_WALLET_TYPE, process.env.BLOCKLET_APP_PSK);
|
|
140
|
+
exports.getPermanentWallet = getPermanentWallet;
|
|
141
|
+
const getEthereumWallet = (permanent = false) => (0, exports.getWallet)('ethereum', permanent ? process.env.BLOCKLET_APP_PSK : process.env.BLOCKLET_APP_SK);
|
|
142
|
+
exports.getEthereumWallet = getEthereumWallet;
|
|
143
|
+
const getAccessWallet = () => (0, exports.getWallet)(process.env.CHAIN_TYPE || process.env.BLOCKLET_WALLET_TYPE,
|
|
144
|
+
// Compatible with previous version where APP_ASK does not exist
|
|
145
|
+
process.env.BLOCKLET_APP_ASK || process.env.BLOCKLET_APP_SK);
|
|
146
|
+
exports.getAccessWallet = getAccessWallet;
|
|
147
|
+
// Expose helper methods as properties of getWallet
|
|
148
|
+
exports.getWallet.getPermanentWallet = exports.getPermanentWallet;
|
|
149
|
+
exports.getWallet.getEthereumWallet = exports.getEthereumWallet;
|
|
150
|
+
exports.getWallet.getPkWallet = exports.getPkWallet;
|
|
151
|
+
exports.getWallet.deriveWallet = exports.deriveWallet;
|
|
152
|
+
exports.getWallet.getAccessWallet = exports.getAccessWallet;
|
|
153
|
+
exports.getWallet.getPermanentWallet = exports.getPermanentWallet;
|
|
154
|
+
exports.getWallet.getEthereumWallet = exports.getEthereumWallet;
|
|
155
|
+
exports.getWallet.getPkWallet = exports.getPkWallet;
|
package/package.json
CHANGED
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
"publishConfig": {
|
|
4
4
|
"access": "public"
|
|
5
5
|
},
|
|
6
|
-
"version": "1.16.54-beta-
|
|
6
|
+
"version": "1.16.54-beta-20251021-070951-25e3083c",
|
|
7
7
|
"description": "graphql client to read/write data on abt node",
|
|
8
8
|
"homepage": "https://www.arcblock.io/docs/blocklet-sdk-nodejs",
|
|
9
9
|
"main": "lib/index.js",
|
|
@@ -16,8 +16,8 @@
|
|
|
16
16
|
"verify": "npm run lint:fix && npm run test && npm run build",
|
|
17
17
|
"lint": "eslint src tests service",
|
|
18
18
|
"lint:fix": "npm run lint -- --fix",
|
|
19
|
-
"test": "
|
|
20
|
-
"coverage": "
|
|
19
|
+
"test": "bun test",
|
|
20
|
+
"coverage": "bun test --coverage",
|
|
21
21
|
"clean": "rm -fr lib",
|
|
22
22
|
"prebuild": "npm run clean && npm run types",
|
|
23
23
|
"types": "rm -rf ./src/types && node tools/version.js && ts-node tools/types.js && rm -f ./src/types/index.ts && prettier --write ./src/types/notification.ts",
|
|
@@ -28,25 +28,26 @@
|
|
|
28
28
|
"author": "linchen1987 <linchen.1987@foxmail.com> (http://github.com/linchen1987)",
|
|
29
29
|
"license": "Apache-2.0",
|
|
30
30
|
"dependencies": {
|
|
31
|
-
"@abtnode/constant": "1.16.54-beta-
|
|
32
|
-
"@abtnode/db-cache": "1.16.54-beta-
|
|
33
|
-
"@abtnode/util": "1.16.54-beta-
|
|
34
|
-
"@arcblock/did": "1.
|
|
35
|
-
"@arcblock/did-connect-js": "1.
|
|
36
|
-
"@arcblock/
|
|
37
|
-
"@arcblock/
|
|
38
|
-
"@
|
|
39
|
-
"@blocklet/
|
|
31
|
+
"@abtnode/constant": "1.16.54-beta-20251021-070951-25e3083c",
|
|
32
|
+
"@abtnode/db-cache": "1.16.54-beta-20251021-070951-25e3083c",
|
|
33
|
+
"@abtnode/util": "1.16.54-beta-20251021-070951-25e3083c",
|
|
34
|
+
"@arcblock/did": "^1.26.2",
|
|
35
|
+
"@arcblock/did-connect-js": "^1.26.2",
|
|
36
|
+
"@arcblock/did-ext": "^1.26.2",
|
|
37
|
+
"@arcblock/jwt": "^1.26.2",
|
|
38
|
+
"@arcblock/ws": "^1.26.2",
|
|
39
|
+
"@blocklet/constant": "1.16.54-beta-20251021-070951-25e3083c",
|
|
40
|
+
"@blocklet/env": "1.16.54-beta-20251021-070951-25e3083c",
|
|
40
41
|
"@blocklet/error": "^0.2.5",
|
|
41
|
-
"@blocklet/meta": "1.16.54-beta-
|
|
42
|
-
"@blocklet/server-js": "1.16.54-beta-
|
|
43
|
-
"@blocklet/theme": "^3.1.
|
|
42
|
+
"@blocklet/meta": "1.16.54-beta-20251021-070951-25e3083c",
|
|
43
|
+
"@blocklet/server-js": "1.16.54-beta-20251021-070951-25e3083c",
|
|
44
|
+
"@blocklet/theme": "^3.1.51",
|
|
44
45
|
"@did-connect/authenticator": "^2.2.8",
|
|
45
46
|
"@did-connect/handler": "^2.2.8",
|
|
46
47
|
"@nedb/core": "^2.1.5",
|
|
47
|
-
"@ocap/mcrypto": "1.
|
|
48
|
-
"@ocap/util": "1.
|
|
49
|
-
"@ocap/wallet": "1.
|
|
48
|
+
"@ocap/mcrypto": "^1.26.2",
|
|
49
|
+
"@ocap/util": "^1.26.2",
|
|
50
|
+
"@ocap/wallet": "^1.26.2",
|
|
50
51
|
"axios": "^1.7.9",
|
|
51
52
|
"cheerio": "1.0.0-rc.12",
|
|
52
53
|
"debug": "^4.4.1",
|
|
@@ -86,5 +87,5 @@
|
|
|
86
87
|
"ts-node": "^10.9.1",
|
|
87
88
|
"typescript": "^5.6.3"
|
|
88
89
|
},
|
|
89
|
-
"gitHead": "
|
|
90
|
+
"gitHead": "db517e9013dbad744e38883f56e4eba1fa1615c1"
|
|
90
91
|
}
|