@human-protocol/sdk 3.0.6 → 3.0.7

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.
@@ -1,5 +1,6 @@
1
1
  import * as openpgp from 'openpgp';
2
2
  import { IKeyPair } from './interfaces';
3
+ type MessageDataType = string | Uint8Array;
3
4
  /**
4
5
  * ## Introduction
5
6
  *
@@ -100,7 +101,7 @@ export declare class Encryption {
100
101
  * const resultMessage = await encription.signAndEncrypt('message', publicKeys);
101
102
  * ```
102
103
  */
103
- signAndEncrypt(message: string, publicKeys: string[]): Promise<string>;
104
+ signAndEncrypt(message: MessageDataType, publicKeys: string[]): Promise<string>;
104
105
  /**
105
106
  * This function decrypts messages using the private key. In addition, the public key can be added for signature verification.
106
107
  *
@@ -132,7 +133,7 @@ export declare class Encryption {
132
133
  * const resultMessage = await encription.decrypt('message');
133
134
  * ```
134
135
  */
135
- decrypt(message: string, publicKey?: string): Promise<string>;
136
+ decrypt(message: string, publicKey?: string): Promise<Uint8Array>;
136
137
  /**
137
138
  * This function signs a message using the private key used to initialize the client.
138
139
  *
@@ -294,7 +295,7 @@ export declare class EncryptionUtils {
294
295
  * const result = await EncriptionUtils.encrypt('message', publicKeys);
295
296
  * ```
296
297
  */
297
- static encrypt(message: string, publicKeys: string[]): Promise<string>;
298
+ static encrypt(message: MessageDataType, publicKeys: string[]): Promise<string>;
298
299
  /**
299
300
  * Verifies if a message appears to be encrypted with OpenPGP.
300
301
  *
@@ -327,4 +328,5 @@ export declare class EncryptionUtils {
327
328
  */
328
329
  static isEncrypted(message: string): boolean;
329
330
  }
331
+ export {};
330
332
  //# sourceMappingURL=encryption.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"encryption.d.ts","sourceRoot":"","sources":["../src/encryption.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,OAAO,MAAM,SAAS,CAAC;AACnC,OAAO,EAAE,QAAQ,EAAE,MAAM,cAAc,CAAC;AAExC;;;;;;;;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AAO,CAAC;IAgBnB;;;;;;;;;;;;;OAaG;WACiB,aAAa,CAAC,OAAO,EAAE,MAAM,GAAG,OAAO,CAAC,MAAM,CAAC;IAYnE;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;OA8BG;WACiB,eAAe,CACjC,IAAI,EAAE,MAAM,EACZ,KAAK,EAAE,MAAM,EACb,UAAU,SAAK,GACd,OAAO,CAAC,QAAQ,CAAC;IAkBpB;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;OAuCG;WACiB,OAAO,CACzB,OAAO,EAAE,eAAe,EACxB,UAAU,EAAE,MAAM,EAAE,GACnB,OAAO,CAAC,MAAM,CAAC;IAiBlB;;;;;;;;;;;;;;;;;;;;;;;;;;;;;OA6BG;WACW,WAAW,CAAC,OAAO,EAAE,MAAM,GAAG,OAAO;CASpD"}
