From 33dbdece177f976db503cd6c39e3a8b610d5f0ac Mon Sep 17 00:00:00 2001 From: Hristiyan Date: Wed, 12 Feb 2025 15:28:23 +0200 Subject: [PATCH] fix: complex keys Signed-off-by: Hristiyan --- .../utils/transactionSignatureModels/index.ts | 50 +++++++++++-------- 1 file changed, 29 insertions(+), 21 deletions(-) diff --git a/front-end/src/renderer/utils/transactionSignatureModels/index.ts b/front-end/src/renderer/utils/transactionSignatureModels/index.ts index af30b1163..b67cac7d8 100644 --- a/front-end/src/renderer/utils/transactionSignatureModels/index.ts +++ b/front-end/src/renderer/utils/transactionSignatureModels/index.ts @@ -62,46 +62,54 @@ export const publicRequiredToSign = async ( const usersPublicKeys: Set = new Set(); const nonUserPublicKeys: Set = new Set(); - /* Ensures the user keys are passed */ if (userKeys.length === 0) { return { usersPublicKeys: [], nonUserPublicKeys: [] }; } - /* Get signature entities */ const { newKeys, accounts, receiverAccounts, nodeId } = getSignatureEntities(transaction); - /* Helper function to classify a key */ - const classifyKey = (key: Key) => { - if (userKeys.some(userKey => isPublicKeyInKeyList(userKey.publicKey, key))) { - usersPublicKeys.add(extractRawPublicKey(key)); + const classifyKey = (key: Key, parentThreshold?: number) => { + if (key instanceof KeyList) { + let signedCount = 0; + const requiredThreshold = key.threshold || parentThreshold || key._keys.length; + + key._keys.forEach(subKey => { + if (userKeys.some(userKey => isPublicKeyInKeyList(userKey.publicKey, subKey))) { + usersPublicKeys.add(extractRawPublicKey(subKey)); + signedCount++; + } else { + nonUserPublicKeys.add(extractRawPublicKey(subKey)); + } + }); + + // If threshold is met, remove all remaining required keys + if (signedCount >= requiredThreshold) { + key._keys.forEach(subKey => { + usersPublicKeys.delete(extractRawPublicKey(subKey)); + nonUserPublicKeys.delete(extractRawPublicKey(subKey)); + }); + } } else { - nonUserPublicKeys.add(extractRawPublicKey(key)); + if (userKeys.some(userKey => isPublicKeyInKeyList(userKey.publicKey, key))) { + usersPublicKeys.add(extractRawPublicKey(key)); + } else { + nonUserPublicKeys.add(extractRawPublicKey(key)); + } } }; - /* Add all public keys from the transaction itself */ - newKeys.forEach(key => { - if (key instanceof KeyList) { - key._keys.forEach(subKey => classifyKey(subKey)); - } else { - classifyKey(key); - } - }); + newKeys.forEach(key => classifyKey(key)); /* Add required keys from account signers */ for (const accountId of accounts) { const accountInfo = await getAccountInfo(accountId, mirrorNodeLink); - if (accountInfo.key) { - classifyKey(accountInfo.key); - } + if (accountInfo.key) classifyKey(accountInfo.key); } /* Add required keys from receiver accounts that require signatures */ for (const accountId of receiverAccounts) { const accountInfo = await getAccountInfo(accountId, mirrorNodeLink); - if (accountInfo.receiverSignatureRequired && accountInfo.key) { - classifyKey(accountInfo.key); - } + if (accountInfo.receiverSignatureRequired && accountInfo.key) classifyKey(accountInfo.key); } /* Add required keys from node accounts */