@inco/js 0.1.21 → 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 +86 -30
- package/dist/encryption/index.cjs +1698 -1
- package/dist/encryption/index.mjs +1698 -1
- package/dist/generated/abis/addTwo.d.ts +8 -0
- package/dist/generated/abis/index.cjs +11 -0
- package/dist/generated/abis/index.mjs +11 -0
- package/dist/index.cjs +1945 -1
- package/dist/index.d.ts +1 -0
- package/dist/index.mjs +1945 -1
- package/dist/lite/index.cjs +1727 -30
- package/dist/lite/index.mjs +1727 -30
- package/dist/lite/reencrypt.d.ts +2 -2
- package/dist/reencryption/index.cjs +1698 -1
- package/dist/reencryption/index.mjs +1698 -1
- package/package.json +1 -1
package/README.md
CHANGED
@@ -1,50 +1,106 @@
|
|
1
|
-
# @inco/js
|
1
|
+
# @inco/js
|
2
2
|
|
3
3
|
The @inco/js package contains the TypeScript SDK for creating dapps built on Inco.
|
4
4
|
|
5
|
-
##
|
5
|
+
## Current Status: Active Development
|
6
6
|
|
7
|
-
|
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
|
-
|
9
|
+
## Install
|
10
10
|
|
11
|
-
|
12
|
-
|
13
|
-
2. Login via npm to Github Package Registry.
|
11
|
+
Choose your favorite package manager:
|
14
12
|
|
15
13
|
```bash
|
16
|
-
|
17
|
-
|
14
|
+
npm install @inco/js
|
15
|
+
# or
|
16
|
+
bun install @inco/js
|
17
|
+
# or
|
18
|
+
yarn add @inco/js
|
18
19
|
```
|
19
20
|
|
20
|
-
|
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
|
-
|
28
|
-
|
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
|
-
|
80
|
+
### 2. Post the ciphertext to the contract
|
32
81
|
|
33
|
-
|
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
|
-
|
84
|
+
### 3. Request a reencryption
|
36
85
|
|
37
|
-
|
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
|
-
|
41
|
-
|
42
|
-
|
88
|
+
```ts
|
89
|
+
import { incoLiteReencryptor } from "@inco/js/lite";
|
90
|
+
import { Hex } from "viem";
|
43
91
|
|
44
|
-
|
45
|
-
|
46
|
-
|
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
|
-
|
104
|
+
## License
|
49
105
|
|
50
|
-
|
106
|
+
See the [LICENSE](./LICENSE) file.
|