web-dc-api 0.1.4 → 0.1.6
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/dist/cjs/index.js +1 -1
- package/dist/dc.min.js +1 -1
- package/dist/esm/index.js +1 -1
- package/dist/index.d.ts +934 -878
- package/package.json +4 -8
- package/dist/cjs/helia-core-B1Xqha7a.js +0 -1
- package/dist/cjs/helia-core-D8Uv1KjQ.js +0 -1
- package/dist/cjs/polkadot-api-7PhQf3ws.js +0 -1
- package/dist/cjs/polkadot-api-CtrJVWuZ.js +0 -1
- package/dist/esm/chunks/helia-core-BxMqyK2Y.js +0 -1
- package/dist/esm/chunks/helia-core-DMXRpcO-.js +0 -1
- package/dist/esm/chunks/polkadot-api-5Y9Bw8VT.js +0 -1
- package/dist/esm/chunks/polkadot-api-D69Ioun_.js +0 -1
- package/lib/common/blowfish/block.ts +0 -259
- package/lib/common/blowfish/cipher.ts +0 -144
- package/lib/common/blowfish/const.ts +0 -195
- package/lib/common/chain.ts +0 -469
- package/lib/common/commonclient.ts +0 -202
- package/lib/common/constants.ts +0 -55
- package/lib/common/dc-key/ed25519.ts +0 -343
- package/lib/common/dc-key/keyManager.ts +0 -424
- package/lib/common/dcapi.ts +0 -98
- package/lib/common/dcutil.ts +0 -627
- package/lib/common/define.ts +0 -70
- package/lib/common/error.ts +0 -67
- package/lib/common/grpc-dc.ts +0 -104
- package/lib/common/module-system.ts +0 -184
- package/lib/common/service-worker.ts +0 -234
- package/lib/common/types/types.ts +0 -344
- package/lib/dc.ts +0 -701
- package/lib/implements/account/client.ts +0 -185
- package/lib/implements/account/manager.ts +0 -683
- package/lib/implements/aiproxy/client.ts +0 -357
- package/lib/implements/aiproxy/manager.ts +0 -670
- package/lib/implements/cache/client.ts +0 -105
- package/lib/implements/cache/manager.ts +0 -127
- package/lib/implements/comment/client.ts +0 -982
- package/lib/implements/comment/manager.ts +0 -1151
- package/lib/implements/dc/client.ts +0 -51
- package/lib/implements/dc/manager.ts +0 -33
- package/lib/implements/file/client.ts +0 -253
- package/lib/implements/file/file-cache-manager.ts +0 -142
- package/lib/implements/file/manager.ts +0 -1240
- package/lib/implements/file/seekableFileStream.ts +0 -344
- package/lib/implements/file/streamwriter.ts +0 -322
- package/lib/implements/keyvalue/client.ts +0 -376
- package/lib/implements/keyvalue/manager.ts +0 -759
- package/lib/implements/message/client.ts +0 -250
- package/lib/implements/message/manager.ts +0 -215
- package/lib/implements/threaddb/cbor/coding.ts +0 -62
- package/lib/implements/threaddb/cbor/event.ts +0 -336
- package/lib/implements/threaddb/cbor/node.ts +0 -542
- package/lib/implements/threaddb/cbor/record.ts +0 -398
- package/lib/implements/threaddb/common/AsyncMutex.ts +0 -24
- package/lib/implements/threaddb/common/addrinfo.ts +0 -135
- package/lib/implements/threaddb/common/dispatcher.ts +0 -81
- package/lib/implements/threaddb/common/idbstore-adapter.ts +0 -260
- package/lib/implements/threaddb/common/json-patcher.ts +0 -204
- package/lib/implements/threaddb/common/key.ts +0 -290
- package/lib/implements/threaddb/common/level-adapter.ts +0 -235
- package/lib/implements/threaddb/common/lineReader.ts +0 -79
- package/lib/implements/threaddb/common/logstore.ts +0 -215
- package/lib/implements/threaddb/common/transformed-datastore.ts +0 -308
- package/lib/implements/threaddb/core/app.ts +0 -206
- package/lib/implements/threaddb/core/core.ts +0 -230
- package/lib/implements/threaddb/core/db.ts +0 -249
- package/lib/implements/threaddb/core/event.ts +0 -54
- package/lib/implements/threaddb/core/head.ts +0 -89
- package/lib/implements/threaddb/core/identity.ts +0 -171
- package/lib/implements/threaddb/core/logstore.ts +0 -137
- package/lib/implements/threaddb/core/options.ts +0 -14
- package/lib/implements/threaddb/core/record.ts +0 -54
- package/lib/implements/threaddb/db/collection.ts +0 -1910
- package/lib/implements/threaddb/db/db.ts +0 -698
- package/lib/implements/threaddb/db/json2Query.ts +0 -192
- package/lib/implements/threaddb/db/query.ts +0 -524
- package/lib/implements/threaddb/dbclient.ts +0 -543
- package/lib/implements/threaddb/dbmanager.ts +0 -1906
- package/lib/implements/threaddb/lsstoreds/addr_book.ts +0 -549
- package/lib/implements/threaddb/lsstoreds/cache.ts +0 -36
- package/lib/implements/threaddb/lsstoreds/cyclic_batch.ts +0 -87
- package/lib/implements/threaddb/lsstoreds/global.ts +0 -151
- package/lib/implements/threaddb/lsstoreds/headbook.ts +0 -373
- package/lib/implements/threaddb/lsstoreds/keybook.ts +0 -297
- package/lib/implements/threaddb/lsstoreds/logstore.ts +0 -29
- package/lib/implements/threaddb/lsstoreds/metadata.ts +0 -223
- package/lib/implements/threaddb/net/define.ts +0 -149
- package/lib/implements/threaddb/net/grpcClient.ts +0 -589
- package/lib/implements/threaddb/net/grpcserver.ts +0 -146
- package/lib/implements/threaddb/net/net.ts +0 -2047
- package/lib/implements/threaddb/pb/lstore.proto +0 -38
- package/lib/implements/threaddb/pb/lstore.ts +0 -393
- package/lib/implements/threaddb/pb/lstore_pb.d.ts +0 -433
- package/lib/implements/threaddb/pb/lstore_pb.js +0 -1085
- package/lib/implements/threaddb/pb/net.proto +0 -194
- package/lib/implements/threaddb/pb/net_pb.d.ts +0 -2349
- package/lib/implements/threaddb/pb/net_pb.js +0 -5525
- package/lib/implements/threaddb/pb/proto-custom-types.ts +0 -212
- package/lib/implements/util/client.ts +0 -72
- package/lib/implements/util/manager.ts +0 -146
- package/lib/implements/wallet/manager.ts +0 -671
- package/lib/index.ts +0 -57
- package/lib/interfaces/DCContext.ts +0 -51
- package/lib/interfaces/aiproxy-interface.ts +0 -145
- package/lib/interfaces/auth-interface.ts +0 -118
- package/lib/interfaces/cache-interface.ts +0 -22
- package/lib/interfaces/client-interface.ts +0 -11
- package/lib/interfaces/comment-interface.ts +0 -167
- package/lib/interfaces/components/news-component.ts +0 -0
- package/lib/interfaces/database-interface.ts +0 -169
- package/lib/interfaces/file-interface.ts +0 -120
- package/lib/interfaces/index.ts +0 -10
- package/lib/interfaces/keyvalue-interface.ts +0 -156
- package/lib/interfaces/message-interface.ts +0 -22
- package/lib/interfaces/util-interface.ts +0 -31
- package/lib/modules/aiproxy-module.ts +0 -246
- package/lib/modules/auth-module.ts +0 -753
- package/lib/modules/cache-module.ts +0 -99
- package/lib/modules/client-module.ts +0 -71
- package/lib/modules/comment-module.ts +0 -429
- package/lib/modules/components/news-components.ts +0 -390
- package/lib/modules/database-module.ts +0 -598
- package/lib/modules/file-module.ts +0 -291
- package/lib/modules/index.ts +0 -13
- package/lib/modules/keyvalue-module.ts +0 -379
- package/lib/modules/message-module.ts +0 -107
- package/lib/modules/util-module.ts +0 -148
- package/lib/polyfills/process-env-browser.ts +0 -1
- package/lib/proto/datasource.ts +0 -93
- package/lib/proto/dcnet.proto +0 -1601
- package/lib/proto/dcnet_proto.d.ts +0 -22857
- package/lib/proto/dcnet_proto.js +0 -55204
- package/lib/proto/dcnet_proto_sparse.js +0 -55166
- package/lib/proto/oidfetch.proto +0 -25
- package/lib/proto/oidfetch_proto.d.ts +0 -585
- package/lib/proto/oidfetch_proto.js +0 -1247
- package/lib/serverless/babel-browser.ts +0 -39
- package/lib/serverless/base_entity.ts +0 -78
- package/lib/serverless/base_repository.ts +0 -414
- package/lib/serverless/browser_schema_extractor.ts +0 -283
- package/lib/serverless/decorator_factory.ts +0 -322
- package/lib/util/BrowserLineReader.ts +0 -73
- package/lib/util/base64.ts +0 -105
- package/lib/util/bcrypt.ts +0 -206
- package/lib/util/curve25519Encryption.ts +0 -418
- package/lib/util/dccrypt.ts +0 -73
- package/lib/util/logger.ts +0 -104
- package/lib/util/utils.ts +0 -289
|
@@ -1,250 +0,0 @@
|
|
|
1
|
-
|
|
2
|
-
import type { Client } from "../../common/dcapi";
|
|
3
|
-
import { Libp2pGrpcClient } from "grpc-libp2p-client";
|
|
4
|
-
import { dcnet } from "../../proto/dcnet_proto";
|
|
5
|
-
import { Errors } from "../../common/error";
|
|
6
|
-
import { toString as uint8ArrayToString } from "uint8arrays/to-string";
|
|
7
|
-
import { DCContext } from "../../../lib/interfaces/DCContext";
|
|
8
|
-
|
|
9
|
-
export class MessageClient {
|
|
10
|
-
client: Client;
|
|
11
|
-
receiverClient: Client | undefined;
|
|
12
|
-
context: DCContext;
|
|
13
|
-
|
|
14
|
-
constructor(
|
|
15
|
-
dcClient: Client,
|
|
16
|
-
context: DCContext,
|
|
17
|
-
receiverClient?: Client
|
|
18
|
-
) {
|
|
19
|
-
this.client = dcClient;
|
|
20
|
-
this.context = context;
|
|
21
|
-
this.receiverClient = receiverClient;
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
sendMsgToUserBox = async (userMsg: dcnet.pb.UserMsg): Promise<number> => {
|
|
25
|
-
const peerId = this.client.peerAddr.getPeerId();
|
|
26
|
-
if (!peerId) {
|
|
27
|
-
throw new Error("peerId is undefined");
|
|
28
|
-
}
|
|
29
|
-
if (!this.receiverClient) {
|
|
30
|
-
throw new Error("receiverClient is undefined");
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
// 用户节点auth签名
|
|
34
|
-
const authSignature = await this.getToUserBoxAuth(userMsg.signature);
|
|
35
|
-
|
|
36
|
-
// let peerPubkey = await extractPublicKeyFromPeerId(peerIdFromString(peerId))
|
|
37
|
-
// const verify = peerPubkey.verify(signature, authSignature)
|
|
38
|
-
|
|
39
|
-
const message = new dcnet.pb.SendMsgToUserBoxRequest({});
|
|
40
|
-
message.msg = userMsg;
|
|
41
|
-
message.AuthSignature = authSignature;
|
|
42
|
-
message.PeerId = new TextEncoder().encode(peerId.toString());
|
|
43
|
-
const messageBytes =
|
|
44
|
-
dcnet.pb.SendMsgToUserBoxRequest.encode(message).finish();
|
|
45
|
-
|
|
46
|
-
const grpcClient = new Libp2pGrpcClient(
|
|
47
|
-
this.receiverClient.p2pNode,
|
|
48
|
-
this.receiverClient.peerAddr,
|
|
49
|
-
this.receiverClient.token,
|
|
50
|
-
this.receiverClient.protocol
|
|
51
|
-
);
|
|
52
|
-
try {
|
|
53
|
-
const responseData = await grpcClient.unaryCall(
|
|
54
|
-
"/dcnet.pb.Service/SendMsgToUserBox",
|
|
55
|
-
messageBytes,
|
|
56
|
-
30000
|
|
57
|
-
);
|
|
58
|
-
console.log("SendMsgToUserBox responseData", responseData);
|
|
59
|
-
const decoded = dcnet.pb.SendMsgToUserBoxReply.decode(responseData);
|
|
60
|
-
console.log("SendMsgToUserBox decoded", decoded);
|
|
61
|
-
return decoded.flag;
|
|
62
|
-
} catch (error: any) {
|
|
63
|
-
if (error.message.indexOf(Errors.INVALID_TOKEN.message) != -1) {
|
|
64
|
-
// try to get token
|
|
65
|
-
const token = await this.receiverClient.GetToken(
|
|
66
|
-
this.context.appInfo.appId || '',
|
|
67
|
-
this.context.getPublicKey().string(),
|
|
68
|
-
(payload: Uint8Array): Promise<Uint8Array> => {
|
|
69
|
-
return this.context.sign(payload);
|
|
70
|
-
}
|
|
71
|
-
);
|
|
72
|
-
if (!token) {
|
|
73
|
-
throw new Error(Errors.INVALID_TOKEN.message);
|
|
74
|
-
}
|
|
75
|
-
const responseData = await grpcClient.unaryCall(
|
|
76
|
-
"/dcnet.pb.Service/SendMsgToUserBox",
|
|
77
|
-
messageBytes,
|
|
78
|
-
30000
|
|
79
|
-
);
|
|
80
|
-
console.log("SendMsgToUserBox responseData", responseData);
|
|
81
|
-
const decoded = dcnet.pb.SendMsgToUserBoxReply.decode(responseData);
|
|
82
|
-
console.log("SendMsgToUserBox decoded", decoded);
|
|
83
|
-
return decoded.flag;
|
|
84
|
-
}
|
|
85
|
-
console.error("SendMsgToUserBox error:", error);
|
|
86
|
-
throw error;
|
|
87
|
-
}
|
|
88
|
-
};
|
|
89
|
-
|
|
90
|
-
private getToUserBoxAuth = async (signature: Uint8Array): Promise<Uint8Array> => {
|
|
91
|
-
const message = new dcnet.pb.GetToUserBoxAuthRequest({});
|
|
92
|
-
message.msgSignature = signature;
|
|
93
|
-
const messageBytes =
|
|
94
|
-
dcnet.pb.GetToUserBoxAuthRequest.encode(message).finish();
|
|
95
|
-
const grpcClient = new Libp2pGrpcClient(
|
|
96
|
-
this.client.p2pNode,
|
|
97
|
-
this.client.peerAddr,
|
|
98
|
-
this.client.token,
|
|
99
|
-
this.client.protocol
|
|
100
|
-
);
|
|
101
|
-
try {
|
|
102
|
-
const responseData = await grpcClient.unaryCall(
|
|
103
|
-
"/dcnet.pb.Service/GetToUserBoxAuth",
|
|
104
|
-
messageBytes,
|
|
105
|
-
30000
|
|
106
|
-
);
|
|
107
|
-
console.log("GetToUserBoxAuth responseData", responseData);
|
|
108
|
-
const decoded = dcnet.pb.GetToUserBoxAuthReply.decode(responseData);
|
|
109
|
-
console.log("GetToUserBoxAuth decoded", decoded);
|
|
110
|
-
return decoded.signature;
|
|
111
|
-
} catch (error: any) {
|
|
112
|
-
if (error.message.indexOf(Errors.INVALID_TOKEN.message) != -1) {
|
|
113
|
-
// try to get token
|
|
114
|
-
const token = await this.client.GetToken(
|
|
115
|
-
this.context.appInfo.appId || '',
|
|
116
|
-
this.context.getPublicKey().string(),
|
|
117
|
-
(payload: Uint8Array): Promise<Uint8Array> => {
|
|
118
|
-
return this.context.sign(payload);
|
|
119
|
-
}
|
|
120
|
-
);
|
|
121
|
-
if (!token) {
|
|
122
|
-
throw new Error(Errors.INVALID_TOKEN.message);
|
|
123
|
-
}
|
|
124
|
-
const responseData = await grpcClient.unaryCall(
|
|
125
|
-
"/dcnet.pb.Service/GetToUserBoxAuth",
|
|
126
|
-
messageBytes,
|
|
127
|
-
30000
|
|
128
|
-
);
|
|
129
|
-
console.log("GetToUserBoxAuth responseData", responseData);
|
|
130
|
-
const decoded = dcnet.pb.GetToUserBoxAuthReply.decode(responseData);
|
|
131
|
-
console.log("GetToUserBoxAuth decoded", decoded);
|
|
132
|
-
return decoded.signature;
|
|
133
|
-
}
|
|
134
|
-
console.error("GetToUserBoxAuth error:", error);
|
|
135
|
-
throw error;
|
|
136
|
-
}
|
|
137
|
-
};
|
|
138
|
-
|
|
139
|
-
async getMaxKeyFromUserBox(appId: string): Promise<string> {
|
|
140
|
-
const message = new dcnet.pb.GetMaxKeyFromUserBoxRequest();
|
|
141
|
-
message.appId = new TextEncoder().encode(appId);
|
|
142
|
-
const messageBytes =
|
|
143
|
-
dcnet.pb.GetMaxKeyFromUserBoxRequest.encode(message).finish();
|
|
144
|
-
try {
|
|
145
|
-
const grpcClient = new Libp2pGrpcClient(
|
|
146
|
-
this.client.p2pNode,
|
|
147
|
-
this.client.peerAddr,
|
|
148
|
-
this.client.token,
|
|
149
|
-
this.client.protocol
|
|
150
|
-
);
|
|
151
|
-
const responseData = await grpcClient.unaryCall(
|
|
152
|
-
"/dcnet.pb.Service/GetMaxKeyFromUserBox",
|
|
153
|
-
messageBytes,
|
|
154
|
-
30000
|
|
155
|
-
);
|
|
156
|
-
const decoded = dcnet.pb.GetMaxKeyFromUserBoxReply.decode(responseData);
|
|
157
|
-
return decoded.maxkey ? uint8ArrayToString(decoded.maxkey) : '';
|
|
158
|
-
} catch (error: any) {
|
|
159
|
-
if (error.message.indexOf(Errors.INVALID_TOKEN.message) != -1) {
|
|
160
|
-
// try to get token
|
|
161
|
-
const token = await this.client.GetToken(
|
|
162
|
-
appId,
|
|
163
|
-
this.context.getPublicKey().string(),
|
|
164
|
-
(payload: Uint8Array): Promise<Uint8Array> => {
|
|
165
|
-
return this.context.sign(payload);
|
|
166
|
-
}
|
|
167
|
-
);
|
|
168
|
-
if (!token) {
|
|
169
|
-
throw new Error(Errors.INVALID_TOKEN.message);
|
|
170
|
-
}
|
|
171
|
-
const grpcClient = new Libp2pGrpcClient(
|
|
172
|
-
this.client.p2pNode,
|
|
173
|
-
this.client.peerAddr,
|
|
174
|
-
this.client.token,
|
|
175
|
-
this.client.protocol
|
|
176
|
-
);
|
|
177
|
-
const responseData = await grpcClient.unaryCall(
|
|
178
|
-
"/dcnet.pb.Service/GetMaxKeyFromUserBox",
|
|
179
|
-
messageBytes,
|
|
180
|
-
30000
|
|
181
|
-
);
|
|
182
|
-
const decoded = dcnet.pb.GetMaxKeyFromUserBoxReply.decode(responseData);
|
|
183
|
-
return decoded.maxkey ? uint8ArrayToString(decoded.maxkey) : '';
|
|
184
|
-
}
|
|
185
|
-
if(error.message.indexOf('datastore: key not found') != -1){
|
|
186
|
-
return '';
|
|
187
|
-
}
|
|
188
|
-
console.error("GetMaxKeyFromUserBox error:", error);
|
|
189
|
-
throw error;
|
|
190
|
-
}
|
|
191
|
-
}
|
|
192
|
-
|
|
193
|
-
async getMsgFromUserBox(
|
|
194
|
-
appId: string,
|
|
195
|
-
seekKey: string,
|
|
196
|
-
limit: number
|
|
197
|
-
): Promise<{ [k: string]: any; }> {
|
|
198
|
-
const message = new dcnet.pb.GetMsgFromUserBoxRequest();
|
|
199
|
-
message.appId = new TextEncoder().encode(appId);
|
|
200
|
-
message.blockheight = 0;
|
|
201
|
-
message.seekKey = new TextEncoder().encode(seekKey);
|
|
202
|
-
message.limit = limit;
|
|
203
|
-
const messageBytes =
|
|
204
|
-
dcnet.pb.GetMsgFromUserBoxRequest.encode(message).finish();
|
|
205
|
-
try {
|
|
206
|
-
const grpcClient = new Libp2pGrpcClient(
|
|
207
|
-
this.client.p2pNode,
|
|
208
|
-
this.client.peerAddr,
|
|
209
|
-
this.client.token,
|
|
210
|
-
this.client.protocol
|
|
211
|
-
);
|
|
212
|
-
const responseData = await grpcClient.unaryCall(
|
|
213
|
-
"/dcnet.pb.Service/GetMsgFromUserBox",
|
|
214
|
-
messageBytes,
|
|
215
|
-
30000
|
|
216
|
-
);
|
|
217
|
-
const decoded = dcnet.pb.GetMsgFromUserBoxReply.decode(responseData);
|
|
218
|
-
return decoded.toJSON();
|
|
219
|
-
} catch (error: any) {
|
|
220
|
-
if (error.message.indexOf(Errors.INVALID_TOKEN.message) != -1) {
|
|
221
|
-
// try to get token
|
|
222
|
-
const token = await this.client.GetToken(
|
|
223
|
-
appId,
|
|
224
|
-
this.context.getPublicKey().string(),
|
|
225
|
-
(payload: Uint8Array): Promise<Uint8Array> => {
|
|
226
|
-
return this.context.sign(payload);
|
|
227
|
-
}
|
|
228
|
-
);
|
|
229
|
-
if (!token) {
|
|
230
|
-
throw new Error(Errors.INVALID_TOKEN.message);
|
|
231
|
-
}
|
|
232
|
-
const grpcClient = new Libp2pGrpcClient(
|
|
233
|
-
this.client.p2pNode,
|
|
234
|
-
this.client.peerAddr,
|
|
235
|
-
this.client.token,
|
|
236
|
-
this.client.protocol
|
|
237
|
-
);
|
|
238
|
-
const responseData = await grpcClient.unaryCall(
|
|
239
|
-
"/dcnet.pb.Service/getMsgFromUserBox",
|
|
240
|
-
messageBytes,
|
|
241
|
-
30000
|
|
242
|
-
);
|
|
243
|
-
const decoded = dcnet.pb.GetMsgFromUserBoxReply.decode(responseData);
|
|
244
|
-
return decoded.toJSON();
|
|
245
|
-
}
|
|
246
|
-
console.error("getMsgFromUserBox error:", error);
|
|
247
|
-
throw error;
|
|
248
|
-
}
|
|
249
|
-
}
|
|
250
|
-
}
|
|
@@ -1,215 +0,0 @@
|
|
|
1
|
-
import { ChainUtil } from "../../common/chain";
|
|
2
|
-
import { DCConnectInfo } from "../../common/types/types";
|
|
3
|
-
import * as buffer from "buffer/";
|
|
4
|
-
import { MessageClient } from "./client";
|
|
5
|
-
import { CID } from "multiformats";
|
|
6
|
-
import { sha256 } from "multiformats/hashes/sha2";
|
|
7
|
-
import { Ed25519PubKey } from "../../common/dc-key/ed25519";
|
|
8
|
-
import { uint32ToLittleEndianBytes } from "../../util/utils";
|
|
9
|
-
import { DcUtil } from "../../common/dcutil";
|
|
10
|
-
import { dcnet } from "../../proto/dcnet_proto";
|
|
11
|
-
import { DCContext } from "../../../lib/interfaces/DCContext";
|
|
12
|
-
const { Buffer } = buffer;
|
|
13
|
-
// 错误定义
|
|
14
|
-
export class DCError extends Error {
|
|
15
|
-
constructor(message: string) {
|
|
16
|
-
super(message);
|
|
17
|
-
this.name = "DCError";
|
|
18
|
-
}
|
|
19
|
-
}
|
|
20
|
-
export const Errors = {
|
|
21
|
-
ErrNoDcPeerConnected: new DCError("no dc peer connected"),
|
|
22
|
-
ErrGetUserInfoError: new DCError("get user info error"),
|
|
23
|
-
ErrNoAccountPeerConnected: new DCError("no account peer connected"),
|
|
24
|
-
ErrNoReceiverPeerConnected: new DCError("no receiver peer connected"),
|
|
25
|
-
};
|
|
26
|
-
|
|
27
|
-
export class MessageManager {
|
|
28
|
-
private context: DCContext;
|
|
29
|
-
private chainUtil: ChainUtil;
|
|
30
|
-
private dc: DcUtil;
|
|
31
|
-
constructor(
|
|
32
|
-
dc: DcUtil,
|
|
33
|
-
chainUtil: ChainUtil,
|
|
34
|
-
context: DCContext
|
|
35
|
-
) {
|
|
36
|
-
this.dc = dc;
|
|
37
|
-
this.chainUtil = chainUtil;
|
|
38
|
-
this.context = context;
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
sendMsgToUserBox = async (
|
|
42
|
-
appId: string,
|
|
43
|
-
receiver: string,
|
|
44
|
-
msg: string
|
|
45
|
-
): Promise<[number | null, Error | null]> => {
|
|
46
|
-
try {
|
|
47
|
-
if (!this.context.AccountBackupDc.client) {
|
|
48
|
-
return [null, Errors.ErrNoAccountPeerConnected];
|
|
49
|
-
}
|
|
50
|
-
const receiverPubkey: Ed25519PubKey = Ed25519PubKey.edPubkeyFromStr(receiver)
|
|
51
|
-
const sendPublicKey = await this.context.getPublicKey();
|
|
52
|
-
|
|
53
|
-
const userMsg = await this.generateMsqBoxReq(
|
|
54
|
-
appId,
|
|
55
|
-
receiverPubkey,
|
|
56
|
-
msg
|
|
57
|
-
)
|
|
58
|
-
// 连接receiver节点
|
|
59
|
-
let receiverClient = await this.dc.connectToUserDcPeer(
|
|
60
|
-
receiverPubkey.raw,
|
|
61
|
-
);
|
|
62
|
-
if(!receiverClient){
|
|
63
|
-
return [null, Errors.ErrNoReceiverPeerConnected]
|
|
64
|
-
}
|
|
65
|
-
const token = await receiverClient.GetToken(
|
|
66
|
-
appId,
|
|
67
|
-
sendPublicKey.string(),
|
|
68
|
-
(payload: Uint8Array): Promise<Uint8Array> => {
|
|
69
|
-
return this.context.sign(payload);
|
|
70
|
-
}
|
|
71
|
-
);
|
|
72
|
-
receiverClient.token = token;
|
|
73
|
-
|
|
74
|
-
const messageClient = new MessageClient(
|
|
75
|
-
this.context.AccountBackupDc.client,
|
|
76
|
-
this.context,
|
|
77
|
-
receiverClient,
|
|
78
|
-
);
|
|
79
|
-
const reply = await messageClient.sendMsgToUserBox(
|
|
80
|
-
userMsg
|
|
81
|
-
);
|
|
82
|
-
return [reply, null];
|
|
83
|
-
} catch (error) {
|
|
84
|
-
throw error;
|
|
85
|
-
}
|
|
86
|
-
};
|
|
87
|
-
|
|
88
|
-
getMsgFromUserBox = async (
|
|
89
|
-
appId: string,
|
|
90
|
-
limit: number = 100
|
|
91
|
-
): Promise<[dcnet.pb.IUserMsg[] | null, Error | null]> => {
|
|
92
|
-
try {
|
|
93
|
-
if (!this.context.AccountBackupDc.client) {
|
|
94
|
-
return [null, Errors.ErrNoAccountPeerConnected];
|
|
95
|
-
}
|
|
96
|
-
const publicKey = await this.context.getPublicKey();
|
|
97
|
-
const publickey = publicKey.string()
|
|
98
|
-
|
|
99
|
-
const clients = await this.dc.connectToUserAllDcPeers(publicKey.raw);
|
|
100
|
-
if(!clients){
|
|
101
|
-
return [null, Errors.ErrNoDcPeerConnected]
|
|
102
|
-
}
|
|
103
|
-
let allMsgs: dcnet.pb.IUserMsg[] = [];
|
|
104
|
-
for (const client of clients) {
|
|
105
|
-
if (client) {
|
|
106
|
-
const peerId = client.peerAddr.getPeerId() || "";
|
|
107
|
-
const publicKeyString = this.context.getPublicKey().string();
|
|
108
|
-
// 获取token
|
|
109
|
-
if(!client.token) {
|
|
110
|
-
const token = await client.GetToken(
|
|
111
|
-
appId,
|
|
112
|
-
publicKeyString,
|
|
113
|
-
(payload: Uint8Array): Promise<Uint8Array> => {
|
|
114
|
-
return this.context.sign(payload);
|
|
115
|
-
}
|
|
116
|
-
);
|
|
117
|
-
client.token = token;
|
|
118
|
-
}
|
|
119
|
-
const messageClient = new MessageClient(
|
|
120
|
-
client,
|
|
121
|
-
this.context,
|
|
122
|
-
);
|
|
123
|
-
let maxKey = await messageClient.getMaxKeyFromUserBox(appId);
|
|
124
|
-
const userBoxMaxKeyStr = localStorage.getItem('userBoxMaxKey_' + publicKeyString) || '';
|
|
125
|
-
let userBoxMaxKey = userBoxMaxKeyStr ? JSON.parse(userBoxMaxKeyStr) : {};
|
|
126
|
-
if(maxKey){
|
|
127
|
-
let preMaxKey = userBoxMaxKey[publickey] || {};
|
|
128
|
-
preMaxKey[peerId] = maxKey;
|
|
129
|
-
}
|
|
130
|
-
let getFlag = true
|
|
131
|
-
while(getFlag){
|
|
132
|
-
try {
|
|
133
|
-
// 获取的maxkey 不等于 之前保存的maxkey
|
|
134
|
-
const res = await messageClient.getMsgFromUserBox(
|
|
135
|
-
appId,
|
|
136
|
-
maxKey,
|
|
137
|
-
limit
|
|
138
|
-
);
|
|
139
|
-
const list = res && res["msgs"]? res["msgs"] : [];
|
|
140
|
-
console.log('messageClient.getMsgFromUserBox list', list)
|
|
141
|
-
list.map((item: dcnet.pb.IUserMsg) => {
|
|
142
|
-
if(item && item.messageId) {
|
|
143
|
-
allMsgs.push(item)
|
|
144
|
-
}
|
|
145
|
-
})
|
|
146
|
-
if(list.length < limit){
|
|
147
|
-
if(maxKey) {
|
|
148
|
-
localStorage.setItem('userBoxMaxKey_' + publicKeyString, JSON.stringify(userBoxMaxKey))
|
|
149
|
-
}
|
|
150
|
-
getFlag = false
|
|
151
|
-
}
|
|
152
|
-
} catch (error) {
|
|
153
|
-
console.log('messageClient.getMsgFromUserBox error', error)
|
|
154
|
-
}
|
|
155
|
-
}
|
|
156
|
-
}
|
|
157
|
-
}
|
|
158
|
-
return [allMsgs, null]
|
|
159
|
-
} catch (error: any) {
|
|
160
|
-
return [null, error]
|
|
161
|
-
}
|
|
162
|
-
};
|
|
163
|
-
private generateMsqBoxReq = async (
|
|
164
|
-
appId: string,
|
|
165
|
-
receiverPubkey: Ed25519PubKey,
|
|
166
|
-
msg: string
|
|
167
|
-
): Promise<dcnet.pb.UserMsg> => {
|
|
168
|
-
const timeStamp = Date.now() * 1000000; // na timestamp
|
|
169
|
-
const preId = `${msg}${timeStamp}`;
|
|
170
|
-
|
|
171
|
-
// Create SHA2-256 hash
|
|
172
|
-
const preIdBytes = new TextEncoder().encode(preId);
|
|
173
|
-
const hash = await sha256.digest(preIdBytes);
|
|
174
|
-
|
|
175
|
-
// Create CID version 1 with raw codec
|
|
176
|
-
const cid = CID.create(1, 0x55, hash);
|
|
177
|
-
const messageIdValue = new TextEncoder().encode(cid.toString());
|
|
178
|
-
|
|
179
|
-
const appIdValue = new TextEncoder().encode(appId);
|
|
180
|
-
|
|
181
|
-
const sendPublicKey = await this.context.getPublicKey();
|
|
182
|
-
const sendPublicKeyValue = new TextEncoder().encode(sendPublicKey.string());
|
|
183
|
-
|
|
184
|
-
const receiverPubkeyValue = new TextEncoder().encode(receiverPubkey.string());
|
|
185
|
-
|
|
186
|
-
const blockHeight = await this.chainUtil.getBlockHeight() || 0;
|
|
187
|
-
const hValue: Uint8Array = uint32ToLittleEndianBytes(
|
|
188
|
-
blockHeight
|
|
189
|
-
);
|
|
190
|
-
const msgValue = new TextEncoder().encode(msg);
|
|
191
|
-
const encryptMsgValue = await receiverPubkey.encrypt(msgValue);
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
const preSign = new Uint8Array([
|
|
195
|
-
...messageIdValue,
|
|
196
|
-
...receiverPubkeyValue,
|
|
197
|
-
...appIdValue,
|
|
198
|
-
...hValue,
|
|
199
|
-
...encryptMsgValue,
|
|
200
|
-
]);
|
|
201
|
-
const signature = await this.context.sign(preSign);
|
|
202
|
-
|
|
203
|
-
const userMsg = new dcnet.pb.UserMsg({});
|
|
204
|
-
userMsg.appId = new TextEncoder().encode(appId);
|
|
205
|
-
userMsg.blockheight = blockHeight;
|
|
206
|
-
userMsg.encryptMsg = encryptMsgValue;
|
|
207
|
-
userMsg.messageId = messageIdValue;
|
|
208
|
-
userMsg.receiverPubkey = receiverPubkeyValue;
|
|
209
|
-
userMsg.senderPubkey = sendPublicKeyValue;
|
|
210
|
-
userMsg.signature = signature;
|
|
211
|
-
return userMsg;
|
|
212
|
-
}
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
}
|
|
@@ -1,62 +0,0 @@
|
|
|
1
|
-
|
|
2
|
-
import { SymmetricKey } from '../common/key'
|
|
3
|
-
import {Node} from './node';
|
|
4
|
-
import * as cbornode from './node';
|
|
5
|
-
import { IBlock } from '../core/core';
|
|
6
|
-
import * as dagCBOR from '@ipld/dag-cbor';
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
/**
|
|
12
|
-
* Encrypts a block's raw data with the given key and wraps it as a DAG-CBOR node
|
|
13
|
-
*
|
|
14
|
-
* @param block - The block to encode
|
|
15
|
-
* @param key - The encryption key
|
|
16
|
-
* @returns Promise resolving to a Node
|
|
17
|
-
*/
|
|
18
|
-
export async function encodeBlock(block: IBlock, key: SymmetricKey): Promise<Node> {
|
|
19
|
-
// Encrypt the block's raw data using the provided key
|
|
20
|
-
const coded = await key.encrypt(block.data());
|
|
21
|
-
return await cbornode.wrapObject(coded);
|
|
22
|
-
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
/**
|
|
26
|
-
* Decrypts a block's raw data with the given key and returns a DAG node
|
|
27
|
-
*
|
|
28
|
-
* @param block - The block to decode
|
|
29
|
-
* @param key - The decryption key
|
|
30
|
-
* @returns Promise resolving to a Node
|
|
31
|
-
*/
|
|
32
|
-
export async function decodeBlock(block: any, key?: SymmetricKey): Promise<Node> {
|
|
33
|
-
let obj = block
|
|
34
|
-
if (typeof block.data === 'function') {
|
|
35
|
-
obj = dagCBOR.decode(block.data());
|
|
36
|
-
}else {
|
|
37
|
-
try {
|
|
38
|
-
obj = dagCBOR.decode(block);
|
|
39
|
-
} catch (e) {
|
|
40
|
-
obj = block;
|
|
41
|
-
}
|
|
42
|
-
}
|
|
43
|
-
let encryptData :Uint8Array;
|
|
44
|
-
if (obj instanceof Uint8Array) {
|
|
45
|
-
encryptData = obj;
|
|
46
|
-
} else {
|
|
47
|
-
try {
|
|
48
|
-
encryptData = dagCBOR.decode(obj._data);
|
|
49
|
-
} catch (e) {
|
|
50
|
-
encryptData = obj._data;
|
|
51
|
-
}
|
|
52
|
-
}
|
|
53
|
-
let decrptData = encryptData;
|
|
54
|
-
// Decrypt the data using the provided key
|
|
55
|
-
if (key) {
|
|
56
|
-
decrptData = await key.decrypt(encryptData);
|
|
57
|
-
}
|
|
58
|
-
return cbornode.decode(decrptData);
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|