Skip to content

Commit

Permalink
Signature Recovery: Handle Inconsistent Near RPC Receipts (#55)
Browse files Browse the repository at this point in the history
  • Loading branch information
bh2smith authored May 25, 2024
1 parent c674abf commit ab0750d
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 3 deletions.
16 changes: 13 additions & 3 deletions src/utils/signature.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ export async function signatureFromTxHash(
jsonrpc: "2.0",
id: "dontcare",
// This could be replaced with `tx`.
// method: "tx",
method: "EXPERIMENTAL_tx_status",
params: [txHash, accountId],
};
Expand All @@ -27,10 +28,19 @@ export async function signatureFromTxHash(
});

const jsonResponse = (await response.json()) as JSONRPCResponse;
const base64Sig = jsonResponse.result.status.SuccessValue;

let base64Sig = jsonResponse.result.status?.SuccessValue;
if (base64Sig === "") {
// Extract receipts_outcome
const receiptsOutcome = jsonResponse.result.receipts_outcome;
// Map to get SuccessValue
const successValues = receiptsOutcome.map(
// eslint-disable-next-line
(outcome: any) => outcome.outcome.status.SuccessValue
);
// Find the first non-empty value
base64Sig = successValues.find((value) => value && value.trim().length > 0);
}
if (base64Sig) {
// Decode from base64
const decodedValue = Buffer.from(base64Sig, "base64").toString("utf-8");
const [big_r, big_s] = JSON.parse(decodedValue);
return { big_r, big_s };
Expand Down
13 changes: 13 additions & 0 deletions tests/unit/utils.signature.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,19 @@ describe("utility: get Signature", () => {
big_s: "67986E234DEC5D51CF6AED452FE1C4544924218AC20B009F81BAAE53C02AFE76",
});
});

it("successful: alternative signatureFromTxHash", async () => {
const sig = await signatureFromTxHash(
url,
"EK4XUwyR29w6eaSfSSPb8he3y7nkTQSbYJVXgSx5vZ4T"
);
expect(sig).toEqual({
big_r:
"024598E193A9377B98A5B4621BA81FDEEA9DED3E3E7F41C073D0537BC2769C10FC",
big_s: "65D23B4EA333FFC5486FA295B7AEAB02EACA4E35E22B55108563A63199B96558",
});
});

it("failed: signatureFromTxHash", async () => {
await expect(signatureFromTxHash(url, failedHash)).rejects.toThrow(
"No valid values found in the array."
Expand Down

0 comments on commit ab0750d

Please sign in to comment.