@blocklet/sdk 1.16.54-beta-20251017-133309-7d40faa6 → 1.16.54-beta-20251023-041534-36eec6b9

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.
Files changed (62) hide show
  1. package/lib/config.js +5 -5
  2. package/lib/connect/authenticator.d.ts +1 -1
  3. package/lib/connect/authenticator.js +9 -11
  4. package/lib/connect/handler.d.ts +1 -1
  5. package/lib/connect/handler.js +3 -1
  6. package/lib/connect/shared.d.ts +1 -1
  7. package/lib/connect/shared.js +17 -6
  8. package/lib/database/index.d.ts +2 -2
  9. package/lib/database/index.js +4 -2
  10. package/lib/did.d.ts +2 -2
  11. package/lib/did.js +4 -7
  12. package/lib/env.d.ts +2 -2
  13. package/lib/env.js +4 -5
  14. package/lib/index.d.ts +9 -9
  15. package/lib/index.js +18 -18
  16. package/lib/middlewares/auth.d.ts +3 -3
  17. package/lib/middlewares/auth.js +7 -8
  18. package/lib/middlewares/blocklet.d.ts +2 -2
  19. package/lib/middlewares/blocklet.js +2 -2
  20. package/lib/middlewares/csrf.js +2 -4
  21. package/lib/middlewares/fallback.d.ts +1 -1
  22. package/lib/middlewares/fallback.js +3 -1
  23. package/lib/middlewares/index.d.ts +5 -5
  24. package/lib/middlewares/index.js +15 -15
  25. package/lib/middlewares/session.d.ts +1 -1
  26. package/lib/middlewares/session.js +3 -1
  27. package/lib/middlewares/sitemap.d.ts +1 -1
  28. package/lib/middlewares/sitemap.js +3 -1
  29. package/lib/middlewares/user.d.ts +1 -1
  30. package/lib/middlewares/user.js +3 -1
  31. package/lib/security/index.d.ts +2 -2
  32. package/lib/security/index.js +3 -3
  33. package/lib/service/blocklet.d.ts +5 -2
  34. package/lib/service/blocklet.js +14 -11
  35. package/lib/service/eventbus.d.ts +2 -2
  36. package/lib/service/eventbus.js +5 -8
  37. package/lib/service/notification.d.ts +7 -6
  38. package/lib/service/notification.js +33 -29
  39. package/lib/service/signature.d.ts +27 -0
  40. package/lib/service/signature.js +112 -0
  41. package/lib/util/app-info.d.ts +1 -1
  42. package/lib/util/app-info.js +2 -2
  43. package/lib/util/check-blocklet-env.d.ts +1 -1
  44. package/lib/util/check-blocklet-env.js +4 -2
  45. package/lib/util/component-api.js +8 -4
  46. package/lib/util/csrf.d.ts +5 -0
  47. package/lib/util/csrf.js +9 -0
  48. package/lib/util/jest-setup.js +9 -4
  49. package/lib/util/jest-teardown.js +2 -2
  50. package/lib/util/send-notification.d.ts +13 -10
  51. package/lib/util/send-notification.js +42 -47
  52. package/lib/util/service-api.js +8 -4
  53. package/lib/util/verify-session.js +10 -7
  54. package/lib/util/verify-sign.d.ts +8 -7
  55. package/lib/util/verify-sign.js +11 -42
  56. package/lib/wallet-authenticator.d.ts +1 -1
  57. package/lib/wallet-authenticator.js +9 -10
  58. package/lib/wallet-handler.d.ts +1 -1
  59. package/lib/wallet-handler.js +3 -1
  60. package/lib/wallet.d.ts +35 -7
  61. package/lib/wallet.js +136 -29
  62. package/package.json +19 -18
