Skip to content

Commit 17daa64

Browse files
authored
Remove Delegate Signature Recovery (#86)
The only reason we had that special search in signature retrieval was for delegated actions through a relayer. The MPC contract recently introduced a 1 yoctoNear deposit so I am guessing that this is no longer possible. Reference Transaction: https://testnet.nearblocks.io/txns/EK4XUwyR29w6eaSfSSPb8he3y7nkTQSbYJVXgSx5vZ4T?tab=execution
1 parent 7546451 commit 17daa64

File tree

3 files changed

+36
-40
lines changed

3 files changed

+36
-40
lines changed

src/types/rpc.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,13 @@
11
// Basic structure of the JSON-RPC response
2-
export interface JSONRPCResponse {
2+
export interface JSONRPCResponse<T> {
33
jsonrpc: string;
4-
result: Result;
5-
id: string;
4+
id: number | string | null;
5+
result?: T;
6+
error?: {
7+
code: number;
8+
message: string;
9+
data?: unknown;
10+
};
611
}
712

813
// Result contains various fields like final execution status, an array of receipts, etc.

src/utils/signature.ts

Lines changed: 20 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -30,27 +30,20 @@ export async function signatureFromTxHash(
3030
},
3131
body: JSON.stringify(payload),
3232
});
33+
if (!response.ok) {
34+
throw new Error(`HTTP error! status: ${response.status}`);
35+
}
36+
37+
const json: JSONRPCResponse<FinalExecutionOutcome> = await response.json();
3338

34-
const jsonResponse = (await response.json()) as JSONRPCResponse;
35-
let base64Sig = jsonResponse.result.status?.SuccessValue;
36-
// TODO: Find an example when successValue isn't available and we need to enter this block.
37-
if (base64Sig === "") {
38-
// Extract receipts_outcome
39-
const receiptsOutcome = jsonResponse.result.receipts_outcome;
40-
// Map to get SuccessValue
41-
const successValues = receiptsOutcome.map(
42-
// eslint-disable-next-line
43-
(outcome: any) => outcome.outcome.status.SuccessValue
44-
);
45-
// Find the first non-empty value
46-
base64Sig = successValues.find((value) => value && value.trim().length > 0);
39+
if (json.error) {
40+
throw new Error(`JSON-RPC error: ${json.error.message}`);
4741
}
48-
if (base64Sig) {
49-
const decodedValue = Buffer.from(base64Sig, "base64").toString("utf-8");
50-
const signature: MPCSignature = JSON.parse(decodedValue);
51-
return transformSignature(signature);
42+
43+
if (json.result) {
44+
return signatureFromOutcome(json.result);
5245
} else {
53-
throw new Error(`No valid values found in transaction receipt ${txHash}`);
46+
throw new Error(`No FinalExecutionOutcome in response: ${json}`);
5447
}
5548
}
5649

@@ -70,8 +63,13 @@ export function signatureFromOutcome(
7063
outcome: FinalExecutionOutcome | Partial<FinalExecutionOutcome>
7164
): Signature {
7265
// TODO: Find example outcome when status is not of this casted type.
73-
const b64Sig = (outcome.status as FinalExecutionStatus).SuccessValue!;
74-
const decodedValue = Buffer.from(b64Sig, "base64").toString("utf-8");
75-
const signature = JSON.parse(decodedValue);
76-
return transformSignature(signature);
66+
const b64Sig = (outcome.status as FinalExecutionStatus).SuccessValue;
67+
if (b64Sig) {
68+
const decodedValue = Buffer.from(b64Sig, "base64").toString("utf-8");
69+
const signature = JSON.parse(decodedValue);
70+
return transformSignature(signature);
71+
}
72+
throw new Error(
73+
`No detectable signature found in transaction ${outcome.transaction_outcome?.id}`
74+
);
7775
}

tests/unit/utils.signature.test.ts

Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -18,22 +18,15 @@ describe("utility: get Signature", () => {
1818
});
1919
});
2020

21-
// Still need a NEW example of this!
22-
// it("successful: alternative signatureFromTxHash", async () => {
23-
// const sig = await signatureFromTxHash(
24-
// url,
25-
// "EK4XUwyR29w6eaSfSSPb8he3y7nkTQSbYJVXgSx5vZ4T"
26-
// );
27-
// expect(sig).toEqual({
28-
// big_r:
29-
// "024598E193A9377B98A5B4621BA81FDEEA9DED3E3E7F41C073D0537BC2769C10FC",
30-
// big_s: "65D23B4EA333FFC5486FA295B7AEAB02EACA4E35E22B55108563A63199B96558",
31-
// });
32-
// });
33-
34-
it("failed: signatureFromTxHash", async () => {
21+
it("signatureFromTxHash fails with no signature", async () => {
3522
await expect(signatureFromTxHash(url, failedHash)).rejects.toThrow(
36-
`No valid values found in transaction receipt ${failedHash}`
23+
`No detectable signature found in transaction ${failedHash}`
24+
);
25+
});
26+
27+
it("signatureFromTxHash fails with parse error", async () => {
28+
await expect(signatureFromTxHash(url, "nonsense")).rejects.toThrow(
29+
"JSON-RPC error: Parse error"
3730
);
3831
});
3932

0 commit comments

Comments
 (0)