generated from fission-codes/project-template
-
Notifications
You must be signed in to change notification settings - Fork 24
/
index.ts
965 lines (780 loc) · 31.5 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
/*
%@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@%
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
%@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@%
@@@@@% %@@@@@@% %@@@@@@@% %@@@@@
@@@@@ @@@@@% @@@@@@ @@@@@
@@@@@% @@@@@ %@@@@@ %@@@@@
@@@@@@% @@@@@ %@@% @@@@@ %@@@@@@
@@@@@@@ @@@@@ %@@@@% @@@@@ @@@@@@@
@@@@@@@ @@@@% @@@@@@ @@@@@ @@@@@@@
@@@@@@@ %@@@@ @@@@@@ @@@@@% @@@@@@@
@@@@@@@ @@@@@ @@@@@@ %@@@@@ @@@@@@@
@@@@@@@ @@@@@@@@@@@@@@@@ @@@@@ @@@@@@@
@@@@@@@ %@@@@@@@@@@@@@@@ @@@@% @@@@@@@
@@@@@@@ %@@% @@@@@@ %@@% @@@@@@@
@@@@@@@ @@@@@@ @@@@@@@
@@@@@@@% %@@@@@@% %@@@@@@@
@@@@@@@@@% %@@@@@@@@@@% %@@@@@@@@@
%@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@%
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
%@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@%
*/
import * as Uint8arrays from "uint8arrays"
import localforage from "localforage"
import * as Auth from "./components/auth/implementation.js"
import * as CapabilitiesImpl from "./components/capabilities/implementation.js"
import * as Capabilities from "./capabilities.js"
import * as Crypto from "./components/crypto/implementation.js"
import * as Depot from "./components/depot/implementation.js"
import * as DID from "./did/local.js"
import * as Events from "./events.js"
import * as Extension from "./extension/index.js"
import * as FileSystemData from "./fs/data.js"
import * as IpfsNode from "./components/depot/implementation/ipfs/node.js"
import * as Manners from "./components/manners/implementation.js"
import * as Reference from "./components/reference/implementation.js"
import * as RootKey from "./common/root-key.js"
import * as Semver from "./common/semver.js"
import * as SessionMod from "./session.js"
import * as Storage from "./components/storage/implementation.js"
import * as Ucan from "./ucan/index.js"
import { SESSION_TYPE as CAPABILITIES_SESSION_TYPE } from "./capabilities.js"
import { TYPE as WEB_CRYPTO_SESSION_TYPE } from "./components/auth/implementation/base.js"
import { VERSION } from "./common/version.js"
import { AccountLinkingConsumer, AccountLinkingProducer, createConsumer, createProducer } from "./linking/index.js"
import { Components } from "./components.js"
import { Configuration, namespace } from "./configuration.js"
import { isString, Maybe } from "./common/index.js"
import { Session } from "./session.js"
import { loadFileSystem, recoverFileSystem } from "./filesystem.js"
import FileSystem from "./fs/filesystem.js"
// TYPES
import { type RecoverFileSystemParams } from "./fs/types/params.js"
// IMPLEMENTATIONS
import * as BaseAuth from "./components/auth/implementation/base.js"
import * as BaseReference from "./components/reference/implementation/base.js"
import * as BrowserCrypto from "./components/crypto/implementation/browser.js"
import * as BrowserStorage from "./components/storage/implementation/browser.js"
import * as FissionIpfsProduction from "./components/depot/implementation/fission-ipfs-production.js"
import * as FissionIpfsStaging from "./components/depot/implementation/fission-ipfs-staging.js"
import * as FissionAuthBaseProduction from "./components/auth/implementation/fission-base-production.js"
import * as FissionAuthBaseStaging from "./components/auth/implementation/fission-base-staging.js"
import * as FissionAuthWnfsProduction from "./components/auth/implementation/fission-wnfs-production.js"
import * as FissionAuthWnfsStaging from "./components/auth/implementation/fission-wnfs-staging.js"
import * as FissionLobbyBase from "./components/capabilities/implementation/fission-lobby.js"
import * as FissionLobbyProduction from "./components/capabilities/implementation/fission-lobby-production.js"
import * as FissionLobbyStaging from "./components/capabilities/implementation/fission-lobby-staging.js"
import * as FissionReferenceProduction from "./components/reference/implementation/fission-production.js"
import * as FissionReferenceStaging from "./components/reference/implementation/fission-staging.js"
import * as MemoryStorage from "./components/storage/implementation/memory.js"
import * as ProperManners from "./components/manners/implementation/base.js"
// RE-EXPORTS
export * from "./appInfo.js"
export * from "./components.js"
export * from "./configuration.js"
export * from "./common/cid.js"
export * from "./common/types.js"
export * from "./common/version.js"
export * from "./permissions.js"
export * as apps from "./apps/index.js"
export * as did from "./did/index.js"
export * as fission from "./common/fission.js"
export * as path from "./path/index.js"
export * as ucan from "./ucan/index.js"
export { AccountLinkingConsumer, AccountLinkingProducer } from "./linking/index.js"
export { FileSystem } from "./fs/filesystem.js"
export { Session } from "./session.js"
// TYPES & CONSTANTS
export type AuthenticationStrategy = {
implementation: Auth.Implementation<Components>
accountConsumer: (username: string) => Promise<AccountLinkingConsumer>
accountProducer: (username: string) => Promise<AccountLinkingProducer>
isUsernameAvailable: (username: string) => Promise<boolean>
isUsernameValid: (username: string) => Promise<boolean>
register: (options: { username: string; email?: string }) => Promise<{ success: boolean }>
session: () => Promise<Maybe<Session>>
}
export type Program = ShortHands & Events.ListenTo<Events.All<Session>> & {
/**
* Authentication strategy, use this interface to register an account and link devices.
*/
auth: AuthenticationStrategy
capabilities: {
/**
* Collect capabilities.
*/
collect: () => Promise<Maybe<string>> // returns username
/**
* Request capabilities.
*
* Permissions from your configuration are passed automatically,
* but you can add additional permissions or override existing ones.
*/
request: (options?: CapabilitiesImpl.RequestOptions) => Promise<void>
/**
* Try to create a `Session` based on capabilities.
*/
session: (username: string) => Promise<Maybe<Session>>
}
/**
* Configuration used to build this program.
*/
configuration: Configuration
/**
* Components used to build this program.
*/
components: Components
/**
* Various file system methods.
*/
fileSystem: FileSystemShortHands
/**
* Existing session, if there is one.
*/
session: Maybe<Session>
}
export enum ProgramError {
InsecureContext = "INSECURE_CONTEXT",
UnsupportedBrowser = "UNSUPPORTED_BROWSER"
}
export type ShortHands = {
accountDID: (username: string) => Promise<string>
agentDID: () => Promise<string>
sharingDID: () => Promise<string>
}
export type FileSystemShortHands = {
addPublicExchangeKey: (fs: FileSystem) => Promise<void>
addSampleData: (fs: FileSystem) => Promise<void>
hasPublicExchangeKey: (fs: FileSystem) => Promise<boolean>
/**
* Load the file system of a given username.
*/
load: (username: string) => Promise<FileSystem>
/**
* Recover a file system.
*/
recover: (params: RecoverFileSystemParams) => Promise<{ success: boolean }>
}
// ENTRY POINTS
/**
* 🚀 Build an ODD program.
*
* This will give you a `Program` object which has the following properties:
* - `session`, a `Session` object if a session was created before.
* - `auth`, a means to control the various auth strategies you configured. Use this to create sessions. Read more about auth components in the toplevel `auth` object documention.
* - `capabilities`, a means to control capabilities. Use this to collect & request capabilities, and to create a session based on them. Read more about capabilities in the toplevel `capabilities` object documentation.
* - `components`, your full set of `Components`.
*
* This object also has a few other functions, for example to load a filesystem.
* These are called "shorthands" because they're the same functions available
* through other places in the ODD SDK, but you don't have to pass in the components.
*
* See `assemble` for more information. Note that this function checks for browser support,
* while `assemble` does not. Use the latter in case you want to bypass the indexedDB check,
* which might not be needed, or available, in certain environments or using certain components.
*/
export async function program(settings: Partial<Components> & Configuration): Promise<Program> {
if (!settings) throw new Error("Expected a settings object of the type `Partial<Components> & Configuration` as the first parameter")
// Check if the browser and context is supported
if (globalThis.isSecureContext === false) throw ProgramError.InsecureContext
if (await isSupported() === false) throw ProgramError.UnsupportedBrowser
// Initialise components & assemble program
const components = await gatherComponents(settings)
return assemble(extractConfig(settings), components)
}
// PREDEFINED COMPONENTS
/**
* Predefined auth configurations.
*
* This component goes hand in hand with the "reference" and "depot" components.
* The "auth" component registers a DID and the reference looks it up.
* The reference component also manages the "data root", the pointer to an account's entire filesystem.
* The depot component is responsible for getting data to and from the other side.
*
* For example, using the Fission architecture, when a data root is updated on the Fission server,
* the server fetches the data from the depot in your app.
*
* So if you want to build a service independent of Fission's infrastructure,
* you will need to write your own reference and depot implementations (see source code).
*
* NOTE: If you're using a non-default component, you'll want to pass that in here as a parameter as well.
* Dependencies: crypto, manners, reference, storage.
*/
export const auth = {
/**
* A standalone authentication system that uses the browser's Web Crypto API
* to create an identity based on a RSA key-pair.
*
* NOTE: This uses a Fission server to register an account (DID).
* Check out the `wnfs` and `base` auth implementations if
* you want to build something without the Fission infrastructure.
*/
async fissionWebCrypto(settings: Configuration & {
disableWnfs?: boolean
staging?: boolean
// Dependencies
crypto?: Crypto.Implementation
manners?: Manners.Implementation
reference?: Reference.Implementation
storage?: Storage.Implementation
}): Promise<Auth.Implementation<Components>> {
const { disableWnfs, staging } = settings
const manners = settings.manners || defaultMannersComponent(settings)
const crypto = settings.crypto || await defaultCryptoComponent(settings)
const storage = settings.storage || defaultStorageComponent(settings)
const reference = settings.reference || await defaultReferenceComponent({ crypto, manners, storage })
if (disableWnfs) {
if (staging) return FissionAuthBaseStaging.implementation({ crypto, reference, storage })
return FissionAuthBaseProduction.implementation({ crypto, reference, storage })
} else {
if (staging) return FissionAuthWnfsStaging.implementation({ crypto, reference, storage })
return FissionAuthWnfsProduction.implementation({ crypto, reference, storage })
}
}
}
/**
* Predefined capabilities configurations.
*
* If you want partial read and/or write access to the filesystem you'll want
* a "capabilities" component. This component is responsible for requesting
* and receiving UCANs, read keys and namefilters from other sources to enable this.
*
* NOTE: If you're using a non-default component, you'll want to pass that in here as a parameter as well.
* Dependencies: crypto, depot.
*/
export const capabilities = {
/**
* A secure enclave in the form of a ODD app which serves as the root authority.
* Your app is redirected to the lobby where the user can create an account or link a device,
* and then request permissions from the user for reading or write to specific parts of the filesystem.
*/
async fissionLobby(settings: Configuration & {
staging?: boolean
// Dependencies
crypto?: Crypto.Implementation
depot?: Depot.Implementation
storage?: Storage.Implementation
}): Promise<CapabilitiesImpl.Implementation> {
const { staging } = settings
const storage = settings.storage || defaultStorageComponent(settings)
const crypto = settings.crypto || await defaultCryptoComponent(settings)
const depot = settings.depot || await defaultDepotComponent({ storage }, settings)
if (staging) return FissionLobbyStaging.implementation({ crypto, depot })
return FissionLobbyProduction.implementation({ crypto, depot })
}
}
/**
* Predefined crypto configurations.
*
* The crypto component is responsible for various cryptographic operations.
* This includes AES and RSA encryption & decryption, creating and storing
* key pairs, verifying DIDs and defining their magic bytes, etc.
*/
export const crypto = {
/**
* The default crypto component, uses primarily the Web Crypto API and [keystore-idb](https://github.com/fission-codes/keystore-idb).
* Keys are stored in a non-exportable way in indexedDB using the Web Crypto API.
*
* IndexedDB store is namespaced.
*/
browser(settings: Configuration): Promise<Crypto.Implementation> {
return defaultCryptoComponent(settings)
}
}
/**
* Predefined depot configurations.
*
* The depot component gets data in and out your program.
* For example, say I want to load and then update a file system.
* The depot will get that file system data for me,
* and after updating it, send the data to where it needs to be.
*/
export const depot = {
/**
* This depot uses IPFS and the Fission servers.
* The data is transferred to the Fission IPFS node,
* where all of your encrypted and public data lives.
* Other ODD programs with this depot fetch the data from there.
*/
async fissionIPFS(
settings: Configuration & {
staging?: boolean
// Dependencies
storage?: Storage.Implementation
}
): Promise<Depot.Implementation> {
const repoName = `${namespace(settings)}/ipfs`
const storage = settings.storage || defaultStorageComponent(settings)
if (settings.staging) return FissionIpfsStaging.implementation({ storage }, repoName)
return FissionIpfsProduction.implementation({ storage }, repoName)
}
}
/**
* Predefined manners configurations.
*
* The manners component allows you to tweak various behaviours of an ODD program,
* such as logging and file system hooks (eg. what to do after a new file system is created).
*/
export const manners = {
/**
* The default ODD SDK behaviour.
*/
default(settings: Configuration): Manners.Implementation {
return defaultMannersComponent(settings)
}
}
/**
* Predefined reference configurations.
*
* The reference component is responsible for looking up and updating various pointers.
* Specifically, the data root, a user's DID root, DNSLinks, DNS TXT records.
* It also holds repositories (see `Repository` class), which contain UCANs and CIDs.
*
* NOTE: If you're using a non-default component, you'll want to pass that in here as a parameter as well.
* Dependencies: crypto, manners, storage.
*/
export const reference = {
/**
* Use the Fission servers as your reference.
*/
async fission(settings: Configuration & {
staging?: boolean
// Dependencies
crypto?: Crypto.Implementation
manners?: Manners.Implementation
storage?: Storage.Implementation
}): Promise<Reference.Implementation> {
const { staging } = settings
const manners = settings.manners || defaultMannersComponent(settings)
const crypto = settings.crypto || await defaultCryptoComponent(settings)
const storage = settings.storage || defaultStorageComponent(settings)
if (staging) return FissionReferenceStaging.implementation({ crypto, manners, storage })
return FissionReferenceProduction.implementation({ crypto, manners, storage })
}
}
/**
* Predefined storage configuration.
*
* A key-value storage abstraction responsible for storing various
* pieces of data, such as session data and UCANs.
*/
export const storage = {
/**
* IndexedDB through the `localForage` library, automatically namespaced.
*/
browser(settings: Configuration): Storage.Implementation {
return defaultStorageComponent(settings)
},
/**
* In-memory store.
*/
memory(): Storage.Implementation {
return MemoryStorage.implementation()
}
}
// ASSEMBLE
/**
* Build an ODD Program based on a given set of `Components`.
* These are various customisable components that determine how an ODD app works.
* Use `program` to work with a default, or partial, set of components.
*
* Additionally this does a few other things:
* - Restores a session if one was made before and loads the user's file system if needed.
* - Attempts to collect capabilities if the configuration has permissions.
* - Provides shorthands to functions so you don't have to pass in components.
* - Ensure backwards compatibility with older ODD SDK clients.
*
* See the `program.fileSystem.load` function if you want to load the user's file system yourself.
*/
export async function assemble(config: Configuration, components: Components): Promise<Program> {
const permissions = config.permissions
// Backwards compatibility (data)
await ensureBackwardsCompatibility(components, config)
// Event emitters
const fsEvents = Events.createEmitter<Events.FileSystem>()
const sessionEvents = Events.createEmitter<Events.Session<Session>>()
const allEvents = Events.merge(fsEvents, sessionEvents)
// Authenticated user
const sessionInfo = await SessionMod.restore(components.storage)
// Auth implementations
const auth: AuthenticationStrategy = (method => {
return {
implementation: method,
accountConsumer(username: string) {
return createConsumer(
{ auth: method, crypto: components.crypto, manners: components.manners },
{ username }
)
},
accountProducer(username: string) {
return createProducer(
{ auth: method, crypto: components.crypto, manners: components.manners },
{ username }
)
},
isUsernameAvailable: method.isUsernameAvailable,
isUsernameValid: method.isUsernameValid,
register: method.register,
async session(): Promise<Maybe<Session>> {
const newSessionInfo = await SessionMod.restore(components.storage)
if (!newSessionInfo) return null
return this.implementation.session(
components,
newSessionInfo.username,
config,
{ fileSystem: fsEvents, session: sessionEvents }
)
}
}
})(components.auth)
// Capabilities
const capabilities = {
async collect() {
const c = await components.capabilities.collect()
if (!c) return null
await Capabilities.collect({
capabilities: c,
crypto: components.crypto,
reference: components.reference,
storage: components.storage
})
return c.username
},
request(options?: CapabilitiesImpl.RequestOptions) {
return components.capabilities.request({
permissions,
...(options || {})
})
},
async session(username: string) {
const ucan = Capabilities.validatePermissions(
components.reference.repositories.ucans,
permissions || {}
)
if (!ucan) {
console.warn("The present UCANs did not satisfy the configured permissions.")
return null
}
const accountDID = await components.reference.didRoot.lookup(username)
const validSecrets = await Capabilities.validateSecrets(
components.crypto,
accountDID,
permissions || {}
)
if (!validSecrets) {
console.warn("The present filesystem secrets did not satisfy the configured permissions.")
return null
}
await SessionMod.provide(components.storage, { type: CAPABILITIES_SESSION_TYPE, username })
const fs = config.fileSystem?.loadImmediately === false ?
undefined :
await loadFileSystem({
config,
dependencies: components,
eventEmitter: fsEvents,
username,
})
return new Session({
fs,
username,
crypto: components.crypto,
storage: components.storage,
type: CAPABILITIES_SESSION_TYPE,
eventEmitter: sessionEvents
})
}
}
// Session
let session = null
if (isCapabilityBasedAuthConfiguration(config)) {
const username = await capabilities.collect()
if (username) session = await capabilities.session(username)
if (sessionInfo && sessionInfo.type === CAPABILITIES_SESSION_TYPE) session = await capabilities.session(sessionInfo.username)
} else if (sessionInfo && sessionInfo.type !== CAPABILITIES_SESSION_TYPE) {
session = await auth.session()
}
// Shorthands
const shorthands = {
// DIDs
accountDID: (username: string) => components.reference.didRoot.lookup(username),
agentDID: () => DID.agent(components.crypto),
sharingDID: () => DID.sharing(components.crypto),
// File system
fileSystem: {
addPublicExchangeKey: (fs: FileSystem) => FileSystemData.addPublicExchangeKey(components.crypto, fs),
addSampleData: (fs: FileSystem) => FileSystemData.addSampleData(fs),
hasPublicExchangeKey: (fs: FileSystem) => FileSystemData.hasPublicExchangeKey(components.crypto, fs),
load: (username: string) => loadFileSystem({ config, username, dependencies: components, eventEmitter: fsEvents }),
recover: (params: RecoverFileSystemParams) => recoverFileSystem({ auth, dependencies: components, ...params }),
}
}
// Create `Program`
const program = {
...shorthands,
...Events.listenTo(allEvents),
configuration: { ...config },
auth,
components,
capabilities,
session,
}
// Inject into global context if necessary
if (config.debug) {
const inject = config.debugging?.injectIntoGlobalContext === undefined
? true
: config.debugging?.injectIntoGlobalContext
if (inject) {
const container = globalThis as any
container.__odd = container.__odd || {}
container.__odd.programs = container.__odd.programs || {}
container.__odd.programs[ namespace(config) ] = program
}
const emitMessages = config.debugging?.emitWindowPostMessages === undefined
? true
: config.debugging?.emitWindowPostMessages
if (emitMessages) {
const { connect, disconnect } = await Extension.create({
namespace: config.namespace,
session,
capabilities: config.permissions,
dependencies: components,
eventEmitters: {
fileSystem: fsEvents,
session: sessionEvents
}
})
const container = globalThis as any
container.__odd = container.__odd || {}
container.__odd.extension = container.__odd.extension || {}
container.__odd.extension.connect = connect
container.__odd.extension.disconnect = disconnect
// Notify extension that the ODD SDK is ready
globalThis.postMessage({
id: "odd-devtools-ready-message",
})
}
}
// Fin
return program
}
// COMPOSITIONS
/**
* Full component sets.
*/
export const compositions = {
/**
* The default Fission stack using web crypto auth.
*/
async fission(settings: Configuration & {
disableWnfs?: boolean
staging?: boolean
// Dependencies
crypto?: Crypto.Implementation
manners?: Manners.Implementation
storage?: Storage.Implementation
}): Promise<Components> {
const crypto = settings.crypto || await defaultCryptoComponent(settings)
const manners = settings.manners || defaultMannersComponent(settings)
const storage = settings.storage || defaultStorageComponent(settings)
const settingsWithComponents = { ...settings, crypto, manners, storage }
const r = await reference.fission(settingsWithComponents)
const d = await depot.fissionIPFS(settingsWithComponents)
const c = await capabilities.fissionLobby({ ...settingsWithComponents, depot: d })
const a = await auth.fissionWebCrypto({ ...settingsWithComponents, reference: r })
return {
auth: a,
capabilities: c,
depot: d,
reference: r,
crypto,
manners,
storage,
}
}
}
export async function gatherComponents(setup: Partial<Components> & Configuration): Promise<Components> {
const config = extractConfig(setup)
const crypto = setup.crypto || await defaultCryptoComponent(config)
const manners = setup.manners || defaultMannersComponent(config)
const storage = setup.storage || defaultStorageComponent(config)
const reference = setup.reference || await defaultReferenceComponent({ crypto, manners, storage })
const depot = setup.depot || await defaultDepotComponent({ storage }, config)
const capabilities = setup.capabilities || defaultCapabilitiesComponent({ crypto, depot })
const auth = setup.auth || defaultAuthComponent({ crypto, reference, storage })
return {
auth,
capabilities,
crypto,
depot,
manners,
reference,
storage,
}
}
// DEFAULT COMPONENTS
export function defaultAuthComponent({ crypto, reference, storage }: BaseAuth.Dependencies): Auth.Implementation<Components> {
return FissionAuthWnfsProduction.implementation({
crypto, reference, storage,
})
}
export function defaultCapabilitiesComponent({ crypto, depot }: FissionLobbyBase.Dependencies): CapabilitiesImpl.Implementation {
return FissionLobbyProduction.implementation({ crypto, depot })
}
export function defaultCryptoComponent(config: Configuration): Promise<Crypto.Implementation> {
return BrowserCrypto.implementation({
storeName: namespace(config),
exchangeKeyName: "exchange-key",
writeKeyName: "write-key"
})
}
export function defaultDepotComponent({ storage }: IpfsNode.Dependencies, config: Configuration): Promise<Depot.Implementation> {
return FissionIpfsProduction.implementation(
{ storage },
`${namespace(config)}/ipfs`
)
}
export function defaultMannersComponent(config: Configuration): Manners.Implementation {
return ProperManners.implementation({
configuration: config
})
}
export function defaultReferenceComponent({ crypto, manners, storage }: BaseReference.Dependencies): Promise<Reference.Implementation> {
return FissionReferenceProduction.implementation({
crypto,
manners,
storage,
})
}
export function defaultStorageComponent(config: Configuration): Storage.Implementation {
return BrowserStorage.implementation({
name: namespace(config)
})
}
// 🛟
/**
* Is this browser supported?
*/
export async function isSupported(): Promise<boolean> {
return localforage.supports(localforage.INDEXEDDB)
// Firefox in private mode can't use indexedDB properly,
// so we test if we can actually make a database.
&& await (() => new Promise(resolve => {
const db = indexedDB.open("testDatabase")
db.onsuccess = () => resolve(true)
db.onerror = () => resolve(false)
}))() as boolean
}
// BACKWARDS COMPAT
async function ensureBackwardsCompatibility(components: Components, config: Configuration): Promise<void> {
// Old pieces:
// - Key pairs: IndexedDB → keystore → exchange-key & write-key
// - UCAN used for account linking/delegation: IndexedDB → localforage → ucan
// - Root read key of the filesystem: IndexedDB → localforage → readKey
// - Authenticated username: IndexedDB → localforage → webnative.auth_username
const [ migK, migV ] = [ "migrated", VERSION ]
const currentVersion = Semver.fromString(VERSION)
if (!currentVersion) throw new Error("The ODD SDK VERSION should be a semver string")
// If already migrated, stop here.
const migrationOccurred = await components.storage
.getItem(migK)
.then(v => typeof v === "string" ? Semver.fromString(v) : null)
.then(v => v && Semver.isBiggerThanOrEqualTo(v, currentVersion))
if (migrationOccurred) return
// Only try to migrate if environment supports indexedDB
if (!globalThis.indexedDB) return
// Migration
const existingDatabases = globalThis.indexedDB.databases
? (await globalThis.indexedDB.databases()).map(db => db.name)
: [ "keystore", "localforage" ]
const keystoreDB = existingDatabases.includes("keystore") ? await bwOpenDatabase("keystore") : null
if (keystoreDB) {
const exchangeKeyPair = await bwGetValue(keystoreDB, "keyvaluepairs", "exchange-key")
const writeKeyPair = await bwGetValue(keystoreDB, "keyvaluepairs", "write-key")
if (exchangeKeyPair && writeKeyPair) {
await components.storage.setItem("exchange-key", exchangeKeyPair)
await components.storage.setItem("write-key", writeKeyPair)
}
}
const localforageDB = existingDatabases.includes("localforage") ? await bwOpenDatabase("localforage") : null
if (localforageDB) {
const accountUcan = await bwGetValue(localforageDB, "keyvaluepairs", "ucan")
const permissionedUcans = await bwGetValue(localforageDB, "keyvaluepairs", "webnative.auth_ucans")
const rootKey = await bwGetValue(localforageDB, "keyvaluepairs", "readKey")
const authedUser = await bwGetValue(localforageDB, "keyvaluepairs", "webnative.auth_username")
if (rootKey && isString(rootKey)) {
const anyUcan = accountUcan || (Array.isArray(permissionedUcans) ? permissionedUcans[ 0 ] : undefined)
const accountDID = anyUcan ? Ucan.rootIssuer(anyUcan) : (typeof authedUser === "string" ? await components.reference.didRoot.lookup(authedUser) : null)
if (!accountDID) throw new Error("Failed to retrieve account DID")
await RootKey.store({
accountDID,
crypto: components.crypto,
readKey: Uint8arrays.fromString(rootKey, "base64pad"),
})
}
if (accountUcan) {
await components.storage.setItem(
components.storage.KEYS.ACCOUNT_UCAN,
accountUcan
)
}
if (authedUser) {
await components.storage.setItem(
components.storage.KEYS.SESSION,
JSON.stringify({
type: isCapabilityBasedAuthConfiguration(config) ? CAPABILITIES_SESSION_TYPE : WEB_CRYPTO_SESSION_TYPE,
username: authedUser
})
)
}
}
await components.storage.setItem(migK, migV)
}
function bwGetValue(db: IDBDatabase, storeName: string, key: string): Promise<Maybe<unknown>> {
return new Promise((resolve, reject) => {
if (!db.objectStoreNames.contains(storeName)) return resolve(null)
const transaction = db.transaction([ storeName ], "readonly")
const store = transaction.objectStore(storeName)
const req = store.get(key)
req.onerror = () => {
// No store, moving on.
resolve(null)
}
req.onsuccess = () => {
resolve(req.result)
}
})
}
function bwOpenDatabase(name: string): Promise<Maybe<IDBDatabase>> {
return new Promise((resolve, reject) => {
const req = globalThis.indexedDB.open(name)
req.onerror = () => {
// No database, moving on.
resolve(null)
}
req.onsuccess = () => {
resolve(req.result)
}
req.onupgradeneeded = e => {
// Don't create database if it didn't exist before
req.transaction?.abort()
globalThis.indexedDB.deleteDatabase(name)
}
})
}
// 🛠
export function extractConfig(opts: Partial<Components> & Configuration): Configuration {
return {
namespace: opts.namespace,
debug: opts.debug,
fileSystem: opts.fileSystem,
permissions: opts.permissions,
userMessages: opts.userMessages,
}
}
/**
* Is this a configuration that uses capabilities?
*/
export function isCapabilityBasedAuthConfiguration(config: Configuration): boolean {
return !!config.permissions
}