@arcium-hq/client 0.1.46 → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (5) hide show
  1. package/README.md +192 -81
  2. package/build/index.cjs +3616 -7529
  3. package/build/index.d.ts +5100 -18357
  4. package/build/index.mjs +3603 -7516
  5. package/package.json +10 -8
package/README.md CHANGED
@@ -1,95 +1,206 @@
1
1
  # Arcium Client SDK
2
2
 
3
- The Arcium client SDK is a TypeScript library for interacting with the Arcium program. It exports the Arcium program's IDL as well as types for interacting with the program's accounts.
3
+ The Arcium Client SDK is a TypeScript library for interacting with the Arcium Solana program, enabling secure multi-party computation on encrypted data.
4
4
 
5
5
  ## Installation
6
6
 
7
7
  ```bash
8
8
  npm install @arcium-hq/client
9
+ # or
10
+ yarn add @arcium-hq/client
11
+ # or
12
+ pnpm add @arcium-hq/client
13
+ ```
9
14
 
10
- or
15
+ ## Quick Start
11
16
 
12
- ```bash
13
- yarn add @arcium-hq/client
17
+ ### 1. Setup and Environment
14
18
 
15
- or
19
+ ```typescript
20
+ import { getArciumEnv } from "@arcium-hq/client";
21
+ import * as anchor from "@coral-xyz/anchor";
16
22
 
17
- ```bash
18
- pnpm add @arcium-hq/client
23
+ // Get Arcium environment configuration
24
+ const arciumEnv = getArciumEnv();
25
+
26
+ // Setup Anchor provider
27
+ anchor.setProvider(anchor.AnchorProvider.env());
28
+ const provider = anchor.getProvider();
29
+ ```
30
+
31
+ ### 2. Encryption Setup
32
+
33
+ To send private data for computation within Arcium, you need to encrypt it using a shared secret derived from your keypair and the Arcium MXE's public key.
34
+
35
+ #### Generate a client keypair:
36
+
37
+ ```typescript
38
+ import { x25519 } from "@arcium-hq/client";
39
+
40
+ const privateKey = x25519.utils.randomPrivateKey();
41
+ const publicKey = x25519.getPublicKey(privateKey);
42
+ ```
43
+
44
+ #### Obtain the MXE's public key:
45
+
46
+ ```typescript
47
+ import { getMXEPublicKey } from "@arcium-hq/client";
48
+
49
+ // Fetch the MXE public key (handles the complex extraction logic internally)
50
+ const mxePublicKey = await getMXEPublicKey(provider, program.programId);
51
+
52
+ if (!mxePublicKey) {
53
+ throw new Error("MXE public key not set");
54
+ }
55
+ ```
56
+
57
+ #### Compute the shared secret and initialize cipher:
58
+
59
+ ```typescript
60
+ import { RescueCipher } from "@arcium-hq/client";
61
+
62
+ const sharedSecret = x25519.getSharedSecret(privateKey, mxePublicKey);
63
+ const cipher = new RescueCipher(sharedSecret);
64
+ ```
65
+
66
+ ### 3. Encrypt and Submit Data
67
+
68
+ ```typescript
69
+ import { randomBytes } from "crypto";
70
+ import { deserializeLE } from "@arcium-hq/client";
71
+
72
+ // Prepare your data as BigInts
73
+ const val1 = BigInt(123);
74
+ const val2 = BigInt(456);
75
+ const plaintext = [val1, val2];
76
+
77
+ // Generate a random nonce (16 bytes)
78
+ const nonce = randomBytes(16);
79
+
80
+ // Encrypt the data
81
+ const ciphertext = cipher.encrypt(plaintext, nonce);
82
+
83
+ // Submit to your program
84
+ const computationOffset = new anchor.BN(randomBytes(8), "hex");
85
+
86
+ const sig = await program.methods
87
+ .yourComputationMethod(
88
+ computationOffset,
89
+ Array.from(ciphertext[0]),
90
+ Array.from(ciphertext[1]),
91
+ Array.from(publicKey),
92
+ new anchor.BN(deserializeLE(nonce).toString())
93
+ )
94
+ .accountsPartial({
95
+ // Account setup - see Account Helpers section
96
+ })
97
+ .rpc({ skipPreflight: true, commitment: "confirmed" });
19
98
  ```
20
99
 
