|
| 1 | +const PREFIX = "Namada::SDK"; |
| 2 | +const MASP_MPC_RELEASE_URL = |
| 3 | + "https://github.com/anoma/masp-mpc/releases/download/namada-trusted-setup/"; |
| 4 | + |
| 5 | +const sha256Hash = async (msg: Uint8Array): Promise<string> => { |
| 6 | + const hashBuffer = await crypto.subtle.digest("SHA-256", msg); |
| 7 | + const hashArray = Array.from(new Uint8Array(hashBuffer)); |
| 8 | + // Return hash as hex |
| 9 | + return hashArray.map((byte) => byte.toString(16).padStart(2, "0")).join(""); |
| 10 | +}; |
| 11 | + |
| 12 | +enum MaspParam { |
| 13 | + Output = "masp-output.params", |
| 14 | + Convert = "masp-convert.params", |
| 15 | + Spend = "masp-spend.params", |
| 16 | +} |
| 17 | + |
| 18 | +type MaspParamBytes = { |
| 19 | + param: MaspParam; |
| 20 | + bytes: Uint8Array; |
| 21 | +}; |
| 22 | + |
| 23 | +/** |
| 24 | + * The following sha256 digests where produced by downloading the following: |
| 25 | + * https://github.com/anoma/masp-mpc/releases/download/namada-trusted-setup/masp-convert.params |
| 26 | + * https://github.com/anoma/masp-mpc/releases/download/namada-trusted-setup/masp-spend.params |
| 27 | + * https://github.com/anoma/masp-mpc/releases/download/namada-trusted-setup/masp-output.params |
| 28 | + * |
| 29 | + * And running "sha256sum" against each file: |
| 30 | + * |
| 31 | + * > sha256sum masp-convert.params |
| 32 | + * 8e049c905e0e46f27662c7577a4e3480c0047ee1171f7f6d9c5b0de757bf71f1 masp-convert.params |
| 33 | + * |
| 34 | + * > sha256sum masp-spend.params |
| 35 | + * 62b3c60ca54bd99eb390198e949660624612f7db7942db84595fa9f1b4a29fd8 masp-spend.params |
| 36 | + * |
| 37 | + * > sha256sum masp-output.params |
| 38 | + * ed8b5d354017d808cfaf7b31eca5c511936e65ef6d276770251f5234ec5328b8 masp-output.params |
| 39 | + * |
| 40 | + * Length is specified in bytes, and can be retrieved with: |
| 41 | + * |
| 42 | + * > wc -c < masp-convert.params |
| 43 | + * 22570940 |
| 44 | + * > wc -c < masp-spend.params |
| 45 | + * 49848572 |
| 46 | + * > wc -c < masp-output.params |
| 47 | + * 16398620 |
| 48 | + */ |
| 49 | +const MASP_PARAM_ATTR: Record< |
| 50 | + MaspParam, |
| 51 | + { length: number; sha256sum: string } |
| 52 | +> = { |
| 53 | + [MaspParam.Output]: { |
| 54 | + length: 16398620, |
| 55 | + sha256sum: |
| 56 | + "ed8b5d354017d808cfaf7b31eca5c511936e65ef6d276770251f5234ec5328b8", |
| 57 | + }, |
| 58 | + [MaspParam.Spend]: { |
| 59 | + length: 49848572, |
| 60 | + sha256sum: |
| 61 | + "62b3c60ca54bd99eb390198e949660624612f7db7942db84595fa9f1b4a29fd8", |
| 62 | + }, |
| 63 | + [MaspParam.Convert]: { |
| 64 | + length: 22570940, |
| 65 | + sha256sum: |
| 66 | + "8e049c905e0e46f27662c7577a4e3480c0047ee1171f7f6d9c5b0de757bf71f1", |
| 67 | + }, |
| 68 | +}; |
| 69 | + |
| 70 | +const validateMaspParamBytes = async ({ |
| 71 | + param, |
| 72 | + bytes, |
| 73 | +}: MaspParamBytes): Promise<Uint8Array> => { |
| 74 | + const { length, sha256sum } = MASP_PARAM_ATTR[param]; |
| 75 | + |
| 76 | + // Reject if invalid length (incomplete download or invalid) |
| 77 | + console.info(`Validating data length for ${param}, expecting ${length}...`); |
| 78 | + |
| 79 | + if (length !== bytes.length) { |
| 80 | + return Promise.reject( |
| 81 | + `[${param}]: Invalid data length! Expected ${length}, received ${bytes.length}!` |
| 82 | + ); |
| 83 | + } |
| 84 | + |
| 85 | + // Reject if invalid hash (otherwise invalid data) |
| 86 | + console.info(`Validating sha256sum for ${param}, expecting ${sha256sum}...`); |
| 87 | + const hash = await sha256Hash(bytes); |
| 88 | + |
| 89 | + if (hash !== sha256sum) { |
| 90 | + return Promise.reject( |
| 91 | + `[${param}]: Invalid sha256sum! Expected ${sha256sum}, received ${hash}!` |
| 92 | + ); |
| 93 | + } |
| 94 | + |
| 95 | + return bytes; |
| 96 | +}; |
| 97 | + |
| 98 | +export async function hasMaspParams(): Promise<boolean> { |
| 99 | + return ( |
| 100 | + (await has(MaspParam.Spend)) && |
| 101 | + (await has(MaspParam.Output)) && |
| 102 | + (await has(MaspParam.Convert)) |
| 103 | + ); |
| 104 | +} |
| 105 | + |
| 106 | +export async function fetchAndStoreMaspParams( |
| 107 | + url?: string |
| 108 | +): Promise<[void, void, void]> { |
| 109 | + return Promise.all([ |
| 110 | + fetchAndStore(MaspParam.Spend, url), |
| 111 | + fetchAndStore(MaspParam.Output, url), |
| 112 | + fetchAndStore(MaspParam.Convert, url), |
| 113 | + ]); |
| 114 | +} |
| 115 | + |
| 116 | +export async function getMaspParams(): Promise<[unknown, unknown, unknown]> { |
| 117 | + return Promise.all([ |
| 118 | + get(MaspParam.Spend), |
| 119 | + get(MaspParam.Output), |
| 120 | + get(MaspParam.Convert), |
| 121 | + ]); |
| 122 | +} |
| 123 | + |
| 124 | +export async function fetchAndStore( |
| 125 | + param: MaspParam, |
| 126 | + url?: string |
| 127 | +): Promise<void> { |
| 128 | + return await fetchParams(param, url) |
| 129 | + .then((data) => set(param, data)) |
| 130 | + .catch((e) => { |
| 131 | + return Promise.reject(`Encountered errors fetching ${param}: ${e}`); |
| 132 | + }); |
| 133 | +} |
| 134 | + |
| 135 | +export async function fetchParams( |
| 136 | + param: MaspParam, |
| 137 | + url: string = MASP_MPC_RELEASE_URL |
| 138 | +): Promise<Uint8Array> { |
| 139 | + return fetch(`${url}${param}`) |
| 140 | + .then((response) => response.arrayBuffer()) |
| 141 | + .then((ab) => { |
| 142 | + const bytes = new Uint8Array(ab); |
| 143 | + return validateMaspParamBytes({ param, bytes }); |
| 144 | + }); |
| 145 | +} |
| 146 | + |
| 147 | +function getDB(): Promise<IDBDatabase> { |
| 148 | + return new Promise((resolve, reject) => { |
| 149 | + const request = indexedDB.open(PREFIX); |
| 150 | + request.onerror = (event) => { |
| 151 | + event.stopPropagation(); |
| 152 | + reject(event.target); |
| 153 | + }; |
| 154 | + |
| 155 | + request.onupgradeneeded = (event) => { |
| 156 | + // eslint-disable-next-line @typescript-eslint/no-explicit-any |
| 157 | + const db = (event.target as any).result; |
| 158 | + |
| 159 | + db.createObjectStore(PREFIX, { keyPath: "key" }); |
| 160 | + }; |
| 161 | + |
| 162 | + request.onsuccess = () => { |
| 163 | + resolve(request.result); |
| 164 | + }; |
| 165 | + }); |
| 166 | +} |
| 167 | + |
| 168 | +export async function get(key: string): Promise<unknown> { |
| 169 | + const tx = (await getDB()).transaction(PREFIX, "readonly"); |
| 170 | + const store = tx.objectStore(PREFIX); |
| 171 | + |
| 172 | + return new Promise((resolve, reject) => { |
| 173 | + const request = store.get(key); |
| 174 | + request.onerror = (event) => { |
| 175 | + event.stopPropagation(); |
| 176 | + |
| 177 | + reject(event.target); |
| 178 | + }; |
| 179 | + request.onsuccess = () => { |
| 180 | + if (!request.result) { |
| 181 | + resolve(undefined); |
| 182 | + } else { |
| 183 | + resolve(request.result.data); |
| 184 | + } |
| 185 | + }; |
| 186 | + }); |
| 187 | +} |
| 188 | + |
| 189 | +export async function has(key: string): Promise<boolean> { |
| 190 | + const tx = (await getDB()).transaction(PREFIX, "readonly"); |
| 191 | + const store = tx.objectStore(PREFIX); |
| 192 | + |
| 193 | + return new Promise((resolve, reject) => { |
| 194 | + const request = store.openCursor(key); |
| 195 | + request.onerror = (event) => { |
| 196 | + event.stopPropagation(); |
| 197 | + |
| 198 | + reject(event.target); |
| 199 | + }; |
| 200 | + request.onsuccess = (e) => { |
| 201 | + // eslint-disable-next-line @typescript-eslint/no-explicit-any |
| 202 | + const cursor = (e.target as any).result; |
| 203 | + resolve(!!cursor); |
| 204 | + }; |
| 205 | + }); |
| 206 | +} |
| 207 | + |
| 208 | +export async function set(key: string, data: unknown): Promise<void> { |
| 209 | + const tx = (await getDB()).transaction(PREFIX, "readwrite"); |
| 210 | + const store = tx.objectStore(PREFIX); |
| 211 | + |
| 212 | + return new Promise((resolve, reject) => { |
| 213 | + const request = store.put({ |
| 214 | + key, |
| 215 | + data, |
| 216 | + }); |
| 217 | + request.onerror = (event) => { |
| 218 | + event.stopPropagation(); |
| 219 | + |
| 220 | + reject(event.target); |
| 221 | + }; |
| 222 | + request.onsuccess = () => { |
| 223 | + resolve(); |
| 224 | + }; |
| 225 | + }); |
| 226 | +} |
0 commit comments