web-dc-api 0.0.87 → 0.0.89
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/cjs/index.js +1 -0
- package/dist/cjs/protobuf-BVBdi7Hh.js +1 -0
- package/dist/dc.min.js +1 -19
- package/dist/esm/chunks/protobuf-CbxDm-Gy.js +1 -0
- package/dist/esm/index.js +1 -0
- package/dist/index.d.ts +947 -886
- package/package.json +14 -17
- package/dist/index.cjs.js +0 -19
- package/dist/index.esm.js +0 -19
- 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 -595
- package/lib/common/define.ts +0 -66
- 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 -694
- 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 -202
- 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 -443
- package/lib/implements/threaddb/dbmanager.ts +0 -1901
- package/lib/implements/threaddb/lsstoreds/addr_book.ts +0 -452
- 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 -280
- 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 -138
- package/lib/implements/threaddb/net/grpcClient.ts +0 -582
- package/lib/implements/threaddb/net/grpcserver.ts +0 -146
- package/lib/implements/threaddb/net/net.ts +0 -2006
- 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 -664
- 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/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/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
package/lib/common/constants.ts
DELETED
|
@@ -1,55 +0,0 @@
|
|
|
1
|
-
export enum Type {
|
|
2
|
-
Filetype = 1, //文件
|
|
3
|
-
Threaddbtype, //数据库
|
|
4
|
-
}
|
|
5
|
-
|
|
6
|
-
// cid是否需要连接节点
|
|
7
|
-
export const cidNeedConnect = {
|
|
8
|
-
NEED: 0,
|
|
9
|
-
NOT_NEED: 1,
|
|
10
|
-
};
|
|
11
|
-
|
|
12
|
-
// openFlag 开放标志
|
|
13
|
-
export enum OpenFlag {
|
|
14
|
-
PUBLIC = 0, //公开 任何人可以读写,不建议使用,评论所产生的空间使用都由创建者承担
|
|
15
|
-
PRIVATE = 1,//私密 只有拥有者可以读写
|
|
16
|
-
AUTH = 2, //需要对评论进行鉴权,主要用于私聊群组,或者内部共享群组,对访问主题进行权限设置,有权限的用户才能访问
|
|
17
|
-
AUTH_WRITE = 3, //需要对评论进行写鉴权,任何人都可以读
|
|
18
|
-
REPORTED = 4, //被举报
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
export enum AIProxyUserPermission {
|
|
23
|
-
QUERY = 8, //查询
|
|
24
|
-
ADMIN = 4, //管理员
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
/**
|
|
29
|
-
* 主题的操作权限
|
|
30
|
-
*/
|
|
31
|
-
export enum ThemePermission {
|
|
32
|
-
/** 无权限 */
|
|
33
|
-
NONE = 0,
|
|
34
|
-
/** 申请权限 */
|
|
35
|
-
APPLY,
|
|
36
|
-
/** 读权限 */
|
|
37
|
-
READ,
|
|
38
|
-
/** 写权限 */
|
|
39
|
-
WRITE,
|
|
40
|
-
/** 管理员权限 */
|
|
41
|
-
ADMIN,
|
|
42
|
-
/**
|
|
43
|
-
* 只写权限
|
|
44
|
-
* 不允许用户修改remark或者物联网设备上报数据使用
|
|
45
|
-
* 权限后面跟随分组列表,以逗号分隔
|
|
46
|
-
*/
|
|
47
|
-
ONLY_WRITE,
|
|
48
|
-
/** 物联网管理人员相关权限,具体权限后续可扩展 */
|
|
49
|
-
DEVICE,
|
|
50
|
-
/** 不存在 */
|
|
51
|
-
NOT_EXIST,
|
|
52
|
-
/** 查询权限,系统可以查询数据,用户只能查询自己的授权数据 */
|
|
53
|
-
QUERY
|
|
54
|
-
}
|
|
55
|
-
|
|
@@ -1,343 +0,0 @@
|
|
|
1
|
-
import { ed25519 } from '@noble/curves/ed25519'
|
|
2
|
-
import * as protobuf from 'protobufjs'
|
|
3
|
-
import { identity } from 'multiformats/hashes/identity'
|
|
4
|
-
import { CID } from 'multiformats/cid'
|
|
5
|
-
import { base58btc } from 'multiformats/bases/base58'
|
|
6
|
-
import { base32 } from 'multiformats/bases/base32'
|
|
7
|
-
import type { MultihashDigest } from 'multiformats/hashes/interface'
|
|
8
|
-
import type { Ed25519PrivateKey, Ed25519PublicKey } from '@libp2p/interface'
|
|
9
|
-
import { bytesToHex,hexToBytes } from '@noble/hashes/utils'
|
|
10
|
-
import { Encryption } from "../../util/curve25519Encryption";
|
|
11
|
-
|
|
12
|
-
enum keyType {
|
|
13
|
-
RSA = 0,
|
|
14
|
-
Ed25519 = 1,
|
|
15
|
-
Secp256k1 = 2,
|
|
16
|
-
ECDSA = 3
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
// 类型定义
|
|
20
|
-
interface KeyMessage {
|
|
21
|
-
type: number;
|
|
22
|
-
data: Uint8Array;
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
//比较两个 Uint8Array
|
|
28
|
-
const arrayEquals = (a:Uint8Array, b:Uint8Array) => {
|
|
29
|
-
if (a.length !== b.length) return false;
|
|
30
|
-
return a.every((val, i) => val === b[i]);
|
|
31
|
-
};
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
const PublicKeyProto = new protobuf.Type("PublicKey")
|
|
36
|
-
.add(new protobuf.Field("type", 1, "uint32"))
|
|
37
|
-
.add(new protobuf.Field("data", 2, "bytes"));
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
// ED25519公钥实现 // 更完整的实现
|
|
42
|
-
export class Ed25519PubKey implements Ed25519PublicKey {
|
|
43
|
-
raw: Uint8Array;
|
|
44
|
-
type :'Ed25519';
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
constructor(publicKey: Uint8Array) {
|
|
48
|
-
if (!(publicKey instanceof Uint8Array)) {
|
|
49
|
-
throw new Error('Public key must be Uint8Array')
|
|
50
|
-
}
|
|
51
|
-
if (publicKey.length !== 32) {
|
|
52
|
-
throw new Error('ed25519: bad public key length')
|
|
53
|
-
}
|
|
54
|
-
this.raw = new Uint8Array(publicKey)
|
|
55
|
-
this.type = 'Ed25519'
|
|
56
|
-
}
|
|
57
|
-
private static readonly proto = (() => {
|
|
58
|
-
return PublicKeyProto;
|
|
59
|
-
})();
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
bytes() {
|
|
64
|
-
return this.raw
|
|
65
|
-
}
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
/**
|
|
69
|
-
* Returns a multihash, the digest of which is the protobuf-encoded public key
|
|
70
|
-
* encoded as an identity hash
|
|
71
|
-
*/
|
|
72
|
-
toMultihash(): MultihashDigest<0> {
|
|
73
|
-
// 使用 protobuf 编码公钥
|
|
74
|
-
const protoBytes = Ed25519PubKey.publicKeyToProto(this);
|
|
75
|
-
const size = protoBytes.length;
|
|
76
|
-
|
|
77
|
-
// 创建 bytes: 格式为 [code(varint), size(varint), ...digest]
|
|
78
|
-
const bytes = new Uint8Array(2 + size);
|
|
79
|
-
bytes[0] = 0x00; // identity hash code
|
|
80
|
-
bytes[1] = size; // size in varint
|
|
81
|
-
bytes.set(protoBytes, 2);
|
|
82
|
-
|
|
83
|
-
return {
|
|
84
|
-
code: 0x00, // identity hash code
|
|
85
|
-
size,
|
|
86
|
-
digest: protoBytes,
|
|
87
|
-
bytes
|
|
88
|
-
};
|
|
89
|
-
}
|
|
90
|
-
|
|
91
|
-
toCID() {
|
|
92
|
-
const hash = this.toMultihash()
|
|
93
|
-
return CID.create(1, 0x72, hash)
|
|
94
|
-
}
|
|
95
|
-
|
|
96
|
-
equals(other: Ed25519PublicKey): boolean {
|
|
97
|
-
return arrayEquals(this.raw,other.raw)
|
|
98
|
-
}
|
|
99
|
-
|
|
100
|
-
verify(data: Uint8Array, sig: Uint8Array) {
|
|
101
|
-
return ed25519.verify(sig, data, this.raw)
|
|
102
|
-
}
|
|
103
|
-
async encrypt(data: Uint8Array): Promise<Uint8Array> {
|
|
104
|
-
const encrypted = await Encryption.encrypt(this.raw, data);
|
|
105
|
-
return encrypted
|
|
106
|
-
}
|
|
107
|
-
|
|
108
|
-
static formEd25519PublicKey(publicKey: Ed25519PublicKey): Ed25519PubKey {
|
|
109
|
-
return new Ed25519PubKey(publicKey.raw)
|
|
110
|
-
}
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
static fromMultihash(multihash: MultihashDigest<0x0>) {
|
|
114
|
-
if (multihash.code !== identity.code) {
|
|
115
|
-
throw new Error('Invalid multihash code')
|
|
116
|
-
}
|
|
117
|
-
return new Ed25519PubKey(multihash.digest)
|
|
118
|
-
}
|
|
119
|
-
// 从 CID 恢复公钥
|
|
120
|
-
static fromCID(cid: CID<unknown, 0x72, 0x0, 1>) {
|
|
121
|
-
if (cid.code !== 0x72) { // libp2p-key codec
|
|
122
|
-
throw new Error('Invalid CID codec')
|
|
123
|
-
}
|
|
124
|
-
return Ed25519PubKey.fromMultihash(cid.multihash)
|
|
125
|
-
}
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
// PublicKeyToProto converts a public key object into an unserialized
|
|
129
|
-
// protobuf PublicKey message.
|
|
130
|
-
static publicKeyToProto(k:Ed25519PubKey):Uint8Array {
|
|
131
|
-
const message:KeyMessage = {
|
|
132
|
-
type: keyType.Ed25519,
|
|
133
|
-
data: k.bytes(),
|
|
134
|
-
}
|
|
135
|
-
const buffer = Ed25519PubKey.proto.encode(message).finish();
|
|
136
|
-
return buffer
|
|
137
|
-
}
|
|
138
|
-
|
|
139
|
-
// PublicKeyFromProto converts a protobuf PublicKey message into a public key object.
|
|
140
|
-
static publicKeyFromProto(buf:Uint8Array):Ed25519PubKey {
|
|
141
|
-
const decoded = Ed25519PubKey.proto.decode(buf) as unknown as KeyMessage;
|
|
142
|
-
if (decoded.type !== keyType.Ed25519) {
|
|
143
|
-
throw new Error('Invalid key type')
|
|
144
|
-
}
|
|
145
|
-
return new Ed25519PubKey(decoded.data)
|
|
146
|
-
}
|
|
147
|
-
//与dc中的go代码兼容
|
|
148
|
-
string(): string {
|
|
149
|
-
const protoBytes = Ed25519PubKey.publicKeyToProto(this)
|
|
150
|
-
// 使用 base32 编码
|
|
151
|
-
return base32.encode(protoBytes)
|
|
152
|
-
}
|
|
153
|
-
|
|
154
|
-
// 从字符串创建公钥
|
|
155
|
-
static unmarshalString(str: string): Ed25519PubKey {
|
|
156
|
-
const protoBytes = base32.decode(str)
|
|
157
|
-
return Ed25519PubKey.publicKeyFromProto(protoBytes)
|
|
158
|
-
}
|
|
159
|
-
/**
|
|
160
|
-
* Returns this key as a multihash with base58btc encoding
|
|
161
|
-
*/
|
|
162
|
-
toString(): string {
|
|
163
|
-
//生成16进制
|
|
164
|
-
return bytesToHex(this.bytes())
|
|
165
|
-
}
|
|
166
|
-
|
|
167
|
-
static fromString(str: string): Ed25519PubKey {
|
|
168
|
-
return new Ed25519PubKey(hexToBytes(str))
|
|
169
|
-
}
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
// 转换为 base58 字符串
|
|
173
|
-
toBase58() {
|
|
174
|
-
const hash = this.toMultihash()
|
|
175
|
-
return base58btc.encode(hash.bytes)
|
|
176
|
-
}
|
|
177
|
-
|
|
178
|
-
// 从 base58 字符串恢复
|
|
179
|
-
static fromBase58(str: string) {
|
|
180
|
-
const bytes = base58btc.decode(str)
|
|
181
|
-
const mh:MultihashDigest<0> = {
|
|
182
|
-
code: 0,
|
|
183
|
-
size: bytes.length - 2,
|
|
184
|
-
digest: bytes.slice(2),
|
|
185
|
-
bytes
|
|
186
|
-
}
|
|
187
|
-
return Ed25519PubKey.fromMultihash(mh)
|
|
188
|
-
}
|
|
189
|
-
|
|
190
|
-
static edPubkeyFromStr (pubkey: string) {
|
|
191
|
-
if(pubkey.indexOf('0x') === 0){
|
|
192
|
-
// hex
|
|
193
|
-
return Ed25519PubKey.fromString(pubkey.substring(2));
|
|
194
|
-
}else {
|
|
195
|
-
// base32
|
|
196
|
-
return Ed25519PubKey.unmarshalString(pubkey);
|
|
197
|
-
}
|
|
198
|
-
}
|
|
199
|
-
}
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
// ED25519私钥实现
|
|
204
|
-
export class Ed25519PrivKey implements Ed25519PrivateKey {
|
|
205
|
-
readonly type:'Ed25519'
|
|
206
|
-
readonly raw: Uint8Array
|
|
207
|
-
readonly publicKey: Ed25519PubKey
|
|
208
|
-
|
|
209
|
-
constructor(privateKey: Uint8Array) {
|
|
210
|
-
if (privateKey.length !== 64) {
|
|
211
|
-
throw new Error('ed25519: bad private key length')
|
|
212
|
-
}
|
|
213
|
-
// 用复制的方式保存私钥
|
|
214
|
-
this.raw = new Uint8Array(privateKey)
|
|
215
|
-
this.type = 'Ed25519'
|
|
216
|
-
// 公钥是私钥的后32字节
|
|
217
|
-
this.publicKey = new Ed25519PubKey(privateKey.slice(32))
|
|
218
|
-
}
|
|
219
|
-
|
|
220
|
-
private static readonly proto = (() => {
|
|
221
|
-
return PublicKeyProto;
|
|
222
|
-
})();
|
|
223
|
-
|
|
224
|
-
static fromSeed(seed: Uint8Array): Ed25519PrivKey {
|
|
225
|
-
if (seed.length !== 32) {
|
|
226
|
-
throw new Error('ed25519: bad seed length')
|
|
227
|
-
}
|
|
228
|
-
|
|
229
|
-
// 生成公钥
|
|
230
|
-
const publicKey = ed25519.getPublicKey(seed)
|
|
231
|
-
|
|
232
|
-
// 构造64字节私钥
|
|
233
|
-
const privateKey = new Uint8Array(64)
|
|
234
|
-
privateKey.set(seed)
|
|
235
|
-
privateKey.set(publicKey, 32)
|
|
236
|
-
|
|
237
|
-
return new Ed25519PrivKey(privateKey)
|
|
238
|
-
}
|
|
239
|
-
|
|
240
|
-
bytes(): Uint8Array {
|
|
241
|
-
return this.raw
|
|
242
|
-
}
|
|
243
|
-
|
|
244
|
-
sign(msg: Uint8Array): Uint8Array {
|
|
245
|
-
// 使用私钥的前32字节(种子)进行签名
|
|
246
|
-
return ed25519.sign(msg, this.raw.slice(0, 32))
|
|
247
|
-
}
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
async decrypt(data: Uint8Array): Promise<Uint8Array> {
|
|
251
|
-
const decrypted = await Encryption.decrypt(this.raw, data);
|
|
252
|
-
return decrypted
|
|
253
|
-
}
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
equals(other: Ed25519PrivKey): boolean {
|
|
257
|
-
return arrayEquals(this.bytes(),other.bytes())
|
|
258
|
-
}
|
|
259
|
-
|
|
260
|
-
// privateKeyToProto converts a public key object into an unserialized
|
|
261
|
-
// protobuf PrivateKey message.
|
|
262
|
-
static privateKeyToProto(k:Ed25519PrivKey):Uint8Array {
|
|
263
|
-
const message:KeyMessage = {
|
|
264
|
-
type: keyType.Ed25519,
|
|
265
|
-
data: k.bytes(),
|
|
266
|
-
}
|
|
267
|
-
const buffer = Ed25519PrivKey.proto.encode(message).finish();
|
|
268
|
-
return buffer
|
|
269
|
-
}
|
|
270
|
-
|
|
271
|
-
// privateKeyFromProto converts a protobuf PrivateKey message into a public key object.
|
|
272
|
-
static privateKeyFromProto(buf:Uint8Array):Ed25519PrivKey {
|
|
273
|
-
const decoded = Ed25519PrivKey.proto.decode(buf) as unknown as KeyMessage;
|
|
274
|
-
if (decoded.type !== keyType.Ed25519) {
|
|
275
|
-
throw new Error('Invalid key type')
|
|
276
|
-
}
|
|
277
|
-
return new Ed25519PrivKey(decoded.data)
|
|
278
|
-
}
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
//与dc中的go代码兼容
|
|
282
|
-
string(): string {
|
|
283
|
-
const protoBytes = Ed25519PrivKey.privateKeyToProto(this)
|
|
284
|
-
// 使用 base32 编码
|
|
285
|
-
return base32.encode(protoBytes)
|
|
286
|
-
}
|
|
287
|
-
|
|
288
|
-
// 从字符串创建私钥
|
|
289
|
-
static unmarshalString(str: string): Ed25519PrivKey {
|
|
290
|
-
const protoBytes = base32.decode(str)
|
|
291
|
-
return Ed25519PrivKey.privateKeyFromProto(protoBytes)
|
|
292
|
-
}
|
|
293
|
-
|
|
294
|
-
toString(): string {
|
|
295
|
-
return bytesToHex(this.bytes())
|
|
296
|
-
}
|
|
297
|
-
|
|
298
|
-
static fromString(str: string): Ed25519PrivKey {
|
|
299
|
-
return new Ed25519PrivKey(hexToBytes(str))
|
|
300
|
-
}
|
|
301
|
-
}
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
// 调试工具函数
|
|
306
|
-
class KeyUtils {
|
|
307
|
-
static generatePrivKey(): Ed25519PrivKey {
|
|
308
|
-
const seed = crypto.getRandomValues(new Uint8Array(32))
|
|
309
|
-
return Ed25519PrivKey.fromSeed(seed)
|
|
310
|
-
}
|
|
311
|
-
}
|
|
312
|
-
|
|
313
|
-
// 使用示例
|
|
314
|
-
async function example() {
|
|
315
|
-
try {
|
|
316
|
-
// 生成新的私钥
|
|
317
|
-
const privKey = KeyUtils.generatePrivKey()
|
|
318
|
-
console.log('Private Key:', privKey.toString())
|
|
319
|
-
|
|
320
|
-
// 获取公钥
|
|
321
|
-
const pubKey = privKey.publicKey
|
|
322
|
-
console.log('Public Key:', pubKey.toString())
|
|
323
|
-
|
|
324
|
-
// 签名示例
|
|
325
|
-
const message = new TextEncoder().encode('Hello, World!')
|
|
326
|
-
const signature = await privKey.sign(message)
|
|
327
|
-
|
|
328
|
-
// 验证签名
|
|
329
|
-
const isValid = pubKey.verify(message, signature)
|
|
330
|
-
console.log('Signature Valid:', isValid)
|
|
331
|
-
|
|
332
|
-
// 从字符串创建私钥
|
|
333
|
-
const privKeyStr = privKey.toString()
|
|
334
|
-
const recoveredPrivKey = Ed25519PrivKey.fromString(privKeyStr)
|
|
335
|
-
console.log('Keys Equal:', privKey.equals(recoveredPrivKey))
|
|
336
|
-
|
|
337
|
-
} catch (error) {
|
|
338
|
-
console.error('Error:', error)
|
|
339
|
-
}
|
|
340
|
-
}
|
|
341
|
-
|
|
342
|
-
// 运行示例
|
|
343
|
-
//example().catch(console.error)
|