@human-protocol/sdk 1.1.13 → 1.1.15

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/src/encryption.ts CHANGED
@@ -1,7 +1,46 @@
1
1
  import * as openpgp from 'openpgp';
2
2
  import { IKeyPair } from './interfaces';
3
+
3
4
  /**
4
- * Class for encryption and decryption operations.
5
+ * ## Introduction
6
+ *
7
+ * Class for sign and decrypt messages.
8
+ *
9
+ * The algorithm includes the implementation of the [PGP encryption algorithm](https://github.com/openpgpjs/openpgpjs) multi-public key encryption on typescript.
10
+ * Using the vanilla [ed25519](https://en.wikipedia.org/wiki/EdDSA#Ed25519) implementation Schnorr signatures for signature and [curve25519](https://en.wikipedia.org/wiki/Curve25519) for encryption. [Learn more](https://wiki.polkadot.network/docs/learn-cryptography).
11
+ *
12
+ * To get an instance of this class, is recommended to initialize it using the static `build` method.
13
+ *
14
+ * ```ts
15
+ * static async build(privateKeyArmored: string, passphrase?: string): Promise<Encryption>
16
+ * ```
17
+ *
18
+ * ## Installation
19
+ *
20
+ * ### npm
21
+ * ```bash
22
+ * npm install @human-protocol/sdk
23
+ * ```
24
+ *
25
+ * ### yarn
26
+ * ```bash
27
+ * yarn install @human-protocol/sdk
28
+ * ```
29
+ *
30
+ * ## Input parameters
31
+ *
32
+ * - `privateKeyArmored` - The encrypted private key in armored format.
33
+ * - `passphrase` - The passphrase for the private key.
34
+ *
35
+ * ## Code example
36
+ *
37
+ * ```ts
38
+ * import { Encryption } from '@human-protocol/sdk';
39
+ *
40
+ * const privateKey = 'Armored_priv_key';
41
+ * const passphrase = 'example_passphrase';
42
+ * const encription = await Encryption.build(privateKey, passphrase);
43
+ * ```
5
44
  */
