@human-protocol/sdk 6.0.0 → 7.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.
Files changed (129) hide show
  1. package/CHANGELOG.md +34 -0
  2. package/dist/base.d.ts +4 -2
  3. package/dist/base.d.ts.map +1 -1
  4. package/dist/base.js +14 -0
  5. package/dist/constants.d.ts +0 -1
  6. package/dist/constants.d.ts.map +1 -1
  7. package/dist/constants.js +27 -16
  8. package/dist/encryption/encryption.d.ts +84 -0
  9. package/dist/encryption/encryption.d.ts.map +1 -0
  10. package/dist/{encryption.js → encryption/encryption.js} +3 -161
  11. package/dist/encryption/encryption_utils.d.ts +101 -0
  12. package/dist/encryption/encryption_utils.d.ts.map +1 -0
  13. package/dist/encryption/encryption_utils.js +191 -0
  14. package/dist/encryption/index.d.ts +4 -0
  15. package/dist/encryption/index.d.ts.map +1 -0
  16. package/dist/encryption/index.js +7 -0
  17. package/dist/encryption/types.d.ts +9 -0
  18. package/dist/encryption/types.d.ts.map +1 -0
  19. package/dist/encryption/types.js +9 -0
  20. package/dist/error.d.ts +6 -10
  21. package/dist/error.d.ts.map +1 -1
  22. package/dist/error.js +12 -14
  23. package/dist/{escrow.d.ts → escrow/escrow_client.d.ts} +16 -196
  24. package/dist/escrow/escrow_client.d.ts.map +1 -0
  25. package/dist/{escrow.js → escrow/escrow_client.js} +50 -430
  26. package/dist/escrow/escrow_utils.d.ts +172 -0
  27. package/dist/escrow/escrow_utils.d.ts.map +1 -0
  28. package/dist/escrow/escrow_utils.js +388 -0
  29. package/dist/escrow/index.d.ts +3 -0
  30. package/dist/escrow/index.d.ts.map +1 -0
  31. package/dist/escrow/index.js +7 -0
  32. package/dist/graphql/queries/statistics.d.ts +1 -0
  33. package/dist/graphql/queries/statistics.d.ts.map +1 -1
  34. package/dist/graphql/queries/statistics.js +36 -2
  35. package/dist/graphql/types.d.ts +3 -1
  36. package/dist/graphql/types.d.ts.map +1 -1
  37. package/dist/index.d.ts +10 -10
  38. package/dist/index.d.ts.map +1 -1
  39. package/dist/index.js +23 -22
  40. package/dist/interfaces.d.ts +2 -7
  41. package/dist/interfaces.d.ts.map +1 -1
  42. package/dist/kvstore/index.d.ts +3 -0
  43. package/dist/kvstore/index.d.ts.map +1 -0
  44. package/dist/kvstore/index.js +7 -0
  45. package/dist/{kvstore.d.ts → kvstore/kvstore_client.d.ts} +7 -112
  46. package/dist/kvstore/kvstore_client.d.ts.map +1 -0
  47. package/dist/{kvstore.js → kvstore/kvstore_client.js} +25 -187
  48. package/dist/kvstore/kvstore_utils.d.ts +105 -0
  49. package/dist/kvstore/kvstore_utils.d.ts.map +1 -0
  50. package/dist/kvstore/kvstore_utils.js +184 -0
  51. package/dist/operator/index.d.ts +2 -0
  52. package/dist/operator/index.d.ts.map +1 -0
  53. package/dist/operator/index.js +5 -0
  54. package/dist/{operator.d.ts → operator/operator_utils.d.ts} +3 -3
  55. package/dist/operator/operator_utils.d.ts.map +1 -0
  56. package/dist/{operator.js → operator/operator_utils.js} +6 -6
  57. package/dist/staking/index.d.ts +3 -0
  58. package/dist/staking/index.d.ts.map +1 -0
  59. package/dist/staking/index.js +7 -0
  60. package/dist/{staking.d.ts → staking/staking_client.d.ts} +10 -71
  61. package/dist/staking/staking_client.d.ts.map +1 -0
  62. package/dist/{staking.js → staking/staking_client.js} +11 -141
  63. package/dist/staking/staking_utils.d.ts +63 -0
  64. package/dist/staking/staking_utils.d.ts.map +1 -0
  65. package/dist/staking/staking_utils.js +137 -0
  66. package/dist/statistics/index.d.ts +2 -0
  67. package/dist/statistics/index.d.ts.map +1 -0
  68. package/dist/statistics/index.js +5 -0
  69. package/dist/{statistics.d.ts → statistics/statistics_utils.d.ts} +4 -13
  70. package/dist/statistics/statistics_utils.d.ts.map +1 -0
  71. package/dist/{statistics.js → statistics/statistics_utils.js} +8 -22
  72. package/dist/transaction/index.d.ts +2 -0
  73. package/dist/transaction/index.d.ts.map +1 -0
  74. package/dist/transaction/index.js +5 -0
  75. package/dist/{transaction.d.ts → transaction/transaction_utils.d.ts} +5 -5
  76. package/dist/transaction/transaction_utils.d.ts.map +1 -0
  77. package/dist/{transaction.js → transaction/transaction_utils.js} +9 -9
  78. package/dist/types.d.ts +22 -1
  79. package/dist/types.d.ts.map +1 -1
  80. package/dist/utils.d.ts +16 -1
  81. package/dist/utils.d.ts.map +1 -1
  82. package/dist/utils.js +72 -6
  83. package/dist/worker/index.d.ts +2 -0
  84. package/dist/worker/index.d.ts.map +1 -0
  85. package/dist/worker/index.js +5 -0
  86. package/dist/{worker.d.ts → worker/worker_utils.d.ts} +3 -3
  87. package/dist/worker/worker_utils.d.ts.map +1 -0
  88. package/dist/{worker.js → worker/worker_utils.js} +5 -5
  89. package/package.json +5 -5
  90. package/src/base.ts +38 -2
  91. package/src/constants.ts +38 -16
  92. package/src/{encryption.ts → encryption/encryption.ts} +1 -193
  93. package/src/encryption/encryption_utils.ts +179 -0
  94. package/src/encryption/index.ts +3 -0
  95. package/src/encryption/types.ts +15 -0
  96. package/src/error.ts +11 -17
  97. package/src/{escrow.ts → escrow/escrow_client.ts} +151 -659
  98. package/src/escrow/escrow_utils.ts +510 -0
  99. package/src/escrow/index.ts +2 -0
  100. package/src/graphql/queries/statistics.ts +37 -1
  101. package/src/graphql/types.ts +4 -1
  102. package/src/index.ts +11 -25
  103. package/src/interfaces.ts +2 -7
  104. package/src/kvstore/index.ts +2 -0
  105. package/src/kvstore/kvstore_client.ts +291 -0
  106. package/src/kvstore/kvstore_utils.ts +244 -0
  107. package/src/operator/index.ts +1 -0
  108. package/src/{operator.ts → operator/operator_utils.ts} +8 -8
  109. package/src/staking/index.ts +2 -0
  110. package/src/{staking.ts → staking/staking_client.ts} +49 -199
  111. package/src/staking/staking_utils.ts +170 -0
  112. package/src/statistics/index.ts +1 -0
  113. package/src/{statistics.ts → statistics/statistics_utils.ts} +14 -26
  114. package/src/transaction/index.ts +1 -0
  115. package/src/{transaction.ts → transaction/transaction_utils.ts} +11 -11
  116. package/src/types.ts +24 -1
  117. package/src/utils.ts +82 -9
  118. package/src/worker/index.ts +1 -0
  119. package/src/{worker.ts → worker/worker_utils.ts} +7 -7
  120. package/dist/encryption.d.ts +0 -189
  121. package/dist/encryption.d.ts.map +0 -1
  122. package/dist/escrow.d.ts.map +0 -1
  123. package/dist/kvstore.d.ts.map +0 -1
  124. package/dist/operator.d.ts.map +0 -1
  125. package/dist/staking.d.ts.map +0 -1
  126. package/dist/statistics.d.ts.map +0 -1
  127. package/dist/transaction.d.ts.map +0 -1
  128. package/dist/worker.d.ts.map +0 -1
  129. package/src/kvstore.ts +0 -497