21
- ## Usage
22
-
23
- To send private data for computation within Arcium, you need to encrypt it using a shared secret derived from your keypair and the Arcium MXE's public key. The client SDK provides utilities for this using X25519 key exchange and the Rescue cipher.
24
-
25
- 1. **Generate a client keypair:**
26
- ```typescript
27
- import { x25519 } from "@arcium-hq/client";
28
-
29
- const privateKey = x25519.utils.randomPrivateKey();
30
- const publicKey = x25519.getPublicKey(privateKey);
31
- ```
32
-
33
- 2. **Obtain the MXE's public key:**
34
- *This key is typically specific to the Arcium cluster you are interacting with. You might obtain it through configuration or environment variables.*
35
- ```typescript
36
- // Example public key (replace with the actual key)
37
- const mxePublicKey = new Uint8Array([
38
- /* ... 32 bytes of the MXE public key ... */
39
- ]);
40
- ```
41
-
42
- 3. **Compute the shared secret:**
43
- ```typescript
44
- const sharedSecret = x25519.getSharedSecret(privateKey, mxePublicKey);
45
- ```
46
-
47
- 4. **Initialize the Rescue cipher:**
48
- ```typescript
49
- import { RescueCipher } from "@arcium-hq/client";
50
-
51
- const cipher = new RescueCipher(sharedSecret);
52
- ```
53
-
54
- 5. **Prepare plaintext and encrypt:**
55
- *The plaintext should be an array of BigInts.*
56
- ```typescript
57
- import { randomBytes } from "crypto";
58
-
59
- const val1 = BigInt(123);
60
- const val2 = BigInt(456);
61
- const plaintext: bigint[] = [val1, val2];
62
-
63
- // Generate a random nonce (16 bytes)
64
- const nonce = randomBytes(16);
65
-
66
- // Encrypt the data
67
- const ciphertext = cipher.encrypt(plaintext, nonce);
68
- ```
69
-
70
- 6. **Send data to Arcium:**
71
- *You typically send the `ciphertext`, your `publicKey`, and the `nonce` (often converted to a `BN`) when calling an Arcium program instruction that requires encrypted inputs.*
72
- ```typescript
73
- // Example structure (actual usage depends on the specific program)
74
- // await program.methods.yourInstruction(
75
- // /* ... other args ... */,
76
- // Array.from(ciphertext[0]),
77
- // Array.from(ciphertext[1]),
78
- // Array.from(publicKey),
79
- // new anchor.BN(deserializeLE(nonce).toString())
80
- // )...
81
- ```
82
-
83
- 7. **Decrypting results:**
84
- *When your program receives results from Arcium (e.g., via an event), they will likely be encrypted. Use the same `cipher` instance and the `nonce` provided in the result/event to decrypt.*
85
- ```typescript
86
- // Assuming `event.encryptedResult` is a Uint8Array or number[]
87
- // and `event.resultNonce` is a Uint8Array or number[] from the Arcium callback
88
- const resultCiphertextArray = [event.encryptedResult];
89
- const resultNonceArray = new Uint8Array(event.resultNonce);
90
-
91
- const decryptedResult = cipher.decrypt(resultCiphertextArray, resultNonceArray);
92
- // decryptedResult will be an array of bigints
93
- const resultValue = decryptedResult[0];
94
- ```
100
+ ### 4. Track and Finalize Computation
101
+
102
+ ```typescript
103
+ import { awaitComputationFinalization } from "@arcium-hq/client";
104
+
105
+ // Wait for computation to complete
106
+ const finalizeSig = await awaitComputationFinalization(
107
+ provider as anchor.AnchorProvider,
108
+ computationOffset,
109
+ program.programId,
110
+ "confirmed"
111
+ );
112
+
113
+ console.log("Computation finalized:", finalizeSig);
114
+ ```
115
+
116
+ ### 5. Decrypt Results
117
+
118
+ ```typescript
119
+ // Listen for program events
120
+ const event = await awaitEvent("yourResultEvent");
121
+
122
+ // Decrypt the result using the same cipher
123
+ const decrypted = cipher.decrypt([event.encryptedResult], event.nonce)[0];
124
+ console.log("Decrypted result:", decrypted);
125
+ ```
126
+
127
+ ## Account Helpers
128
+
129
+ The SDK provides helper functions to derive all necessary Arcium PDAs:
130
+
131
+ ```typescript
132
+ import {
133
+ getMXEAccAddress,
134
+ getMempoolAccAddress,
135
+ getCompDefAccAddress,
136
+ getExecutingPoolAccAddress,
137
+ getComputationAccAddress,
138
+ getCompDefAccOffset,
139
+ getArciumAccountBaseSeed,
140
+ getArciumProgAddress,
141
+ } from "@arcium-hq/client";
142
+
143
+ // Get various account addresses
144
+ const mxeAccount = getMXEAccAddress(program.programId);
145
+ const mempoolAccount = getMempoolAccAddress(program.programId);
146
+ const executingPool = getExecutingPoolAccAddress(program.programId);
147
+
148
+ // Get computation definition address
149
+ const compDefOffset = getCompDefAccOffset("your_computation_name");
150
+ const compDefAccount = getCompDefAccAddress(
151
+ program.programId,
152
+ Buffer.from(compDefOffset).readUInt32LE()
153
+ );
154
+
155
+ // Get computation account for a specific offset
156
+ const computationAccount = getComputationAccAddress(
157
+ program.programId,
158
+ computationOffset
159
+ );
160
+ ```
161
+
162
+ ## Circuit Management
163
+
164
+ ### Upload a Circuit
165
+
166
+ ```typescript
167
+ import { uploadCircuit } from "@arcium-hq/client";
168
+ import * as fs from "fs";
169
+
170
+ const rawCircuit = fs.readFileSync("build/your_circuit.arcis");
171
+
172
+ await uploadCircuit(
173
+ provider as anchor.AnchorProvider,
174
+ "your_circuit_name",
175
+ program.programId,
176
+ rawCircuit,
177
+ true // use raw circuit
178
+ );
179
+ ```
180
+
181
+ ### Finalize Computation Definition
182
+
183
+ ```typescript
184
+ import { buildFinalizeCompDefTx } from "@arcium-hq/client";
185
+
186
+ const finalizeTx = await buildFinalizeCompDefTx(
187
+ provider as anchor.AnchorProvider,
188
+ Buffer.from(compDefOffset).readUInt32LE(),
189
+ program.programId
190
+ );
191
+
192
+ // Set blockhash and sign
193
+ const latestBlockhash = await provider.connection.getLatestBlockhash();
194
+ finalizeTx.recentBlockhash = latestBlockhash.blockhash;
195
+ finalizeTx.lastValidBlockHeight = latestBlockhash.lastValidBlockHeight;
196
+ finalizeTx.sign(owner);
197
+
198
+ // Send transaction
199
+ await provider.sendAndConfirm(finalizeTx);
200
+ ```
201
+
202
+ ## API Reference
203
+
204
+ <!-- TODO: Add API reference url -->
95
205
 
206
+ For detailed API documentation, please refer to the [API reference](https://github.com/arcium-hq) included with the package.