diff --git a/src/helpers/metadataUtils.ts b/src/helpers/metadataUtils.ts index ab6e1e9..51c6e9e 100644 --- a/src/helpers/metadataUtils.ts +++ b/src/helpers/metadataUtils.ts @@ -25,6 +25,23 @@ export async function decryptNodeData(eciesData: EciesHex, ciphertextHex: string return decryptedSigBuffer; } +export async function decryptNodeDataWithPadding(eciesData: EciesHex, ciphertextHex: string, privKey: Buffer): Promise { + const ciphertextHexPadding = ciphertextHex.padStart(64, "0"); + const metadata = encParamsHexToBuf(eciesData); + + try { + const decryptedSigBuffer = await decrypt(privKey, { + ...metadata, + ciphertext: Buffer.from(ciphertextHexPadding, "hex"), + }); + return decryptedSigBuffer; + } catch (error) { + log.warn("Failed to decrypt padded share cipher", error); + // try without cipher text padding + return decrypt(privKey, { ...metadata, ciphertext: Buffer.from(ciphertextHex, "hex") }); + } +} + export function generateMetadataParams(ecCurve: ec, serverTimeOffset: number, message: string, privateKey: BN): MetadataParams { const key = ecCurve.keyFromPrivate(privateKey.toString("hex", 64)); const setData = { diff --git a/src/helpers/nodeUtils.ts b/src/helpers/nodeUtils.ts index 213ba74..45ede9d 100644 --- a/src/helpers/nodeUtils.ts +++ b/src/helpers/nodeUtils.ts @@ -39,7 +39,7 @@ import { } from "./common"; import { derivePubKey, generateAddressFromPrivKey, generateAddressFromPubKey, generatePrivateKey, generateShares, keccak256 } from "./keyUtils"; import { lagrangeInterpolation } from "./langrangeInterpolatePoly"; -import { decryptNodeData, decryptSeedData, getMetadata, getOrSetNonce } from "./metadataUtils"; +import { decryptNodeData, decryptNodeDataWithPadding, decryptSeedData, getMetadata, getOrSetNonce } from "./metadataUtils"; export const GetPubKeyOrKeyAssign = async (params: { endpoints: string[]; @@ -548,12 +548,11 @@ export async function retrieveOrImportShare(params: { if (keys?.length > 0) { const latestKey = currentShareResponse.result.keys[0]; nodeIndexes.push(new BN(latestKey.node_index)); - if (latestKey.share_metadata) { sharePromises.push( - decryptNodeData( + decryptNodeDataWithPadding( latestKey.share_metadata, - Buffer.from(latestKey.share, "base64").toString("binary").padStart(64, "0"), + Buffer.from(latestKey.share, "base64").toString("binary"), sessionAuthKey ).catch((err) => log.error("share decryption", err)) );