@novasamatech/host-papp 0.5.0-0 → 0.5.0-10

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (184) hide show
  1. package/.papi/descriptors/dist/common-types.d.ts +8667 -0
  2. package/.papi/descriptors/dist/common.d.ts +1 -0
  3. package/.papi/descriptors/dist/descriptors-UUEW32EL.mjs +27 -0
  4. package/.papi/descriptors/dist/descriptors.d.ts +1 -0
  5. package/.papi/descriptors/dist/index.d.ts +10 -0
  6. package/.papi/descriptors/dist/index.js +237 -0
  7. package/.papi/descriptors/dist/index.mjs +148 -0
  8. package/.papi/descriptors/dist/metadataTypes-E4AQJDJR.mjs +6 -0
  9. package/.papi/descriptors/dist/metadataTypes.d.ts +2 -0
  10. package/.papi/descriptors/dist/people_lite.d.ts +7757 -0
  11. package/.papi/descriptors/dist/people_lite_metadata-EIVHV27X.mjs +6 -0
  12. package/.papi/descriptors/dist/people_lite_metadata.d.ts +2 -0
  13. package/.papi/descriptors/package.json +24 -0
  14. package/.papi/metadata/people_lite.scale +0 -0
  15. package/.papi/polkadot-api.json +15 -0
  16. package/dist/adapters/identity/rpc.d.ts +6 -4
  17. package/dist/adapters/identity/rpc.js +96 -26
  18. package/dist/adapters/identity/types.d.ts +3 -1
  19. package/dist/adapters/lazyClient/papi.js +5 -0
  20. package/dist/adapters/lazyClient/types.d.ts +1 -0
  21. package/dist/adapters/statement/rpc.js +58 -10
  22. package/dist/adapters/statement/types.d.ts +6 -3
  23. package/dist/adapters/storage/localStorage.js +26 -4
  24. package/dist/adapters/storage/memory.js +14 -4
  25. package/dist/adapters/storage/types.d.ts +5 -2
  26. package/dist/adapters/storage/types.js +1 -1
  27. package/dist/components/auth/codec.d.ts +9 -0
  28. package/dist/components/auth/codec.js +10 -0
  29. package/dist/components/auth/codecs.d.ts +9 -0
  30. package/dist/components/auth/codecs.js +10 -0
  31. package/dist/components/auth/index.d.ts +36 -0
  32. package/dist/components/auth/index.js +150 -0
  33. package/dist/components/auth/types.d.ts +15 -0
  34. package/dist/components/auth/types.js +1 -0
  35. package/dist/components/session.d.ts +34 -0
  36. package/dist/components/session.js +54 -0
  37. package/dist/components/sso/index.d.ts +36 -0
  38. package/dist/components/sso/index.js +150 -0
  39. package/dist/components/sso/scale/handshake.d.ts +9 -0
  40. package/dist/components/sso/scale/handshake.js +10 -0
  41. package/dist/components/sso/types.d.ts +15 -0
  42. package/dist/components/sso/types.js +1 -0
  43. package/dist/components/transport.d.ts +27 -0
  44. package/dist/components/transport.js +57 -0
  45. package/dist/components/user/codec.d.ts +16 -0
  46. package/dist/components/user/codec.js +13 -0
  47. package/dist/components/user/index.d.ts +22 -0
  48. package/dist/components/user/index.js +58 -0
  49. package/dist/components/user/ssoMessageStream.d.ts +10 -0
  50. package/dist/components/user/ssoMessageStream.js +8 -0
  51. package/dist/components/user/ssoSession.d.ts +5 -0
  52. package/dist/components/user/ssoSession.js +5 -0
  53. package/dist/components/user/storage.d.ts +27 -0
  54. package/dist/components/user/storage.js +143 -0
  55. package/dist/components/user/types.d.ts +6 -0
  56. package/dist/components/user/types.js +1 -0
  57. package/dist/components/user/userSessionStorage.d.ts +20 -0
  58. package/dist/components/user/userSessionStorage.js +24 -0
  59. package/dist/components/user.d.ts +74 -0
  60. package/dist/components/user.js +188 -0
  61. package/dist/constants.d.ts +2 -1
  62. package/dist/constants.js +5 -1
  63. package/dist/crypto.d.ts +29 -0
  64. package/dist/crypto.js +86 -0
  65. package/dist/helpers/abortError.d.ts +4 -0
  66. package/dist/helpers/abortError.js +8 -0
  67. package/dist/helpers/callbackRaceResolver.d.ts +1 -0
  68. package/dist/helpers/callbackRaceResolver.js +17 -0
  69. package/dist/helpers/result.d.ts +12 -0
  70. package/dist/helpers/result.js +15 -0
  71. package/dist/helpers/result.spec.d.ts +1 -0
  72. package/dist/helpers/result.spec.js +23 -0
  73. package/dist/helpers/state.d.ts +16 -0
  74. package/dist/helpers/state.js +51 -0
  75. package/dist/helpers/utils.d.ts +2 -1
  76. package/dist/helpers/utils.js +11 -2
  77. package/dist/helpers/zipWith.d.ts +4 -0
  78. package/dist/helpers/zipWith.js +11 -0
  79. package/dist/identity/impl.d.ts +6 -0
  80. package/dist/identity/impl.js +68 -0
  81. package/dist/identity/rpcAdapter.d.ts +3 -0
  82. package/dist/identity/rpcAdapter.js +46 -0
  83. package/dist/identity/types.d.ts +21 -0
  84. package/dist/identity/types.js +1 -0
  85. package/dist/index.d.ts +7 -3
  86. package/dist/index.js +2 -7
  87. package/dist/modules/crypto.d.ts +8 -9
  88. package/dist/modules/crypto.js +20 -42
  89. package/dist/modules/secretStorage.d.ts +13 -12
  90. package/dist/modules/secretStorage.js +34 -43
  91. package/dist/modules/session/helpers.d.ts +5 -0
  92. package/dist/modules/session/helpers.js +29 -0
  93. package/dist/modules/session/session.d.ts +12 -0
  94. package/dist/modules/session/session.js +50 -0
  95. package/dist/modules/session/types.d.ts +12 -0
  96. package/dist/modules/session/types.js +1 -0
  97. package/dist/modules/signIn.d.ts +32 -11
  98. package/dist/modules/signIn.js +98 -101
  99. package/dist/modules/state.d.ts +16 -0
  100. package/dist/modules/state.js +50 -0
  101. package/dist/modules/statementStore.d.ts +10 -11
  102. package/dist/modules/statementStore.js +16 -14
  103. package/dist/modules/statementTopic.d.ts +34 -0
  104. package/dist/modules/statementTopic.js +46 -0
  105. package/dist/modules/storageView.d.ts +25 -0
  106. package/dist/modules/storageView.js +51 -0
  107. package/dist/modules/syncStorage.d.ts +25 -0
  108. package/dist/modules/syncStorage.js +76 -0
  109. package/dist/modules/transport/codec.d.ts +24 -0
  110. package/dist/modules/transport/codec.js +36 -0
  111. package/dist/modules/transport/crypto.d.ts +2 -0
  112. package/dist/modules/transport/crypto.js +20 -0
  113. package/dist/modules/transport/transport.d.ts +42 -0
  114. package/dist/modules/transport/transport.js +66 -0
  115. package/dist/modules/user.d.ts +67 -0
  116. package/dist/modules/user.js +188 -0
  117. package/dist/modules/userManager.d.ts +15 -0
  118. package/dist/modules/userManager.js +105 -0
  119. package/dist/modules/userStorage.d.ts +19 -0
  120. package/dist/modules/userStorage.js +108 -0
  121. package/dist/modules/userStore.d.ts +15 -0
  122. package/dist/modules/userStore.js +105 -0
  123. package/dist/papp.d.ts +25 -13
  124. package/dist/papp.js +19 -50
  125. package/dist/sso/auth/attestationService.d.ts +18 -0
  126. package/dist/sso/auth/attestationService.js +171 -0
  127. package/dist/sso/auth/impl.d.ts +53 -0
  128. package/dist/sso/auth/impl.js +161 -0
  129. package/dist/sso/auth/scale/handshake.d.ts +9 -0
  130. package/dist/sso/auth/scale/handshake.js +10 -0
  131. package/dist/sso/auth/types.d.ts +17 -0
  132. package/dist/sso/auth/types.js +1 -0
  133. package/dist/sso/session/impl.d.ts +23 -0
  134. package/dist/sso/session/impl.js +57 -0
  135. package/dist/sso/session/scale/remoteMessage.d.ts +10 -0
  136. package/dist/sso/session/scale/remoteMessage.js +13 -0
  137. package/dist/sso/session/sessionManager.d.ts +23 -0
  138. package/dist/sso/session/sessionManager.js +58 -0
  139. package/dist/sso/session/ssoSession.d.ts +8 -0
  140. package/dist/sso/session/ssoSession.js +5 -0
  141. package/dist/sso/session/ssoSessionStorage.d.ts +21 -0
  142. package/dist/sso/session/ssoSessionStorage.js +20 -0
  143. package/dist/sso/session/types.d.ts +6 -0
  144. package/dist/sso/session/types.js +1 -0
  145. package/dist/sso/session/userSessionStorage.d.ts +21 -0
  146. package/dist/sso/session/userSessionStorage.js +20 -0
  147. package/dist/sso/sessionManager/attestationService.d.ts +5 -0
  148. package/dist/sso/sessionManager/attestationService.js +15 -0
  149. package/dist/sso/sessionManager/impl.d.ts +22 -0
  150. package/dist/sso/sessionManager/impl.js +71 -0
  151. package/dist/sso/sessionManager/repository/ssoSessionRepository.d.ts +22 -0
  152. package/dist/sso/sessionManager/repository/ssoSessionRepository.js +27 -0
  153. package/dist/sso/sessionManager/scale/hex.d.ts +1 -0
  154. package/dist/sso/sessionManager/scale/hex.js +3 -0
  155. package/dist/sso/sessionManager/scale/remoteMessage.d.ts +41 -0
  156. package/dist/sso/sessionManager/scale/remoteMessage.js +13 -0
  157. package/dist/sso/sessionManager/scale/signPayloadRequest.d.ts +19 -0
  158. package/dist/sso/sessionManager/scale/signPayloadRequest.js +19 -0
  159. package/dist/sso/sessionManager/scale/signPayloadResponse.d.ts +12 -0
  160. package/dist/sso/sessionManager/scale/signPayloadResponse.js +9 -0
  161. package/dist/sso/sessionManager/scale/signRequest.d.ts +19 -0
  162. package/dist/sso/sessionManager/scale/signRequest.js +19 -0
  163. package/dist/sso/sessionManager/scale/signResponse.d.ts +6 -0
  164. package/dist/sso/sessionManager/scale/signResponse.js +5 -0
  165. package/dist/sso/sessionManager/ssoSession.d.ts +23 -0
  166. package/dist/sso/sessionManager/ssoSession.js +69 -0
  167. package/dist/sso/sessionManager/ssoSessionProver.d.ts +4 -0
  168. package/dist/sso/sessionManager/ssoSessionProver.js +35 -0
  169. package/dist/sso/sessionManager/types.d.ts +6 -0
  170. package/dist/sso/sessionManager/types.js +1 -0
  171. package/dist/sso/sessionManager/userSession.d.ts +22 -0
  172. package/dist/sso/sessionManager/userSession.js +106 -0
  173. package/dist/sso/ssoSessionProver.d.ts +4 -0
  174. package/dist/sso/ssoSessionProver.js +35 -0
  175. package/dist/sso/ssoSessionRepository.d.ts +18 -0
  176. package/dist/sso/ssoSessionRepository.js +27 -0
  177. package/dist/sso/userSecretRepository.d.ts +17 -0
  178. package/dist/sso/userSecretRepository.js +45 -0
  179. package/dist/sso/userSessionRepository.d.ts +18 -0
  180. package/dist/sso/userSessionRepository.js +26 -0
  181. package/dist/structs.d.ts +10 -10
  182. package/dist/structs.js +17 -13
  183. package/dist/types.d.ts +1 -1
  184. package/package.json +14 -7
