Skip to content

Commit 046c371

Browse files
authored
Add auth inputs to delegated proving request (#1081)
* added auth header for proving request method * removed nested try blocks * updated doc string * refactored inputs as params object * modifed submitProvingRequest method docstring
1 parent 853f91b commit 046c371

File tree

1 file changed

+46
-27
lines changed

1 file changed

+46
-27
lines changed

sdk/src/network-client.ts

Lines changed: 46 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,21 @@ interface AleoNetworkClientOptions {
2121
headers?: { [key: string]: string };
2222
}
2323

24+
/**
25+
* Options for executing a fee authorization.
26+
*
27+
* @property provingRequest {ProvingRequest | string} The proving request being submitted to the network.
28+
* @property url {string} The URL of the delegated proving service.
29+
* @property apiKey {string} The API key to use for authentication. NOTE: This is not currently used but will be in a future release.
30+
* @property delegatedProvingJWT {string} JWT token for authentication. NOTE: This will be deprecated in favor of apiKey in a future release.
31+
*/
32+
interface DelegatedProvingParams {
33+
provingRequest: ProvingRequest | string;
34+
url?: string;
35+
apiKey?: string;
36+
delegatedProvingJWT?: string;
37+
}
38+
2439
/**
2540
* Client library that encapsulates REST calls to publicly exposed endpoints of Aleo nodes. The methods provided in this
2641
* allow users to query public information from the Aleo blockchain and submit transactions to the network.
@@ -1597,38 +1612,42 @@ class AleoNetworkClient {
15971612
/**
15981613
* Submit a `ProvingRequest` to a remote proving service for delegated proving. If the broadcast flag of the `ProvingRequest` is set to `true` the remote service will attempt to broadcast the result `Transaction` on behalf of the requestor.
15991614
*
1600-
* @param {ProvingRequest | string} provingRequest - The `ProvingRequest` (generated by the ProgramManager) to submit.
1601-
* @param {string} url - (Optional) The url of the proving service.
1615+
* @param {DelegatedProvingParams} options - The optional parameters required to submit a proving request.
16021616
* @returns {Promise<ProvingResponse>} The ProvingResponse containing the transaction result and the result of the broadcast if the `broadcast` flag was set to `true`.
16031617
*/
1604-
async submitProvingRequest(provingRequest: ProvingRequest | string, url?: string): Promise<ProvingResponse> {
1605-
const prover_uri = url ? url : this.host;
1606-
const provingRequestString =
1607-
provingRequest instanceof ProvingRequest
1608-
? provingRequest.toString()
1609-
: provingRequest;
1618+
async submitProvingRequest(options: DelegatedProvingParams): Promise<ProvingResponse> {
1619+
const proverUri = options.url ?? this.host;
1620+
const provingRequestString = options.provingRequest instanceof ProvingRequest
1621+
? options.provingRequest.toString()
1622+
: options.provingRequest;
1623+
1624+
// Build headers with proper auth fallback
1625+
const headers: Record<string, string> = {
1626+
...this.headers,
1627+
"X-ALEO-METHOD": "submitProvingRequest",
1628+
"Content-Type": "application/json"
1629+
};
1630+
1631+
// Add auth header based on what's available
1632+
if (options.delegatedProvingJWT) {
1633+
headers["Authorization"] = `Bearer ${options.delegatedProvingJWT}`;
1634+
} else if (options.apiKey) {
1635+
headers["X-API-Key"] = options.apiKey;
1636+
}
1637+
16101638
try {
16111639
const response = await retryWithBackoff(() =>
1612-
post(prover_uri + "/prove", {
1613-
body: provingRequestString,
1614-
headers: Object.assign({}, {...this.headers, "X-ALEO-METHOD": "submitProvingRequest"}, {
1615-
"Content-Type": "application/json",
1616-
}),
1617-
}),
1618-
);
1619-
1620-
try {
1621-
const text = await response.text();
1622-
return parseJSON(text);
1623-
} catch (error: any) {
1624-
throw new Error(
1625-
`Error posting proving request. Aleo network response: ${error.message}`,
1626-
);
1627-
}
1628-
} catch (error: any) {
1629-
throw new Error(
1630-
`Error posting proving request: No response received: ${error.message}`,
1640+
post(`${proverUri}/prove`, {
1641+
body: provingRequestString,
1642+
headers
1643+
})
16311644
);
1645+
1646+
const responseText = await response.text();
1647+
return parseJSON(responseText);
1648+
} catch (error) {
1649+
const errorMessage = error instanceof Error ? error.message : 'Unknown error';
1650+
throw new Error(`Failed to submit proving request: ${errorMessage}`);
16321651
}
16331652
}
16341653

0 commit comments

Comments
 (0)