Skip to content

Commit

Permalink
Remove Delegate Signature Recovery (#86)
Browse files Browse the repository at this point in the history
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
  • Loading branch information
bh2smith authored Aug 5, 2024
1 parent 7546451 commit 17daa64
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 40 deletions.
11 changes: 8 additions & 3 deletions src/types/rpc.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
// Basic structure of the JSON-RPC response
export interface JSONRPCResponse {
export interface JSONRPCResponse<T> {
jsonrpc: string;
result: Result;
id: string;
id: number | string | null;
result?: T;
error?: {
code: number;
message: string;
data?: unknown;
};
}

// Result contains various fields like final execution status, an array of receipts, etc.
Expand Down
42 changes: 20 additions & 22 deletions src/utils/signature.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,27 +30,20 @@ export async function signatureFromTxHash(
},
body: JSON.stringify(payload),
});
if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`);
}

const json: JSONRPCResponse<FinalExecutionOutcome> = await response.json();

const jsonResponse = (await response.json()) as JSONRPCResponse;
let base64Sig = jsonResponse.result.status?.SuccessValue;
// TODO: Find an example when successValue isn't available and we need to enter this block.
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 (json.error) {
throw new Error(`JSON-RPC error: ${json.error.message}`);
}
if (base64Sig) {
const decodedValue = Buffer.from(base64Sig, "base64").toString("utf-8");
const signature: MPCSignature = JSON.parse(decodedValue);
return transformSignature(signature);

if (json.result) {
return signatureFromOutcome(json.result);
} else {
throw new Error(`No valid values found in transaction receipt ${txHash}`);
throw new Error(`No FinalExecutionOutcome in response: ${json}`);
}
}

Expand All @@ -70,8 +63,13 @@ export function signatureFromOutcome(
outcome: FinalExecutionOutcome | Partial<FinalExecutionOutcome>
): Signature {
// TODO: Find example outcome when status is not of this casted type.
const b64Sig = (outcome.status as FinalExecutionStatus).SuccessValue!;
const decodedValue = Buffer.from(b64Sig, "base64").toString("utf-8");
const signature = JSON.parse(decodedValue);
return transformSignature(signature);
const b64Sig = (outcome.status as FinalExecutionStatus).SuccessValue;
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}`
);
}
23 changes: 8 additions & 15 deletions tests/unit/utils.signature.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,22 +18,15 @@ describe("utility: get Signature", () => {
});
});

// Still need a NEW example of this!
// it("successful: alternative signatureFromTxHash", async () => {
// const sig = await signatureFromTxHash(
// url,
// "EK4XUwyR29w6eaSfSSPb8he3y7nkTQSbYJVXgSx5vZ4T"
// );
// expect(sig).toEqual({
// big_r:
// "024598E193A9377B98A5B4621BA81FDEEA9DED3E3E7F41C073D0537BC2769C10FC",
// big_s: "65D23B4EA333FFC5486FA295B7AEAB02EACA4E35E22B55108563A63199B96558",
// });
// });

it("failed: signatureFromTxHash", async () => {
it("signatureFromTxHash fails with no signature", async () => {
await expect(signatureFromTxHash(url, failedHash)).rejects.toThrow(
`No valid values found in transaction receipt ${failedHash}`
`No detectable signature found in transaction ${failedHash}`
);
});

it("signatureFromTxHash fails with parse error", async () => {
await expect(signatureFromTxHash(url, "nonsense")).rejects.toThrow(
"JSON-RPC error: Parse error"
);
});

Expand Down

0 comments on commit 17daa64

Please sign in to comment.