web-dc-api 0.0.87 → 0.0.89

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 (142) hide show
  1. package/dist/cjs/index.js +1 -0
  2. package/dist/cjs/protobuf-BVBdi7Hh.js +1 -0
  3. package/dist/dc.min.js +1 -19
  4. package/dist/esm/chunks/protobuf-CbxDm-Gy.js +1 -0
  5. package/dist/esm/index.js +1 -0
  6. package/dist/index.d.ts +947 -886
  7. package/package.json +14 -17
  8. package/dist/index.cjs.js +0 -19
  9. package/dist/index.esm.js +0 -19
  10. package/lib/common/blowfish/block.ts +0 -259
  11. package/lib/common/blowfish/cipher.ts +0 -144
  12. package/lib/common/blowfish/const.ts +0 -195
  13. package/lib/common/chain.ts +0 -469
  14. package/lib/common/commonclient.ts +0 -202
  15. package/lib/common/constants.ts +0 -55
  16. package/lib/common/dc-key/ed25519.ts +0 -343
  17. package/lib/common/dc-key/keyManager.ts +0 -424
  18. package/lib/common/dcapi.ts +0 -98
  19. package/lib/common/dcutil.ts +0 -595
  20. package/lib/common/define.ts +0 -66
  21. package/lib/common/error.ts +0 -67
  22. package/lib/common/grpc-dc.ts +0 -104
  23. package/lib/common/module-system.ts +0 -184
  24. package/lib/common/service-worker.ts +0 -234
  25. package/lib/common/types/types.ts +0 -344
  26. package/lib/dc.ts +0 -694
  27. package/lib/implements/account/client.ts +0 -185
  28. package/lib/implements/account/manager.ts +0 -683
  29. package/lib/implements/aiproxy/client.ts +0 -357
  30. package/lib/implements/aiproxy/manager.ts +0 -670
  31. package/lib/implements/cache/client.ts +0 -105
  32. package/lib/implements/cache/manager.ts +0 -127
  33. package/lib/implements/comment/client.ts +0 -982
  34. package/lib/implements/comment/manager.ts +0 -1151
  35. package/lib/implements/dc/client.ts +0 -51
  36. package/lib/implements/dc/manager.ts +0 -33
  37. package/lib/implements/file/client.ts +0 -253
  38. package/lib/implements/file/file-cache-manager.ts +0 -142
  39. package/lib/implements/file/manager.ts +0 -1240
  40. package/lib/implements/file/seekableFileStream.ts +0 -344
  41. package/lib/implements/file/streamwriter.ts +0 -322
  42. package/lib/implements/keyvalue/client.ts +0 -376
  43. package/lib/implements/keyvalue/manager.ts +0 -759
  44. package/lib/implements/message/client.ts +0 -250
  45. package/lib/implements/message/manager.ts +0 -215
  46. package/lib/implements/threaddb/cbor/coding.ts +0 -62
  47. package/lib/implements/threaddb/cbor/event.ts +0 -336
  48. package/lib/implements/threaddb/cbor/node.ts +0 -542
  49. package/lib/implements/threaddb/cbor/record.ts +0 -398
  50. package/lib/implements/threaddb/common/AsyncMutex.ts +0 -24
  51. package/lib/implements/threaddb/common/addrinfo.ts +0 -135
  52. package/lib/implements/threaddb/common/dispatcher.ts +0 -81
  53. package/lib/implements/threaddb/common/idbstore-adapter.ts +0 -260
  54. package/lib/implements/threaddb/common/json-patcher.ts +0 -204
  55. package/lib/implements/threaddb/common/key.ts +0 -290
  56. package/lib/implements/threaddb/common/level-adapter.ts +0 -235
  57. package/lib/implements/threaddb/common/lineReader.ts +0 -79
  58. package/lib/implements/threaddb/common/logstore.ts +0 -215
  59. package/lib/implements/threaddb/common/transformed-datastore.ts +0 -308
  60. package/lib/implements/threaddb/core/app.ts +0 -202
  61. package/lib/implements/threaddb/core/core.ts +0 -230
  62. package/lib/implements/threaddb/core/db.ts +0 -249
  63. package/lib/implements/threaddb/core/event.ts +0 -54
  64. package/lib/implements/threaddb/core/head.ts +0 -89
  65. package/lib/implements/threaddb/core/identity.ts +0 -171
  66. package/lib/implements/threaddb/core/logstore.ts +0 -137
  67. package/lib/implements/threaddb/core/options.ts +0 -14
  68. package/lib/implements/threaddb/core/record.ts +0 -54
  69. package/lib/implements/threaddb/db/collection.ts +0 -1910
  70. package/lib/implements/threaddb/db/db.ts +0 -698
  71. package/lib/implements/threaddb/db/json2Query.ts +0 -192
  72. package/lib/implements/threaddb/db/query.ts +0 -524
  73. package/lib/implements/threaddb/dbclient.ts +0 -443
  74. package/lib/implements/threaddb/dbmanager.ts +0 -1901
  75. package/lib/implements/threaddb/lsstoreds/addr_book.ts +0 -452
  76. package/lib/implements/threaddb/lsstoreds/cache.ts +0 -36
  77. package/lib/implements/threaddb/lsstoreds/cyclic_batch.ts +0 -87
  78. package/lib/implements/threaddb/lsstoreds/global.ts +0 -151
  79. package/lib/implements/threaddb/lsstoreds/headbook.ts +0 -280
  80. package/lib/implements/threaddb/lsstoreds/keybook.ts +0 -297
  81. package/lib/implements/threaddb/lsstoreds/logstore.ts +0 -29
  82. package/lib/implements/threaddb/lsstoreds/metadata.ts +0 -223
  83. package/lib/implements/threaddb/net/define.ts +0 -138
  84. package/lib/implements/threaddb/net/grpcClient.ts +0 -582
  85. package/lib/implements/threaddb/net/grpcserver.ts +0 -146
  86. package/lib/implements/threaddb/net/net.ts +0 -2006
  87. package/lib/implements/threaddb/pb/lstore.proto +0 -38
  88. package/lib/implements/threaddb/pb/lstore.ts +0 -393
  89. package/lib/implements/threaddb/pb/lstore_pb.d.ts +0 -433
  90. package/lib/implements/threaddb/pb/lstore_pb.js +0 -1085
  91. package/lib/implements/threaddb/pb/net.proto +0 -194
  92. package/lib/implements/threaddb/pb/net_pb.d.ts +0 -2349
  93. package/lib/implements/threaddb/pb/net_pb.js +0 -5525
  94. package/lib/implements/threaddb/pb/proto-custom-types.ts +0 -212
  95. package/lib/implements/util/client.ts +0 -72
  96. package/lib/implements/util/manager.ts +0 -146
  97. package/lib/implements/wallet/manager.ts +0 -664
  98. package/lib/index.ts +0 -57
  99. package/lib/interfaces/DCContext.ts +0 -51
  100. package/lib/interfaces/aiproxy-interface.ts +0 -145
  101. package/lib/interfaces/auth-interface.ts +0 -118
  102. package/lib/interfaces/cache-interface.ts +0 -22
  103. package/lib/interfaces/client-interface.ts +0 -11
  104. package/lib/interfaces/comment-interface.ts +0 -167
  105. package/lib/interfaces/database-interface.ts +0 -169
  106. package/lib/interfaces/file-interface.ts +0 -120
  107. package/lib/interfaces/index.ts +0 -10
  108. package/lib/interfaces/keyvalue-interface.ts +0 -156
  109. package/lib/interfaces/message-interface.ts +0 -22
  110. package/lib/interfaces/util-interface.ts +0 -31
  111. package/lib/modules/aiproxy-module.ts +0 -246
  112. package/lib/modules/auth-module.ts +0 -753
  113. package/lib/modules/cache-module.ts +0 -99
  114. package/lib/modules/client-module.ts +0 -71
  115. package/lib/modules/comment-module.ts +0 -429
  116. package/lib/modules/database-module.ts +0 -598
  117. package/lib/modules/file-module.ts +0 -291
  118. package/lib/modules/index.ts +0 -13
  119. package/lib/modules/keyvalue-module.ts +0 -379
  120. package/lib/modules/message-module.ts +0 -107
  121. package/lib/modules/util-module.ts +0 -148
  122. package/lib/polyfills/process-env-browser.ts +0 -1
  123. package/lib/proto/datasource.ts +0 -93
  124. package/lib/proto/dcnet.proto +0 -1601
  125. package/lib/proto/dcnet_proto.d.ts +0 -22857
  126. package/lib/proto/dcnet_proto.js +0 -55204
  127. package/lib/proto/dcnet_proto_sparse.js +0 -55166
  128. package/lib/proto/oidfetch.proto +0 -25
  129. package/lib/proto/oidfetch_proto.d.ts +0 -585
  130. package/lib/proto/oidfetch_proto.js +0 -1247
  131. package/lib/serverless/babel-browser.ts +0 -39
  132. package/lib/serverless/base_entity.ts +0 -78
  133. package/lib/serverless/base_repository.ts +0 -414
  134. package/lib/serverless/browser_schema_extractor.ts +0 -283
  135. package/lib/serverless/decorator_factory.ts +0 -322
  136. package/lib/util/BrowserLineReader.ts +0 -73
  137. package/lib/util/base64.ts +0 -105
  138. package/lib/util/bcrypt.ts +0 -206
  139. package/lib/util/curve25519Encryption.ts +0 -418
  140. package/lib/util/dccrypt.ts +0 -73
  141. package/lib/util/logger.ts +0 -104
  142. package/lib/util/utils.ts +0 -289
