@substrate-system/keys 0.2.44 → 0.3.1
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 +72 -27
- package/dist/_base.cjs +11 -4
- package/dist/_base.cjs.map +2 -2
- package/dist/_base.d.ts +10 -3
- package/dist/_base.d.ts.map +1 -1
- package/dist/_base.js +11 -4
- package/dist/_base.js.map +2 -2
- package/dist/_base.min.cjs +1 -1
- package/dist/_base.min.cjs.map +3 -3
- 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 +88 -23
- package/dist/browser/index.min.js +2 -2
- package/dist/crypto.min.js +2 -2
- package/dist/crypto.min.js.map +3 -3
- package/dist/ecc/index.cjs +11 -1
- package/dist/ecc/index.cjs.map +2 -2
- package/dist/ecc/index.d.ts +7 -1
- package/dist/ecc/index.d.ts.map +1 -1
- package/dist/ecc/index.js +11 -1
- 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/index.min.js +2 -2
- package/dist/index.min.js.map +3 -3
- package/dist/meta.json +12 -12
- package/dist/rsa/index.cjs +8 -1
- package/dist/rsa/index.cjs.map +2 -2
- package/dist/rsa/index.d.ts +3 -1
- package/dist/rsa/index.d.ts.map +1 -1
- package/dist/rsa/index.js +8 -1
- package/dist/rsa/index.js.map +2 -2
- package/dist/rsa/index.min.cjs +1 -1
- package/dist/rsa/index.min.cjs.map +3 -3
- package/dist/rsa/index.min.js +2 -2
- package/dist/rsa/index.min.js.map +3 -3
- package/dist/util.min.js +1 -1
- package/dist/util.min.js.map +3 -3
- package/package.json +2 -1
package/README.md
CHANGED
|
@@ -8,10 +8,12 @@
|
|
|
8
8
|
[](LICENSE)
|
|
9
9
|
|
|
10
10
|
|
|
11
|
-
Create and store keypairs in the browser with the
|
|
11
|
+
Create and store keypairs in the browser with the
|
|
12
|
+
[web crypto API](https://developer.mozilla.org/en-US/docs/Web/API/Web_Crypto_API).
|
|
12
13
|
|
|
13
14
|
Use [`indexedDB`](https://developer.mozilla.org/en-US/docs/Web/API/IndexedDB_API)
|
|
14
|
-
to store
|
|
15
|
+
to store
|
|
16
|
+
[non-extractable keypairs](https://developer.mozilla.org/en-US/docs/Web/API/SubtleCrypto/generateKey#extractable)
|
|
15
17
|
in the browser. "Non-extractable" means that the browser prevents you from ever
|
|
16
18
|
reading the private key, but the keys can still be persisted and
|
|
17
19
|
re-used indefinitely.
|
|
@@ -19,13 +21,14 @@ re-used indefinitely.
|
|
|
19
21
|
>
|
|
20
22
|
> [!TIP]
|
|
21
23
|
> Use the [persist method](https://developer.mozilla.org/en-US/docs/Web/API/StorageManager/persist)
|
|
22
|
-
> to
|
|
24
|
+
> to request that the browser not delete from `indexedDB`.
|
|
23
25
|
>
|
|
24
26
|
|
|
25
27
|
Each instance of `Keys` has two keypairs — one for signing, and another
|
|
26
28
|
for encrypting.
|
|
27
29
|
|
|
28
|
-
See also,
|
|
30
|
+
See also,
|
|
31
|
+
[the API docs generated from typescript](https://substrate-system.github.io/keys/).
|
|
29
32
|
|
|
30
33
|
<details><summary><h2>Contents</h2></summary>
|
|
31
34
|
|
|
@@ -46,7 +49,7 @@ See also, [the API docs generated from typescript](https://substrate-system.gith
|
|
|
46
49
|
+ [`crypto.verify`](#cryptoverify)
|
|
47
50
|
+ [`crypto.keyTypeFromDid`](#cryptokeytypefromdid)
|
|
48
51
|
- [Get started](#get-started)
|
|
49
|
-
* [
|
|
52
|
+
* [Storage](#storage)
|
|
50
53
|
* [ECC keys](#ecc-keys)
|
|
51
54
|
+ [Create a keypair](#create-a-keypair)
|
|
52
55
|
+ [Encrypt a Message](#encrypt-a-message)
|
|
@@ -68,6 +71,7 @@ See also, [the API docs generated from typescript](https://substrate-system.gith
|
|
|
68
71
|
+ [`keys.encryptAsString` methods](#keysencryptasstring-methods)
|
|
69
72
|
* [decrypt something](#decrypt-something)
|
|
70
73
|
- [Examples](#examples)
|
|
74
|
+
* [Verify a signature](#verify-a-signature)
|
|
71
75
|
* [Create a new `Keys` instance](#create-a-new-keys-instance)
|
|
72
76
|
+ [Parameters](#parameters)
|
|
73
77
|
+ [`.create()` example](#create-example)
|
|
@@ -243,20 +247,23 @@ const rsaType = keyTypeFromDid(rsaKeys.DID)
|
|
|
243
247
|
|
|
244
248
|
## Get started
|
|
245
249
|
|
|
246
|
-
###
|
|
250
|
+
### Storage
|
|
247
251
|
|
|
248
|
-
|
|
249
|
-
a separate function so that you do not need to bundle all of `keys` just to
|
|
250
|
-
verify something.
|
|
252
|
+
Can use `dbKey` in `.create` to add a prefix to both stored key names:
|
|
251
253
|
|
|
252
|
-
```
|
|
253
|
-
|
|
254
|
+
```ts
|
|
255
|
+
const keys = await EccKeys.create(false, false, { dbKey: 'alice' })
|
|
256
|
+
await keys.persist()
|
|
257
|
+
```
|
|
254
258
|
|
|
255
|
-
|
|
256
|
-
|
|
259
|
+
For `.create`, pass `dbKey` in the third argument to prefix both IndexedDB
|
|
260
|
+
key names. For example, `dbKey: 'alice'` stores ECC keypairs as
|
|
261
|
+
`alice.ecc-exchange` and `alice.ecc-write`, or RSA keypairs as
|
|
262
|
+
`alice.rsa-exchange-key` and `alice.rsa-write-key`.
|
|
257
263
|
|
|
258
|
-
|
|
259
|
-
const
|
|
264
|
+
```ts
|
|
265
|
+
const rsaKeys = await RsaKeys.create(false, false, { dbKey: 'alice' })
|
|
266
|
+
await rsaKeys.persist()
|
|
260
267
|
```
|
|
261
268
|
|
|
262
269
|
### ECC keys
|
|
@@ -270,13 +277,11 @@ ECC is now supported in all major browsers.
|
|
|
270
277
|
```js
|
|
271
278
|
import { EccKeys, verify } from '@substrate-system/keys/ecc'
|
|
272
279
|
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
// save the keys to indexedDB
|
|
276
|
-
await keys.persist()
|
|
280
|
+
// create a keypair, and save it to indexedDB
|
|
281
|
+
const keys = await EccKeys.load()
|
|
277
282
|
|
|
278
283
|
// ... sometime in the future ...
|
|
279
|
-
// get
|
|
284
|
+
// get the same keys back from indexedDB
|
|
280
285
|
const keysAgain = await EccKeys.load()
|
|
281
286
|
|
|
282
287
|
console.assert(keys.DID === keysAgain.DID) // true
|
|
@@ -443,8 +448,9 @@ const name = await keys.deviceName
|
|
|
443
448
|
|
|
444
449
|
#### `keys.hasPersisted`
|
|
445
450
|
|
|
446
|
-
A flag indicating whether
|
|
447
|
-
|
|
451
|
+
A flag indicating whether these keys are saved in `indexedDB`. It is `true`
|
|
452
|
+
after `.persist`, and after any `.load` that did not opt out with
|
|
453
|
+
`{ persist: false }`.
|
|
448
454
|
|
|
449
455
|
#### `keys.destroyed`
|
|
450
456
|
|
|
@@ -674,11 +680,26 @@ const decrypted = await keys.decrypt(encryptedMsg)
|
|
|
674
680
|
|
|
675
681
|
## Examples
|
|
676
682
|
|
|
683
|
+
### Verify a signature
|
|
684
|
+
|
|
685
|
+
This is exposed as a separate import so that you do not need to bundle all of
|
|
686
|
+
`keys` just to verify a signature.
|
|
687
|
+
|
|
688
|
+
```js
|
|
689
|
+
import { verify } from '@substrate-system/keys/crypto'
|
|
690
|
+
|
|
691
|
+
// ed25519
|
|
692
|
+
const isOk = await verify({ message, publicKey: ecc.DID, signature })
|
|
693
|
+
|
|
694
|
+
// RSA
|
|
695
|
+
const isOk = await verify({ message, publicKey: rsa.DID, signature })
|
|
696
|
+
```
|
|
697
|
+
|
|
677
698
|
### Create a new `Keys` instance
|
|
678
699
|
|
|
679
|
-
Use the factory function `EccKeys.create` or `RsaKeys.create`. The optional
|
|
680
|
-
`
|
|
681
|
-
`
|
|
700
|
+
Use the factory function `EccKeys.create` or `RsaKeys.create`. The optional
|
|
701
|
+
`dbKey` parameter prefixes the names used to save both keypairs in
|
|
702
|
+
`indexedDB`.
|
|
682
703
|
|
|
683
704
|
**ECC:**
|
|
684
705
|
```ts
|
|
@@ -690,6 +711,7 @@ class EccKeys {
|
|
|
690
711
|
session?:boolean, // default false
|
|
691
712
|
extractable?:boolean,
|
|
692
713
|
keys?:{
|
|
714
|
+
dbKey?:string,
|
|
693
715
|
exchangeKeys?:CryptoKeyPair|null,
|
|
694
716
|
writeKeys?:CryptoKeyPair|null,
|
|
695
717
|
}
|
|
@@ -706,6 +728,7 @@ class RsaKeys {
|
|
|
706
728
|
session?:boolean, // defaut false
|
|
707
729
|
extractable?:boolean,
|
|
708
730
|
keys?:{
|
|
731
|
+
dbKey?:string,
|
|
709
732
|
exchangeKeys?:CryptoKeyPair|null,
|
|
710
733
|
writeKeys?:CryptoKeyPair|null,
|
|
711
734
|
}
|
|
@@ -717,6 +740,9 @@ class RsaKeys {
|
|
|
717
740
|
|
|
718
741
|
- `session` (optional, boolean): If `true`, keys are created in memory only and won't be saved to `indexedDB` even if `persist()` is called.
|
|
719
742
|
- `extractable` (optional, boolean): If `true`, creates extractable keys that can be exported/read. Defaults to `false` for security.
|
|
743
|
+
- `keys.dbKey` (optional, string): Prefix for both IndexedDB key names.
|
|
744
|
+
- `keys.exchangeKeys` (optional): Existing exchange keypair to use.
|
|
745
|
+
- `keys.writeKeys` (optional): Existing signing keypair to use.
|
|
720
746
|
|
|
721
747
|
>
|
|
722
748
|
> [!WARNING]
|
|
@@ -789,6 +815,10 @@ class EccKeys { // or RsaKeys
|
|
|
789
815
|
}
|
|
790
816
|
```
|
|
791
817
|
|
|
818
|
+
You only need this for keys made with [`.create`](#create-a-new-keys-instance), or
|
|
819
|
+
for a [`.load`](#static-load) that opted out with `{ persist: false }`.
|
|
820
|
+
Otherwise `.load` has already saved them.
|
|
821
|
+
|
|
792
822
|
#### `.persist` example
|
|
793
823
|
```js
|
|
794
824
|
import { EccKeys } from '@substrate-system/keys/ecc'
|
|
@@ -803,6 +833,11 @@ await keys.persist()
|
|
|
803
833
|
Create a `Keys` instance from data saved to `indexedDB`. Pass in different
|
|
804
834
|
`indexedDB` key names for the keys if you need to.
|
|
805
835
|
|
|
836
|
+
If there is nothing saved yet, `.load` creates a new keypair *and saves it*,
|
|
837
|
+
so the keys you get back are durable -- reload the page, call `.load` again,
|
|
838
|
+
and you get the same identity. There is no separate `.persist` step to
|
|
839
|
+
remember. Pass `{ persist: false }` if you want the old two-step behavior.
|
|
840
|
+
|
|
806
841
|
#### `static .load`
|
|
807
842
|
```ts
|
|
808
843
|
class EccKeys { // or RsaKeys
|
|
@@ -811,6 +846,7 @@ class EccKeys { // or RsaKeys
|
|
|
811
846
|
writeKeyName?:string,
|
|
812
847
|
session?:boolean,
|
|
813
848
|
extractable?:boolean,
|
|
849
|
+
persist?:boolean,
|
|
814
850
|
}):Promise<EccKeys>
|
|
815
851
|
}
|
|
816
852
|
```
|
|
@@ -821,6 +857,7 @@ class EccKeys { // or RsaKeys
|
|
|
821
857
|
- `writeKeyName` (optional, string): Custom name for the signing key in `indexedDB`
|
|
822
858
|
- `session` (optional, boolean): If `true`, creates session-only keys if no keys exist in `indexedDB`
|
|
823
859
|
- `extractable` (optional, boolean): If `true` and keys don't exist in `indexedDB`, new keys will be created as extractable. Defaults to `false`.
|
|
860
|
+
- `persist` (optional, boolean): Save the keys to `indexedDB` before returning. Defaults to `true`. Session keys are never written, so `session: true` wins over this.
|
|
824
861
|
|
|
825
862
|
#### example
|
|
826
863
|
|
|
@@ -828,8 +865,10 @@ class EccKeys { // or RsaKeys
|
|
|
828
865
|
import { EccKeys } from '@substrate-system/keys/ecc'
|
|
829
866
|
// or: import { RsaKeys } from '@substrate-system/keys/rsa'
|
|
830
867
|
|
|
831
|
-
// Load existing keys from indexedDB, or create
|
|
868
|
+
// Load existing keys from indexedDB, or create and save new
|
|
869
|
+
// non-extractable ones
|
|
832
870
|
const newKeys = await EccKeys.load()
|
|
871
|
+
console.log(newKeys.hasPersisted) // true
|
|
833
872
|
|
|
834
873
|
// Load with custom options
|
|
835
874
|
const customKeys = await EccKeys.load({
|
|
@@ -837,6 +876,11 @@ const customKeys = await EccKeys.load({
|
|
|
837
876
|
writeKeyName: 'my-custom-signing-key',
|
|
838
877
|
extractable: true // If keys don't exist, create them as extractable
|
|
839
878
|
})
|
|
879
|
+
|
|
880
|
+
// Opt out of saving -- you are responsible for calling `.persist`
|
|
881
|
+
const unsaved = await EccKeys.load({ persist: false })
|
|
882
|
+
console.log(unsaved.hasPersisted) // false
|
|
883
|
+
await unsaved.persist()
|
|
840
884
|
```
|
|
841
885
|
|
|
842
886
|
|
|
@@ -1096,7 +1140,8 @@ const decrypted = await keys.decrypt(encrypted)
|
|
|
1096
1140
|
|
|
1097
1141
|
Create a keypair, but do not save it in `indexedDB`, even if you call `persist`.
|
|
1098
1142
|
Pass `true` as the session parameter to `.create` or
|
|
1099
|
-
pass `{ session: true }` to `.load`.
|
|
1143
|
+
pass `{ session: true }` to `.load`. Session keys are never written to
|
|
1144
|
+
`indexedDB`, so `session: true` beats the `persist` default in `.load`.
|
|
1100
1145
|
|
|
1101
1146
|
```js
|
|
1102
1147
|
import { EccKeys } from '@substrate-system/keys/ecc'
|
package/dist/_base.cjs
CHANGED
|
@@ -233,18 +233,25 @@ const _AbstractKeys = class _AbstractKeys {
|
|
|
233
233
|
* Restore some keys from indexedDB, or create a new keypair if it doesn't
|
|
234
234
|
* exist yet.
|
|
235
235
|
*
|
|
236
|
-
*
|
|
237
|
-
*
|
|
238
|
-
*
|
|
236
|
+
* By default this saves the keys to `indexedDB` before returning, so the
|
|
237
|
+
* keypair you get back survives a page reload. Pass `{ persist: false }`
|
|
238
|
+
* to skip that and call `.persist()` yourself. Session keys are never
|
|
239
|
+
* written to `indexedDB`, so `{ session: true }` wins over `persist`.
|
|
240
|
+
*
|
|
241
|
+
* @param {{ encryptionKeyName, signingKeyName, session, persist }} opts
|
|
242
|
+
* Strings to use as keys in indexedDB, a session boolean -- is this in
|
|
243
|
+
* memory only? Or can it be persisted -- and a persist boolean.
|
|
239
244
|
* @returns {Promise<AbstractKeys>}
|
|
240
245
|
*/
|
|
241
246
|
static async load(opts = {
|
|
242
247
|
session: false
|
|
243
248
|
}) {
|
|
249
|
+
const shouldPersist = opts.persist !== false;
|
|
244
250
|
const exchangeKeyName = opts.encryptionKeyName || this.EXCHANGE_KEY_NAME;
|
|
245
251
|
const writeKeyName = opts.writeKeyName || this.WRITE_KEY_NAME;
|
|
246
252
|
const cached = this._instance;
|
|
247
253
|
if (cached && !cached.destroyed && cached.exchangeKeyName === exchangeKeyName && cached.writeKeyName === writeKeyName) {
|
|
254
|
+
if (shouldPersist && !cached.hasPersisted) await cached.persist();
|
|
248
255
|
return cached;
|
|
249
256
|
}
|
|
250
257
|
let hasPersisted = true;
|
|
@@ -272,11 +279,11 @@ const _AbstractKeys = class _AbstractKeys {
|
|
|
272
279
|
writeKeyName
|
|
273
280
|
});
|
|
274
281
|
this._instance = keys;
|
|
282
|
+
if (shouldPersist && !hasPersisted) await keys.persist();
|
|
275
283
|
return keys;
|
|
276
284
|
}
|
|
277
285
|
};
|
|
278
286
|
__name(_AbstractKeys, "AbstractKeys");
|
|
279
|
-
// type:'ecc'|'rsa'
|
|
280
287
|
__publicField(_AbstractKeys, "EXCHANGE_KEY_NAME");
|
|
281
288
|
// needs to be defined by child class
|
|
282
289
|
__publicField(_AbstractKeys, "WRITE_KEY_NAME");
|
package/dist/_base.cjs.map
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../src/_base.ts"],
|
|
4
|
-
"sourcesContent": ["import { fromString, type SupportedEncodings, toString } from 'uint8arrays'\nimport { get, set, delMany } from 'idb-keyval'\nimport type {\n CharSize,\n Msg,\n SymmKeyLength,\n DID,\n SymmKey,\n} from './types.js'\nimport { AES } from './aes/index.js'\nimport {\n toBase64,\n joinBufs,\n sha256,\n} from './util.js'\nimport {\n rsaOperations,\n publicKeyToDid,\n getPublicKeyAsArrayBuffer,\n} from './crypto.js'\nimport { KeysDeletedError } from './errors.js'\n\nexport { publicKeyToDid, getPublicKeyAsArrayBuffer }\nexport { KeysDeletedError }\nexport * from './constants.js'\nexport type { DID }\nexport { getPublicKeyAsUint8Array } from './crypto.js'\nexport type SerializedKeys = {\n DID:DID;\n pulicExchangeKey:string;\n}\n\nexport interface RsaEncryptor {\n (\n content:string|Uint8Array,\n recipient?:CryptoKey|string,\n aesKey?:SymmKey|Uint8Array|string,\n keysize?:SymmKeyLength\n ):Promise<Uint8Array>\n}\n\nexport interface EccEncryptor {\n (\n content:string|Uint8Array,\n recipient?:CryptoKey|string,\n info?:string,\n aesKey?:SymmKey|Uint8Array|string,\n keysize?:SymmKeyLength\n ):Promise<Uint8Array>\n}\n\nexport interface RsaEncryptorAsString {\n (\n content:string|Uint8Array,\n recipient?:CryptoKey|string,\n aesKey?:SymmKey|Uint8Array|string,\n keysize?:SymmKeyLength\n ):Promise<string>\n}\n\nexport interface EccEncryptorAsString {\n (\n content:string|Uint8Array,\n recipient?:CryptoKey|string,\n info?:string,\n aesKey?:SymmKey|Uint8Array|string,\n keysize?:SymmKeyLength\n ):Promise<string>\n}\n\n// Type helpers to constrain implementations\nexport type RsaKeysType = AbstractKeys & {\n encrypt: RsaEncryptor;\n encryptAsString: RsaEncryptorAsString;\n}\n\nexport type EccKeysType = AbstractKeys & {\n encrypt: EccEncryptor;\n encryptAsString: EccEncryptorAsString;\n}\n\n/**\n * Args to constructor.\n */\nexport type KeyArgs = {\n keys:{ exchange:CryptoKeyPair, write:CryptoKeyPair };\n did:DID;\n hasPersisted:boolean;\n isSessionOnly?:boolean; // in memory only?\n exchangeKeyName?:string;\n writeKeyName?:string;\n}\n\n/**\n * The class that extends AbstractKeys\n */\ninterface ChildKeys<T extends AbstractKeys = AbstractKeys> {\n new (opts:KeyArgs):T;\n _instance:T;\n TYPE:'ecc'|'rsa';\n INFO:string;\n _createExchangeKeys(extractable?:boolean):Promise<CryptoKeyPair>\n _createWriteKeys(extractable?:boolean):Promise<CryptoKeyPair>\n}\n\n/**\n * The parent key. Doesn't implement the encrypt/sign functions.\n */\nexport abstract class AbstractKeys {\n DID:DID\n hasPersisted:boolean\n isSessionOnly:boolean\n /**\n * Have these keys been deleted? A deleted keypair is terminal -- the key\n * material is gone, and any use of it throws.\n */\n destroyed:boolean\n /**\n * The `indexedDB` names this keypair was loaded from, and the names\n * `persist` and `delete` will use.\n */\n readonly exchangeKeyName:string\n readonly writeKeyName:string\n // type:'ecc'|'rsa'\n static EXCHANGE_KEY_NAME:string // needs to be defined by child class\n static WRITE_KEY_NAME:string\n static _instance // a cache for indexedDB\n\n // the key material -- read it via the `exchangeKey`/`writeKey` getters,\n // which throw if this instance was deleted\n protected _exchangeKey:CryptoKeyPair|null\n protected _writeKey:CryptoKeyPair|null\n\n constructor (opts:KeyArgs) {\n const { keys } = opts\n const ctor = this.constructor as typeof AbstractKeys\n this.DID = opts.did\n this._exchangeKey = keys.exchange\n this._writeKey = keys.write\n this.hasPersisted = opts.hasPersisted\n this.isSessionOnly = !!opts.isSessionOnly\n this.destroyed = false\n this.exchangeKeyName = opts.exchangeKeyName || ctor.EXCHANGE_KEY_NAME\n this.writeKeyName = opts.writeKeyName || ctor.WRITE_KEY_NAME\n }\n\n get exchangeKey ():CryptoKeyPair {\n if (this.destroyed || !this._exchangeKey) throw new KeysDeletedError()\n return this._exchangeKey\n }\n\n get writeKey ():CryptoKeyPair {\n if (this.destroyed || !this._writeKey) throw new KeysDeletedError()\n return this._writeKey\n }\n\n /**\n * By default, encrypt the given data to yourself, as a \"note to self\".\n */\n abstract encrypt (\n content:string|Uint8Array,\n recipient?:CryptoKey|string|null,\n aesKeyOrInfo?:SymmKey|Uint8Array|string|null,\n keysizeOrAesKey?:SymmKeyLength|SymmKey|Uint8Array|string|null,\n keysize?:SymmKeyLength|null\n ):Promise<Uint8Array>\n\n abstract encryptAsString (\n content:string|Uint8Array,\n recipient?:CryptoKey|string|null,\n aesKeyOrInfo?:SymmKey|Uint8Array|string|null,\n keysizeOrAesKey?:SymmKeyLength|SymmKey|Uint8Array|string|null,\n keysize?:SymmKeyLength|null\n ):Promise<string>\n\n abstract decrypt(\n msg:string|Uint8Array|ArrayBuffer,\n publicKeyOrKeysize?:CryptoKey|string|SymmKeyLength|null,\n aesAlgorithm?:string|null,\n ):Promise<ArrayBuffer|Uint8Array>\n\n abstract decryptAsString(\n msg:string|Uint8Array|ArrayBuffer,\n publicKeyOrKeysize?:CryptoKey|string|SymmKeyLength|null,\n aesAlgorithm?:string|null,\n ):Promise<string>\n\n abstract sign(msg:Msg, charsize?:CharSize):Promise<Uint8Array>\n abstract signAsString(msg:string, charsize?:CharSize):Promise<string>\n\n publicExchangeKeyAsString (format?:SupportedEncodings):Promise<string> {\n return this.publicExchangeKey.asString(format)\n }\n\n publicWriteKeyAsString (format?:SupportedEncodings):Promise<string> {\n return this.publicWriteKey.asString(format)\n }\n\n get publicWriteKey () {\n const publicKey = this.writeKey.publicKey\n return Object.assign(publicKey, {\n asString: async (format?:SupportedEncodings):Promise<string> => {\n const arrayBuffer = await getPublicKeyAsArrayBuffer(this.writeKey)\n const uint8Array = new Uint8Array(arrayBuffer)\n return format ? toString(uint8Array, format) : toBase64(uint8Array)\n }\n })\n }\n\n get publicExchangeKey () {\n const publicKey = this.exchangeKey.publicKey\n return Object.assign(publicKey, {\n asString: async (format?: SupportedEncodings): Promise<string> => {\n const arrayBuffer = await getPublicKeyAsArrayBuffer(this.exchangeKey)\n const uint8Array = new Uint8Array(arrayBuffer)\n return format ? toString(uint8Array, format) : toBase64(uint8Array)\n }\n })\n }\n\n get privateWriteKey ():CryptoKey {\n return this.writeKey.privateKey\n }\n\n get privateExchangeKey ():CryptoKey {\n return this.exchangeKey.privateKey\n }\n\n /**\n * The machine-readable name for this keypair.\n */\n get deviceName ():Promise<string> {\n return AbstractKeys.deviceName(this.DID)\n }\n\n /**\n * Return a 32-character, DNS-friendly hash of the given DID.\n *\n * @param {DID} did A DID format string\n * @returns {string} 32 character, base32 hash of the DID\n */\n static deviceName (did:DID):Promise<string> {\n return getDeviceName(did)\n }\n\n /**\n * Get the relevant AES key.\n * - if this is an ECC keypair, then use DHKE with the given public key\n * - if this is RSA, use your private key to decrypt the given AES key\n */\n abstract getAesKey (\n publicKey?:CryptoKey|string|null,\n info?:string|null\n ):Promise<CryptoKey>\n\n /**\n * Save this keys instance to `indexedDB`.\n */\n async persist ():Promise<void> {\n if (this.destroyed) throw new KeysDeletedError()\n if (this.isSessionOnly) return\n\n await Promise.all([\n set(this.exchangeKeyName, this.exchangeKey),\n set(this.writeKeyName, this.writeKey)\n ])\n\n this.hasPersisted = true\n }\n\n /**\n * Delete this keypair. This deletes the keys from indexedDB, drops the\n * in-memory key material, and clears the instance cache, so `load` will\n * not hand out these keys again.\n *\n * After this, the instance is `destroyed` -- using it throws\n * a `KeysDeletedError`.\n */\n async delete ():Promise<void> {\n const exchangeName = this.exchangeKeyName\n const writeName = this.writeKeyName\n await delMany([exchangeName, writeName])\n const ctor = this.constructor as typeof AbstractKeys\n ctor._invalidate(exchangeName, writeName)\n this._destroy()\n }\n\n /**\n * Drop the in-memory key material. Internal -- call `delete` instead.\n */\n _destroy ():void {\n this.destroyed = true\n this.hasPersisted = false\n this._exchangeKey = null\n this._writeKey = null\n }\n\n /**\n * If the cached instance was loaded from the given names, then its keys\n * are gone too. Destroy it, and clear the cache. Internal.\n */\n static _invalidate (exchangeName:string, writeName:string):void {\n const cached:AbstractKeys|null = this._instance\n if (!cached) return\n if (cached.exchangeKeyName !== exchangeName) return\n if (cached.writeKeyName !== writeName) return\n cached._destroy()\n this._instance = null\n }\n\n /**\n * Delete a stored keypair without loading it first. Deletes the keys from\n * indexedDB, and destroys the cached instance, if there is one for\n * these names.\n *\n * @param {{ encryptionKeyName, writeKeyName }} [opts] The indexedDB names\n * to delete. Defaults to this class's names.\n */\n static async delete (opts:{\n encryptionKeyName?:string,\n writeKeyName?:string\n } = {}):Promise<void> {\n const exchangeName = opts.encryptionKeyName || this.EXCHANGE_KEY_NAME\n const writeName = opts.writeKeyName || this.WRITE_KEY_NAME\n await delMany([exchangeName, writeName])\n this._invalidate(exchangeName, writeName)\n }\n\n abstract toJson (format?:SupportedEncodings):Promise<{\n DID:DID;\n publicExchangeKey:string;\n }>\n\n /**\n * Return a 32-character, DNS friendly hash of the public signing key.\n *\n * @returns {Promise<string>}\n */\n async getDeviceName ():Promise<string> {\n return AbstractKeys.deviceName(this.DID)\n }\n\n static _createExchangeKeys (_extractable?:boolean):Promise<CryptoKeyPair> {\n throw new Error('The child should implement this')\n }\n\n static _createWriteKeys (_extractable?:boolean):Promise<CryptoKeyPair> {\n throw new Error('The child should implement this')\n }\n\n static async create<T extends AbstractKeys> (\n this:ChildKeys,\n session?:boolean,\n extractable?:boolean,\n keys?:{\n exchangeKeys?:CryptoKeyPair|null,\n writeKeys?:CryptoKeyPair|null,\n }\n ):Promise<T> {\n // encryption\n const exchange = keys?.exchangeKeys || await this._createExchangeKeys(\n extractable\n )\n // signatures\n const write = keys?.writeKeys || await this._createWriteKeys(extractable)\n\n const publicSigningKey = await getPublicKeyAsArrayBuffer(write)\n const did = await publicKeyToDid(\n new Uint8Array(publicSigningKey),\n this.TYPE === 'ecc' ? 'ed25519' : 'rsa'\n )\n\n const keysInstance = new this({\n keys: { exchange, write },\n did,\n hasPersisted: false,\n isSessionOnly: !!session\n })\n\n this._instance = keysInstance\n\n return keysInstance as T\n }\n\n static async exist (opts:{\n encryptionKeyName?:string,\n writeKeyName?:string\n } = {}):Promise<boolean> {\n const keys = await get(opts.writeKeyName || this.WRITE_KEY_NAME)\n const excahngeKeys = await get(opts.encryptionKeyName || this.EXCHANGE_KEY_NAME)\n if (!keys || !excahngeKeys) return false\n return true\n }\n\n /**\n * Restore some keys from indexedDB, or create a new keypair if it doesn't\n * exist yet.\n *\n * @param {{ encryptionKeyName, signingKeyName, session }} opts Strings to\n * use as keys in indexedDB, and a session boolean, is this in memory\n * only? Or can it be persisted.\n * @returns {Promise<AbstractKeys>}\n */\n static async load<T extends AbstractKeys = AbstractKeys> (\n this:ChildKeys & typeof AbstractKeys,\n opts:Partial<{\n encryptionKeyName:string,\n writeKeyName:string,\n session:boolean,\n extractable:boolean,\n }> = {\n session: false,\n }\n ):Promise<T> {\n const exchangeKeyName = opts.encryptionKeyName || this.EXCHANGE_KEY_NAME\n const writeKeyName = opts.writeKeyName || this.WRITE_KEY_NAME\n\n // cache -- only if it is for the same indexedDB entries\n const cached:AbstractKeys|null = this._instance\n if (\n cached &&\n !cached.destroyed &&\n cached.exchangeKeyName === exchangeKeyName &&\n cached.writeKeyName === writeKeyName\n ) {\n return cached as T\n }\n\n let hasPersisted = true\n let exchangeKeys:CryptoKeyPair|undefined = await get(exchangeKeyName)\n let writeKeys:CryptoKeyPair|undefined = await get(writeKeyName)\n\n if (!exchangeKeys) {\n hasPersisted = false\n exchangeKeys = await this._createExchangeKeys(opts.extractable)\n }\n if (!writeKeys) {\n hasPersisted = false\n writeKeys = await this._createWriteKeys(opts.extractable)\n }\n\n const publicSigningKey = await getPublicKeyAsArrayBuffer(writeKeys)\n const did = await publicKeyToDid(\n new Uint8Array(publicSigningKey),\n this.TYPE === 'ecc' ? 'ed25519' : 'rsa'\n )\n\n const keys = new this({\n keys: { exchange: exchangeKeys, write: writeKeys },\n did,\n hasPersisted,\n isSessionOnly: !!opts.session,\n exchangeKeyName,\n writeKeyName\n }) as T\n\n this._instance = keys\n return keys\n }\n}\n\n/**\n * Encrypt the given message to the given public key. If an AES key is not\n * provided, one will be created. Use an AES key to encrypt the given\n * content, then we encrypt the AES key to the given public key.\n *\n * @param {{ content, publicKey }} opts The content to encrypt and\n * public key to encrypt to\n * @param {SymmKey|Uint8Array|string} [aesKey] An optional AES key to encrypt\n * to the given public key\n * @returns {Promise<ArrayBuffer>} The encrypted AES key, concattenated with\n * the encrypted content.\n */\nexport async function encryptTo (\n opts:{ content:string|Uint8Array; publicKey:CryptoKey|string; },\n aesKey?:SymmKey|Uint8Array|string,\n):Promise<ArrayBuffer> {\n const { content, publicKey } = opts\n const key = aesKey || await AES.create()\n const encryptedContent = await AES.encrypt(\n typeof content === 'string' ? fromString(content) : content,\n typeof key === 'string' ? await AES.import(key) : key,\n )\n const encryptedKey = await encryptKeyTo({ key, publicKey })\n\n return joinBufs(encryptedKey, encryptedContent)\n}\n\n/**\n * Encrypt the given AES key to the given public key. Return the encrypted AES\n * key concattenated with the cipher text.\n *\n * @param { content, publicKey } opts The content to encrypt and key to\n * encrypt to.\n * @param {SymmKey|Uint8Array|string} [aesKey] Optional -- the AES key. One will\n * be created if not passed in.\n * @returns {Promise<string>} The encrypted AES key concattenated with the\n * cipher text.\n */\nencryptTo.asString = async function (\n opts:{ content:string|Uint8Array; publicKey:CryptoKey|string },\n aesKey?:SymmKey|Uint8Array|string\n):Promise<string> {\n const joined = await encryptTo(opts, aesKey)\n return toString(new Uint8Array(joined), 'base64pad')\n}\n\nexport async function encryptKeyTo ({ key, publicKey }:{\n key:string|Uint8Array|CryptoKey;\n publicKey:CryptoKey|Uint8Array|string;\n}, format:'arraybuffer'):Promise<ArrayBuffer>\n\nexport async function encryptKeyTo ({ key, publicKey }:{\n key:string|Uint8Array|CryptoKey;\n publicKey:CryptoKey|Uint8Array|string;\n}, format:'uint8array'):Promise<Uint8Array>\n\nexport async function encryptKeyTo ({ key, publicKey }:{\n key:string|Uint8Array|CryptoKey;\n publicKey:CryptoKey|Uint8Array|string;\n}, format?:undefined):Promise<Uint8Array>\n\n/**\n * Encrypt the given content to the given public key. This is RSA encryption,\n * and should be used only to encrypt AES keys.\n *\n * @param {{ content, publicKey }} params The content to encrypt, and public key\n * to encrypt it to.\n * @returns {Promise<Uint8Array>}\n */\nexport async function encryptKeyTo ({ key, publicKey }:{\n key:string|Uint8Array|CryptoKey;\n publicKey:CryptoKey|Uint8Array|string;\n}, format?:'uint8array'|'arraybuffer'):Promise<Uint8Array|ArrayBuffer> {\n let _key:Uint8Array|string\n if (key instanceof CryptoKey) {\n _key = await AES.export(key)\n } else {\n _key = key\n }\n\n const buf = await rsaOperations.encrypt(_key, publicKey)\n if (format && format === 'arraybuffer') return buf\n return new Uint8Array(buf)\n}\n\nencryptKeyTo.asString = async function ({ key, publicKey }:{\n key:string|Uint8Array|CryptoKey;\n publicKey:CryptoKey|string|Uint8Array;\n}, format?:SupportedEncodings):Promise<string> {\n const asArr = await encryptKeyTo({ key, publicKey })\n return format ? toString(asArr, format) : toBase64(asArr)\n}\n\n// /**\n// * Decrypt the given message with the given key. We expect the `iv` to be\n// * prefixed to the encrypted message.\n// * @param msg The message to decrypt\n// * @param key The key to decrypt with\n// * @param opts Optional args for algorithm and stuff\n// * @returns {Promise<ArrayBuffer>}\n// */\n// async function decryptBytes (\n// msg:Msg,\n// key:CryptoKey|string,\n// opts?:Partial<{\n// alg:SymmAlgorithm;\n// length: SymmKeyLength;\n// iv: ArrayBuffer;\n// }>\n// ):Promise<ArrayBuffer> {\n// const cipherText = normalizeBase64ToBuf(msg)\n// const importedKey = typeof key === 'string' ?\n// await importKey(key, opts) :\n// key\n// // `iv` is prefixed to the cypher text\n// const iv = cipherText.slice(0, IV_LENGTH)\n// const cipherBytes = cipherText.slice(IV_LENGTH)\n// const msgBuff = await webcrypto.subtle.decrypt({\n// name: DEFAULT_SYMM_ALGORITHM,\n// iv\n// }, importedKey, cipherBytes)\n\n// return msgBuff\n// }\n\nexport async function getDeviceName (did:DID|string) {\n const hashedUsername = await sha256(\n new TextEncoder().encode(did.normalize('NFD'))\n )\n\n return toString(hashedUsername, 'base32').slice(0, 32)\n}\n"],
|
|
5
|
-
"mappings": ";;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,yBAA8D;AAC9D,wBAAkC;AAQlC,iBAAoB;AACpB,kBAIO;AACP,oBAIO;AACP,oBAAiC;AAIjC,yBAAc,2BAxBd;AA0BA,IAAAA,iBAAyC;AAkFlC,MAAe,gBAAf,MAAe,cAAa;AAAA,
|
|
4
|
+
"sourcesContent": ["import { fromString, type SupportedEncodings, toString } from 'uint8arrays'\nimport { get, set, delMany } from 'idb-keyval'\nimport type {\n CharSize,\n Msg,\n SymmKeyLength,\n DID,\n SymmKey,\n} from './types.js'\nimport { AES } from './aes/index.js'\nimport {\n toBase64,\n joinBufs,\n sha256,\n} from './util.js'\nimport {\n rsaOperations,\n publicKeyToDid,\n getPublicKeyAsArrayBuffer,\n} from './crypto.js'\nimport { KeysDeletedError } from './errors.js'\n\nexport { publicKeyToDid, getPublicKeyAsArrayBuffer }\nexport { KeysDeletedError }\nexport * from './constants.js'\nexport type { DID }\nexport { getPublicKeyAsUint8Array } from './crypto.js'\nexport type SerializedKeys = {\n DID:DID;\n pulicExchangeKey:string;\n}\n\nexport interface RsaEncryptor {\n (\n content:string|Uint8Array,\n recipient?:CryptoKey|string,\n aesKey?:SymmKey|Uint8Array|string,\n keysize?:SymmKeyLength\n ):Promise<Uint8Array>\n}\n\nexport interface EccEncryptor {\n (\n content:string|Uint8Array,\n recipient?:CryptoKey|string,\n info?:string,\n aesKey?:SymmKey|Uint8Array|string,\n keysize?:SymmKeyLength\n ):Promise<Uint8Array>\n}\n\nexport interface RsaEncryptorAsString {\n (\n content:string|Uint8Array,\n recipient?:CryptoKey|string,\n aesKey?:SymmKey|Uint8Array|string,\n keysize?:SymmKeyLength\n ):Promise<string>\n}\n\nexport interface EccEncryptorAsString {\n (\n content:string|Uint8Array,\n recipient?:CryptoKey|string,\n info?:string,\n aesKey?:SymmKey|Uint8Array|string,\n keysize?:SymmKeyLength\n ):Promise<string>\n}\n\n// Type helpers to constrain implementations\nexport type RsaKeysType = AbstractKeys & {\n encrypt: RsaEncryptor;\n encryptAsString: RsaEncryptorAsString;\n}\n\nexport type EccKeysType = AbstractKeys & {\n encrypt: EccEncryptor;\n encryptAsString: EccEncryptorAsString;\n}\n\n/**\n * Args to constructor.\n */\nexport type KeyArgs = {\n keys:{ exchange:CryptoKeyPair, write:CryptoKeyPair };\n did:DID;\n hasPersisted:boolean;\n isSessionOnly?:boolean; // in memory only?\n exchangeKeyName?:string;\n writeKeyName?:string;\n}\n\n/**\n * The class that extends AbstractKeys\n */\ninterface ChildKeys<T extends AbstractKeys = AbstractKeys> {\n new (opts:KeyArgs):T;\n _instance:T;\n TYPE:'ecc'|'rsa';\n INFO:string;\n _createExchangeKeys(extractable?:boolean):Promise<CryptoKeyPair>\n _createWriteKeys(extractable?:boolean):Promise<CryptoKeyPair>\n}\n\n/**\n * The parent key. Doesn't implement the encrypt/sign functions.\n */\nexport abstract class AbstractKeys {\n DID:DID\n hasPersisted:boolean\n isSessionOnly:boolean\n /**\n * Have these keys been deleted? A deleted keypair is terminal -- the key\n * material is gone, and any use of it throws.\n */\n destroyed:boolean\n /**\n * The `indexedDB` names this keypair was loaded from, and the names\n * `persist` and `delete` will use.\n */\n readonly exchangeKeyName:string\n readonly writeKeyName:string\n static EXCHANGE_KEY_NAME:string // needs to be defined by child class\n static WRITE_KEY_NAME:string\n static _instance // a cache for indexedDB\n\n // the key material -- read it via the `exchangeKey`/`writeKey` getters,\n // which throw if this instance was deleted\n protected _exchangeKey:CryptoKeyPair|null\n protected _writeKey:CryptoKeyPair|null\n\n constructor (opts:KeyArgs) {\n const { keys } = opts\n const ctor = this.constructor as typeof AbstractKeys\n this.DID = opts.did\n this._exchangeKey = keys.exchange\n this._writeKey = keys.write\n this.hasPersisted = opts.hasPersisted\n this.isSessionOnly = !!opts.isSessionOnly\n this.destroyed = false\n this.exchangeKeyName = opts.exchangeKeyName || ctor.EXCHANGE_KEY_NAME\n this.writeKeyName = opts.writeKeyName || ctor.WRITE_KEY_NAME\n }\n\n get exchangeKey ():CryptoKeyPair {\n if (this.destroyed || !this._exchangeKey) throw new KeysDeletedError()\n return this._exchangeKey\n }\n\n get writeKey ():CryptoKeyPair {\n if (this.destroyed || !this._writeKey) throw new KeysDeletedError()\n return this._writeKey\n }\n\n /**\n * By default, encrypt the given data to yourself, as a \"note to self\".\n */\n abstract encrypt (\n content:string|Uint8Array,\n recipient?:CryptoKey|string|null,\n aesKeyOrInfo?:SymmKey|Uint8Array|string|null,\n keysizeOrAesKey?:SymmKeyLength|SymmKey|Uint8Array|string|null,\n keysize?:SymmKeyLength|null\n ):Promise<Uint8Array>\n\n abstract encryptAsString (\n content:string|Uint8Array,\n recipient?:CryptoKey|string|null,\n aesKeyOrInfo?:SymmKey|Uint8Array|string|null,\n keysizeOrAesKey?:SymmKeyLength|SymmKey|Uint8Array|string|null,\n keysize?:SymmKeyLength|null\n ):Promise<string>\n\n abstract decrypt(\n msg:string|Uint8Array|ArrayBuffer,\n publicKeyOrKeysize?:CryptoKey|string|SymmKeyLength|null,\n aesAlgorithm?:string|null,\n ):Promise<ArrayBuffer|Uint8Array>\n\n abstract decryptAsString(\n msg:string|Uint8Array|ArrayBuffer,\n publicKeyOrKeysize?:CryptoKey|string|SymmKeyLength|null,\n aesAlgorithm?:string|null,\n ):Promise<string>\n\n abstract sign(msg:Msg, charsize?:CharSize):Promise<Uint8Array>\n abstract signAsString(msg:string, charsize?:CharSize):Promise<string>\n\n publicExchangeKeyAsString (format?:SupportedEncodings):Promise<string> {\n return this.publicExchangeKey.asString(format)\n }\n\n publicWriteKeyAsString (format?:SupportedEncodings):Promise<string> {\n return this.publicWriteKey.asString(format)\n }\n\n get publicWriteKey () {\n const publicKey = this.writeKey.publicKey\n return Object.assign(publicKey, {\n asString: async (format?:SupportedEncodings):Promise<string> => {\n const arrayBuffer = await getPublicKeyAsArrayBuffer(this.writeKey)\n const uint8Array = new Uint8Array(arrayBuffer)\n return format ? toString(uint8Array, format) : toBase64(uint8Array)\n }\n })\n }\n\n get publicExchangeKey () {\n const publicKey = this.exchangeKey.publicKey\n return Object.assign(publicKey, {\n asString: async (format?: SupportedEncodings): Promise<string> => {\n const arrayBuffer = await getPublicKeyAsArrayBuffer(this.exchangeKey)\n const uint8Array = new Uint8Array(arrayBuffer)\n return format ? toString(uint8Array, format) : toBase64(uint8Array)\n }\n })\n }\n\n get privateWriteKey ():CryptoKey {\n return this.writeKey.privateKey\n }\n\n get privateExchangeKey ():CryptoKey {\n return this.exchangeKey.privateKey\n }\n\n /**\n * The machine-readable name for this keypair.\n */\n get deviceName ():Promise<string> {\n return AbstractKeys.deviceName(this.DID)\n }\n\n /**\n * Return a 32-character, DNS-friendly hash of the given DID.\n *\n * @param {DID} did A DID format string\n * @returns {string} 32 character, base32 hash of the DID\n */\n static deviceName (did:DID):Promise<string> {\n return getDeviceName(did)\n }\n\n /**\n * Get the relevant AES key.\n * - if this is an ECC keypair, then use DHKE with the given public key\n * - if this is RSA, use your private key to decrypt the given AES key\n */\n abstract getAesKey (\n publicKey?:CryptoKey|string|null,\n info?:string|null\n ):Promise<CryptoKey>\n\n /**\n * Save this keys instance to `indexedDB`.\n */\n async persist ():Promise<void> {\n if (this.destroyed) throw new KeysDeletedError()\n if (this.isSessionOnly) return\n\n await Promise.all([\n set(this.exchangeKeyName, this.exchangeKey),\n set(this.writeKeyName, this.writeKey)\n ])\n\n this.hasPersisted = true\n }\n\n /**\n * Delete this keypair. This deletes the keys from indexedDB, drops the\n * in-memory key material, and clears the instance cache, so `load` will\n * not hand out these keys again.\n *\n * After this, the instance is `destroyed` -- using it throws\n * a `KeysDeletedError`.\n */\n async delete ():Promise<void> {\n const exchangeName = this.exchangeKeyName\n const writeName = this.writeKeyName\n await delMany([exchangeName, writeName])\n const ctor = this.constructor as typeof AbstractKeys\n ctor._invalidate(exchangeName, writeName)\n this._destroy()\n }\n\n /**\n * Drop the in-memory key material. Internal -- call `delete` instead.\n */\n _destroy ():void {\n this.destroyed = true\n this.hasPersisted = false\n this._exchangeKey = null\n this._writeKey = null\n }\n\n /**\n * If the cached instance was loaded from the given names, then its keys\n * are gone too. Destroy it, and clear the cache. Internal.\n */\n static _invalidate (exchangeName:string, writeName:string):void {\n const cached:AbstractKeys|null = this._instance\n if (!cached) return\n if (cached.exchangeKeyName !== exchangeName) return\n if (cached.writeKeyName !== writeName) return\n cached._destroy()\n this._instance = null\n }\n\n /**\n * Delete a stored keypair without loading it first. Deletes the keys from\n * indexedDB, and destroys the cached instance, if there is one for\n * these names.\n *\n * @param {{ encryptionKeyName, writeKeyName }} [opts] The indexedDB names\n * to delete. Defaults to this class's names.\n */\n static async delete (opts:{\n encryptionKeyName?:string,\n writeKeyName?:string\n } = {}):Promise<void> {\n const exchangeName = opts.encryptionKeyName || this.EXCHANGE_KEY_NAME\n const writeName = opts.writeKeyName || this.WRITE_KEY_NAME\n await delMany([exchangeName, writeName])\n this._invalidate(exchangeName, writeName)\n }\n\n abstract toJson (format?:SupportedEncodings):Promise<{\n DID:DID;\n publicExchangeKey:string;\n }>\n\n /**\n * Return a 32-character, DNS friendly hash of the public signing key.\n *\n * @returns {Promise<string>}\n */\n async getDeviceName ():Promise<string> {\n return AbstractKeys.deviceName(this.DID)\n }\n\n static _createExchangeKeys (_extractable?:boolean):Promise<CryptoKeyPair> {\n throw new Error('The child should implement this')\n }\n\n static _createWriteKeys (_extractable?:boolean):Promise<CryptoKeyPair> {\n throw new Error('The child should implement this')\n }\n\n static async create<T extends AbstractKeys> (\n this:ChildKeys,\n session?:boolean,\n extractable?:boolean,\n keys?:{\n exchangeKeys?:CryptoKeyPair|null,\n writeKeys?:CryptoKeyPair|null,\n dbKey?:string,\n }\n ):Promise<T> {\n // encryption\n const exchange = keys?.exchangeKeys || await this._createExchangeKeys(\n extractable\n )\n // signatures\n const write = keys?.writeKeys || await this._createWriteKeys(extractable)\n\n const publicSigningKey = await getPublicKeyAsArrayBuffer(write)\n const did = await publicKeyToDid(\n new Uint8Array(publicSigningKey),\n this.TYPE === 'ecc' ? 'ed25519' : 'rsa'\n )\n\n const keysInstance = new this({\n keys: { exchange, write },\n did,\n hasPersisted: false,\n isSessionOnly: !!session\n })\n\n this._instance = keysInstance\n\n return keysInstance as T\n }\n\n static async exist (opts:{\n encryptionKeyName?:string,\n writeKeyName?:string\n } = {}):Promise<boolean> {\n const keys = await get(opts.writeKeyName || this.WRITE_KEY_NAME)\n const excahngeKeys = await get(opts.encryptionKeyName || this.EXCHANGE_KEY_NAME)\n if (!keys || !excahngeKeys) return false\n return true\n }\n\n /**\n * Restore some keys from indexedDB, or create a new keypair if it doesn't\n * exist yet.\n *\n * By default this saves the keys to `indexedDB` before returning, so the\n * keypair you get back survives a page reload. Pass `{ persist: false }`\n * to skip that and call `.persist()` yourself. Session keys are never\n * written to `indexedDB`, so `{ session: true }` wins over `persist`.\n *\n * @param {{ encryptionKeyName, signingKeyName, session, persist }} opts\n * Strings to use as keys in indexedDB, a session boolean -- is this in\n * memory only? Or can it be persisted -- and a persist boolean.\n * @returns {Promise<AbstractKeys>}\n */\n static async load<T extends AbstractKeys = AbstractKeys> (\n this:ChildKeys & typeof AbstractKeys,\n opts:Partial<{\n encryptionKeyName:string,\n writeKeyName:string,\n session:boolean,\n extractable:boolean,\n persist:boolean,\n }> = {\n session: false,\n }\n ):Promise<T> {\n const shouldPersist = opts.persist !== false\n const exchangeKeyName = opts.encryptionKeyName || this.EXCHANGE_KEY_NAME\n const writeKeyName = opts.writeKeyName || this.WRITE_KEY_NAME\n\n // cache -- only if it is for the same indexedDB entries\n const cached:AbstractKeys|null = this._instance\n if (\n cached &&\n !cached.destroyed &&\n cached.exchangeKeyName === exchangeKeyName &&\n cached.writeKeyName === writeKeyName\n ) {\n if (shouldPersist && !cached.hasPersisted) await cached.persist()\n return cached as T\n }\n\n let hasPersisted = true\n let exchangeKeys:CryptoKeyPair|undefined = await get(exchangeKeyName)\n let writeKeys:CryptoKeyPair|undefined = await get(writeKeyName)\n\n if (!exchangeKeys) {\n hasPersisted = false\n exchangeKeys = await this._createExchangeKeys(opts.extractable)\n }\n if (!writeKeys) {\n hasPersisted = false\n writeKeys = await this._createWriteKeys(opts.extractable)\n }\n\n const publicSigningKey = await getPublicKeyAsArrayBuffer(writeKeys)\n const did = await publicKeyToDid(\n new Uint8Array(publicSigningKey),\n this.TYPE === 'ecc' ? 'ed25519' : 'rsa'\n )\n\n const keys = new this({\n keys: { exchange: exchangeKeys, write: writeKeys },\n did,\n hasPersisted,\n isSessionOnly: !!opts.session,\n exchangeKeyName,\n writeKeyName\n }) as T\n\n this._instance = keys\n if (shouldPersist && !hasPersisted) await keys.persist()\n return keys\n }\n}\n\n/**\n * Encrypt the given message to the given public key. If an AES key is not\n * provided, one will be created. Use an AES key to encrypt the given\n * content, then we encrypt the AES key to the given public key.\n *\n * @param {{ content, publicKey }} opts The content to encrypt and\n * public key to encrypt to\n * @param {SymmKey|Uint8Array|string} [aesKey] An optional AES key to encrypt\n * to the given public key\n * @returns {Promise<ArrayBuffer>} The encrypted AES key, concattenated with\n * the encrypted content.\n */\nexport async function encryptTo (\n opts:{ content:string|Uint8Array; publicKey:CryptoKey|string; },\n aesKey?:SymmKey|Uint8Array|string,\n):Promise<ArrayBuffer> {\n const { content, publicKey } = opts\n const key = aesKey || await AES.create()\n const encryptedContent = await AES.encrypt(\n typeof content === 'string' ? fromString(content) : content,\n typeof key === 'string' ? await AES.import(key) : key,\n )\n const encryptedKey = await encryptKeyTo({ key, publicKey })\n\n return joinBufs(encryptedKey, encryptedContent)\n}\n\n/**\n * Encrypt the given AES key to the given public key. Return the encrypted AES\n * key concattenated with the cipher text.\n *\n * @param { content, publicKey } opts The content to encrypt and key to\n * encrypt to.\n * @param {SymmKey|Uint8Array|string} [aesKey] Optional -- the AES key. One will\n * be created if not passed in.\n * @returns {Promise<string>} The encrypted AES key concattenated with the\n * cipher text.\n */\nencryptTo.asString = async function (\n opts:{ content:string|Uint8Array; publicKey:CryptoKey|string },\n aesKey?:SymmKey|Uint8Array|string\n):Promise<string> {\n const joined = await encryptTo(opts, aesKey)\n return toString(new Uint8Array(joined), 'base64pad')\n}\n\nexport async function encryptKeyTo ({ key, publicKey }:{\n key:string|Uint8Array|CryptoKey;\n publicKey:CryptoKey|Uint8Array|string;\n}, format:'arraybuffer'):Promise<ArrayBuffer>\n\nexport async function encryptKeyTo ({ key, publicKey }:{\n key:string|Uint8Array|CryptoKey;\n publicKey:CryptoKey|Uint8Array|string;\n}, format:'uint8array'):Promise<Uint8Array>\n\nexport async function encryptKeyTo ({ key, publicKey }:{\n key:string|Uint8Array|CryptoKey;\n publicKey:CryptoKey|Uint8Array|string;\n}, format?:undefined):Promise<Uint8Array>\n\n/**\n * Encrypt the given content to the given public key. This is RSA encryption,\n * and should be used only to encrypt AES keys.\n *\n * @param {{ content, publicKey }} params The content to encrypt, and public key\n * to encrypt it to.\n * @returns {Promise<Uint8Array>}\n */\nexport async function encryptKeyTo ({ key, publicKey }:{\n key:string|Uint8Array|CryptoKey;\n publicKey:CryptoKey|Uint8Array|string;\n}, format?:'uint8array'|'arraybuffer'):Promise<Uint8Array|ArrayBuffer> {\n let _key:Uint8Array|string\n if (key instanceof CryptoKey) {\n _key = await AES.export(key)\n } else {\n _key = key\n }\n\n const buf = await rsaOperations.encrypt(_key, publicKey)\n if (format && format === 'arraybuffer') return buf\n return new Uint8Array(buf)\n}\n\nencryptKeyTo.asString = async function ({ key, publicKey }:{\n key:string|Uint8Array|CryptoKey;\n publicKey:CryptoKey|string|Uint8Array;\n}, format?:SupportedEncodings):Promise<string> {\n const asArr = await encryptKeyTo({ key, publicKey })\n return format ? toString(asArr, format) : toBase64(asArr)\n}\n\n// /**\n// * Decrypt the given message with the given key. We expect the `iv` to be\n// * prefixed to the encrypted message.\n// * @param msg The message to decrypt\n// * @param key The key to decrypt with\n// * @param opts Optional args for algorithm and stuff\n// * @returns {Promise<ArrayBuffer>}\n// */\n// async function decryptBytes (\n// msg:Msg,\n// key:CryptoKey|string,\n// opts?:Partial<{\n// alg:SymmAlgorithm;\n// length: SymmKeyLength;\n// iv: ArrayBuffer;\n// }>\n// ):Promise<ArrayBuffer> {\n// const cipherText = normalizeBase64ToBuf(msg)\n// const importedKey = typeof key === 'string' ?\n// await importKey(key, opts) :\n// key\n// // `iv` is prefixed to the cypher text\n// const iv = cipherText.slice(0, IV_LENGTH)\n// const cipherBytes = cipherText.slice(IV_LENGTH)\n// const msgBuff = await webcrypto.subtle.decrypt({\n// name: DEFAULT_SYMM_ALGORITHM,\n// iv\n// }, importedKey, cipherBytes)\n\n// return msgBuff\n// }\n\nexport async function getDeviceName (did:DID|string) {\n const hashedUsername = await sha256(\n new TextEncoder().encode(did.normalize('NFD'))\n )\n\n return toString(hashedUsername, 'base32').slice(0, 32)\n}\n"],
|
|
5
|
+
"mappings": ";;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,yBAA8D;AAC9D,wBAAkC;AAQlC,iBAAoB;AACpB,kBAIO;AACP,oBAIO;AACP,oBAAiC;AAIjC,yBAAc,2BAxBd;AA0BA,IAAAA,iBAAyC;AAkFlC,MAAe,gBAAf,MAAe,cAAa;AAAA,EAwB/B,YAAa,MAAc;AAvB3B;AACA;AACA;AAKA;AAAA;AAAA;AAAA;AAAA;AAKA;AAAA;AAAA;AAAA;AAAA,wBAAS;AACT,wBAAS;AAOT;AAAA;AAAA;AAAA,wBAAU;AACV,wBAAU;AAGN,UAAM,EAAE,KAAK,IAAI;AACjB,UAAM,OAAO,KAAK;AAClB,SAAK,MAAM,KAAK;AAChB,SAAK,eAAe,KAAK;AACzB,SAAK,YAAY,KAAK;AACtB,SAAK,eAAe,KAAK;AACzB,SAAK,gBAAgB,CAAC,CAAC,KAAK;AAC5B,SAAK,YAAY;AACjB,SAAK,kBAAkB,KAAK,mBAAmB,KAAK;AACpD,SAAK,eAAe,KAAK,gBAAgB,KAAK;AAAA,EAClD;AAAA,EAEA,IAAI,cAA6B;AAC7B,QAAI,KAAK,aAAa,CAAC,KAAK,aAAc,OAAM,IAAI,+BAAiB;AACrE,WAAO,KAAK;AAAA,EAChB;AAAA,EAEA,IAAI,WAA0B;AAC1B,QAAI,KAAK,aAAa,CAAC,KAAK,UAAW,OAAM,IAAI,+BAAiB;AAClE,WAAO,KAAK;AAAA,EAChB;AAAA,EAoCA,0BAA2B,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AAG,WAAW;AACvD;AAyBA,eAAsB,aAAc,EAAE,KAAK,UAAU,GAGlD,QAAoE;AACnE,MAAI;AACJ,MAAI,eAAe,WAAW;AAC1B,WAAO,MAAM,eAAI,OAAO,GAAG;AAAA,EAC/B,OAAO;AACH,WAAO;AAAA,EACX;AAEA,QAAM,MAAM,MAAM,4BAAc,QAAQ,MAAM,SAAS;AACvD,MAAI,UAAU,WAAW,cAAe,QAAO;AAC/C,SAAO,IAAI,WAAW,GAAG;AAC7B;AAdsB;AAgBtB,aAAa,WAAW,eAAgB,EAAE,KAAK,UAAU,GAGtD,QAA4C;AAC3C,QAAM,QAAQ,MAAM,aAAa,EAAE,KAAK,UAAU,CAAC;AACnD,SAAO,aAAS,6BAAS,OAAO,MAAM,QAAI,sBAAS,KAAK;AAC5D;AAkCA,eAAsB,cAAe,KAAgB;AACjD,QAAM,iBAAiB,UAAM;AAAA,IACzB,IAAI,YAAY,EAAE,OAAO,IAAI,UAAU,KAAK,CAAC;AAAA,EACjD;AAEA,aAAO,6BAAS,gBAAgB,QAAQ,EAAE,MAAM,GAAG,EAAE;AACzD;AANsB;",
|
|
6
6
|
"names": ["import_crypto"]
|
|
7
7
|
}
|
package/dist/_base.d.ts
CHANGED
|
@@ -167,6 +167,7 @@ export declare abstract class AbstractKeys {
|
|
|
167
167
|
static create<T extends AbstractKeys>(this: ChildKeys, session?: boolean, extractable?: boolean, keys?: {
|
|
168
168
|
exchangeKeys?: CryptoKeyPair | null;
|
|
169
169
|
writeKeys?: CryptoKeyPair | null;
|
|
170
|
+
dbKey?: string;
|
|
170
171
|
}): Promise<T>;
|
|
171
172
|
static exist(opts?: {
|
|
172
173
|
encryptionKeyName?: string;
|
|
@@ -176,9 +177,14 @@ export declare abstract class AbstractKeys {
|
|
|
176
177
|
* Restore some keys from indexedDB, or create a new keypair if it doesn't
|
|
177
178
|
* exist yet.
|
|
178
179
|
*
|
|
179
|
-
*
|
|
180
|
-
*
|
|
181
|
-
*
|
|
180
|
+
* By default this saves the keys to `indexedDB` before returning, so the
|
|
181
|
+
* keypair you get back survives a page reload. Pass `{ persist: false }`
|
|
182
|
+
* to skip that and call `.persist()` yourself. Session keys are never
|
|
183
|
+
* written to `indexedDB`, so `{ session: true }` wins over `persist`.
|
|
184
|
+
*
|
|
185
|
+
* @param {{ encryptionKeyName, signingKeyName, session, persist }} opts
|
|
186
|
+
* Strings to use as keys in indexedDB, a session boolean -- is this in
|
|
187
|
+
* memory only? Or can it be persisted -- and a persist boolean.
|
|
182
188
|
* @returns {Promise<AbstractKeys>}
|
|
183
189
|
*/
|
|
184
190
|
static load<T extends AbstractKeys = AbstractKeys>(this: ChildKeys & typeof AbstractKeys, opts?: Partial<{
|
|
@@ -186,6 +192,7 @@ export declare abstract class AbstractKeys {
|
|
|
186
192
|
writeKeyName: string;
|
|
187
193
|
session: boolean;
|
|
188
194
|
extractable: boolean;
|
|
195
|
+
persist: boolean;
|
|
189
196
|
}>): Promise<T>;
|
|
190
197
|
}
|
|
191
198
|
/**
|
package/dist/_base.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"_base.d.ts","sourceRoot":"","sources":["../src/_base.ts"],"names":[],"mappings":"AAAA,OAAO,EAAc,KAAK,kBAAkB,EAAY,MAAM,aAAa,CAAA;AAE3E,OAAO,KAAK,EACR,QAAQ,EACR,GAAG,EACH,aAAa,EACb,GAAG,EACH,OAAO,EACV,MAAM,YAAY,CAAA;AAOnB,OAAO,EAEH,cAAc,EACd,yBAAyB,EAC5B,MAAM,aAAa,CAAA;AACpB,OAAO,EAAE,gBAAgB,EAAE,MAAM,aAAa,CAAA;AAE9C,OAAO,EAAE,cAAc,EAAE,yBAAyB,EAAE,CAAA;AACpD,OAAO,EAAE,gBAAgB,EAAE,CAAA;AAC3B,cAAc,gBAAgB,CAAA;AAC9B,YAAY,EAAE,GAAG,EAAE,CAAA;AACnB,OAAO,EAAE,wBAAwB,EAAE,MAAM,aAAa,CAAA;AACtD,MAAM,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AM,GAAC,UAAU,GAAC,SAAS,CAAC;IAChC,SAAS,EAAC,SAAS,GAAC,UAAU,GAAC,MAAM,CAAC;CACzC,EAAE,MAAM,EAAC,YAAY,GAAE,OAAO,CAAC,UAAU,CAAC,CAAA;AAE3C,wBAAsB,YAAY,CAAE,EAAE,GAAG,EAAE,SAAS,EAAE,EAAC;IACnD,GAAG,EAAC,MAAM,GAAC,UAAU,GAAC,SAAS,CAAC;IAChC,SAAS,EAAC,SAAS,GAAC,UAAU,GAAC,MAAM,CAAC;CACzC,EAAE,MAAM,CAAC,EAAC,SAAS,GAAE,OAAO,CAAC,UAAU,CAAC,CAAA;yBAHnB,YAAY;uCA6ByB;QACvD,GAAG,EAAC,MAAM,GAAC,UAAU,GAAC,SAAS,CAAC;QAChC,SAAS,EAAC,SAAS,GAAC,MAAM,GAAC,UAAU,CAAC;KACzC,WAAU,kBAAkB,KAAE,OAAO,CAAC,MAAM,CAAC;;AAqC9C,wBAAsB,aAAa,CAAE,GAAG,EAAC,GAAG,GAAC,MAAM,mBAMlD"}
|
package/dist/_base.js
CHANGED
|
@@ -211,18 +211,25 @@ const _AbstractKeys = class _AbstractKeys {
|
|
|
211
211
|
* Restore some keys from indexedDB, or create a new keypair if it doesn't
|
|
212
212
|
* exist yet.
|
|
213
213
|
*
|
|
214
|
-
*
|
|
215
|
-
*
|
|
216
|
-
*
|
|
214
|
+
* By default this saves the keys to `indexedDB` before returning, so the
|
|
215
|
+
* keypair you get back survives a page reload. Pass `{ persist: false }`
|
|
216
|
+
* to skip that and call `.persist()` yourself. Session keys are never
|
|
217
|
+
* written to `indexedDB`, so `{ session: true }` wins over `persist`.
|
|
218
|
+
*
|
|
219
|
+
* @param {{ encryptionKeyName, signingKeyName, session, persist }} opts
|
|
220
|
+
* Strings to use as keys in indexedDB, a session boolean -- is this in
|
|
221
|
+
* memory only? Or can it be persisted -- and a persist boolean.
|
|
217
222
|
* @returns {Promise<AbstractKeys>}
|
|
218
223
|
*/
|
|
219
224
|
static async load(opts = {
|
|
220
225
|
session: false
|
|
221
226
|
}) {
|
|
227
|
+
const shouldPersist = opts.persist !== false;
|
|
222
228
|
const exchangeKeyName = opts.encryptionKeyName || this.EXCHANGE_KEY_NAME;
|
|
223
229
|
const writeKeyName = opts.writeKeyName || this.WRITE_KEY_NAME;
|
|
224
230
|
const cached = this._instance;
|
|
225
231
|
if (cached && !cached.destroyed && cached.exchangeKeyName === exchangeKeyName && cached.writeKeyName === writeKeyName) {
|
|
232
|
+
if (shouldPersist && !cached.hasPersisted) await cached.persist();
|
|
226
233
|
return cached;
|
|
227
234
|
}
|
|
228
235
|
let hasPersisted = true;
|
|
@@ -250,11 +257,11 @@ const _AbstractKeys = class _AbstractKeys {
|
|
|
250
257
|
writeKeyName
|
|
251
258
|
});
|
|
252
259
|
this._instance = keys;
|
|
260
|
+
if (shouldPersist && !hasPersisted) await keys.persist();
|
|
253
261
|
return keys;
|
|
254
262
|
}
|
|
255
263
|
};
|
|
256
264
|
__name(_AbstractKeys, "AbstractKeys");
|
|
257
|
-
// type:'ecc'|'rsa'
|
|
258
265
|
__publicField(_AbstractKeys, "EXCHANGE_KEY_NAME");
|
|
259
266
|
// needs to be defined by child class
|
|
260
267
|
__publicField(_AbstractKeys, "WRITE_KEY_NAME");
|