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,398 +0,0 @@
|
|
|
1
|
-
import { IThreadLogInfo } from '../core/core';
|
|
2
|
-
import { IRecord } from '../core/record';
|
|
3
|
-
import { IThreadEvent as NetEvent, EventHeader as NetEventHeader } from '../core/event';
|
|
4
|
-
import { Ed25519PrivKey as PrivKey,Ed25519PubKey as PubKey} from "../../../common/dc-key/ed25519";
|
|
5
|
-
import type { PublicKey } from '@libp2p/interface-keys'
|
|
6
|
-
import { SymmetricKey } from '../common/key';
|
|
7
|
-
import { CID, Link } from 'multiformats/cid';
|
|
8
|
-
import * as dagCBOR from '@ipld/dag-cbor';
|
|
9
|
-
import { Node, wrapObject } from './node';
|
|
10
|
-
import { Block } from './node';
|
|
11
|
-
import { Event, EventFromNode,EventObj,EventHeader } from './event';
|
|
12
|
-
import { encodeBlock, decodeBlock } from './coding';
|
|
13
|
-
import { DAGCBOR } from '@helia/dag-cbor';
|
|
14
|
-
import { Blocks } from 'helia';
|
|
15
|
-
import {net as net_pb} from "../pb/net_pb";
|
|
16
|
-
import { IPLDNode } from '../core/core';
|
|
17
|
-
import * as cbornode from './node';
|
|
18
|
-
import { KeyConverter, PeerIDConverter } from '../pb/proto-custom-types';
|
|
19
|
-
import { decode } from 'multiformats/hashes/digest';
|
|
20
|
-
import { peerIdFromMultihash } from '@libp2p/peer-id';
|
|
21
|
-
import {multiaddr} from "@multiformats/multiaddr";
|
|
22
|
-
import { Head } from '../core/head';
|
|
23
|
-
// 记录的节点结构
|
|
24
|
-
interface RecordObj {
|
|
25
|
-
block: CID;
|
|
26
|
-
sig: Uint8Array;
|
|
27
|
-
pubKey: Uint8Array;
|
|
28
|
-
prev?: CID;
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
// 创建记录的配置
|
|
32
|
-
export interface CreateRecordConfig {
|
|
33
|
-
block: IPLDNode;
|
|
34
|
-
prev?: CID;
|
|
35
|
-
key: PrivKey;
|
|
36
|
-
pubKey: PubKey;
|
|
37
|
-
serviceKey: SymmetricKey;
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
// 用于传输的记录协议结构
|
|
41
|
-
interface LogRecord {
|
|
42
|
-
recordNode: Uint8Array;
|
|
43
|
-
eventNode: Uint8Array;
|
|
44
|
-
headerNode: Uint8Array;
|
|
45
|
-
bodyNode: Uint8Array;
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
/**
|
|
49
|
-
* 从给定的块和日志私钥创建新记录
|
|
50
|
-
* @param ctx 上下文
|
|
51
|
-
* @param dag DAG服务
|
|
52
|
-
* @param config 记录创建配置
|
|
53
|
-
* @returns 创建的记录
|
|
54
|
-
*/
|
|
55
|
-
export async function CreateRecord(
|
|
56
|
-
bstore: Blocks,
|
|
57
|
-
config: CreateRecordConfig
|
|
58
|
-
): Promise<IRecord> {
|
|
59
|
-
// 序列化公钥
|
|
60
|
-
const pkb = await KeyConverter.publicToBytes(config.pubKey);
|
|
61
|
-
|
|
62
|
-
// 创建签名有效载荷
|
|
63
|
-
let payload: Uint8Array;
|
|
64
|
-
if (config.prev && CID.asCID(config.prev) !== null) {
|
|
65
|
-
// 连接块CID和前一个CID的字节
|
|
66
|
-
const blockBytes = config.block.cid().bytes;
|
|
67
|
-
const prevBytes = config.prev.bytes;
|
|
68
|
-
payload = new Uint8Array(blockBytes.length + prevBytes.length);
|
|
69
|
-
payload.set(blockBytes);
|
|
70
|
-
payload.set(prevBytes, blockBytes.length);
|
|
71
|
-
} else {
|
|
72
|
-
payload = pkb;
|
|
73
|
-
|
|
74
|
-
}
|
|
75
|
-
|
|
76
|
-
// 使用私钥签名
|
|
77
|
-
const sig = await config.key.sign(payload);
|
|
78
|
-
|
|
79
|
-
// 创建记录对象
|
|
80
|
-
const obj: RecordObj = {
|
|
81
|
-
block: config.block.cid(),
|
|
82
|
-
sig: sig,
|
|
83
|
-
pubKey: pkb,
|
|
84
|
-
};
|
|
85
|
-
if (config.prev) {
|
|
86
|
-
obj.prev = config.prev;
|
|
87
|
-
}
|
|
88
|
-
|
|
89
|
-
const node = await wrapObject(obj);
|
|
90
|
-
|
|
91
|
-
// 将对象打包为CBOR节点
|
|
92
|
-
// const bytes = dagCBOR.encode(obj);
|
|
93
|
-
// const hash = await sha256.digest(bytes);
|
|
94
|
-
// const cid = CID.createV1(dagCBOR.code, hash);
|
|
95
|
-
// const node = new Block(bytes,cid );
|
|
96
|
-
|
|
97
|
-
// 使用服务密钥加密节点
|
|
98
|
-
const coded = await encodeBlock(node, config.serviceKey);
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
if (bstore) {
|
|
102
|
-
await bstore.put(coded.cid(), coded.data());
|
|
103
|
-
}
|
|
104
|
-
|
|
105
|
-
// 返回Record实例
|
|
106
|
-
return new Record(
|
|
107
|
-
coded,
|
|
108
|
-
obj,
|
|
109
|
-
config.block
|
|
110
|
-
);
|
|
111
|
-
}
|
|
112
|
-
|
|
113
|
-
/**
|
|
114
|
-
* 从给定CID获取记录
|
|
115
|
-
* @param ctx 上下文
|
|
116
|
-
* @param dag DAG服务
|
|
117
|
-
* @param id 记录CID
|
|
118
|
-
* @param key 解密密钥
|
|
119
|
-
* @returns 获取的记录
|
|
120
|
-
*/
|
|
121
|
-
export async function GetRecord(
|
|
122
|
-
dag: DAGCBOR,
|
|
123
|
-
id: CID,
|
|
124
|
-
key: SymmetricKey
|
|
125
|
-
): Promise<IRecord> {
|
|
126
|
-
const blockData = await dag.get<Uint8Array>(id);
|
|
127
|
-
const decrypt = await key.decrypt(blockData);
|
|
128
|
-
const decoded = dagCBOR.decode<RecordObj>(decrypt);
|
|
129
|
-
decoded.block = id; //使用加密后且cborencode后数据的cid
|
|
130
|
-
// const dblock = await dag.get<Uint8Array>(decoded.block);
|
|
131
|
-
const wrapedRnode = await wrapObject(blockData);
|
|
132
|
-
const record = new Record(wrapedRnode, decoded);
|
|
133
|
-
return record
|
|
134
|
-
}
|
|
135
|
-
|
|
136
|
-
/**
|
|
137
|
-
* 使用给定密钥从节点解码记录
|
|
138
|
-
* @param coded 编码的节点
|
|
139
|
-
* @param key 解密密钥
|
|
140
|
-
* @returns 解码的记录
|
|
141
|
-
*/
|
|
142
|
-
export async function RecordFromNode(
|
|
143
|
-
coded: Node,
|
|
144
|
-
key: SymmetricKey
|
|
145
|
-
): Promise<IRecord> {
|
|
146
|
-
const block = new Block(coded.rawData(), coded.cid());
|
|
147
|
-
// 解码节点
|
|
148
|
-
const node = await decodeBlock(block, key);
|
|
149
|
-
// 解析记录对象
|
|
150
|
-
const obj = dagCBOR.decode<RecordObj>(node.rawData());
|
|
151
|
-
obj.block = coded.cid(); //使用加密后且cborencode后数据的cid
|
|
152
|
-
// const wrapedRnode = await wrapObject(block);
|
|
153
|
-
// 返回Record实例
|
|
154
|
-
return new Record(
|
|
155
|
-
coded,
|
|
156
|
-
obj
|
|
157
|
-
);
|
|
158
|
-
}
|
|
159
|
-
|
|
160
|
-
/**
|
|
161
|
-
* 从DAG服务中删除记录
|
|
162
|
-
* @param ctx 上下文
|
|
163
|
-
* @param blockstore 块存储
|
|
164
|
-
* @param rec 要删除的记录
|
|
165
|
-
*/
|
|
166
|
-
export async function RemoveRecord(
|
|
167
|
-
blockstore: Blocks,
|
|
168
|
-
rec: IRecord
|
|
169
|
-
): Promise<void> {
|
|
170
|
-
await blockstore.delete(rec.cid());
|
|
171
|
-
}
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
/**
|
|
176
|
-
* 将日志信息转换为protobuf格式
|
|
177
|
-
* @param info 日志信息
|
|
178
|
-
* @returns protobuf格式的日志
|
|
179
|
-
*/
|
|
180
|
-
export async function logToProto(lg: IThreadLogInfo): Promise<net_pb.pb.ILog> {
|
|
181
|
-
if (!lg.pubKey){
|
|
182
|
-
throw new Error('Missing required fields in LogInfo: pubKey');
|
|
183
|
-
}
|
|
184
|
-
const publicKeyBytes = await KeyConverter.publicToBytes(lg.pubKey);
|
|
185
|
-
const lgid = lg.id.toString();
|
|
186
|
-
const idBytes = PeerIDConverter.toBytes(lgid);
|
|
187
|
-
const addrs = lg.addrs? lg.addrs.map(addr => addr.bytes) : [];
|
|
188
|
-
// 创建protobuf日志对象
|
|
189
|
-
const log: net_pb.pb.ILog = {
|
|
190
|
-
ID: idBytes,
|
|
191
|
-
pubKey:publicKeyBytes,
|
|
192
|
-
addrs: addrs,
|
|
193
|
-
head: lg.head?.id ? lg.head.id.bytes : undefined,
|
|
194
|
-
counter: lg.head?.counter
|
|
195
|
-
} as net_pb.pb.ILog;
|
|
196
|
-
return log;
|
|
197
|
-
}
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
/**
|
|
201
|
-
* 将protobuf格式的日志转换为应用格式
|
|
202
|
-
* @param protoLog protobuf格式的日志
|
|
203
|
-
* @returns 应用格式的日志信息
|
|
204
|
-
*/
|
|
205
|
-
export async function logFromProto(protoLog: net_pb.pb.ILog): Promise<IThreadLogInfo> {
|
|
206
|
-
if (!protoLog.ID || !protoLog.pubKey) {
|
|
207
|
-
throw new Error('Missing required fields in Log: ID or pubKey');
|
|
208
|
-
}
|
|
209
|
-
// const multihash = decode(protoLog.ID);
|
|
210
|
-
// const id = peerIdFromMultihash(multihash);
|
|
211
|
-
// 解析日志ID
|
|
212
|
-
const id = PeerIDConverter.fromBytes(protoLog.ID);
|
|
213
|
-
|
|
214
|
-
// 解析公钥
|
|
215
|
-
const pubKey = await KeyConverter.publicFromBytes(protoLog.pubKey);
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
// 解析地址
|
|
219
|
-
const addrs = (protoLog.addrs || []).map(addr => multiaddr(addr));
|
|
220
|
-
|
|
221
|
-
// 解析头部信息
|
|
222
|
-
const head = protoLog.head && protoLog.head.length > 0
|
|
223
|
-
? CID.decode(protoLog.head)
|
|
224
|
-
: undefined;
|
|
225
|
-
|
|
226
|
-
// 解析计数器
|
|
227
|
-
const counter = protoLog.counter
|
|
228
|
-
? Number(protoLog.counter)
|
|
229
|
-
: 0;
|
|
230
|
-
|
|
231
|
-
return {
|
|
232
|
-
id,
|
|
233
|
-
pubKey,
|
|
234
|
-
addrs,
|
|
235
|
-
managed: true,
|
|
236
|
-
head: {
|
|
237
|
-
id: head,
|
|
238
|
-
counter
|
|
239
|
-
} as Head
|
|
240
|
-
};
|
|
241
|
-
}
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
/**
|
|
245
|
-
* 将记录转换为用于传输的proto版本
|
|
246
|
-
* 节点以加密形式发送
|
|
247
|
-
* @param ctx 上下文
|
|
248
|
-
* @param dag DAG服务
|
|
249
|
-
* @param rec 记录
|
|
250
|
-
* @returns proto记录
|
|
251
|
-
*/
|
|
252
|
-
export async function RecordToProto(
|
|
253
|
-
bstore: Blocks,
|
|
254
|
-
rec: IRecord
|
|
255
|
-
): Promise<net_pb.pb.Log.Record> {
|
|
256
|
-
const block = await rec.getBlock( bstore);
|
|
257
|
-
let event: NetEvent;
|
|
258
|
-
if (block instanceof Event) {
|
|
259
|
-
event = block;
|
|
260
|
-
} else {
|
|
261
|
-
event = await EventFromNode(block as Node);
|
|
262
|
-
}
|
|
263
|
-
const header = await event.getHeader( bstore);
|
|
264
|
-
const body = await event.getBody(bstore);
|
|
265
|
-
const record = new net_pb.pb.Log.Record();
|
|
266
|
-
record.recordNode = rec.data();
|
|
267
|
-
record.eventNode = event.data();
|
|
268
|
-
record.headerNode = header.data();
|
|
269
|
-
record.bodyNode = body.data();
|
|
270
|
-
return record;
|
|
271
|
-
}
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
export async function RecordFromProto(
|
|
277
|
-
rec: net_pb.pb.Log.Record,
|
|
278
|
-
key: SymmetricKey
|
|
279
|
-
): Promise<IRecord> {
|
|
280
|
-
if (!key) {
|
|
281
|
-
throw new Error("解密密钥是必需的");
|
|
282
|
-
}
|
|
283
|
-
const encryptRec = dagCBOR.decode<Uint8Array>(rec.recordNode);
|
|
284
|
-
const recData = await key.decrypt(encryptRec);
|
|
285
|
-
const recObj = dagCBOR.decode<RecordObj>(recData);
|
|
286
|
-
const rnode = await cbornode.decode(rec.recordNode)
|
|
287
|
-
recObj.block = rnode.cid(); //使用加密后且cborencode后数据的cid
|
|
288
|
-
|
|
289
|
-
// 解析记录对象
|
|
290
|
-
const eventNode = await cbornode.decode(rec.eventNode);
|
|
291
|
-
const headerNode = await cbornode.decode(rec.headerNode);
|
|
292
|
-
const bodyNode =await cbornode.decode(rec.bodyNode);
|
|
293
|
-
// 解析事件对象
|
|
294
|
-
const eobj = dagCBOR.decode<EventObj>(eventNode.rawData());
|
|
295
|
-
|
|
296
|
-
// 创建事件头和事件
|
|
297
|
-
const eventHeader = new EventHeader(headerNode);
|
|
298
|
-
const event = new Event(
|
|
299
|
-
eventNode,
|
|
300
|
-
eobj,
|
|
301
|
-
eventHeader,
|
|
302
|
-
bodyNode
|
|
303
|
-
);
|
|
304
|
-
|
|
305
|
-
// 返回记录
|
|
306
|
-
return new Record(
|
|
307
|
-
rnode,
|
|
308
|
-
recObj,
|
|
309
|
-
event,
|
|
310
|
-
|
|
311
|
-
);
|
|
312
|
-
}
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
/**
|
|
316
|
-
* 表示记录的IPLD节点
|
|
317
|
-
*/
|
|
318
|
-
export class Record implements IRecord {
|
|
319
|
-
private _node: Node;
|
|
320
|
-
private _obj: RecordObj;
|
|
321
|
-
private _block?: IPLDNode;
|
|
322
|
-
|
|
323
|
-
constructor(node: Node, obj: RecordObj, block?: IPLDNode) {
|
|
324
|
-
this._node = node;
|
|
325
|
-
this._obj = obj;
|
|
326
|
-
if (block) {
|
|
327
|
-
this._block = block;
|
|
328
|
-
}
|
|
329
|
-
}
|
|
330
|
-
|
|
331
|
-
cid(): CID {
|
|
332
|
-
return this._node.cid();
|
|
333
|
-
}
|
|
334
|
-
|
|
335
|
-
rawData(): Uint8Array {
|
|
336
|
-
return this._node.rawData();
|
|
337
|
-
}
|
|
338
|
-
|
|
339
|
-
blockID(): CID {
|
|
340
|
-
return this._obj.block;
|
|
341
|
-
}
|
|
342
|
-
async getBlock( bstore:Blocks): Promise<IPLDNode> {
|
|
343
|
-
if (this._block) {
|
|
344
|
-
return this._block;
|
|
345
|
-
}
|
|
346
|
-
|
|
347
|
-
const data = await bstore.get(this._obj.block);
|
|
348
|
-
this._block = await cbornode.wrapObject(data);
|
|
349
|
-
return this._block;
|
|
350
|
-
}
|
|
351
|
-
|
|
352
|
-
prevID(): CID | undefined {
|
|
353
|
-
return this._obj.prev;
|
|
354
|
-
}
|
|
355
|
-
|
|
356
|
-
sig(): Uint8Array {
|
|
357
|
-
return this._obj.sig;
|
|
358
|
-
}
|
|
359
|
-
|
|
360
|
-
pubKey(): Uint8Array {
|
|
361
|
-
return this._obj.pubKey;
|
|
362
|
-
}
|
|
363
|
-
links(): Link[] {
|
|
364
|
-
return this._node.links();
|
|
365
|
-
}
|
|
366
|
-
size(): number {
|
|
367
|
-
return this._node.size();
|
|
368
|
-
}
|
|
369
|
-
|
|
370
|
-
data(): Uint8Array {
|
|
371
|
-
return this._node.data();
|
|
372
|
-
}
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
async verify(key: PublicKey): Promise<Error|undefined> {
|
|
376
|
-
if (!this._block) {
|
|
377
|
-
return new Error("块未加载");
|
|
378
|
-
}
|
|
379
|
-
|
|
380
|
-
let payload: Uint8Array;
|
|
381
|
-
if (this.prevID() && CID.asCID(this.prevID()) !== null) {
|
|
382
|
-
// 连接块CID和前一个CID的字节
|
|
383
|
-
const blockBytes = this._block.cid().bytes;
|
|
384
|
-
const prevBytes = this.prevID()!.bytes;
|
|
385
|
-
payload = new Uint8Array(blockBytes.length + prevBytes.length);
|
|
386
|
-
payload.set(blockBytes);
|
|
387
|
-
payload.set(prevBytes, blockBytes.length);
|
|
388
|
-
} else {
|
|
389
|
-
payload = this.pubKey();
|
|
390
|
-
}
|
|
391
|
-
|
|
392
|
-
const ok = await key.verify(payload, this.sig());
|
|
393
|
-
if (!ok) {
|
|
394
|
-
return new Error("签名无效");
|
|
395
|
-
}
|
|
396
|
-
return
|
|
397
|
-
}
|
|
398
|
-
}
|
|
@@ -1,24 +0,0 @@
|
|
|
1
|
-
export class AsyncMutex {
|
|
2
|
-
private _locked: boolean = false;
|
|
3
|
-
private _queue: (() => void)[] = [];
|
|
4
|
-
|
|
5
|
-
async acquire(): Promise<void> {
|
|
6
|
-
return new Promise<void>((resolve) => {
|
|
7
|
-
if (!this._locked) {
|
|
8
|
-
this._locked = true;
|
|
9
|
-
resolve();
|
|
10
|
-
} else {
|
|
11
|
-
this._queue.push(resolve);
|
|
12
|
-
}
|
|
13
|
-
});
|
|
14
|
-
}
|
|
15
|
-
|
|
16
|
-
release(): void {
|
|
17
|
-
if (this._queue.length > 0) {
|
|
18
|
-
const next = this._queue.shift()!;
|
|
19
|
-
next();
|
|
20
|
-
} else {
|
|
21
|
-
this._locked = false;
|
|
22
|
-
}
|
|
23
|
-
}
|
|
24
|
-
}
|
|
@@ -1,135 +0,0 @@
|
|
|
1
|
-
// addr-info.ts
|
|
2
|
-
import { peerIdFromString } from '@libp2p/peer-id'
|
|
3
|
-
import { PeerId } from '@libp2p/interface';
|
|
4
|
-
import { PeerRecord } from '@libp2p/peer-record'
|
|
5
|
-
import { multiaddr, type Multiaddr } from '@multiformats/multiaddr'
|
|
6
|
-
import { CodeError } from '@libp2p/interfaces/errors'
|
|
7
|
-
import { equals as uint8ArrayEquals } from 'uint8arrays'
|
|
8
|
-
|
|
9
|
-
export interface AddrInfo {
|
|
10
|
-
peerId: PeerId
|
|
11
|
-
multiaddrs: Multiaddr[]
|
|
12
|
-
}
|
|
13
|
-
|
|
14
|
-
export class AddrInfoManager implements AddrInfo {
|
|
15
|
-
public readonly peerId: PeerId
|
|
16
|
-
public multiaddrs: Multiaddr[]
|
|
17
|
-
|
|
18
|
-
constructor({ peerId, multiaddrs }: AddrInfo) {
|
|
19
|
-
this.peerId = peerId
|
|
20
|
-
this.multiaddrs = multiaddrs
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
/**
|
|
24
|
-
* 验证地址有效性 (等价 Go 的 Validate 方法)
|
|
25
|
-
*/
|
|
26
|
-
validate(): void {
|
|
27
|
-
if (this.peerId.type !== 'Ed25519') {
|
|
28
|
-
throw new CodeError('Only Ed25519 peer IDs are supported', 'ERR_INVALID_PEER_ID')
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
const seen = new Set<string>()
|
|
32
|
-
for (const ma of this.multiaddrs) {
|
|
33
|
-
const str = ma.toString()
|
|
34
|
-
if (seen.has(str)) {
|
|
35
|
-
//删除重复的地址
|
|
36
|
-
this.multiaddrs.splice(this.multiaddrs.indexOf(ma), 1)
|
|
37
|
-
continue
|
|
38
|
-
}
|
|
39
|
-
seen.add(str)
|
|
40
|
-
|
|
41
|
-
if (ma.getPeerId() != this.peerId.toString()) {
|
|
42
|
-
throw new CodeError(`Multiaddr ${str} does not contain peer ID ${this.peerId}`, 'ERR_INVALID_MULTIADDR')
|
|
43
|
-
}
|
|
44
|
-
}
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
/**
|
|
48
|
-
* 序列化方法 (等价 Go 的 MarshalBinary)
|
|
49
|
-
*/
|
|
50
|
-
marshal(): Uint8Array {
|
|
51
|
-
this.validate()
|
|
52
|
-
const record = new PeerRecord({
|
|
53
|
-
peerId: this.peerId,
|
|
54
|
-
multiaddrs: this.multiaddrs
|
|
55
|
-
})
|
|
56
|
-
return record.marshal()
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
/**
|
|
60
|
-
* 反序列化方法 (等价 Go 的 UnmarshalBinary)
|
|
61
|
-
*/
|
|
62
|
-
static unmarshal(data: Uint8Array): AddrInfoManager {
|
|
63
|
-
const record = PeerRecord.createFromProtobuf(data)
|
|
64
|
-
return new AddrInfoManager({
|
|
65
|
-
peerId: record.peerId,
|
|
66
|
-
multiaddrs: record.multiaddrs
|
|
67
|
-
})
|
|
68
|
-
}
|
|
69
|
-
|
|
70
|
-
/**
|
|
71
|
-
* 合并地址 (等价 Go 的地址合并逻辑)
|
|
72
|
-
*/
|
|
73
|
-
merge(other: AddrInfo): AddrInfoManager {
|
|
74
|
-
if (this.peerId.toString() != other.peerId.toString() ) {
|
|
75
|
-
throw new CodeError('Cannot merge different peer IDs', 'ERR_MISMATCHED_PEER_IDS')
|
|
76
|
-
}
|
|
77
|
-
|
|
78
|
-
const mergedAddrs = new Map<string, Multiaddr>()
|
|
79
|
-
for (const addr of [...this.multiaddrs, ...other.multiaddrs]) {
|
|
80
|
-
mergedAddrs.set(addr.toString(), addr)
|
|
81
|
-
}
|
|
82
|
-
|
|
83
|
-
return new AddrInfoManager({
|
|
84
|
-
peerId: this.peerId,
|
|
85
|
-
multiaddrs: Array.from(mergedAddrs.values())
|
|
86
|
-
})
|
|
87
|
-
}
|
|
88
|
-
|
|
89
|
-
/**
|
|
90
|
-
* 地址过滤 (类似 Go 的地址筛选逻辑)
|
|
91
|
-
*/
|
|
92
|
-
filter(predicate: (ma: Multiaddr) => boolean): AddrInfoManager {
|
|
93
|
-
return new AddrInfoManager({
|
|
94
|
-
peerId: this.peerId,
|
|
95
|
-
multiaddrs: this.multiaddrs.filter(predicate)
|
|
96
|
-
})
|
|
97
|
-
}
|
|
98
|
-
}
|
|
99
|
-
|
|
100
|
-
// 使用示例
|
|
101
|
-
async function exampleUsage() {
|
|
102
|
-
// 创建实例 (等价 Go 的 NewAddrInfo)
|
|
103
|
-
const peerId = await peerIdFromString('QmNnooDu5...')
|
|
104
|
-
const multiaddrs = [
|
|
105
|
-
multiaddr('/ip4/192.168.1.1/tcp/4001/p2p/' + peerId.toString())
|
|
106
|
-
]
|
|
107
|
-
|
|
108
|
-
const addrInfo = new AddrInfoManager({ peerId, multiaddrs })
|
|
109
|
-
|
|
110
|
-
try {
|
|
111
|
-
// 验证地址
|
|
112
|
-
addrInfo.validate()
|
|
113
|
-
|
|
114
|
-
// 序列化
|
|
115
|
-
const encoded = addrInfo.marshal()
|
|
116
|
-
|
|
117
|
-
// 反序列化
|
|
118
|
-
const decoded = AddrInfoManager.unmarshal(encoded)
|
|
119
|
-
console.log(decoded.multiaddrs)
|
|
120
|
-
|
|
121
|
-
// 合并测试
|
|
122
|
-
const otherAddr = new AddrInfoManager({
|
|
123
|
-
peerId,
|
|
124
|
-
multiaddrs: [multiaddr('/ip4/10.0.0.1/tcp/4001/p2p/' + peerId.toString())]
|
|
125
|
-
})
|
|
126
|
-
const merged = addrInfo.merge(otherAddr)
|
|
127
|
-
console.log(merged.multiaddrs)
|
|
128
|
-
|
|
129
|
-
// 过滤测试
|
|
130
|
-
const filtered = merged.filter(ma => ma.toString().startsWith('/ip4/192'))
|
|
131
|
-
console.log(filtered.multiaddrs)
|
|
132
|
-
} catch (err) {
|
|
133
|
-
console.error('Address validation failed:', err)
|
|
134
|
-
}
|
|
135
|
-
}
|
|
@@ -1,81 +0,0 @@
|
|
|
1
|
-
import * as buffer from "buffer/";
|
|
2
|
-
const { Buffer } = buffer;
|
|
3
|
-
import { Key, Query,Pair } from 'interface-datastore';
|
|
4
|
-
import {DBPrefix } from '../core/db';
|
|
5
|
-
import { Event ,TxnDatastoreExtended, Transaction} from '../core/db'; // Assuming Event is defined in a core module
|
|
6
|
-
import { jsonStringify } from "../../../util/utils";
|
|
7
|
-
|
|
8
|
-
export const dsDispatcherPrefix = DBPrefix.dsPrefix.child(new Key('dispatcher'));
|
|
9
|
-
|
|
10
|
-
// Reducer applies an event to an existing state.
|
|
11
|
-
interface Reducer {
|
|
12
|
-
reduce(events: Event[]): Promise<void>;
|
|
13
|
-
}
|
|
14
|
-
|
|
15
|
-
// Dispatcher is used to dispatch events to registered reducers.
|
|
16
|
-
export class Dispatcher {
|
|
17
|
-
private store: TxnDatastoreExtended;
|
|
18
|
-
private reducers: Reducer[];
|
|
19
|
-
private lock: { lock: () => void; unlock: () => void };
|
|
20
|
-
private lastID: number;
|
|
21
|
-
|
|
22
|
-
constructor(store: TxnDatastoreExtended) {
|
|
23
|
-
this.store = store;
|
|
24
|
-
this.reducers = [];
|
|
25
|
-
this.lock = { lock: () => {}, unlock: () => {} };
|
|
26
|
-
this.lastID = 0;
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
// Store returns the internal event store.
|
|
30
|
-
getStore(): TxnDatastoreExtended {
|
|
31
|
-
return this.store;
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
// Register takes a reducer to be invoked with each dispatched event.
|
|
35
|
-
register(reducer: Reducer): void {
|
|
36
|
-
this.lock.lock();
|
|
37
|
-
this.lastID++;
|
|
38
|
-
this.reducers.push(reducer);
|
|
39
|
-
this.lock.unlock();
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
// Dispatch dispatches a payload to all registered reducers.
|
|
43
|
-
async dispatch(events: Event[]): Promise<void> {
|
|
44
|
-
this.lock.lock();
|
|
45
|
-
|
|
46
|
-
const txn = await this.store.newTransactionExtended(false);
|
|
47
|
-
try {
|
|
48
|
-
for (const event of events) {
|
|
49
|
-
const key = this.getKey(event);
|
|
50
|
-
const buffer = Buffer.from(jsonStringify(event));
|
|
51
|
-
await txn.put(key, buffer);
|
|
52
|
-
}
|
|
53
|
-
await txn.commit();
|
|
54
|
-
} catch (err) {
|
|
55
|
-
txn.discard();
|
|
56
|
-
throw err;
|
|
57
|
-
} finally {
|
|
58
|
-
this.lock.unlock();
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
// Safe to fire off reducers now that event is persisted
|
|
62
|
-
this.reducers.map(async (reducer) => {
|
|
63
|
-
await reducer.reduce(events);
|
|
64
|
-
});
|
|
65
|
-
}
|
|
66
|
-
|
|
67
|
-
// Query searches the internal event store and returns a query result.
|
|
68
|
-
async *query(query: Query): AsyncIterable<Pair>{
|
|
69
|
-
const result = this.store.query(query);
|
|
70
|
-
return result;
|
|
71
|
-
}
|
|
72
|
-
|
|
73
|
-
// Key format: <timestamp>/<instance-id>/<type>
|
|
74
|
-
private getKey(event: Event): Key {
|
|
75
|
-
const timestamp = event.timestamp
|
|
76
|
-
const collection = event.collection;
|
|
77
|
-
const instanceID = event.instanceID;
|
|
78
|
-
return dsDispatcherPrefix.child(new Key(timestamp.toString())).child(new Key(collection.toString())).child(new Key(instanceID.toString()));
|
|
79
|
-
}
|
|
80
|
-
}
|
|
81
|
-
|