package/lib/wallet.js CHANGED
@@ -1,55 +1,162 @@
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
- const cacheWallet = new lru_cache_1.LRUCache({ max: 4, ttl: 60 * 1000 });
12
+ exports.cacheWallet = new lru_cache_1.LRUCache({ max: 4, ttl: 60 * 1000 });
13
+ const REMOTE_CACHE_PLACEHOLDER = 'REMOTE_SIGN';
9
14
  /**
10
- * @param {string} [type=process.env.CHAIN_TYPE] can only be 'eth|ethereum' or 'default|arcblock'
11
- * @param {string} [appSk=process.env.BLOCKLET_APP_SK] must be hex
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 getWallet = (type, appSk = process.env.BLOCKLET_APP_SK) => {
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
- const currentWallet = (0, wallet_1.fromSecretKey)(sk, t);
33
- cacheWallet.set(cacheKey, currentWallet);
34
- return currentWallet;
29
+ return (0, wallet_1.fromPublicKey)(appPk, t);
35
30
  };
36
- // BLOCKLET_WALLET_TYPE is for backward compatibility
37
- getWallet.getPermanentWallet = () => getWallet(process.env.CHAIN_TYPE || process.env.BLOCKLET_WALLET_TYPE, process.env.BLOCKLET_APP_PSK);
38
- getWallet.getEthereumWallet = (permanent = false) => getWallet('ethereum', permanent ? process.env.BLOCKLET_APP_PSK : process.env.BLOCKLET_APP_SK);
39
- getWallet.getPkWallet = (type, appPk = process.env.BLOCKLET_APP_PK) => {
40
- let t;
41
- let sk;
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
+ * @param keyType - Key type to use ('sk' or 'psk')
37
+ * @returns Wallet object with remote sign and signJWT methods
38
+ */
39
+ const createRemoteWallet = (publicKey, type, keyType = 'sk') => {
40
+ if (!publicKey) {
41
+ throw new Error('Missing publicKey for creating remote wallet');
42
+ }
43
+ // Create base wallet from public key
44
+ const baseWallet = (0, wallet_1.fromPublicKey)(publicKey, type);
45
+ const remoteWallet = Object.create(baseWallet);
46
+ // Add remote sign method
47
+ // Support full wallet.sign signature: sign(data, encoding?, hashBeforeSign?)
48
+ remoteWallet.sign = async (payload, encoding, hashBeforeSign) => {
49
+ try {
50
+ const { signature } = await (0, signature_1.remoteSign)(payload, { keyType, encoding, hashBeforeSign });
51
+ if (!signature) {
52
+ throw new Error('Empty signature returned from blocklet-service');
53
+ }
54
+ return signature;
55
+ }
56
+ catch (error) {
57
+ throw new Error(`Remote signing failed: ${error.message || 'unknown error'}. Ensure blocklet-service signing API is available.`);
58
+ }
59
+ };
60
+ // Add remote signJWT method
61
+ // Match the signature: signJWT(payload?, doSign?, version?)
62
+ remoteWallet.signJWT = async (payload, doSign, version) => {
63
+ try {
64
+ const { token } = await (0, signature_1.remoteSignJWT)(payload, { doSign, version, keyType });
65
+ if (!token) {
66
+ throw new Error('Empty JWT token returned from blocklet-service');
67
+ }
68
+ return token;
69
+ }
70
+ catch (error) {
71
+ throw new Error(`Remote JWT signing failed: ${error.message || 'unknown error'}. Ensure blocklet-service signing API is available.`);
72
+ }
73
+ };
74
+ // Add remote signETH method
75
+ remoteWallet.signETH = async (data, hashBeforeSign) => {
76
+ try {
77
+ const { signature } = await (0, signature_1.remoteSignETH)(data, { hashBeforeSign, keyType });
78
+ if (!signature) {
79
+ throw new Error('Empty signature returned from blocklet-service');
80
+ }
81
+ return signature;
82
+ }
83
+ catch (error) {
84
+ throw new Error(`Remote ETH signing failed: ${error.message || 'unknown error'}. Ensure blocklet-service signing API is available.`);
85
+ }
86
+ };
87
+ return remoteWallet;
88
+ };
89
+ exports.createRemoteWallet = createRemoteWallet;
90
+ /**
91
+ * @param {string} [type=process.env.CHAIN_TYPE] can only be 'eth|ethereum' or 'default|arcblock'
92
+ * @param {string} [appSk=process.env.BLOCKLET_APP_SK] must be hex
93
+ * @param {string} [keyType='sk'] key type to use ('sk' or 'psk')
94
+ * @return {WalletObject} {WalletObject}
95
+ */
96
+ const getWallet = (type, appSk = process.env.BLOCKLET_APP_SK, keyType = 'sk') => {
42
97
  // BLOCKLET_WALLET_TYPE is for backward compatibility
43
98
  // eslint-disable-next-line no-param-reassign
44
99
  type = type || process.env.CHAIN_TYPE || process.env.BLOCKLET_WALLET_TYPE;
45
- if ((0, did_1.isEthereumType)((0, did_1.DidType)(type))) {
46
- sk = appPk.slice(0, 66);
47
- t = (0, wallet_1.WalletType)(type);
100
+ const cacheKey = [type, appSk || REMOTE_CACHE_PLACEHOLDER, keyType].join('_');
101
+ const cache = exports.cacheWallet.get(cacheKey);
102
+ if (cache)
103
+ return cache;
104
+ const walletType = (0, did_1.isEthereumType)((0, did_1.DidType)(type))
105
+ ? (0, wallet_1.WalletType)(type)
106
+ : (0, wallet_1.WalletType)({ role: mcrypto_1.types.RoleType.ROLE_APPLICATION, pk: mcrypto_1.types.KeyType.ED25519, hash: mcrypto_1.types.HashType.SHA3 });
107
+ if (!appSk) {
108
+ // Select public key based on keyType
109
+ const appPk = keyType === 'psk' ? process.env.BLOCKLET_APP_PPK : process.env.BLOCKLET_APP_PK;
110
+ if (!appPk) {
111
+ throw new Error(`Missing public key for ${keyType.toUpperCase()} wallet: BLOCKLET_APP_P${keyType.toUpperCase()}`);
112
+ }
113
+ const currentWallet = (0, exports.createRemoteWallet)(appPk, walletType, keyType);
114
+ exports.cacheWallet.set(cacheKey, currentWallet);
115
+ return currentWallet;
48
116
  }
49
- else {
50
- sk = appPk;
51
- t = (0, wallet_1.WalletType)({ role: mcrypto_1.types.RoleType.ROLE_APPLICATION, pk: mcrypto_1.types.KeyType.ED25519, hash: mcrypto_1.types.HashType.SHA3 });
117
+ const sk = (0, did_1.isEthereumType)((0, did_1.DidType)(type)) ? appSk.slice(0, 66) : appSk;
118
+ const currentWallet = (0, wallet_1.fromSecretKey)(sk, walletType);
119
+ exports.cacheWallet.set(cacheKey, currentWallet);
120
+ return currentWallet;
121
+ };
122
+ exports.getWallet = getWallet;
123
+ /**
124
+ * Create wallet from app DID with automatic fallback to remote signing
125
+ * @param sub - Subject identifier (e.g., 'email|user@example.com')
126
+ * @param type - DID type shortcut (e.g., 'ethereum')
127
+ * @param index - Index for deriving wallet (default: 0)
128
+ * @returns Wallet object with sign and signJWT methods
129
+ */
130
+ const deriveWallet = async (sub, type, index) => {
131
+ const appSk = process.env.BLOCKLET_APP_SK;
132
+ // If we have a secret key, use local fromAppDid
133
+ if (appSk) {
134
+ return (0, did_ext_1.fromAppDid)(sub, appSk, type, index);
135
+ }
136
+ // No secret key available, use remote fromAppDid
137
+ const walletJSON = await (0, signature_1.remoteDeriveWallet)(sub, type, index);
138
+ const wallet = (0, wallet_1.fromJSON)(walletJSON);
139
+ if (!(0, wallet_1.isValid)(wallet, true)) {
140
+ throw new Error('Invalid response from remote fromAppDid: wallet is invalid');
52
141
  }
53
- return (0, wallet_1.fromPublicKey)(sk, t);
142
+ return wallet;
54
143
  };
55
- module.exports = getWallet;
144
+ exports.deriveWallet = deriveWallet;
145
+ // BLOCKLET_WALLET_TYPE is for backward compatibility
146
+ const getPermanentWallet = () => (0, exports.getWallet)(process.env.CHAIN_TYPE || process.env.BLOCKLET_WALLET_TYPE, process.env.BLOCKLET_APP_PSK, 'psk');
147
+ exports.getPermanentWallet = getPermanentWallet;
148
+ const getEthereumWallet = (permanent = false) => (0, exports.getWallet)('ethereum', permanent ? process.env.BLOCKLET_APP_PSK : process.env.BLOCKLET_APP_SK, permanent ? 'psk' : 'sk');
149
+ exports.getEthereumWallet = getEthereumWallet;
150
+ const getAccessWallet = () => (0, exports.getWallet)(process.env.CHAIN_TYPE || process.env.BLOCKLET_WALLET_TYPE,
151
+ // Compatible with previous version where APP_ASK does not exist
152
+ process.env.BLOCKLET_APP_ASK || process.env.BLOCKLET_APP_SK, 'sk');
153
+ exports.getAccessWallet = getAccessWallet;
154
+ // Expose helper methods as properties of getWallet
155
+ exports.getWallet.getPermanentWallet = exports.getPermanentWallet;
156
+ exports.getWallet.getEthereumWallet = exports.getEthereumWallet;
157
+ exports.getWallet.getPkWallet = exports.getPkWallet;
158
+ exports.getWallet.deriveWallet = exports.deriveWallet;
159
+ exports.getWallet.getAccessWallet = exports.getAccessWallet;
160
+ exports.getWallet.getPermanentWallet = exports.getPermanentWallet;
161
+ exports.getWallet.getEthereumWallet = exports.getEthereumWallet;
162
+ 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-20251017-133309-7d40faa6",
6
+ "version": "1.16.54-beta-20251023-041534-36eec6b9",
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": "jest --forceExit --detectOpenHandles",
20
- "coverage": "npm run test -- --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-20251017-133309-7d40faa6",
32
- "@abtnode/db-cache": "1.16.54-beta-20251017-133309-7d40faa6",
33
- "@abtnode/util": "1.16.54-beta-20251017-133309-7d40faa6",
34
- "@arcblock/did": "1.25.6",
35
- "@arcblock/did-connect-js": "1.25.6",
36
- "@arcblock/jwt": "1.25.6",
37
- "@arcblock/ws": "1.25.6",
38
- "@blocklet/constant": "1.16.54-beta-20251017-133309-7d40faa6",
39
- "@blocklet/env": "1.16.54-beta-20251017-133309-7d40faa6",
31
+ "@abtnode/constant": "1.16.54-beta-20251023-041534-36eec6b9",
32
+ "@abtnode/db-cache": "1.16.54-beta-20251023-041534-36eec6b9",
33
+ "@abtnode/util": "1.16.54-beta-20251023-041534-36eec6b9",
34
+ "@arcblock/did": "^1.26.3",
35
+ "@arcblock/did-connect-js": "^1.26.3",
36
+ "@arcblock/did-ext": "^1.26.3",
37
+ "@arcblock/jwt": "^1.26.3",
38
+ "@arcblock/ws": "^1.26.3",
39
+ "@blocklet/constant": "1.16.54-beta-20251023-041534-36eec6b9",
40
+ "@blocklet/env": "1.16.54-beta-20251023-041534-36eec6b9",
40
41
  "@blocklet/error": "^0.2.5",
41
- "@blocklet/meta": "1.16.54-beta-20251017-133309-7d40faa6",
42
- "@blocklet/server-js": "1.16.54-beta-20251017-133309-7d40faa6",
42
+ "@blocklet/meta": "1.16.54-beta-20251023-041534-36eec6b9",
43
+ "@blocklet/server-js": "1.16.54-beta-20251023-041534-36eec6b9",
43
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.25.6",
48
- "@ocap/util": "1.25.6",
49
- "@ocap/wallet": "1.25.6",
48
+ "@ocap/mcrypto": "^1.26.3",
49
+ "@ocap/util": "^1.26.3",
50
+ "@ocap/wallet": "^1.26.3",
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": "78c11632ed47a75502a20c257568fc9e1655ca05"
90
+ "gitHead": "a642bc21c7c0f4aaccae2758d6b1d836d078bc1b"
90
91
  }