web-dc-api 0.1.5 → 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,105 +0,0 @@
|
|
|
1
|
-
import type { Multiaddr } from "@multiformats/multiaddr";
|
|
2
|
-
import { Client } from "../../common/dcapi";
|
|
3
|
-
import { dcnet } from "../../proto/dcnet_proto";
|
|
4
|
-
import { Libp2pGrpcClient } from "grpc-libp2p-client";
|
|
5
|
-
import { toString as uint8ArrayToString } from "uint8arrays/to-string";
|
|
6
|
-
import { DcUtil } from "../../../lib/common/dcutil";
|
|
7
|
-
import { DCContext } from "../../../lib/interfaces";
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
export class CacheClient {
|
|
13
|
-
client: Client;
|
|
14
|
-
context: DCContext;
|
|
15
|
-
dc: DcUtil;
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
constructor(context: DCContext,dc: DcUtil,dcClient: Client) {
|
|
19
|
-
this.client = dcClient;
|
|
20
|
-
this.context = context;
|
|
21
|
-
this.dc = dc;
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
async getCacheValue(peerAddr: Multiaddr, key: string): Promise<string> {
|
|
25
|
-
try {
|
|
26
|
-
if (this.client.p2pNode == null) {
|
|
27
|
-
throw new Error("p2pNode is null");
|
|
28
|
-
}
|
|
29
|
-
if (!peerAddr) {
|
|
30
|
-
peerAddr = this.client.peerAddr;
|
|
31
|
-
}else if (!peerAddr.equals(this.client.peerAddr)) {
|
|
32
|
-
await this.dc.connectToPeer(peerAddr.toString());
|
|
33
|
-
this.client = new Client(this.client.p2pNode,this.client.blockstore, peerAddr, this.client.protocol);
|
|
34
|
-
await this.client.GetToken(this.context.appInfo.appId, this.context.publicKey!.string(), this.context.sign, peerAddr);
|
|
35
|
-
}
|
|
36
|
-
const message = new dcnet.pb.GetCacheValueRequest({});
|
|
37
|
-
message.key = new TextEncoder().encode(key);
|
|
38
|
-
const messageBytes =
|
|
39
|
-
dcnet.pb.GetCacheValueRequest.encode(message).finish();
|
|
40
|
-
const grpcClient = new Libp2pGrpcClient(
|
|
41
|
-
this.client.p2pNode,
|
|
42
|
-
peerAddr || this.client.peerAddr,
|
|
43
|
-
this.client.token,
|
|
44
|
-
this.client.protocol
|
|
45
|
-
);
|
|
46
|
-
const responseData = await grpcClient.unaryCall(
|
|
47
|
-
"/dcnet.pb.Service/GetCacheValue",
|
|
48
|
-
messageBytes,
|
|
49
|
-
30000
|
|
50
|
-
);
|
|
51
|
-
const decoded = dcnet.pb.GetCacheValueReply.decode(responseData);
|
|
52
|
-
const encodedValue = uint8ArrayToString(decoded.value);
|
|
53
|
-
return encodedValue;
|
|
54
|
-
} catch (err) {
|
|
55
|
-
console.error("GetCacheValue error:", err);
|
|
56
|
-
throw err;
|
|
57
|
-
}
|
|
58
|
-
}
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
async setCacheKey(
|
|
62
|
-
value: string,
|
|
63
|
-
blockheight: number,
|
|
64
|
-
expire: number,
|
|
65
|
-
signature: Uint8Array,
|
|
66
|
-
peerAddr?: Multiaddr
|
|
67
|
-
): Promise<string> {
|
|
68
|
-
try {
|
|
69
|
-
if (this.client.p2pNode == null) {
|
|
70
|
-
throw new Error("p2pNode is null");
|
|
71
|
-
}
|
|
72
|
-
if (!peerAddr) {
|
|
73
|
-
peerAddr = this.client.peerAddr;
|
|
74
|
-
}
|
|
75
|
-
const message = new dcnet.pb.SetCacheKeyRequest({});
|
|
76
|
-
message.value = new TextEncoder().encode(value);
|
|
77
|
-
message.blockheight = blockheight;
|
|
78
|
-
message.expire = expire;
|
|
79
|
-
message.signature = signature;
|
|
80
|
-
const messageBytes = dcnet.pb.SetCacheKeyRequest.encode(message).finish();
|
|
81
|
-
const grpcClient = new Libp2pGrpcClient(
|
|
82
|
-
this.client.p2pNode,
|
|
83
|
-
peerAddr || this.client.peerAddr,
|
|
84
|
-
this.client.token,
|
|
85
|
-
this.client.protocol
|
|
86
|
-
);
|
|
87
|
-
const reply = await grpcClient.unaryCall(
|
|
88
|
-
"/dcnet.pb.Service/SetCacheKey",
|
|
89
|
-
messageBytes,
|
|
90
|
-
30000
|
|
91
|
-
);
|
|
92
|
-
const decoded = dcnet.pb.SetCacheKeyReply.decode(reply);
|
|
93
|
-
if (decoded.cacheKey) {
|
|
94
|
-
const result = uint8ArrayToString(decoded.cacheKey);
|
|
95
|
-
return result;
|
|
96
|
-
}
|
|
97
|
-
throw new Error("SetCacheKey failed,flag: " + decoded.flag);
|
|
98
|
-
} catch (err) {
|
|
99
|
-
console.error("SetCacheKey error:", err);
|
|
100
|
-
throw err;
|
|
101
|
-
}
|
|
102
|
-
}
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
}
|
|
@@ -1,127 +0,0 @@
|
|
|
1
|
-
import type { Multiaddr } from "@multiformats/multiaddr";
|
|
2
|
-
import { DCConnectInfo } from "../../common/types/types";
|
|
3
|
-
import { CacheClient } from "./client";
|
|
4
|
-
import { DcUtil } from "../../common/dcutil";
|
|
5
|
-
import { ChainUtil } from "../../common/chain";
|
|
6
|
-
import { sha256, uint32ToLittleEndianBytes } from "../../util/utils";
|
|
7
|
-
import { DCContext } from "../../../lib/interfaces/DCContext";
|
|
8
|
-
|
|
9
|
-
// 错误定义
|
|
10
|
-
export class CacheError extends Error {
|
|
11
|
-
constructor(message: string) {
|
|
12
|
-
super(message);
|
|
13
|
-
this.name = "CacheError";
|
|
14
|
-
}
|
|
15
|
-
}
|
|
16
|
-
export const Errors = {
|
|
17
|
-
ErrNoDcPeerConnected: new CacheError("no dc peer connected"),
|
|
18
|
-
ErrKeyNotValid: new CacheError("key not valid"),
|
|
19
|
-
// nodeAddr is null
|
|
20
|
-
ErrNodeAddrIsNull: new CacheError("nodeAddr is null"),
|
|
21
|
-
// chainUtil is null
|
|
22
|
-
ErrChainUtilIsNull: new CacheError("chainUtil is null"),
|
|
23
|
-
// account privatekey sign is null
|
|
24
|
-
ErrAccountPrivateSignIsNull: new CacheError("account privatekey sign is null"),
|
|
25
|
-
};
|
|
26
|
-
|
|
27
|
-
export class CacheManager{
|
|
28
|
-
dc: DcUtil;
|
|
29
|
-
chainUtil: ChainUtil | undefined;
|
|
30
|
-
context : DCContext | undefined;
|
|
31
|
-
constructor( dc: DcUtil, chainUtil?: ChainUtil, context?: DCContext) {
|
|
32
|
-
this.dc = dc;
|
|
33
|
-
this.chainUtil = chainUtil;
|
|
34
|
-
this.context = context;
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
async getCacheValue(key: string, peerAddr?: Multiaddr): Promise<[string | null, Error | null]> {
|
|
38
|
-
if (!this.context?.connectedDc?.client) {
|
|
39
|
-
return [null, Errors.ErrNoDcPeerConnected];
|
|
40
|
-
}
|
|
41
|
-
//解析出peerid与cachekey
|
|
42
|
-
const pkeys = key.split("/");
|
|
43
|
-
if (pkeys.length != 2) {
|
|
44
|
-
console.log("key format error!");
|
|
45
|
-
return [null, Errors.ErrKeyNotValid];
|
|
46
|
-
}
|
|
47
|
-
if (!this.context?.connectedDc.client) {
|
|
48
|
-
console.log("dcClient is null");
|
|
49
|
-
return [null, Errors.ErrNoDcPeerConnected];
|
|
50
|
-
}
|
|
51
|
-
const peerid = pkeys[0]!;
|
|
52
|
-
const cacheKey = pkeys[1]!;
|
|
53
|
-
try {
|
|
54
|
-
let nodeAddr: Multiaddr | undefined;
|
|
55
|
-
if (this.context?.connectedDc.nodeAddr) {
|
|
56
|
-
const connectedPeerId = this.context.connectedDc.nodeAddr.getPeerId() || "";
|
|
57
|
-
if (connectedPeerId && connectedPeerId === peerid) {
|
|
58
|
-
// 同一个节点
|
|
59
|
-
nodeAddr = this.context.connectedDc.nodeAddr;
|
|
60
|
-
} else {
|
|
61
|
-
// 不是同一个节点
|
|
62
|
-
nodeAddr = await this.dc?._getNodeAddr(peerid);
|
|
63
|
-
}
|
|
64
|
-
} else {
|
|
65
|
-
nodeAddr = await this.dc?._getNodeAddr(peerid);
|
|
66
|
-
}
|
|
67
|
-
if (!nodeAddr) {
|
|
68
|
-
console.log("nodeAddr is null");
|
|
69
|
-
return [null, Errors.ErrNodeAddrIsNull];
|
|
70
|
-
}
|
|
71
|
-
|
|
72
|
-
const cacheClient = new CacheClient(this.context!,this.dc,this.context.connectedDc.client);
|
|
73
|
-
const reply = await cacheClient.getCacheValue(nodeAddr, cacheKey);
|
|
74
|
-
console.log("GetCacheValueReply reply:", reply);
|
|
75
|
-
return [reply, null];
|
|
76
|
-
} catch (err) {
|
|
77
|
-
console.log("getCacheValue error:", err);
|
|
78
|
-
return [null, err as Error];
|
|
79
|
-
}
|
|
80
|
-
}
|
|
81
|
-
|
|
82
|
-
// 设置缓存值
|
|
83
|
-
setCacheKey = async (value: string, expire: number, peerAddr?: Multiaddr) : Promise<[string | null, Error | null]> => {
|
|
84
|
-
if (!this.context?.AccountBackupDc.client) {
|
|
85
|
-
return [null, Errors.ErrNoDcPeerConnected];
|
|
86
|
-
}
|
|
87
|
-
if (!this.chainUtil) {
|
|
88
|
-
console.log("chainUtil is null");
|
|
89
|
-
return [null, Errors.ErrChainUtilIsNull];
|
|
90
|
-
}
|
|
91
|
-
if(!this.context) {
|
|
92
|
-
console.log("context is null");
|
|
93
|
-
return [null, Errors.ErrAccountPrivateSignIsNull];
|
|
94
|
-
}
|
|
95
|
-
//获取最新区块高度
|
|
96
|
-
const blockHeight = await this.chainUtil.getBlockHeight();
|
|
97
|
-
const valueArray = new TextEncoder().encode(value);
|
|
98
|
-
const hashValue = await sha256(valueArray);
|
|
99
|
-
|
|
100
|
-
// 将 Blockheight 和 Expire 转换为小端字节数组
|
|
101
|
-
const hValue: Uint8Array = uint32ToLittleEndianBytes(
|
|
102
|
-
blockHeight ? blockHeight : 0
|
|
103
|
-
);
|
|
104
|
-
const expValue: Uint8Array = uint32ToLittleEndianBytes(expire);
|
|
105
|
-
|
|
106
|
-
// 将 expValue 和 hValue 连接起来
|
|
107
|
-
const preSignPart1 = new Uint8Array(expValue.length + hValue.length);
|
|
108
|
-
preSignPart1.set(expValue, 0);
|
|
109
|
-
preSignPart1.set(hValue, expValue.length);
|
|
110
|
-
|
|
111
|
-
// 将 hashValue 追加到 preSignPart1 之后
|
|
112
|
-
const preSign = new Uint8Array(preSignPart1.length + hashValue.length);
|
|
113
|
-
preSign.set(preSignPart1, 0);
|
|
114
|
-
preSign.set(hashValue, preSignPart1.length);
|
|
115
|
-
|
|
116
|
-
const signature = await this.context.sign(preSign);
|
|
117
|
-
const cacheClient = new CacheClient(this.context,this.dc,this.context?.AccountBackupDc.client);
|
|
118
|
-
const setCacheValueReply = await cacheClient.setCacheKey(
|
|
119
|
-
value,
|
|
120
|
-
blockHeight ? blockHeight : 0,
|
|
121
|
-
expire,
|
|
122
|
-
signature
|
|
123
|
-
);
|
|
124
|
-
console.log("SetCacheKey reply:", setCacheValueReply);
|
|
125
|
-
return [setCacheValueReply, null];
|
|
126
|
-
};
|
|
127
|
-
}
|