@@ -25,6 +25,12 @@ var __importStar = (this && this.__importStar) || function (mod) {
25
25
  Object.defineProperty(exports, "__esModule", { value: true });
26
26
  exports.EncryptionUtils = exports.Encryption = void 0;
27
27
  const openpgp = __importStar(require("openpgp"));
28
+ function makeMessageDataBinary(message) {
29
+ if (typeof message === 'string') {
30
+ return Buffer.from(message);
31
+ }
32
+ return message;
33
+ }
28
34
  /**
29
35
  * ## Introduction
30
36
  *
@@ -139,13 +145,15 @@ class Encryption {
139
145
  * ```
140
146
  */
141
147
  async signAndEncrypt(message, publicKeys) {
142
- const plaintext = message;
143
148
  const pgpPublicKeys = await Promise.all(publicKeys.map((armoredKey) => openpgp.readKey({ armoredKey })));
144
- const pgpMessage = await openpgp.createMessage({ text: plaintext });
149
+ const pgpMessage = await openpgp.createMessage({
150
+ binary: makeMessageDataBinary(message),
151
+ });
145
152
  const encrypted = await openpgp.encrypt({
146
153
  message: pgpMessage,
147
154
  encryptionKeys: pgpPublicKeys,
148
155
  signingKeys: this.privateKey,
156
+ format: 'armored',
149
157
  });
150
158
  return encrypted;
151
159
  }
@@ -181,17 +189,33 @@ class Encryption {
181
189
  * ```
182
190
  */
183
191
  async decrypt(message, publicKey) {
184
- const pgpMessage = await openpgp.readMessage({ armoredMessage: message });
192
+ const pgpMessage = await openpgp.readMessage({
193
+ armoredMessage: message,
194
+ });
185
195
  const decryptionOptions = {
186
196
  message: pgpMessage,
187
197
  decryptionKeys: this.privateKey,
188
- expectSigned: !!publicKey,
198
+ format: 'binary',
189
199
  };
190
- if (publicKey) {
200
+ const shouldVerifySignature = !!publicKey;
201
+ if (shouldVerifySignature) {
191
202
  const pgpPublicKey = await openpgp.readKey({ armoredKey: publicKey });
192
203
  decryptionOptions.verificationKeys = pgpPublicKey;
193
204
  }
194
- const { data: decrypted } = await openpgp.decrypt(decryptionOptions);
205
+ const { data: decrypted, signatures } = await openpgp.decrypt(decryptionOptions);
206
+ /**
207
+ * There is an option to automatically verify signatures - `expectSigned`,
208
+ * but atm it has a bug - https://github.com/openpgpjs/openpgpjs/issues/1803,
209
+ * so we have to verify it manually till it's fixed.
210
+ */
211
+ try {
212
+ if (shouldVerifySignature) {
213
+ await signatures[0].verified;
214
+ }
215
+ }
216
+ catch {
217
+ throw new Error('Signature could not be verified');
218
+ }
195
219
  return decrypted;
196
220
  }
197
221
  /**
@@ -404,12 +428,14 @@ class EncryptionUtils {
404
428
  * ```
405
429
  */
406
430
  static async encrypt(message, publicKeys) {
407
- const plaintext = message;
408
431
  const pgpPublicKeys = await Promise.all(publicKeys.map((armoredKey) => openpgp.readKey({ armoredKey })));
409
- const pgpMessage = await openpgp.createMessage({ text: plaintext });
432
+ const pgpMessage = await openpgp.createMessage({
433
+ binary: makeMessageDataBinary(message),
434
+ });
410
435
  const encrypted = await openpgp.encrypt({
411
436
  message: pgpMessage,
412
437
  encryptionKeys: pgpPublicKeys,
438
+ format: 'armored',
413
439
  });
414
440
  return encrypted;
415
441
  }
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@human-protocol/sdk",
3
3
  "description": "Human Protocol SDK",
4
- "version": "3.0.6",
4
+ "version": "3.0.7",
5
5
  "files": [
6
6
  "src",
7
7
  "dist"
package/src/encryption.ts CHANGED
@@ -1,6 +1,16 @@
1
1
  import * as openpgp from 'openpgp';
2
2
  import { IKeyPair } from './interfaces';
3
3
 
4
+ type MessageDataType = string | Uint8Array;
5
+
6
+ function makeMessageDataBinary(message: MessageDataType): Uint8Array {
7
+ if (typeof message === 'string') {
8
+ return Buffer.from(message);
9
+ }
10
+
11
+ return message;
12
+ }
13
+
4
14
  /**
5
15
  * ## Introduction
6
16
  *
@@ -126,20 +136,21 @@ export class Encryption {
126
136
  * ```
127
137
  */
128
138
  public async signAndEncrypt(
129
- message: string,
139
+ message: MessageDataType,
130
140
  publicKeys: string[]
131
141
  ): Promise<string> {
132
- const plaintext = message;
133
-
134
142
  const pgpPublicKeys = await Promise.all(
135
143
  publicKeys.map((armoredKey) => openpgp.readKey({ armoredKey }))
136
144
  );
137
145
 
138
- const pgpMessage = await openpgp.createMessage({ text: plaintext });
146
+ const pgpMessage = await openpgp.createMessage({
147
+ binary: makeMessageDataBinary(message),
148
+ });
139
149
  const encrypted = await openpgp.encrypt({
140
150
  message: pgpMessage,
141
151
  encryptionKeys: pgpPublicKeys,
142
152
  signingKeys: this.privateKey,
153
+ format: 'armored',
143
154
  });
144
155
 
145
156
  return encrypted as string;
@@ -176,23 +187,43 @@ export class Encryption {
176
187
  * const resultMessage = await encription.decrypt('message');
177
188
  * ```
178
189
  */
179
- public async decrypt(message: string, publicKey?: string): Promise<string> {
180
- const pgpMessage = await openpgp.readMessage({ armoredMessage: message });
190
+ public async decrypt(
191
+ message: string,
192
+ publicKey?: string
193
+ ): Promise<Uint8Array> {
194
+ const pgpMessage = await openpgp.readMessage({
195
+ armoredMessage: message,
196
+ });
181
197
 
182
198
  const decryptionOptions: openpgp.DecryptOptions = {
183
199
  message: pgpMessage,
184
200
  decryptionKeys: this.privateKey,
185
- expectSigned: !!publicKey,
201
+ format: 'binary',
186
202
  };
187
203
 
188
- if (publicKey) {
204
+ const shouldVerifySignature = !!publicKey;
205
+ if (shouldVerifySignature) {
189
206
  const pgpPublicKey = await openpgp.readKey({ armoredKey: publicKey });
190
207
  decryptionOptions.verificationKeys = pgpPublicKey;
191
208
  }
192
209
 
193
- const { data: decrypted } = await openpgp.decrypt(decryptionOptions);
210
+ const { data: decrypted, signatures } =
211
+ await openpgp.decrypt(decryptionOptions);
212
+
213
+ /**
214
+ * There is an option to automatically verify signatures - `expectSigned`,
215
+ * but atm it has a bug - https://github.com/openpgpjs/openpgpjs/issues/1803,
216
+ * so we have to verify it manually till it's fixed.
217
+ */
218
+ try {
219
+ if (shouldVerifySignature) {
220
+ await signatures[0].verified;
221
+ }
222
+ } catch {
223
+ throw new Error('Signature could not be verified');
224
+ }
194
225
 
195
- return decrypted as string;
226
+ return decrypted as Uint8Array;
196
227
  }
197
228
 
198
229
  /**
@@ -419,19 +450,20 @@ export class EncryptionUtils {
419
450
  * ```
420
451
  */
421
452
  public static async encrypt(
422
- message: string,
453
+ message: MessageDataType,
423
454
  publicKeys: string[]
424
455
  ): Promise<string> {
425
- const plaintext = message;
426
-
427
456
  const pgpPublicKeys = await Promise.all(
428
457
  publicKeys.map((armoredKey) => openpgp.readKey({ armoredKey }))
429
458
  );
430
459
 
431
- const pgpMessage = await openpgp.createMessage({ text: plaintext });
460
+ const pgpMessage = await openpgp.createMessage({
461
+ binary: makeMessageDataBinary(message),
462
+ });
432
463
  const encrypted = await openpgp.encrypt({
433
464
  message: pgpMessage,
434
465
  encryptionKeys: pgpPublicKeys,
466
+ format: 'armored',
435
467
  });
436
468
 
437
469
  return encrypted as string;