|
| 1 | +import { encParamsHexToBuf, getKeyCurve, keccak256, NonceMetadataParams, SetNonceData } from "@toruslabs/common-lib"; |
| 2 | +import { KEY_TYPE, LEGACY_NETWORKS_ROUTE_MAP, TORUS_LEGACY_NETWORK_TYPE, TORUS_NETWORK_TYPE, TORUS_SAPPHIRE_NETWORK } from "@toruslabs/constants"; |
| 3 | +import { decrypt } from "@toruslabs/eccrypto"; |
| 4 | +import { Data, post } from "@toruslabs/http-helpers"; |
| 5 | +import BN from "bn.js"; |
| 6 | +import { curve, ec as EC } from "elliptic"; |
| 7 | +import { keccak256 as keccakHash } from "ethereum-cryptography/keccak"; |
| 8 | +import stringify from "json-stable-stringify"; |
| 9 | +import log from "loglevel"; |
| 10 | + |
| 11 | +import { SAPPHIRE_DEVNET_METADATA_URL, SAPPHIRE_METADATA_URL } from "../constants"; |
| 12 | +import { EciesHex, EncryptedSeed, GetOrSetNonceResult, KeyType, MetadataParams, SapphireMetadataParams } from "../interfaces"; |
| 13 | + |
| 14 | +export class GetOrSetNonceError extends Error {} |
| 15 | + |
| 16 | +export const getSecpKeyFromEd25519 = ( |
| 17 | + ed25519Scalar: BN |
| 18 | +): { |
| 19 | + scalar: BN; |
| 20 | + point: curve.base.BasePoint; |
| 21 | +} => { |
| 22 | + const secp256k1Curve = getKeyCurve(KEY_TYPE.SECP256K1); |
| 23 | + |
| 24 | + const ed25519Key = ed25519Scalar.toString("hex", 64); |
| 25 | + const keyHash: Uint8Array = keccakHash(Buffer.from(ed25519Key, "hex")); |
| 26 | + const secpKey = new BN(keyHash).umod(secp256k1Curve.n).toString("hex", 64); |
| 27 | + const bufferKey = Buffer.from(secpKey, "hex"); |
| 28 | + |
| 29 | + const secpKeyPair = secp256k1Curve.keyFromPrivate(bufferKey); |
| 30 | + |
| 31 | + if (bufferKey.length !== 32) { |
| 32 | + throw new Error(`Key length must be equal to 32. got ${bufferKey.length}`); |
| 33 | + } |
| 34 | + return { |
| 35 | + scalar: secpKeyPair.getPrivate(), |
| 36 | + point: secpKeyPair.getPublic(), |
| 37 | + }; |
| 38 | +}; |
| 39 | + |
| 40 | +export function convertMetadataToNonce(params: { message?: string }) { |
| 41 | + if (!params || !params.message) { |
| 42 | + return new BN(0); |
| 43 | + } |
| 44 | + return new BN(params.message, 16); |
| 45 | +} |
| 46 | + |
| 47 | +export async function decryptNodeData(eciesData: EciesHex, ciphertextHex: string, privKey: Buffer): Promise<Buffer> { |
| 48 | + const metadata = encParamsHexToBuf(eciesData); |
| 49 | + const decryptedSigBuffer = await decrypt(privKey, { |
| 50 | + ...metadata, |
| 51 | + ciphertext: Buffer.from(ciphertextHex, "hex"), |
| 52 | + }); |
| 53 | + return decryptedSigBuffer; |
| 54 | +} |
| 55 | + |
| 56 | +export async function decryptNodeDataWithPadding(eciesData: EciesHex, ciphertextHex: string, privKey: Buffer): Promise<Buffer> { |
| 57 | + const metadata = encParamsHexToBuf(eciesData); |
| 58 | + try { |
| 59 | + const decryptedSigBuffer = await decrypt(privKey, { |
| 60 | + ...metadata, |
| 61 | + ciphertext: Buffer.from(ciphertextHex, "hex"), |
| 62 | + }); |
| 63 | + return decryptedSigBuffer; |
| 64 | + } catch (error) { |
| 65 | + // ciphertext can be any length. not just 64. depends on input. we have this for legacy reason |
| 66 | + const ciphertextHexPadding = ciphertextHex.padStart(64, "0"); |
| 67 | + |
| 68 | + log.warn("Failed to decrypt padded share cipher", error); |
| 69 | + // try without cipher text padding |
| 70 | + return decrypt(privKey, { ...metadata, ciphertext: Buffer.from(ciphertextHexPadding, "hex") }); |
| 71 | + } |
| 72 | +} |
| 73 | + |
| 74 | +export function generateMetadataParams(ecCurve: EC, serverTimeOffset: number, message: string, privateKey: BN): MetadataParams { |
| 75 | + const key = ecCurve.keyFromPrivate(privateKey.toString("hex", 64), "hex"); |
| 76 | + const setData = { |
| 77 | + data: message, |
| 78 | + timestamp: new BN(~~(serverTimeOffset + Date.now() / 1000)).toString(16), |
| 79 | + }; |
| 80 | + const sig = key.sign(keccak256(Buffer.from(stringify(setData), "utf8")).slice(2)); |
| 81 | + return { |
| 82 | + pub_key_X: key.getPublic().getX().toString("hex"), // DO NOT PAD THIS. BACKEND DOESN'T |
| 83 | + pub_key_Y: key.getPublic().getY().toString("hex"), // DO NOT PAD THIS. BACKEND DOESN'T |
| 84 | + set_data: setData, |
| 85 | + signature: Buffer.from(sig.r.toString(16, 64) + sig.s.toString(16, 64) + new BN("").toString(16, 2), "hex").toString("base64"), |
| 86 | + }; |
| 87 | +} |
| 88 | + |
| 89 | +export async function getMetadata( |
| 90 | + legacyMetadataHost: string, |
| 91 | + data: Omit<MetadataParams, "set_data" | "signature">, |
| 92 | + options: RequestInit = {} |
| 93 | +): Promise<BN> { |
| 94 | + try { |
| 95 | + const metadataResponse = await post<{ message?: string }>(`${legacyMetadataHost}/get`, data, options, { useAPIKey: true }); |
| 96 | + if (!metadataResponse || !metadataResponse.message) { |
| 97 | + return new BN(0); |
| 98 | + } |
| 99 | + return new BN(metadataResponse.message, 16); // nonce |
| 100 | + } catch (error) { |
| 101 | + log.error("get metadata error", error); |
| 102 | + return new BN(0); |
| 103 | + } |
| 104 | +} |
| 105 | + |
| 106 | +export function generateNonceMetadataParams( |
| 107 | + serverTimeOffset: number, |
| 108 | + operation: string, |
| 109 | + privateKey: BN, |
| 110 | + keyType: KeyType, |
| 111 | + nonce?: BN, |
| 112 | + seed?: string |
| 113 | +): NonceMetadataParams { |
| 114 | + // // metadata only uses secp for sig validation |
| 115 | + // const key = getKeyCurve(KEY_TYPE.SECP256K1).keyFromPrivate(privateKey.toString("hex", 64), "hex"); |
| 116 | + // const setData: Partial<SetNonceData> = { |
| 117 | + // operation, |
| 118 | + // timestamp: new BN(~~(serverTimeOffset + Date.now() / 1000)).toString(16), |
| 119 | + // }; |
| 120 | + |
| 121 | + // if (nonce) { |
| 122 | + // setData.data = nonce.toString("hex", 64); |
| 123 | + // } |
| 124 | + |
| 125 | + // if (seed) { |
| 126 | + // setData.seed = seed; |
| 127 | + // } else { |
| 128 | + // setData.seed = ""; // setting it as empty to keep ordering same while serializing the data on backend. |
| 129 | + // } |
| 130 | + |
| 131 | + // const sig = key.sign(keccak256(Buffer.from(stringify(setData), "utf8")).slice(2)); |
| 132 | + // return { |
| 133 | + // pub_key_X: key.getPublic().getX().toString("hex", 64), |
| 134 | + // pub_key_Y: key.getPublic().getY().toString("hex", 64), |
| 135 | + // set_data: setData, |
| 136 | + // key_type: keyType, |
| 137 | + // signature: Buffer.from(sig.r.toString(16, 64) + sig.s.toString(16, 64) + new BN("").toString(16, 2), "hex").toString("base64"), |
| 138 | + // }; |
| 139 | + // metadata only uses secp for sig validation |
| 140 | + const key = getKeyCurve(KEY_TYPE.SECP256K1).keyFromPrivate(privateKey.toString("hex", 64), "hex"); |
| 141 | + const setData: Partial<SetNonceData> = { |
| 142 | + operation, |
| 143 | + timestamp: new BN(~~(serverTimeOffset + Date.now() / 1000)).toString(16), |
| 144 | + }; |
| 145 | + |
| 146 | + if (nonce) { |
| 147 | + setData.data = nonce.toString("hex", 64); |
| 148 | + } |
| 149 | + |
| 150 | + if (seed) { |
| 151 | + setData.seed = seed; |
| 152 | + } else { |
| 153 | + setData.seed = ""; // setting it as empty to keep ordering same while serializing the data on backend. |
| 154 | + } |
| 155 | + |
| 156 | + const sig = key.sign(keccak256(Buffer.from(stringify(setData), "utf8")).slice(2)); |
| 157 | + return { |
| 158 | + pub_key_X: key.getPublic().getX().toString("hex", 64), |
| 159 | + pub_key_Y: key.getPublic().getY().toString("hex", 64), |
| 160 | + set_data: setData, |
| 161 | + key_type: keyType, |
| 162 | + signature: Buffer.from(sig.r.toString(16, 64) + sig.s.toString(16, 64) + new BN("").toString(16, 2), "hex").toString("base64"), |
| 163 | + }; |
| 164 | +} |
| 165 | + |
| 166 | +export async function getOrSetNonce( |
| 167 | + metadataHost: string, |
| 168 | + ecCurve: EC, |
| 169 | + serverTimeOffset: number, |
| 170 | + X: string, |
| 171 | + Y: string, |
| 172 | + privKey?: BN, |
| 173 | + getOnly = false, |
| 174 | + isLegacyMetadata = true, |
| 175 | + nonce = new BN(0), |
| 176 | + keyType: KeyType = "secp256k1", |
| 177 | + seed = "" |
| 178 | +): Promise<GetOrSetNonceResult> { |
| 179 | + // for legacy metadata |
| 180 | + if (isLegacyMetadata) { |
| 181 | + let data: Data; |
| 182 | + const msg = getOnly ? "getNonce" : "getOrSetNonce"; |
| 183 | + if (privKey) { |
| 184 | + data = generateMetadataParams(ecCurve, serverTimeOffset, msg, privKey); |
| 185 | + } else { |
| 186 | + data = { |
| 187 | + pub_key_X: X, |
| 188 | + pub_key_Y: Y, |
| 189 | + set_data: { data: msg }, |
| 190 | + }; |
| 191 | + } |
| 192 | + return post<GetOrSetNonceResult>(`${metadataHost}/get_or_set_nonce`, data, undefined, { useAPIKey: true }); |
| 193 | + } |
| 194 | + |
| 195 | + // for sapphire metadata |
| 196 | + const operation = getOnly ? "getNonce" : "getOrSetNonce"; |
| 197 | + if (operation === "getOrSetNonce") { |
| 198 | + if (!privKey) { |
| 199 | + throw new Error("privKey is required while `getOrSetNonce` for non legacy metadata"); |
| 200 | + } |
| 201 | + if (nonce.cmp(new BN(0)) === 0) { |
| 202 | + throw new Error("nonce is required while `getOrSetNonce` for non legacy metadata"); |
| 203 | + } |
| 204 | + if (keyType === KEY_TYPE.ED25519 && !seed) { |
| 205 | + throw new Error("seed is required while `getOrSetNonce` for non legacy metadata for ed25519 key type"); |
| 206 | + } |
| 207 | + const data = generateNonceMetadataParams(serverTimeOffset, operation, privKey, keyType, nonce, seed); |
| 208 | + |
| 209 | + return post<GetOrSetNonceResult>(`${metadataHost}/get_or_set_nonce`, data, undefined, { useAPIKey: true }); |
| 210 | + } |
| 211 | + const data = { |
| 212 | + pub_key_X: X, |
| 213 | + pub_key_Y: Y, |
| 214 | + set_data: { operation }, |
| 215 | + key_type: keyType, |
| 216 | + }; |
| 217 | + return post<GetOrSetNonceResult>(`${metadataHost}/get_or_set_nonce`, data, undefined, { useAPIKey: true }); |
| 218 | +} |
| 219 | +export async function getNonce( |
| 220 | + legacyMetadataHost: string, |
| 221 | + ecCurve: EC, |
| 222 | + serverTimeOffset: number, |
| 223 | + X: string, |
| 224 | + Y: string, |
| 225 | + privKey?: BN |
| 226 | +): Promise<GetOrSetNonceResult> { |
| 227 | + return getOrSetNonce(legacyMetadataHost, ecCurve, serverTimeOffset, X, Y, privKey, true); |
| 228 | +} |
| 229 | + |
| 230 | +export const decryptSeedData = async (seedBase64: string, finalUserKey: BN) => { |
| 231 | + const decryptionKey = getSecpKeyFromEd25519(finalUserKey); |
| 232 | + const seedUtf8 = Buffer.from(seedBase64, "base64").toString("utf-8"); |
| 233 | + const seedJson = JSON.parse(seedUtf8) as EncryptedSeed; |
| 234 | + const bufferMetadata = { ...encParamsHexToBuf(seedJson.metadata), mode: "AES256" }; |
| 235 | + const bufferKey = decryptionKey.scalar.toArrayLike(Buffer, "be", 32); |
| 236 | + const decText = await decrypt(bufferKey, { |
| 237 | + ...bufferMetadata, |
| 238 | + ciphertext: Buffer.from(seedJson.enc_text, "hex"), |
| 239 | + }); |
| 240 | + |
| 241 | + return decText; |
| 242 | +}; |
| 243 | + |
| 244 | +export async function getOrSetSapphireMetadataNonce( |
| 245 | + network: TORUS_NETWORK_TYPE, |
| 246 | + X: string, |
| 247 | + Y: string, |
| 248 | + serverTimeOffset?: number, |
| 249 | + privKey?: BN |
| 250 | +): Promise<GetOrSetNonceResult> { |
| 251 | + if (LEGACY_NETWORKS_ROUTE_MAP[network as TORUS_LEGACY_NETWORK_TYPE]) { |
| 252 | + throw new Error("getOrSetSapphireMetadataNonce should only be used for sapphire networks"); |
| 253 | + } |
| 254 | + let data: SapphireMetadataParams = { |
| 255 | + pub_key_X: X, |
| 256 | + pub_key_Y: Y, |
| 257 | + key_type: "secp256k1", |
| 258 | + set_data: { operation: "getOrSetNonce" }, |
| 259 | + }; |
| 260 | + if (privKey) { |
| 261 | + const key = getKeyCurve(KEY_TYPE.SECP256K1).keyFromPrivate(privKey.toString("hex", 64), "hex"); |
| 262 | + |
| 263 | + const setData = { |
| 264 | + operation: "getOrSetNonce", |
| 265 | + timestamp: new BN(~~(serverTimeOffset + Date.now() / 1000)).toString(16), |
| 266 | + }; |
| 267 | + const sig = key.sign(keccak256(Buffer.from(stringify(setData), "utf8")).slice(2)); |
| 268 | + data = { |
| 269 | + ...data, |
| 270 | + set_data: setData, |
| 271 | + signature: Buffer.from(sig.r.toString(16, 64) + sig.s.toString(16, 64) + new BN("").toString(16, 2), "hex").toString("base64"), |
| 272 | + }; |
| 273 | + } |
| 274 | + |
| 275 | + const metadataUrl = network === TORUS_SAPPHIRE_NETWORK.SAPPHIRE_DEVNET ? SAPPHIRE_DEVNET_METADATA_URL : SAPPHIRE_METADATA_URL; |
| 276 | + |
| 277 | + return post<GetOrSetNonceResult>(`${metadataUrl}/get_or_set_nonce`, data, undefined, { useAPIKey: true }); |
| 278 | +} |
0 commit comments