@@ -0,0 +1,53 @@
1
+ import type { LazyClient, StatementStoreAdapter } from '@novasamatech/statement-store';
2
+ import { ResultAsync } from 'neverthrow';
3
+ import type { UserSecretRepository } from '../userSecretRepository.js';
4
+ import type { StoredUserSession, UserSessionRepository } from '../userSessionRepository.js';
5
+ export type AuthComponent = ReturnType<typeof createAuth>;
6
+ type Params = {
7
+ metadata: string;
8
+ statementStore: StatementStoreAdapter;
9
+ ssoSessionRepository: UserSessionRepository;
10
+ userSecretRepository: UserSecretRepository;
11
+ lazyClient: LazyClient;
12
+ };
13
+ export declare function createAuth({ metadata, statementStore, ssoSessionRepository, userSecretRepository, lazyClient, }: Params): {
14
+ status: {
15
+ read: () => {
16
+ step: "none";
17
+ } | {
18
+ step: "initial";
19
+ } | {
20
+ step: "attestation";
21
+ } | {
22
+ step: "pairing";
23
+ payload: string;
24
+ } | {
25
+ step: "error";
26
+ message: string;
27
+ } | {
28
+ step: "finished";
29
+ session: StoredUserSession;
30
+ };
31
+ subscribe: (fn: (value: {
32
+ step: "none";
33
+ } | {
34
+ step: "initial";
35
+ } | {
36
+ step: "attestation";
37
+ } | {
38
+ step: "pairing";
39
+ payload: string;
40
+ } | {
41
+ step: "error";
42
+ message: string;
43
+ } | {
44
+ step: "finished";
45
+ session: StoredUserSession;
46
+ }) => void) => () => void;
47
+ onFirstSubscribe: (callback: VoidFunction) => import("nanoevents").Unsubscribe;
48
+ onLastUnsubscribe: (callback: VoidFunction) => import("nanoevents").Unsubscribe;
49
+ };
50
+ authenticate(): ResultAsync<StoredUserSession | null, Error>;
51
+ abortAuthentication(): void;
52
+ };
53
+ export {};
@@ -0,0 +1,161 @@
1
+ import { createAccountId, createEncryption, createLocalSessionAccount, createRemoteSessionAccount, khash, } from '@novasamatech/statement-store';
2
+ import { mergeUint8, toHex } from '@polkadot-api/utils';
3
+ import { generateMnemonic } from '@polkadot-labs/hdkd-helpers';
4
+ import { Result, ResultAsync, err, fromPromise, fromThrowable, ok } from 'neverthrow';
5
+ import { createEncrSecret, createSharedSecret, deriveSr25519Account, getEncrPub, stringToBytes } from '../../crypto.js';
6
+ import { AbortError } from '../../helpers/abortError.js';
7
+ import { createState, readonly } from '../../helpers/state.js';
8
+ import { toError } from '../../helpers/utils.js';
9
+ import { createStoredUserSession } from '../userSessionRepository.js';
10
+ import { createAliceVerifier, createAttestationService } from './attestationService.js';
11
+ import { HandshakeData, HandshakeResponsePayload, HandshakeResponseSensitiveData } from './scale/handshake.js';
12
+ export function createAuth({ metadata, statementStore, ssoSessionRepository, userSecretRepository, lazyClient, }) {
13
+ const authStatus = createState({ step: 'none' });
14
+ let authResult = null;
15
+ let abort = null;
16
+ function attestateAccount(account, signal) {
17
+ const attestationService = createAttestationService(lazyClient);
18
+ authStatus.write({ step: 'attestation' });
19
+ const verifier = createAliceVerifier();
20
+ const username = attestationService.claimUsername();
21
+ return attestationService
22
+ .grantVerifierAllowance(verifier)
23
+ .andThrough(() => processSignal(signal))
24
+ .andThen(() => attestationService.registerLitePerson(username, account, verifier))
25
+ .andThrough(() => processSignal(signal));
26
+ }
27
+ function handshake(account, mnemonic, signal) {
28
+ const localAccount = createLocalSessionAccount(createAccountId(account.publicKey));
29
+ return createEncrKeys(mnemonic).asyncAndThen(({ encrSecret, encrPublicKey }) => {
30
+ const handshakePayload = createHandshakePayloadV1({
31
+ ssPublicKey: account.publicKey,
32
+ encrPublicKey,
33
+ metadata,
34
+ }).andTee(payload => authStatus.write({ step: 'pairing', payload: createDeeplink(payload) }));
35
+ const pappResponse = handshakePayload
36
+ .andThen(() => createHandshakeTopic(localAccount, encrPublicKey))
37
+ .asyncAndThen(topic => waitForStatements(callback => statementStore.subscribeStatements([topic], callback), signal, (statements, resolve) => {
38
+ for (const statement of statements) {
39
+ if (!statement.data)
40
+ continue;
41
+ const session = retrieveSession({
42
+ localAccount,
43
+ encrSecret,
44
+ payload: statement.data.asBytes(),
45
+ }).unwrapOr(null);
46
+ if (session) {
47
+ resolve(session);
48
+ break;
49
+ }
50
+ }
51
+ }));
52
+ const secretesSaved = pappResponse.andThen(({ id }) => userSecretRepository.write(id, { ssSecret: account.secret, encrSecret, mnemonic }));
53
+ const userCreated = secretesSaved.andThen(() => pappResponse.andThen(ssoSessionRepository.add));
54
+ const result = ResultAsync.combine([userCreated, secretesSaved]).map(([session]) => session);
55
+ return result.orElse(e => (AbortError.isAbortError(e) ? ok(null) : err(toError(e))));
56
+ });
57
+ }
58
+ const authModule = {
59
+ status: readonly(authStatus),
60
+ authenticate() {
61
+ if (authResult) {
62
+ return authResult;
63
+ }
64
+ abort = new AbortController();
65
+ const signal = abort.signal;
66
+ const mnemonic = generateMnemonic();
67
+ const account = deriveSr25519Account(mnemonic, '//wallet');
68
+ authStatus.write({ step: 'initial' });
69
+ authResult = attestateAccount(account, signal)
70
+ .andThen(() => handshake(account, mnemonic, signal))
71
+ .andTee(session => {
72
+ authStatus.write(session ? { step: 'finished', session } : { step: 'none' });
73
+ })
74
+ .orTee(e => {
75
+ authResult = null;
76
+ abort = null;
77
+ authStatus.write({ step: 'error', message: e.message });
78
+ });
79
+ return authResult;
80
+ },
81
+ abortAuthentication() {
82
+ if (abort) {
83
+ abort.abort(new AbortError('Aborted by user.'));
84
+ abort = null;
85
+ }
86
+ authResult = null;
87
+ authStatus.reset();
88
+ },
89
+ };
90
+ return authModule;
91
+ }
92
+ const createHandshakeTopic = fromThrowable((account, encrPublicKey) => khash(account.accountId, mergeUint8([encrPublicKey, stringToBytes('topic')])), toError);
93
+ const createHandshakePayloadV1 = fromThrowable(({ encrPublicKey, ssPublicKey, metadata, }) => HandshakeData.enc({
94
+ tag: 'v1',
95
+ value: [ssPublicKey, encrPublicKey, metadata],
96
+ }), toError);
97
+ function parseHandshakePayload(payload) {
98
+ const decoded = HandshakeResponsePayload.dec(payload);
99
+ switch (decoded.tag) {
100
+ case 'v1':
101
+ return {
102
+ encrypted: decoded.value[0],
103
+ tmpKey: decoded.value[1],
104
+ };
105
+ default:
106
+ throw new Error('Unsupported handshake payload version');
107
+ }
108
+ }
109
+ const createEncrKeys = fromThrowable((mnemonic) => {
110
+ const encrSecret = createEncrSecret(mnemonic);
111
+ return {
112
+ encrSecret,
113
+ encrPublicKey: getEncrPub(encrSecret),
114
+ };
115
+ }, toError);
116
+ function retrieveSession({ payload, encrSecret, localAccount, }) {
117
+ const { encrypted, tmpKey } = parseHandshakePayload(payload);
118
+ const symmetricKey = createSharedSecret(encrSecret, tmpKey);
119
+ return createEncryption(symmetricKey)
120
+ .decrypt(encrypted)
121
+ .map(decrypted => {
122
+ const [pappEncrPublicKey, pappAccountId] = HandshakeResponseSensitiveData.dec(decrypted);
123
+ const sharedSecret = createSharedSecret(encrSecret, pappEncrPublicKey);
124
+ const peerAccount = createRemoteSessionAccount(createAccountId(pappAccountId), sharedSecret);
125
+ return createStoredUserSession(localAccount, peerAccount);
126
+ });
127
+ }
128
+ function createDeeplink(payload) {
129
+ return `polkadotapp://pair?handshake=${toHex(payload)}`;
130
+ }
131
+ function waitForStatements(subscribe, signal, callback) {
132
+ return fromPromise(new Promise((resolve, reject) => {
133
+ const unsubscribe = subscribe(statements => {
134
+ const abortError = processSignal(signal).match(() => null, e => e);
135
+ if (abortError) {
136
+ unsubscribe();
137
+ reject(abortError);
138
+ return;
139
+ }
140
+ try {
141
+ callback(statements, value => {
142
+ unsubscribe();
143
+ resolve(value);
144
+ });
145
+ }
146
+ catch (e) {
147
+ unsubscribe();
148
+ reject(e);
149
+ }
150
+ });
151
+ }), toError);
152
+ }
153
+ function processSignal(signal) {
154
+ try {
155
+ signal.throwIfAborted();
156
+ return ok();
157
+ }
158
+ catch (e) {
159
+ return err(toError(e));
160
+ }
161
+ }
@@ -0,0 +1,9 @@
1
+ export declare const HandshakeData: import("scale-ts").Codec<{
2
+ tag: "v1";
3
+ value: [import("../../../crypto.js").SsPublicKey, import("../../../crypto.js").EncrPublicKey, string];
4
+ }>;
5
+ export declare const HandshakeResponsePayload: import("scale-ts").Codec<{
6
+ tag: "v1";
7
+ value: [Uint8Array<ArrayBufferLike>, Uint8Array<ArrayBufferLike>];
8
+ }>;
9
+ export declare const HandshakeResponseSensitiveData: import("scale-ts").Codec<[Uint8Array<ArrayBufferLike>, Uint8Array<ArrayBufferLike>]>;
@@ -0,0 +1,10 @@
1
+ import { Bytes, Enum, Tuple, str } from 'scale-ts';
2
+ import { EncrPubKey, SsPubKey } from '../../../crypto.js';
3
+ export const HandshakeData = Enum({
4
+ v1: Tuple(SsPubKey, EncrPubKey, str),
5
+ });
6
+ export const HandshakeResponsePayload = Enum({
7
+ // [encrypted, tmp_key]
8
+ v1: Tuple(Bytes(), Bytes(65)),
9
+ });
10
+ export const HandshakeResponseSensitiveData = Tuple(Bytes(65), Bytes(32));
@@ -0,0 +1,17 @@
1
+ import type { StoredUserSession } from '../userSessionRepository.js';
2
+ export type AuthentificationStatus = {
3
+ step: 'none';
4
+ } | {
5
+ step: 'initial';
6
+ } | {
7
+ step: 'attestation';
8
+ } | {
9
+ step: 'pairing';
10
+ payload: string;
11
+ } | {
12
+ step: 'error';
13
+ message: string;
14
+ } | {
15
+ step: 'finished';
16
+ session: StoredUserSession;
17
+ };
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,23 @@
1
+ import type { Transport } from '@novasamatech/statement-store';
2
+ import type { StorageAdapter } from '@novasamatech/storage-adapter';
3
+ import type { UserSession, ssoSessionStorage } from './ssoSessionStorage.js';
4
+ export type SsoSessionManager = ReturnType<typeof createSsoSessionManager>;
5
+ type Params = {
6
+ transport: Transport;
7
+ storage: StorageAdapter;
8
+ ssoSessionStorage: ssoSessionStorage;
9
+ };
10
+ export declare function createSsoSessionManager({ ssoSessionStorage, transport }: Params): {
11
+ sessions: {
12
+ add(value: UserSession): import("neverthrow").ResultAsync<UserSession, Error>;
13
+ filter(fn: (value: UserSession) => boolean): import("neverthrow").ResultAsync<UserSession[], Error>;
14
+ mutate(fn: (value: UserSession[]) => UserSession[]): import("neverthrow").ResultAsync<UserSession[], Error>;
15
+ read(): import("neverthrow").ResultAsync<UserSession[], Error>;
16
+ write(value: UserSession[]): import("neverthrow").ResultAsync<UserSession[], Error> | import("neverthrow").ResultAsync<null, Error>;
17
+ clear(): import("neverthrow").ResultAsync<void, Error>;
18
+ subscribe(fn: (value: UserSession[]) => void): VoidFunction;
19
+ };
20
+ disconnect: (session: UserSession) => import("neverthrow").ResultAsync<undefined, Error>;
21
+ destroy(): void;
22
+ };
23
+ export {};
@@ -0,0 +1,57 @@
1
+ import { okAsync } from 'neverthrow';
2
+ import { createSSOSession } from './ssoSession.js';
3
+ export function createSsoSessionManager({ ssoSessionStorage, transport }) {
4
+ let unsubStatements = null;
5
+ const disconnect = (session) => {
6
+ return ssoSessionStorage.mutate(sessions => sessions.filter(s => s.id !== session.id)).map(() => undefined);
7
+ };
8
+ const unsubSessions = ssoSessionStorage.subscribe(userSessions => {
9
+ if (unsubStatements) {
10
+ unsubStatements();
11
+ unsubStatements = null;
12
+ }
13
+ const ssoSessions = [];
14
+ for (const userSession of userSessions) {
15
+ const session = createSSOSession({
16
+ localAccount: userSession.local,
17
+ remoteAccount: userSession.remote,
18
+ transport,
19
+ });
20
+ session.subscribe(message => {
21
+ switch (message.data.tag) {
22
+ case 'v1': {
23
+ switch (message.data.value.tag) {
24
+ case 'Disconnected':
25
+ return disconnect(userSession).map(() => true);
26
+ }
27
+ }
28
+ }
29
+ return okAsync(false);
30
+ });
31
+ ssoSessions.push(session);
32
+ }
33
+ unsubStatements = () => {
34
+ for (const session of ssoSessions) {
35
+ session.dispose();
36
+ }
37
+ };
38
+ });
39
+ return {
40
+ sessions: ssoSessionStorage,
41
+ disconnect,
42
+ destroy() {
43
+ unsubSessions();
44
+ },
45
+ };
46
+ }
47
+ // function createDisconnectMessage(ssSecret: SsSecret, topic: Uint8Array) {
48
+ // const statement = createStatement(ssSecret, {
49
+ // priority: 0,
50
+ // channel: createRequestChannel(topic),
51
+ // topics: [topic],
52
+ // data: SSOMessage.enc({
53
+ // tag: 'Disconnected',
54
+ // value: undefined,
55
+ // }),
56
+ // });
57
+ // }
@@ -0,0 +1,10 @@
1
+ export declare const HostRemoteMessageCodec: import("scale-ts").Codec<{
2
+ messageId: string;
3
+ data: {
4
+ tag: "v1";
5
+ value: {
6
+ tag: "Disconnected";
7
+ value: undefined;
8
+ };
9
+ };
10
+ }>;
@@ -0,0 +1,13 @@
1
+ import { Enum, Struct, _void, str } from 'scale-ts';
2
+ export const HostRemoteMessageCodec = Struct({
3
+ messageId: str,
4
+ data: Enum({
5
+ v1: Enum({
6
+ Disconnected: _void,
7
+ // TODO implement
8
+ // SigningRequest: Bytes(),
9
+ // TODO implement
10
+ // SigningResponse: Bytes(),
11
+ }),
12
+ }),
13
+ });
@@ -0,0 +1,23 @@
1
+ import type { StorageAdapter } from '../../adapters/storage/types.js';
2
+ import type { Transport } from '../../modules/transport/transport.js';
3
+ import type { UserSession, UserSessionStorage } from './userSessionStorage.js';
4
+ export type SessionManager = ReturnType<typeof createSessionManager>;
5
+ type Params = {
6
+ transport: Transport;
7
+ storage: StorageAdapter;
8
+ userSessionStorage: UserSessionStorage;
9
+ };
10
+ export declare function createSessionManager({ userSessionStorage, storage, transport }: Params): {
11
+ sessions: {
12
+ add(value: UserSession): import("neverthrow").ResultAsync<UserSession, Error>;
13
+ filter(fn: (value: UserSession) => boolean): import("neverthrow").ResultAsync<UserSession[], Error>;
14
+ mutate(fn: (value: UserSession[]) => UserSession[]): import("neverthrow").ResultAsync<UserSession[], Error>;
15
+ read(): import("neverthrow").ResultAsync<UserSession[], Error>;
16
+ write(value: UserSession[]): import("neverthrow").ResultAsync<UserSession[], Error> | import("neverthrow").ResultAsync<null, Error>;
17
+ clear(): import("neverthrow").ResultAsync<void, Error>;
18
+ subscribe(fn: (value: UserSession[]) => void): VoidFunction;
19
+ };
20
+ disconnect: (session: UserSession) => import("neverthrow").ResultAsync<undefined, Error>;
21
+ destroy(): void;
22
+ };
23
+ export {};
@@ -0,0 +1,58 @@
1
+ import { okAsync } from 'neverthrow';
2
+ import { createSSOSession } from './ssoSession.js';
3
+ export function createSessionManager({ userSessionStorage, storage, transport }) {
4
+ let unsubStatements = null;
5
+ const disconnect = (session) => {
6
+ return userSessionStorage.mutate(sessions => sessions.filter(s => s.id !== session.id)).map(() => undefined);
7
+ };
8
+ const unsubSessions = userSessionStorage.subscribe(userSessions => {
9
+ if (unsubStatements) {
10
+ unsubStatements();
11
+ unsubStatements = null;
12
+ }
13
+ const ssoSessions = [];
14
+ for (const userSession of userSessions) {
15
+ const session = createSSOSession({
16
+ ownAccount: userSession.host,
17
+ peerAccount: userSession.peer,
18
+ storage,
19
+ transport,
20
+ });
21
+ session.subscribe(message => {
22
+ switch (message.data.tag) {
23
+ case 'v1': {
24
+ switch (message.data.value.tag) {
25
+ case 'Disconnected':
26
+ return disconnect(userSession).map(() => true);
27
+ }
28
+ }
29
+ }
30
+ return okAsync(false);
31
+ });
32
+ ssoSessions.push(session);
33
+ }
34
+ unsubStatements = () => {
35
+ for (const session of ssoSessions) {
36
+ session.dispose();
37
+ }
38
+ };
39
+ });
40
+ return {
41
+ sessions: userSessionStorage,
42
+ disconnect,
43
+ destroy() {
44
+ unsubSessions();
45
+ },
46
+ };
47
+ }
48
+ // function createDisconnectMessage(ssSecret: SsSecret, topic: Uint8Array) {
49
+ // const statement = createStatement(ssSecret, {
50
+ // priority: 0,
51
+ // channel: createRequestChannel(topic),
52
+ // topics: [topic],
53
+ // data: SSOMessage.enc({
54
+ // tag: 'Disconnected',
55
+ // value: undefined,
56
+ // }),
57
+ // });
58
+ // }
@@ -0,0 +1,8 @@
1
+ import type { LocalSessionAccount, RemoteSessionAccount, Session, Transport } from '@novasamatech/statement-store';
2
+ import { HostRemoteMessageCodec } from './scale/remoteMessage.js';
3
+ export type SsoSession = Session<typeof HostRemoteMessageCodec>;
4
+ export declare function createSSOSession({ localAccount, remoteAccount, transport, }: {
5
+ localAccount: LocalSessionAccount;
6
+ remoteAccount: RemoteSessionAccount;
7
+ transport: Transport;
8
+ }): SsoSession;
@@ -0,0 +1,5 @@
1
+ import { createSession } from '@novasamatech/statement-store';
2
+ import { HostRemoteMessageCodec } from './scale/remoteMessage.js';
3
+ export function createSSOSession({ localAccount, remoteAccount, transport, }) {
4
+ return createSession({ localAccount, remoteAccount, transport, codec: HostRemoteMessageCodec });
5
+ }
@@ -0,0 +1,21 @@
1
+ import type { LocalSessionAccount, RemoteSessionAccount } from '@novasamatech/statement-store';
2
+ import type { StorageAdapter } from '@novasamatech/storage-adapter';
3
+ export type ssoSessionStorage = ReturnType<typeof createSsoSessionStorage>;
4
+ export type UserSession = {
5
+ id: string;
6
+ local: LocalSessionAccount;
7
+ remote: RemoteSessionAccount;
8
+ };
9
+ type Params = {
10
+ storage: StorageAdapter;
11
+ };
12
+ export declare const createSsoSessionStorage: ({ storage }: Params) => {
13
+ add(value: UserSession): import("neverthrow").ResultAsync<UserSession, Error>;
14
+ filter(fn: (value: UserSession) => boolean): import("neverthrow").ResultAsync<UserSession[], Error>;
15
+ mutate(fn: (value: UserSession[]) => UserSession[]): import("neverthrow").ResultAsync<UserSession[], Error>;
16
+ read(): import("neverthrow").ResultAsync<UserSession[], Error>;
17
+ write(value: UserSession[]): import("neverthrow").ResultAsync<UserSession[], Error> | import("neverthrow").ResultAsync<null, Error>;
18
+ clear(): import("neverthrow").ResultAsync<void, Error>;
19
+ subscribe(fn: (value: UserSession[]) => void): VoidFunction;
20
+ };
21
+ export {};
@@ -0,0 +1,20 @@
1
+ import { LocalSessionAccountCodec, RemoteSessionAccountCodec } from '@novasamatech/statement-store';
2
+ import { fieldListView } from '@novasamatech/storage-adapter';
3
+ import { fromHex, toHex } from '@polkadot-api/utils';
4
+ import { Struct, Vector, str } from 'scale-ts';
5
+ const userSessionCodec = Struct({
6
+ id: str,
7
+ local: LocalSessionAccountCodec,
8
+ remote: RemoteSessionAccountCodec,
9
+ });
10
+ const userSessionsCodec = Vector(userSessionCodec);
11
+ export const createSsoSessionStorage = ({ storage }) => {
12
+ return fieldListView({
13
+ storage,
14
+ key: 'Sessions',
15
+ autosync: true,
16
+ initial: [],
17
+ from: x => userSessionsCodec.dec(fromHex(x)),
18
+ to: x => toHex(userSessionsCodec.enc(x)),
19
+ });
20
+ };
@@ -0,0 +1,6 @@
1
+ import type { EncrSecret, SharedSecret, SsSecret } from '../../modules/crypto.js';
2
+ export type UserSecrets = {
3
+ sharedSecret: SharedSecret;
4
+ encr: EncrSecret;
5
+ ss: SsSecret;
6
+ };
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,21 @@
1
+ import type { LocalSessionAccount, RemoteSessionAccount } from '@novasamatech/statement-store';
2
+ import type { StorageAdapter } from '@novasamatech/storage-adapter';
3
+ export type UserSessionStorage = ReturnType<typeof createUserSessionStorage>;
4
+ export type UserSession = {
5
+ id: string;
6
+ local: LocalSessionAccount;
7
+ remote: RemoteSessionAccount;
8
+ };
9
+ type Params = {
10
+ storage: StorageAdapter;
11
+ };
12
+ export declare const createUserSessionStorage: ({ storage }: Params) => {
13
+ add(value: UserSession): import("neverthrow").ResultAsync<UserSession, Error>;
14
+ filter(fn: (value: UserSession) => boolean): import("neverthrow").ResultAsync<UserSession[], Error>;
15
+ mutate(fn: (value: UserSession[]) => UserSession[]): import("neverthrow").ResultAsync<UserSession[], Error>;
16
+ read(): import("neverthrow").ResultAsync<UserSession[], Error>;
17
+ write(value: UserSession[]): import("neverthrow").ResultAsync<UserSession[], Error> | import("neverthrow").ResultAsync<null, Error>;
18
+ clear(): import("neverthrow").ResultAsync<void, Error>;
19
+ subscribe(fn: (value: UserSession[]) => void): VoidFunction;
20
+ };
21
+ export {};
@@ -0,0 +1,20 @@
1
+ import { LocalSessionAccountCodec, RemoteSessionAccountCodec } from '@novasamatech/statement-store';
2
+ import { fieldListView } from '@novasamatech/storage-adapter';
3
+ import { fromHex, toHex } from '@polkadot-api/utils';
4
+ import { Struct, Vector, str } from 'scale-ts';
5
+ const userSessionCodec = Struct({
6
+ id: str,
7
+ local: LocalSessionAccountCodec,
8
+ remote: RemoteSessionAccountCodec,
9
+ });
10
+ const userSessionsCodec = Vector(userSessionCodec);
11
+ export const createUserSessionStorage = ({ storage }) => {
12
+ return fieldListView({
13
+ storage,
14
+ key: 'Sessions',
15
+ autosync: true,
16
+ initial: [],
17
+ from: x => userSessionsCodec.dec(fromHex(x)),
18
+ to: x => toHex(userSessionsCodec.enc(x)),
19
+ });
20
+ };
@@ -0,0 +1,5 @@
1
+ import type { LazyClient } from '@novasamatech/statement-store';
2
+ import type { IdentityRepository } from '../../identity/types.js';
3
+ export declare const createAttestationService: (lazyClient: LazyClient, identity: IdentityRepository) => {
4
+ claimUsername(name: string): void;
5
+ };
@@ -0,0 +1,15 @@
1
+ export const createAttestationService = (lazyClient, identity) => {
2
+ function generateSuffix(length) {
3
+ let suffix = '';
4
+ for (let i = 0; i < length; i++) {
5
+ suffix += (Math.random() * 9).toFixed();
6
+ }
7
+ return suffix;
8
+ }
9
+ return {
10
+ claimUsername(name) {
11
+ const client = lazyClient.getClient();
12
+ const api = client.getUnsafeApi();
13
+ },
14
+ };
15
+ };
@@ -0,0 +1,22 @@
1
+ import type { StatementStoreAdapter } from '@novasamatech/statement-store';
2
+ import type { StorageAdapter } from '@novasamatech/storage-adapter';
3
+ import type { Callback } from '../../types.js';
4
+ import type { UserSecretRepository } from '../userSecretRepository.js';
5
+ import type { StoredUserSession, UserSessionRepository } from '../userSessionRepository.js';
6
+ import type { UserSession } from './userSession.js';
7
+ export type SsoSessionManager = ReturnType<typeof createSsoSessionManager>;
8
+ type Params = {
9
+ storage: StorageAdapter;
10
+ statementStore: StatementStoreAdapter;
11
+ ssoSessionRepository: UserSessionRepository;
12
+ userSecretRepository: UserSecretRepository;
13
+ };
14
+ export declare function createSsoSessionManager({ ssoSessionRepository, userSecretRepository, statementStore, storage, }: Params): {
15
+ sessions: {
16
+ read: () => UserSession[];
17
+ subscribe: (callback: Callback<UserSession[]>) => () => void;
18
+ };
19
+ disconnect(userSession: StoredUserSession): import("neverthrow").ResultAsync<undefined, Error>;
20
+ dispose(): void;
21
+ };
22
+ export {};