Skip to content

Commit c1fc21a

Browse files
[DPS] Add functionality for encrypting ProvingRequests (#1172)
* Add functionality for encrypting ProvingRequest ciphertexts * Make cryptobox functions synchronous, enclose private request functions in a retry loop, update delegated proving types * Add encryption for scanner registration requests * Modify provingResponse type to handle all possible responses * Add correct types for the Proving Response + add a ProvingRequestSafe method that enables handling of responses by the caller instead of throwing * Allow for ProvingResponse types to be BigInts
1 parent c2d4e47 commit c1fc21a

File tree

12 files changed

+509
-39
lines changed

12 files changed

+509
-39
lines changed

sdk/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@
5656
"comlink": "^4.4.2",
5757
"core-js": "^3.40.0",
5858
"mime": "^4.0.6",
59+
"libsodium-wrappers": "^0.8.2",
5960
"xmlhttprequest-ssl": "^4.0.0"
6061
},
6162
"devDependencies": {

sdk/src/browser.ts

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,10 @@ import { Account } from "./account.js";
44
import { AleoNetworkClient, ProgramImports } from "./network-client.js";
55
import { BlockJSON, Header, Metadata } from "./models/blockJSON.js";
66
import { ConfirmedTransactionJSON } from "./models/confirmed_transaction.js";
7+
import { CryptoBoxPubKey } from "./models/cryptoBoxPubkey.js";
78
import { DeploymentJSON, VerifyingKeys } from "./models/deployment/deploymentJSON.js";
89
import { DeploymentObject } from "./models/deployment/deploymentObject.js";
10+
import { EncryptedProvingRequest } from "./models/encryptedProvingRequest.js";
911
import { EncryptedRecord } from "./models/record-provider/encryptedRecord.js";
1012
import { ExecutionJSON, FeeExecutionJSON } from "./models/execution/executionJSON.js";
1113
import { ExecutionObject, FeeExecutionObject } from "./models/execution/executionObject.js";
@@ -25,7 +27,7 @@ import { PlaintextLiteral} from "./models/plaintext/literal.js";
2527
import { PlaintextObject } from "./models/plaintext/plaintext.js";
2628
import { PlaintextStruct} from "./models/plaintext/struct.js";
2729
import { ProvingRequestJSON } from "./models/provingRequest.js";
28-
import { ProvingResponse } from "./models/provingResponse.js";
30+
import { ProvingResponse, BroadcastResponse, BroadcastResult, ProvingResult, ProvingFailure, ProvingSuccess, ProveApiErrorBody, ProvingRequestError, isProvingResponse, isProveApiErrorBody } from "./models/provingResponse.js";
2931
import { RatificationJSON } from "./models/ratification.js";
3032
import { RecordsFilter } from "./models/record-scanner/recordsFilter.js";
3133
import { RecordsResponseFilter } from "./models/record-scanner/recordsResponseFilter.js";
@@ -142,10 +144,14 @@ export {
142144
AleoNetworkClient,
143145
BlockJSON,
144146
BlockHeightSearch,
147+
BroadcastResponse,
148+
BroadcastResult,
145149
CachedKeyPair,
146150
ConfirmedTransactionJSON,
151+
CryptoBoxPubKey,
147152
DeploymentJSON,
148153
DeploymentObject,
154+
EncryptedProvingRequest,
149155
EncryptedRecord,
150156
ExecutionJSON,
151157
ExecutionObject,
@@ -157,6 +163,8 @@ export {
157163
FunctionKeyPair,
158164
FunctionKeyProvider,
159165
Header,
166+
isProvingResponse,
167+
isProveApiErrorBody,
160168
ImportedPrograms,
161169
ImportedVerifyingKeys,
162170
InputJSON,
@@ -177,7 +185,12 @@ export {
177185
PlaintextObject,
178186
PlaintextStruct,
179187
ProgramImports,
188+
ProveApiErrorBody,
189+
ProvingFailure,
190+
ProvingRequestError,
180191
ProvingRequestJSON,
192+
ProvingResult,
193+
ProvingSuccess,
181194
ProvingResponse,
182195
RatificationJSON,
183196
RecordsFilter,
@@ -194,3 +207,5 @@ export {
194207
TransitionObject,
195208
VerifyingKeys,
196209
};
210+
211+
export { encryptAuthorization, encryptProvingRequest, encryptViewKey, encryptRegistrationRequest } from "./security.js";

sdk/src/constants.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,5 +118,5 @@ export const RECORD_DOMAIN = "RecordScannerV0";
118118
* Zero address on Aleo blockchain that corresponds to field element 0. Used as padding in Merkle trees and as a sentinel value.
119119
*/
120120
export const ZERO_ADDRESS = "aleo1qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq3ljyzc";
121-
122121
export const FIVE_MINUTES = 5 * 60 * 1000; // 5 minutes in milliseconds
122+

sdk/src/models/cryptoBoxPubkey.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
export interface CryptoBoxPubKey {
2+
key_id: string,
3+
public_key: string,
4+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
export interface EncryptedProvingRequest {
2+
key_id: string,
3+
ciphertext: string,
4+
}

sdk/src/models/provingResponse.ts

Lines changed: 63 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,66 @@
1-
import { TransactionJSON } from "./transaction/transactionJSON";
1+
import { TransactionJSON } from "./transaction/transactionJSON.js";
22

3+
/** HTTP status and optional message from snarkOS broadcast (Accepted/Rejected variants). */
4+
export interface BroadcastResponse {
5+
status_code: bigint | number;
6+
message?: string;
7+
}
8+
9+
/** Result of the optional broadcast step. Discriminated by `status`. */
10+
export type BroadcastResult =
11+
| { status: "Accepted"; status_code: bigint | number; message?: string }
12+
| { status: "Rejected"; status_code: bigint | number; message?: string }
13+
| { status: "Failed"; message: string }
14+
| { status: "Skipped" };
15+
16+
/** Success response body for POST /prove (HTTP 200). */
317
export interface ProvingResponse {
4-
transaction: TransactionJSON,
5-
broadcast?: boolean,
18+
transaction: TransactionJSON;
19+
broadcast_result: BroadcastResult;
20+
}
21+
22+
/** Error response body for POST /prove (HTTP 400, 500, 503). Same shape for all error cases. */
23+
export interface ProveApiErrorBody {
24+
message: string;
25+
}
26+
27+
/** Error thrown on prove API failure; `status` is set for retry logic (e.g. retryWithBackoff checks error.status >= 500). */
28+
export interface ProvingRequestError extends Error {
29+
status?: bigint | number;
30+
}
31+
32+
/** Success variant of a proving request result. */
33+
export interface ProvingSuccess {
34+
ok: true;
35+
data: ProvingResponse;
36+
}
37+
38+
/** Failure variant of a proving request result (HTTP 400, 500, 503). */
39+
export interface ProvingFailure {
40+
ok: false;
41+
status: bigint | number;
42+
error: ProveApiErrorBody;
43+
}
44+
45+
/** Result of a proving request. Type used to give callers the ability to self-handle errors. */
46+
export type ProvingResult = ProvingSuccess | ProvingFailure;
47+
48+
/** Type guard: value is a ProvingResponse. */
49+
export function isProvingResponse(value: unknown): value is ProvingResponse {
50+
return (
51+
typeof value === "object" &&
52+
value !== null &&
53+
"transaction" in value &&
54+
"broadcast_result" in value &&
55+
typeof (value as ProvingResponse).broadcast_result === "object"
56+
);
57+
}
58+
59+
/** Type guard: value is a ProveApiErrorBody. */
60+
export function isProveApiErrorBody(value: unknown): value is ProveApiErrorBody {
61+
return (
62+
typeof value === "object" &&
63+
value !== null &&
64+
"message" in value
65+
);
666
}

0 commit comments

Comments
 (0)