6
45
  export class Encryption {
7
46
  private privateKey: openpgp.PrivateKey;
@@ -44,11 +83,48 @@ export class Encryption {
44
83
  }
45
84
 
46
85
  /**
47
- * Signs and encrypts a message using the specified public keys.
86
+ * This function signs and encrypts a message using the private key used to initialize the client and the specified public keys.
87
+ *
88
+ * @param {string} message Message to sign and encrypt.
89
+ * @param {string[]} publicKeys Array of public keys to use for encryption.
90
+ * @returns {Promise<string>} Message signed and encrypted.
91
+ *
92
+ * **Code example**
93
+ *
94
+ * ```ts
95
+ * import { Encryption } from '@human-protocol/sdk';
96
+ * import { EscrowClient } from '@human-protocol/sdk';
48
97
  *
49
- * @param {string} message - The message to encrypt.
50
- * @param {string[]} publicKeys - The public keys in armored format.
51
- * @returns {Promise<string>} - The encrypted message.
98
+ * const privateKey = 'Armored_priv_key';
99
+ * const passphrase = 'example_passphrase';
100
+ * const encription = await Encryption.build(privateKey, passphrase);
101
+ * const publicKey1 = `-----BEGIN PGP PUBLIC KEY BLOCK-----
102
+ * xjMEZKQEMxYJKwYBBAHaRw8BAQdA5oZTq4UPlS0IXn4kEaSqQdAa9+Cq522v
103
+ * WYxJQn3vo1/NFEh1bWFuIDxodW1hbkBobXQuYWk+wowEEBYKAD4FAmSkBDME
104
+ * CwkHCAkQJBFPuuhtQo4DFQgKBBYAAgECGQECGwMCHgEWIQTQ5fbVPB9CWIdf
105
+ * XdYkEU+66G1CjgAAKYYA/jMyDCtJtqu6hj22kq9SW6fuV1FCT2ySJ9vBhumF
106
+ * X8wWAP433zVFl4VECOkgGk8qFr8BgkYxaz16GOFAqYbfO6oMBc44BGSkBDMS
107
+ * CisGAQQBl1UBBQEBB0AKR+A48zVVYZWQvgu7Opn2IGvzI9jePB/J8pzqRhg2
108
+ * YAMBCAfCeAQYFggAKgUCZKQEMwkQJBFPuuhtQo4CGwwWIQTQ5fbVPB9CWIdf
109
+ * XdYkEU+66G1CjgAA0xgBAK4AIahFFnmWR2Mp6A3q021cZXpGklc0Xw1Hfswc
110
+ * UYLqAQDfdym4kiUvKO1+REKASt0Gwykndl7hra9txqlUL5DXBQ===Vwgv
111
+ * -----END PGP PUBLIC KEY BLOCK-----`;
112
+ *
113
+ * const publicKey2 = `-----BEGIN PGP PUBLIC KEY BLOCK-----
114
+ * xjMEZKQEMxYJKwYBBAHaRw8BAQdAG6h+E+6T/RV2tIHer3FP/jKThAyGcoVx
115
+ * FzhnP0hncPzNFEh1bWFuIDxodW1hbkBobXQuYWk+wowEEBYKAD4FAmSkBDME
116
+ * CwkHCAkQPIq5xLhlTYkDFQgKBBYAAgECGQECGwMCHgEWIQTcxtMgul/AeUvH
117
+ * bio8irnEuGVNiQAA/HsBANpfFkxNYixpsBk8LlaaCaPy5f1/cWNPgODM9uzo
118
+ * ciSTAQDtAYynu4dSJO9GbMuDuc0FaUHRWJK3mS6JkvedYL4oBM44BGSkBDMS
119
+ * CisGAQQBl1UBBQEBB0DWbEG7DMhkeSc8ZPzrH8XNSCqS3t9y/oQidFR+xN3Z
120
+ * bAMBCAfCeAQYFggAKgUCZKQEMwkQPIq5xLhlTYkCGwwWIQTcxtMgul/AeUvH
121
+ * bio8irnEuGVNiQAAqt8BAM/4Lw0RVOb0L5Ki9CyxO/6AKvRg4ra3Q3WR+duP
122
+ * s/88AQCDErzvn+SOX4s3gvZcM3Vr4wh4Q2syHV8Okgx8STYPDg===DsVk
123
+ * -----END PGP PUBLIC KEY BLOCK-----`;
124
+ *
125
+ * const publicKeys = [publicKey1, publicKey2];
126
+ * const resultMessage = await encription.signAndEncrypt('message', publicKeys);
127
+ * ```
52
128
  */
53
129
  public async signAndEncrypt(
54
130
  message: string,
@@ -71,11 +147,35 @@ export class Encryption {
71
147
  }
72
148
 
73
149
  /**
74
- * Decrypts an encrypted message using the private key.
150
+ * This function decrypt message message using the private key. In addition, the public key can be added for signature verification.
151
+ *
152
+ * @param {string} message Message to decrypt.
153
+ * @param {string} publicKey Public key used to verify signature if needed. Optional.
154
+ * @returns {Promise<string>} Message decrypted.
155
+ *
156
+ * **Code example**
75
157
  *
76
- * @param {string} message - The encrypted message.
77
- * @param {string} publicKey - Optional: The public key in armored format for signature verification.
78
- * @returns {Promise<string>} - The decrypted message.
158
+ * ```ts
159
+ * import { Encryption } from '@human-protocol/sdk';
160
+ *
161
+ * const privateKey = 'Armored_priv_key';
162
+ * const passphrase = 'example_passphrase';
163
+ * const encription = await Encryption.build(privateKey, passphrase);
164
+ *
165
+ * const publicKey = `-----BEGIN PGP PUBLIC KEY BLOCK-----
166
+ * xjMEZKQEMxYJKwYBBAHaRw8BAQdA5oZTq4UPlS0IXn4kEaSqQdAa9+Cq522v
167
+ * WYxJQn3vo1/NFEh1bWFuIDxodW1hbkBobXQuYWk+wowEEBYKAD4FAmSkBDME
168
+ * CwkHCAkQJBFPuuhtQo4DFQgKBBYAAgECGQECGwMCHgEWIQTQ5fbVPB9CWIdf
169
+ * XdYkEU+66G1CjgAAKYYA/jMyDCtJtqu6hj22kq9SW6fuV1FCT2ySJ9vBhumF
170
+ * X8wWAP433zVFl4VECOkgGk8qFr8BgkYxaz16GOFAqYbfO6oMBc44BGSkBDMS
171
+ * CisGAQQBl1UBBQEBB0AKR+A48zVVYZWQvgu7Opn2IGvzI9jePB/J8pzqRhg2
172
+ * YAMBCAfCeAQYFggAKgUCZKQEMwkQJBFPuuhtQo4CGwwWIQTQ5fbVPB9CWIdf
173
+ * XdYkEU+66G1CjgAA0xgBAK4AIahFFnmWR2Mp6A3q021cZXpGklc0Xw1Hfswc
174
+ * UYLqAQDfdym4kiUvKO1+REKASt0Gwykndl7hra9txqlUL5DXBQ===Vwgv
175
+ * -----END PGP PUBLIC KEY BLOCK-----`;
176
+ *
177
+ * const resultMessage = await encription.decrypt('message');
178
+ * ```
79
179
  */
80
180
  public async decrypt(message: string, publicKey?: string): Promise<string> {
81
181
  const pgpMessage = await openpgp.readMessage({ armoredMessage: message });
@@ -97,10 +197,22 @@ export class Encryption {
97
197
  }
98
198
 
99
199
  /**
100
- * Signs a message using the private key.
200
+ * This function signs a message using the private key used to initialize the client.
201
+ *
202
+ * @param {string} message Message to sign.
203
+ * @returns {Promise<string>} Message signed.
101
204
  *
102
- * @param {string} message - The message to sign.
103
- * @returns {Promise<string>} - The signed message.
205
+ * **Code example**
206
+ *
207
+ * ```ts
208
+ * import { Encryption } from '@human-protocol/sdk';
209
+ *
210
+ * const privateKey = 'Armored_priv_key';
211
+ * const passphrase = 'example_passphrase';
212
+ * const encription = await Encryption.build(privateKey, passphrase);
213
+ *
214
+ * const resultMessage = await encription.sign('message');
215
+ * ```
104
216
  */
105
217
  public async sign(message: string): Promise<string> {
106
218
  const unsignedMessage = await openpgp.createCleartextMessage({
@@ -117,15 +229,57 @@ export class Encryption {
117
229
  }
118
230
 
119
231
  /**
232
+ * ## Introduction
233
+ *
120
234
  * Utility class for encryption-related operations.
235
+ *
236
+ * ## Installation
237
+ *
238
+ * ### npm
239
+ * ```bash
240
+ * npm install @human-protocol/sdk
241
+ * ```
242
+ *
243
+ * ### yarn
244
+ * ```bash
245
+ * yarn install @human-protocol/sdk
246
+ * ```
247
+ *
248
+ * ## Code example
249
+ *
250
+ * ```ts
251
+ * import { EncryptionUtils } from '@human-protocol/sdk';
252
+ *
253
+ * const keyPair = await EncryptionUtils.generateKeyPair('Human', 'human@hmt.ai');
254
+ * ```
121
255
  */
122
256
  export class EncryptionUtils {
123
257
  /**
124
- * Verifies the signature of a signed message using the public key.
258
+ * This function verifies the signature of a signed message using the public key.
259
+ *
260
+ * @param {string} message Message to verify.
261
+ * @param {string} publicKey Public key to verify that the message was sign by a specific source.
262
+ * @returns {Promise<boolean>} True if verified. False if not verified.
125
263
  *
126
- * @param {string} message - The signed message.
127
- * @param {string} publicKey - The public key in armored format.
128
- * @returns {Promise<boolean>} - A boolean indicating if the signature is valid.
264
+ * **Code example**
265
+ *
266
+ * ```ts
267
+ * import { EncryptionUtils } from '@human-protocol/sdk';
268
+ *
269
+ * const publicKey = `-----BEGIN PGP PUBLIC KEY BLOCK-----
270
+ * xjMEZKQEMxYJKwYBBAHaRw8BAQdA5oZTq4UPlS0IXn4kEaSqQdAa9+Cq522v
271
+ * WYxJQn3vo1/NFEh1bWFuIDxodW1hbkBobXQuYWk+wowEEBYKAD4FAmSkBDME
272
+ * CwkHCAkQJBFPuuhtQo4DFQgKBBYAAgECGQECGwMCHgEWIQTQ5fbVPB9CWIdf
273
+ * XdYkEU+66G1CjgAAKYYA/jMyDCtJtqu6hj22kq9SW6fuV1FCT2ySJ9vBhumF
274
+ * X8wWAP433zVFl4VECOkgGk8qFr8BgkYxaz16GOFAqYbfO6oMBc44BGSkBDMS
275
+ * CisGAQQBl1UBBQEBB0AKR+A48zVVYZWQvgu7Opn2IGvzI9jePB/J8pzqRhg2
276
+ * YAMBCAfCeAQYFggAKgUCZKQEMwkQJBFPuuhtQo4CGwwWIQTQ5fbVPB9CWIdf
277
+ * XdYkEU+66G1CjgAA0xgBAK4AIahFFnmWR2Mp6A3q021cZXpGklc0Xw1Hfswc
278
+ * UYLqAQDfdym4kiUvKO1+REKASt0Gwykndl7hra9txqlUL5DXBQ===Vwgv
279
+ * -----END PGP PUBLIC KEY BLOCK-----`;
280
+ *
281
+ * const result = await EncriptionUtils.verify('message', publicKey);
282
+ * ```
129
283
  */
130
284
  public static async verify(
131
285
  message: string,
@@ -147,11 +301,18 @@ export class EncryptionUtils {
147
301
  }
148
302
 
149
303
  /**
150
- * Gets the signed data from a signed message.
304
+ * This function gets signed data from a signed message.
305
+ *
306
+ * @param {string} message Message.
307
+ * @returns {Promise<string>} Signed data.
151
308
  *
152
- * @param {string} message - The signed message.
153
- * @returns {Promise<string>} - The signed data.
154
- * @throws {Error} - An error object if an error occurred.
309
+ * **Code example**
310
+ *
311
+ * ```ts
312
+ * import { EncryptionUtils } from '@human-protocol/sdk';
313
+ *
314
+ * const signedData = await EncriptionUtils.getSignedData('message');
315
+ * ```
155
316
  */
156
317
  public static async getSignedData(message: string): Promise<string> {
157
318
  const signedMessage = await openpgp.readCleartextMessage({
@@ -166,12 +327,35 @@ export class EncryptionUtils {
166
327
  }
167
328
 
168
329
  /**
169
- * Generates a key pair for encryption and decryption.
330
+ * This function generates a key pair for encryption and decryption.
331
+ *
332
+ * @param {string} name Name for the key pair.
333
+ * @param {string} email Email for the key pair.
334
+ * @param {string} passphrase Passphrase to encrypt the private key. Optional.
335
+ * @returns {Promise<IKeyPair>} Key pair generated.
336
+ *
337
+ * **Code example**
170
338
  *
171
- * @param {string} name - The name for the key pair.
172
- * @param {string} email - The email for the key pair.
173
- * @param {string} passphrase - The passphrase used to encrypt the private key.
174
- * @returns {Promise<IKeyPair>} - The generated key pair.
339
+ * ```ts
340
+ * import { EncryptionUtils } from '@human-protocol/sdk';
341
+ *
342
+ * const publicKey = `-----BEGIN PGP PUBLIC KEY BLOCK-----
343
+ * xjMEZKQEMxYJKwYBBAHaRw8BAQdA5oZTq4UPlS0IXn4kEaSqQdAa9+Cq522v
344
+ * WYxJQn3vo1/NFEh1bWFuIDxodW1hbkBobXQuYWk+wowEEBYKAD4FAmSkBDME
345
+ * CwkHCAkQJBFPuuhtQo4DFQgKBBYAAgECGQECGwMCHgEWIQTQ5fbVPB9CWIdf
346
+ * XdYkEU+66G1CjgAAKYYA/jMyDCtJtqu6hj22kq9SW6fuV1FCT2ySJ9vBhumF
347
+ * X8wWAP433zVFl4VECOkgGk8qFr8BgkYxaz16GOFAqYbfO6oMBc44BGSkBDMS
348
+ * CisGAQQBl1UBBQEBB0AKR+A48zVVYZWQvgu7Opn2IGvzI9jePB/J8pzqRhg2
349
+ * YAMBCAfCeAQYFggAKgUCZKQEMwkQJBFPuuhtQo4CGwwWIQTQ5fbVPB9CWIdf
350
+ * XdYkEU+66G1CjgAA0xgBAK4AIahFFnmWR2Mp6A3q021cZXpGklc0Xw1Hfswc
351
+ * UYLqAQDfdym4kiUvKO1+REKASt0Gwykndl7hra9txqlUL5DXBQ===Vwgv
352
+ * -----END PGP PUBLIC KEY BLOCK-----`;
353
+ *
354
+ * const name = 'YOUR_NAME';
355
+ * const email = 'YOUR_EMAIL';
356
+ * const passphrase = 'YOUR_PASSPHRASE';
357
+ * const result = await EncriptionUtils.generateKeyPair(name, email, passphrase);
358
+ * ```
175
359
  */
176
360
  public static async generateKeyPair(
177
361
  name: string,
@@ -196,11 +380,44 @@ export class EncryptionUtils {
196
380
  }
197
381
 
198
382
  /**
199
- * Encrypts a message using the specified public keys.
383
+ * This function encrypts a message using the specified public keys.
384
+ *
385
+ * @param {string} message Message to encrypt.
386
+ * @param {string} publicKey Array of public keys to use for encryption.
387
+ * @returns {Promise<string>} Message encrypted.
388
+ *
389
+ * **Code example**
390
+ *
391
+ * ```ts
392
+ * import { EncryptionUtils } from '@human-protocol/sdk';
393
+ *
394
+ * const publicKey1 = `-----BEGIN PGP PUBLIC KEY BLOCK-----
395
+ * xjMEZKQEMxYJKwYBBAHaRw8BAQdA5oZTq4UPlS0IXn4kEaSqQdAa9+Cq522v
396
+ * WYxJQn3vo1/NFEh1bWFuIDxodW1hbkBobXQuYWk+wowEEBYKAD4FAmSkBDME
397
+ * CwkHCAkQJBFPuuhtQo4DFQgKBBYAAgECGQECGwMCHgEWIQTQ5fbVPB9CWIdf
398
+ * XdYkEU+66G1CjgAAKYYA/jMyDCtJtqu6hj22kq9SW6fuV1FCT2ySJ9vBhumF
399
+ * X8wWAP433zVFl4VECOkgGk8qFr8BgkYxaz16GOFAqYbfO6oMBc44BGSkBDMS
400
+ * CisGAQQBl1UBBQEBB0AKR+A48zVVYZWQvgu7Opn2IGvzI9jePB/J8pzqRhg2
401
+ * YAMBCAfCeAQYFggAKgUCZKQEMwkQJBFPuuhtQo4CGwwWIQTQ5fbVPB9CWIdf
402
+ * XdYkEU+66G1CjgAA0xgBAK4AIahFFnmWR2Mp6A3q021cZXpGklc0Xw1Hfswc
403
+ * UYLqAQDfdym4kiUvKO1+REKASt0Gwykndl7hra9txqlUL5DXBQ===Vwgv
404
+ * -----END PGP PUBLIC KEY BLOCK-----`;
405
+ *
406
+ * const publicKey2 = `-----BEGIN PGP PUBLIC KEY BLOCK-----
407
+ * xjMEZKQEMxYJKwYBBAHaRw8BAQdAG6h+E+6T/RV2tIHer3FP/jKThAyGcoVx
408
+ * FzhnP0hncPzNFEh1bWFuIDxodW1hbkBobXQuYWk+wowEEBYKAD4FAmSkBDME
409
+ * CwkHCAkQPIq5xLhlTYkDFQgKBBYAAgECGQECGwMCHgEWIQTcxtMgul/AeUvH
410
+ * bio8irnEuGVNiQAA/HsBANpfFkxNYixpsBk8LlaaCaPy5f1/cWNPgODM9uzo
411
+ * ciSTAQDtAYynu4dSJO9GbMuDuc0FaUHRWJK3mS6JkvedYL4oBM44BGSkBDMS
412
+ * CisGAQQBl1UBBQEBB0DWbEG7DMhkeSc8ZPzrH8XNSCqS3t9y/oQidFR+xN3Z
413
+ * bAMBCAfCeAQYFggAKgUCZKQEMwkQPIq5xLhlTYkCGwwWIQTcxtMgul/AeUvH
414
+ * bio8irnEuGVNiQAAqt8BAM/4Lw0RVOb0L5Ki9CyxO/6AKvRg4ra3Q3WR+duP
415
+ * s/88AQCDErzvn+SOX4s3gvZcM3Vr4wh4Q2syHV8Okgx8STYPDg===DsVk
416
+ * -----END PGP PUBLIC KEY BLOCK-----`;
200
417
  *
201
- * @param {string} message - The message to encrypt.
202
- * @param {string[]} publicKeys - The public keys in armored format.
203
- * @returns {Promise<string>} - The encrypted message.
418
+ * const publicKeys = [publicKey1, publicKey2]
419
+ * const result = await EncriptionUtils.encrypt('message', publicKeys);
420
+ * ```
204
421
  */
205
422
  public static async encrypt(
206
423
  message: string,