@inco/js 0.1.20 → 0.1.22

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 CHANGED
@@ -1,50 +1,106 @@
1
- # @inco/js (called `@inco/js` for now)
1
+ # @inco/js
2
2
 
3
3
  The @inco/js package contains the TypeScript SDK for creating dapps built on Inco.
4
4
 
5
- ## Install the package
5
+ ## Current Status: Active Development
6
6
 
7
- Since the package is private in the Github package registry, you'll need to use npm to log in into the registry first. Obviously, this is only needed if you want to access the package from outside the monorepo, as the monorepo already has workspaces set up.
7
+ The SDK is currently going through active development, to support all new features that Inco offers. As such, do expect breaking changes in subsequent releases, which will be documented in the [CHANGELOG](./CHANGELOG.md). The current state of `@inco/js` includes all low-level blocks to build functional dapps, but an improved public-facing API is also in the works.
8
8
 
9
- 1. Create a Github Personal Access Token (Classic)
9
+ ## Install
10
10
 
11
- Go to the [Personal Access Tokens](https://github.com/settings/tokens) page on Github, and create a new access token with the `read:packages` permission. Give it an explicit name like `inco-js-npm-github-pkg-registry`, and save the access token in a secure place, e.g. your password manager.
12
-
13
- 2. Login via npm to Github Package Registry.
11
+ Choose your favorite package manager:
14
12
 
15
13
  ```bash
16
- cd /path/to/my/project
17
- npm login --scope=@inco-fhevm --registry=https://npm.pkg.github.com
14
+ npm install @inco/js
15
+ # or
16
+ bun install @inco/js
17
+ # or
18
+ yarn add @inco/js
18
19
  ```
19
20
 
20
- You'll be asked to enter Username and Password. Put in your Github username, and paste the access token on step 1 as password.
21
-
22
- 3. Tell npm to look into Github Package Registry.
23
-
24
- Create a new `.npmrc` in `/path/to/my/project`, with the following lines:
21
+ ## Usage
25
22
 
26
- ```
27
- @inco-fhevm:registry=https://npm.pkg.github.com
28
- //npm.pkg.github.com/:_authToken=${READ_PACKAGE_TOKEN}
23
+ A typical usage of `@inco/js` includes 3 steps:
24
+
25
+ 1. Encrypting a value.
26
+ 2. Posting the ciphertext to the contract, which will perform confidential computes on it.
27
+ 3. Requesting a reencryption of the result of the computation.
28
+
29
+ ### 1. Encrypt a value
30
+
31
+ ```ts
32
+ import { getSupportedChain, getViemChain, handleTypes, HexString } from '@inco/js';
33
+ import { EciesScheme, encryptionSchemes, PlaintextWithContextOf } from '@inco/js/encryption';
34
+ import {
35
+ decodeSecp256k1PublicKey,
36
+ generateSecp256k1Keypair,
37
+ getActiveLightningDeployment,
38
+ getEciesEncryptor,
39
+ } from '@inco/js/lite';
40
+ import { createWalletClient, hexToBytes } from 'viem';
41
+
42
+ // Setup. Do it once at initialization.
43
+ const chain = getSupportedChain('baseSepolia');
44
+ const lightningConfig = getActiveLightningDeployment(chain);
45
+ const walletClient = createWalletClient({
46
+ chain: getViemChain(chain),
47
+ account: /* Choose your account */,
48
+ transport: /* Choose your transport */,
49
+ });
50
+ const dappAddress = '0x0000000000000000000000000000000000000000'; // Put your contract address here
51
+
52
+ // Define plaintext, along with context needed for verification.
53
+ const plaintext = 42n; // bigint
54
+ const plaintextWithContext: PlaintextWithContextOf<EciesScheme, typeof handleTypes.euint256> = {
55
+ plaintext: {
56
+ scheme: encryptionSchemes.ecies,
57
+ value: plaintext,
58
+ type: handleTypes.euint256,
59
+ },
60
+ context: {
61
+ hostChainId: BigInt(chain.id),
62
+ aclAddress: lightningConfig.executorAddress,
63
+ userAddress: walletClient.account.address,
64
+ contractAddress: dappAddress,
65
+ },
66
+ };
67
+
68
+ // Encrypt.
69
+ const ephemeralKeypair = await generateSecp256k1Keypair();
70
+ const eciesPubKey = decodeSecp256k1PublicKey(hexToBytes(lightningConfig.eciesPublicKey));
71
+ const encryptor = getEciesEncryptor({
72
+ scheme: encryptionSchemes.ecies,
73
+ pubKeyA: eciesPubKey,
74
+ privKeyB: ephemeralKeypair,
75
+ });
76
+ const { ciphertext } = await encryptor(plaintextWithContext);
77
+ console.log(ciphertext.value); // Will print the encrypted value as hex
29
78
  ```
30
79
 
31
- Then, you can do `npm install @inco/js`!
80
+ ### 2. Post the ciphertext to the contract
32
81
 
33
- Note: If using bun or another package manager, take a look how they manage install scopes.
82
+ For this step, there is nothing specific to the `@inco/js`. We recommend continue using [viem](https://viem.sh). In particular, take a look at their [`writeContract`](https://viem.sh/docs/contract/writeContract) methods to make a transaction on chain, and pass in the `ciphertext.value` from the previous step as the input ciphertext.
34
83
 
35
- 4. Resources
84
+ ### 3. Request a reencryption
36
85
 
37
- - Github docs: https://docs.github.com/en/packages/working-with-a-github-packages-registry/working-with-the-npm-registry
38
- - Medium article: https://medium.com/@aamiralihussain53/a-guide-to-publishing-private-npm-package-on-github-9c533a251e2d
86
+ After submitting your transaction, the Inco covalidators will process your request and perform the computation. In your contract, the result handle of the computation should be stored on chain, let's call it `resultHandle` of type `Handle` (hex).
39
87
 
40
- ## Usage
41
-
42
- Currently, there are no docs as how to use the @inco/js package, as it only exposes low-level functions. There's some API work to do to make it more user-friendly.
88
+ ```ts
89
+ import { incoLiteReencryptor } from "@inco/js/lite";
90
+ import { Hex } from "viem";
43
91
 
44
- To take a look at the low-level functions, see [the E2E test](./test/incolite.ts), which shows how to encrypt, send an encrypted input to the host chain, request a reencrypt and wait for a decrypt.
45
-
46
- ## Publishing the package
92
+ // Reencrypt.
93
+ let resultHandle = "0x..." as Hex; // Retrieve it, e.g. by reading from your dapp contract.
94
+ const reencryptor = await incoLiteReencryptor({
95
+ chainId: BigInt(chain.id),
96
+ walletClient: walletClient,
97
+ }); // This step will ask you to sign an EIP-712 needed for reencryption verification
98
+ const resultPlaintext = await reencryptor({
99
+ handle: resultHandle,
100
+ });
101
+ console.log(resultPlaintext.value); // Plaintext of the final result after computation
102
+ ```
47
103
 
48
- Currently the publishing process is manual, and anyone with enough rights can run `bun run publish:github` to publish to the Github Packages registry.
104
+ ## License
49
105
 
50
- Some gitops process should replace this manual setup.
106
+ See the [LICENSE](./LICENSE) file.