@substrate-system/keys 0.2.3 → 0.2.6

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 (77) hide show
  1. package/README.md +279 -160
  2. package/dist/_base.d.ts +183 -0
  3. package/dist/_base.d.ts.map +1 -0
  4. package/dist/aes/index.cjs +123 -0
  5. package/dist/aes/index.cjs.map +7 -0
  6. package/dist/aes/index.d.ts +20 -0
  7. package/dist/aes/index.d.ts.map +1 -0
  8. package/dist/aes/index.js +119 -0
  9. package/dist/aes/index.js.map +7 -0
  10. package/dist/aes/index.min.cjs +2 -0
  11. package/dist/aes/index.min.cjs.map +7 -0
  12. package/dist/aes/index.min.js +4 -0
  13. package/dist/aes/index.min.js.map +7 -0
  14. package/dist/constants.d.ts +15 -4
  15. package/dist/constants.d.ts.map +1 -1
  16. package/dist/ecc/index.cjs +412 -0
  17. package/dist/ecc/index.cjs.map +7 -0
  18. package/dist/ecc/index.d.ts +111 -0
  19. package/dist/ecc/index.d.ts.map +1 -0
  20. package/dist/ecc/index.js +414 -0
  21. package/dist/ecc/index.js.map +7 -0
  22. package/dist/ecc/index.min.cjs +2 -0
  23. package/dist/ecc/index.min.cjs.map +7 -0
  24. package/dist/ecc/index.min.js +4 -0
  25. package/dist/ecc/index.min.js.map +7 -0
  26. package/dist/errors.d.ts +27 -0
  27. package/dist/errors.d.ts.map +1 -0
  28. package/dist/index.d.ts +3 -190
  29. package/dist/index.d.ts.map +1 -1
  30. package/dist/meta.json +91 -118
  31. package/dist/rsa/index.cjs +283 -0
  32. package/dist/rsa/index.cjs.map +7 -0
  33. package/dist/rsa/index.d.ts +118 -0
  34. package/dist/rsa/index.d.ts.map +1 -0
  35. package/dist/rsa/index.js +287 -0
  36. package/dist/rsa/index.js.map +7 -0
  37. package/dist/rsa/index.min.cjs +2 -0
  38. package/dist/rsa/index.min.cjs.map +7 -0
  39. package/dist/rsa/index.min.js +4 -0
  40. package/dist/rsa/index.min.js.map +7 -0
  41. package/dist/types.d.ts +20 -2
  42. package/dist/types.d.ts.map +1 -1
  43. package/dist/util.d.ts +15 -14
  44. package/dist/util.d.ts.map +1 -1
  45. package/package.json +38 -12
  46. package/dist/constants.cjs +0 -64
  47. package/dist/constants.cjs.map +0 -7
  48. package/dist/constants.js +0 -44
  49. package/dist/constants.js.map +0 -7
  50. package/dist/constants.min.cjs +0 -2
  51. package/dist/constants.min.cjs.map +0 -7
  52. package/dist/constants.min.js +0 -2
  53. package/dist/constants.min.js.map +0 -7
  54. package/dist/index.cjs +0 -480
  55. package/dist/index.cjs.map +0 -7
  56. package/dist/index.js +0 -493
  57. package/dist/index.js.map +0 -7
  58. package/dist/index.min.cjs +0 -2
  59. package/dist/index.min.cjs.map +0 -7
  60. package/dist/index.min.js +0 -4
  61. package/dist/index.min.js.map +0 -7
  62. package/dist/types.cjs +0 -57
  63. package/dist/types.cjs.map +0 -7
  64. package/dist/types.js +0 -37
  65. package/dist/types.js.map +0 -7
  66. package/dist/types.min.cjs +0 -2
  67. package/dist/types.min.cjs.map +0 -7
  68. package/dist/types.min.js +0 -2
  69. package/dist/types.min.js.map +0 -7
  70. package/dist/util.cjs +0 -375
  71. package/dist/util.cjs.map +0 -7
  72. package/dist/util.js +0 -373
  73. package/dist/util.js.map +0 -7
  74. package/dist/util.min.cjs +0 -4
  75. package/dist/util.min.cjs.map +0 -7
  76. package/dist/util.min.js +0 -4
  77. package/dist/util.min.js.map +0 -7
package/README.md CHANGED
@@ -1,6 +1,6 @@
1
1
  # keys
