@arcium-hq/client 0.1.45
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.
- package/README.md +95 -0
- package/build/index.cjs +12226 -0
- package/build/index.d.ts +21353 -0
- package/build/index.mjs +12160 -0
- package/package.json +76 -0
package/README.md
ADDED
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
# Arcium Client SDK
|
|
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.
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
npm install @arcium-hq/client
|
|
9
|
+
|
|
10
|
+
or
|
|
11
|
+
|
|
12
|
+
```bash
|
|
13
|
+
yarn add @arcium-hq/client
|
|
14
|
+
|
|
15
|
+
or
|
|
16
|
+
|
|
17
|
+
```bash
|
|
18
|
+
pnpm add @arcium-hq/client
|
|
19
|
+
```
|
|
20
|
+
|
|
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
|
+
```
|
|
95
|
+
|