@novasamatech/host-papp 0.5.0-17 → 0.5.0-19
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.papi/descriptors/generated.json +1 -0
- package/dist/crypto.d.ts +2 -2
- package/dist/crypto.js +8 -7
- package/dist/helpers/abortError.d.ts +0 -1
- package/dist/helpers/abortError.js +0 -3
- package/dist/index.d.ts +1 -1
- package/dist/papp.js +2 -2
- package/dist/sso/auth/attestationService.js +1 -1
- package/dist/sso/auth/impl.d.ts +29 -3
- package/dist/sso/auth/impl.js +60 -35
- package/dist/sso/auth/types.d.ts +13 -2
- package/dist/sso/userSecretRepository.d.ts +1 -1
- package/dist/sso/userSecretRepository.js +2 -2
- package/package.json +5 -5
- package/dist/adapters/identity/rpc.d.ts +0 -6
- package/dist/adapters/identity/rpc.js +0 -101
- package/dist/adapters/identity/types.d.ts +0 -10
- package/dist/adapters/identity/types.js +0 -1
- package/dist/adapters/lazyClient/papi.d.ts +0 -3
- package/dist/adapters/lazyClient/papi.js +0 -17
- package/dist/adapters/lazyClient/types.d.ts +0 -5
- package/dist/adapters/lazyClient/types.js +0 -1
- package/dist/adapters/statement/rpc.d.ts +0 -3
- package/dist/adapters/statement/rpc.js +0 -93
- package/dist/adapters/statement/types.d.ts +0 -9
- package/dist/adapters/statement/types.js +0 -1
- package/dist/adapters/storage/localStorage.d.ts +0 -2
- package/dist/adapters/storage/localStorage.js +0 -34
- package/dist/adapters/storage/memory.d.ts +0 -2
- package/dist/adapters/storage/memory.js +0 -22
- package/dist/adapters/storage/types.d.ts +0 -7
- package/dist/adapters/storage/types.js +0 -1
- package/dist/adapters/transport/rpc.d.ts +0 -3
- package/dist/adapters/transport/rpc.js +0 -51
- package/dist/adapters/transport/types.d.ts +0 -6
- package/dist/adapters/transport/types.js +0 -1
- package/dist/components/auth/codec.d.ts +0 -9
- package/dist/components/auth/codec.js +0 -10
- package/dist/components/auth/codecs.d.ts +0 -9
- package/dist/components/auth/codecs.js +0 -10
- package/dist/components/auth/index.d.ts +0 -36
- package/dist/components/auth/index.js +0 -150
- package/dist/components/auth/types.d.ts +0 -15
- package/dist/components/auth/types.js +0 -1
- package/dist/components/session.d.ts +0 -34
- package/dist/components/session.js +0 -54
- package/dist/components/sso/index.d.ts +0 -36
- package/dist/components/sso/index.js +0 -150
- package/dist/components/sso/scale/handshake.d.ts +0 -9
- package/dist/components/sso/scale/handshake.js +0 -10
- package/dist/components/sso/types.d.ts +0 -15
- package/dist/components/sso/types.js +0 -1
- package/dist/components/transport.d.ts +0 -27
- package/dist/components/transport.js +0 -57
- package/dist/components/user/codec.d.ts +0 -16
- package/dist/components/user/codec.js +0 -13
- package/dist/components/user/index.d.ts +0 -22
- package/dist/components/user/index.js +0 -58
- package/dist/components/user/ssoMessageStream.d.ts +0 -10
- package/dist/components/user/ssoMessageStream.js +0 -8
- package/dist/components/user/ssoSession.d.ts +0 -5
- package/dist/components/user/ssoSession.js +0 -5
- package/dist/components/user/storage.d.ts +0 -27
- package/dist/components/user/storage.js +0 -143
- package/dist/components/user/types.d.ts +0 -6
- package/dist/components/user/types.js +0 -1
- package/dist/components/user/userSessionStorage.d.ts +0 -20
- package/dist/components/user/userSessionStorage.js +0 -24
- package/dist/components/user.d.ts +0 -74
- package/dist/components/user.js +0 -188
- package/dist/helpers/result.d.ts +0 -12
- package/dist/helpers/result.js +0 -15
- package/dist/helpers/result.spec.d.ts +0 -1
- package/dist/helpers/result.spec.js +0 -23
- package/dist/helpers.d.ts +0 -1
- package/dist/helpers.js +0 -3
- package/dist/modules/accounts.d.ts +0 -1
- package/dist/modules/accounts.js +0 -2
- package/dist/modules/crypto.d.ts +0 -23
- package/dist/modules/crypto.js +0 -51
- package/dist/modules/secretStorage.d.ts +0 -15
- package/dist/modules/secretStorage.js +0 -44
- package/dist/modules/session/helpers.d.ts +0 -5
- package/dist/modules/session/helpers.js +0 -29
- package/dist/modules/session/session.d.ts +0 -12
- package/dist/modules/session/session.js +0 -50
- package/dist/modules/session/types.d.ts +0 -12
- package/dist/modules/session/types.js +0 -1
- package/dist/modules/signIn.d.ts +0 -67
- package/dist/modules/signIn.js +0 -188
- package/dist/modules/state.d.ts +0 -16
- package/dist/modules/state.js +0 -50
- package/dist/modules/statementStore.d.ts +0 -12
- package/dist/modules/statementStore.js +0 -22
- package/dist/modules/statementTopic.d.ts +0 -34
- package/dist/modules/statementTopic.js +0 -46
- package/dist/modules/storageView.d.ts +0 -25
- package/dist/modules/storageView.js +0 -51
- package/dist/modules/syncStorage.d.ts +0 -25
- package/dist/modules/syncStorage.js +0 -76
- package/dist/modules/transport/codec.d.ts +0 -24
- package/dist/modules/transport/codec.js +0 -36
- package/dist/modules/transport/crypto.d.ts +0 -2
- package/dist/modules/transport/crypto.js +0 -20
- package/dist/modules/transport/transport.d.ts +0 -42
- package/dist/modules/transport/transport.js +0 -66
- package/dist/modules/user.d.ts +0 -67
- package/dist/modules/user.js +0 -188
- package/dist/modules/userManager.d.ts +0 -15
- package/dist/modules/userManager.js +0 -105
- package/dist/modules/userStorage.d.ts +0 -19
- package/dist/modules/userStorage.js +0 -108
- package/dist/modules/userStore.d.ts +0 -15
- package/dist/modules/userStore.js +0 -105
- package/dist/sso/session/impl.d.ts +0 -23
- package/dist/sso/session/impl.js +0 -57
- package/dist/sso/session/scale/remoteMessage.d.ts +0 -10
- package/dist/sso/session/scale/remoteMessage.js +0 -13
- package/dist/sso/session/sessionManager.d.ts +0 -23
- package/dist/sso/session/sessionManager.js +0 -58
- package/dist/sso/session/ssoSession.d.ts +0 -8
- package/dist/sso/session/ssoSession.js +0 -5
- package/dist/sso/session/ssoSessionStorage.d.ts +0 -21
- package/dist/sso/session/ssoSessionStorage.js +0 -20
- package/dist/sso/session/types.d.ts +0 -6
- package/dist/sso/session/types.js +0 -1
- package/dist/sso/session/userSessionStorage.d.ts +0 -21
- package/dist/sso/session/userSessionStorage.js +0 -20
- package/dist/sso/sessionManager/attestationService.d.ts +0 -5
- package/dist/sso/sessionManager/attestationService.js +0 -15
- package/dist/sso/sessionManager/repository/ssoSessionRepository.d.ts +0 -22
- package/dist/sso/sessionManager/repository/ssoSessionRepository.js +0 -27
- package/dist/sso/sessionManager/scale/signRequest.d.ts +0 -19
- package/dist/sso/sessionManager/scale/signRequest.js +0 -19
- package/dist/sso/sessionManager/scale/signResponse.d.ts +0 -6
- package/dist/sso/sessionManager/scale/signResponse.js +0 -5
- package/dist/sso/sessionManager/ssoSession.d.ts +0 -23
- package/dist/sso/sessionManager/ssoSession.js +0 -69
- package/dist/sso/sessionManager/ssoSessionProver.d.ts +0 -4
- package/dist/sso/sessionManager/ssoSessionProver.js +0 -35
- package/dist/sso/ssoSessionRepository.d.ts +0 -18
- package/dist/sso/ssoSessionRepository.js +0 -27
- package/dist/structs.d.ts +0 -24
- package/dist/structs.js +0 -36
|
@@ -1,42 +0,0 @@
|
|
|
1
|
-
import type { Statement } from '@polkadot-api/sdk-statement';
|
|
2
|
-
import type { ResultAsync } from 'neverthrow';
|
|
3
|
-
import type { Codec } from 'scale-ts';
|
|
4
|
-
import type { StatementAdapter } from '../../adapters/statement/types.js';
|
|
5
|
-
import type { Callback } from '../../types.js';
|
|
6
|
-
import type { SsSecret } from '../crypto.js';
|
|
7
|
-
import type { Account } from '../session/types.js';
|
|
8
|
-
import type { TransportError } from './codec.js';
|
|
9
|
-
export type Transport = ReturnType<typeof createTransport>;
|
|
10
|
-
type RequestMessage<T> = {
|
|
11
|
-
type: 'request';
|
|
12
|
-
requestId: string;
|
|
13
|
-
data: T;
|
|
14
|
-
};
|
|
15
|
-
type ResponseMessage = {
|
|
16
|
-
type: 'response';
|
|
17
|
-
requestId: string;
|
|
18
|
-
code: TransportError;
|
|
19
|
-
};
|
|
20
|
-
type Message<T> = RequestMessage<T> | ResponseMessage;
|
|
21
|
-
type Params = {
|
|
22
|
-
adapter: StatementAdapter;
|
|
23
|
-
};
|
|
24
|
-
export declare function createTransport({ adapter }: Params): {
|
|
25
|
-
subscribeSession(sessionId: Uint8Array, callback: Callback<Statement[]>): VoidFunction;
|
|
26
|
-
subscribe<T>({ ownAccount, peerAccount, codec, }: {
|
|
27
|
-
ownAccount: Account;
|
|
28
|
-
peerAccount: Account;
|
|
29
|
-
codec: Codec<T>;
|
|
30
|
-
}, callback: Callback<Message<T>[]>): VoidFunction;
|
|
31
|
-
handleRequest<T>(params: {
|
|
32
|
-
ownAccount: Account;
|
|
33
|
-
peerAccount: Account;
|
|
34
|
-
codec: Codec<T>;
|
|
35
|
-
}, callback: Callback<RequestMessage<T>>): VoidFunction;
|
|
36
|
-
submitRequest({ ownAccount, peerAccount, secret }: {
|
|
37
|
-
ownAccount: Account;
|
|
38
|
-
peerAccount: Account;
|
|
39
|
-
secret: SsSecret;
|
|
40
|
-
}, message: Uint8Array): ResultAsync<void, Error>;
|
|
41
|
-
};
|
|
42
|
-
export {};
|
|
@@ -1,66 +0,0 @@
|
|
|
1
|
-
import { Result, fromThrowable, ok } from 'neverthrow';
|
|
2
|
-
import { nonNullable, toError } from '../../helpers/utils.js';
|
|
3
|
-
import { decrypt } from '../crypto.js';
|
|
4
|
-
import { createRequestChannel, createSessionId } from '../session/helpers.js';
|
|
5
|
-
import { createStatement } from '../statementStore.js';
|
|
6
|
-
import { StatementData } from './codec.js';
|
|
7
|
-
const decryptResults = fromThrowable(decrypt, toError);
|
|
8
|
-
function mapMessage(statementData) {
|
|
9
|
-
switch (statementData.tag) {
|
|
10
|
-
case 'request':
|
|
11
|
-
return statementData.value.data.map((data, index) => ({
|
|
12
|
-
type: 'request',
|
|
13
|
-
requestId: `${statementData.value.requestId}-${index.toString()}`,
|
|
14
|
-
data,
|
|
15
|
-
}));
|
|
16
|
-
case 'response':
|
|
17
|
-
return [
|
|
18
|
-
{
|
|
19
|
-
type: 'response',
|
|
20
|
-
requestId: statementData.value.requestId,
|
|
21
|
-
code: statementData.value.responseCode,
|
|
22
|
-
},
|
|
23
|
-
];
|
|
24
|
-
}
|
|
25
|
-
}
|
|
26
|
-
export function createTransport({ adapter }) {
|
|
27
|
-
const transport = {
|
|
28
|
-
subscribeSession(sessionId, callback) {
|
|
29
|
-
return adapter.subscribeStatements([sessionId], callback);
|
|
30
|
-
},
|
|
31
|
-
subscribe({ ownAccount, peerAccount, codec, }, callback) {
|
|
32
|
-
const sessionId = createSessionId(peerAccount.publicKey, peerAccount, ownAccount);
|
|
33
|
-
const statementDataCodec = StatementData(codec);
|
|
34
|
-
return adapter.subscribeStatements([sessionId], statements => {
|
|
35
|
-
Result.combine(statements.map(statement => {
|
|
36
|
-
if (!statement.data)
|
|
37
|
-
return ok(null);
|
|
38
|
-
return decryptResults(peerAccount.publicKey, statement.data.asBytes())
|
|
39
|
-
.map(statementDataCodec.dec)
|
|
40
|
-
.orElse(() => ok(null));
|
|
41
|
-
}))
|
|
42
|
-
.map(messages => messages.filter(nonNullable).flatMap(mapMessage))
|
|
43
|
-
.andTee(messages => {
|
|
44
|
-
if (messages.length > 0) {
|
|
45
|
-
callback(messages);
|
|
46
|
-
}
|
|
47
|
-
});
|
|
48
|
-
});
|
|
49
|
-
},
|
|
50
|
-
handleRequest(params, callback) {
|
|
51
|
-
return transport.subscribe(params, messages => {
|
|
52
|
-
messages.filter(m => m.type === 'request').forEach(callback);
|
|
53
|
-
});
|
|
54
|
-
},
|
|
55
|
-
submitRequest({ ownAccount, peerAccount, secret }, message) {
|
|
56
|
-
const sessionId = createSessionId(peerAccount.publicKey, ownAccount, peerAccount);
|
|
57
|
-
return createStatement(secret, {
|
|
58
|
-
channel: createRequestChannel(sessionId),
|
|
59
|
-
priority: 0,
|
|
60
|
-
topics: [sessionId],
|
|
61
|
-
data: message,
|
|
62
|
-
}).andThen(adapter.submitStatement);
|
|
63
|
-
},
|
|
64
|
-
};
|
|
65
|
-
return transport;
|
|
66
|
-
}
|
package/dist/modules/user.d.ts
DELETED
|
@@ -1,67 +0,0 @@
|
|
|
1
|
-
import type { StatementAdapter } from '../adapters/statement/types.js';
|
|
2
|
-
import type { StorageAdapter } from '../adapters/storage/types.js';
|
|
3
|
-
import type { Result } from '../helpers/result.js';
|
|
4
|
-
import type { EncrPublicKey, SsPublicKey } from './crypto.js';
|
|
5
|
-
import type { UserSession } from './userStore.js';
|
|
6
|
-
export declare const HandshakeData: import("scale-ts").Codec<{
|
|
7
|
-
tag: "V1";
|
|
8
|
-
value: [SsPublicKey, EncrPublicKey, string];
|
|
9
|
-
}>;
|
|
10
|
-
export declare const HandshakeResponsePayload: import("scale-ts").Codec<{
|
|
11
|
-
tag: "V1";
|
|
12
|
-
value: [Uint8Array<ArrayBufferLike>, Uint8Array<ArrayBufferLike>];
|
|
13
|
-
}>;
|
|
14
|
-
export declare const HandshakeResponseSensitiveData: import("scale-ts").Codec<[Uint8Array<ArrayBufferLike>, Uint8Array<ArrayBufferLike>]>;
|
|
15
|
-
export type AuthentificationStatus = {
|
|
16
|
-
step: 'none';
|
|
17
|
-
} | {
|
|
18
|
-
step: 'initial';
|
|
19
|
-
} | {
|
|
20
|
-
step: 'pairing';
|
|
21
|
-
payload: string;
|
|
22
|
-
} | {
|
|
23
|
-
step: 'error';
|
|
24
|
-
message: string;
|
|
25
|
-
} | {
|
|
26
|
-
step: 'finished';
|
|
27
|
-
user: UserSession;
|
|
28
|
-
};
|
|
29
|
-
type Params = {
|
|
30
|
-
/**
|
|
31
|
-
* Host app Id.
|
|
32
|
-
* CAUTION! This value should be stable.
|
|
33
|
-
*/
|
|
34
|
-
appId: string;
|
|
35
|
-
/**
|
|
36
|
-
* URL for additional metadata that will be displayed during pairing process.
|
|
37
|
-
* Content of provided json shound be
|
|
38
|
-
* ```ts
|
|
39
|
-
* interface Metadata {
|
|
40
|
-
* name: string;
|
|
41
|
-
* icon: string; // url for icon. Icon should be a rasterized image with min size 256x256 px.
|
|
42
|
-
* }
|
|
43
|
-
* ```
|
|
44
|
-
*/
|
|
45
|
-
metadata: string;
|
|
46
|
-
statements: StatementAdapter;
|
|
47
|
-
storage: StorageAdapter;
|
|
48
|
-
};
|
|
49
|
-
export declare function createUserModule({ appId, metadata, statements, storage }: Params): {
|
|
50
|
-
authStatus: {
|
|
51
|
-
read(): AuthentificationStatus;
|
|
52
|
-
write(value: AuthentificationStatus): void;
|
|
53
|
-
reset(): void;
|
|
54
|
-
subscribe(fn: (value: AuthentificationStatus) => void): import("nanoevents").Unsubscribe;
|
|
55
|
-
};
|
|
56
|
-
storage: {
|
|
57
|
-
readSelectedUserSession(): Promise<Result<UserSession | null>>;
|
|
58
|
-
readUserSession(accountId: string): Promise<Result<UserSession | null>>;
|
|
59
|
-
createUserSession(user: UserSession): Promise<Result<UserSession>>;
|
|
60
|
-
removeUserSession(accountId: string): Promise<Result<void, Error>>;
|
|
61
|
-
readAccounts(): Promise<Result<string[], Error>>;
|
|
62
|
-
selectAccount(accountId: string): Promise<Result<void, Error>>;
|
|
63
|
-
};
|
|
64
|
-
authenticate(): Promise<Result<UserSession | null>>;
|
|
65
|
-
abortAuthentication(): void;
|
|
66
|
-
};
|
|
67
|
-
export {};
|
package/dist/modules/user.js
DELETED
|
@@ -1,188 +0,0 @@
|
|
|
1
|
-
import { toHex } from '@polkadot-api/utils';
|
|
2
|
-
import { Bytes, Enum, Tuple, str } from 'scale-ts';
|
|
3
|
-
import { err, fromPromise, ok, seq } from '../helpers/result.js';
|
|
4
|
-
import { isAbortError, toError } from '../helpers/utils.js';
|
|
5
|
-
import { ENCR_SECRET_SEED_SIZE, EncrPubKey, SS_SECRET_SEED_SIZE, SsPubKey, createEncrSecret, createRandomSeed, createSharedSecret, createSsSecret, createSymmetricKey, decrypt, getEncrPub, getSsPub, khash, mergeBytes, stringToBytes, } from './crypto.js';
|
|
6
|
-
import { createSecretStorage } from './secretStorage.js';
|
|
7
|
-
import { createSession } from './statementStore.js';
|
|
8
|
-
import { createSyncStorage } from './syncStorage.js';
|
|
9
|
-
import { createUserStore } from './userStore.js';
|
|
10
|
-
// codecs
|
|
11
|
-
export const HandshakeData = Enum({
|
|
12
|
-
V1: Tuple(SsPubKey, EncrPubKey, str),
|
|
13
|
-
});
|
|
14
|
-
export const HandshakeResponsePayload = Enum({
|
|
15
|
-
// [encrypted, tmp_key]
|
|
16
|
-
V1: Tuple(Bytes(), Bytes(65)),
|
|
17
|
-
});
|
|
18
|
-
export const HandshakeResponseSensitiveData = Tuple(Bytes(65), Bytes(32));
|
|
19
|
-
export function createUserModule({ appId, metadata, statements, storage }) {
|
|
20
|
-
const userStorage = createUserStore(appId, storage);
|
|
21
|
-
const secretStorage = createSecretStorage(appId, storage);
|
|
22
|
-
const authStatus = createSyncStorage({ step: 'none' });
|
|
23
|
-
let signInPromise = null;
|
|
24
|
-
let abort = null;
|
|
25
|
-
async function handshake(signal) {
|
|
26
|
-
authStatus.write({ step: 'initial' });
|
|
27
|
-
const secrets = await getSecretKeys(appId, secretStorage);
|
|
28
|
-
return secrets.andThenPromise(async ({ ssPublicKey, encrPublicKey, encrSecret }) => {
|
|
29
|
-
const handshakeTopic = createHandshakeTopic({ encrPublicKey, ssPublicKey });
|
|
30
|
-
const handshakePayload = createHandshakePayloadV1({ ssPublicKey, encrPublicKey, metadata });
|
|
31
|
-
authStatus.write({ step: 'pairing', payload: createDeeplink(handshakePayload) });
|
|
32
|
-
const statementStoreResponse = fromPromise(waitForStatements(statements, handshakeTopic, signal, (statements, resolve) => {
|
|
33
|
-
for (const statement of [...statements].reverse()) {
|
|
34
|
-
if (!statement.data)
|
|
35
|
-
continue;
|
|
36
|
-
const { sessionTopic, accountId } = retrieveSessionTopic({
|
|
37
|
-
payload: statement.data.asBytes(),
|
|
38
|
-
encrSecret,
|
|
39
|
-
ssPublicKey,
|
|
40
|
-
});
|
|
41
|
-
resolve({ sessionTopic, accountId: toHex(accountId) });
|
|
42
|
-
break;
|
|
43
|
-
}
|
|
44
|
-
}), toError);
|
|
45
|
-
return statementStoreResponse
|
|
46
|
-
.then(x => x.andThenPromise(userStorage.createUserSession))
|
|
47
|
-
.then(async (result) => result
|
|
48
|
-
.map(user => {
|
|
49
|
-
authStatus.write({ step: 'finished', user });
|
|
50
|
-
return user;
|
|
51
|
-
})
|
|
52
|
-
.orElse(e => {
|
|
53
|
-
const error = toError(e);
|
|
54
|
-
if (isAbortError(error)) {
|
|
55
|
-
authStatus.write({ step: 'none' });
|
|
56
|
-
return ok(null);
|
|
57
|
-
}
|
|
58
|
-
else {
|
|
59
|
-
authStatus.write({ step: 'error', message: error.message });
|
|
60
|
-
return err(error);
|
|
61
|
-
}
|
|
62
|
-
}));
|
|
63
|
-
});
|
|
64
|
-
}
|
|
65
|
-
const userModule = {
|
|
66
|
-
authStatus,
|
|
67
|
-
storage: userStorage,
|
|
68
|
-
async authenticate() {
|
|
69
|
-
if (signInPromise) {
|
|
70
|
-
return signInPromise;
|
|
71
|
-
}
|
|
72
|
-
abort = new AbortController();
|
|
73
|
-
signInPromise = handshake(abort.signal);
|
|
74
|
-
return signInPromise;
|
|
75
|
-
},
|
|
76
|
-
abortAuthentication() {
|
|
77
|
-
if (abort) {
|
|
78
|
-
signInPromise = null;
|
|
79
|
-
authStatus.reset();
|
|
80
|
-
abort.abort();
|
|
81
|
-
}
|
|
82
|
-
},
|
|
83
|
-
};
|
|
84
|
-
return userModule;
|
|
85
|
-
}
|
|
86
|
-
function createHandshakeTopic({ encrPublicKey, ssPublicKey, }) {
|
|
87
|
-
return khash(ssPublicKey, mergeBytes(encrPublicKey, stringToBytes('topic')));
|
|
88
|
-
}
|
|
89
|
-
function createHandshakePayloadV1({ encrPublicKey, ssPublicKey, metadata, }) {
|
|
90
|
-
return HandshakeData.enc({
|
|
91
|
-
tag: 'V1',
|
|
92
|
-
value: [ssPublicKey, encrPublicKey, metadata],
|
|
93
|
-
});
|
|
94
|
-
}
|
|
95
|
-
function parseHandshakePayload(payload) {
|
|
96
|
-
const decoded = HandshakeResponsePayload.dec(payload);
|
|
97
|
-
switch (decoded.tag) {
|
|
98
|
-
case 'V1':
|
|
99
|
-
return {
|
|
100
|
-
encrypted: decoded.value[0],
|
|
101
|
-
tmpKey: decoded.value[1],
|
|
102
|
-
};
|
|
103
|
-
default:
|
|
104
|
-
throw new Error('Unsupported handshake payload version');
|
|
105
|
-
}
|
|
106
|
-
}
|
|
107
|
-
function retrieveSessionTopic({ payload, encrSecret, ssPublicKey, }) {
|
|
108
|
-
const { encrypted, tmpKey } = parseHandshakePayload(payload);
|
|
109
|
-
const symmetricKey = createSymmetricKey(createSharedSecret(encrSecret, tmpKey));
|
|
110
|
-
const decrypted = decrypt(symmetricKey, encrypted);
|
|
111
|
-
const [pappEncrPublicKey, userPublicKey] = HandshakeResponseSensitiveData.dec(decrypted);
|
|
112
|
-
const sharedSecret = createSharedSecret(encrSecret, pappEncrPublicKey);
|
|
113
|
-
const session = createSession({
|
|
114
|
-
sharedSecret: sharedSecret,
|
|
115
|
-
accountA: ssPublicKey,
|
|
116
|
-
accountB: pappEncrPublicKey,
|
|
117
|
-
});
|
|
118
|
-
return {
|
|
119
|
-
accountId: userPublicKey,
|
|
120
|
-
sessionTopic: session.a,
|
|
121
|
-
};
|
|
122
|
-
}
|
|
123
|
-
async function getSsKeys(appId, secretStorage) {
|
|
124
|
-
return (await secretStorage.readSsSecret())
|
|
125
|
-
.andThenPromise(async (ssSecret) => {
|
|
126
|
-
if (ssSecret) {
|
|
127
|
-
return ok(ssSecret);
|
|
128
|
-
}
|
|
129
|
-
const seed = createRandomSeed(appId, SS_SECRET_SEED_SIZE);
|
|
130
|
-
const newSsSecret = createSsSecret(seed);
|
|
131
|
-
const write = await secretStorage.writeSsSecret(newSsSecret);
|
|
132
|
-
return write.map(() => newSsSecret);
|
|
133
|
-
})
|
|
134
|
-
.then(x => x.map(ssSecret => ({
|
|
135
|
-
ssSecret: ssSecret,
|
|
136
|
-
ssPublicKey: getSsPub(ssSecret),
|
|
137
|
-
})));
|
|
138
|
-
}
|
|
139
|
-
async function getEncrKeys(appId, secretStorage) {
|
|
140
|
-
return (await secretStorage.readEncrSecret())
|
|
141
|
-
.andThenPromise(async (encrSecret) => {
|
|
142
|
-
if (encrSecret) {
|
|
143
|
-
return ok(encrSecret);
|
|
144
|
-
}
|
|
145
|
-
const seed = createRandomSeed(appId, ENCR_SECRET_SEED_SIZE);
|
|
146
|
-
const newEncrSecret = createEncrSecret(seed);
|
|
147
|
-
const write = await secretStorage.writeEncrSecret(newEncrSecret);
|
|
148
|
-
return write.map(() => newEncrSecret);
|
|
149
|
-
})
|
|
150
|
-
.then(x => x.map(encrSecret => ({
|
|
151
|
-
encrSecret,
|
|
152
|
-
encrPublicKey: getEncrPub(encrSecret),
|
|
153
|
-
})));
|
|
154
|
-
}
|
|
155
|
-
async function getSecretKeys(appId, secretStorage) {
|
|
156
|
-
return seq(await getSsKeys(appId, secretStorage), await getEncrKeys(appId, secretStorage)).map(([ss, encr]) => ({
|
|
157
|
-
...ss,
|
|
158
|
-
...encr,
|
|
159
|
-
}));
|
|
160
|
-
}
|
|
161
|
-
function createDeeplink(payload) {
|
|
162
|
-
return `polkadotapp://pair?handshake=${toHex(payload)}`;
|
|
163
|
-
}
|
|
164
|
-
function waitForStatements(transport, topic, abortSignal, callback) {
|
|
165
|
-
return new Promise((resolve, reject) => {
|
|
166
|
-
const unsubscribe = transport.subscribeStatements([topic], statements => {
|
|
167
|
-
if (abortSignal?.aborted) {
|
|
168
|
-
unsubscribe();
|
|
169
|
-
try {
|
|
170
|
-
abortSignal.throwIfAborted();
|
|
171
|
-
}
|
|
172
|
-
catch (e) {
|
|
173
|
-
reject(e);
|
|
174
|
-
}
|
|
175
|
-
}
|
|
176
|
-
try {
|
|
177
|
-
callback(statements, value => {
|
|
178
|
-
unsubscribe();
|
|
179
|
-
resolve(value);
|
|
180
|
-
});
|
|
181
|
-
}
|
|
182
|
-
catch (e) {
|
|
183
|
-
unsubscribe();
|
|
184
|
-
reject(e);
|
|
185
|
-
}
|
|
186
|
-
});
|
|
187
|
-
});
|
|
188
|
-
}
|
|
@@ -1,15 +0,0 @@
|
|
|
1
|
-
import type { StorageAdapter } from '../adapters/storage/types.js';
|
|
2
|
-
import type { Result } from '../helpers/result.js';
|
|
3
|
-
import type { SessionTopic } from '../types.js';
|
|
4
|
-
export type User = {
|
|
5
|
-
accountId: string;
|
|
6
|
-
sessionTopic: SessionTopic;
|
|
7
|
-
};
|
|
8
|
-
export declare const createUserManager: (appId: string, storage: StorageAdapter) => {
|
|
9
|
-
readSelectedUser(): Promise<Result<User | null>>;
|
|
10
|
-
readUser(accountId: string): Promise<Result<User | null>>;
|
|
11
|
-
createUser(user: User): Promise<Result<User>>;
|
|
12
|
-
removeUser(accountId: string): Promise<Result<void, Error>>;
|
|
13
|
-
readAccounts(): Promise<Result<string[], Error>>;
|
|
14
|
-
selectAccount(accountId: string): Promise<Result<void, Error>>;
|
|
15
|
-
};
|
|
@@ -1,105 +0,0 @@
|
|
|
1
|
-
import { err, ok, seq } from '../helpers/result.js';
|
|
2
|
-
import { createSecretStorage } from './secretStorage.js';
|
|
3
|
-
export const createUserManager = (appId, storage) => {
|
|
4
|
-
const secretStorage = createSecretStorage(appId, storage);
|
|
5
|
-
const usersStorage = createUserStorage(storage);
|
|
6
|
-
const selectedUserStorage = createSelectedUserStorage(storage);
|
|
7
|
-
const manager = {
|
|
8
|
-
async readSelectedUser() {
|
|
9
|
-
const selectedUser = await selectedUserStorage.read();
|
|
10
|
-
return selectedUser.andThenPromise(async (selectedUser) => {
|
|
11
|
-
if (selectedUser === null) {
|
|
12
|
-
return ok(null);
|
|
13
|
-
}
|
|
14
|
-
const user = await manager.readUser(selectedUser);
|
|
15
|
-
return user.andThenPromise(async (user) => {
|
|
16
|
-
if (user === null) {
|
|
17
|
-
await selectedUserStorage.clear();
|
|
18
|
-
}
|
|
19
|
-
return ok(user);
|
|
20
|
-
});
|
|
21
|
-
});
|
|
22
|
-
},
|
|
23
|
-
async readUser(accountId) {
|
|
24
|
-
const user = await secretStorage.readSessionTopic(accountId).then(existingSessionTopic => existingSessionTopic.map(sessionTopic => {
|
|
25
|
-
return sessionTopic ? { sessionTopic, accountId } : null;
|
|
26
|
-
}));
|
|
27
|
-
await user
|
|
28
|
-
.andThen(v => (v ? ok(undefined) : err('User not found')))
|
|
29
|
-
.orElsePromise(() => manager.removeUser(accountId));
|
|
30
|
-
return user;
|
|
31
|
-
},
|
|
32
|
-
async createUser(user) {
|
|
33
|
-
return (await usersStorage.add(user.accountId)).andThenPromise(async () => seq(await selectedUserStorage.write(user.accountId), await secretStorage.writeSessionTopic(user.accountId, user.sessionTopic)).map(() => user));
|
|
34
|
-
},
|
|
35
|
-
async removeUser(accountId) {
|
|
36
|
-
const op = seq(await selectedUserStorage.read(), await usersStorage.remove(accountId));
|
|
37
|
-
return op.andThenPromise(async ([selectedUser, users]) => {
|
|
38
|
-
if (selectedUser === accountId) {
|
|
39
|
-
const nextSelectedUser = users.at(0);
|
|
40
|
-
if (nextSelectedUser) {
|
|
41
|
-
await selectedUserStorage.write(nextSelectedUser);
|
|
42
|
-
}
|
|
43
|
-
else {
|
|
44
|
-
await selectedUserStorage.clear();
|
|
45
|
-
}
|
|
46
|
-
}
|
|
47
|
-
return secretStorage.clearSessionTopic(accountId);
|
|
48
|
-
});
|
|
49
|
-
},
|
|
50
|
-
async readAccounts() {
|
|
51
|
-
return usersStorage.read();
|
|
52
|
-
},
|
|
53
|
-
async selectAccount(accountId) {
|
|
54
|
-
return selectedUserStorage.write(accountId);
|
|
55
|
-
},
|
|
56
|
-
};
|
|
57
|
-
return manager;
|
|
58
|
-
};
|
|
59
|
-
const createUserStorage = (storage) => {
|
|
60
|
-
const KEY = 'Users';
|
|
61
|
-
return {
|
|
62
|
-
async read() {
|
|
63
|
-
const users = await storage.read(KEY);
|
|
64
|
-
return users.map(users => {
|
|
65
|
-
return users === null ? [] : JSON.parse(users);
|
|
66
|
-
});
|
|
67
|
-
},
|
|
68
|
-
async add(user) {
|
|
69
|
-
const usersRaw = await storage.read(KEY);
|
|
70
|
-
const users = usersRaw.map(x => (x ? JSON.parse(x) : []));
|
|
71
|
-
return users.andThenPromise(async (users) => {
|
|
72
|
-
if (users.some(x => x === user)) {
|
|
73
|
-
throw new Error(`User ${user} already exists.`);
|
|
74
|
-
}
|
|
75
|
-
const newUsers = users.concat(user);
|
|
76
|
-
return storage.write(KEY, JSON.stringify(newUsers)).then(x => x.map(() => newUsers));
|
|
77
|
-
});
|
|
78
|
-
},
|
|
79
|
-
async remove(user) {
|
|
80
|
-
const usersRaw = await storage.read(KEY);
|
|
81
|
-
const users = usersRaw.map(x => (x ? JSON.parse(x) : []));
|
|
82
|
-
return users.andThenPromise(async (users) => {
|
|
83
|
-
const newUsers = users.filter(x => x !== user);
|
|
84
|
-
if (newUsers.length !== users.length) {
|
|
85
|
-
return storage.write(KEY, JSON.stringify(newUsers)).then(x => x.map(() => newUsers));
|
|
86
|
-
}
|
|
87
|
-
return ok([]);
|
|
88
|
-
});
|
|
89
|
-
},
|
|
90
|
-
};
|
|
91
|
-
};
|
|
92
|
-
const createSelectedUserStorage = (storage) => {
|
|
93
|
-
const KEY = 'SelectedUser';
|
|
94
|
-
return {
|
|
95
|
-
read() {
|
|
96
|
-
return storage.read(KEY);
|
|
97
|
-
},
|
|
98
|
-
write(accountId) {
|
|
99
|
-
return storage.write(KEY, accountId);
|
|
100
|
-
},
|
|
101
|
-
clear() {
|
|
102
|
-
return storage.clear(KEY);
|
|
103
|
-
},
|
|
104
|
-
};
|
|
105
|
-
};
|
|
@@ -1,19 +0,0 @@
|
|
|
1
|
-
import type { StorageAdapter } from '../adapters/storage/types.js';
|
|
2
|
-
import type { Result } from '../helpers/result.js';
|
|
3
|
-
import type { SessionTopic } from '../types.js';
|
|
4
|
-
export type UserSession = {
|
|
5
|
-
accountId: string;
|
|
6
|
-
sessionTopic: SessionTopic;
|
|
7
|
-
};
|
|
8
|
-
export declare const createUserStorage: (appId: string, storage: StorageAdapter) => {
|
|
9
|
-
sessions: {
|
|
10
|
-
read(accountId: string): Promise<Result<UserSession | null>>;
|
|
11
|
-
readSelectedUser(): Promise<Result<UserSession | null>>;
|
|
12
|
-
create(user: UserSession): Promise<Result<UserSession>>;
|
|
13
|
-
remove(accountId: string): Promise<Result<void, Error>>;
|
|
14
|
-
};
|
|
15
|
-
accounts: {
|
|
16
|
-
read(): Promise<Result<string[], Error>>;
|
|
17
|
-
select(accountId: string): Promise<Result<string | null, Error> | Result<null, Error>>;
|
|
18
|
-
};
|
|
19
|
-
};
|
|
@@ -1,108 +0,0 @@
|
|
|
1
|
-
import { err, ok, seq } from '../helpers/result.js';
|
|
2
|
-
import { createSecretStorage } from './secretStorage.js';
|
|
3
|
-
import { reactiveStorage } from './syncStorage.js';
|
|
4
|
-
export const createUserStorage = (appId, storage) => {
|
|
5
|
-
const secretStorage = createSecretStorage(appId, storage);
|
|
6
|
-
const usersStorage = createSessionsStorage(storage);
|
|
7
|
-
const selectedUserStorage = createSelectedUserStorage(storage);
|
|
8
|
-
const store = {
|
|
9
|
-
sessions: {
|
|
10
|
-
async read(accountId) {
|
|
11
|
-
const user = await secretStorage.readSessionTopic(accountId).then(existingSessionTopic => existingSessionTopic.map(sessionTopic => {
|
|
12
|
-
return sessionTopic ? { sessionTopic, accountId } : null;
|
|
13
|
-
}));
|
|
14
|
-
await user
|
|
15
|
-
.andThen(v => (v ? ok(undefined) : err('User not found')))
|
|
16
|
-
.orElsePromise(() => store.sessions.remove(accountId));
|
|
17
|
-
return user;
|
|
18
|
-
},
|
|
19
|
-
async readSelectedUser() {
|
|
20
|
-
const selectedUser = await selectedUserStorage.read();
|
|
21
|
-
return selectedUser.andThenPromise(async (selectedUser) => {
|
|
22
|
-
if (selectedUser === null) {
|
|
23
|
-
return ok(null);
|
|
24
|
-
}
|
|
25
|
-
const user = await store.sessions.read(selectedUser);
|
|
26
|
-
return user.andThenPromise(async (user) => {
|
|
27
|
-
if (user === null) {
|
|
28
|
-
await selectedUserStorage.clear();
|
|
29
|
-
}
|
|
30
|
-
return ok(user);
|
|
31
|
-
});
|
|
32
|
-
});
|
|
33
|
-
},
|
|
34
|
-
async create(user) {
|
|
35
|
-
return (await usersStorage.add(user.accountId)).andThenPromise(async () => seq(await selectedUserStorage.write(user.accountId), await secretStorage.writeSessionTopic(user.accountId, user.sessionTopic)).map(() => user));
|
|
36
|
-
},
|
|
37
|
-
async remove(accountId) {
|
|
38
|
-
const op = seq(await selectedUserStorage.read(), await usersStorage.remove(accountId));
|
|
39
|
-
return op.andThenPromise(async ([selectedUser, users]) => {
|
|
40
|
-
if (selectedUser === accountId) {
|
|
41
|
-
const nextSelectedUser = users.at(0);
|
|
42
|
-
if (nextSelectedUser) {
|
|
43
|
-
await selectedUserStorage.write(nextSelectedUser);
|
|
44
|
-
}
|
|
45
|
-
else {
|
|
46
|
-
await selectedUserStorage.clear();
|
|
47
|
-
}
|
|
48
|
-
}
|
|
49
|
-
return secretStorage.clearSessionTopic(accountId);
|
|
50
|
-
});
|
|
51
|
-
},
|
|
52
|
-
},
|
|
53
|
-
accounts: {
|
|
54
|
-
async read() {
|
|
55
|
-
return usersStorage.read();
|
|
56
|
-
},
|
|
57
|
-
async select(accountId) {
|
|
58
|
-
return selectedUserStorage.write(accountId);
|
|
59
|
-
},
|
|
60
|
-
},
|
|
61
|
-
};
|
|
62
|
-
return store;
|
|
63
|
-
};
|
|
64
|
-
const createSessionsStorage = (storage) => {
|
|
65
|
-
const reactive = reactiveStorage({
|
|
66
|
-
storage,
|
|
67
|
-
key: 'Users',
|
|
68
|
-
autosync: true,
|
|
69
|
-
initial: [],
|
|
70
|
-
from: x => JSON.parse(x),
|
|
71
|
-
to: x => JSON.stringify(x),
|
|
72
|
-
});
|
|
73
|
-
return {
|
|
74
|
-
async read() {
|
|
75
|
-
return reactive.read();
|
|
76
|
-
},
|
|
77
|
-
async add(user) {
|
|
78
|
-
const users = await reactive.read();
|
|
79
|
-
return users.andThenPromise(async (users) => {
|
|
80
|
-
if (users.some(x => x === user)) {
|
|
81
|
-
throw new Error(`User ${user} already exists.`);
|
|
82
|
-
}
|
|
83
|
-
const newUsers = users.concat(user);
|
|
84
|
-
return reactive.write(newUsers).then(x => x.map(() => newUsers));
|
|
85
|
-
});
|
|
86
|
-
},
|
|
87
|
-
async remove(user) {
|
|
88
|
-
const users = await reactive.read();
|
|
89
|
-
return users.andThenPromise(async (users) => {
|
|
90
|
-
const newUsers = users.filter(x => x !== user);
|
|
91
|
-
if (newUsers.length !== users.length) {
|
|
92
|
-
return reactive.write(newUsers).then(x => x.map(() => newUsers));
|
|
93
|
-
}
|
|
94
|
-
return ok([]);
|
|
95
|
-
});
|
|
96
|
-
},
|
|
97
|
-
};
|
|
98
|
-
};
|
|
99
|
-
const createSelectedUserStorage = (storage) => {
|
|
100
|
-
return reactiveStorage({
|
|
101
|
-
storage,
|
|
102
|
-
key: 'SelectedUser',
|
|
103
|
-
autosync: true,
|
|
104
|
-
initial: null,
|
|
105
|
-
from: x => x,
|
|
106
|
-
to: x => x,
|
|
107
|
-
});
|
|
108
|
-
};
|
|
@@ -1,15 +0,0 @@
|
|
|
1
|
-
import type { StorageAdapter } from '../adapters/storage/types.js';
|
|
2
|
-
import type { Result } from '../helpers/result.js';
|
|
3
|
-
import type { SessionTopic } from '../types.js';
|
|
4
|
-
export type UserSession = {
|
|
5
|
-
accountId: string;
|
|
6
|
-
sessionTopic: SessionTopic;
|
|
7
|
-
};
|
|
8
|
-
export declare const createUserStore: (appId: string, storage: StorageAdapter) => {
|
|
9
|
-
readSelectedUserSession(): Promise<Result<UserSession | null>>;
|
|
10
|
-
readUserSession(accountId: string): Promise<Result<UserSession | null>>;
|
|
11
|
-
createUserSession(user: UserSession): Promise<Result<UserSession>>;
|
|
12
|
-
removeUserSession(accountId: string): Promise<Result<void, Error>>;
|
|
13
|
-
readAccounts(): Promise<Result<string[], Error>>;
|
|
14
|
-
selectAccount(accountId: string): Promise<Result<void, Error>>;
|
|
15
|
-
};
|