@novasamatech/host-papp 0.5.0-8 → 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.
- package/.papi/descriptors/dist/common-types.d.ts +8667 -0
- package/.papi/descriptors/dist/common.d.ts +1 -0
- package/.papi/descriptors/dist/descriptors-UUEW32EL.mjs +27 -0
- package/.papi/descriptors/dist/descriptors.d.ts +1 -0
- package/.papi/descriptors/dist/index.d.ts +10 -0
- package/.papi/descriptors/dist/index.js +237 -0
- package/.papi/descriptors/dist/index.mjs +148 -0
- package/.papi/descriptors/dist/metadataTypes-E4AQJDJR.mjs +6 -0
- package/.papi/descriptors/dist/metadataTypes.d.ts +2 -0
- package/.papi/descriptors/dist/people_lite.d.ts +7757 -0
- package/.papi/descriptors/dist/people_lite_metadata-EIVHV27X.mjs +6 -0
- package/.papi/descriptors/dist/people_lite_metadata.d.ts +2 -0
- package/.papi/descriptors/generated.json +1 -0
- package/.papi/descriptors/package.json +24 -0
- package/.papi/metadata/people_lite.scale +0 -0
- package/.papi/polkadot-api.json +15 -0
- package/dist/constants.d.ts +2 -2
- package/dist/constants.js +2 -2
- package/dist/{modules/crypto.d.ts → crypto.d.ts} +14 -12
- package/dist/crypto.js +87 -0
- package/dist/helpers/abortError.d.ts +0 -1
- package/dist/helpers/abortError.js +0 -3
- package/dist/{modules → helpers}/state.d.ts +1 -1
- package/dist/{modules → helpers}/state.js +5 -4
- package/dist/helpers/zipWith.d.ts +4 -0
- package/dist/helpers/zipWith.js +11 -0
- package/dist/identity/impl.d.ts +6 -0
- package/dist/identity/impl.js +68 -0
- package/dist/identity/rpcAdapter.d.ts +3 -0
- package/dist/identity/rpcAdapter.js +46 -0
- package/dist/identity/types.d.ts +21 -0
- package/dist/index.d.ts +7 -4
- package/dist/index.js +2 -7
- package/dist/papp.d.ts +24 -13
- package/dist/papp.js +19 -38
- package/dist/sso/auth/attestationService.d.ts +18 -0
- package/dist/sso/auth/attestationService.js +171 -0
- package/dist/sso/auth/impl.d.ts +79 -0
- package/dist/sso/auth/impl.js +186 -0
- package/dist/{components/auth/codec.d.ts → sso/auth/scale/handshake.d.ts} +3 -3
- package/dist/{components/auth/codec.js → sso/auth/scale/handshake.js} +3 -3
- package/dist/sso/auth/types.d.ts +28 -0
- package/dist/sso/sessionManager/impl.d.ts +22 -0
- package/dist/sso/sessionManager/impl.js +71 -0
- package/dist/sso/sessionManager/scale/hex.d.ts +1 -0
- package/dist/sso/sessionManager/scale/hex.js +3 -0
- package/dist/sso/sessionManager/scale/remoteMessage.d.ts +41 -0
- package/dist/sso/sessionManager/scale/remoteMessage.js +13 -0
- package/dist/sso/sessionManager/scale/signPayloadRequest.d.ts +20 -0
- package/dist/sso/sessionManager/scale/signPayloadRequest.js +20 -0
- package/dist/sso/sessionManager/scale/signPayloadResponse.d.ts +14 -0
- package/dist/sso/sessionManager/scale/signPayloadResponse.js +10 -0
- package/dist/{components/user → sso/sessionManager}/types.d.ts +1 -1
- package/dist/sso/sessionManager/userSession.d.ts +22 -0
- package/dist/sso/sessionManager/userSession.js +116 -0
- package/dist/sso/ssoSessionProver.d.ts +4 -0
- package/dist/sso/ssoSessionProver.js +35 -0
- package/dist/sso/userSecretRepository.d.ts +17 -0
- package/dist/sso/userSecretRepository.js +45 -0
- package/dist/sso/userSessionRepository.d.ts +18 -0
- package/dist/sso/userSessionRepository.js +26 -0
- package/package.json +13 -8
- 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/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/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/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/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/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.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.js +0 -84
- package/dist/modules/secretStorage.d.ts +0 -12
- package/dist/modules/secretStorage.js +0 -40
- 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 -46
- 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/statementStore.d.ts +0 -12
- package/dist/modules/statementStore.js +0 -20
- 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 -18
- package/dist/modules/transport/transport.d.ts +0 -27
- package/dist/modules/transport/transport.js +0 -56
- 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/structs.d.ts +0 -24
- package/dist/structs.js +0 -36
- /package/dist/{adapters/identity → identity}/types.js +0 -0
- /package/dist/{adapters/lazyClient → sso/auth}/types.js +0 -0
- /package/dist/{adapters/statement → sso/sessionManager}/types.js +0 -0
package/dist/modules/crypto.js
DELETED
|
@@ -1,84 +0,0 @@
|
|
|
1
|
-
import { gcm } from '@noble/ciphers/aes.js';
|
|
2
|
-
import { p256 } from '@noble/curves/nist.js';
|
|
3
|
-
import { blake2b } from '@noble/hashes/blake2.js';
|
|
4
|
-
import { hkdf } from '@noble/hashes/hkdf.js';
|
|
5
|
-
import { sha256 } from '@noble/hashes/sha2.js';
|
|
6
|
-
import { randomBytes } from '@noble/hashes/utils.js';
|
|
7
|
-
import { HDKD as sr25519HDKD, getPublicKey as sr25519GetPublicKey, secretFromSeed as sr25519SecretFromSeed, sign as sr25519Sign, } from '@scure/sr25519';
|
|
8
|
-
import { Bytes, str, u32 } from 'scale-ts';
|
|
9
|
-
// schemas
|
|
10
|
-
function brandedBytesCodec(length) {
|
|
11
|
-
return Bytes(length);
|
|
12
|
-
}
|
|
13
|
-
export const SsPubKey = brandedBytesCodec(32);
|
|
14
|
-
export const EncrPubKey = brandedBytesCodec(65);
|
|
15
|
-
// helpers
|
|
16
|
-
const textEncoder = new TextEncoder();
|
|
17
|
-
const textDecoder = new TextDecoder();
|
|
18
|
-
export function stringToBytes(str) {
|
|
19
|
-
return textEncoder.encode(str);
|
|
20
|
-
}
|
|
21
|
-
export function bytesToString(bytes) {
|
|
22
|
-
return textDecoder.decode(bytes);
|
|
23
|
-
}
|
|
24
|
-
export function mergeBytes(...bytes) {
|
|
25
|
-
const len = bytes.reduce((l, b) => l + b.length, 0);
|
|
26
|
-
const merged = new Uint8Array(len);
|
|
27
|
-
let offset = 0;
|
|
28
|
-
for (const arr of bytes) {
|
|
29
|
-
merged.set(arr, offset);
|
|
30
|
-
offset += arr.length;
|
|
31
|
-
}
|
|
32
|
-
return merged;
|
|
33
|
-
}
|
|
34
|
-
// statement store key pair
|
|
35
|
-
export const SS_SECRET_SEED_SIZE = 32;
|
|
36
|
-
export function createSsSecret(seed) {
|
|
37
|
-
return sr25519SecretFromSeed(seed);
|
|
38
|
-
}
|
|
39
|
-
export function createSsHardDerivation(secret, derivation) {
|
|
40
|
-
const chainCode = new Uint8Array(32);
|
|
41
|
-
chainCode.set(typeof derivation === 'string' ? str.enc(derivation) : u32.enc(derivation));
|
|
42
|
-
return sr25519HDKD.secretHard(secret, chainCode);
|
|
43
|
-
}
|
|
44
|
-
export function getSsPub(secret) {
|
|
45
|
-
return sr25519GetPublicKey(secret);
|
|
46
|
-
}
|
|
47
|
-
export function signWithSsSecret(secret, message) {
|
|
48
|
-
return sr25519Sign(secret, message);
|
|
49
|
-
}
|
|
50
|
-
// encryption key pair
|
|
51
|
-
export const ENCR_SECRET_SEED_SIZE = 48;
|
|
52
|
-
export function createEncrSecret(seed) {
|
|
53
|
-
const { secretKey } = p256.keygen(seed);
|
|
54
|
-
return secretKey;
|
|
55
|
-
}
|
|
56
|
-
export function getEncrPub(secret) {
|
|
57
|
-
return p256.getPublicKey(secret, false);
|
|
58
|
-
}
|
|
59
|
-
// helpers
|
|
60
|
-
export function createRandomSeed(suffix, size) {
|
|
61
|
-
return blake2b(mergeBytes(randomBytes(128), stringToBytes(suffix)), { dkLen: size });
|
|
62
|
-
}
|
|
63
|
-
export function createStableSeed(value, size) {
|
|
64
|
-
return blake2b(stringToBytes(value), { dkLen: size });
|
|
65
|
-
}
|
|
66
|
-
export function khash(secret, message) {
|
|
67
|
-
return blake2b(message, { dkLen: 256 / 8, key: secret });
|
|
68
|
-
}
|
|
69
|
-
export function createSharedSecret(secret, publicKey) {
|
|
70
|
-
return p256.getSharedSecret(secret, publicKey).slice(1, 33);
|
|
71
|
-
}
|
|
72
|
-
export function encrypt(secret, cipherText) {
|
|
73
|
-
const nonce = randomBytes(12);
|
|
74
|
-
const aesKey = hkdf(sha256, secret, new Uint8Array(), new Uint8Array(), 32);
|
|
75
|
-
const aes = gcm(aesKey, nonce);
|
|
76
|
-
return aes.encrypt(mergeBytes(nonce, cipherText));
|
|
77
|
-
}
|
|
78
|
-
export function decrypt(secret, message) {
|
|
79
|
-
const nonce = message.slice(0, 12);
|
|
80
|
-
const cipherText = message.slice(12);
|
|
81
|
-
const aesKey = hkdf(sha256, secret, new Uint8Array(), new Uint8Array(), 32);
|
|
82
|
-
const aes = gcm(aesKey, nonce);
|
|
83
|
-
return aes.decrypt(cipherText);
|
|
84
|
-
}
|
|
@@ -1,12 +0,0 @@
|
|
|
1
|
-
import type { ResultAsync } from 'neverthrow';
|
|
2
|
-
import type { StorageAdapter } from '../adapters/storage/types.js';
|
|
3
|
-
import type { EncrSecret, SsSecret } from './crypto.js';
|
|
4
|
-
export type SecretStorage = {
|
|
5
|
-
readSsSecret(accountId: string): ResultAsync<SsSecret | null, Error>;
|
|
6
|
-
writeSsSecret(accountId: string, value: SsSecret): ResultAsync<void, Error>;
|
|
7
|
-
clearSsSecret(accountId: string): ResultAsync<void, Error>;
|
|
8
|
-
readEncrSecret(accountId: string): ResultAsync<EncrSecret | null, Error>;
|
|
9
|
-
writeEncrSecret(accountId: string, value: EncrSecret): ResultAsync<void, Error>;
|
|
10
|
-
clearEncrSecret(accountId: string): ResultAsync<void, Error>;
|
|
11
|
-
};
|
|
12
|
-
export declare function createSecretStorage(appId: string, storage: StorageAdapter): SecretStorage;
|
|
@@ -1,40 +0,0 @@
|
|
|
1
|
-
import { gcm } from '@noble/ciphers/aes.js';
|
|
2
|
-
import { blake2b } from '@noble/hashes/blake2.js';
|
|
3
|
-
import { fromHex, toHex } from '@polkadot-api/utils';
|
|
4
|
-
import { stringToBytes } from './crypto.js';
|
|
5
|
-
export function createSecretStorage(appId, storage) {
|
|
6
|
-
const ssSecret = rwBytes('SsSecret', appId, storage);
|
|
7
|
-
const encrSecret = rwBytes('EncrSecret', appId, storage);
|
|
8
|
-
return {
|
|
9
|
-
readSsSecret: ssSecret.read,
|
|
10
|
-
writeSsSecret: ssSecret.write,
|
|
11
|
-
clearSsSecret: ssSecret.clear,
|
|
12
|
-
readEncrSecret: encrSecret.read,
|
|
13
|
-
writeEncrSecret: encrSecret.write,
|
|
14
|
-
clearEncrSecret: encrSecret.clear,
|
|
15
|
-
};
|
|
16
|
-
}
|
|
17
|
-
const createKey = (key, context) => `${key}_${context}`;
|
|
18
|
-
const rwBytes = (baseKey, appId, storage) => ({
|
|
19
|
-
read(context) {
|
|
20
|
-
return storage.read(createKey(baseKey, context)).map(value => {
|
|
21
|
-
if (value) {
|
|
22
|
-
const aes = getAes(appId);
|
|
23
|
-
return aes.decrypt(fromHex(value));
|
|
24
|
-
}
|
|
25
|
-
else {
|
|
26
|
-
return null;
|
|
27
|
-
}
|
|
28
|
-
});
|
|
29
|
-
},
|
|
30
|
-
write(context, value) {
|
|
31
|
-
const aes = getAes(appId);
|
|
32
|
-
return storage.write(createKey(baseKey, context), toHex(aes.encrypt(value)));
|
|
33
|
-
},
|
|
34
|
-
clear(context) {
|
|
35
|
-
return storage.clear(createKey(baseKey, context));
|
|
36
|
-
},
|
|
37
|
-
});
|
|
38
|
-
function getAes(appId) {
|
|
39
|
-
return gcm(blake2b(stringToBytes(appId), { dkLen: 16 }), blake2b(stringToBytes('nonce'), { dkLen: 32 }));
|
|
40
|
-
}
|
|
@@ -1,5 +0,0 @@
|
|
|
1
|
-
import type { Account } from './types.js';
|
|
2
|
-
export declare function createSessionId(accountA: Account, accountB: Account): Uint8Array<ArrayBufferLike>;
|
|
3
|
-
export declare function createRequestChannel(session: Uint8Array): Uint8Array<ArrayBufferLike>;
|
|
4
|
-
export declare function createResponseChannel(session: Uint8Array): Uint8Array<ArrayBufferLike>;
|
|
5
|
-
export declare function createAccount(accountId: Uint8Array, publicKey: Uint8Array, pin?: string): Account;
|
|
@@ -1,29 +0,0 @@
|
|
|
1
|
-
import { mergeUint8 } from '@polkadot-api/utils';
|
|
2
|
-
import { khash, stringToBytes } from '../crypto.js';
|
|
3
|
-
export function createSessionId(accountA, accountB) {
|
|
4
|
-
const sessionPrefix = stringToBytes('session');
|
|
5
|
-
const pinSeparator = stringToBytes('/');
|
|
6
|
-
function makePin(pin) {
|
|
7
|
-
return pin ? mergeUint8([pinSeparator, stringToBytes(pin)]) : pinSeparator;
|
|
8
|
-
}
|
|
9
|
-
const accountSessionParams = mergeUint8([
|
|
10
|
-
accountA.accountId,
|
|
11
|
-
accountB.accountId,
|
|
12
|
-
makePin(accountA.pin),
|
|
13
|
-
makePin(accountB.pin),
|
|
14
|
-
]);
|
|
15
|
-
return khash(accountA.publicKey, mergeUint8(sessionPrefix, accountSessionParams));
|
|
16
|
-
}
|
|
17
|
-
export function createRequestChannel(session) {
|
|
18
|
-
return khash(session, stringToBytes('request'));
|
|
19
|
-
}
|
|
20
|
-
export function createResponseChannel(session) {
|
|
21
|
-
return khash(session, stringToBytes('response'));
|
|
22
|
-
}
|
|
23
|
-
export function createAccount(accountId, publicKey, pin) {
|
|
24
|
-
return {
|
|
25
|
-
accountId,
|
|
26
|
-
publicKey,
|
|
27
|
-
pin,
|
|
28
|
-
};
|
|
29
|
-
}
|
|
@@ -1,12 +0,0 @@
|
|
|
1
|
-
import type { Codec } from 'scale-ts';
|
|
2
|
-
import type { StorageAdapter } from '../../adapters/storage/types.js';
|
|
3
|
-
import type { Transport } from '../transport/transport.js';
|
|
4
|
-
import type { Account, Session } from './types.js';
|
|
5
|
-
export type SessionParams<T extends Codec<any>> = {
|
|
6
|
-
ownAccount: Account;
|
|
7
|
-
peerAccount: Account;
|
|
8
|
-
transport: Transport;
|
|
9
|
-
storage: StorageAdapter;
|
|
10
|
-
codec: T;
|
|
11
|
-
};
|
|
12
|
-
export declare function createSession<T extends Codec<any>>({ ownAccount, peerAccount, transport, storage, codec, }: SessionParams<T>): Session<T>;
|
|
@@ -1,46 +0,0 @@
|
|
|
1
|
-
import { okAsync } from 'neverthrow';
|
|
2
|
-
import { toHex } from 'polkadot-api/utils';
|
|
3
|
-
import { storageListView } from '../storageView.js';
|
|
4
|
-
import { createSessionId } from './helpers.js';
|
|
5
|
-
export function createSession({ ownAccount, peerAccount, transport, storage, codec, }) {
|
|
6
|
-
// const ownSession = createSessionId(ownAccount, peerAccount);
|
|
7
|
-
const peerSession = createSessionId(peerAccount, ownAccount);
|
|
8
|
-
const processedStorage = storageListView({
|
|
9
|
-
key: `ProcessesMessages_${toHex(peerSession)}`,
|
|
10
|
-
storage,
|
|
11
|
-
initial: [],
|
|
12
|
-
from: JSON.parse,
|
|
13
|
-
to: JSON.stringify,
|
|
14
|
-
});
|
|
15
|
-
let subscriptions = [];
|
|
16
|
-
return {
|
|
17
|
-
subscribe(callback) {
|
|
18
|
-
const unsub = transport.handleRequest(ownAccount, peerAccount, codec, async (message) => {
|
|
19
|
-
processedStorage.read().andThen(processed => {
|
|
20
|
-
if (processed.includes(message.requestId)) {
|
|
21
|
-
return okAsync();
|
|
22
|
-
}
|
|
23
|
-
return callback(message.data).andThen(processed => {
|
|
24
|
-
if (processed) {
|
|
25
|
-
return processedStorage.mutate(p => p.concat(message.requestId));
|
|
26
|
-
}
|
|
27
|
-
else {
|
|
28
|
-
return okAsync();
|
|
29
|
-
}
|
|
30
|
-
});
|
|
31
|
-
});
|
|
32
|
-
});
|
|
33
|
-
subscriptions.push(unsub);
|
|
34
|
-
return () => {
|
|
35
|
-
unsub();
|
|
36
|
-
subscriptions = subscriptions.filter(x => x !== unsub);
|
|
37
|
-
};
|
|
38
|
-
},
|
|
39
|
-
dispose() {
|
|
40
|
-
for (const unsub of subscriptions) {
|
|
41
|
-
unsub();
|
|
42
|
-
}
|
|
43
|
-
subscriptions = [];
|
|
44
|
-
},
|
|
45
|
-
};
|
|
46
|
-
}
|
|
@@ -1,12 +0,0 @@
|
|
|
1
|
-
import type { ResultAsync } from 'neverthrow';
|
|
2
|
-
import type { Codec, CodecType } from 'scale-ts';
|
|
3
|
-
import type { Callback } from '../../types.js';
|
|
4
|
-
export type Session<T extends Codec<any>> = {
|
|
5
|
-
subscribe(callback: Callback<CodecType<T>, ResultAsync<boolean, Error>>): VoidFunction;
|
|
6
|
-
dispose(): void;
|
|
7
|
-
};
|
|
8
|
-
export type Account = {
|
|
9
|
-
accountId: Uint8Array;
|
|
10
|
-
publicKey: Uint8Array;
|
|
11
|
-
pin: string | undefined;
|
|
12
|
-
};
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export {};
|
package/dist/modules/signIn.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 { User } from './userManager.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 SignInStatus = {
|
|
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: User;
|
|
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 createSignInFlow({ appId, metadata, statements, storage }: Params): {
|
|
50
|
-
signInStatus: {
|
|
51
|
-
read(): SignInStatus;
|
|
52
|
-
write(value: SignInStatus): void;
|
|
53
|
-
reset(): void;
|
|
54
|
-
subscribe(fn: (value: SignInStatus) => void): import("nanoevents").Unsubscribe;
|
|
55
|
-
};
|
|
56
|
-
users: {
|
|
57
|
-
readSelectedUser(): Promise<Result<User | null>>;
|
|
58
|
-
readUser(accountId: string): Promise<Result<User | null>>;
|
|
59
|
-
createUser(user: User): Promise<Result<User>>;
|
|
60
|
-
removeUser(accountId: string): Promise<Result<void, Error>>;
|
|
61
|
-
readAccounts(): Promise<Result<string[], Error>>;
|
|
62
|
-
selectAccount(accountId: string): Promise<Result<void, Error>>;
|
|
63
|
-
};
|
|
64
|
-
signIn(): Promise<Result<User | null>>;
|
|
65
|
-
abortSignIn(): void;
|
|
66
|
-
};
|
|
67
|
-
export {};
|
package/dist/modules/signIn.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 { createUserManager } from './userManager.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 createSignInFlow({ appId, metadata, statements, storage }) {
|
|
20
|
-
const userManager = createUserManager(appId, storage);
|
|
21
|
-
const secretStorage = createSecretStorage(appId, storage);
|
|
22
|
-
const signInStatus = createSyncStorage({ step: 'none' });
|
|
23
|
-
let signInPromise = null;
|
|
24
|
-
let abort = null;
|
|
25
|
-
async function handshake(signal) {
|
|
26
|
-
signInStatus.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
|
-
signInStatus.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(userManager.createUser))
|
|
47
|
-
.then(async (result) => result
|
|
48
|
-
.map(user => {
|
|
49
|
-
signInStatus.write({ step: 'finished', user });
|
|
50
|
-
return user;
|
|
51
|
-
})
|
|
52
|
-
.orElse(e => {
|
|
53
|
-
const error = toError(e);
|
|
54
|
-
if (isAbortError(error)) {
|
|
55
|
-
signInStatus.write({ step: 'none' });
|
|
56
|
-
return ok(null);
|
|
57
|
-
}
|
|
58
|
-
else {
|
|
59
|
-
signInStatus.write({ step: 'error', message: error.message });
|
|
60
|
-
return err(error);
|
|
61
|
-
}
|
|
62
|
-
}));
|
|
63
|
-
});
|
|
64
|
-
}
|
|
65
|
-
const signInFlow = {
|
|
66
|
-
signInStatus,
|
|
67
|
-
users: userManager,
|
|
68
|
-
async signIn() {
|
|
69
|
-
if (signInPromise) {
|
|
70
|
-
return signInPromise;
|
|
71
|
-
}
|
|
72
|
-
abort = new AbortController();
|
|
73
|
-
signInPromise = handshake(abort.signal);
|
|
74
|
-
return signInPromise;
|
|
75
|
-
},
|
|
76
|
-
abortSignIn() {
|
|
77
|
-
if (abort) {
|
|
78
|
-
signInPromise = null;
|
|
79
|
-
signInStatus.reset();
|
|
80
|
-
abort.abort();
|
|
81
|
-
}
|
|
82
|
-
},
|
|
83
|
-
};
|
|
84
|
-
return signInFlow;
|
|
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,12 +0,0 @@
|
|
|
1
|
-
import type { UserSession } from '../components/user/userSessionStorage.js';
|
|
2
|
-
import type { SsSecret } from './crypto.js';
|
|
3
|
-
import type { Account } from './session/types.js';
|
|
4
|
-
export declare function createUserSession(hostAccount: Account, peerAccount: Account): UserSession;
|
|
5
|
-
type StatementPayload = {
|
|
6
|
-
priority: number;
|
|
7
|
-
channel: Uint8Array;
|
|
8
|
-
topics: Uint8Array[];
|
|
9
|
-
data: Uint8Array;
|
|
10
|
-
};
|
|
11
|
-
export declare function createStatement(secret: SsSecret, payload: StatementPayload): Promise<import("@polkadot-api/sdk-statement").SignedStatement>;
|
|
12
|
-
export {};
|
|
@@ -1,20 +0,0 @@
|
|
|
1
|
-
import { getStatementSigner } from '@polkadot-api/sdk-statement';
|
|
2
|
-
import { Binary } from '@polkadot-api/substrate-bindings';
|
|
3
|
-
import { nanoid } from 'nanoid';
|
|
4
|
-
import { getSsPub, signWithSsSecret } from './crypto.js';
|
|
5
|
-
export function createUserSession(hostAccount, peerAccount) {
|
|
6
|
-
return {
|
|
7
|
-
id: nanoid(12),
|
|
8
|
-
peer: peerAccount,
|
|
9
|
-
host: hostAccount,
|
|
10
|
-
};
|
|
11
|
-
}
|
|
12
|
-
export function createStatement(secret, payload) {
|
|
13
|
-
const signer = getStatementSigner(getSsPub(secret), 'sr25519', data => signWithSsSecret(secret, data));
|
|
14
|
-
return signer.sign({
|
|
15
|
-
priority: payload.priority,
|
|
16
|
-
channel: Binary.fromBytes(payload.channel),
|
|
17
|
-
topics: payload.topics.map(Binary.fromBytes),
|
|
18
|
-
data: Binary.fromBytes(payload.data),
|
|
19
|
-
});
|
|
20
|
-
}
|
|
@@ -1,34 +0,0 @@
|
|
|
1
|
-
import type { Statement } from '@polkadot-api/sdk-statement';
|
|
2
|
-
import type { StatementAdapter } from '../adapters/statement/types.js';
|
|
3
|
-
import type { StorageAdapter } from '../adapters/storage/types.js';
|
|
4
|
-
import type { Callback } from '../types.js';
|
|
5
|
-
import type { SsSecret } from './crypto.js';
|
|
6
|
-
export declare function createSession({ sharedSecret, accountA, accountB, pinA, pinB, }: {
|
|
7
|
-
sharedSecret: Uint8Array;
|
|
8
|
-
accountA: Uint8Array;
|
|
9
|
-
accountB: Uint8Array;
|
|
10
|
-
pinA?: string;
|
|
11
|
-
pinB?: string;
|
|
12
|
-
}): {
|
|
13
|
-
a: Uint8Array<ArrayBufferLike>;
|
|
14
|
-
b: Uint8Array<ArrayBufferLike>;
|
|
15
|
-
};
|
|
16
|
-
export declare function createRequestChannel(session: Uint8Array): Uint8Array<ArrayBufferLike>;
|
|
17
|
-
export declare function createResponseChannel(session: Uint8Array): Uint8Array<ArrayBufferLike>;
|
|
18
|
-
type StatementPayload = {
|
|
19
|
-
priority: number;
|
|
20
|
-
channel: Uint8Array;
|
|
21
|
-
topics: Uint8Array[];
|
|
22
|
-
data: Uint8Array;
|
|
23
|
-
};
|
|
24
|
-
export declare function createStatement(secret: SsSecret, payload: StatementPayload): Promise<import("@polkadot-api/sdk-statement").SignedStatement>;
|
|
25
|
-
type Params = {
|
|
26
|
-
topic: Uint8Array;
|
|
27
|
-
statement: StatementAdapter;
|
|
28
|
-
storage: StorageAdapter;
|
|
29
|
-
};
|
|
30
|
-
export declare function createStatementTopic({ topic, statement }: Params): {
|
|
31
|
-
subscribe(callback: Callback<Statement, boolean>): VoidFunction;
|
|
32
|
-
submit(): void;
|
|
33
|
-
};
|
|
34
|
-
export {};
|
|
@@ -1,46 +0,0 @@
|
|
|
1
|
-
import { getStatementSigner } from '@polkadot-api/sdk-statement';
|
|
2
|
-
import { Binary } from '@polkadot-api/substrate-bindings';
|
|
3
|
-
import { getSsPub, khash, mergeBytes, signWithSsSecret, stringToBytes } from './crypto.js';
|
|
4
|
-
export function createSession({ sharedSecret, accountA, accountB, pinA, pinB, }) {
|
|
5
|
-
const sessionPrefix = stringToBytes('session');
|
|
6
|
-
const pinSeparator = stringToBytes('/');
|
|
7
|
-
function makePin(pin) {
|
|
8
|
-
return pin ? mergeBytes(pinSeparator, stringToBytes(pin)) : pinSeparator;
|
|
9
|
-
}
|
|
10
|
-
const accountASessionParams = mergeBytes(sessionPrefix, accountA, accountB, makePin(pinA), makePin(pinB));
|
|
11
|
-
const accountBSessionParams = mergeBytes(sessionPrefix, accountB, accountA, makePin(pinB), makePin(pinA));
|
|
12
|
-
return {
|
|
13
|
-
a: khash(sharedSecret, accountASessionParams),
|
|
14
|
-
b: khash(sharedSecret, accountBSessionParams),
|
|
15
|
-
};
|
|
16
|
-
}
|
|
17
|
-
export function createRequestChannel(session) {
|
|
18
|
-
return khash(session, stringToBytes('request'));
|
|
19
|
-
}
|
|
20
|
-
export function createResponseChannel(session) {
|
|
21
|
-
return khash(session, stringToBytes('response'));
|
|
22
|
-
}
|
|
23
|
-
export function createStatement(secret, payload) {
|
|
24
|
-
const signer = getStatementSigner(getSsPub(secret), 'sr25519', data => signWithSsSecret(secret, data));
|
|
25
|
-
return signer.sign({
|
|
26
|
-
priority: payload.priority,
|
|
27
|
-
channel: Binary.fromBytes(payload.channel),
|
|
28
|
-
topics: payload.topics.map(Binary.fromBytes),
|
|
29
|
-
data: Binary.fromBytes(payload.data),
|
|
30
|
-
});
|
|
31
|
-
}
|
|
32
|
-
export function createStatementTopic({ topic, statement }) {
|
|
33
|
-
return {
|
|
34
|
-
subscribe(callback) {
|
|
35
|
-
return statement.subscribeStatements([topic], statements => {
|
|
36
|
-
for (const statement of statements) {
|
|
37
|
-
const processed = callback(statement);
|
|
38
|
-
if (processed) {
|
|
39
|
-
// TODO save anywhere
|
|
40
|
-
}
|
|
41
|
-
}
|
|
42
|
-
});
|
|
43
|
-
},
|
|
44
|
-
submit() { },
|
|
45
|
-
};
|
|
46
|
-
}
|
|
@@ -1,25 +0,0 @@
|
|
|
1
|
-
import type { ResultAsync } from 'neverthrow';
|
|
2
|
-
import type { StorageAdapter } from '../adapters/storage/types.js';
|
|
3
|
-
type Params<T> = {
|
|
4
|
-
storage: StorageAdapter;
|
|
5
|
-
key: string;
|
|
6
|
-
initial: T;
|
|
7
|
-
autosync?: boolean;
|
|
8
|
-
from(value: string): T;
|
|
9
|
-
to(value: T): string | null;
|
|
10
|
-
};
|
|
11
|
-
export declare function storageView<T>({ storage, initial, key, from, to, autosync }: Params<T>): {
|
|
12
|
-
read(): ResultAsync<T, Error>;
|
|
13
|
-
write(value: T): ResultAsync<T, Error> | ResultAsync<null, Error>;
|
|
14
|
-
clear(): ResultAsync<void, Error>;
|
|
15
|
-
subscribe(fn: (value: T) => void): () => void;
|
|
16
|
-
};
|
|
17
|
-
export declare function storageListView<T>(params: Params<T[]>): {
|
|
18
|
-
add(value: T): ResultAsync<T, Error>;
|
|
19
|
-
mutate(fn: (value: T[]) => T[]): ResultAsync<T[], Error>;
|
|
20
|
-
read(): ResultAsync<T[], Error>;
|
|
21
|
-
write(value: T[]): ResultAsync<T[], Error> | ResultAsync<null, Error>;
|
|
22
|
-
clear(): ResultAsync<void, Error>;
|
|
23
|
-
subscribe(fn: (value: T[]) => void): () => void;
|
|
24
|
-
};
|
|
25
|
-
export {};
|