@@ -1,21 +1,5 @@
1
1
  import * as openpgp from 'openpgp';
2
- import { IKeyPair } from './interfaces';
3
-
4
- /**
5
- * Type representing the data type of a message.
6
- * It can be either a string or a Uint8Array.
7
- *
8
- * @public
9
- */
10
- export type MessageDataType = string | Uint8Array;
11
-
12
- function makeMessageDataBinary(message: MessageDataType): Uint8Array {
13
- if (typeof message === 'string') {
14
- return Buffer.from(message);
15
- }
16
-
17
- return message;
18
- }
2
+ import { makeMessageDataBinary, MessageDataType } from './types';
19
3
 
20
4
  /**
21
5
  * Class for signing and decrypting messages.
@@ -191,179 +175,3 @@ export class Encryption {
191
175
  return cleartextMessage;
192
176
  }
193
177
  }
194
-
195
- /**
196
- * Utility class for encryption-related operations.
197
- */
198
- export class EncryptionUtils {
199
- /**
200
- * This function verifies the signature of a signed message using the public key.
201
- *
202
- * @param message - Message to verify.
203
- * @param publicKey - Public key to verify that the message was signed by a specific source.
204
- * @returns True if verified. False if not verified.
205
- *
206
- * @example
207
- * ```ts
208
- * import { EncryptionUtils } from '@human-protocol/sdk';
209
- *
210
- * const publicKey = '-----BEGIN PGP PUBLIC KEY BLOCK-----...';
211
- * const result = await EncryptionUtils.verify('message', publicKey);
212
- * console.log('Verification result:', result);
213
- * ```
214
- */
215
- public static async verify(
216
- message: string,
217
- publicKey: string
218
- ): Promise<boolean> {
219
- const pgpPublicKey = await openpgp.readKey({ armoredKey: publicKey });
220
- const signedMessage = await openpgp.readCleartextMessage({
221
- cleartextMessage: message,
222
- });
223
-
224
- const verificationResult = await signedMessage.verify([pgpPublicKey]);
225
- const { verified } = verificationResult[0];
226
-
227
- try {
228
- return await verified;
229
- } catch {
230
- return false;
231
- }
232
- }
233
-
234
- /**
235
- * This function gets signed data from a signed message.
236
- *
237
- * @param message - Message.
238
- * @returns Signed data.
239
- * @throws Error If data could not be extracted from the message
240
- *
241
- * @example
242
- * ```ts
243
- * import { EncryptionUtils } from '@human-protocol/sdk';
244
- *
245
- * const signedData = await EncryptionUtils.getSignedData('message');
246
- * console.log('Signed data:', signedData);
247
- * ```
248
- */
249
- public static async getSignedData(message: string): Promise<string> {
250
- const signedMessage = await openpgp.readCleartextMessage({
251
- cleartextMessage: message,
252
- });
253
-
254
- try {
255
- return signedMessage.getText();
256
- } catch (e) {
257
- throw new Error('Could not get data: ' + e.message);
258
- }
259
- }
260
-
261
- /**
262
- * This function generates a key pair for encryption and decryption.
263
- *
264
- * @param name - Name for the key pair.
265
- * @param email - Email for the key pair.
266
- * @param passphrase - Passphrase to encrypt the private key (optional, defaults to empty string).
267
- * @returns Key pair generated.
268
- *
269
- * @example
270
- * ```ts
271
- * import { EncryptionUtils } from '@human-protocol/sdk';
272
- *
273
- * const name = 'YOUR_NAME';
274
- * const email = 'YOUR_EMAIL';
275
- * const passphrase = 'YOUR_PASSPHRASE';
276
- * const keyPair = await EncryptionUtils.generateKeyPair(name, email, passphrase);
277
- * console.log('Public key:', keyPair.publicKey);
278
- * ```
279
- */
280
- public static async generateKeyPair(
281
- name: string,
282
- email: string,
283
- passphrase = ''
284
- ): Promise<IKeyPair> {
285
- const { privateKey, publicKey, revocationCertificate } =
286
- await openpgp.generateKey({
287
- type: 'ecc',
288
- curve: 'ed25519Legacy',
289
- userIDs: [{ name: name, email: email }],
290
- passphrase: passphrase,
291
- format: 'armored',
292
- });
293
-
294
- return {
295
- passphrase: passphrase,
296
- privateKey,
297
- publicKey,
298
- revocationCertificate,
299
- };
300
- }
301
-
302
- /**
303
- * This function encrypts a message using the specified public keys.
304
- *
305
- * @param message - Message to encrypt.
306
- * @param publicKeys - Array of public keys to use for encryption.
307
- * @returns Message encrypted.
308
- *
309
- * @example
310
- * ```ts
311
- * import { EncryptionUtils } from '@human-protocol/sdk';
312
- *
313
- * const publicKey1 = '-----BEGIN PGP PUBLIC KEY BLOCK-----...';
314
- * const publicKey2 = '-----BEGIN PGP PUBLIC KEY BLOCK-----...';
315
- * const publicKeys = [publicKey1, publicKey2];
316
- * const encryptedMessage = await EncryptionUtils.encrypt('message', publicKeys);
317
- * console.log('Encrypted message:', encryptedMessage);
318
- * ```
319
- */
320
- public static async encrypt(
321
- message: MessageDataType,
322
- publicKeys: string[]
323
- ): Promise<string> {
324
- const pgpPublicKeys = await Promise.all(
325
- publicKeys.map((armoredKey) => openpgp.readKey({ armoredKey }))
326
- );
327
-
328
- const pgpMessage = await openpgp.createMessage({
329
- binary: makeMessageDataBinary(message),
330
- });
331
- const encrypted = await openpgp.encrypt({
332
- message: pgpMessage,
333
- encryptionKeys: pgpPublicKeys,
334
- format: 'armored',
335
- });
336
-
337
- return encrypted as string;
338
- }
339
-
340
- /**
341
- * Verifies if a message appears to be encrypted with OpenPGP.
342
- *
343
- * @param message - Message to verify.
344
- * @returns `true` if the message appears to be encrypted, `false` if not.
345
- *
346
- * @example
347
- * ```ts
348
- * import { EncryptionUtils } from '@human-protocol/sdk';
349
- *
350
- * const message = '-----BEGIN PGP MESSAGE-----...';
351
- * const isEncrypted = EncryptionUtils.isEncrypted(message);
352
- *
353
- * if (isEncrypted) {
354
- * console.log('The message is encrypted with OpenPGP.');
355
- * } else {
356
- * console.log('The message is not encrypted with OpenPGP.');
357
- * }
358
- * ```
359
- */
360
- public static isEncrypted(message: string): boolean {
361
- const startMarker = '-----BEGIN PGP MESSAGE-----';
362
- const endMarker = '-----END PGP MESSAGE-----';
363
-
364
- const hasStartMarker = message.includes(startMarker);
365
- const hasEndMarker = message.includes(endMarker);
366
-
367
- return hasStartMarker && hasEndMarker;
368
- }
369
- }
@@ -0,0 +1,179 @@
1
+ import * as openpgp from 'openpgp';
2
+ import { IKeyPair } from '../interfaces';
3
+ import { makeMessageDataBinary, MessageDataType } from './types';
4
+
5
+ /**
6
+ * Utility class for encryption-related operations.
7
+ */
8
+ export class EncryptionUtils {
9
+ /**
10
+ * This function verifies the signature of a signed message using the public key.
11
+ *
12
+ * @param message - Message to verify.
13
+ * @param publicKey - Public key to verify that the message was signed by a specific source.
14
+ * @returns True if verified. False if not verified.
15
+ *
16
+ * @example
17
+ * ```ts
18
+ * import { EncryptionUtils } from '@human-protocol/sdk';
19
+ *
20
+ * const publicKey = '-----BEGIN PGP PUBLIC KEY BLOCK-----...';
21
+ * const result = await EncryptionUtils.verify('message', publicKey);
22
+ * console.log('Verification result:', result);
23
+ * ```
24
+ */
25
+ public static async verify(
26
+ message: string,
27
+ publicKey: string
28
+ ): Promise<boolean> {
29
+ const pgpPublicKey = await openpgp.readKey({ armoredKey: publicKey });
30
+ const signedMessage = await openpgp.readCleartextMessage({
31
+ cleartextMessage: message,
32
+ });
33
+
34
+ const verificationResult = await signedMessage.verify([pgpPublicKey]);
35
+ const { verified } = verificationResult[0];
36
+
37
+ try {
38
+ return await verified;
39
+ } catch {
40
+ return false;
41
+ }
42
+ }
43
+
44
+ /**
45
+ * This function gets signed data from a signed message.
46
+ *
47
+ * @param message - Message.
48
+ * @returns Signed data.
49
+ * @throws Error If data could not be extracted from the message
50
+ *
51
+ * @example
52
+ * ```ts
53
+ * import { EncryptionUtils } from '@human-protocol/sdk';
54
+ *
55
+ * const signedData = await EncryptionUtils.getSignedData('message');
56
+ * console.log('Signed data:', signedData);
57
+ * ```
58
+ */
59
+ public static async getSignedData(message: string): Promise<string> {
60
+ const signedMessage = await openpgp.readCleartextMessage({
61
+ cleartextMessage: message,
62
+ });
63
+
64
+ try {
65
+ return signedMessage.getText();
66
+ } catch (e) {
67
+ throw new Error('Could not get data: ' + e.message);
68
+ }
69
+ }
70
+
71
+ /**
72
+ * This function generates a key pair for encryption and decryption.
73
+ *
74
+ * @param name - Name for the key pair.
75
+ * @param email - Email for the key pair.
76
+ * @param passphrase - Passphrase to encrypt the private key (optional, defaults to empty string).
77
+ * @returns Key pair generated.
78
+ *
79
+ * @example
80
+ * ```ts
81
+ * import { EncryptionUtils } from '@human-protocol/sdk';
82
+ *
83
+ * const name = 'YOUR_NAME';
84
+ * const email = 'YOUR_EMAIL';
85
+ * const passphrase = 'YOUR_PASSPHRASE';
86
+ * const keyPair = await EncryptionUtils.generateKeyPair(name, email, passphrase);
87
+ * console.log('Public key:', keyPair.publicKey);
88
+ * ```
89
+ */
90
+ public static async generateKeyPair(
91
+ name: string,
92
+ email: string,
93
+ passphrase = ''
94
+ ): Promise<IKeyPair> {
95
+ const { privateKey, publicKey, revocationCertificate } =
96
+ await openpgp.generateKey({
97
+ type: 'ecc',
98
+ curve: 'ed25519Legacy',
99
+ userIDs: [{ name: name, email: email }],
100
+ passphrase: passphrase,
101
+ format: 'armored',
102
+ });
103
+
104
+ return {
105
+ passphrase: passphrase,
106
+ privateKey,
107
+ publicKey,
108
+ revocationCertificate,
109
+ };
110
+ }
111
+
112
+ /**
113
+ * This function encrypts a message using the specified public keys.
114
+ *
115
+ * @param message - Message to encrypt.
116
+ * @param publicKeys - Array of public keys to use for encryption.
117
+ * @returns Message encrypted.
118
+ *
119
+ * @example
120
+ * ```ts
121
+ * import { EncryptionUtils } from '@human-protocol/sdk';
122
+ *
123
+ * const publicKey1 = '-----BEGIN PGP PUBLIC KEY BLOCK-----...';
124
+ * const publicKey2 = '-----BEGIN PGP PUBLIC KEY BLOCK-----...';
125
+ * const publicKeys = [publicKey1, publicKey2];
126
+ * const encryptedMessage = await EncryptionUtils.encrypt('message', publicKeys);
127
+ * console.log('Encrypted message:', encryptedMessage);
128
+ * ```
129
+ */
130
+ public static async encrypt(
131
+ message: MessageDataType,
132
+ publicKeys: string[]
133
+ ): Promise<string> {
134
+ const pgpPublicKeys = await Promise.all(
135
+ publicKeys.map((armoredKey) => openpgp.readKey({ armoredKey }))
136
+ );
137
+
138
+ const pgpMessage = await openpgp.createMessage({
139
+ binary: makeMessageDataBinary(message),
140
+ });
141
+ const encrypted = await openpgp.encrypt({
142
+ message: pgpMessage,
143
+ encryptionKeys: pgpPublicKeys,
144
+ format: 'armored',
145
+ });
146
+
147
+ return encrypted as string;
148
+ }
149
+
150
+ /**
151
+ * Verifies if a message appears to be encrypted with OpenPGP.
152
+ *
153
+ * @param message - Message to verify.
154
+ * @returns `true` if the message appears to be encrypted, `false` if not.
155
+ *
156
+ * @example
157
+ * ```ts
158
+ * import { EncryptionUtils } from '@human-protocol/sdk';
159
+ *
160
+ * const message = '-----BEGIN PGP MESSAGE-----...';
161
+ * const isEncrypted = EncryptionUtils.isEncrypted(message);
162
+ *
163
+ * if (isEncrypted) {
164
+ * console.log('The message is encrypted with OpenPGP.');
165
+ * } else {
166
+ * console.log('The message is not encrypted with OpenPGP.');
167
+ * }
168
+ * ```
169
+ */
170
+ public static isEncrypted(message: string): boolean {
171
+ const startMarker = '-----BEGIN PGP MESSAGE-----';
172
+ const endMarker = '-----END PGP MESSAGE-----';
173
+
174
+ const hasStartMarker = message.includes(startMarker);
175
+ const hasEndMarker = message.includes(endMarker);
176
+
177
+ return hasStartMarker && hasEndMarker;
178
+ }
179
+ }
@@ -0,0 +1,3 @@
1
+ export { Encryption } from './encryption';
2
+ export { EncryptionUtils } from './encryption_utils';
3
+ export type { MessageDataType } from './types';
@@ -0,0 +1,15 @@
1
+ /**
2
+ * Type representing the data type of a message.
3
+ * It can be either a string or a Uint8Array.
4
+ *
5
+ * @public
6
+ */
7
+ export type MessageDataType = string | Uint8Array;
8
+
9
+ export function makeMessageDataBinary(message: MessageDataType): Uint8Array {
10
+ if (typeof message === 'string') {
11
+ return Buffer.from(message);
12
+ }
13
+
14
+ return message;
15
+ }
package/src/error.ts CHANGED
@@ -180,20 +180,6 @@ export const ErrorInvalidManifest = new Error('Invalid manifest');
180
180
  */
