-
Notifications
You must be signed in to change notification settings - Fork 105
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add service worker and logic to download & validate MASP params
- Loading branch information
Showing
7 changed files
with
274 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
/** | ||
* Define constants for MASP Params Storage | ||
*/ | ||
const STORAGE_PREFIX = "/namadillo"; | ||
|
||
enum MaspParam { | ||
Output = "masp-output.params", | ||
Convert = "masp-convert.params", | ||
Spend = "masp-spend.params", | ||
} | ||
|
||
const MASP_PARAM_LEN: Record<MaspParam, number> = { | ||
[MaspParam.Output]: 16398620, | ||
[MaspParam.Spend]: 49848572, | ||
[MaspParam.Convert]: 22570940, | ||
}; | ||
|
||
enum MsgType { | ||
FetchParam = "fetch-params", | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,153 @@ | ||
/** | ||
* Establish types for IndexedDB | ||
*/ | ||
interface KVStore<T> { | ||
get<U extends T>(key: string): Promise<U | undefined>; | ||
set<U extends T>(key: string, data: U | null): Promise<void>; | ||
prefix(): string; | ||
} | ||
|
||
let durability: IDBTransactionMode = "readwrite"; | ||
|
||
class IndexedDBKVStore<T> implements KVStore<T> { | ||
protected cachedDB?: IDBDatabase; | ||
|
||
constructor(protected readonly _prefix: string) { } | ||
|
||
public async get<U extends T>(key: string): Promise<U | undefined> { | ||
const tx = (await this.getDB()).transaction([this.prefix()], "readonly"); | ||
const store = tx.objectStore(this.prefix()); | ||
|
||
return new Promise((resolve, reject) => { | ||
const request = store.get(key); | ||
request.onerror = (event) => { | ||
event.stopPropagation(); | ||
|
||
reject(event.target); | ||
}; | ||
request.onsuccess = () => { | ||
if (!request.result) { | ||
resolve(undefined); | ||
} else { | ||
resolve(request.result.data); | ||
} | ||
}; | ||
}); | ||
} | ||
|
||
public async set<U extends T>(key: string, data: U | null): Promise<void> { | ||
if (data === null) { | ||
const tx = (await this.getDB()).transaction([this.prefix()], durability, { | ||
durability: "strict", | ||
}); | ||
const store = tx.objectStore(this.prefix()); | ||
|
||
return new Promise((resolve, reject) => { | ||
const request = store.delete(key); | ||
request.onerror = (event) => { | ||
event.stopPropagation(); | ||
|
||
reject(event.target); | ||
}; | ||
request.onsuccess = () => { | ||
resolve(); | ||
}; | ||
}); | ||
} else { | ||
const tx = (await this.getDB()).transaction([this.prefix()], durability, { | ||
durability: "strict", | ||
}); | ||
const store = tx.objectStore(this.prefix()); | ||
|
||
return new Promise((resolve, reject) => { | ||
const request = store.put({ | ||
key, | ||
data, | ||
}); | ||
request.onerror = (event) => { | ||
event.stopPropagation(); | ||
|
||
reject(event.target); | ||
}; | ||
request.onsuccess = () => { | ||
resolve(); | ||
}; | ||
}); | ||
} | ||
} | ||
|
||
public prefix(): string { | ||
return this._prefix; | ||
} | ||
|
||
protected async getDB(): Promise<IDBDatabase> { | ||
if (this.cachedDB) { | ||
return this.cachedDB; | ||
} | ||
|
||
return new Promise((resolve, reject) => { | ||
const request = indexedDB.open(this.prefix()); | ||
request.onerror = (event) => { | ||
event.stopPropagation(); | ||
reject(event.target); | ||
}; | ||
|
||
request.onupgradeneeded = (event) => { | ||
// eslint-disable-next-line @typescript-eslint/ban-ts-comment | ||
// @ts-ignore | ||
const db = event.target.result; | ||
|
||
db.createObjectStore(this.prefix(), { keyPath: "key" }); | ||
}; | ||
|
||
request.onsuccess = () => { | ||
this.cachedDB = request.result; | ||
resolve(request.result); | ||
}; | ||
}); | ||
} | ||
|
||
public static async durabilityCheck(): Promise<boolean> { | ||
const { TARGET } = process.env; | ||
let isDurable: boolean; | ||
|
||
if (TARGET === "chrome") { | ||
durability = "readwrite"; | ||
isDurable = true; | ||
} else { | ||
const prefix = "durability-check"; | ||
const db: IDBDatabase = await new Promise((resolve, reject) => { | ||
const request = indexedDB.open(prefix); | ||
request.onerror = (event) => { | ||
event.stopPropagation(); | ||
reject(event.target); | ||
}; | ||
|
||
request.onupgradeneeded = (event) => { | ||
// eslint-disable-next-line @typescript-eslint/ban-ts-comment | ||
// @ts-ignore | ||
const db = event.target.result; | ||
|
||
db.createObjectStore(prefix, { keyPath: "key" }); | ||
}; | ||
|
||
request.onsuccess = () => { | ||
resolve(request.result); | ||
}; | ||
}); | ||
|
||
try { | ||
db.transaction([prefix], "readwriteflush" as IDBTransactionMode, { | ||
durability: "strict", | ||
}); | ||
durability = "readwriteflush" as IDBTransactionMode; | ||
isDurable = true; | ||
} catch { | ||
durability = "readwrite"; | ||
isDurable = false; | ||
} | ||
} | ||
|
||
return isDurable; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
importScripts("indexedDb.js"); | ||
importScripts("constants.js"); | ||
// const { NAMADA_INTERFACE_PROXY: isProxy = false } = process.env; | ||
|
||
const isProxy = true; | ||
const TRUSTED_SETUP_URL = | ||
isProxy ? | ||
"http://localhost:8010/proxy" | ||
: "https://github.com/anoma/masp-mpc/releases/download/namada-trusted-setup"; | ||
|
||
const store = new IndexedDBKVStore(STORAGE_PREFIX); | ||
|
||
const fetchAndStoreMaspParam = async (maspParam: MaspParam): Promise<void> => { | ||
console.info(`Fetching ${maspParam}...`); | ||
return fetch([TRUSTED_SETUP_URL, maspParam].join("/")) | ||
.then(async (response) => { | ||
if (response.ok) { | ||
const reader = response.body?.getReader(); | ||
if (!reader) { | ||
throw new Error("No readable stream returned!"); | ||
} | ||
return new ReadableStream({ | ||
start(controller) { | ||
return pump(); | ||
function pump() { | ||
return reader?.read().then(({ done, value }) => { | ||
// When no more data needs to be consumed, close the stream | ||
if (done) { | ||
controller.close(); | ||
return; | ||
} | ||
// Enqueue the next data chunk into our target stream | ||
controller.enqueue(value); | ||
return pump(); | ||
}); | ||
} | ||
}, | ||
}); | ||
} | ||
}) | ||
.then((stream) => new Response(stream)) | ||
.then((response) => response.blob()) | ||
.then(async (blob) => { | ||
const arrayBuffer = await blob.arrayBuffer(); | ||
const data = new Uint8Array(arrayBuffer); | ||
|
||
// Validate data length: | ||
if (data.length === MASP_PARAM_LEN[maspParam]) { | ||
console.info(`Storing ${maspParam} => `, data); | ||
return store.set(maspParam, data); | ||
} | ||
return Promise.reject( | ||
`Failed to download ${maspParam}: ${data.length} does not match ${MASP_PARAM_LEN[maspParam]}` | ||
); | ||
}); | ||
}; | ||
|
||
(async () => { | ||
const maspOutputParam = await store.get(MaspParam.Output); | ||
console.log("Found output?: ", maspOutputParam); | ||
|
||
const maspSpendParam = await store.get(MaspParam.Spend); | ||
console.log("Found spend?: ", maspSpendParam); | ||
|
||
const maspConvertParam = await store.get(MaspParam.Convert); | ||
console.log("Found convert?", maspConvertParam); | ||
|
||
if (!maspOutputParam) await fetchAndStoreMaspParam(MaspParam.Output); | ||
if (!maspSpendParam) await fetchAndStoreMaspParam(MaspParam.Spend); | ||
if (!maspConvertParam) await fetchAndStoreMaspParam(MaspParam.Convert); | ||
})(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
{ | ||
"extends": "./tsconfig.json", | ||
"compilerOptions": { | ||
"baseUrl": "./sw", | ||
"lib": ["ESNext", "WebWorker"], | ||
"noEmit": false, | ||
"strict": false, | ||
"outDir": "public", | ||
"target": "ESNext", | ||
"isolatedModules": false | ||
}, | ||
"include": ["sw"] | ||
} |