@novasamatech/host-papp 0.5.0-9 → 0.5.0

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 (170) 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/generated.json +1 -0
  14. package/.papi/descriptors/package.json +24 -0
  15. package/.papi/metadata/people_lite.scale +0 -0
  16. package/.papi/polkadot-api.json +15 -0
  17. package/dist/constants.d.ts +2 -2
  18. package/dist/constants.js +2 -2
  19. package/dist/crypto.d.ts +11 -5
  20. package/dist/crypto.js +47 -11
  21. package/dist/helpers/abortError.d.ts +0 -1
  22. package/dist/helpers/abortError.js +0 -3
  23. package/dist/identity/rpcAdapter.js +13 -5
  24. package/dist/identity/types.d.ts +3 -4
  25. package/dist/index.d.ts +4 -1
  26. package/dist/index.js +1 -0
  27. package/dist/papp.js +3 -3
  28. package/dist/sso/auth/attestationService.d.ts +18 -0
  29. package/dist/sso/auth/attestationService.js +171 -0
  30. package/dist/sso/auth/impl.d.ts +36 -5
  31. package/dist/sso/auth/impl.js +94 -56
  32. package/dist/sso/auth/types.d.ts +15 -2
  33. package/dist/sso/sessionManager/scale/hex.d.ts +1 -0
  34. package/dist/sso/sessionManager/scale/hex.js +3 -0
  35. package/dist/sso/sessionManager/scale/remoteMessage.d.ts +30 -12
  36. package/dist/sso/sessionManager/scale/remoteMessage.js +10 -11
  37. package/dist/sso/sessionManager/scale/signPayloadRequest.d.ts +20 -0
  38. package/dist/sso/sessionManager/scale/signPayloadRequest.js +20 -0
  39. package/dist/sso/sessionManager/scale/signPayloadResponse.d.ts +14 -0
  40. package/dist/sso/sessionManager/scale/signPayloadResponse.js +10 -0
  41. package/dist/sso/sessionManager/userSession.d.ts +5 -7
  42. package/dist/sso/sessionManager/userSession.js +53 -12
  43. package/dist/sso/userSecretRepository.d.ts +1 -0
  44. package/dist/sso/userSecretRepository.js +2 -1
  45. package/dist/sso/userSessionRepository.js +0 -1
  46. package/package.json +12 -9
  47. package/dist/adapters/identity/rpc.d.ts +0 -6
  48. package/dist/adapters/identity/rpc.js +0 -101
  49. package/dist/adapters/identity/types.d.ts +0 -10
  50. package/dist/adapters/identity/types.js +0 -1
  51. package/dist/adapters/lazyClient/papi.d.ts +0 -3
  52. package/dist/adapters/lazyClient/papi.js +0 -17
  53. package/dist/adapters/lazyClient/types.d.ts +0 -5
  54. package/dist/adapters/lazyClient/types.js +0 -1
  55. package/dist/adapters/statement/rpc.d.ts +0 -3
  56. package/dist/adapters/statement/rpc.js +0 -93
  57. package/dist/adapters/statement/types.d.ts +0 -9
  58. package/dist/adapters/statement/types.js +0 -1
  59. package/dist/adapters/storage/localStorage.d.ts +0 -2
  60. package/dist/adapters/storage/localStorage.js +0 -34
  61. package/dist/adapters/storage/memory.d.ts +0 -2
  62. package/dist/adapters/storage/memory.js +0 -22
  63. package/dist/adapters/storage/types.d.ts +0 -7
  64. package/dist/adapters/storage/types.js +0 -1
  65. package/dist/adapters/transport/rpc.d.ts +0 -3
  66. package/dist/adapters/transport/rpc.js +0 -51
  67. package/dist/adapters/transport/types.d.ts +0 -6
  68. package/dist/adapters/transport/types.js +0 -1
  69. package/dist/components/auth/codec.d.ts +0 -9
  70. package/dist/components/auth/codec.js +0 -10
  71. package/dist/components/auth/codecs.d.ts +0 -9
  72. package/dist/components/auth/codecs.js +0 -10
  73. package/dist/components/auth/index.d.ts +0 -36
  74. package/dist/components/auth/index.js +0 -150
  75. package/dist/components/auth/types.d.ts +0 -15
  76. package/dist/components/auth/types.js +0 -1
  77. package/dist/components/session.d.ts +0 -34
  78. package/dist/components/session.js +0 -54
  79. package/dist/components/sso/index.d.ts +0 -36
  80. package/dist/components/sso/index.js +0 -150
  81. package/dist/components/sso/scale/handshake.d.ts +0 -9
  82. package/dist/components/sso/scale/handshake.js +0 -10
  83. package/dist/components/sso/types.d.ts +0 -15
  84. package/dist/components/sso/types.js +0 -1
  85. package/dist/components/transport.d.ts +0 -27
  86. package/dist/components/transport.js +0 -57
  87. package/dist/components/user/codec.d.ts +0 -16
  88. package/dist/components/user/codec.js +0 -13
  89. package/dist/components/user/index.d.ts +0 -22
  90. package/dist/components/user/index.js +0 -58
  91. package/dist/components/user/ssoMessageStream.d.ts +0 -10
  92. package/dist/components/user/ssoMessageStream.js +0 -8
  93. package/dist/components/user/ssoSession.d.ts +0 -5
  94. package/dist/components/user/ssoSession.js +0 -5
  95. package/dist/components/user/storage.d.ts +0 -27
  96. package/dist/components/user/storage.js +0 -143
  97. package/dist/components/user/types.d.ts +0 -6
  98. package/dist/components/user/types.js +0 -1
  99. package/dist/components/user/userSessionStorage.d.ts +0 -20
  100. package/dist/components/user/userSessionStorage.js +0 -24
  101. package/dist/components/user.d.ts +0 -74
  102. package/dist/components/user.js +0 -188
  103. package/dist/helpers/result.d.ts +0 -12
  104. package/dist/helpers/result.js +0 -15
  105. package/dist/helpers/result.spec.d.ts +0 -1
  106. package/dist/helpers/result.spec.js +0 -23
  107. package/dist/helpers.d.ts +0 -1
  108. package/dist/helpers.js +0 -3
  109. package/dist/modules/accounts.d.ts +0 -1
  110. package/dist/modules/accounts.js +0 -2
  111. package/dist/modules/crypto.d.ts +0 -23
  112. package/dist/modules/crypto.js +0 -51
  113. package/dist/modules/secretStorage.d.ts +0 -15
  114. package/dist/modules/secretStorage.js +0 -44
  115. package/dist/modules/session/helpers.d.ts +0 -5
  116. package/dist/modules/session/helpers.js +0 -29
  117. package/dist/modules/session/session.d.ts +0 -12
  118. package/dist/modules/session/session.js +0 -50
  119. package/dist/modules/session/types.d.ts +0 -12
  120. package/dist/modules/session/types.js +0 -1
  121. package/dist/modules/signIn.d.ts +0 -67
  122. package/dist/modules/signIn.js +0 -188
  123. package/dist/modules/state.d.ts +0 -16
  124. package/dist/modules/state.js +0 -50
  125. package/dist/modules/statementStore.d.ts +0 -12
  126. package/dist/modules/statementStore.js +0 -22
  127. package/dist/modules/statementTopic.d.ts +0 -34
  128. package/dist/modules/statementTopic.js +0 -46
  129. package/dist/modules/storageView.d.ts +0 -25
  130. package/dist/modules/storageView.js +0 -51
  131. package/dist/modules/syncStorage.d.ts +0 -25
  132. package/dist/modules/syncStorage.js +0 -76
  133. package/dist/modules/transport/codec.d.ts +0 -24
  134. package/dist/modules/transport/codec.js +0 -36
  135. package/dist/modules/transport/crypto.d.ts +0 -2
  136. package/dist/modules/transport/crypto.js +0 -20
  137. package/dist/modules/transport/transport.d.ts +0 -42
  138. package/dist/modules/transport/transport.js +0 -66
  139. package/dist/modules/user.d.ts +0 -67
  140. package/dist/modules/user.js +0 -188
  141. package/dist/modules/userManager.d.ts +0 -15
  142. package/dist/modules/userManager.js +0 -105
  143. package/dist/modules/userStorage.d.ts +0 -19
  144. package/dist/modules/userStorage.js +0 -108
  145. package/dist/modules/userStore.d.ts +0 -15
  146. package/dist/modules/userStore.js +0 -105
  147. package/dist/sso/session/impl.d.ts +0 -23
  148. package/dist/sso/session/impl.js +0 -57
  149. package/dist/sso/session/scale/remoteMessage.d.ts +0 -10
  150. package/dist/sso/session/scale/remoteMessage.js +0 -13
  151. package/dist/sso/session/sessionManager.d.ts +0 -23
  152. package/dist/sso/session/sessionManager.js +0 -58
  153. package/dist/sso/session/ssoSession.d.ts +0 -8
  154. package/dist/sso/session/ssoSession.js +0 -5
  155. package/dist/sso/session/ssoSessionStorage.d.ts +0 -21
  156. package/dist/sso/session/ssoSessionStorage.js +0 -20
  157. package/dist/sso/session/types.d.ts +0 -6
  158. package/dist/sso/session/types.js +0 -1
  159. package/dist/sso/session/userSessionStorage.d.ts +0 -21
  160. package/dist/sso/session/userSessionStorage.js +0 -20
  161. package/dist/sso/sessionManager/repository/ssoSessionRepository.d.ts +0 -22
  162. package/dist/sso/sessionManager/repository/ssoSessionRepository.js +0 -27
  163. package/dist/sso/sessionManager/ssoSession.d.ts +0 -23
  164. package/dist/sso/sessionManager/ssoSession.js +0 -69
  165. package/dist/sso/sessionManager/ssoSessionProver.d.ts +0 -4
  166. package/dist/sso/sessionManager/ssoSessionProver.js +0 -35
  167. package/dist/sso/ssoSessionRepository.d.ts +0 -18
  168. package/dist/sso/ssoSessionRepository.js +0 -27
  169. package/dist/structs.d.ts +0 -24
  170. package/dist/structs.js +0 -36
