Skip to content
This repository has been archived by the owner on Jan 24, 2023. It is now read-only.

Commit

Permalink
Address most review comments
Browse files Browse the repository at this point in the history
  • Loading branch information
mrcnski committed Feb 11, 2022
1 parent 307b83a commit 58d6196
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 29 deletions.
5 changes: 3 additions & 2 deletions src/mysky.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ export const PORTAL_STORAGE_KEY = "portal";
export const SEED_STORAGE_KEY = "seed";

export const PORTAL_LOGIN_COMPLETE_SENTINEL_KEY = "portal-login-complete";
const PORTAL_LOGIN_COMPLETE_SUCCESS_VALUE = "1";

export const INITIAL_PORTAL = "https://siasky.net";

Expand Down Expand Up @@ -717,7 +718,7 @@ export class MySky {
await this.loginFromUi(seed);

// Signal to MySky UI that we are done.
localStorage.setItem(PORTAL_LOGIN_COMPLETE_SENTINEL_KEY, "1");
localStorage.setItem(PORTAL_LOGIN_COMPLETE_SENTINEL_KEY, PORTAL_LOGIN_COMPLETE_SUCCESS_VALUE);
} catch (e) {
log(`Error in storage event listener: ${e}`);

Expand Down Expand Up @@ -888,7 +889,7 @@ export async function getCurrentAndReferrerDomains(): Promise<{
// anywhere else for now.
let currentDomain;
if (ALPHA_ENABLED && DEV_ENABLED) {
throw new Error("Alpha and dev modes cannot both be enbaled");
throw new Error("Alpha and dev modes cannot both be enabled");
} else if (ALPHA_ENABLED) {
currentDomain = "sandbridge.hns";
} else if (DEV_ENABLED) {
Expand Down
4 changes: 4 additions & 0 deletions src/provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ export type SeedProviderResponse = {
action: SeedProviderAction;
};

// TODO: Either remove, or fully implement if we still want to have custom
// permissions providers. This is from when we decided that users can choose
// their own permissions providers, but we didn't yet have a way to access/save
// user settings.
/**
* Tries to get the saved permissions provider preference, returning the default provider if not found.
*
Expand Down
52 changes: 27 additions & 25 deletions src/skydb_internal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import { log, validateObject, validateString } from "./util";
* Gets Encrypted JSON at the given path through MySky.
*
* @param client - The Skynet client.
* @param seed - The user seed.
* @param seed - The root MySky user seed.
* @param path - The data path.
* @returns - An object containing the decrypted json data.
* @throws - Will throw if the user does not have Hidden Read permission on the path.
Expand Down Expand Up @@ -57,7 +57,7 @@ export async function getJSONEncryptedInternal(
* Sets Encrypted JSON at the given path through MySky.
*
* @param client - The Skynet client.
* @param seed - The user seed.
* @param seed - The root MySky user seed.
* @param path - The data path.
* @param json - The json to encrypt and set.
* @returns - An object containing the original json data.
Expand All @@ -80,36 +80,40 @@ export async function setJSONEncryptedInternal(
const opts = { hashedDataKeyHex: true };

// Immediately fail if the mutex is not available.
return await client.db.revisionNumberCache.withCachedEntryLock(publicKey, dataKey, async (cachedRevisionEntry) => {
// Get the cached revision number before doing anything else.
const newRevision = incrementRevision(cachedRevisionEntry.revision);
return await client.db.revisionNumberCache.withCachedEntryLock(
publicKey,
dataKey,
async (cachedRevisionEntry: { revision: bigint }) => {
// Get the cached revision number before doing anything else.
const newRevision = incrementRevision(cachedRevisionEntry.revision);

// Derive the key.
const encryptionKey = deriveEncryptedFileKeyEntropy(pathSeed);
// Derive the key.
const encryptionKey = deriveEncryptedFileKeyEntropy(pathSeed);

// Pad and encrypt json file.
log("Calling encryptJSONFile");
const data = encryptJSONFile(json, { version: ENCRYPTED_JSON_RESPONSE_VERSION }, encryptionKey);
// Pad and encrypt json file.
log("Calling encryptJSONFile");
const data = encryptJSONFile(json, { version: ENCRYPTED_JSON_RESPONSE_VERSION }, encryptionKey);

log("Calling getOrCreateSkyDBRegistryEntry");
const [entry] = await getOrCreateSkyDBRegistryEntry(client, dataKey, data, newRevision, opts);
log("Calling getOrCreateSkyDBRegistryEntry");
const [entry] = await getOrCreateSkyDBRegistryEntry(client, dataKey, data, newRevision, opts);

// Sign the entry.
log("Calling signEncryptedRegistryEntryInternal");
const signature = await signEncryptedRegistryEntryInternal(seed, entry, path);
// Sign the entry.
log("Calling signEncryptedRegistryEntryInternal");
const signature = await signEncryptedRegistryEntryInternal(seed, entry, path);

log("Calling postSignedEntry");
await client.registry.postSignedEntry(publicKey, entry, signature, opts);
log("Calling postSignedEntry");
await client.registry.postSignedEntry(publicKey, entry, signature, opts);

return { data: json };
});
return { data: json };
}
);
}

/**
* Gets the encrypted path seed for the given path without requiring
* permissions. This should NOT be exported - for internal use only.
*
* @param seed - The user seed.
* @param seed - The root MySky user seed.
* @param path - The given file or directory path.
* @param isDirectory - Whether the path corresponds to a directory.
* @returns - The hex-encoded encrypted path seed.
Expand Down Expand Up @@ -147,7 +151,7 @@ function incrementRevision(revision: bigint): bigint {
* Signs the encrypted registry entry without requiring permissions. For
* internal use only.
*
* @param seed - The user seed.
* @param seed - The root MySky user seed.
* @param entry - The encrypted registry entry.
* @param path - The MySky path.
* @returns - The signature.
Expand Down Expand Up @@ -175,7 +179,7 @@ async function signEncryptedRegistryEntryInternal(
* Internal version of `signRegistryEntryHelper` that does not check for
* permissions.
*
* @param seed - The user seed.
* @param seed - The root MySky user seed.
* @param entry - The registry entry.
* @returns - The signature.
*/
Expand All @@ -186,7 +190,5 @@ async function signRegistryEntryHelperInternal(seed: Uint8Array, entry: Registry
const { privateKey } = genKeyPairFromSeed(seed);

// Sign the entry.
const signature = await signEntry(privateKey, entry, true);

return signature;
return await signEntry(privateKey, entry, true);
}
4 changes: 2 additions & 2 deletions src/user_settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ type UserSettings = { portal: string | null; email: string | null };
* them if found.
*
* @param client - The Skynet client.
* @param seed - The user seed.
* @param seed - The root MySky user seed.
* @param mySkyDomain - The domain of the current MySky instance.
* @returns - The portal and email, if found.
*/
Expand Down Expand Up @@ -47,7 +47,7 @@ export async function getUserSettings(
* Sets the user settings.
*
* @param client - The Skynet client.
* @param seed - The user seed.
* @param seed - The root MySky user seed.
* @param mySkyDomain - The domain of the current MySky instance.
* @param settings - The given user settings.
*/
Expand Down

0 comments on commit 58d6196

Please sign in to comment.