-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathdcapi.html
More file actions
2204 lines (2037 loc) · 118 KB
/
Copy pathdcapi.html
File metadata and controls
2204 lines (2037 loc) · 118 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<!DOCTYPE html>
<html>
<head>
<title>接口文档_DC.md</title>
<meta http-equiv="Content-type" content="text/html;charset=UTF-8">
<style>
/* https://github.com/microsoft/vscode/blob/master/extensions/markdown-language-features/media/markdown.css */
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
body {
font-family: var(--vscode-markdown-font-family, -apple-system, BlinkMacSystemFont, "Segoe WPC", "Segoe UI", "Ubuntu", "Droid Sans", sans-serif);
font-size: var(--vscode-markdown-font-size, 14px);
padding: 0 26px;
line-height: var(--vscode-markdown-line-height, 22px);
word-wrap: break-word;
}
#code-csp-warning {
position: fixed;
top: 0;
right: 0;
color: white;
margin: 16px;
text-align: center;
font-size: 12px;
font-family: sans-serif;
background-color:#444444;
cursor: pointer;
padding: 6px;
box-shadow: 1px 1px 1px rgba(0,0,0,.25);
}
#code-csp-warning:hover {
text-decoration: none;
background-color:#007acc;
box-shadow: 2px 2px 2px rgba(0,0,0,.25);
}
body.scrollBeyondLastLine {
margin-bottom: calc(100vh - 22px);
}
body.showEditorSelection .code-line {
position: relative;
}
body.showEditorSelection .code-active-line:before,
body.showEditorSelection .code-line:hover:before {
content: "";
display: block;
position: absolute;
top: 0;
left: -12px;
height: 100%;
}
body.showEditorSelection li.code-active-line:before,
body.showEditorSelection li.code-line:hover:before {
left: -30px;
}
.vscode-light.showEditorSelection .code-active-line:before {
border-left: 3px solid rgba(0, 0, 0, 0.15);
}
.vscode-light.showEditorSelection .code-line:hover:before {
border-left: 3px solid rgba(0, 0, 0, 0.40);
}
.vscode-light.showEditorSelection .code-line .code-line:hover:before {
border-left: none;
}
.vscode-dark.showEditorSelection .code-active-line:before {
border-left: 3px solid rgba(255, 255, 255, 0.4);
}
.vscode-dark.showEditorSelection .code-line:hover:before {
border-left: 3px solid rgba(255, 255, 255, 0.60);
}
.vscode-dark.showEditorSelection .code-line .code-line:hover:before {
border-left: none;
}
.vscode-high-contrast.showEditorSelection .code-active-line:before {
border-left: 3px solid rgba(255, 160, 0, 0.7);
}
.vscode-high-contrast.showEditorSelection .code-line:hover:before {
border-left: 3px solid rgba(255, 160, 0, 1);
}
.vscode-high-contrast.showEditorSelection .code-line .code-line:hover:before {
border-left: none;
}
img {
max-width: 100%;
max-height: 100%;
}
a {
text-decoration: none;
}
a:hover {
text-decoration: underline;
}
a:focus,
input:focus,
select:focus,
textarea:focus {
outline: 1px solid -webkit-focus-ring-color;
outline-offset: -1px;
}
hr {
border: 0;
height: 2px;
border-bottom: 2px solid;
}
h1 {
padding-bottom: 0.3em;
line-height: 1.2;
border-bottom-width: 1px;
border-bottom-style: solid;
}
h1, h2, h3 {
font-weight: normal;
}
table {
border-collapse: collapse;
}
table > thead > tr > th {
text-align: left;
border-bottom: 1px solid;
}
table > thead > tr > th,
table > thead > tr > td,
table > tbody > tr > th,
table > tbody > tr > td {
padding: 5px 10px;
}
table > tbody > tr + tr > td {
border-top: 1px solid;
}
blockquote {
margin: 0 7px 0 5px;
padding: 0 16px 0 10px;
border-left-width: 5px;
border-left-style: solid;
}
code {
font-family: Menlo, Monaco, Consolas, "Droid Sans Mono", "Courier New", monospace, "Droid Sans Fallback";
font-size: 1em;
line-height: 1.357em;
}
body.wordWrap pre {
white-space: pre-wrap;
}
pre:not(.hljs),
pre.hljs code > div {
padding: 16px;
border-radius: 3px;
overflow: auto;
}
pre code {
color: var(--vscode-editor-foreground);
tab-size: 4;
}
/** Theming */
.vscode-light pre {
background-color: rgba(220, 220, 220, 0.4);
}
.vscode-dark pre {
background-color: rgba(10, 10, 10, 0.4);
}
.vscode-high-contrast pre {
background-color: rgb(0, 0, 0);
}
.vscode-high-contrast h1 {
border-color: rgb(0, 0, 0);
}
.vscode-light table > thead > tr > th {
border-color: rgba(0, 0, 0, 0.69);
}
.vscode-dark table > thead > tr > th {
border-color: rgba(255, 255, 255, 0.69);
}
.vscode-light h1,
.vscode-light hr,
.vscode-light table > tbody > tr + tr > td {
border-color: rgba(0, 0, 0, 0.18);
}
.vscode-dark h1,
.vscode-dark hr,
.vscode-dark table > tbody > tr + tr > td {
border-color: rgba(255, 255, 255, 0.18);
}
</style>
<style>
/* Tomorrow Theme */
/* http://jmblog.github.com/color-themes-for-google-code-highlightjs */
/* Original theme - https://github.com/chriskempson/tomorrow-theme */
/* Tomorrow Comment */
.hljs-comment,
.hljs-quote {
color: #8e908c;
}
/* Tomorrow Red */
.hljs-variable,
.hljs-template-variable,
.hljs-tag,
.hljs-name,
.hljs-selector-id,
.hljs-selector-class,
.hljs-regexp,
.hljs-deletion {
color: #c82829;
}
/* Tomorrow Orange */
.hljs-number,
.hljs-built_in,
.hljs-builtin-name,
.hljs-literal,
.hljs-type,
.hljs-params,
.hljs-meta,
.hljs-link {
color: #f5871f;
}
/* Tomorrow Yellow */
.hljs-attribute {
color: #eab700;
}
/* Tomorrow Green */
.hljs-string,
.hljs-symbol,
.hljs-bullet,
.hljs-addition {
color: #718c00;
}
/* Tomorrow Blue */
.hljs-title,
.hljs-section {
color: #4271ae;
}
/* Tomorrow Purple */
.hljs-keyword,
.hljs-selector-tag {
color: #8959a8;
}
.hljs {
display: block;
overflow-x: auto;
color: #4d4d4c;
padding: 0.5em;
}
.hljs-emphasis {
font-style: italic;
}
.hljs-strong {
font-weight: bold;
}
</style>
<style>
/*
* Markdown PDF CSS
*/
body {
font-family: -apple-system, BlinkMacSystemFont, "Segoe WPC", "Segoe UI", "Ubuntu", "Droid Sans", sans-serif, "Meiryo";
padding: 0 12px;
}
pre {
background-color: #f8f8f8;
border: 1px solid #cccccc;
border-radius: 3px;
overflow-x: auto;
white-space: pre-wrap;
overflow-wrap: break-word;
}
pre:not(.hljs) {
padding: 23px;
line-height: 19px;
}
blockquote {
background: rgba(127, 127, 127, 0.1);
border-color: rgba(0, 122, 204, 0.5);
}
.emoji {
height: 1.4em;
}
code {
font-size: 14px;
line-height: 19px;
}
/* for inline code */
:not(pre):not(.hljs) > code {
color: #C9AE75; /* Change the old color so it seems less like an error */
font-size: inherit;
}
/* Page Break : use <div class="page"/> to insert page break
-------------------------------------------------------- */
.page {
page-break-after: always;
}
</style>
<script src="https://unpkg.com/mermaid/dist/mermaid.min.js"></script>
</head>
<body>
<script>
mermaid.initialize({
startOnLoad: true,
theme: document.body.classList.contains('vscode-dark') || document.body.classList.contains('vscode-high-contrast')
? 'dark'
: 'default'
});
</script>
<h1 id="dcapi-%E5%BC%80%E5%8F%91%E6%96%87%E6%A1%A3">DCAPI 开发文档</h1>
<br>
<h2 id="%E7%9B%AE%E5%BD%95">目录</h2>
<ol>
<li>概览</li>
<li>初始化</li>
<li>认证模块 (auth)</li>
<li>文件模块 (file)</li>
<li>客户端模块 (client)</li>
<li>消息模块 (message)</li>
<li>评论模块 (comment)</li>
<li>键值存储模块 (keyvalue)</li>
<li>数据库模块 (database)</li>
<li>缓存模块 (cache)</li>
<li>AI代理模块 (aiproxy)</li>
<li>常见使用流程</li>
</ol>
<p><br><br></p>
<h2 id="%E6%A6%82%E8%A7%88">概览</h2>
<p>DCAPI 提供了一整套去中心化云服务接口。基于这些接口,开发者只需关注前端的呈现效果。一旦开发完成,应用可以直接打包并发布到 DC 去中心化云服务中,生成访问链接后即可邀请用户使用,无需任何服务器端的部署和维护。如果希望将开发完成的应用发布到自己的域名,只需进行简单的配置即可完成。DC API 提供了身份验证、文件存储、消息传递、评论系统和数据库管理等核心功能,且基于区块链技术,支持各种去中心化应用程序的开发。</p>
<p><br><br></p>
<h2 id="%E4%BD%BF%E7%94%A8%E6%B5%81%E7%A8%8B">使用流程</h2>
<p>以下是使用DC API的基本流程图,展示了从引入库到使用各个模块的步骤。</p>
<pre><code class="language-mermaid"><div class="mermaid">
flowchart TD
subgraph "引入/注入 web-dc-api"
A1[使用CDN引入] --> B
A2[使用npm安装并引入] --> B
end
subgraph "创建DC对象"
B[创建DC实例] --> C[配置参数]
C --> D[调用dc.init初始化]
end
subgraph " 使用各模块功能"
D --> E1[认证模块]
D --> E2[文件模块]
D --> E3[AI代理模块]
D --> E4[...]
end
subgraph "认证模块方法"
E1 --> F1[dc.auth.accountLoginWithWallet]
E1 --> F2[dc.auth.accountLogin]
E1 --> F3[dc.auth.bindNFTAccount]
E1 --> F4[dc.auth.getUserInfoWithNft]
end
subgraph "文件模块方法"
E2 --> G1[dc.file.addFile]
E2 --> G2[dc.file.getFile]
E2 --> G3[dc.file.createFileStream]
E2 --> G4[dc.file.getSeekableFileStream]
end
subgraph "AI代理模块方法"
E3 --> K1[dc.aiproxy.createProxyConfig]
E3 --> K2[dc.aiproxy.configAIProxy]
E3 --> K3[dc.aiproxy.DoAIProxyCall]
end
</div></code></pre>
<p>DAPP最佳实践是直接使用AccountLoginWithWallet登录,这样可以省去用户注册账号的流程,直接使用DCWallet钱包登录即可。</p>
<pre><code class="language-mermaid"><div class="mermaid">flowchart TD
subgraph "用户注册/登录"
A1[通过DCWallet登录] --> A
end
subgraph "添加链下评论空间"
B --> B1[调用dc.comment.addUserOffChainSpace]
B1 --> B2[检查操作结果]
end
subgraph "添加链下操作次数"
C --> C1[调用dc.comment.addUserOffChainOpTimes]
C1 --> C2[指定操作次数]
end
subgraph "正常操作"
D --> D1[文件上传下载]
D --> D2[发布评论]
D --> D3[键值存储]
D --> D4[数据库操作]
D --> D5[AI代理调用]
end
A[用户注册账号成功] --> B[为用户添加链下评论空间]
B --> C[为用户添加链下操作次数]
C --> D[用户可以开始正常操作]
</div></code></pre>
<h2 id="%E5%88%9D%E5%A7%8B%E5%8C%96">初始化</h2>
<p>首先需要创建DC实例并初始化它,可以通过两种方式来引入DC API:</p>
<ol>
<li>使用CDN引入,必须同时引入web-dc-api和grpc-libp2p-client两个库。</li>
</ol>
<pre class="hljs"><code><div><span class="hljs-tag"><<span class="hljs-name">script</span> <span class="hljs-attr">src</span>=<span class="hljs-string">"https://cdn.jsdelivr.net/npm/web-dc-api@latest/dist/dc.min.js"</span>></span><span class="hljs-tag"></<span class="hljs-name">script</span>></span>
<span class="hljs-tag"><<span class="hljs-name">script</span> <span class="hljs-attr">src</span>=<span class="hljs-string">"https://cdn.jsdelivr.net/npm/grpc-libp2p-client@latest/dist/grpc.min.js"</span>></span><span class="hljs-tag"></<span class="hljs-name">script</span>></span>
<span class="hljs-tag"><<span class="hljs-name">script</span>></span><span class="javascript">
<span class="hljs-keyword">const</span> dc = <span class="hljs-keyword">new</span> DC({
<span class="hljs-attr">wssUrl</span>: <span class="hljs-string">'wss://chain.baybird.cn'</span>, <span class="hljs-comment">// 区块链路径</span>
<span class="hljs-attr">backWssUrl</span>: <span class="hljs-string">'wss://dc.baybird.cn'</span>, <span class="hljs-comment">// 可选备用区块链路径</span>
<span class="hljs-attr">appInfo</span>: {
<span class="hljs-attr">appId</span>: <span class="hljs-string">'testHtml'</span>, <span class="hljs-comment">// 应用ID</span>
<span class="hljs-attr">appName</span>: <span class="hljs-string">'testHtml Name'</span>, <span class="hljs-comment">// 应用名称</span>
<span class="hljs-attr">appVersion</span>: <span class="hljs-string">'v0.0.1'</span>, <span class="hljs-comment">// 应用版本号</span>
},
});
<span class="hljs-comment">// 初始化DC客户端,连接区块链和存储节点</span>
dc.init().then(<span class="hljs-function"><span class="hljs-params">()</span> =></span> {
<span class="hljs-built_in">console</span>.log(<span class="hljs-string">'DC客户端已初始化'</span>);
});
</span><span class="hljs-tag"></<span class="hljs-name">script</span>></span>
</div></code></pre>
<ol>
<li>依赖包引入</li>
</ol>
<pre class="hljs"><code><div>npm install web-dc-api
</div></code></pre>
<pre class="hljs"><code><div><span class="hljs-keyword">import</span> { DC } <span class="hljs-keyword">from</span> <span class="hljs-string">'web-dc-api'</span>;
<span class="hljs-keyword">const</span> dc = <span class="hljs-keyword">new</span> DC({
wssUrl: <span class="hljs-string">'wss://chain.baybird.cn'</span>, <span class="hljs-comment">// 区块链路径</span>
backWssUrl: <span class="hljs-string">'wss://dc.baybird.cn'</span>, <span class="hljs-comment">// 可选备用区块链路径</span>
appInfo: {
appId: <span class="hljs-string">'testHtml'</span>, <span class="hljs-comment">// 应用ID</span>
appName: <span class="hljs-string">'testHtml Name'</span>, <span class="hljs-comment">// 应用名称</span>
appVersion: <span class="hljs-string">'v0.0.1'</span>, <span class="hljs-comment">// 应用版本号</span>
},
});
<span class="hljs-comment">// 初始化DC客户端,连接区块链和存储节点</span>
<span class="hljs-keyword">await</span> dc.init();
</div></code></pre>
<p><br><br></p>
<h2 id="%E8%AE%A4%E8%AF%81%E6%A8%A1%E5%9D%97-auth">认证模块 (auth)</h2>
<p>负责用户身份验证、账户管理和访问控制。如果只是开发DAPP,只要调用accountLoginWithWallet登录即可,可以不用管这个模块的剩余功能,剩余功能大部分都在DCWallet中已经实现,开发者只要专注于认证模块以外的功能即可。(注意:为了用户能正常使用去中心云服务,用户首次注册,并登录成功后,将会消耗一部分云服务token,用来生成链下存储以及链下操作次数)。</p>
<h3 id="%E6%96%B9%E6%B3%95">方法</h3>
<h4 id="accountloginwithwallet"><code>accountLoginWithWallet()</code></h4>
<p>直接通过DCWallet钱包登录。DAPP开发者直接调用该方法即可。不需要关注账号密码生成、以及保存等细节。</p>
<p><strong>返回:</strong> Promise<[boolean,AccountInfo|null]> - 登录响应</p>
<pre class="hljs"><code><div><span class="hljs-keyword">interface</span> AccountInfo{
nftAccount:<span class="hljs-built_in">string</span>, <span class="hljs-comment">// NFT账号</span>
appAccount:Ed25519PubKey, <span class="hljs-comment">// 应用专用账号公钥 </span>
ethAccount:<span class="hljs-built_in">string</span>, <span class="hljs-comment">// 以太坊兼容链上账号</span>
chainId:<span class="hljs-built_in">string</span>, <span class="hljs-comment">// 区块链ID</span>
chainName:<span class="hljs-built_in">string</span>, <span class="hljs-comment">// 区块链名称</span>
}
</div></code></pre>
<br>
<h4 id="accountloginnftaccount-password-safecode"><code>accountLogin(nftAccount, password, safecode)</code></h4>
<p>用NFT账户登录系统。</p>
<p><strong>参数:</strong></p>
<ul>
<li><code>nftAccount</code>: string - NFT账户名称</li>
<li><code>password</code>: string - 密码</li>
<li><code>safecode</code>: string - 安全码,默认"000000",主要为了加强密码强度</li>
</ul>
<p><strong>返回:</strong> Promise<boolean> - 登录是否成功</p>
<pre class="hljs"><code><div><span class="hljs-keyword">const</span> success = <span class="hljs-keyword">await</span> dc.auth.accountLogin(<span class="hljs-string">'my-nft-account'</span>, <span class="hljs-string">'password'</span>, <span class="hljs-string">'000000'</span>);
<span class="hljs-keyword">if</span> (success) {
<span class="hljs-built_in">console</span>.log(<span class="hljs-string">'登录成功'</span>);
}
</div></code></pre>
<br>
<h4 id="bindnftaccountaccount-password-seccode-mnemonic"><code>bindNFTAccount(account, password, seccode, mnemonic)</code></h4>
<p>将私钥绑定到NFT账号。</p>
<p><strong>参数:</strong></p>
<ul>
<li><code>account</code>: string - NFT账号</li>
<li><code>password</code>: string - 密码</li>
<li><code>seccode</code>: string - 安全码</li>
<li><code>mnemonic</code>: string - 助记词</li>
</ul>
<p><strong>返回:</strong> Promise<[NFTBindStatus, Error | null]> - 绑定状态码和错误信息</p>
<pre class="hljs"><code><div><span class="hljs-keyword">const</span> [status, error] = <span class="hljs-keyword">await</span> dc.auth.bindNFTAccount(<span class="hljs-string">'nft-id'</span>, <span class="hljs-string">'password'</span>, <span class="hljs-string">'seccode'</span>, <span class="hljs-string">'mnemonic phrase'</span>);
<span class="hljs-keyword">if</span> (status === NFTBindStatus.Success) {
<span class="hljs-built_in">console</span>.log(<span class="hljs-string">'绑定成功'</span>);
}
</div></code></pre>
<br>
<h4 id="generateappaccountappid-mnemonic"><code>generateAppAccount(appId, mnemonic)</code></h4>
<p>创建应用子账号。</p>
<p><strong>参数:</strong></p>
<ul>
<li><code>appId</code>: string - 应用ID</li>
<li><code>mnemonic</code>: string - 助记词</li>
</ul>
<p><strong>返回:</strong> Promise<[string | null, Error | null]> - 私钥字符串和错误信息</p>
<pre class="hljs"><code><div><span class="hljs-keyword">const</span> [privateKey, error] = <span class="hljs-keyword">await</span> dc.auth.generateAppAccount(<span class="hljs-string">'myapp'</span>, <span class="hljs-string">'mnemonic phrase'</span>);
<span class="hljs-keyword">if</span> (privateKey) {
<span class="hljs-built_in">console</span>.log(<span class="hljs-string">'子账号私钥:'</span>, privateKey);
}
</div></code></pre>
<br>
<h4 id="isnftaccountbindsuccessaccount"><code>isNftAccountBindSuccess(account)</code></h4>
<p>检查NFT账号是否成功绑定到用户公钥。</p>
<p><strong>参数:</strong></p>
<ul>
<li><code>account</code>: string - NFT账号</li>
</ul>
<p><strong>返回:</strong> Promise<boolean> - 是否成功绑定</p>
<pre class="hljs"><code><div><span class="hljs-keyword">const</span> isBound = <span class="hljs-keyword">await</span> dc.auth.isNftAccountBindSuccess(<span class="hljs-string">'nft-account'</span>);
<span class="hljs-built_in">console</span>.log(<span class="hljs-string">'NFT账号绑定状态:'</span>, isBound);
</div></code></pre>
<br>
<h4 id="isnftaccountbindedaccount"><code>isNftAccountBinded(account)</code></h4>
<p>检查NFT账号是否已被任何账号绑定。</p>
<p><strong>参数:</strong></p>
<ul>
<li><code>account</code>: string - NFT账号</li>
</ul>
<p><strong>返回:</strong> Promise<boolean> - 是否已被绑定</p>
<pre class="hljs"><code><div><span class="hljs-keyword">const</span> isAlreadyBinded = <span class="hljs-keyword">await</span> dc.auth.isNftAccountBinded(<span class="hljs-string">'nft-account'</span>);
<span class="hljs-built_in">console</span>.log(<span class="hljs-string">'NFT账号是否已被绑定:'</span>, isAlreadyBinded);
</div></code></pre>
<br>
<h4 id="getuserinfowithnftnftaccount"><code>getUserInfoWithNft(nftAccount)</code></h4>
<p>根据NFT账户获取用户信息。</p>
<p><strong>参数:</strong></p>
<ul>
<li><code>nftAccount</code>: string - NFT账户</li>
</ul>
<p><strong>返回:</strong> Promise<any> - 用户信息对象</p>
<pre class="hljs"><code><div><span class="hljs-keyword">const</span> userInfo = <span class="hljs-keyword">await</span> dc.auth.getUserInfoWithNft(<span class="hljs-string">'nft-account'</span>);
<span class="hljs-built_in">console</span>.log(<span class="hljs-string">'用户信息:'</span>, userInfo);
</div></code></pre>
<br>
<h4 id="getuserinfowithaccountpubkeyaccount-string"><code>getUserInfoWithAccount(pubkeyAccount: string)</code></h4>
<p><strong>参数:</strong></p>
<ul>
<li><code>pubkeyAccount</code>: string - 公钥账号,即pubkey</li>
</ul>
<p><strong>返回:</strong> Promise<User> - 用户信息对象</p>
<pre class="hljs"><code><div><span class="hljs-keyword">const</span> userInfo = <span class="hljs-keyword">await</span> dc.auth.getUserInfoWithAccount(<span class="hljs-string">'pubkeyAccount'</span>);
<span class="hljs-built_in">console</span>.log(<span class="hljs-string">'用户信息:'</span>, userInfo);
</div></code></pre>
<pre class="hljs"><code><div><span class="hljs-keyword">export</span> <span class="hljs-keyword">interface</span> User {
callMinusNumber: <span class="hljs-built_in">number</span>; <span class="hljs-comment">//调用手续费单位(与用户订阅的空间大小相关,空间越大这个值越小)</span>
commentFrozenStatus: <span class="hljs-built_in">number</span>; <span class="hljs-comment">//评论相关功能(包括keyvalue数据库、主题评论等功能)冻结状态</span>
commentReportAmount: <span class="hljs-built_in">number</span>; <span class="hljs-comment">//评论举报次数</span>
commentReportNumber: <span class="hljs-built_in">number</span>; <span class="hljs-comment">//下一次消除举报次数的区块高度</span>
dbConfig: <span class="hljs-built_in">string</span>; <span class="hljs-comment">//用户个体库配置信息,格式(threadid|sk|rk)加密后的值,(用户公钥加密后的字符串值,用户私钥可以解密)</span>
dbUpdateNumber: <span class="hljs-built_in">number</span>; <span class="hljs-comment">//用户个体库信息更新区块高度</span>
encNftAccount: <span class="hljs-built_in">string</span>; <span class="hljs-comment">//用户绑定的账号加密后字符串(用户公钥加密后的值,用户私钥可以解密)</span>
expireNumber: <span class="hljs-built_in">number</span>; <span class="hljs-comment">//订阅过期区块高度</span>
loginNumber: <span class="hljs-built_in">number</span>; <span class="hljs-comment">//登录次数</span>
nftUpdateNumber: <span class="hljs-built_in">number</span>; <span class="hljs-comment">//用户nft账号更新区块高度</span>
offchainOptimes: <span class="hljs-built_in">number</span>; <span class="hljs-comment">//链下允许总调用次数,当前会一直累加</span>
offchainSpace: <span class="hljs-built_in">number</span>; <span class="hljs-comment">//链下允许总调用空间,当前会一直累加</span>
parentAccount: <span class="hljs-built_in">string</span>; <span class="hljs-comment">//父账号pubkey</span>
peers: <span class="hljs-built_in">Array</span><<span class="hljs-built_in">string</span>>; <span class="hljs-comment">//账号登录信息存储的节点ID列表</span>
purchaseNumber: <span class="hljs-built_in">number</span>; <span class="hljs-comment">//购买次数</span>
requestPeers: <span class="hljs-built_in">Array</span><<span class="hljs-built_in">string</span>>; <span class="hljs-comment">//允许上传文件的节点ID列表,如果不在列表中则无法上传文件,需要先发起绑定请求</span>
spamFrozenStatus: <span class="hljs-built_in">number</span>; <span class="hljs-comment">//垃圾信息相关功能冻结状态</span>
spamReportAmount: <span class="hljs-built_in">number</span>; <span class="hljs-comment">//垃圾信息举报次数</span>
spamReportNumber: <span class="hljs-built_in">number</span>; <span class="hljs-comment">//下一次消除垃圾信息举报次数的区块高度</span>
subscribePrice: <span class="hljs-built_in">string</span>; <span class="hljs-comment">//订阅价格</span>
subscribeSpace: <span class="hljs-built_in">number</span>; <span class="hljs-comment">//订阅空间大小,单位KB</span>
usedSpace: <span class="hljs-built_in">number</span>; <span class="hljs-comment">//已使用空间大小,单位KB</span>
}
</div></code></pre>
<br>
<h4 id="ifenoughuserspaceneedsize-number"><code>ifEnoughUserSpace(needSize?: number)</code></h4>
<p><strong>参数:</strong></p>
<ul>
<li><code>needSize</code>: number (可选) - 需要检查的空间大小,单位为字节,默认不传则检查当前用户空间是否足够</li>
</ul>
<p><strong>返回:</strong> Promise<boolean> - 是否有足够的用户空间</p>
<pre class="hljs"><code><div><span class="hljs-keyword">const</span> flag = <span class="hljs-keyword">await</span> dc.auth.ifEnoughUserSpace(<span class="hljs-number">100</span> * <span class="hljs-number">1024</span>*<span class="hljs-number">1024</span>); <span class="hljs-comment">// 检查是否有100MB空间</span>
<span class="hljs-keyword">if</span> (flag) {
<span class="hljs-built_in">console</span>.log(<span class="hljs-string">'用户空间足够'</span>);
} <span class="hljs-keyword">else</span> {
<span class="hljs-built_in">console</span>.log(<span class="hljs-string">'用户空间不足'</span>);
}
</div></code></pre>
<br>
<h4 id="refreshuserinfo"><code>refreshUserInfo()</code></h4>
<p><strong>返回:</strong> Promise<User> - 刷新后的用户信息对象</p>
<pre class="hljs"><code><div><span class="hljs-keyword">const</span> userInfo = <span class="hljs-keyword">await</span> dc.auth.refreshUserInfo();
<span class="hljs-built_in">console</span>.log(<span class="hljs-string">'刷新后的用户信息:'</span>, userInfo);
</div></code></pre>
<h4 id="signpayload-uint8array">sign(payload: Uint8Array)</h4>
<p><strong>参数:</strong></p>
<ul>
<li><code>payload</code>: Uint8Array - 需要签名的数据</li>
</ul>
<p><strong>返回:</strong> Promise<Uint8Array> - 签名结果</p>
<pre class="hljs"><code><div><span class="hljs-keyword">const</span> payload = <span class="hljs-keyword">new</span> TextEncoder().encode(<span class="hljs-string">'需要签名的数据'</span>);
<span class="hljs-keyword">const</span> signature = <span class="hljs-keyword">await</span> dc.auth.sign(payload);
<span class="hljs-built_in">console</span>.log(<span class="hljs-string">'签名结果:'</span>, signature);
</div></code></pre>
<p><br><br></p>
<h2 id="%E6%96%87%E4%BB%B6%E6%A8%A1%E5%9D%97-file">文件模块 (file)</h2>
<p>提供文件上传、下载和缓存功能。</p>
<h3 id="%E6%96%B9%E6%B3%95">方法</h3>
<h4 id="addfilefile-enkey-onupdatetransmitsize"><code>addFile(file, enkey, onUpdateTransmitSize)</code></h4>
<p>上传文件到存储系统。</p>
<p><strong>参数:</strong></p>
<ul>
<li><code>file</code>: File - 要上传的文件</li>
<li><code>enkey</code>: string - 加密密钥</li>
<li><code>onUpdateTransmitSize</code>: (status: number, size: number) => void - 进度回调函数</li>
</ul>
<p><strong>返回:</strong> Promise<any> - 上传结果,包含CID等信息</p>
<pre class="hljs"><code><div><span class="hljs-keyword">const</span> file = <span class="hljs-keyword">new</span> File([<span class="hljs-string">'content'</span>], <span class="hljs-string">'test.txt'</span>);
<span class="hljs-keyword">const</span> result = <span class="hljs-keyword">await</span> dc.file.addFile(
file,
<span class="hljs-string">'encryption-key'</span>,
<span class="hljs-function">(<span class="hljs-params">status, size</span>) =></span> <span class="hljs-built_in">console</span>.log(<span class="hljs-string">`上传进度:<span class="hljs-subst">${status}</span>%, 大小:<span class="hljs-subst">${size}</span>`</span>)
);
<span class="hljs-built_in">console</span>.log(<span class="hljs-string">'上传结果:'</span>, result);
</div></code></pre>
<br>
<h4 id="getfilecid-decryptkey"><code>getFile(cid, decryptKey)</code></h4>
<p>获取文件内容。</p>
<p><strong>参数:</strong></p>
<ul>
<li><code>cid</code>: string - 文件内容标识符</li>
<li><code>decryptKey</code>: string - 解密密钥</li>
</ul>
<p><strong>返回:</strong> Promise<Uint8Array | undefined> - 文件字节数组</p>
<pre class="hljs"><code><div><span class="hljs-keyword">const</span> fileContent = <span class="hljs-keyword">await</span> dc.file.getFile(<span class="hljs-string">'QmFileHash...'</span>, <span class="hljs-string">'decryption-key'</span>);
<span class="hljs-keyword">if</span> (fileContent) {
<span class="hljs-built_in">console</span>.log(<span class="hljs-string">'文件内容字节长度:'</span>, fileContent.length);
}
</div></code></pre>
<br>
<h4 id="createfilestreamcid-decryptkey"><code>createFileStream(cid, decryptKey)</code></h4>
<p>创建文件可读流,适用于大文件处理。</p>
<p><strong>参数:</strong></p>
<ul>
<li><code>cid</code>: string - 文件内容标识符</li>
<li><code>decryptKey</code>: string - 解密密钥</li>
</ul>
<p><strong>返回:</strong> Promise<ReadableStream<Uint8Array> | null> - 文件可读流</p>
<pre class="hljs"><code><div><span class="hljs-keyword">const</span> stream = <span class="hljs-keyword">await</span> dc.file.createFileStream(<span class="hljs-string">'QmFileHash...'</span>, <span class="hljs-string">'decryption-key'</span>);
<span class="hljs-keyword">if</span> (stream) {
<span class="hljs-comment">// 使用可读流处理文件</span>
<span class="hljs-keyword">const</span> reader = stream.getReader();
<span class="hljs-keyword">while</span> (<span class="hljs-literal">true</span>) {
<span class="hljs-keyword">const</span> { done, value } = <span class="hljs-keyword">await</span> reader.read();
<span class="hljs-keyword">if</span> (done) <span class="hljs-keyword">break</span>;
<span class="hljs-built_in">console</span>.log(<span class="hljs-string">'读取数据块:'</span>, value.length);
}
}
</div></code></pre>
<br>
<h4 id="getseekablefilestreamipfspath-decryptkey"><code>getSeekableFileStream(ipfsPath, decryptKey)</code></h4>
<p>获取支持随机访问的文件流,适用于视频播放等场景。</p>
<p><strong>参数:</strong></p>
<ul>
<li><code>ipfsPath</code>: string - IPFS路径或CID</li>
<li><code>decryptKey</code>: string - 解密密钥</li>
</ul>
<p><strong>返回:</strong> Promise<SeekableFileStream> - 可随机访问的文件流</p>
<pre class="hljs"><code><div><span class="hljs-keyword">const</span> seekableStream = <span class="hljs-keyword">await</span> dc.file.getSeekableFileStream(<span class="hljs-string">'/ipfs/QmFileHash...'</span>, <span class="hljs-string">'decryption-key'</span>);
<span class="hljs-comment">// 从文件的第1000字节开始读取</span>
<span class="hljs-keyword">const</span> data = <span class="hljs-keyword">await</span> seekableStream.read(<span class="hljs-number">1000</span>, <span class="hljs-number">1024</span>);
<span class="hljs-built_in">console</span>.log(<span class="hljs-string">'读取的数据:'</span>, data);
</div></code></pre>
<br>
<h4 id="clearfilecachepathname"><code>clearFileCache(pathname)</code></h4>
<p>清理文件缓存。</p>
<p><strong>参数:</strong></p>
<ul>
<li><code>pathname</code>: string (可选) - 特定文件路径,不提供则清除所有缓存</li>
</ul>
<p><strong>返回:</strong> void</p>
<pre class="hljs"><code><div><span class="hljs-comment">// 清理特定文件缓存</span>
dc.file.clearFileCache(<span class="hljs-string">'/ipfs/QmFileHash...'</span>);
<span class="hljs-comment">// 清理所有缓存</span>
dc.file.clearFileCache();
</div></code></pre>
<p><br><br></p>
<h2 id="%E5%AE%A2%E6%88%B7%E7%AB%AF%E6%A8%A1%E5%9D%97-client">客户端模块 (client)</h2>
<p>提供底层节点连接和网络通信功能。</p>
<h3 id="%E6%96%B9%E6%B3%95">方法</h3>
<h4 id="gethostid"><code>getHostID()</code></h4>
<p>获取当前连接的节点ID和客户端公网地址。</p>
<p><strong>参数:</strong> 无</p>
<p><strong>返回:</strong> Promise<[{ peerID: string; reqAddr: string } | null, Error | null]> - 主机信息和错误</p>
<pre class="hljs"><code><div><span class="hljs-keyword">const</span> [hostInfo, error] = <span class="hljs-keyword">await</span> dc.client.getHostID();
<span class="hljs-keyword">if</span> (hostInfo) {
<span class="hljs-built_in">console</span>.log(<span class="hljs-string">'节点ID:'</span>, hostInfo.peerID);
<span class="hljs-built_in">console</span>.log(<span class="hljs-string">'客户端公网地址:'</span>, hostInfo.reqAddr);
}
</div></code></pre>
<p><br><br></p>
<h2 id="%E6%B6%88%E6%81%AF%E6%A8%A1%E5%9D%97-message">消息模块 (message)</h2>
<p>提供用户消息盒子的发送和接收功能。</p>
<h3 id="%E6%96%B9%E6%B3%95">方法</h3>
<h4 id="sendmsgtouserboxreceiver-msg"><code>sendMsgToUserBox(receiver, msg)</code></h4>
<p>向用户消息盒子发送消息。</p>
<p><strong>参数:</strong></p>
<ul>
<li><code>receiver</code>: string - 接收者的公钥</li>
<li><code>msg</code>: string - 消息内容</li>
</ul>
<p><strong>返回:</strong> Promise<any> - 发送结果,包含消息ID和时间戳</p>
<pre class="hljs"><code><div><span class="hljs-keyword">const</span> result = <span class="hljs-keyword">await</span> dc.message.sendMsgToUserBox(<span class="hljs-string">'receiver-pubkey'</span>, <span class="hljs-string">'Hello World!'</span>);
<span class="hljs-built_in">console</span>.log(<span class="hljs-string">'消息发送结果:'</span>, result);
</div></code></pre>
<br>
<h4 id="getmsgfromuserboxlimit"><code>getMsgFromUserBox(limit)</code></h4>
<p>获取当前用户消息盒子中的消息。</p>
<p><strong>参数:</strong></p>
<ul>
<li><code>limit</code>: number (可选) - 返回消息的最大数量</li>
</ul>
<p><strong>返回:</strong> Promise<any> - 消息列表</p>
<pre class="hljs"><code><div><span class="hljs-keyword">const</span> messages = <span class="hljs-keyword">await</span> dc.message.getMsgFromUserBox(<span class="hljs-number">10</span>);
<span class="hljs-built_in">console</span>.log(<span class="hljs-string">'收到的消息:'</span>, messages);
</div></code></pre>
<p><br><br></p>
<h2 id="%E8%AF%84%E8%AE%BA%E6%A8%A1%E5%9D%97-comment">评论模块 (comment)</h2>
<p>提供主题评论功能。</p>
<h3 id="%E6%96%B9%E6%B3%95">方法</h3>
<h4 id="adduseroffchainspace"><code>addUserOffChainSpace()</code></h4>
<p>为当前用户添加链下评论空间。当用户首次登录成功后,应该先调用此方法来完成链下空间配置。如果是集成sdk,sdk会自动完成这部操作。</p>
<p><strong>参数:</strong> 无</p>
<p><strong>返回:</strong> Promise<[boolean | null, Error | null]> - 操作结果</p>
<pre class="hljs"><code><div><span class="hljs-keyword">const</span> [success, error] = <span class="hljs-keyword">await</span> dc.comment.addUserOffChainSpace();
<span class="hljs-keyword">if</span> (success) {
<span class="hljs-built_in">console</span>.log(<span class="hljs-string">'成功添加用户评论空间'</span>);
}
</div></code></pre>
<br>
<h4 id="adduseroffchainoptimestimes-vaccount"><code>addUserOffChainOpTimes(times, vaccount?)</code></h4>
<p>为用户添加链下操作次数,DC为了提升性能,用户发布评论等操作,无需上链,用于发布评论等链下操作次数,当用户首次登录成功后,应该先调用此方法来完成链下操作次数配置。如果是集成sdk,sdk会自动完成这部操作。</p>
<p><strong>参数:</strong></p>
<ul>
<li><code>times</code>: number - 操作次数</li>
<li><code>vaccount</code>: string (可选) - 用户的虚拟账号</li>
</ul>
<p><strong>返回:</strong> Promise<[boolean | null, Error | null]> - 操作结果, 0:成功 1:评论空间没有配置 2:评论空间不足 3:评论数据同步中</p>
<pre class="hljs"><code><div><span class="hljs-keyword">const</span> [result, error] = <span class="hljs-keyword">await</span> dc.comment.addUserOffChainOpTimes(<span class="hljs-number">10</span>);
<span class="hljs-keyword">if</span> (result) {
<span class="hljs-built_in">console</span>.log(<span class="hljs-string">'成功添加用户链下操作次数'</span>);
}
</div></code></pre>
<br>
<h4 id="getthemeobjthemeauthor-startheight-direction-offset-limit-seekkey"><code>getThemeObj(themeAuthor, startHeight, direction, offset, limit, seekKey)</code></h4>
<p>获取指定用户的主题对象列表。</p>
<p><strong>参数:</strong> Promise<string> - 主题对象列表</p>
<pre class="hljs"><code><div><span class="hljs-keyword">const</span> themes = <span class="hljs-keyword">await</span> dc.comment.getThemeObj(
<span class="hljs-string">'author-pubkey'</span>,
<span class="hljs-number">0</span>, <span class="hljs-comment">// 起始高度</span>
<span class="hljs-number">0</span>, <span class="hljs-comment">// 从新到旧</span>
<span class="hljs-number">0</span>, <span class="hljs-comment">// 不偏移</span>
<span class="hljs-number">50</span>, <span class="hljs-comment">// 最多返回50条</span>
<span class="hljs-string">''</span> <span class="hljs-comment">// 不使用起始键</span>
);
<span class="hljs-built_in">console</span>.log(<span class="hljs-string">'主题对象列表:'</span>, themes);
</div></code></pre>
<br>
<h4 id="addthemeobjtheme-openflag-commentspace"><code>addThemeObj(theme, openFlag, commentSpace)</code></h4>
<p>为指定主题开通评论功能。</p>
<p><strong>参数:</strong></p>
<ul>
<li><code>theme</code>: string - 主题标识符</li>
<li><code>openFlag</code>: OpenFlag - 评论可见性标志</li>
<li><code>commentSpace</code>: number (可选) - 评论空间大小,默认50MB</li>
</ul>
<p><strong>返回:</strong> Promise<[number | null, Error | null]> - 状态码和错误</p>
<pre class="hljs"><code><div><span class="hljs-keyword">const</span> [status, error] = <span class="hljs-keyword">await</span> dc.comment.addThemeObj(
<span class="hljs-string">'my-article-1'</span>,
OpenFlag.PUBLIC, <span class="hljs-comment">// 评论设为公开</span>
<span class="hljs-number">100</span> * <span class="hljs-number">1024</span> * <span class="hljs-number">1024</span> <span class="hljs-comment">// 100MB</span>
);
<span class="hljs-keyword">if</span> (status === <span class="hljs-number">0</span>) {
<span class="hljs-built_in">console</span>.log(<span class="hljs-string">'成功开通评论功能'</span>);
}
</div></code></pre>
<br>
<h4 id="addthemespacetheme-addspace"><code>addThemeSpace(theme, addSpace)</code></h4>
<p>为已开通评论的主题增加评论空间。</p>
<p><strong>参数:</strong></p>
<ul>
<li><code>theme</code>: string - 主题标识符</li>
<li><code>addSpace</code>: number - 增加的空间大小(字节)</li>
</ul>
<p><strong>返回:</strong> Promise<any> - 操作结果</p>
<pre class="hljs"><code><div><span class="hljs-keyword">const</span> result = <span class="hljs-keyword">await</span> dc.comment.addThemeSpace(<span class="hljs-string">'my-article-1'</span>, <span class="hljs-number">50</span> * <span class="hljs-number">1024</span> * <span class="hljs-number">1024</span>); <span class="hljs-comment">// 增加50MB</span>
<span class="hljs-built_in">console</span>.log(<span class="hljs-string">'增加空间结果:'</span>, result);
</div></code></pre>
<br>
<h4 id="publishcommenttothemetheme-themeauthor-commenttype-comment-refercommentkey-openflag"><code>publishCommentToTheme(theme, themeAuthor, commentType, comment, refercommentkey, openFlag)</code></h4>
<p>发表评论到指定主题。</p>
<p><strong>参数:</strong></p>
<ul>
<li><code>theme</code>: string - 主题标识符</li>
<li><code>themeAuthor</code>: string - 主题作者的公钥</li>
<li><code>commentType</code>: number - 评论类型 (0:普通评论, 1:回复评论等)</li>
<li><code>comment</code>: string - 评论内容</li>
<li><code>refercommentkey</code>: string (可选) - 引用评论的键</li>
<li><code>openFlag</code>: number (可选) - 评论可见性</li>
</ul>
<p><strong>返回:</strong> Promise<any> - 发布结果</p>
<pre class="hljs"><code><div><span class="hljs-keyword">const</span> result = <span class="hljs-keyword">await</span> dc.comment.publishCommentToTheme(
<span class="hljs-string">'my-article-1'</span>,
<span class="hljs-string">'author-pubkey'</span>,
<span class="hljs-number">0</span>, <span class="hljs-comment">// 普通评论</span>
<span class="hljs-string">'这是一条评论内容'</span>,
<span class="hljs-string">''</span>, <span class="hljs-comment">// 不引用其他评论</span>
OpenFlag.PUBLIC <span class="hljs-comment">// 公开评论</span>
);
<span class="hljs-built_in">console</span>.log(<span class="hljs-string">'评论发布结果:'</span>, result);
</div></code></pre>
<br>
<h4 id="deleteselfcommenttheme-themeauthor-commentkey"><code>deleteSelfComment(theme, themeAuthor, commentKey)</code></h4>
<p>删除自己发布的评论。</p>
<p><strong>参数:</strong></p>
<ul>
<li><code>theme</code>: string - 主题标识符</li>
<li><code>themeAuthor</code>: string - 主题作者的公钥</li>
<li><code>commentKey</code>: string - 评论的唯一键</li>
</ul>
<p><strong>返回:</strong> Promise<any> - 删除结果</p>
<pre class="hljs"><code><div><span class="hljs-keyword">const</span> result = <span class="hljs-keyword">await</span> dc.comment.deleteSelfComment(
<span class="hljs-string">'my-article-1'</span>,
<span class="hljs-string">'author-pubkey'</span>,
<span class="hljs-string">'comment-key-123'</span>
);
<span class="hljs-built_in">console</span>.log(<span class="hljs-string">'删除评论结果:'</span>, result);
</div></code></pre>
<br>
<h4 id="getthemecommentstheme-themeauthor-startheight-direction-offset-limit-seekkey"><code>getThemeComments(theme, themeAuthor, startHeight, direction, offset, limit, seekKey)</code></h4>
<p>获取指定主题的评论列表,无法查询作者设置为私密的评论</p>
<p><strong>参数:</strong></p>
<ul>
<li><code>theme</code>: string - 主题标识符</li>
<li><code>themeAuthor</code>: string - 主题作者的公钥</li>
<li><code>startHeight</code>: number (可选) - 查询起始高度,默认0</li>
<li><code>direction</code>: number (可选) - 查询方向 (0:从新到旧, 1:从旧到新),默认0</li>
<li><code>offset</code>: number (可选) - 结果集偏移量,默认0</li>
<li><code>limit</code>: number (可选) - 最大返回数量,默认100</li>
<li><code>seekKey</code>: string (可选) - 查询的起始键,格式为"blockheight/key"</li>
</ul>
<p><strong>返回:</strong> Promise<any> - 评论列表</p>
<pre class="hljs"><code><div><span class="hljs-keyword">const</span> comments = <span class="hljs-keyword">await</span> dc.comment.getThemeComments(
<span class="hljs-string">'my-article-1'</span>,
<span class="hljs-string">'author-pubkey'</span>,
<span class="hljs-number">0</span>, <span class="hljs-comment">// 起始高度</span>
<span class="hljs-number">0</span>, <span class="hljs-comment">// 从新到旧</span>
<span class="hljs-number">0</span>, <span class="hljs-comment">// 不偏移</span>
<span class="hljs-number">50</span> <span class="hljs-comment">// 最多返回50条</span>
);
<span class="hljs-built_in">console</span>.log(<span class="hljs-string">'主题评论:'</span>, comments);
</div></code></pre>
<br>
<h4 id="configauth-themeauthor-theme-authpubkey-permission-remark-vaccount"><code>configAuth( themeAuthor, theme, authPubkey, permission, remark, vaccount)</code></h4>
<p>配置主题的授权信息,即为其他用户授权评论权限。</p>
<p><strong>参数:</strong></p>
<ul>
<li><code>themeAuthor</code>: string - 主题作者的公钥</li>
<li><code>theme</code>: string - 主题名称</li>
<li><code>authPubkey</code>: string - 被授权者的公钥</li>
<li><code>permission</code>: ThemePermission - 权限级别 (1:只读, 3:读写)</li>
<li><code>remark</code>: string - 备注信息</li>
<li><code>vaccount</code>: string (可选) - 虚拟账户</li>
</ul>
<p><strong>返回:</strong> Promise<[number, Error | null]> - 状态码和错误信息</p>
<pre class="hljs"><code><div><span class="hljs-keyword">const</span> [status, error] = <span class="hljs-keyword">await</span> dc.comment.configAuth(
<span class="hljs-string">'theme-author-pubkey'</span>,
<span class="hljs-string">'my-article-1'</span>,
<span class="hljs-string">'user-to-authorize-pubkey'</span>,
ThemePermission.READ, <span class="hljs-comment">// 只读权限 </span>
<span class="hljs-string">'测试授权'</span>
);
<span class="hljs-keyword">if</span> (status === <span class="hljs-number">0</span>) {
<span class="hljs-built_in">console</span>.log(<span class="hljs-string">'授权配置成功'</span>);
}
</div></code></pre>
<pre class="hljs"><code><div>
<span class="hljs-comment">/**
* 主题的操作权限
*/</span>
<span class="hljs-keyword">export</span> <span class="hljs-keyword">enum</span> ThemePermission {
<span class="hljs-comment">/** 无权限 */</span>
NONE = <span class="hljs-number">0</span>,
<span class="hljs-comment">/** 申请权限 */</span>
APPLY,
<span class="hljs-comment">/** 读权限 */</span>
READ,
<span class="hljs-comment">/** 写权限 */</span>
WRITE,
<span class="hljs-comment">/** 管理员权限 */</span>
ADMIN,
<span class="hljs-comment">/**
* 只写权限
* 不允许用户修改remark或者物联网设备上报数据使用
* 权限后面跟随分组列表,以逗号分隔
*/</span>
ONLY_WRITE,
<span class="hljs-comment">/** 物联网管理人员相关权限,具体权限后续可扩展 */</span>
DEVICE,
<span class="hljs-comment">/** 不存在 */</span>
NOT_EXIST,
<span class="hljs-comment">/** 查询权限,系统可以查询数据,用户只能查询自己的授权数据 */</span>
QUERY
}
</div></code></pre>
<br>
<h4 id="getauthlistthemeauthor-theme-vaccount"><code>getAuthList(themeAuthor, theme, vaccount)</code></h4>
<p>获取指定主题的授权列表</p>
<p><strong>参数:</strong></p>
<ul>
<li><code>themeAuthor</code>: string - 主题作者的公钥</li>
<li><code>theme</code>: string - 主题/对象标识符</li>
<li><code>vaccount</code>: string (可选) - 虚拟账户</li>
</ul>
<p><strong>返回:</strong> Promise<[ThemeAuthInfo[] | null, ThemeComment[] | null, Error | null]> - 存授权列表、评论列表格式的授权列表(带原始签名)和错误信息</p>
<pre class="hljs"><code><div><span class="hljs-keyword">const</span> [authList, commentList, error] = <span class="hljs-keyword">await</span> dc.comment.getAuthList(
<span class="hljs-string">'theme-author-pubkey'</span>,
<span class="hljs-string">'my-article-1'</span>,
<span class="hljs-string">'virtual-account-id'</span> <span class="hljs-comment">// 可选</span>
);
<span class="hljs-keyword">if</span> (authList) {
<span class="hljs-built_in">console</span>.log(<span class="hljs-string">'授权列表:'</span>, authList);
<span class="hljs-built_in">console</span>.log(<span class="hljs-string">'评论格式列表:'</span>, commentList);
}
</div></code></pre>
<br>
<h4 id="getusercommentsuserpubkey-startheight-direction-offset-limit-seekkey"><code>getUserComments(userPubkey, startHeight, direction, offset, limit, seekKey)</code></h4>
<p>获取指定用户发布的评论列表,无法查询用户设置为私密的评论</p>
<p><strong>参数:</strong></p>
<ul>
<li><code>userPubkey</code>: string - 用户公钥</li>
<li><code>startHeight</code>: number (可选) - 查询起始高度,默认0</li>
<li><code>direction</code>: number (可选) - 查询方向 (0:从新到旧, 1:从旧到新),默认0</li>
<li><code>offset</code>: number (可选) - 结果集偏移量,默认0</li>