@@ -1,101 +0,0 @@
1
- import { Result, ResultAsync, err, errAsync, fromPromise, ok, okAsync } from 'neverthrow';
2
- import { AccountId } from 'polkadot-api';
3
- import { toError } from '../../helpers/utils.js';
4
- function zipWith(arrays, iteratee) {
5
- if (arrays.length === 0)
6
- return [];
7
- const minLength = Math.min(...arrays.map(arr => arr.length));
8
- const result = [];
9
- for (let i = 0; i < minLength; i++) {
10
- const values = arrays.map(arr => arr[i]);
11
- result.push(iteratee(values));
12
- }
13
- return result;
14
- }
15
- export function createCachedIdentityRequester(storage, getKey, request) {
16
- function readSingleCacheRecord(accountId) {
17
- return storage.read(getKey(accountId)).andThen(raw => {
18
- if (!raw) {
19
- return ok(null);
20
- }
21
- try {
22
- return ok(JSON.parse(raw));
23
- }
24
- catch (e) {
25
- return err(toError(e));
26
- }
27
- });
28
- }
29
- function writeSingleCacheRecord(accountId, identity) {
30
- if (identity === null) {
31
- return okAsync(undefined);
32
- }
33
- return storage.write(getKey(accountId), JSON.stringify(identity));
34
- }
35
- function readCache(accounts) {
36
- if (accounts.length === 0) {
37
- return okAsync({});
38
- }
39
- const identities = ResultAsync.combine(accounts.map(readSingleCacheRecord));
40
- return identities.map(identities => {
41
- return Object.fromEntries(identities.map((x, i) => {
42
- const accountId = accounts.at(i);
43
- if (!accountId) {
44
- throw new Error(`Identity not found`);
45
- }
46
- return [accountId, x];
47
- }));
48
- });
49
- }
50
- function writeCache(identities) {
51
- return ResultAsync.combine(Object.entries(identities).map(args => writeSingleCacheRecord(...args))).map(() => identities);
52
- }
53
- return (accounts) => {
54
- return readCache(accounts).andThen(existing => {
55
- const emptyIdentities = Object.entries(existing)
56
- .filter(([, identity]) => identity === null)
57
- .map(([accountId]) => accountId);
58
- if (emptyIdentities.length === 0) {
59
- return okAsync(existing);
60
- }
61
- return request(accounts)
62
- .map(response => Object.fromEntries(zipWith([accounts, response], x => x)))
63
- .andThen(writeCache)
64
- .map(fetched => ({
65
- ...existing,
66
- ...fetched,
67
- }));
68
- });
69
- };
70
- }
71
- export function createIdentityRpcAdapter(lazyClient, storage) {
72
- const requester = createCachedIdentityRequester(storage, (accountId) => `identity_${accountId}`, accounts => {
73
- const client = lazyClient.getClient();
74
- const unsafeApi = client.getUnsafeApi();
75
- const method = unsafeApi.query.Resources?.Consumers;
76
- if (!method) {
77
- return errAsync(new Error('Method Resources:Consumers not found'));
78
- }
79
- const results = fromPromise(method.getValues([accounts.map(accCodec.dec)]), toError);
80
- return results.andThen(results => {
81
- if (!results) {
82
- return ok(accounts.map(() => null));
83
- }
84
- return ok(zipWith([accounts, results], x => x).map(([accountId, raw]) => {
85
- if (!raw) {
86
- return null;
87
- }
88
- return {
89
- accountId: accountId,
90
- fullUsername: raw.full_username ? raw.full_username.asText() : null,
91
- liteUsername: raw.lite_username ? raw.lite_username.asText() : null,
92
- credibility: raw.credibility.type,
93
- };
94
- }));
95
- });
96
- });
97
- const accCodec = AccountId();
98
- return {
99
- readIdentities: requester,
100
- };
101
- }
@@ -1,10 +0,0 @@
1
- import type { ResultAsync } from 'neverthrow';
2
- export type Identity = {
3
- accountId: string;
4
- fullUsername: string | null;
5
- liteUsername: string;
6
- credibility: string;
7
- };
8
- export type IdentityAdapter = {
9
- readIdentities(accounts: string[]): ResultAsync<Record<string, Identity | null>, Error>;
10
- };
@@ -1 +0,0 @@
1
- export {};
@@ -1,3 +0,0 @@
1
- import type { JsonRpcProvider } from '@polkadot-api/json-rpc-provider';
2
- import type { LazyClientAdapter } from './types.js';
3
- export declare const createPapiLazyClient: (provider: JsonRpcProvider) => LazyClientAdapter;
@@ -1,17 +0,0 @@
1
- import { createClient } from 'polkadot-api';
2
- export const createPapiLazyClient = (provider) => {
3
- let client = null;
4
- return {
5
- getClient() {
6
- if (!client) {
7
- client = createClient(provider);
8
- }
9
- return client;
10
- },
11
- disconnect() {
12
- if (client) {
13
- client.destroy();
14
- }
15
- },
16
- };
17
- };
@@ -1,5 +0,0 @@
1
- import type { PolkadotClient } from 'polkadot-api';
2
- export type LazyClientAdapter = {
3
- getClient(): PolkadotClient;
4
- disconnect(): void;
5
- };
@@ -1 +0,0 @@
1
- export {};
@@ -1,3 +0,0 @@
1
- import type { LazyClientAdapter } from '../lazyClient/types.js';
2
- import type { StatementAdapter } from './types.js';
3
- export declare function createPapiStatementAdapter(lazyClient: LazyClientAdapter): StatementAdapter;
@@ -1,93 +0,0 @@
1
- import { createStatementSdk } from '@polkadot-api/sdk-statement';
2
- import { Binary } from '@polkadot-api/substrate-bindings';
3
- import { toHex } from '@polkadot-api/utils';
4
- import { fromPromise } from 'neverthrow';
5
- import { toError } from '../../helpers/utils.js';
6
- const POLLING_INTERVAL = 1000;
7
- function createKey(topics) {
8
- return topics.map(toHex).sort().join('');
9
- }
10
- export function createPapiStatementAdapter(lazyClient) {
11
- const sdk = createStatementSdk((method, params) => {
12
- const client = lazyClient.getClient();
13
- return client._request(method, params);
14
- });
15
- const pollings = new Map();
16
- const subscriptions = new Map();
17
- function addSubscription(key, subscription) {
18
- let subs = subscriptions.get(key);
19
- if (!subs) {
20
- subs = [];
21
- subscriptions.set(key, subs);
22
- }
23
- subs.push(subscription);
24
- return subs;
25
- }
26
- function removeSubscription(key, subscription) {
27
- let subs = subscriptions.get(key);
28
- if (!subs) {
29
- return [];
30
- }
31
- subs = subs.filter(x => x !== subscription);
32
- return subs;
33
- }
34
- const transportProvider = {
35
- queryStatements(topics, destination) {
36
- return fromPromise(sdk.getStatements({
37
- topics: topics.map(t => Binary.fromBytes(t)),
38
- dest: destination ? Binary.fromBytes(destination) : null,
39
- }), toError);
40
- },
41
- subscribeStatements(topics, callback) {
42
- const key = createKey(topics);
43
- const callbacks = addSubscription(key, callback);
44
- if (callbacks.length === 1) {
45
- const unsub = polling(POLLING_INTERVAL, () => transportProvider.queryStatements(topics), statements => {
46
- const list = subscriptions.get(key);
47
- if (list) {
48
- for (const fn of list) {
49
- fn(statements);
50
- }
51
- }
52
- });
53
- pollings.set(key, unsub);
54
- }
55
- return () => {
56
- const callbacks = removeSubscription(key, callback);
57
- if (callbacks.length === 0) {
58
- const stopPolling = pollings.get(key);
59
- stopPolling?.();
60
- pollings.delete(key);
61
- }
62
- };
63
- },
64
- submitStatement(statement) {
65
- return fromPromise(sdk.submit(statement), toError);
66
- },
67
- };
68
- return transportProvider;
69
- }
70
- function polling(interval, request, callback) {
71
- let active = true;
72
- let tm = null;
73
- function createCycle() {
74
- tm = setTimeout(() => {
75
- if (!active) {
76
- return;
77
- }
78
- request().match(data => {
79
- callback(data);
80
- createCycle();
81
- }, () => {
82
- createCycle();
83
- });
84
- }, interval);
85
- }
86
- createCycle();
87
- return () => {
88
- active = false;
89
- if (tm !== null) {
90
- clearTimeout(tm);
91
- }
92
- };
93
- }
@@ -1,9 +0,0 @@
1
- import type { SignedStatement, Statement } from '@polkadot-api/sdk-statement';
2
- import type { ResultAsync } from 'neverthrow';
3
- import type { Callback } from '../../types.js';
4
- export type StatementsCallback = Callback<Statement[]>;
5
- export type StatementAdapter = {
6
- queryStatements(topics: Uint8Array[], destination?: Uint8Array): ResultAsync<Statement[], Error>;
7
- subscribeStatements(topics: Uint8Array[], callback: StatementsCallback): VoidFunction;
8
- submitStatement(statement: SignedStatement): ResultAsync<void, Error>;
9
- };
@@ -1 +0,0 @@
1
- export {};
@@ -1,2 +0,0 @@
1
- import type { StorageAdapter } from './types.js';
2
- export declare function createLocalStorageAdapter(prefix: string): StorageAdapter;
@@ -1,34 +0,0 @@
1
- import { createNanoEvents } from 'nanoevents';
2
- import { fromAsyncThrowable } from 'neverthrow';
3
- import { toError } from '../../helpers/utils.js';
4
- export function createLocalStorageAdapter(prefix) {
5
- const events = createNanoEvents();
6
- const withPrefix = (key) => `PAPP_${prefix}_${key}`;
7
- return {
8
- write: fromAsyncThrowable(async (key, value) => {
9
- localStorage.setItem(withPrefix(key), value);
10
- events.emit(key, value);
11
- }, toError),
12
- read: fromAsyncThrowable(async (key) => {
13
- return localStorage.getItem(withPrefix(key));
14
- }, toError),
15
- clear: fromAsyncThrowable(async (key) => {
16
- localStorage.removeItem(withPrefix(key));
17
- events.emit(key, null);
18
- }, toError),
19
- subscribe(key, callback) {
20
- const prefixedKey = withPrefix(key);
21
- const unsubscribeLocalListener = events.on(prefixedKey, callback);
22
- const externalListener = (event) => {
23
- if (event.storageArea === localStorage && event.key === prefixedKey) {
24
- callback(event.newValue);
25
- }
26
- };
27
- window.addEventListener('storage', externalListener);
28
- return () => {
29
- unsubscribeLocalListener();
30
- window.removeEventListener('storage', externalListener);
31
- };
32
- },
33
- };
34
- }
@@ -1,2 +0,0 @@
1
- import type { StorageAdapter } from './types.js';
2
- export declare function createMemoryAdapter(external?: Record<string, string>): StorageAdapter;
@@ -1,22 +0,0 @@
1
- import { createNanoEvents } from 'nanoevents';
2
- import { fromAsyncThrowable } from 'neverthrow';
3
- export function createMemoryAdapter(external) {
4
- const events = createNanoEvents();
5
- const storage = external ? { ...external } : {};
6
- return {
7
- write: fromAsyncThrowable(async (key, value) => {
8
- storage[key] = value;
9
- events.emit(key, value);
10
- }),
11
- read: fromAsyncThrowable(async (key) => {
12
- return storage[key] ?? null;
13
- }),
14
- clear: fromAsyncThrowable(async (key) => {
15
- delete storage[key];
16
- events.emit(key, null);
17
- }),
18
- subscribe(key, callback) {
19
- return events.on(key, callback);
20
- },
21
- };
22
- }
@@ -1,7 +0,0 @@
1
- import { ResultAsync } from 'neverthrow';
2
- export type StorageAdapter = {
3
- write(key: string, value: string): ResultAsync<void, Error>;
4
- read(key: string): ResultAsync<string | null, Error>;
5
- clear(key: string): ResultAsync<void, Error>;
6
- subscribe(key: string, callback: (value: string | null) => unknown): VoidFunction;
7
- };
@@ -1 +0,0 @@
1
- import { ResultAsync } from 'neverthrow';
@@ -1,3 +0,0 @@
1
- import type { JsonRpcProvider } from '@polkadot-api/json-rpc-provider';
2
- import type { Transport } from './types.js';
3
- export declare function createRpcTransport(rpcProvider: JsonRpcProvider): Transport;
@@ -1,51 +0,0 @@
1
- import { createClient } from '@polkadot-api/raw-client';
2
- import { createStatementSdk } from '@polkadot-api/sdk-statement';
3
- import { FixedSizeBinary } from '@polkadot-api/substrate-bindings';
4
- export function createRpcTransport(rpcProvider) {
5
- const POLLING_INTERVAL = 1000;
6
- const client = createClient(rpcProvider);
7
- const sdk = createStatementSdk((method, params) => {
8
- return new Promise((resolve, reject) => {
9
- client.request(method, params, {
10
- onSuccess: resolve,
11
- onError: reject,
12
- });
13
- });
14
- });
15
- const transportProvider = {
16
- getStatements(topics) {
17
- // @ts-expect-error lib versions mismatch
18
- return sdk.getStatements({ topics: topics.map(topic => new FixedSizeBinary(topic)) });
19
- },
20
- subscribeStatements(topics, callback) {
21
- return polling(POLLING_INTERVAL, () => transportProvider.getStatements(topics), callback);
22
- },
23
- submitStatement(statement) {
24
- return sdk.submit(statement);
25
- },
26
- };
27
- return transportProvider;
28
- }
29
- function polling(interval, request, callback) {
30
- let active = true;
31
- let tm = null;
32
- function createCycle() {
33
- tm = setTimeout(() => {
34
- if (!active) {
35
- return;
36
- }
37
- request()
38
- .then(callback)
39
- .finally(() => {
40
- createCycle();
41
- });
42
- }, interval);
43
- }
44
- createCycle();
45
- return () => {
46
- active = false;
47
- if (tm !== null) {
48
- clearTimeout(tm);
49
- }
50
- };
51
- }
@@ -1,6 +0,0 @@
1
- import type { SignedStatement, Statement } from '@polkadot-api/sdk-statement';
2
- export type Transport = {
3
- getStatements(topics: Uint8Array[]): Promise<Statement[]>;
4
- subscribeStatements(topics: Uint8Array[], callback: (response: Statement[]) => unknown): VoidFunction;
5
- submitStatement(statement: SignedStatement): Promise<void>;
6
- };
@@ -1 +0,0 @@
1
- export {};
@@ -1,9 +0,0 @@
1
- export declare const HandshakeData: import("scale-ts").Codec<{
2
- tag: "V1";
3
- value: [import("../../modules/crypto.js").SsPublicKey, import("../../modules/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>]>;
@@ -1,10 +0,0 @@
1
- import { Bytes, Enum, Tuple, str } from 'scale-ts';
2
- import { EncrPubKey, SsPubKey } from '../../modules/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));
@@ -1,9 +0,0 @@
1
- export declare const HandshakeData: import("scale-ts").Codec<{
2
- tag: "V1";
3
- value: [import("../../modules/crypto.js").SsPublicKey, import("../../modules/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>]>;
@@ -1,10 +0,0 @@
1
- import { Bytes, Enum, Tuple, str } from 'scale-ts';
2
- import { EncrPubKey, SsPubKey } from '../../modules/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));
@@ -1,36 +0,0 @@
1
- import type { ResultAsync } from 'neverthrow';
2
- import type { Transport } from '../../modules/transport/transport.js';
3
- import type { UserSession, UserSessionStorage } from '../user/userSessionStorage.js';
4
- import type { AuthentificationStatus } from './types.js';
5
- export type AuthComponent = ReturnType<typeof createAuthComponent>;
6
- type Params = {
7
- /**
8
- * Host app Id.
9
- * CAUTION! This value should be stable.
10
- */
11
- appId: string;
12
- /**
13
- * URL for additional metadata that will be displayed during pairing process.
14
- * Content of provided json shound be
15
- * ```ts
16
- * interface Metadata {
17
- * name: string;
18
- * icon: string; // url for icon. Icon should be a rasterized image with min size 256x256 px.
19
- * }
20
- * ```
21
- */
22
- metadata: string;
23
- transport: Transport;
24
- userSessionStorage: UserSessionStorage;
25
- };
26
- export declare function createAuthComponent({ appId, metadata, transport, userSessionStorage }: Params): {
27
- status: {
28
- read: () => AuthentificationStatus;
29
- subscribe: (fn: (value: AuthentificationStatus) => void) => () => void;
30
- onFirstSubscribe: (callback: VoidFunction) => import("nanoevents").Unsubscribe;
31
- onLastUnsubscribe: (callback: VoidFunction) => import("nanoevents").Unsubscribe;
32
- };
33
- authenticate(): ResultAsync<UserSession | null, Error>;
34
- abortAuthentication(): void;
35
- };
36
- export {};
@@ -1,150 +0,0 @@
1
- import { toHex } from '@polkadot-api/utils';
2
- import { err, errAsync, fromPromise, fromThrowable, ok } from 'neverthrow';
3
- import { AbortError } from '../../helpers/abortError.js';
4
- import { toError } from '../../helpers/utils.js';
5
- import { ENCR_SECRET_SEED_SIZE, SS_SECRET_SEED_SIZE, createEncrSecret, createRandomSeed, createSharedSecret, createSsHardDerivation, createSsSecret, decrypt, getEncrPub, getSsPub, khash, mergeBytes, stringToBytes, } from '../../modules/crypto.js';
6
- import { createAccount } from '../../modules/session/helpers.js';
7
- import { createState, readonly } from '../../modules/state.js';
8
- import { createUserSession } from '../../modules/statementStore.js';
9
- import { HandshakeData, HandshakeResponsePayload, HandshakeResponseSensitiveData } from './codec.js';
10
- export function createAuthComponent({ appId, metadata, transport, userSessionStorage }) {
11
- const authStatus = createState({ step: 'none' });
12
- let authResults = null;
13
- let abort = null;
14
- function handshake(signal) {
15
- try {
16
- authStatus.write({ step: 'initial' });
17
- const { encrSecret, encrPublicKey, ssPublicKey } = getSecretKeys(appId);
18
- const hostAccount = createAccount(ssPublicKey, encrPublicKey);
19
- const handshakePayload = createHandshakePayloadV1({ ssPublicKey, encrPublicKey, metadata }).andTee(payload => authStatus.write({ step: 'pairing', payload: createDeeplink(payload) }));
20
- const handshakeTopic = createHandshakeTopic(hostAccount);
21
- const pappResponse = handshakePayload
22
- .andThen(() => handshakeTopic)
23
- .asyncAndThen(topic => waitForStatements(callback => transport.subscribeSession(topic, callback), signal, (statements, resolve) => {
24
- for (const statement of [...statements].reverse()) {
25
- if (!statement.data)
26
- continue;
27
- const session = retrieveSession({
28
- hostAccount,
29
- encrSecret,
30
- payload: statement.data.asBytes(),
31
- });
32
- resolve(session);
33
- break;
34
- }
35
- }));
36
- const userCreated = pappResponse.andThen(userSessionStorage.add);
37
- return userCreated
38
- .orElse(e => (AbortError.isAbortError(e) ? ok(null) : err(toError(e))))
39
- .andTee(session => {
40
- if (session) {
41
- authStatus.write({ step: 'finished', session });
42
- }
43
- else {
44
- authStatus.write({ step: 'none' });
45
- }
46
- })
47
- .orTee(e => authStatus.write({ step: 'error', message: e.message }));
48
- }
49
- catch (e) {
50
- return errAsync(toError(e));
51
- }
52
- }
53
- const authModule = {
54
- status: readonly(authStatus),
55
- authenticate() {
56
- if (authResults) {
57
- return authResults;
58
- }
59
- abort = new AbortController();
60
- authResults = handshake(abort.signal);
61
- return authResults;
62
- },
63
- abortAuthentication() {
64
- if (abort) {
65
- authResults = null;
66
- authStatus.reset();
67
- abort.abort(new AbortError('Aborted by user.'));
68
- }
69
- },
70
- };
71
- return authModule;
72
- }
73
- const createHandshakeTopic = fromThrowable((account) => khash(account.accountId, mergeBytes(account.publicKey, stringToBytes('topic'))), toError);
74
- const createHandshakePayloadV1 = fromThrowable(({ encrPublicKey, ssPublicKey, metadata, }) => HandshakeData.enc({
75
- tag: 'V1',
76
- value: [ssPublicKey, encrPublicKey, metadata],
77
- }), toError);
78
- function parseHandshakePayload(payload) {
79
- const decoded = HandshakeResponsePayload.dec(payload);
80
- switch (decoded.tag) {
81
- case 'V1':
82
- return {
83
- encrypted: decoded.value[0],
84
- tmpKey: decoded.value[1],
85
- };
86
- default:
87
- throw new Error('Unsupported handshake payload version');
88
- }
89
- }
90
- function retrieveSession({ payload, encrSecret, hostAccount, }) {
91
- const { encrypted, tmpKey } = parseHandshakePayload(payload);
92
- const symmetricKey = createSharedSecret(encrSecret, tmpKey);
93
- const decrypted = decrypt(symmetricKey, encrypted);
94
- const [pappEncrPublicKey, pappAccountId] = HandshakeResponseSensitiveData.dec(decrypted);
95
- const sharedSecret = createSharedSecret(encrSecret, pappEncrPublicKey);
96
- const peerAccount = createAccount(pappAccountId, sharedSecret);
97
- return createUserSession(hostAccount, peerAccount);
98
- }
99
- function getSsKeys(appId) {
100
- const seed = createRandomSeed(appId, SS_SECRET_SEED_SIZE);
101
- const ssSecret = createSsHardDerivation(createSsSecret(seed), '//wallet');
102
- return {
103
- ssSecret: ssSecret,
104
- ssPublicKey: getSsPub(ssSecret),
105
- };
106
- }
107
- function getEncrKeys(appId) {
108
- const seed = createRandomSeed(appId, ENCR_SECRET_SEED_SIZE);
109
- const encrSecret = createEncrSecret(seed);
110
- return {
111
- encrSecret,
112
- encrPublicKey: getEncrPub(encrSecret),
113
- };
114
- }
115
- function getSecretKeys(appId) {
116
- const ss = getSsKeys(appId);
117
- const encr = getEncrKeys(appId);
118
- return {
119
- ...ss,
120
- ...encr,
121
- };
122
- }
123
- function createDeeplink(payload) {
124
- return `polkadotapp://pair?handshake=${toHex(payload)}`;
125
- }
126
- function waitForStatements(subscribe, abortSignal, callback) {
127
- return fromPromise(new Promise((resolve, reject) => {
128
- const unsubscribe = subscribe(statements => {
129
- if (abortSignal?.aborted) {
130
- unsubscribe();
131
- try {
132
- abortSignal.throwIfAborted();
133
- }
134
- catch (e) {
135
- reject(e);
136
- }
137
- }
138
- try {
139
- callback(statements, value => {
140
- unsubscribe();
141
- resolve(value);
142
- });
143
- }
144
- catch (e) {
145
- unsubscribe();
146
- reject(e);
147
- }
148
- });
149
- }), toError);
150
- }
@@ -1,15 +0,0 @@
1
- import type { UserSession } from '../user/userSessionStorage.js';
2
- export type AuthentificationStatus = {
3
- step: 'none';
4
- } | {
5
- step: 'initial';
6
- } | {
7
- step: 'pairing';
8
- payload: string;
9
- } | {
10
- step: 'error';
11
- message: string;
12
- } | {
13
- step: 'finished';
14
- session: UserSession;
15
- };
@@ -1 +0,0 @@
1
- export {};