2
2
  [![tests](https://img.shields.io/github/actions/workflow/status/substrate-system/keys/nodejs.yml?style=flat-square)](https://github.com/substrate-system/keys/actions/workflows/nodejs.yml)
3
- [![types](https://img.shields.io/npm/types/@bicycle-codes/keys?style=flat-square)](README.md)
3
+ [![types](https://img.shields.io/npm/types/@substrate-system/keys?style=flat-square)](README.md)
4
4
  [![module](https://img.shields.io/badge/module-ESM%2FCJS-blue?style=flat-square)](README.md)
5
5
  [![semantic versioning](https://img.shields.io/badge/semver-2.0.0-blue?logo=semver&style=flat-square)](https://semver.org/)
6
6
  [![Common Changelog](https://nichoth.github.io/badge/common-changelog.svg)](./CHANGELOG.md)
@@ -23,8 +23,7 @@ reading the private key, but the keys can be persisted and re-used indefinitely.
23
23
  >
24
24
 
25
25
  Each instance of `Keys` has two keypairs -- one for signing, and another for
26
- encrypting. We are **using RSA keys only** right now, because we are
27
- [waiting for all browsers to support ECC](https://developer.mozilla.org/en-US/docs/Web/API/SubtleCrypto/generateKey#browser_compatibility).
26
+ encrypting.
28
27
 
29
28
  See also, [the API docs generated from typescript](https://bicycle-codes.github.io/keys/).
30
29
 
@@ -32,18 +31,17 @@ See also, [the API docs generated from typescript](https://bicycle-codes.github.
32
31
 
33
32
  <!-- toc -->
34
33
 
35
- - [install](#install)
36
- - [Asymmetric Encryption](#asymmetric-encryption)
34
+ - [Install](#install)
37
35
  - [Modules](#modules)
38
36
  * [`exports`](#exports)
39
37
  * [ESM](#esm)
40
38
  * [Common JS](#common-js)
41
39
  * [pre-built JS](#pre-built-js)
42
- - [get started](#get-started)
43
- * [Create a keypair](#create-a-keypair)
40
+ - [Get started](#get-started)
41
+ * [ECC](#ecc)
44
42
  * [some notes about the `keys` instance](#some-notes-about-the-keys-instance)
45
43
  * [Delete a keypair](#delete-a-keypair)
46
- * [sign and verify something](#sign-and-verify-something)
44
+ * [Sign and Verify Something](#sign-and-verify-something)
47
45
  * [encrypt something](#encrypt-something)
48
46
  * [decrypt something](#decrypt-something)
49
47
  - [examples](#examples)
@@ -58,7 +56,7 @@ See also, [the API docs generated from typescript](https://bicycle-codes.github.
58
56
  * [Asymmetrically encrypt some arbitrary data](#asymmetrically-encrypt-some-arbitrary-data)
59
57
  * [Asymmetrically encrypt a string, return a new string](#asymmetrically-encrypt-a-string-return-a-new-string)
60
58
  * [Decrypt a message](#decrypt-a-message)
61
- * [`.decrypt.asString`](#decryptasstring)
59
+ * [Backward compatibility: `.decrypt.asString`](#backward-compatibility-decryptasstring)
62
60
  * [In memory only](#in-memory-only)
63
61
  - [AES](#aes)
64
62
  * [`create`](#create)
@@ -72,20 +70,12 @@ See also, [the API docs generated from typescript](https://bicycle-codes.github.
72
70
  </details>
73
71
 
74
72
 
75
- ## install
73
+ ## Install
76
74
 
77
75
  ```sh
78
- npm i -S @bicycle-codes/keys
76
+ npm i -S @substrate-system/keys
79
77
  ```
80
78
 
81
- ## Asymmetric Encryption
82
-
83
- Asymmetric encryption means using an AES key to encrypt the content, then
84
- encrypting the AES key to a public RSA key. If you encrypt something to Bob's
85
- public key, that means we create a new buffer of encrypted AES key +
86
- encrypted content, where the encrypted AES key can only be decrypted by Bob's
87
- private key.
88
-
89
79
  ## Modules
90
80
 
91
81
  ### `exports`
@@ -94,12 +84,16 @@ This exposes ESM and common JS via [package.json `exports` field](https://nodejs
94
84
 
95
85
  ### ESM
96
86
  ```js
97
- import { Keys } from '@bicycle-codes/keys'
87
+ import { EccKeys, verify } from '@substrate-system/keys/ecc'
88
+ import { RsaKeys, verify } from '@substrate-system/keys/rsa'
89
+ import { AES } from '@substrate-system/keys/aes'
98
90
  ```
99
91
 
100
92
  ### Common JS
101
93
  ```js
102
- const { Keys } = require('@bicycle-codes/keys')
94
+ const { EccKeys, verify } = require('@substrate-system/keys/ecc')
95
+ const { RsaKeys, verify } = require('@substrate-system/keys/rsa')
96
+ const { AES } = require('@substrate-system/keys/aes')
103
97
  ```
104
98
 
105
99
  ### pre-built JS
@@ -108,7 +102,7 @@ accessible to your web server, then link to them in HTML.
108
102
 
109
103
  #### copy
110
104
  ```sh
111
- cp ./node_modules/@bicycle-codes/keys/dist/index.min.js ./public/keys.min.js
105
+ cp ./node_modules/@substrate-system/keys/dist/index.min.js ./public/keys.min.js
112
106
  ```
113
107
 
114
108
  #### HTML
@@ -119,22 +113,26 @@ cp ./node_modules/@bicycle-codes/keys/dist/index.min.js ./public/keys.min.js
119
113
  ------------------------------------------------------
120
114
 
121
115
 
122
- ## get started
116
+ ## Get started
117
+
118
+ ### ECC
123
119
 
124
- ### Create a keypair
120
+ #### Create a keypair
125
121
  Create a new keypair, then save it in `indexedDB`.
126
122
 
123
+ ECC is now supported in all major browsers.
124
+
127
125
  ```js
128
- import { Keys } from '@bicycle-codes/keys'
126
+ import { EccKeys, verify } from '@substrate-system/keys/ecc'
129
127
 
130
- const keys = await Keys.create()
128
+ const keys = await EccKeys.create()
131
129
 
132
130
  // save the keys to indexedDB
133
131
  await keys.persist()
134
132
 
135
133
  // ... sometime in the future ...
136
134
  // get our keys from indexedDB
137
- const keysAgain = await Keys.load()
135
+ const keysAgain = await EccKeys.load()
138
136
 
139
137
  console.assert(keys.DID === keysAgain.DID) // true
140
138
  ```
@@ -152,7 +150,7 @@ this instance. The DID looks like this:
152
150
 
153
151
  #### `keys.getDeviceName` / `keys.deviceName`
154
152
 
155
- Return the 32 character, DNS friendly hash of the signing public key.
153
+ Return a 32 character, DNS friendly hash of the signing public key.
156
154
 
157
155
  ```js
158
156
  const name = await keys.getDeviceName()
@@ -161,47 +159,38 @@ const name = await keys.getDeviceName()
161
159
  const name = await keys.deviceName
162
160
  ```
163
161
 
164
- #### `keys.persisted`
162
+ #### `keys.hasPersisted`
165
163
 
166
164
  A flag indicating whether `.persist` has been called, meaning that these keys
167
165
  are saved in `indexedDB`.
168
166
 
169
- #### `keys.publicEncryptKey`
167
+ #### `keys.publicExchangeKey`
170
168
 
171
- The public encryption `CryptoKey`.
169
+ The public encryption `CryptoKey`. For ECC keys, this is the X25519 exchange key.
170
+ For RSA keys, this is the RSA encryption key.
172
171
 
173
- #### `keys.getPublicEncryptKey()`
172
+ #### `keys.publicExchangeKeyAsString()`
174
173
 
175
- Get the public encryption key, as a `base64` string. For other formats,
176
- [see below](#keysgetpublicencryptkeyformat).
174
+ Get the public encryption key as a `base64` string. For other formats,
175
+ [see below](#format-options).
177
176
 
178
177
  ```ts
179
178
  {
180
- async getPublicEncryptKey ():Promise<string>
179
+ async publicExchangeKeyAsString (format?:SupportedEncodings):Promise<string>
181
180
  }
182
181
  ```
183
182
 
184
- ##### `keys.getPublicEncryptKey(format)`
183
+ #### `keys.publicWriteKey`
185
184
 
186
- Get the public encryption key. The given format should be a
187
- [supported encoding](https://github.com/achingbrain/uint8arrays/blob/26684d4fa1a78f3e5c16e74bf13192e881db4fcf/src/util/bases.ts#L46) in
188
- [uint8arrays](https://github.com/achingbrain/uint8arrays).
185
+ The public signing `CryptoKey`. This is the Ed25519 or RSA signing key.
189
186
 
190
- ```ts
191
- {
192
- async getPublicEncryptKey (
193
- format?:SupportedEncodings
194
- ):Promise<string>
195
- }
196
- ```
197
-
198
- #### keys.getPublicEncryptKey.uint8Array
187
+ #### `keys.publicWriteKeyAsString()`
199
188
 
200
- Get the public encryption key as a `Uint8Array`.
189
+ Get the public signing key as a string.
201
190
 
202
191
  ```ts
203
192
  {
204
- function uint8Array:()=>Promise<Uint8Array<ArrayBufferLike>>
193
+ async publicWriteKeyAsString (format?:SupportedEncodings):Promise<string>
205
194
  }
206
195
  ```
207
196
 
@@ -216,7 +205,8 @@ await keys.delete()
216
205
  --------------------------------------------------------------------------
217
206
 
218
207
 
219
- ### sign and verify something
208
+ ### Sign and Verify Something
209
+
220
210
  `.verify` takes the content, the signature, and the DID for the public key
221
211
  used to sign. The DID is exposed as the property `.DID` on a `Keys` instance.
222
212
 
@@ -227,27 +217,85 @@ used to sign. The DID is exposed as the property `.DID` on a `Keys` instance.
227
217
  >
228
218
 
229
219
  ```js
230
- import { Keys, verify } from '@bicycle-codes/keys'
220
+ import { RsaKeys, verify } from '@substrate-system/keys/rsa'
221
+ // or: import { EccKeys, verify } from '@substrate-system/keys/ecc'
231
222
 
232
- const keys = await Keys.create()
223
+ const keys = await RsaKeys.create()
224
+ // or: const keys = await EccKeys.create()
233
225
 
234
226
  // sign something
235
- const sig = await keys.sign.asString('hello string')
227
+ const sig = await keys.signAsString('hello string')
228
+ // or string format: const sig = await keys.sign.asString('hello string')
236
229
 
237
230
  // verify the signature
238
231
  const isOk = await verify('hello string', sig, keys.DID)
239
232
  ```
240
233
 
241
234
  ### encrypt something
242
- Take the public key we are encrypting to, return an `ArrayBuffer`, containing
243
- the encrypted AES key concattenated with the `iv` and encrypted content.
235
+ Take the public key we are encrypting to, return encrypted content.
236
+
237
+ #### `keys.encrypt` methods
238
+
239
+ Encrypt something, return a Uint8Array.
240
+
241
+ **ECC:**
242
+
243
+ >
244
+ > [!NOTE]
245
+ > `recipient` is optional. If it is omitted, then this will encrypt to
246
+ > its own public key, a "note to self."
247
+ >
248
+
249
+ ```ts
250
+ async encrypt (
251
+ content:string|Uint8Array,
252
+ recipient?:CryptoKey|string, // their public key
253
+ info?:string,
254
+ aesKey?:SymmKey|Uint8Array|string,
255
+ keysize?:SymmKeyLength
256
+ ):Promise<Uint8Array>
257
+ ```
244
258
 
259
+ **RSA:**
260
+ ```ts
261
+ async encrypt (
262
+ content:string|Uint8Array,
263
+ recipient?:CryptoKey|string,
264
+ aesKey?:SymmKey|Uint8Array|string, // For RSA, can pass in AES key
265
+ keysize?:SymmKeyLength,
266
+ ):Promise<Uint8Array>
267
+ ```
268
+
269
+ #### `keys.encryptAsString` methods
270
+
271
+ Encrypt something, return a string.
272
+
273
+ **ECC:**
274
+ ```ts
275
+ async encryptAsString (
276
+ content:string|Uint8Array,
277
+ recipient?:CryptoKey|string,
278
+ info?:string,
279
+ aesKey?:SymmKey|Uint8Array|string,
280
+ keysize?:SymmKeyLength,
281
+ ):Promise<string>
282
+ ```
283
+
284
+ **RSA:**
285
+ ```ts
286
+ async encryptAsString (
287
+ content:string|Uint8Array,
288
+ recipient?:CryptoKey|string,
289
+ aesKey?:SymmKey|Uint8Array|string,
290
+ keysize?:SymmKeyLength,
291
+ ):Promise<string>
292
+ ```
245
293
 
246
294
  ```js
247
- import { encryptTo } from '@bicycle-codes/keys'
295
+ import { encryptTo } from '@substrate-system/keys/rsa' // RSA version
248
296
 
249
297
  // need to know the public key we are encrypting for
250
- const publicKey = await keys.getPublicEncryptKey()
298
+ const publicKey = await keys.publicExchangeKeyAsString() // Both ECC and RSA
251
299
 
252
300
  const encrypted = await encryptTo({
253
301
  content: 'hello public key',
@@ -257,19 +305,21 @@ const encrypted = await encryptTo({
257
305
  const encrypted = await encryptTo.asString({
258
306
  content: 'hello public key',
259
307
  publicKey
260
- })
261
- // => <encrypted text>
308
+ }) // => <encrypted text>
262
309
  ```
263
310
 
264
311
  ### decrypt something
265
312
  A `Keys` instance has a method `decrypt`. The `encryptedMessage` argument is
266
- an `ArrayBuffer` as returned from `encryptTo`, above.
313
+ an `ArrayBuffer`, as returned from `encryptTo`, above.
267
314
 
268
315
  ```js
269
- import { Keys } from '@bicycle-codes/keys'
316
+ import { EccKeys } from '@substrate-system/keys/ecc'
317
+ // or: import { RsaKeys } from '@substrate-system/keys/rsa'
270
318
 
271
- const keys = await Keys.create()
272
- // ...
319
+ const keys = await EccKeys.create()
320
+ // or: const keys = await RsaKeys.create()
321
+
322
+ // This will decrypt the message using our own public key
273
323
  const decrypted = await keys.decrypt(encryptedMsg)
274
324
  ```
275
325
 
@@ -279,20 +329,27 @@ const decrypted = await keys.decrypt(encryptedMsg)
279
329
 
280
330
  ### Create a new `Keys` instance
281
331
 
282
- Use the factory function `Keys.create`. The optional parameters,
332
+ Use the factory function `EccKeys.create` or `RsaKeys.create`. The optional parameters,
283
333
  `encryptionKeyName` and `signingKeyName`, are added as properties to the
284
- `keys` instance -- `ENCRYPTION_KEY_NAME` and `SIGNING_KEY_NAME`. These are
285
- used as indexes for saving the keys in `indexedDB`.
334
+ `keys` instance. These are used as indexes for saving the keys in `indexedDB`.
286
335
 
336
+ **ECC:**
287
337
  ```ts
288
- class Keys {
289
- ENCRYPTION_KEY_NAME:string = 'encryption-key'
290
- SIGNING_KEY_NAME:string = 'signing-key'
291
-
292
- static async create (opts?:{
293
- encryptionKeyName:string,
294
- signingKeyName:string
295
- }):Promise<Keys>
338
+ class EccKeys {
339
+ static EXCHANGE_KEY_NAME:string = 'ecc-exchange'
340
+ static WRITE_KEY_NAME:string = 'ecc-write'
341
+
342
+ static async create (session?:boolean):Promise<EccKeys>
343
+ }
344
+ ```
345
+
346
+ **RSA:**
347
+ ```ts
348
+ class RsaKeys {
349
+ static EXCHANGE_KEY_NAME:string = 'rsa-exchange'
350
+ static WRITE_KEY_NAME:string = 'rsa-write'
351
+
352
+ static async create (session?:boolean):Promise<RsaKeys>
296
353
  }
297
354
  ```
298
355
 
@@ -301,9 +358,10 @@ class Keys {
301
358
  Use the factory function b/c async.
302
359
 
303
360
  ```js
304
- import { Keys } from '@bicycle-codes/keys'
361
+ import { EccKeys } from '@substrate-system/keys/ecc'
362
+ // or: import { RsaKeys } from '@substrate-system/keys/rsa'
305
363
 
306
- const keys = await Keys.create()
364
+ const keys = await EccKeys.create()
307
365
  ```
308
366
 
309
367
  ### Get a hash of the DID
@@ -316,7 +374,7 @@ The static method requires a `DID` string to be passed in.
316
374
  #### static method
317
375
 
318
376
  ```ts
319
- class Keys {
377
+ class EccKeys { // or RsaKeys
320
378
  static async deviceName (did:DID):Promise<string>
321
379
  }
322
380
  ```
@@ -326,37 +384,36 @@ class Keys {
326
384
  If used as an instance method, this will use the `DID` assigned to the instance.
327
385
 
328
386
  ```ts
329
- class Keys {
387
+ class EccKeys { // or RsaKeys
330
388
  async getDeviceName ():Promise<string>
331
389
  }
332
390
  ```
333
391
 
334
392
  ### Persist the keys
335
- Save the keys to `indexedDB`. This depends on the values of class properties
336
- `ENCRYPTION_KEY_NAME` and `SIGNING_KEY_NAME`. Set them if you want to change the
337
- indexes under which the keys are saved to `indexedDB`.
393
+ Save the keys to `indexedDB`. This depends on the values of the static class
394
+ properties `EXCHANGE_KEY_NAME` and `WRITE_KEY_NAME`.
395
+ Set them if you want to change the indexes under which the keys are
396
+ saved to `indexedDB`.
338
397
 
339
398
  By default we use these:
340
- ```js
341
- const DEFAULT_ENC_NAME = 'encryption-key'
342
- const DEFAULT_SIG_NAME = 'signing-key'
343
- ```
399
+ - **ECC**: `'ecc-exchange'` and `'ecc-write'`
400
+ - **RSA**: `'rsa-exchange'` and `'rsa-write'`
344
401
 
345
402
  #### `.persist`
346
403
 
347
404
  ```ts
348
- class Keys {
405
+ class EccKeys { // or RsaKeys
349
406
  async persist ():Promise<void>
350
407
  }
351
408
  ```
352
409
 
353
410
  #### `.persist` example
354
411
  ```js
355
- import { Keys } from '@bicycle-codes/keys'
412
+ import { EccKeys } from '@substrate-system/keys/ecc'
356
413
 
357
- const keys = await Keys.create()
358
- keys.ENCRYPTION_KEY_NAME = 'encryption-key-custom-name'
359
- keys.SIGNING_KEY_NAME = 'signing-key-custom-name'
414
+ const keys = await EccKeys.create()
415
+ EccKeys.EXCHANGE_KEY_NAME = 'encryption-key-custom-name'
416
+ EccKeys.WRITE_KEY_NAME = 'signing-key-custom-name'
360
417
  await keys.persist()
361
418
  ```
362
419
 
@@ -366,35 +423,38 @@ Create a `Keys` instance from data saved to `indexedDB`. Pass in different
366
423
 
367
424
  #### `static .load`
368
425
  ```ts
369
- class Keys {
370
- static async load (opts:{
371
- encryptionKeyName,
372
- signingKeyName
373
- } = {
374
- encryptionKeyName: DEFAULT_ENC_NAME,
375
- signingKeyName: DEFAULT_SIG_NAME
376
- }):Promise<Keys>
426
+ class EccKeys { // or RsaKeys
427
+ static async load (opts?:{
428
+ encryptionKeyName?:string,
429
+ signingKeyName?:string,
430
+ session?:boolean,
431
+ }):Promise<EccKeys>
377
432
  }
378
433
  ```
379
434
 
380
435
  #### example
381
436
  ```js
382
- import { Keys } from '@bicycle-codes/keys'
437
+ import { EccKeys } from '@substrate-system/keys/ecc'
438
+ // or: import { RsaKeys } from '@substrate-system/keys/rsa'
383
439
 
384
- const newKeys = await Keys.load()
440
+ const newKeys = await EccKeys.load()
385
441
  ```
386
442
 
387
443
 
388
444
  ### Sign something
389
445
  Create a new signature for the given input.
390
446
 
447
+ **ECC:**
391
448
  ```ts
392
- class Keys {
393
- async sign (
394
- msg:ArrayBuffer|string|Uint8Array,
395
- charsize?:CharSize,
396
- ):Promise<Uint8Array>
397
- }
449
+ async sign (msg:Msg, _charsize?:CharSize):Promise<Uint8Array>
450
+ ```
451
+
452
+ **RSA:**
453
+ ```ts
454
+ async sign (
455
+ msg:Msg,
456
+ charsize:CharSize = DEFAULT_CHAR_SIZE
457
+ ):Promise<Uint8Array>
398
458
  ```
399
459
 
400
460
  #### example
@@ -404,21 +464,25 @@ const sig = await keys.sign('hello signatures')
404
464
 
405
465
  ### Get a signature as a string
406
466
 
407
- #### `keys.sign.asString(msg)`
467
+ #### `keys.signAsString(msg)`
468
+
469
+ Sign a message and return the signature as a base64 encoded string.
408
470
 
409
471
  ```ts
410
472
  {
411
- /**
412
- * Sign a message, return the signature as a base64 encoded string.
413
- *
414
- * @param {Msg} msg The message to sign
415
- * @param {CharSize} [charsize] Character size
416
- * @returns {Promise<string>}
417
- */
418
- asString: async (msg:Msg, charsize?:CharSize):Promise<string>
473
+ async signAsString (msg:string, charsize?:CharSize):Promise<string>
419
474
  }
420
475
  ```
421
476
 
477
+ ```js
478
+ const sig = await keys.signAsString('hello string')
479
+ // => ubW9PIjb360v...
480
+ ```
481
+
482
+ #### Backward compatibility: `keys.sign.asString(msg)`
483
+
484
+ For backward compatibility, the `.asString` method is still available:
485
+
422
486
  ```js
423
487
  const sig = await keys.sign.asString('hello string')
424
488
  // => ubW9PIjb360v...
@@ -430,6 +494,8 @@ that it can be used independently from any keypairs. You need to pass in the
430
494
  data that was signed, the signature, and the `DID` string of the public key used
431
495
  to create the signature.
432
496
 
497
+ This works the same for either RSA or ECC keys.
498
+
433
499
  ```ts
434
500
  async function verify (
435
501
  msg:string|Uint8Array,
@@ -438,15 +504,25 @@ async function verify (
438
504
  ):Promise<boolean>
439
505
  ```
440
506
 
507
+ **RSA verification uses RSA-PSS with SHA-256:**
508
+
441
509
  ```js
442
- import { verify } from '@bicycle-codes/keys'
510
+ import { verify } from '@substrate-system/keys/rsa'
511
+
512
+ const isOk = await verify('hello string', sig, keys.DID)
513
+ ```
514
+
515
+ **ECC verification uses Ed25519:**
516
+
517
+ ```js
518
+ import { verify } from '@substrate-system/keys/ecc'
443
519
 
444
520
  const isOk = await verify('hello string', sig, keys.DID)
445
521
  ```
446
522
 
447
523
  ### Encrypt a key
448
524
 
449
- Use asynchronous (RSA) encryption to encrypt an AES key to the given public key.
525
+ Use asymmetric (RSA) encryption to encrypt an AES key to the given public key.
450
526
 
451
527
  ```ts
452
528
  async function encryptKeyTo ({ key, publicKey }:{
@@ -457,18 +533,18 @@ async function encryptKeyTo ({ key, publicKey }:{
457
533
 
458
534
  #### example
459
535
  ```js
460
- import { encryptKeyTo } from '@bicycle-codes/keys'
536
+ import { encryptKeyTo } from '@substrate-system/keys/rsa'
461
537
 
462
538
  // pass in a CryptoKey
463
539
  const encrypted = await encryptKeyTo({
464
540
  key: myAesKey,
465
- publicKey: keys.publicEncryptKey
541
+ publicKey: keys.publicExchangeKey
466
542
  })
467
543
 
468
544
  // pass in a base64 string
469
545
  const encryptedTwo = await encryptKeyTo({
470
546
  key: aesKey,
471
- publicKey: await keys.getPublicEncryptKey()
547
+ publicKey: await keys.publicExchangeKeyAsString()
472
548
  }) // => Uint8Array
473
549
  ```
474
550
 
@@ -477,8 +553,11 @@ const encryptedTwo = await encryptKeyTo({
477
553
  Encrypt the given key to the public key, and return the result as a
478
554
  base64 string.
479
555
 
556
+ > !NOTE
557
+ > This is only relevant for RSA keys
558
+
480
559
  ```ts
481
- import { encryptKeyTo } from '@bicycle-codes/keys'
560
+ import { encryptKeyTo } from '@substrate-system/keys/rsa'
482
561
 
483
562
  encryptKeyTo.asString = async function ({ key, publicKey }:{
484
563
  key:string|Uint8Array|CryptoKey;
@@ -487,8 +566,11 @@ encryptKeyTo.asString = async function ({ key, publicKey }:{
487
566
  ```
488
567
 
489
568
  #### format
490
- `encryptKeyTo.asString` takes an optional second argument for [the format](https://github.com/achingbrain/uint8arrays/blob/26684d4fa1a78f3e5c16e74bf13192e881db4fcf/src/util/bases.ts#L46)
491
- of the returned string. Format is anything supported by [uint8arrays](https://github.com/achingbrain/uint8arrays). By default, if omitted, it is `base64`.
569
+ `encryptKeyTo.asString` takes an optional second argument for
570
+ [the format](https://github.com/achingbrain/uint8arrays/blob/26684d4fa1a78f3e5c16e74bf13192e881db4fcf/src/util/bases.ts#L46)
571
+ of the returned string.
572
+ Format is anything supported by [uint8arrays](https://github.com/achingbrain/uint8arrays).
573
+ By default, if omitted, it is `base64`.
492
574
 
493
575
 
494
576
  ### Asymmetrically encrypt some arbitrary data
@@ -497,8 +579,14 @@ Encrypt the given message to the given public key. If an AES key is not
497
579
  provided, one will be created. Use the AES key to encrypt the given
498
580
  content, then encrypt the AES key to the given public key.
499
581
 
582
+ > !NOTE
583
+ > This is only relevant for RSA keys.
584
+ > If using ECC keys, a symmetric key is automatically generated
585
+ > via diffie-hellman.
586
+
500
587
  The return value is an ArrayBuffer containing the encrypted AES key +
501
- the `iv` + the encrypted content.
588
+ the `iv` + the encrypted content if using RSA. It is `salt` + `iv` + cipher text
589
+ if using ECC.
502
590
 
503
591
  To decrypt, pass the returned value to `keys.decrypt`, where `keys` is an
504
592
  instance with the corresponding private key.
@@ -515,11 +603,11 @@ async function encryptTo (
515
603
 
516
604
  #### example
517
605
  ```js
518
- import { encryptTo } from '@bicycle-codes/keys'
606
+ import { encryptTo } from '@substrate-system/keys/rsa'
519
607
 
520
608
  const encrypted = await encryptTo({
521
609
  content: 'hello encryption',
522
- publicKey: keys.publicEncryptKey
610
+ publicKey: keys.publicExchangeKey
523
611
  })
524
612
 
525
613
  // => ArrayBuffer
@@ -528,22 +616,42 @@ const encrypted = await encryptTo({
528
616
  ### Asymmetrically encrypt a string, return a new string
529
617
 
530
618
  Encrypt the given string, and return a new string that is the (encrypted) AES
531
- key concattenated with the `iv` and cipher text. The
532
- corresponding method `keys.decrypt.asString` will know how to parse and
619
+ key concatenated with the `iv` and cipher text. The
620
+ corresponding method `keys.decryptAsString` will know how to parse and
533
621
  decrypt the resulting text.
534
622
 
535
- Use the functions `encryptTo.asString` and `keys.decrypt.asString`.
623
+ Use the functions `encryptTo.asString` and `keys.decryptAsString`.
624
+
625
+ #### `keys.decryptAsString`
536
626
 
537
- #### `keys.decrypt.asString`
627
+ **ECC:**
538
628
  ```ts
539
- async function asString (msg:string, keysize?:SymmKeyLength):Promise<string>
629
+ async decryptAsString (
630
+ msg:string|Uint8Array|ArrayBuffer,
631
+ publicKey?:CryptoKey|string,
632
+ aesAlgorithm?:string,
633
+ info?:string,
634
+ ):Promise<string>
540
635
  ```
541
636
 
637
+ **RSA:**
638
+ ```ts
639
+ async decryptAsString (
640
+ msg:string|Uint8Array|ArrayBuffer,
641
+ keysize?:CryptoKey|string|SymmKeyLength,
642
+ _aesAlgorithm?:string,
643
+ ):Promise<string>
644
+ ```
645
+
646
+ ##### example
647
+
542
648
  ```js
543
- import { Keys, encryptTo } from '@bicycle-codes/keys'
649
+ import { RsaKeys, encryptTo } from '@substrate-system/keys/rsa' // RSA example
650
+ // or: import { EccKeys } from '@substrate-system/keys/ecc'
544
651
 
545
- const keys = await Keys.create()
546
- const pubKey = await keys.getPublicEncryptKey()
652
+ const keys = await RsaKeys.create()
653
+ // or: const keys = await EccKeys.create()
654
+ const pubKey = await keys.publicExchangeKeyAsString() // Both ECC and RSA
547
655
  const msg = { type: 'test', content: 'hello' }
548
656
  const cipherText = await encryptTo.asString({
549
657
  content: JSON.stringify(msg),
@@ -551,35 +659,43 @@ const cipherText = await encryptTo.asString({
551
659
  publicKey: pubKey
552
660
  }) // => string
553
661
 
554
- const text = await keys.decrypt.asString(cipherText)
662
+ const text = await keys.decryptAsString(cipherText)
555
663
  const data = JSON.parse(text)
556
664
  // => { type: 'test', content: 'hello' }
557
665
  ```
558
666
 
559
667
  ### Decrypt a message
668
+
669
+ **ECC:**
560
670
  ```ts
561
- class Keys {
562
- async decrypt (
563
- msg:string|Uint8Array|ArrayBuffer,
564
- keysize?:SymmKeyLength
565
- ):Promise<Uint8Array>
566
- }
671
+ async decrypt (
672
+ msg:string|Uint8Array|ArrayBuffer,
673
+ publicKey?:CryptoKey|string,
674
+ aesAlgorithm?:string,
675
+ info?:string,
676
+ ):Promise<ArrayBuffer>
677
+ ```
678
+
679
+ > !NOTE
680
+ > ECC keys will use our own public key if it is not passed in.
681
+
682
+ **RSA:**
683
+ ```ts
684
+ async decrypt (
685
+ msg:string|Uint8Array|ArrayBuffer,
686
+ keysize?:CryptoKey|string|SymmKeyLength,
687
+ _aesAlgorithm?:string,
688
+ ):Promise<Uint8Array>
567
689
  ```
568
690
 
569
691
  ```js
570
692
  const decrypted = await keys.decrypt(encrypted)
571
- // => Uint8Array
693
+ // => ArrayBuffer (ECC) or Uint8Array (RSA)
572
694
  ```
573
695
 
574
- ### `.decrypt.asString`
696
+ ### Backward compatibility: `.decrypt.asString`
575
697
  Decrypt a message, and stringify the result.
576
698
 
577
- ```ts
578
- {
579
- async function asString (msg:EncryptedMessage):Promise<string>
580
- }
581
- ```
582
-
583
699
  ```js
584
700
  await keys.decrypt.asString(encryptedString)
585
701
  // => 'hello encryption'
@@ -587,15 +703,18 @@ await keys.decrypt.asString(encryptedString)
587
703
 
588
704
  ### In memory only
589
705
  Create a keypair, but do not save it in `indexedDB`, even if you call `persist`.
590
- Pass an option `.session` to `.create` or `.load`.
706
+ Pass `true` as the session parameter to `.create` or pass `{ session: true }` to `.load`.
591
707
 
592
708
  ```js
593
- import { Keys } from '@bicycle-codes/keys'
709
+ import { EccKeys } from '@substrate-system/keys/ecc'
710
+ // or: import { RsaKeys } from '@substrate-system/keys/rsa'
594
711
 
595
- const keys = await Keys.create({ session: true })
712
+ const keys = await EccKeys.create(true)
713
+ // or: const keys = await RsaKeys.create(true)
596
714
 
597
715
  // or pass it to `.load`
598
- const keysTwo = await Keys.load({ session: true })
716
+ const keysTwo = await EccKeys.load({ session: true })
717
+ // or: const keysTwo = await RsaKeys.load({ session: true })
599
718
  ```
600
719
 
601
720
  ## AES
@@ -606,7 +725,7 @@ Expose several AES functions with nice defaults.
606
725
  * `iv` size: [`12` bytes](https://crypto.stackexchange.com/questions/41601/aes-gcm-recommended-iv-size-why-12-bytes) (96 bits)
607
726
 
608
727
  ```js
609
- import { AES } from '@bicycle-codes/keys'
728
+ import { AES } from '@substrate-system/keys/aes'
610
729
 
611
730
  const key = await AES.create(/* ... optional arguments ... */)
612
731
  ```
@@ -622,7 +741,7 @@ function create (opts:{ alg:string, length:number } = {
622
741
  ```
623
742
 
624
743
  ```ts
625
- import { AES } from '@bicycle-codes/keys'
744
+ import { AES } from '@substrate-system/keys/aes'
626
745
  const aesKey = await AES.create()
627
746
  ```
628
747
 
@@ -634,7 +753,7 @@ Get the AES key as a `Uint8Array`.
634
753
  ```
635
754
 
636
755
  ```js
637
- import { AES } from '@bicycle-codes/keys'
756
+ import { AES } from '@substrate-system/keys/aes'
638
757
  const exported = await AES.export(aesKey)
639
758
  ```
640
759
 
@@ -649,7 +768,7 @@ async function asString (
649
768
  ```
650
769
 
651
770
  ```js
652
- import { AES } from '@bicycle-codes/keys'
771
+ import { AES } from '@substrate-system/keys/aes'
653
772
  const exported = await AES.export.asString(aesKey)
654
773
  ```
655
774
 
@@ -666,7 +785,7 @@ async function encrypt (
666
785
  ```
667
786
 
668
787
  ```js
669
- import { AES } from '@bicycle-codes/keys'
788
+ import { AES } from '@substrate-system/keys/aes'
670
789
  import { fromString } from 'uint8arrays'
671
790
 
672
791
  const encryptedText = await AES.encrypt(fromString('hello AES'), aesKey)
@@ -682,7 +801,7 @@ async function decrypt (
682
801
  ```
683
802
 
684
803
  ```js
685
- import { AES } from '@bicycle-codes/keys'
804
+ import { AES } from '@substrate-system/keys/aes'
686
805
 
687
806
  const decryptedText = await AES.decrypt(encryptedText, aesKey)
688
807
  ```