181
181
  export const ErrorNoURLprovided = new Error('No URL provided');
182
182
 
183
- /**
184
- * @constant {Error} - Fee must be between 0 and 100.
185
- */
186
- export const ErrorFeeMustBeBetweenZeroAndHundred = new Error(
187
- 'Fee must be between 0 and 100'
188
- );
189
-
190
- /**
191
- * @constant {Error} - Total fee must be less than 100.
192
- */
193
- export const ErrorTotalFeeMustBeLessThanHundred = new Error(
194
- 'Total fee must be less than 100'
195
- );
196
-
197
183
  /**
198
184
  * @constant {Error} - Recipient cannot be an empty array.
199
185
  */
@@ -344,8 +330,16 @@ export class InvalidEthereumAddressError extends Error {
344
330
  }
345
331
  }
346
332
 
347
- export class InvalidKeyError extends Error {
348
- constructor(key: string, address: string) {
349
- super(`Key "${key}" not found for address ${address}`);
333
+ export class SubgraphRequestError extends Error {
334
+ public readonly statusCode?: number;
335
+ public readonly url: string;
336
+
337
+ constructor(message: string, url: string, statusCode?: number) {
338
+ super(message);
339
+ this.name = this.constructor.name;
340
+ this.url = url;
341
+ this.statusCode = statusCode;
350
342
  }
351
343
  }
344
+
345
+ export class SubgraphBadIndexerError extends SubgraphRequestError {}