From 2582ad804ca3091a799c9d75ca198efbe7b942d4 Mon Sep 17 00:00:00 2001 From: Benjamin Smith Date: Sat, 10 Aug 2024 00:08:22 +0200 Subject: [PATCH] more comments and more concise code --- src/utils/signature.ts | 41 +++++++++++++++++++++-------------------- 1 file changed, 21 insertions(+), 20 deletions(-) diff --git a/src/utils/signature.ts b/src/utils/signature.ts index ed433b0..f5ab261 100644 --- a/src/utils/signature.ts +++ b/src/utils/signature.ts @@ -75,29 +75,30 @@ export function signatureFromOutcome( | FinalExecutionOutcome | Omit ): Signature { + // TODO - find a scenario when outcome.status is `FinalExecutionStatusBasic`! let b64Sig = (outcome.status as FinalExecutionStatus).SuccessValue; - if (b64Sig === "") { + if (!b64Sig) { // This scenario occurs when sign call is relayed (i.e. executed by someone else). - // We have to dig more into the receipt and extract the signature from within. // E.g. https://testnet.nearblocks.io/txns/G1f1HVUxDBWXAEimgNWobQ9yCx1EgA2tzYHJBFUfo3dj - // Extract receipts_outcome - const receiptsOutcome = outcome.receipts_outcome; - // Map to get SuccessValues: The Signature will appear twice. - const successValues = receiptsOutcome.map( - (outcome) => (outcome.outcome.status as FinalExecutionStatus).SuccessValue - ); - // We want the second occurence of the signature - // (the other is nested inside `{ Ok: signature }`) - b64Sig = successValues - .reverse() // Find the LAST non-empty value! + // We have to dig into `receipts_outcome` and extract the signature from within. + // We want the second occurence of the signature because + // the first is nested inside `{ Ok: MPCSignature }`) + b64Sig = outcome.receipts_outcome + // Map to get SuccessValues: The Signature will appear twice. + .map( + (outcome) => + (outcome.outcome.status as FinalExecutionStatus).SuccessValue + ) + // Reverse the to "find" the last non-empty value! + .reverse() .find((value) => value && value.trim().length > 0); + if (!b64Sig) { + throw new Error( + `No detectable signature found in transaction ${outcome.transaction_outcome?.id}` + ); + } } - if (b64Sig) { - const decodedValue = Buffer.from(b64Sig, "base64").toString("utf-8"); - const signature = JSON.parse(decodedValue); - return transformSignature(signature); - } - throw new Error( - `No detectable signature found in transaction ${outcome.transaction_outcome?.id}` - ); + const decodedValue = Buffer.from(b64Sig, "base64").toString("utf-8"); + const signature = JSON.parse(decodedValue); + return transformSignature(signature); }