@caict/bop-typescript-sdk 1.0.0
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/CHANGELOG.md +34 -0
- package/LICENSE +202 -0
- package/NOTICE +5 -0
- package/README.md +112 -0
- package/package.json +90 -0
- package/src/bop/bop-base/README.md +5 -0
- package/src/bop/bop-base/base.ts +366 -0
- package/src/bop/bop-base/index.ts +83 -0
- package/src/bop/bop-base/permit.ts +116 -0
- package/src/bop/bop-base/test/base.test.ts +867 -0
- package/src/bop/bop-base/test/index.test.ts +69 -0
- package/src/bop/bop-base/test/permit.test.ts +125 -0
- package/src/bop/bop-base/tsconfig.json +8 -0
- package/src/bop/bop-proto/README.md +2 -0
- package/src/bop/bop-proto/bop-ws.proto +266 -0
- package/src/bop/bop-proto/bop-ws.ts +4447 -0
- package/src/bop/bop-proto/bop.proto +898 -0
- package/src/bop/bop-proto/bop.ts +12890 -0
- package/src/bop/bop-proto/tsconfig.json +8 -0
- package/src/bop/bop-ws/index.ts +315 -0
- package/src/bop/bop-ws/sample/sample.ts +107 -0
- package/src/bop/bop-ws/tsconfig.json +8 -0
- package/src/bop/index.ts +14 -0
- package/src/bop-sdk/index.ts +22 -0
- package/src/bop-sdk/interface/abstract-service/abstract-account.ts +23 -0
- package/src/bop-sdk/interface/abstract-service/abstract-chain.ts +9 -0
- package/src/bop-sdk/interface/abstract-service/abstract-contract.ts +16 -0
- package/src/bop-sdk/interface/abstract-service/abstract-ledger.ts +15 -0
- package/src/bop-sdk/interface/abstract-service/abstract-provider.ts +13 -0
- package/src/bop-sdk/interface/abstract-service/abstract-signer.ts +133 -0
- package/src/bop-sdk/interface/abstract-service/abstract-transaction.ts +86 -0
- package/src/bop-sdk/interface/provider-ws.ts +10 -0
- package/src/bop-sdk/interface/provider.ts +22 -0
- package/src/bop-sdk/interface/service/3rd.ts +54 -0
- package/src/bop-sdk/interface/service/account.ts +86 -0
- package/src/bop-sdk/interface/service/chain.ts +27 -0
- package/src/bop-sdk/interface/service/contract.ts +51 -0
- package/src/bop-sdk/interface/service/helper.ts +426 -0
- package/src/bop-sdk/interface/service/ledger.ts +75 -0
- package/src/bop-sdk/interface/service/offline/check/index.js +138 -0
- package/src/bop-sdk/interface/service/offline/common/constant.js +6 -0
- package/src/bop-sdk/interface/service/offline/common/snowFlakeUtils.js +192 -0
- package/src/bop-sdk/interface/service/offline/common/util.js +511 -0
- package/src/bop-sdk/interface/service/offline/exception/customErrors.js +29 -0
- package/src/bop-sdk/interface/service/offline/exception/errors.js +90 -0
- package/src/bop-sdk/interface/service/offline/exception/index.js +5 -0
- package/src/bop-sdk/interface/service/offline/operation/accountCreateOperation.js +28 -0
- package/src/bop-sdk/interface/service/offline/operation/accountSetMetadataOperation.js +44 -0
- package/src/bop-sdk/interface/service/offline/operation/accountSetPrivilegeOperation.js +49 -0
- package/src/bop-sdk/interface/service/offline/operation/contractCreateOperation.js +43 -0
- package/src/bop-sdk/interface/service/offline/operation/contractInvokeOperation.js +42 -0
- package/src/bop-sdk/interface/service/offline/operation/contractInvokeRequestOperation.js +69 -0
- package/src/bop-sdk/interface/service/offline/operation/gasSendOperation.js +29 -0
- package/src/bop-sdk/interface/service/offline/operation/gasSendRequestOperation.js +61 -0
- package/src/bop-sdk/interface/service/offline/operation/index.js +20 -0
- package/src/bop-sdk/interface/service/offline/proto/chain_pb.js +12079 -0
- package/src/bop-sdk/interface/service/offline/proto/common_pb.js +2867 -0
- package/src/bop-sdk/interface/service/offline/transaction/index.js +988 -0
- package/src/bop-sdk/interface/service/offline/transaction/operations.js +188 -0
- package/src/bop-sdk/interface/service/offline/validate/account.js +152 -0
- package/src/bop-sdk/interface/service/offline/validate/contract.js +101 -0
- package/src/bop-sdk/interface/service/offline/validate/index.js +10 -0
- package/src/bop-sdk/interface/service/offline/validate/transaction.js +51 -0
- package/src/bop-sdk/interface/service/transaction.ts +731 -0
- package/src/bop-sdk/interface/signer.ts +46 -0
- package/src/bop-sdk/interface/test/3rd.test.ts +1571 -0
- package/src/bop-sdk/interface/test/account.test.ts +284 -0
- package/src/bop-sdk/interface/test/chain.test.ts +45 -0
- package/src/bop-sdk/interface/test/contract.test.ts +283 -0
- package/src/bop-sdk/interface/test/ledger.test.ts +166 -0
- package/src/bop-sdk/interface/test/provider.test.ts +407 -0
- package/src/bop-sdk/interface/test/signer.test.ts +105 -0
- package/src/bop-sdk/interface/test/transaction.test.ts +713 -0
- package/src/bop-sdk/proto/bop-sdk-common.proto +159 -0
- package/src/bop-sdk/proto/bop-sdk-common.ts +829 -0
- package/src/bop-sdk/proto/bop-sdk-interface.proto +581 -0
- package/src/bop-sdk/proto/bop-sdk-interface.ts +10839 -0
- package/src/bop-sdk/proto/bop-sdk-utils.proto +86 -0
- package/src/bop-sdk/proto/bop-sdk-utils.ts +1495 -0
- package/src/bop-sdk/utils/abi/abi-coder.ts +147 -0
- package/src/bop-sdk/utils/abi/address.ts +109 -0
- package/src/bop-sdk/utils/abi/bignumber.ts +403 -0
- package/src/bop-sdk/utils/abi/bytes.ts +565 -0
- package/src/bop-sdk/utils/abi/coders/abstract-coder.ts +239 -0
- package/src/bop-sdk/utils/abi/coders/address.ts +58 -0
- package/src/bop-sdk/utils/abi/coders/anonymous.ts +25 -0
- package/src/bop-sdk/utils/abi/coders/array.ts +275 -0
- package/src/bop-sdk/utils/abi/coders/boolean.ts +21 -0
- package/src/bop-sdk/utils/abi/coders/bytes.ts +36 -0
- package/src/bop-sdk/utils/abi/coders/fixed-bytes.ts +35 -0
- package/src/bop-sdk/utils/abi/coders/null.ts +25 -0
- package/src/bop-sdk/utils/abi/coders/number.ts +56 -0
- package/src/bop-sdk/utils/abi/coders/string.ts +24 -0
- package/src/bop-sdk/utils/abi/coders/tuple.ts +73 -0
- package/src/bop-sdk/utils/abi/constants/addresses.ts +1 -0
- package/src/bop-sdk/utils/abi/constants/bignumbers.ts +30 -0
- package/src/bop-sdk/utils/abi/constants/hashes.ts +2 -0
- package/src/bop-sdk/utils/abi/constants/index.ts +15 -0
- package/src/bop-sdk/utils/abi/constants/strings.ts +2 -0
- package/src/bop-sdk/utils/abi/fragments.ts +1243 -0
- package/src/bop-sdk/utils/abi/id.ts +6 -0
- package/src/bop-sdk/utils/abi/index.ts +46 -0
- package/src/bop-sdk/utils/abi/interface.ts +921 -0
- package/src/bop-sdk/utils/abi/keccak256.ts +9 -0
- package/src/bop-sdk/utils/abi/properties.ts +172 -0
- package/src/bop-sdk/utils/abi/strings/bytes32.ts +40 -0
- package/src/bop-sdk/utils/abi/strings/idna.ts +258 -0
- package/src/bop-sdk/utils/abi/strings/index.ts +28 -0
- package/src/bop-sdk/utils/abi/strings/utf8.ts +373 -0
- package/src/bop-sdk/utils/encryption-address.ts +501 -0
- package/src/bop-sdk/utils/encryption-crypto.ts +427 -0
- package/src/bop-sdk/utils/encryption-mnemonics.ts +120 -0
- package/src/bop-sdk/utils/index.ts +27 -0
- package/src/bop-sdk/utils/logger.ts +480 -0
- package/src/index.ts +35 -0
|
@@ -0,0 +1,501 @@
|
|
|
1
|
+
import * as bifEncryption from "@caict/bif-encryption";
|
|
2
|
+
import {
|
|
3
|
+
CheckKeyResponse,
|
|
4
|
+
GetAccountPublicKeyInfoResponse,
|
|
5
|
+
GetBidAndKeyPairResponse,
|
|
6
|
+
GetKeyResponse,
|
|
7
|
+
KeyType,
|
|
8
|
+
} from "../proto/bop-sdk-utils";
|
|
9
|
+
import { SdkStatusCode } from "../proto/bop-sdk-common";
|
|
10
|
+
import base58, * as bs58 from "@bifproject/bs58";
|
|
11
|
+
|
|
12
|
+
export class EncryptionAddress {
|
|
13
|
+
constructor() {}
|
|
14
|
+
|
|
15
|
+
private rawKeyToHex(rawPrivateKey?: Uint8Array): string {
|
|
16
|
+
if (rawPrivateKey === undefined) {
|
|
17
|
+
return "";
|
|
18
|
+
}
|
|
19
|
+
let hexStr: string = "";
|
|
20
|
+
hexStr += Array.from(rawPrivateKey)
|
|
21
|
+
.map((byte) => byte.toString(16).padStart(2, "0"))
|
|
22
|
+
.join("");
|
|
23
|
+
return hexStr;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
private remove0xPrefix(str: string): string {
|
|
27
|
+
if (str.startsWith("0x")) {
|
|
28
|
+
return str.substring(2);
|
|
29
|
+
}
|
|
30
|
+
return str;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
private isHexadecimalString(str: string): boolean {
|
|
34
|
+
const hexRegex = /^[0-9a-fA-F]+$/;
|
|
35
|
+
return hexRegex.test(this.remove0xPrefix(str));
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
private hexStringToNumberArray(hexStr: string): number[] | null {
|
|
39
|
+
if (!this.isHexadecimalString(hexStr)) {
|
|
40
|
+
return null;
|
|
41
|
+
}
|
|
42
|
+
const byteArray: number[] = [];
|
|
43
|
+
for (let i = 0; i < hexStr.length; i += 2) {
|
|
44
|
+
const byteHex = hexStr.slice(i, i + 2);
|
|
45
|
+
byteArray.push(parseInt(byteHex, 16));
|
|
46
|
+
}
|
|
47
|
+
return byteArray;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
private startsWithDidBid(identifier: string): boolean {
|
|
51
|
+
const regex = /^(did:bid:ef|did:bid:zf)/;
|
|
52
|
+
return regex.test(identifier);
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
private isValidChainCode(str: string): boolean {
|
|
56
|
+
// 使用正则表达式来检查字符串是否仅包含数字和小写字母
|
|
57
|
+
const regex = /^[a-z0-9]{4}$/;
|
|
58
|
+
return regex.test(str);
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
public getBidAndKeyPair(chainCode?: string): GetBidAndKeyPairResponse {
|
|
62
|
+
interface InternalAccountKeyInfo {
|
|
63
|
+
encPrivateKey?: string | undefined;
|
|
64
|
+
encPublicKey?: string | undefined;
|
|
65
|
+
encAddress?: string | undefined;
|
|
66
|
+
rawPrivateKey?: Uint8Array | undefined;
|
|
67
|
+
rawPublicKey?: Uint8Array | undefined;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
if (!this.isValidChainCode(chainCode) && chainCode !== "") {
|
|
71
|
+
const response: GetBidAndKeyPairResponse = {
|
|
72
|
+
errorCode: SdkStatusCode.INVALIDCHAINCODE,
|
|
73
|
+
errorDesc: "invalid chain code",
|
|
74
|
+
};
|
|
75
|
+
return response;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
let keypair = bifEncryption.getBidAndKeyPair(
|
|
79
|
+
chainCode,
|
|
80
|
+
) as InternalAccountKeyInfo;
|
|
81
|
+
const response: GetBidAndKeyPairResponse = {
|
|
82
|
+
errorCode: SdkStatusCode.SUCCESS,
|
|
83
|
+
errorDesc: "",
|
|
84
|
+
result: {
|
|
85
|
+
keyType: KeyType.ED25519,
|
|
86
|
+
encPrivateKey: keypair.encPrivateKey,
|
|
87
|
+
encPublicKey: keypair.encPublicKey,
|
|
88
|
+
encAddress: keypair.encAddress,
|
|
89
|
+
rawPrivateKey: this.rawKeyToHex(keypair.rawPrivateKey),
|
|
90
|
+
rawPublicKey: this.rawKeyToHex(keypair.rawPublicKey),
|
|
91
|
+
},
|
|
92
|
+
};
|
|
93
|
+
return response;
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
public getBidAndKeyPairBySM2(chainCode?: string): GetBidAndKeyPairResponse {
|
|
97
|
+
interface InternalAccountKeyInfo {
|
|
98
|
+
encPrivateKey?: string | undefined;
|
|
99
|
+
encPublicKey?: string | undefined;
|
|
100
|
+
encAddress?: string | undefined;
|
|
101
|
+
rawPrivateKey?: Uint8Array | undefined;
|
|
102
|
+
rawPublicKey?: Uint8Array | undefined;
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
if (
|
|
106
|
+
!this.isValidChainCode(chainCode) &&
|
|
107
|
+
chainCode !== undefined &&
|
|
108
|
+
chainCode !== ""
|
|
109
|
+
) {
|
|
110
|
+
const response: GetBidAndKeyPairResponse = {
|
|
111
|
+
errorCode: SdkStatusCode.INVALIDCHAINCODE,
|
|
112
|
+
errorDesc: "invalid chain code",
|
|
113
|
+
};
|
|
114
|
+
return response;
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
let keypair = bifEncryption.getBidAndKeyPairBySM2(
|
|
118
|
+
chainCode,
|
|
119
|
+
) as InternalAccountKeyInfo;
|
|
120
|
+
const response: GetBidAndKeyPairResponse = {
|
|
121
|
+
errorCode: SdkStatusCode.SUCCESS,
|
|
122
|
+
errorDesc: "",
|
|
123
|
+
result: {
|
|
124
|
+
keyType: KeyType.SM2,
|
|
125
|
+
encPrivateKey: keypair.encPrivateKey,
|
|
126
|
+
encPublicKey: keypair.encPublicKey,
|
|
127
|
+
encAddress: keypair.encAddress,
|
|
128
|
+
rawPrivateKey: this.rawKeyToHex(keypair.rawPrivateKey),
|
|
129
|
+
rawPublicKey: this.rawKeyToHex(keypair.rawPublicKey),
|
|
130
|
+
},
|
|
131
|
+
};
|
|
132
|
+
return response;
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
public privateKeyManager(
|
|
136
|
+
type: KeyType,
|
|
137
|
+
chainCode?: string,
|
|
138
|
+
): GetBidAndKeyPairResponse {
|
|
139
|
+
interface InternalAccountKeyInfo {
|
|
140
|
+
encPrivateKey?: string | undefined;
|
|
141
|
+
encPublicKey?: string | undefined;
|
|
142
|
+
encAddress?: string | undefined;
|
|
143
|
+
rawPrivateKey?: Uint8Array | undefined;
|
|
144
|
+
rawPublicKey?: Uint8Array | undefined;
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
if (type !== KeyType.ED25519 && type != KeyType.SM2) {
|
|
148
|
+
const response: GetBidAndKeyPairResponse = {
|
|
149
|
+
errorCode: SdkStatusCode.UNSUPPORTKEYTYPE,
|
|
150
|
+
errorDesc: "unsupport key type",
|
|
151
|
+
};
|
|
152
|
+
|
|
153
|
+
return response;
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
if (
|
|
157
|
+
!this.isValidChainCode(chainCode) &&
|
|
158
|
+
chainCode !== undefined &&
|
|
159
|
+
chainCode !== ""
|
|
160
|
+
) {
|
|
161
|
+
const response: GetBidAndKeyPairResponse = {
|
|
162
|
+
errorCode: SdkStatusCode.INVALIDCHAINCODE,
|
|
163
|
+
errorDesc: "invalid chain code",
|
|
164
|
+
};
|
|
165
|
+
return response;
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
let keypair = bifEncryption.privateKeyManager(
|
|
169
|
+
type === KeyType.ED25519 ? 0x65 : 0x7a,
|
|
170
|
+
chainCode,
|
|
171
|
+
) as InternalAccountKeyInfo;
|
|
172
|
+
const response: GetBidAndKeyPairResponse = {
|
|
173
|
+
errorCode: SdkStatusCode.SUCCESS,
|
|
174
|
+
errorDesc: "",
|
|
175
|
+
result: {
|
|
176
|
+
keyType: type,
|
|
177
|
+
encPrivateKey: keypair.encPrivateKey,
|
|
178
|
+
encPublicKey: keypair.encPublicKey,
|
|
179
|
+
encAddress: keypair.encAddress,
|
|
180
|
+
rawPrivateKey: this.rawKeyToHex(keypair.rawPrivateKey),
|
|
181
|
+
rawPublicKey: this.rawKeyToHex(keypair.rawPublicKey),
|
|
182
|
+
},
|
|
183
|
+
};
|
|
184
|
+
return response;
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
public privateKeyManagerByKey(priKey: string): GetBidAndKeyPairResponse {
|
|
188
|
+
try {
|
|
189
|
+
priKey = this.remove0xPrefix(priKey);
|
|
190
|
+
interface InternalAccountKeyInfo {
|
|
191
|
+
encPrivateKey?: string | undefined;
|
|
192
|
+
encPublicKey?: string | undefined;
|
|
193
|
+
encAddress?: string | undefined;
|
|
194
|
+
rawPrivateKey?: Uint8Array | undefined;
|
|
195
|
+
rawPublicKey?: Uint8Array | undefined;
|
|
196
|
+
}
|
|
197
|
+
let keypair = bifEncryption.privateKeyManagerByKey(
|
|
198
|
+
priKey,
|
|
199
|
+
) as InternalAccountKeyInfo;
|
|
200
|
+
const response: GetBidAndKeyPairResponse = {
|
|
201
|
+
errorCode: SdkStatusCode.SUCCESS,
|
|
202
|
+
errorDesc: "",
|
|
203
|
+
result: {
|
|
204
|
+
keyType:
|
|
205
|
+
bifEncryption.getCryptoTypeFromPrivKey(priKey) === 0x65
|
|
206
|
+
? KeyType.ED25519
|
|
207
|
+
: KeyType.SM2,
|
|
208
|
+
encPrivateKey: keypair.encPrivateKey,
|
|
209
|
+
encPublicKey: keypair.encPublicKey,
|
|
210
|
+
encAddress: keypair.encAddress,
|
|
211
|
+
rawPrivateKey: this.rawKeyToHex(keypair.rawPrivateKey),
|
|
212
|
+
rawPublicKey: this.rawKeyToHex(keypair.rawPublicKey),
|
|
213
|
+
},
|
|
214
|
+
};
|
|
215
|
+
return response;
|
|
216
|
+
} catch (error: any) {
|
|
217
|
+
const response: GetBidAndKeyPairResponse = {
|
|
218
|
+
errorCode: SdkStatusCode.INVALIDPRIVATEKEY,
|
|
219
|
+
errorDesc: error.message,
|
|
220
|
+
};
|
|
221
|
+
return response;
|
|
222
|
+
}
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
public publicKeyManager(pubKey: string): GetAccountPublicKeyInfoResponse {
|
|
226
|
+
try {
|
|
227
|
+
pubKey = this.remove0xPrefix(pubKey);
|
|
228
|
+
interface InternalAccountPublicKeyInfo {
|
|
229
|
+
keyType?: KeyType | undefined;
|
|
230
|
+
encPublicKey?: string | undefined;
|
|
231
|
+
rawPublicKey?: Uint8Array | undefined;
|
|
232
|
+
encAddress?: string | undefined;
|
|
233
|
+
}
|
|
234
|
+
let keypair = bifEncryption.publicKeyManager(
|
|
235
|
+
pubKey,
|
|
236
|
+
) as InternalAccountPublicKeyInfo;
|
|
237
|
+
const response: GetAccountPublicKeyInfoResponse = {
|
|
238
|
+
errorCode: SdkStatusCode.SUCCESS,
|
|
239
|
+
errorDesc: "",
|
|
240
|
+
result: {
|
|
241
|
+
keyType:
|
|
242
|
+
bifEncryption.getCryptoTypeFromPubKey(pubKey) === 0x65
|
|
243
|
+
? KeyType.ED25519
|
|
244
|
+
: KeyType.SM2,
|
|
245
|
+
encPublicKey: pubKey,
|
|
246
|
+
rawPublicKey: this.rawKeyToHex(keypair.rawPublicKey),
|
|
247
|
+
encAddress: keypair.encAddress,
|
|
248
|
+
},
|
|
249
|
+
};
|
|
250
|
+
return response;
|
|
251
|
+
} catch (error: any) {
|
|
252
|
+
const response: GetAccountPublicKeyInfoResponse = {
|
|
253
|
+
errorCode: SdkStatusCode.INVALIDPUBLICKEY,
|
|
254
|
+
errorDesc: error.message,
|
|
255
|
+
};
|
|
256
|
+
return response;
|
|
257
|
+
}
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
public getEncPublicKey(priKey: string): GetKeyResponse {
|
|
261
|
+
try {
|
|
262
|
+
priKey = this.remove0xPrefix(priKey);
|
|
263
|
+
let encPublicKey = bifEncryption.getEncPublicKey(priKey) as string;
|
|
264
|
+
const response: GetKeyResponse = {
|
|
265
|
+
errorCode: SdkStatusCode.SUCCESS,
|
|
266
|
+
errorDesc: "",
|
|
267
|
+
result: encPublicKey,
|
|
268
|
+
};
|
|
269
|
+
return response;
|
|
270
|
+
} catch (error: any) {
|
|
271
|
+
const response: GetKeyResponse = {
|
|
272
|
+
errorCode: SdkStatusCode.INVALIDPRIVATEKEY,
|
|
273
|
+
errorDesc: error.message,
|
|
274
|
+
};
|
|
275
|
+
return response;
|
|
276
|
+
}
|
|
277
|
+
}
|
|
278
|
+
|
|
279
|
+
public getEncPublicKeyByRaw(
|
|
280
|
+
type: KeyType,
|
|
281
|
+
rawPublicKey: string,
|
|
282
|
+
): GetKeyResponse {
|
|
283
|
+
try {
|
|
284
|
+
if (type !== KeyType.ED25519 && type != KeyType.SM2) {
|
|
285
|
+
const response: GetKeyResponse = {
|
|
286
|
+
errorCode: SdkStatusCode.UNSUPPORTKEYTYPE,
|
|
287
|
+
errorDesc: "unsupport key type",
|
|
288
|
+
};
|
|
289
|
+
return response;
|
|
290
|
+
}
|
|
291
|
+
let encPublicKey = bifEncryption.getEncPublicKeyByRaw(
|
|
292
|
+
this.hexStringToNumberArray(this.remove0xPrefix(rawPublicKey)),
|
|
293
|
+
type === KeyType.ED25519 ? 0x65 : 0x7a,
|
|
294
|
+
) as string;
|
|
295
|
+
|
|
296
|
+
const response: GetKeyResponse = {
|
|
297
|
+
errorCode: SdkStatusCode.SUCCESS,
|
|
298
|
+
errorDesc: "",
|
|
299
|
+
result: encPublicKey,
|
|
300
|
+
};
|
|
301
|
+
return response;
|
|
302
|
+
} catch (error: any) {
|
|
303
|
+
const response: GetKeyResponse = {
|
|
304
|
+
errorCode: SdkStatusCode.INVALIDPUBLICKEY,
|
|
305
|
+
errorDesc: error.message,
|
|
306
|
+
};
|
|
307
|
+
return response;
|
|
308
|
+
}
|
|
309
|
+
}
|
|
310
|
+
|
|
311
|
+
public getEncPrivateKeyByRaw(
|
|
312
|
+
type: KeyType,
|
|
313
|
+
rawPrivateKey: string,
|
|
314
|
+
): GetKeyResponse {
|
|
315
|
+
try {
|
|
316
|
+
if (type !== KeyType.ED25519 && type != KeyType.SM2) {
|
|
317
|
+
const response: GetKeyResponse = {
|
|
318
|
+
errorCode: SdkStatusCode.UNSUPPORTKEYTYPE,
|
|
319
|
+
errorDesc: "unsupport key type",
|
|
320
|
+
};
|
|
321
|
+
return response;
|
|
322
|
+
}
|
|
323
|
+
|
|
324
|
+
let encPrivateKey = bifEncryption.getEncPrivateKeyByRaw(
|
|
325
|
+
this.hexStringToNumberArray(this.remove0xPrefix(rawPrivateKey)),
|
|
326
|
+
type === KeyType.ED25519 ? 0x65 : 0x7a,
|
|
327
|
+
) as string;
|
|
328
|
+
|
|
329
|
+
const response: GetKeyResponse = {
|
|
330
|
+
errorCode: SdkStatusCode.SUCCESS,
|
|
331
|
+
errorDesc: "",
|
|
332
|
+
result: encPrivateKey,
|
|
333
|
+
};
|
|
334
|
+
return response;
|
|
335
|
+
} catch (error: any) {
|
|
336
|
+
const response: GetKeyResponse = {
|
|
337
|
+
errorCode: SdkStatusCode.INVALIDPRIVATEKEY,
|
|
338
|
+
errorDesc: error.message,
|
|
339
|
+
};
|
|
340
|
+
return response;
|
|
341
|
+
}
|
|
342
|
+
}
|
|
343
|
+
|
|
344
|
+
public parsePrivateKey(encPrivateKey: string): GetKeyResponse {
|
|
345
|
+
try {
|
|
346
|
+
let rawPrivateKey = bifEncryption.parsePrivateKey(
|
|
347
|
+
this.remove0xPrefix(encPrivateKey),
|
|
348
|
+
) as Uint8Array;
|
|
349
|
+
const response: GetKeyResponse = {
|
|
350
|
+
errorCode: SdkStatusCode.SUCCESS,
|
|
351
|
+
errorDesc: "",
|
|
352
|
+
result: this.rawKeyToHex(rawPrivateKey),
|
|
353
|
+
};
|
|
354
|
+
return response;
|
|
355
|
+
} catch (error: any) {
|
|
356
|
+
const response: GetKeyResponse = {
|
|
357
|
+
errorCode: SdkStatusCode.INVALIDPRIVATEKEY,
|
|
358
|
+
errorDesc: error.message,
|
|
359
|
+
};
|
|
360
|
+
return response;
|
|
361
|
+
}
|
|
362
|
+
}
|
|
363
|
+
|
|
364
|
+
public parsePublicKey(encPublicKey: string): GetKeyResponse {
|
|
365
|
+
try {
|
|
366
|
+
let rawPublicKey = bifEncryption.parsePublicKey(
|
|
367
|
+
this.remove0xPrefix(encPublicKey),
|
|
368
|
+
) as Uint8Array;
|
|
369
|
+
const response: GetKeyResponse = {
|
|
370
|
+
errorCode: SdkStatusCode.SUCCESS,
|
|
371
|
+
errorDesc: "",
|
|
372
|
+
result: this.rawKeyToHex(rawPublicKey),
|
|
373
|
+
};
|
|
374
|
+
return response;
|
|
375
|
+
} catch (error: any) {
|
|
376
|
+
const response: GetKeyResponse = {
|
|
377
|
+
errorCode: SdkStatusCode.INVALIDPUBLICKEY,
|
|
378
|
+
errorDesc: error.message,
|
|
379
|
+
};
|
|
380
|
+
return response;
|
|
381
|
+
}
|
|
382
|
+
}
|
|
383
|
+
|
|
384
|
+
public publicToAddress(encPublicKey: string): GetKeyResponse {
|
|
385
|
+
try {
|
|
386
|
+
let rawPublicKey = bifEncryption.publicToAddress(
|
|
387
|
+
this.remove0xPrefix(encPublicKey),
|
|
388
|
+
) as string;
|
|
389
|
+
const response: GetKeyResponse = {
|
|
390
|
+
errorCode: SdkStatusCode.SUCCESS,
|
|
391
|
+
errorDesc: "",
|
|
392
|
+
result: rawPublicKey,
|
|
393
|
+
};
|
|
394
|
+
return response;
|
|
395
|
+
} catch (error: any) {
|
|
396
|
+
const response: GetKeyResponse = {
|
|
397
|
+
errorCode: SdkStatusCode.INVALIDPUBLICKEY,
|
|
398
|
+
errorDesc: error.message,
|
|
399
|
+
};
|
|
400
|
+
return response;
|
|
401
|
+
}
|
|
402
|
+
}
|
|
403
|
+
|
|
404
|
+
public isPrivateKey(encPrivateKey: string): CheckKeyResponse {
|
|
405
|
+
let isPri = bifEncryption.isPrivateKey(
|
|
406
|
+
this.remove0xPrefix(encPrivateKey),
|
|
407
|
+
) as boolean;
|
|
408
|
+
const response: CheckKeyResponse = {
|
|
409
|
+
errorCode: SdkStatusCode.SUCCESS,
|
|
410
|
+
errorDesc: "",
|
|
411
|
+
result: isPri,
|
|
412
|
+
};
|
|
413
|
+
return response;
|
|
414
|
+
}
|
|
415
|
+
|
|
416
|
+
public isPublicKey(encPublicKey: string): CheckKeyResponse {
|
|
417
|
+
let isPub = bifEncryption.isPublicKey(
|
|
418
|
+
this.remove0xPrefix(encPublicKey),
|
|
419
|
+
) as boolean;
|
|
420
|
+
const response: CheckKeyResponse = {
|
|
421
|
+
errorCode: SdkStatusCode.SUCCESS,
|
|
422
|
+
errorDesc: "",
|
|
423
|
+
result: isPub,
|
|
424
|
+
};
|
|
425
|
+
return response;
|
|
426
|
+
}
|
|
427
|
+
|
|
428
|
+
public encAddressToHex(encAddress: string): GetKeyResponse {
|
|
429
|
+
try {
|
|
430
|
+
if (!this.startsWithDidBid(encAddress)) {
|
|
431
|
+
throw new Error("invalid address");
|
|
432
|
+
}
|
|
433
|
+
// 截取掉did:bid:前缀
|
|
434
|
+
const addressWithoutPrefix = encAddress.slice(8);
|
|
435
|
+
let prefixCode: string;
|
|
436
|
+
if (addressWithoutPrefix.startsWith("ef")) {
|
|
437
|
+
prefixCode = "0x6566";
|
|
438
|
+
} else {
|
|
439
|
+
prefixCode = "0x7a66";
|
|
440
|
+
}
|
|
441
|
+
const base58Decoded = base58.decode(addressWithoutPrefix.slice(2));
|
|
442
|
+
|
|
443
|
+
let hexStr = "";
|
|
444
|
+
for (const byte of base58Decoded) {
|
|
445
|
+
const hex = byte.toString(16).padStart(2, "0");
|
|
446
|
+
hexStr += hex;
|
|
447
|
+
}
|
|
448
|
+
// 拼接字符串
|
|
449
|
+
const result = prefixCode + hexStr;
|
|
450
|
+
const response: GetKeyResponse = {
|
|
451
|
+
errorCode: SdkStatusCode.SUCCESS,
|
|
452
|
+
errorDesc: "",
|
|
453
|
+
result: result,
|
|
454
|
+
};
|
|
455
|
+
return response;
|
|
456
|
+
} catch (error: any) {
|
|
457
|
+
const response: GetKeyResponse = {
|
|
458
|
+
errorCode: SdkStatusCode.INVALIDADDRESS,
|
|
459
|
+
errorDesc: error.message,
|
|
460
|
+
};
|
|
461
|
+
return response;
|
|
462
|
+
}
|
|
463
|
+
}
|
|
464
|
+
|
|
465
|
+
public hexToEncAddress(hexAddress: string): GetKeyResponse {
|
|
466
|
+
try {
|
|
467
|
+
if (!this.isHexadecimalString(hexAddress)) {
|
|
468
|
+
throw new Error("invalid address");
|
|
469
|
+
}
|
|
470
|
+
// 去除0x前缀(如果存在)
|
|
471
|
+
let addressWithout0x = this.remove0xPrefix(hexAddress);
|
|
472
|
+
// 截取前两个字节
|
|
473
|
+
const prefixBytes = addressWithout0x.slice(0, 4);
|
|
474
|
+
let prefix;
|
|
475
|
+
if (prefixBytes === "6566") {
|
|
476
|
+
prefix = "ef";
|
|
477
|
+
} else if (prefixBytes === "7a66") {
|
|
478
|
+
prefix = "zf";
|
|
479
|
+
} else {
|
|
480
|
+
throw new Error("invalid address");
|
|
481
|
+
}
|
|
482
|
+
// 对剩余数据进行Base58编码
|
|
483
|
+
const dataToEncode = addressWithout0x.slice(4);
|
|
484
|
+
const base58Encoded = bs58.encode(Buffer.from(dataToEncode, "hex"));
|
|
485
|
+
// 拼接并返回最终的地址字符串
|
|
486
|
+
const result = `did:bid:${prefix}${base58Encoded}`;
|
|
487
|
+
const response: GetKeyResponse = {
|
|
488
|
+
errorCode: SdkStatusCode.SUCCESS,
|
|
489
|
+
errorDesc: "",
|
|
490
|
+
result: result,
|
|
491
|
+
};
|
|
492
|
+
return response;
|
|
493
|
+
} catch (error: any) {
|
|
494
|
+
const response: GetKeyResponse = {
|
|
495
|
+
errorCode: SdkStatusCode.INVALIDADDRESS,
|
|
496
|
+
errorDesc: error.message,
|
|
497
|
+
};
|
|
498
|
+
return response;
|
|
499
|
+
}
|
|
500
|
+
}
|
|
501
|
+
}
|