@@ -1,759 +0,0 @@
1
- import { KeyValueClient } from "./client";
2
- import {
3
- DCConnectInfo,
4
- ThemeAuthInfo,
5
- ThemeComment,
6
- } from "../../common/types/types";
7
- import { OpenFlag } from "../../common/constants";
8
- import { CommentManager } from "../comment/manager";
9
- import { HeliaLibp2p } from "helia";
10
- import { ChainUtil } from "../../common/chain";
11
- import { DcUtil } from "../../common/dcutil";
12
- import { Ed25519PubKey } from "../../common/dc-key/ed25519";
13
- import { sha256, uint32ToLittleEndianBytes } from "../../util/utils";
14
- import { base32 } from "multiformats/bases/base32";
15
- import { Client } from "../../common/dcapi";
16
- import { CommentType, Direction } from "../../common/define";
17
- import { DCContext } from "../../../lib/interfaces/DCContext";
18
- import { AnyExtensionField } from "protobufjs";
19
- import { isArray } from "util";
20
-
21
- //定义Key-Value存储的数据类型
22
- export enum KeyValueStoreType { //存储主题类型 1:鉴权主题(读写都需要鉴权) 2:公共主题(默认所有用户可读,写需要鉴权)
23
- Auth = 1,
24
- Public = 2,
25
- }
26
- // 错误定义
27
- export class KeyValueError extends Error {
28
- constructor(message: string) {
29
- super(message);
30
- this.name = "KeyValueError";
31
- }
32
- }
33
- export const Errors = {
34
- ErrNoDcPeerConnected: new KeyValueError("no dc peer connected"),
35
- ErrPublicKeyIsNull: new KeyValueError("publickey is null"),
36
- };
37
-
38
- export class KeyValueDB {
39
- private appId: string;
40
- private dbname: string;
41
- private themeAuthor: string;
42
- private manager: KeyValueManager;
43
- constructor(
44
- appId: string,
45
- dbname: string,
46
- themeAuthor: string,
47
- manager: KeyValueManager
48
- ) {
49
- this.appId = appId;
50
- this.dbname = dbname;
51
- this.themeAuthor = themeAuthor;
52
- this.manager = manager;
53
- }
54
-
55
- //设置键值对
56
- async set(
57
- key: string,
58
- value: any,
59
- indexs: string = "",
60
- vaccount: string = ""
61
- ): Promise<[boolean | null,number | null, Error | null]> {
62
- return this.manager.setKeyValue(
63
- this.appId,
64
- this.themeAuthor,
65
- this.dbname,
66
- key,
67
- value,
68
- indexs,
69
- vaccount
70
- );
71
- }
72
-
73
-
74
-
75
- //获取键值对
76
- async get(
77
- key: string,
78
- writerPubkey?: string,
79
- vaccount?: string
80
- ): Promise<[string | null, Error | null]> {
81
- if (!writerPubkey) {//没有指定写入者,则获取该key的最新值
82
- const [values, err] = await this.getWithIndex("indexkey_keyself_" + key, "", 1, "", Direction.Reverse, 0, vaccount);
83
- if (err) {
84
- return [null, err];
85
- }
86
- if (!values) {
87
- return [null, null];
88
- }
89
- const jsonValues = JSON.parse(values);
90
- if (!Array.isArray(jsonValues)) {
91
- return [values, null];
92
- }
93
- if (jsonValues.length == 0) {
94
- return [null, null];
95
- }
96
- let keyValue ="";
97
- try{
98
- keyValue = jsonValues[0][key];
99
- }catch(err){
100
- return [null, err instanceof Error ? err : new Error(String(err))];
101
- }
102
- if (!keyValue || keyValue.length == 0) {
103
- keyValue = jsonValues[0];
104
- }
105
- return [keyValue, null];
106
- }
107
- return this.manager.getValueWithKey(
108
- this.appId,
109
- this.themeAuthor,
110
- this.dbname,
111
- writerPubkey,
112
- key,
113
- vaccount
114
- );
115
- }
116
-
117
- //批量获取键值对
118
- async getBatch(
119
- keys: string,
120
- writerPubkey: string,
121
- vaccount: string
122
- ): Promise<[string | null, Error | null]> {
123
- if (!writerPubkey) {
124
- writerPubkey = this.themeAuthor;
125
- }
126
- return this.manager.getValuesWithKeys(
127
- this.appId,
128
- this.themeAuthor,
129
- this.dbname,
130
- writerPubkey,
131
- keys,
132
- vaccount
133
- );
134
- }
135
-
136
- //通过索引获取键值对
137
- async getWithIndex(
138
- indexKey: string,
139
- indexValue: string,
140
- limit: number = 1000,
141
- seekKey: string = "",
142
- direction: Direction = Direction.Forward,
143
- offset: number = 0,
144
- vaccount: string = ""
145
- ): Promise<[string | null, Error | null]> {
146
- return this.manager.getWithIndex(
147
- this.appId,
148
- this.themeAuthor,
149
- this.dbname,
150
- indexKey,
151
- indexValue,
152
- seekKey,
153
- direction,
154
- offset,
155
- limit,
156
- vaccount
157
- );
158
- }
159
-
160
- //配置授权
161
- async configAuth(
162
- authPubkey: string,
163
- permission: number,
164
- remark: string,
165
- vaccount?: string
166
- ): Promise<[number | null, Error | null]> {
167
- return this.manager.doConfigAuth(
168
- this.appId,
169
- this.themeAuthor,
170
- this.dbname,
171
- authPubkey,
172
- permission,
173
- remark,
174
- vaccount
175
- );
176
- }
177
-
178
- //获取授权列表
179
- async getAuthList(
180
- vaccount?: string
181
- ): Promise<[ThemeAuthInfo[] | null, ThemeComment[] | null, Error | null]> {
182
- return this.manager.getAuthList(
183
- this.appId,
184
- this.themeAuthor,
185
- this.dbname,
186
- vaccount
187
- );
188
- }
189
- }
190
-
191
- export class KeyValueManager {
192
- private dc: DcUtil;
193
- private chainUtil: ChainUtil;
194
- private context: DCContext;
195
- constructor(
196
- dc: DcUtil,
197
- chainUtil: ChainUtil,
198
- context: DCContext
199
- ) {
200
- this.dc = dc;
201
- this.chainUtil = chainUtil;
202
- this.context = context;
203
- }
204
-
205
- // 创建Key-Value存储
206
- async createStore(
207
- appId: string,
208
- theme: string,
209
- space: number,
210
- type: KeyValueStoreType
211
- ): Promise<[KeyValueDB | null, Error | null]> {
212
- if(!this.context.publicKey){
213
- return [null, Errors.ErrPublicKeyIsNull];
214
- }
215
- // Default group to "DCAPP" if empty
216
- if (appId === "") {
217
- appId = "DCAPP";
218
- }
219
-
220
- // Set minimum space (100M)
221
- if (space < 100 << 20) {
222
- space = 100 << 20;
223
- }
224
- // Theme must start with "keyvalue_"
225
- if (!theme.startsWith("keyvalue_")) {
226
- theme = "keyvalue_" + theme;
227
- }
228
- // NOTE: There seems to be a logical error in the original code:
229
- // It checks if theme ends with "_pub" and returns an error if it does,
230
- // but the error message suggests it should end with "_pub".
231
- // I'm assuming the condition should check if it doesn't end with "_pub"
232
- if (type === KeyValueStoreType.Public) {
233
- // Public theme must end with "_pub"
234
- if (!theme.endsWith("_pub")) {
235
- return [
236
- null,
237
- new Error(
238
- "vaCreateStoreTheme failed, public theme must end with '_pub'"
239
- ),
240
- ];
241
- }
242
- }
243
-
244
- try {
245
- // Assuming AddThemeObjDeal is implemented elsewhere
246
- const commentManager = new CommentManager(this.context);
247
- const res = await commentManager.addThemeObj(
248
- appId,
249
- theme,
250
- OpenFlag.AUTH,
251
- space || 50 * 1024 * 1024 // 50M
252
- );
253
- if (res[0] !== 0 || res[1] !== null) {
254
- return [null, new Error(`vaCreateStoreTheme failed, resFlag: ${res}`)];
255
- }
256
- // Create KeyValueDB instance
257
- const keyValueDB = new KeyValueDB(
258
- appId,
259
- theme,
260
- this.context.publicKey.string(),
261
- this
262
- );
263
- return [keyValueDB, null];
264
- } catch (error) {
265
- return [null, error as Error];
266
- }
267
- }
268
-
269
- async getKeyValueDB(
270
- appId: string,
271
- theme: string,
272
- ThemeAuthor: string
273
- ): Promise<[KeyValueDB | null, Error | null]> {
274
- const commentManager = new CommentManager(this.context);
275
- // Ensure theme starts with "keyvalue_"
276
- if (!theme.startsWith("keyvalue_")) {
277
- theme = "keyvalue_" + theme;
278
- }
279
- //先判断主题是否存在,不存在就报错
280
- const [flag,err] = await commentManager.isThemeExist(appId, theme, ThemeAuthor);
281
- if(err){
282
- return [null, err];
283
- }
284
- if (!flag) {
285
- return [
286
- null,
287
- new Error(`Theme ${theme} does not exist for appId ${appId}`),
288
- ];
289
- }
290
- const keyValueDB = new KeyValueDB(appId, theme, ThemeAuthor, this);
291
- return [keyValueDB, null];
292
- }
293
-
294
- async doConfigAuth(
295
- appId: string,
296
- themeAuthor: string,
297
- theme: string,
298
- authPubkey: string,
299
- permission: number,
300
- remark: string,
301
- vaccount?: string
302
- ): Promise<[number | null, Error | null]> {
303
- if(!this.context.publicKey){
304
- return [null, Errors.ErrPublicKeyIsNull];
305
- }
306
- if (!theme.startsWith("keyvalue_")) {
307
- theme = "keyvalue_" + theme;
308
- }
309
- if (!theme.endsWith("_authlist")) {
310
- theme = theme + "_authlist";
311
- }
312
-
313
- const userPubkey = this.context.getPublicKey();
314
- let userPubkeyStr = userPubkey.string();
315
-
316
- let client = this.context.AccountBackupDc?.client || null;
317
- if (!client){
318
- client = await this.dc.connectToUserDcPeer(this.context.publicKey.raw);
319
- }
320
-
321
- if (client === null) {
322
- return [null, new Error("ErrConnectToAccountPeersFail")];
323
- }
324
-
325
- if (client.peerAddr === null) {
326
- return [null, new Error("ErrConnectToAccountPeersFail")];
327
- }
328
- if (client.token == "") {
329
- await client.GetToken(appId, this.context.publicKey.string(), this.context.sign);
330
- }
331
- const themeAuthorPubkey: Ed25519PubKey =
332
- Ed25519PubKey.edPubkeyFromStr(themeAuthor);
333
-
334
- let pubkeyFlag = true;
335
- let forPubkey: Ed25519PubKey | null = null;
336
- try {
337
- forPubkey = Ed25519PubKey.edPubkeyFromStr(authPubkey);
338
- } catch (error) {
339
- pubkeyFlag = false;
340
- }
341
- let forPubkeyHex: string;
342
- if (pubkeyFlag && forPubkey !== null) {
343
- forPubkeyHex = forPubkey.string();
344
- } else {
345
- forPubkeyHex = authPubkey;
346
- }
347
-
348
- const content = `${forPubkeyHex}:${permission}:${remark}`;
349
-
350
- // Generate contentCid (sha256 of content)
351
- const commentUint8 = new TextEncoder().encode(content);
352
- const contentHash = await sha256(commentUint8);
353
- const contentCid = base32.encode(contentHash);
354
-
355
- // Get blockchain height
356
- let blockHeight: number;
357
- try {
358
- blockHeight = await this.chainUtil.getBlockHeight() || 0;
359
- } catch (error) {
360
- return [null, new Error("ErrGetBlockHeightFail")];
361
- }
362
-
363
- const contentSize = commentUint8.length;
364
-
365
- // Create binary representation of blockHeight (little endian)
366
- const hValue: Uint8Array = uint32ToLittleEndianBytes(
367
- blockHeight ? blockHeight : 0
368
- );
369
- // Create binary representation of type (little endian)
370
- const typeValue: Uint8Array = uint32ToLittleEndianBytes(
371
- CommentType.Comment
372
- );
373
- // sign(Theme+appId+objAuthor+blockheight+contentCid)
374
- const themeValue: Uint8Array = new TextEncoder().encode(theme);
375
- const appIdValue: Uint8Array = new TextEncoder().encode(appId);
376
- const themeAuthorValue: Uint8Array = new TextEncoder().encode(
377
- themeAuthorPubkey.string()
378
- );
379
- const contentCidValue: Uint8Array = new TextEncoder().encode(contentCid);
380
- let preSign = new Uint8Array([
381
- ...themeValue,
382
- ...appIdValue,
383
- ...themeAuthorValue,
384
- ...hValue,
385
- ...contentCidValue,
386
- ...typeValue,
387
- ]);
388
-
389
- const signature = await this.context.sign(preSign);
390
-
391
- const keyValueClient = new KeyValueClient(client, this.context);
392
- try {
393
- const res = await keyValueClient.configThemeObjAuth(
394
- theme,
395
- appId,
396
- themeAuthor,
397
- blockHeight,
398
- userPubkeyStr,
399
- contentCid,
400
- content,
401
- contentSize,
402
- CommentType.Comment,
403
- signature,
404
- vaccount
405
- );
406
-
407
- if (res !== 0) {
408
- return [res, new Error(`configThemeObjAuth fail, resFlag: ${res}`)];
409
- } else {
410
- return [0, null];
411
- }
412
- } catch (error: any) {
413
- return [null, error];
414
- }
415
- }
416
-
417
- async getAuthList(
418
- appId: string,
419
- themeAuthor: string,
420
- theme: string,
421
- vaccount?: string
422
- ): Promise<[ThemeAuthInfo[] | null, ThemeComment[] | null, Error | null]> {
423
- if (!theme.startsWith("keyvalue_")) {
424
- theme = "keyvalue_" + theme;
425
- }
426
- if (!theme.endsWith("_authlist")) {
427
- theme = theme + "_authlist";
428
- }
429
- let seekKey: string = "";
430
- let originAuthList: ThemeComment[] = [];
431
- let authList: ThemeAuthInfo[] = [];
432
- try {
433
- while (true) {
434
- const commentManager = new CommentManager(this.context);
435
- const res = await commentManager.getThemeComments(
436
- appId,
437
- theme,
438
- themeAuthor,
439
- 0,
440
- Direction.Forward,
441
- 0,
442
- 1000,
443
- seekKey || "",
444
- vaccount
445
- );
446
- if (res[0] && res[0].length == 0) {
447
- break;
448
- }
449
-
450
- const resList = res[0];
451
- if(!resList || resList.length == 0){
452
- break;
453
- }
454
- for (let i = 0; i < resList.length; i++) {
455
- originAuthList.push(resList[i]!);
456
- const content = resList[i]!.comment;
457
- const parts = content.split(":");
458
- if (parts.length < 2) {
459
- continue;
460
- }
461
- const authPubkey = parts[0]!;
462
- const permission = parseInt(parts[1]!);
463
- const remark = content.substring(parts[0]!.length + 2);
464
- authList.push({
465
- pubkey: authPubkey,
466
- permission: permission,
467
- remark: remark,
468
- });
469
- }
470
- if (resList.length < 1000) {
471
- break;
472
- }
473
- seekKey = `${resList[resList.length - 1]!.blockheight}/${
474
- resList[resList.length - 1]!.commentCid
475
- }`;
476
- }
477
- } catch (error: any) {
478
- return [authList, originAuthList, error];
479
- }
480
- return [authList, originAuthList, null];
481
- }
482
-
483
- async setKeyValue(
484
- appId: string,
485
- themeAuthor: string,
486
- theme: string,
487
- key: string,
488
- value: string,
489
- indexs: string, //索引列表,格式为key1:value1$$$key2:value2
490
- vaccount?: string
491
- ): Promise<[boolean | null, number | null, Error | null]> {
492
- if (!this.context.publicKey) {
493
- return [null, null, Errors.ErrPublicKeyIsNull];
494
- }
495
- if (!theme.startsWith("keyvalue_")) {
496
- theme = "keyvalue_" + theme;
497
- }
498
- const userPubkey = this.context.getPublicKey();
499
- let userPubkeyStr = userPubkey.string();
500
- let client = this.context.AccountBackupDc?.client || null;
501
- if (!client){
502
- client = await this.dc.connectToUserDcPeer(this.context.publicKey.raw);
503
- }
504
-
505
- if (client === null) {
506
- return [null, null, new Error("ErrConnectToAccountPeersFail")];
507
- }
508
-
509
- if (client.peerAddr === null) {
510
- return [null, null, new Error("ErrConnectToAccountPeersFail")];
511
- }
512
- if (client.token == "") {
513
- await client.GetToken(appId, this.context.publicKey.string(), this.context.sign);
514
- }
515
- let content = `${key}:${value}`;
516
- if (indexs != "") {
517
- content = `$$i_start$$${indexs}$$i_end$$${content}`;
518
- }
519
- const contentUint8 = new TextEncoder().encode(content);
520
- const contenthash = await sha256(contentUint8);
521
- const contentCidBase32 = base32.encode(contenthash);
522
-
523
- const contentSize = contentUint8.length;
524
-
525
- const blockHeight: number = await this.chainUtil.getBlockHeight() || 0;
526
- const hValue: Uint8Array = uint32ToLittleEndianBytes(
527
- blockHeight ? blockHeight : 0
528
- );
529
- const themeValue: Uint8Array = new TextEncoder().encode(theme);
530
- const themeAuthorValue: Uint8Array = new TextEncoder().encode(themeAuthor);
531
- const appIdValue: Uint8Array = new TextEncoder().encode(appId);
532
- const contentCidValue: Uint8Array = new TextEncoder().encode(
533
- contentCidBase32
534
- );
535
- const typeValue: Uint8Array = uint32ToLittleEndianBytes(
536
- CommentType.KeyValue
537
- );
538
-
539
- const preSign = new Uint8Array([
540
- ...themeValue,
541
- ...appIdValue,
542
- ...themeAuthorValue,
543
- ...hValue,
544
- ...contentCidValue,
545
- ...typeValue,
546
- ]);
547
- const signature = await this.context.sign(preSign);
548
- const keyValueClient = new KeyValueClient(client, this.context);
549
- try {
550
- const [resFlag,resTimestamp] = await keyValueClient.setKeyValue(
551
- theme,
552
- appId,
553
- themeAuthor,
554
- blockHeight,
555
- userPubkeyStr,
556
- contentCidBase32,
557
- content,
558
- contentSize,
559
- CommentType.KeyValue,
560
- signature,
561
- vaccount
562
- );
563
-
564
- if (resFlag !== 0) {
565
- return [null, resTimestamp, new Error(`setKeyValue fail, resFlag:${resFlag}`)];
566
- }
567
- return [true, resTimestamp, null];
568
- } catch (error: any) {
569
- return [null, null, error];
570
- }
571
- }
572
-
573
- async getValueWithKey(
574
- appId: string,
575
- themeAuthor: string,
576
- theme: string,
577
- writerPubkey: string,
578
- key: string,
579
- vaccount?: string
580
- ): Promise<[string | null, Error | null]> {
581
- if(!this.context.publicKey){
582
- return [null, Errors.ErrPublicKeyIsNull];
583
- }
584
- if (!theme.startsWith("keyvalue_")) {
585
- theme = "keyvalue_" + theme;
586
- }
587
- let client = this.context.AccountBackupDc.client || null;
588
- if (themeAuthor != this.context.publicKey.string()) {
589
- //查询他人主题评论
590
- const authorPublicKey: Ed25519PubKey =
591
- Ed25519PubKey.edPubkeyFromStr(themeAuthor);
592
- client = await this.dc.connectToUserDcPeer(authorPublicKey.raw);
593
- if (!client) {
594
- return [null, Errors.ErrNoDcPeerConnected];
595
- }
596
- }
597
- if (client === null) {
598
- return [null, new Error("ErrConnectToAccountPeersFail")];
599
- }
600
-
601
- if (client.peerAddr === null) {
602
- return [null, new Error("ErrConnectToAccountPeersFail")];
603
- }
604
- if (client.token == "") {
605
- await client.GetToken(appId, this.context.publicKey.string(), this.context.sign);
606
- }
607
-
608
- const keyValueClient = new KeyValueClient(client, this.context);
609
- try {
610
- const res = await keyValueClient.getValueWithKey(
611
- theme,
612
- appId,
613
- themeAuthor,
614
- writerPubkey,
615
- key,
616
- vaccount
617
- );
618
-
619
- if (res == null) {
620
- return [
621
- null,
622
- new Error(`vaGetValueWithKeyForVAccount fail, resFlag:${res}`),
623
- ];
624
- }
625
- const keyValue = new TextDecoder().decode(res);
626
- return [keyValue, null];
627
- } catch (error: any) {
628
- return [null, error];
629
- }
630
- }
631
-
632
- async getValuesWithKeys(
633
- appId: string,
634
- themeAuthor: string,
635
- theme: string,
636
- writerPubkey: string,
637
- keys: string,
638
- vaccount?: string
639
- ): Promise<[string | null, Error | null]> {
640
- if(!this.context.publicKey){
641
- return [null, Errors.ErrPublicKeyIsNull];
642
- }
643
- if (!theme.startsWith("keyvalue_")) {
644
- theme = "keyvalue_" + theme;
645
- }
646
- let client = this.context.AccountBackupDc.client || null;
647
- if (themeAuthor != this.context.publicKey.string()) {
648
- //查询他人主题评论
649
- const authorPublicKey: Ed25519PubKey =
650
- Ed25519PubKey.edPubkeyFromStr(themeAuthor);
651
- client = await this.dc.connectToUserDcPeer(authorPublicKey.raw);
652
- if (!client) {
653
- return [null, Errors.ErrNoDcPeerConnected];
654
- }
655
- }
656
- if (client === null) {
657
- return [null, new Error("ErrConnectToAccountPeersFail")];
658
- }
659
-
660
- if (client.peerAddr === null) {
661
- return [null, new Error("ErrConnectToAccountPeersFail")];
662
- }
663
- if (client.token == "") {
664
- await client.GetToken(appId, this.context.publicKey.string(), this.context.sign);
665
- }
666
-
667
- const keyValueClient = new KeyValueClient(client, this.context);
668
- try {
669
- const res = await keyValueClient.getValuesWithKeys(
670
- theme,
671
- appId,
672
- themeAuthor,
673
- writerPubkey,
674
- keys,
675
- vaccount
676
- );
677
-
678
- if (res == null) {
679
- return [
680
- null,
681
- new Error(`vaGetValuesWithKeysForVAccount fail, resFlag:${res}`),
682
- ];
683
- }
684
- const keyValues = new TextDecoder().decode(res);
685
- return [keyValues, null];
686
- } catch (error: any) {
687
- return [null, error];
688
- }
689
- }
690
-
691
- async getWithIndex(
692
- appId: string,
693
- themeAuthor: string,
694
- theme: string,
695
- indexKey: string,
696
- indexValue: string,
697
- seekKey: string,
698
- direction: Direction = Direction.Forward,
699
- offset: number,
700
- limit: number,
701
- vaccount?: string
702
- ): Promise<[string | null, Error | null]> {
703
- if(!this.context.publicKey){
704
- return [null, Errors.ErrPublicKeyIsNull];
705
- }
706
- if (!theme.startsWith("keyvalue_")) {
707
- theme = "keyvalue_" + theme;
708
- }
709
- let client = this.context.AccountBackupDc.client || null;
710
- if (themeAuthor != this.context.publicKey.string()) {
711
- //查询他人主题评论
712
- const authorPublicKey: Ed25519PubKey =
713
- Ed25519PubKey.edPubkeyFromStr(themeAuthor);
714
- client = await this.dc.connectToUserDcPeer(authorPublicKey.raw);
715
- if (!client) {
716
- return [null, Errors.ErrNoDcPeerConnected];
717
- }
718
- //获取token
719
- await client.GetToken(appId, this.context.publicKey.string(), this.context.sign);
720
- }
721
- if (client === null) {
722
- return [null, new Error("ErrConnectToAccountPeersFail")];
723
- }
724
-
725
- if (client.peerAddr === null) {
726
- return [null, new Error("ErrConnectToAccountPeersFail")];
727
- }
728
- if (client.token == "") {
729
- await client.GetToken(appId, this.context.publicKey.string(), this.context.sign);
730
- }
731
-
732
- const keyValueClient = new KeyValueClient(client, this.context);
733
- try {
734
- const res = await keyValueClient.getValuesWithIndex(
735
- appId,
736
- themeAuthor,
737
- theme,
738
- indexKey,
739
- indexValue,
740
- seekKey,
741
- direction,
742
- offset,
743
- limit,
744
- vaccount
745
- );
746
-
747
- if (res == null) {
748
- return [
749
- null,
750
- new Error(`vaGetValuesWithKeysForVAccount fail, resFlag:${res}`),
751
- ];
752
- }
753
- const keyValues = new TextDecoder().decode(res);
754
- return [keyValues, null];
755
- } catch (error: any) {
756
- return [null, error];
757
- }
758
- }
759
- }