@substrate-system/keys 0.2.34 → 0.2.36
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/README.md +131 -13
- package/dist/_base.min.js +2 -2
- package/dist/_base.min.js.map +3 -3
- package/dist/aes/index.min.js +2 -2
- package/dist/aes/index.min.js.map +3 -3
- package/dist/browser/index.js +107 -15
- package/dist/browser/index.min.js +2 -2
- package/dist/constants.cjs +4 -8
- package/dist/constants.cjs.map +2 -2
- package/dist/constants.d.ts +2 -4
- package/dist/constants.d.ts.map +1 -1
- package/dist/constants.js +4 -8
- package/dist/constants.js.map +2 -2
- package/dist/constants.min.cjs +1 -1
- package/dist/constants.min.cjs.map +3 -3
- package/dist/constants.min.js +1 -1
- package/dist/constants.min.js.map +3 -3
- package/dist/crypto.min.js +2 -2
- package/dist/crypto.min.js.map +3 -3
- package/dist/ecc/index.cjs +103 -7
- package/dist/ecc/index.cjs.map +2 -2
- package/dist/ecc/index.d.ts +34 -3
- package/dist/ecc/index.d.ts.map +1 -1
- package/dist/ecc/index.js +105 -11
- package/dist/ecc/index.js.map +2 -2
- package/dist/ecc/index.min.cjs +1 -1
- package/dist/ecc/index.min.cjs.map +3 -3
- package/dist/ecc/index.min.js +2 -2
- package/dist/ecc/index.min.js.map +3 -3
- package/dist/errors.min.js +1 -1
- package/dist/index.min.js +2 -2
- package/dist/index.min.js.map +3 -3
- package/dist/meta.json +14 -16
- package/dist/rsa/index.cjs.map +2 -2
- package/dist/rsa/index.d.ts.map +1 -1
- package/dist/rsa/index.js.map +2 -2
- package/dist/rsa/index.min.cjs.map +2 -2
- package/dist/rsa/index.min.js +2 -2
- package/dist/rsa/index.min.js.map +3 -3
- package/dist/types.cjs.map +2 -2
- package/dist/types.d.ts +10 -0
- package/dist/types.d.ts.map +1 -1
- package/dist/types.js.map +2 -2
- package/dist/types.min.cjs +1 -1
- package/dist/types.min.cjs.map +2 -2
- package/dist/types.min.js +1 -1
- package/dist/types.min.js.map +2 -2
- package/dist/util.min.js +1 -1
- package/dist/util.min.js.map +3 -3
- package/package.json +4 -10
package/README.md
CHANGED
|
@@ -5,7 +5,6 @@
|
|
|
5
5
|
[](https://semver.org/)
|
|
6
6
|
[](./CHANGELOG.md)
|
|
7
7
|
[](https://packagephobia.com/result?p=@substrate-system/keys)
|
|
8
|
-
[](https://bundlephobia.com/package/@substrate-system/keys)
|
|
9
8
|
[](LICENSE)
|
|
10
9
|
|
|
11
10
|
|
|
@@ -37,38 +36,73 @@ See also, [the API docs generated from typescript](https://substrate-system.gith
|
|
|
37
36
|
* [ESM](#esm)
|
|
38
37
|
* [Common JS](#common-js)
|
|
39
38
|
* [pre-bundled JS](#pre-bundled-js)
|
|
39
|
+
+ [copy](#copy)
|
|
40
|
+
+ [HTML](#html)
|
|
40
41
|
* [`@substrate-system/keys`](#substrate-systemkeys)
|
|
41
42
|
* [`ecc`](#ecc)
|
|
42
43
|
* [`rsa`](#rsa)
|
|
43
|
-
* [`aes`](#aes)
|
|
44
|
+
* [import `aes`](#import-aes)
|
|
44
45
|
* [`crypto`](#crypto)
|
|
46
|
+
+ [`crypto.verify`](#cryptoverify)
|
|
47
|
+
+ [`crypto.keyTypeFromDid`](#cryptokeytypefromdid)
|
|
45
48
|
- [Get started](#get-started)
|
|
46
49
|
* [Verify a signature](#verify-a-signature)
|
|
47
|
-
* [ECC](#ecc)
|
|
50
|
+
* [ECC keys](#ecc-keys)
|
|
51
|
+
+ [Create a keypair](#create-a-keypair)
|
|
52
|
+
+ ["Add a device"](#add-a-device)
|
|
48
53
|
* [some notes about the `keys` instance](#some-notes-about-the-keys-instance)
|
|
54
|
+
+ [`keys.DID`](#keysdid)
|
|
55
|
+
+ [`keys.getDeviceName` / `keys.deviceName`](#keysgetdevicename--keysdevicename)
|
|
56
|
+
+ [`keys.hasPersisted`](#keyshaspersisted)
|
|
57
|
+
+ [`keys.publicExchangeKey`](#keyspublicexchangekey)
|
|
58
|
+
+ [`keys.publicExchangeKeyAsString()`](#keyspublicexchangekeyasstring)
|
|
59
|
+
+ [`keys.publicWriteKey`](#keyspublicwritekey)
|
|
60
|
+
+ [`keys.publicWriteKeyAsString()`](#keyspublicwritekeyasstring)
|
|
49
61
|
* [Delete a keypair](#delete-a-keypair)
|
|
50
62
|
* [Sign and Verify Something](#sign-and-verify-something)
|
|
51
63
|
* [encrypt something](#encrypt-something)
|
|
64
|
+
+ [`keys.encrypt` methods](#keysencrypt-methods)
|
|
65
|
+
+ [`keys.encryptAsString` methods](#keysencryptasstring-methods)
|
|
52
66
|
* [decrypt something](#decrypt-something)
|
|
53
|
-
- [
|
|
67
|
+
- [Examples](#examples)
|
|
54
68
|
* [Create a new `Keys` instance](#create-a-new-keys-instance)
|
|
69
|
+
+ [Parameters](#parameters)
|
|
70
|
+
+ [`.create()` example](#create-example)
|
|
55
71
|
* [Get a hash of the DID](#get-a-hash-of-the-did)
|
|
72
|
+
+ [Static `deviceName` method](#static-devicename-method)
|
|
73
|
+
+ [Instance `getdeviceName` method](#instance-getdevicename-method)
|
|
56
74
|
* [Persist the keys](#persist-the-keys)
|
|
75
|
+
+ [`.persist`](#persist)
|
|
76
|
+
+ [`.persist` example](#persist-example)
|
|
57
77
|
* [Restore from indexedDB](#restore-from-indexeddb)
|
|
78
|
+
+ [`static .load`](#static-load)
|
|
79
|
+
+ [Parameters](#parameters-1)
|
|
80
|
+
+ [example](#example)
|
|
58
81
|
* [Sign something](#sign-something)
|
|
82
|
+
+ [example](#example-1)
|
|
59
83
|
* [Get a signature as a string](#get-a-signature-as-a-string)
|
|
84
|
+
+ [`keys.signAsString(msg)`](#keyssignasstringmsg)
|
|
85
|
+
+ [Backward compatibility: `keys.sign.asString(msg)`](#backward-compatibility-keyssignasstringmsg)
|
|
60
86
|
* [Verify a signature](#verify-a-signature-1)
|
|
61
87
|
* [Encrypt a key](#encrypt-a-key)
|
|
88
|
+
+ [example](#example-2)
|
|
89
|
+
+ [encrypt a key, return a string](#encrypt-a-key-return-a-string)
|
|
90
|
+
+ [format](#format)
|
|
62
91
|
* [Asymmetrically encrypt some arbitrary data](#asymmetrically-encrypt-some-arbitrary-data)
|
|
92
|
+
+ [example](#example-3)
|
|
63
93
|
* [Asymmetrically encrypt a string, return a new string](#asymmetrically-encrypt-a-string-return-a-new-string)
|
|
94
|
+
+ [`keys.decryptAsString`](#keysdecryptasstring)
|
|
64
95
|
* [Decrypt a message](#decrypt-a-message)
|
|
65
96
|
* [In memory only](#in-memory-only)
|
|
66
97
|
- [AES](#aes)
|
|
67
98
|
* [`create`](#create)
|
|
68
99
|
* [`export`](#export)
|
|
100
|
+
+ [`aes.export` example](#aesexport-example)
|
|
69
101
|
* [`export.asString`](#exportasstring)
|
|
102
|
+
+ [`export.asString` example](#exportasstring-example)
|
|
70
103
|
* [`AES.encrypt`](#aesencrypt)
|
|
71
104
|
* [`AES.decrypt`](#aesdecrypt)
|
|
105
|
+
- [See also](#see-also)
|
|
72
106
|
|
|
73
107
|
<!-- tocstop -->
|
|
74
108
|
|
|
@@ -121,6 +155,8 @@ cp ./node_modules/@substrate-system/keys/dist/index.min.js ./public/keys.min.js
|
|
|
121
155
|
|
|
122
156
|
### `@substrate-system/keys`
|
|
123
157
|
|
|
158
|
+
Import everything.
|
|
159
|
+
|
|
124
160
|
```js
|
|
125
161
|
import * as keys from '@substrate-system/keys'
|
|
126
162
|
```
|
|
@@ -153,7 +189,7 @@ import {
|
|
|
153
189
|
} from '@substrate-system/keys/rsa'
|
|
154
190
|
```
|
|
155
191
|
|
|
156
|
-
### `aes`
|
|
192
|
+
### [import `aes`](#aes)
|
|
157
193
|
|
|
158
194
|
Import AES (symmetric) encryption only.
|
|
159
195
|
|
|
@@ -206,7 +242,9 @@ const rsaType = keyTypeFromDid(rsaKeys.DID)
|
|
|
206
242
|
|
|
207
243
|
### Verify a signature
|
|
208
244
|
|
|
209
|
-
This function takes either an Ed25519 key or an RSA key.
|
|
245
|
+
This function takes either an Ed25519 key or an RSA key. This is exposed as
|
|
246
|
+
a separate function so that you do not need to bundle all of `keys` just to
|
|
247
|
+
verify something.
|
|
210
248
|
|
|
211
249
|
```js
|
|
212
250
|
import { verify } from '@substrate-system/keys/crypto'
|
|
@@ -218,9 +256,10 @@ const isOk = await verify({ message, publicKey: ecc.DID, signature })
|
|
|
218
256
|
const isOk = await verify({ message, publicKey: rsa.DID, signature })
|
|
219
257
|
```
|
|
220
258
|
|
|
221
|
-
### ECC
|
|
259
|
+
### ECC keys
|
|
222
260
|
|
|
223
261
|
#### Create a keypair
|
|
262
|
+
|
|
224
263
|
Create a new keypair, then save it in `indexedDB`.
|
|
225
264
|
|
|
226
265
|
ECC is now supported in all major browsers.
|
|
@@ -240,6 +279,67 @@ const keysAgain = await EccKeys.load()
|
|
|
240
279
|
console.assert(keys.DID === keysAgain.DID) // true
|
|
241
280
|
```
|
|
242
281
|
|
|
282
|
+
#### "Add a device"
|
|
283
|
+
|
|
284
|
+
Take an existing AES key, and encrypt it to a new ECC keypair.
|
|
285
|
+
See [asymmetric docs](./docs-asymmetric/README.md) for more about how
|
|
286
|
+
this works.
|
|
287
|
+
|
|
288
|
+
- Use `wrap()` to encrypt an AES key for a new device's public key
|
|
289
|
+
- Use `unwrap()` on the new device to recover the AES key
|
|
290
|
+
- Add devices without re-encrypting the entire dataset
|
|
291
|
+
|
|
292
|
+
|
|
293
|
+
```ts
|
|
294
|
+
import { EccKeys } from '@substrate-system/keys/ecc'
|
|
295
|
+
import { AES } from '@substrate-system/keys/aes'
|
|
296
|
+
|
|
297
|
+
// Device 1 has a symmetric key for encrypting data
|
|
298
|
+
const device1 = await EccKeys.create()
|
|
299
|
+
const contentKey = await AES.create({ length: 256 })
|
|
300
|
+
|
|
301
|
+
// Encrypt some data.
|
|
302
|
+
// (AES.encrypt automatically prepends iv to ciphertext).
|
|
303
|
+
const message = 'secret data for all devices'
|
|
304
|
+
const encrypted = await AES.encrypt(
|
|
305
|
+
new TextEncoder().encode(message),
|
|
306
|
+
contentKey
|
|
307
|
+
)
|
|
308
|
+
|
|
309
|
+
// ... device 1 needs to obtain device 2's public exchange key ...
|
|
310
|
+
|
|
311
|
+
// wrap the content key for device 2
|
|
312
|
+
const wrapped = await device1.wrap(contentKey, device2.publicExchangeKey)
|
|
313
|
+
|
|
314
|
+
console.log(wrapped)
|
|
315
|
+
// {
|
|
316
|
+
// enc: 'base64-encoded-ephemeral-public-key...',
|
|
317
|
+
// wrappedKey: 'base64-encoded-wrapped-content-key...'
|
|
318
|
+
// }
|
|
319
|
+
|
|
320
|
+
// Store encrypted data + wrapped key entries for each device
|
|
321
|
+
|
|
322
|
+
|
|
323
|
+
// -----------------------------------------
|
|
324
|
+
// device 2
|
|
325
|
+
// -----------------------------------------
|
|
326
|
+
|
|
327
|
+
// ... device 2 needs to obtain the wrapped key ...
|
|
328
|
+
|
|
329
|
+
const device2 = await EccKeys.create()
|
|
330
|
+
|
|
331
|
+
// Device 2 unwraps the content key
|
|
332
|
+
const device2ContentKey = await device2.unwrap(wrapped.enc, wrapped.wrappedKey)
|
|
333
|
+
|
|
334
|
+
// Device 2 decrypts the data with the unwrapped key
|
|
335
|
+
// (AES.decrypt automatically extracts iv from the encrypted data)
|
|
336
|
+
const decrypted = await AES.decrypt(encrypted, device2ContentKey)
|
|
337
|
+
|
|
338
|
+
|
|
339
|
+
console.log(new TextDecoder().decode(decrypted))
|
|
340
|
+
// => 'secret data for all devices'
|
|
341
|
+
```
|
|
342
|
+
|
|
243
343
|
### some notes about the `keys` instance
|
|
244
344
|
|
|
245
345
|
#### `keys.DID`
|
|
@@ -440,7 +540,7 @@ const decrypted = await keys.decrypt(encryptedMsg)
|
|
|
440
540
|
|
|
441
541
|
----------------------------------------------------------------------
|
|
442
542
|
|
|
443
|
-
##
|
|
543
|
+
## Examples
|
|
444
544
|
|
|
445
545
|
### Create a new `Keys` instance
|
|
446
546
|
|
|
@@ -486,7 +586,12 @@ class RsaKeys {
|
|
|
486
586
|
- `session` (optional, boolean): If `true`, keys are created in memory only and won't be saved to `indexedDB` even if `persist()` is called.
|
|
487
587
|
- `extractable` (optional, boolean): If `true`, creates extractable keys that can be exported/read. Defaults to `false` for security.
|
|
488
588
|
|
|
489
|
-
>
|
|
589
|
+
>
|
|
590
|
+
> [!WARNING]
|
|
591
|
+
> Set `extractable: true` only when you need to export private keys.
|
|
592
|
+
> Non-extractable keys (default) are better for security.
|
|
593
|
+
>
|
|
594
|
+
|
|
490
595
|
|
|
491
596
|
#### `.create()` example
|
|
492
597
|
|
|
@@ -504,6 +609,8 @@ const extractableKeys = await EccKeys.create(false, true)
|
|
|
504
609
|
|
|
505
610
|
// Create session-only, extractable keys
|
|
506
611
|
const sessionKeys = await EccKeys.create(true, true)
|
|
612
|
+
|
|
613
|
+
// Session-only keys are not persisted.
|
|
507
614
|
```
|
|
508
615
|
|
|
509
616
|
### Get a hash of the DID
|
|
@@ -513,7 +620,7 @@ this will use the `DID` assigned to the given `Keys` instance.
|
|
|
513
620
|
|
|
514
621
|
The static method requires a `DID` string to be passed in.
|
|
515
622
|
|
|
516
|
-
####
|
|
623
|
+
#### Static `deviceName` method
|
|
517
624
|
|
|
518
625
|
```ts
|
|
519
626
|
class EccKeys { // or RsaKeys
|
|
@@ -521,7 +628,7 @@ class EccKeys { // or RsaKeys
|
|
|
521
628
|
}
|
|
522
629
|
```
|
|
523
630
|
|
|
524
|
-
####
|
|
631
|
+
#### Instance `getdeviceName` method
|
|
525
632
|
|
|
526
633
|
If used as an instance method, this will use the `DID` assigned to the instance.
|
|
527
634
|
|
|
@@ -873,7 +980,7 @@ const keysTwo = await EccKeys.load({ session: true })
|
|
|
873
980
|
|
|
874
981
|
## AES
|
|
875
982
|
|
|
876
|
-
Expose several AES functions
|
|
983
|
+
Expose several AES functions, use nice defaults.
|
|
877
984
|
|
|
878
985
|
* algorithm: `AES-GCM`
|
|
879
986
|
* key size: `256`
|
|
@@ -890,6 +997,11 @@ const key = await AES.create(/* ... optional arguments ... */)
|
|
|
890
997
|
Create a new AES key. By default uses 256 bits & GCM algorithm.
|
|
891
998
|
|
|
892
999
|
```ts
|
|
1000
|
+
import {
|
|
1001
|
+
DEFAULT_SYMM_ALGORITHM,
|
|
1002
|
+
DEFAULT_SYMM_LENGTH
|
|
1003
|
+
} from '@substrate-system/keys/constants'
|
|
1004
|
+
|
|
893
1005
|
function create (opts:{ alg:string, length:number } = {
|
|
894
1006
|
alg: DEFAULT_SYMM_ALGORITHM, // AES-GCM
|
|
895
1007
|
length: DEFAULT_SYMM_LENGTH // 256
|
|
@@ -911,7 +1023,7 @@ export const AES = {
|
|
|
911
1023
|
}
|
|
912
1024
|
```
|
|
913
1025
|
|
|
914
|
-
#### `export` example
|
|
1026
|
+
#### `aes.export` example
|
|
915
1027
|
|
|
916
1028
|
```js
|
|
917
1029
|
import { AES } from '@substrate-system/keys/aes'
|
|
@@ -979,3 +1091,9 @@ const decryped = await AES.decrypt(encryptedText, aesKey)
|
|
|
979
1091
|
const decryptedText = toString(decrypted)
|
|
980
1092
|
// => 'hello AES'
|
|
981
1093
|
```
|
|
1094
|
+
|
|
1095
|
+
## See also
|
|
1096
|
+
|
|
1097
|
+
* [KEM Trails – Understanding Key Encapsulation Mechanisms](https://soatok.blog/2024/02/26/kem-trails-understanding-key-encapsulation-mechanisms/)
|
|
1098
|
+
> the primary motivation for KEMs comes from post-quantum cryptography, not RSA.
|
|
1099
|
+
* [Understanding HKDF](https://soatok.blog/2021/11/17/understanding-hkdf/)
|