@novasamatech/host-papp 0.5.0-18 → 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/sso/auth/attestationService.js +1 -1
- 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,58 +0,0 @@
|
|
|
1
|
-
import { okAsync } from 'neverthrow';
|
|
2
|
-
import { createSSOSession } from './ssoSession.js';
|
|
3
|
-
export function createUserSessionsComponent({ 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
|
-
// }
|
|
@@ -1,5 +0,0 @@
|
|
|
1
|
-
import type { SessionParams } from '../../modules/session/session.js';
|
|
2
|
-
import type { Session } from '../../modules/session/types.js';
|
|
3
|
-
import { HostRemoteMessageCodec } from './codec.js';
|
|
4
|
-
export type SsoSession = Session<typeof HostRemoteMessageCodec>;
|
|
5
|
-
export declare function createSSOSession({ ownAccount, peerAccount, transport, storage, }: Omit<SessionParams<any>, 'codec'>): SsoSession;
|
|
@@ -1,5 +0,0 @@
|
|
|
1
|
-
import { createSession } from '../../modules/session/session.js';
|
|
2
|
-
import { HostRemoteMessageCodec } from './codec.js';
|
|
3
|
-
export function createSSOSession({ ownAccount, peerAccount, transport, storage, }) {
|
|
4
|
-
return createSession({ ownAccount, peerAccount, transport, storage, codec: HostRemoteMessageCodec });
|
|
5
|
-
}
|
|
@@ -1,27 +0,0 @@
|
|
|
1
|
-
import type { ResultAsync } from 'neverthrow';
|
|
2
|
-
import type { StorageAdapter } from '../../adapters/storage/types.js';
|
|
3
|
-
import type { Callback } from '../../types.js';
|
|
4
|
-
import type { UserSecrets, UserSession } from './types.js';
|
|
5
|
-
export type UserStorage = ReturnType<typeof createUserStorage>;
|
|
6
|
-
type Params = {
|
|
7
|
-
appId: string;
|
|
8
|
-
storage: StorageAdapter;
|
|
9
|
-
};
|
|
10
|
-
export declare const createUserStorage: ({ appId, storage }: Params) => {
|
|
11
|
-
sessions: {
|
|
12
|
-
read(accountId: string): ResultAsync<UserSession | null, Error>;
|
|
13
|
-
readSelectedUser(): ResultAsync<UserSession | null, Error>;
|
|
14
|
-
subscribeSessions(callback: Callback<UserSession[]>): () => void;
|
|
15
|
-
create(user: UserSession, secrets: UserSecrets): ResultAsync<UserSession, Error>;
|
|
16
|
-
remove(accountId: string): ResultAsync<void, Error>;
|
|
17
|
-
readSecrets(accountId: string): ResultAsync<UserSecrets | null, Error>;
|
|
18
|
-
};
|
|
19
|
-
accounts: {
|
|
20
|
-
read(): ResultAsync<string[], Error>;
|
|
21
|
-
subscribe(callback: (accounts: string[]) => void): () => void;
|
|
22
|
-
readSelectedUser(): ResultAsync<string | null, Error>;
|
|
23
|
-
subscribeSelectedAccount(callback: (accountId: string | null) => void): VoidFunction;
|
|
24
|
-
select(accountId: string): ResultAsync<undefined, Error>;
|
|
25
|
-
};
|
|
26
|
-
};
|
|
27
|
-
export {};
|
|
@@ -1,143 +0,0 @@
|
|
|
1
|
-
import { okAsync } from 'neverthrow';
|
|
2
|
-
import { callbackRaceResolver } from '../../helpers/callbackRaceResolver.js';
|
|
3
|
-
import { seq, seqAsync } from '../../helpers/result.js';
|
|
4
|
-
import { nonNullable } from '../../helpers/utils.js';
|
|
5
|
-
import { createSecretStorage } from '../../modules/secretStorage.js';
|
|
6
|
-
import { storageListView, storageView } from '../../modules/storageView.js';
|
|
7
|
-
export const createUserStorage = ({ appId, storage }) => {
|
|
8
|
-
const secretStorage = createSecretStorage(appId, storage);
|
|
9
|
-
const usersStorage = createSessionsStorage(storage);
|
|
10
|
-
const selectedUserStorage = createSelectedUserStorage(storage);
|
|
11
|
-
const store = {
|
|
12
|
-
sessions: {
|
|
13
|
-
read(accountId) {
|
|
14
|
-
const sessions = seqAsync(secretStorage.readSessionId(accountId, 'A'), secretStorage.readSessionId(accountId, 'B'));
|
|
15
|
-
return sessions.map(([sessionIdA, sessionIdB]) => {
|
|
16
|
-
if (nonNullable(sessionIdA) && nonNullable(sessionIdB)) {
|
|
17
|
-
return { accountId, sessionIdA, sessionIdB };
|
|
18
|
-
}
|
|
19
|
-
return null;
|
|
20
|
-
});
|
|
21
|
-
},
|
|
22
|
-
readSelectedUser() {
|
|
23
|
-
return store.accounts.readSelectedUser().andThen(selectedUser => {
|
|
24
|
-
if (selectedUser === null) {
|
|
25
|
-
return okAsync(null);
|
|
26
|
-
}
|
|
27
|
-
return store.sessions.read(selectedUser).andThen(user => {
|
|
28
|
-
if (user === null) {
|
|
29
|
-
return selectedUserStorage.clear().map(() => user);
|
|
30
|
-
}
|
|
31
|
-
return okAsync(user);
|
|
32
|
-
});
|
|
33
|
-
});
|
|
34
|
-
},
|
|
35
|
-
subscribeSessions(callback) {
|
|
36
|
-
const resolver = callbackRaceResolver(callback, async (accounts) => {
|
|
37
|
-
if (accounts.length === 0) {
|
|
38
|
-
return [];
|
|
39
|
-
}
|
|
40
|
-
return seq(...(await Promise.all(accounts.map(store.sessions.read))))
|
|
41
|
-
.map(sessions => sessions.filter(nonNullable))
|
|
42
|
-
.unwrapOr([]);
|
|
43
|
-
});
|
|
44
|
-
return store.accounts.subscribe(resolver);
|
|
45
|
-
},
|
|
46
|
-
create(user, secrets) {
|
|
47
|
-
return usersStorage
|
|
48
|
-
.add(user.accountId)
|
|
49
|
-
.andThen(() => seqAsync(secretStorage.writeSsSecret(user.accountId, secrets.ss), secretStorage.writeEncrSecret(user.accountId, secrets.encr), secretStorage.writeSharedSecret(user.accountId, secrets.sharedSecret), secretStorage.writeSessionId(user.accountId, 'A', user.sessionIdA), secretStorage.writeSessionId(user.accountId, 'B', user.sessionIdB), selectedUserStorage.write(user.accountId)).map(() => user));
|
|
50
|
-
},
|
|
51
|
-
remove(accountId) {
|
|
52
|
-
const op = seqAsync(secretStorage.clearSsSecret(accountId), secretStorage.clearEncrSecret(accountId), secretStorage.clearSharedSecret(accountId), secretStorage.clearSessionId(accountId, 'A'), secretStorage.clearSessionId(accountId, 'B'), usersStorage.remove(accountId), selectedUserStorage.read());
|
|
53
|
-
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
54
|
-
return op.andThen(([_1, _2, _3, _4, _5, users, selectedUser]) => {
|
|
55
|
-
if (selectedUser === accountId) {
|
|
56
|
-
const nextSelectedUser = users.at(0);
|
|
57
|
-
if (nextSelectedUser) {
|
|
58
|
-
return selectedUserStorage.write(nextSelectedUser).map(() => undefined);
|
|
59
|
-
}
|
|
60
|
-
else {
|
|
61
|
-
return selectedUserStorage.clear();
|
|
62
|
-
}
|
|
63
|
-
}
|
|
64
|
-
return okAsync(undefined);
|
|
65
|
-
});
|
|
66
|
-
},
|
|
67
|
-
readSecrets(accountId) {
|
|
68
|
-
const op = seqAsync(secretStorage.readSsSecret(accountId), secretStorage.readEncrSecret(accountId), secretStorage.readSharedSecret(accountId));
|
|
69
|
-
return op.map(([ss, encr, sharedSecret]) => {
|
|
70
|
-
if (nonNullable(ss) && nonNullable(encr) && nonNullable(sharedSecret)) {
|
|
71
|
-
return { ss, encr, sharedSecret };
|
|
72
|
-
}
|
|
73
|
-
return null;
|
|
74
|
-
});
|
|
75
|
-
},
|
|
76
|
-
},
|
|
77
|
-
accounts: {
|
|
78
|
-
read() {
|
|
79
|
-
return usersStorage.read();
|
|
80
|
-
},
|
|
81
|
-
subscribe(callback) {
|
|
82
|
-
return usersStorage.subscribe(callback);
|
|
83
|
-
},
|
|
84
|
-
readSelectedUser() {
|
|
85
|
-
return selectedUserStorage.read();
|
|
86
|
-
},
|
|
87
|
-
subscribeSelectedAccount(callback) {
|
|
88
|
-
return selectedUserStorage.subscribe(callback);
|
|
89
|
-
},
|
|
90
|
-
select(accountId) {
|
|
91
|
-
return selectedUserStorage.write(accountId).map(() => undefined);
|
|
92
|
-
},
|
|
93
|
-
},
|
|
94
|
-
};
|
|
95
|
-
return store;
|
|
96
|
-
};
|
|
97
|
-
const createSessionsStorage = (storage) => {
|
|
98
|
-
const view = storageListView({
|
|
99
|
-
storage,
|
|
100
|
-
key: 'Users',
|
|
101
|
-
autosync: true,
|
|
102
|
-
initial: [],
|
|
103
|
-
from: x => JSON.parse(x),
|
|
104
|
-
to: x => JSON.stringify(x),
|
|
105
|
-
});
|
|
106
|
-
return {
|
|
107
|
-
read() {
|
|
108
|
-
return view.read();
|
|
109
|
-
},
|
|
110
|
-
add(user) {
|
|
111
|
-
return view.mutate(users => {
|
|
112
|
-
if (users.some(x => x === user)) {
|
|
113
|
-
throw new Error(`User ${user} already exists.`);
|
|
114
|
-
}
|
|
115
|
-
return users.concat(user);
|
|
116
|
-
});
|
|
117
|
-
},
|
|
118
|
-
remove(user) {
|
|
119
|
-
return view.mutate(users => {
|
|
120
|
-
const newUsers = users.filter(x => x !== user);
|
|
121
|
-
if (newUsers.length !== users.length) {
|
|
122
|
-
return newUsers;
|
|
123
|
-
}
|
|
124
|
-
return users;
|
|
125
|
-
});
|
|
126
|
-
},
|
|
127
|
-
subscribe(callback) {
|
|
128
|
-
return view.subscribe(v => {
|
|
129
|
-
callback(v ?? []);
|
|
130
|
-
});
|
|
131
|
-
},
|
|
132
|
-
};
|
|
133
|
-
};
|
|
134
|
-
const createSelectedUserStorage = (storage) => {
|
|
135
|
-
return storageView({
|
|
136
|
-
storage,
|
|
137
|
-
key: 'SelectedUser',
|
|
138
|
-
autosync: true,
|
|
139
|
-
initial: null,
|
|
140
|
-
from: x => x,
|
|
141
|
-
to: x => x,
|
|
142
|
-
});
|
|
143
|
-
};
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export {};
|
|
@@ -1,20 +0,0 @@
|
|
|
1
|
-
import type { StorageAdapter } from '../../adapters/storage/types.js';
|
|
2
|
-
import type { Account } from '../../modules/session/types.js';
|
|
3
|
-
export type UserSessionStorage = ReturnType<typeof createUserSessionStorage>;
|
|
4
|
-
export type UserSession = {
|
|
5
|
-
id: string;
|
|
6
|
-
host: Account;
|
|
7
|
-
peer: Account;
|
|
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
|
-
mutate(fn: (value: UserSession[]) => UserSession[]): import("neverthrow").ResultAsync<UserSession[], Error>;
|
|
15
|
-
read(): any;
|
|
16
|
-
write(value: UserSession[]): any;
|
|
17
|
-
clear(): any;
|
|
18
|
-
subscribe(fn: (value: UserSession[]) => void): () => void;
|
|
19
|
-
};
|
|
20
|
-
export {};
|
|
@@ -1,24 +0,0 @@
|
|
|
1
|
-
import { fromHex, toHex } from '@polkadot-api/utils';
|
|
2
|
-
import { Bytes, Option, Struct, Vector, str } from 'scale-ts';
|
|
3
|
-
import { storageListView } from '../../modules/storageView.js';
|
|
4
|
-
const accountCodec = Struct({
|
|
5
|
-
accountId: Bytes(),
|
|
6
|
-
publicKey: Bytes(),
|
|
7
|
-
pin: Option(str),
|
|
8
|
-
});
|
|
9
|
-
const userSessionCodec = Struct({
|
|
10
|
-
id: str,
|
|
11
|
-
host: accountCodec,
|
|
12
|
-
peer: accountCodec,
|
|
13
|
-
});
|
|
14
|
-
const userSessionsCodec = Vector(userSessionCodec);
|
|
15
|
-
export const createUserSessionStorage = ({ storage }) => {
|
|
16
|
-
return storageListView({
|
|
17
|
-
storage,
|
|
18
|
-
key: 'Sessions',
|
|
19
|
-
autosync: true,
|
|
20
|
-
initial: [],
|
|
21
|
-
from: x => userSessionsCodec.dec(fromHex(x)),
|
|
22
|
-
to: x => toHex(userSessionsCodec.enc(x)),
|
|
23
|
-
});
|
|
24
|
-
};
|
|
@@ -1,74 +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 '../modules/crypto.js';
|
|
5
|
-
import type { UserSession } from '../modules/userStorage.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 createUserComponent({ appId, metadata, statements, storage }: Params): {
|
|
50
|
-
authStatus: {
|
|
51
|
-
touched(): boolean;
|
|
52
|
-
read(): AuthentificationStatus;
|
|
53
|
-
write(value: AuthentificationStatus): AuthentificationStatus;
|
|
54
|
-
reset(): void;
|
|
55
|
-
subscribe(fn: (value: AuthentificationStatus) => void): () => void;
|
|
56
|
-
onFirstSubscribe(callback: VoidFunction): import("nanoevents").Unsubscribe;
|
|
57
|
-
onLastUnsubscribe(callback: VoidFunction): import("nanoevents").Unsubscribe;
|
|
58
|
-
};
|
|
59
|
-
storage: {
|
|
60
|
-
sessions: {
|
|
61
|
-
read(accountId: string): Promise<Result<UserSession | null>>;
|
|
62
|
-
readSelectedUser(): Promise<Result<UserSession | null>>;
|
|
63
|
-
create(user: UserSession): Promise<Result<UserSession>>;
|
|
64
|
-
remove(accountId: string): Promise<Result<void, Error>>;
|
|
65
|
-
};
|
|
66
|
-
accounts: {
|
|
67
|
-
read(): Promise<Result<string[], Error>>;
|
|
68
|
-
select(accountId: string): Promise<Result<string | null, Error> | Result<null, Error>>;
|
|
69
|
-
};
|
|
70
|
-
};
|
|
71
|
-
authenticate(): Promise<Result<UserSession | null>>;
|
|
72
|
-
abortAuthentication(): void;
|
|
73
|
-
};
|
|
74
|
-
export {};
|
package/dist/components/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 '../modules/crypto.js';
|
|
6
|
-
import { createSecretStorage } from '../modules/secretStorage.js';
|
|
7
|
-
import { createSession } from '../modules/statementStore.js';
|
|
8
|
-
import { createSyncStorage } from '../modules/syncStorage.js';
|
|
9
|
-
import { createUserStorage } from '../modules/userStorage.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 createUserComponent({ appId, metadata, statements, storage }) {
|
|
20
|
-
const userStorage = createUserStorage(appId, storage);
|
|
21
|
-
const secretStorage = createSecretStorage(appId, storage);
|
|
22
|
-
const authStatus = createSyncStorage({ step: 'none' });
|
|
23
|
-
let authPromise = 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.sessions.create))
|
|
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 (authPromise) {
|
|
70
|
-
return authPromise;
|
|
71
|
-
}
|
|
72
|
-
abort = new AbortController();
|
|
73
|
-
authPromise = handshake(abort.signal);
|
|
74
|
-
return authPromise;
|
|
75
|
-
},
|
|
76
|
-
abortAuthentication() {
|
|
77
|
-
if (abort) {
|
|
78
|
-
authPromise = 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
|
-
}
|
package/dist/helpers/result.d.ts
DELETED
|
@@ -1,12 +0,0 @@
|
|
|
1
|
-
import type { Result, ResultAsync } from 'neverthrow';
|
|
2
|
-
type InferOk<R> = R extends Result<infer Ok, unknown> ? Ok : never;
|
|
3
|
-
type InferOkAsync<R> = R extends ResultAsync<infer Ok, unknown> ? Ok : never;
|
|
4
|
-
type InferErr<R> = R extends Result<unknown, infer Err> ? Err : never;
|
|
5
|
-
type InferErrAsync<R> = R extends ResultAsync<unknown, infer Err> ? Err : never;
|
|
6
|
-
type InferOks<Results> = Results extends [infer Head, ...infer Tail] ? [InferOk<Head>, ...InferOks<Tail>] : Results extends Result<unknown, unknown>[] ? InferOk<Results[number]>[] : [];
|
|
7
|
-
type InferOksAsync<Results> = Results extends [infer Head, ...infer Tail] ? [InferOkAsync<Head>, ...InferOksAsync<Tail>] : Results extends ResultAsync<unknown, unknown>[] ? InferOkAsync<Results[number]>[] : [];
|
|
8
|
-
type SeqResults<Results extends Result<unknown, unknown>[]> = Result<InferOks<Results>, InferErr<Results[number]> | Error>;
|
|
9
|
-
type SeqResultsAsync<Results extends ResultAsync<unknown, unknown>[]> = ResultAsync<InferOksAsync<Results>, InferErrAsync<Results[number]> | Error>;
|
|
10
|
-
export declare function seq<const Results extends Result<unknown, unknown>[]>(...result: Results): SeqResults<Results>;
|
|
11
|
-
export declare function seqAsync<const Results extends ResultAsync<unknown, unknown>[]>(...result: Results): SeqResultsAsync<Results>;
|
|
12
|
-
export {};
|
package/dist/helpers/result.js
DELETED
|
@@ -1,15 +0,0 @@
|
|
|
1
|
-
import { err, errAsync } from 'neverthrow';
|
|
2
|
-
export function seq(...result) {
|
|
3
|
-
const [head, ...tail] = result;
|
|
4
|
-
if (head === undefined) {
|
|
5
|
-
return err(new Error('Seq is empty'));
|
|
6
|
-
}
|
|
7
|
-
return tail.reduce((a, r) => a.andThen(av => r.map(rv => [...av, rv])), head.map(r => [r]));
|
|
8
|
-
}
|
|
9
|
-
export function seqAsync(...result) {
|
|
10
|
-
const [head, ...tail] = result;
|
|
11
|
-
if (head === undefined) {
|
|
12
|
-
return errAsync(new Error('Seq is empty'));
|
|
13
|
-
}
|
|
14
|
-
return tail.reduce((a, r) => a.andThen(av => r.map(rv => [...av, rv])), head.map(r => [r]));
|
|
15
|
-
}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export {};
|
|
@@ -1,23 +0,0 @@
|
|
|
1
|
-
import { err, errAsync, ok, okAsync } from 'neverthrow';
|
|
2
|
-
import { describe, expect, it } from 'vitest';
|
|
3
|
-
import { seq, seqAsync } from './result.js';
|
|
4
|
-
describe('Result', () => {
|
|
5
|
-
describe('seq', () => {
|
|
6
|
-
it('should concat ok values', () => {
|
|
7
|
-
expect(seq(ok(1), ok('2'))._unsafeUnwrap()).toEqual([1, '2']);
|
|
8
|
-
});
|
|
9
|
-
it('should fallback to err', () => {
|
|
10
|
-
expect(seq(ok(1), err('2')).orElse(ok)._unsafeUnwrap()).toEqual('2');
|
|
11
|
-
expect(seq(err(1), ok('2')).orElse(ok)._unsafeUnwrap()).toEqual(1);
|
|
12
|
-
});
|
|
13
|
-
});
|
|
14
|
-
describe('seqAsync', () => {
|
|
15
|
-
it('should concat ok values', async () => {
|
|
16
|
-
expect((await seqAsync(okAsync(1), okAsync('2')))._unsafeUnwrap()).toEqual([1, '2']);
|
|
17
|
-
});
|
|
18
|
-
it('should fallback to err', async () => {
|
|
19
|
-
await expect(seqAsync(okAsync(1), errAsync('2')).then(x => x._unsafeUnwrapErr())).resolves.toEqual('2');
|
|
20
|
-
await expect(seqAsync(errAsync(1), okAsync('2')).then(x => x._unsafeUnwrapErr())).resolves.toEqual(1);
|
|
21
|
-
});
|
|
22
|
-
});
|
|
23
|
-
});
|
package/dist/helpers.d.ts
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export declare function isAbortError(err: object): boolean;
|
package/dist/helpers.js
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export declare function getAccountsFlow(): void;
|
package/dist/modules/accounts.js
DELETED