@inco/js 0.8.0-devnet-11 → 0.8.0-devnet-12
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 +541 -47
- package/dist/cjs/binary.d.ts +84 -0
- package/dist/cjs/binary.js +81 -4
- package/dist/cjs/chain.d.ts +21 -4
- package/dist/cjs/chain.js +18 -7
- package/dist/cjs/encryption/encryption.d.ts +115 -0
- package/dist/cjs/encryption/encryption.js +98 -7
- package/dist/cjs/generated/local-node.d.ts +2 -2
- package/dist/cjs/generated/local-node.js +2 -2
- package/dist/cjs/handle.d.ts +41 -0
- package/dist/cjs/handle.js +31 -6
- package/dist/cjs/lite/lightning.d.ts +19 -0
- package/dist/cjs/lite/lightning.js +20 -5
- package/dist/cjs/local/local-node.d.ts +18 -0
- package/dist/cjs/local/local-node.js +18 -3
- package/dist/cjs/reencryption/eip712.d.ts +27 -0
- package/dist/cjs/reencryption/eip712.js +20 -7
- package/dist/cjs/reencryption/types.d.ts +24 -0
- package/dist/cjs/viem.d.ts +11 -0
- package/dist/cjs/viem.js +12 -1
- package/dist/esm/binary.d.ts +84 -0
- package/dist/esm/binary.js +81 -4
- package/dist/esm/chain.d.ts +21 -4
- package/dist/esm/chain.js +17 -6
- package/dist/esm/encryption/encryption.d.ts +115 -0
- package/dist/esm/encryption/encryption.js +98 -7
- package/dist/esm/generated/local-node.d.ts +2 -2
- package/dist/esm/generated/local-node.js +2 -2
- package/dist/esm/handle.d.ts +41 -0
- package/dist/esm/handle.js +31 -6
- package/dist/esm/lite/lightning.d.ts +19 -0
- package/dist/esm/lite/lightning.js +20 -5
- package/dist/esm/local/local-node.d.ts +18 -0
- package/dist/esm/local/local-node.js +18 -3
- package/dist/esm/reencryption/eip712.d.ts +27 -0
- package/dist/esm/reencryption/eip712.js +20 -7
- package/dist/esm/reencryption/types.d.ts +24 -0
- package/dist/esm/viem.d.ts +11 -0
- package/dist/esm/viem.js +12 -1
- package/dist/types/binary.d.ts +84 -0
- package/dist/types/chain.d.ts +21 -4
- package/dist/types/encryption/encryption.d.ts +115 -0
- package/dist/types/generated/local-node.d.ts +2 -2
- package/dist/types/handle.d.ts +41 -0
- package/dist/types/lite/lightning.d.ts +19 -0
- package/dist/types/local/local-node.d.ts +18 -0
- package/dist/types/reencryption/eip712.d.ts +27 -0
- package/dist/types/reencryption/types.d.ts +24 -0
- package/dist/types/viem.d.ts +11 -0
- package/package.json +1 -7
|
@@ -1,37 +1,79 @@
|
|
|
1
1
|
import { Schema } from 'effect';
|
|
2
2
|
import { ByteArray, Hex } from 'viem';
|
|
3
3
|
import { Bytes32 } from '../binary.js';
|
|
4
|
+
/**
|
|
5
|
+
* A function that encrypts a plaintext value, embedding its {@link InputContext} into the resulting ciphertext.
|
|
6
|
+
*
|
|
7
|
+
* @typeParam S - The encryption scheme (e.g. X-Wing).
|
|
8
|
+
* @example
|
|
9
|
+
* ```ts
|
|
10
|
+
* const result = await encrypt({ plaintext: { scheme: 2, type: 5, value: 42n }, context });
|
|
11
|
+
* // result.handle, result.ciphertext, result.prehandle
|
|
12
|
+
* ```
|
|
13
|
+
*/
|
|
4
14
|
export type Encryptor<S extends EncryptionScheme = EncryptionScheme> = <T extends SupportedFheType>(plaintext: PlaintextWithContextOf<S, T>) => Promise<EncryptResultOf<S, T>>;
|
|
15
|
+
/**
|
|
16
|
+
* A function that decrypts a ciphertext back to its plaintext value.
|
|
17
|
+
*
|
|
18
|
+
* @typeParam S - The encryption scheme (e.g. X-Wing).
|
|
19
|
+
* @example
|
|
20
|
+
* ```ts
|
|
21
|
+
* const plaintext = await decrypt({ scheme: 2, type: 5, value: '0x...' });
|
|
22
|
+
* ```
|
|
23
|
+
*/
|
|
5
24
|
export type Decryptor<S extends EncryptionScheme = EncryptionScheme> = <T extends SupportedFheType>(ciphertext: CiphertextOf<S, T>) => Promise<PlaintextOf<S, T>>;
|
|
25
|
+
/**
|
|
26
|
+
* Subset of ENCRYPTION types currently supported for encryption/decryption.
|
|
27
|
+
*
|
|
28
|
+
* @remarks TODO: review need of `euint64` and `euint160` — `Lib.sol` only supports `euint256` and `ebool`.
|
|
29
|
+
*/
|
|
6
30
|
export declare const supportedFheTypes: {
|
|
7
31
|
readonly euint64: 5;
|
|
8
32
|
readonly euint160: 7;
|
|
9
33
|
readonly euint256: 8;
|
|
10
34
|
readonly ebool: 0;
|
|
11
35
|
};
|
|
36
|
+
/** Schema that validates a string is one of the supported ENCRYPTION type names. */
|
|
12
37
|
export declare const SupportedFheTypeName: Schema.SchemaClass<"ebool" | "euint64" | "euint160" | "euint256", "ebool" | "euint64" | "euint160" | "euint256", never>;
|
|
38
|
+
/** A supported ENCRYPTION type name (e.g. `"euint256"`, `"ebool"`). */
|
|
13
39
|
export type SupportedFheTypeName = typeof SupportedFheTypeName.Type;
|
|
40
|
+
/** Schema that validates a number is one of the supported ENCRYPTION type integer identifiers. */
|
|
14
41
|
export declare const SupportedFheType: Schema.SchemaClass<0 | 5 | 7 | 8, 0 | 5 | 7 | 8, never>;
|
|
42
|
+
/** Integer identifier of a supported ENCRYPTION type (a subset of all ENCRYPTION types). */
|
|
15
43
|
export type SupportedFheType = typeof SupportedFheType.Type;
|
|
44
|
+
/** Map of encryption scheme names to their integer identifiers. Currently only X-Wing (2) is supported. */
|
|
16
45
|
export declare const encryptionSchemes: {
|
|
17
46
|
readonly xwing: 2;
|
|
18
47
|
};
|
|
48
|
+
/**
|
|
49
|
+
* Returns the human-readable name of an encryption scheme.
|
|
50
|
+
* @param scheme - The encryption scheme integer identifier.
|
|
51
|
+
* @returns The scheme name (e.g. `"X-Wing"`).
|
|
52
|
+
* @throws If the scheme identifier is unknown.
|
|
53
|
+
*/
|
|
19
54
|
export declare function getEncryptionSchemeName(scheme: number): string;
|
|
55
|
+
/** The typeof {@link encryptionSchemes} — mapping from scheme names to integer IDs. */
|
|
20
56
|
export type EncryptionSchemes = typeof encryptionSchemes;
|
|
57
|
+
/** The integer identifier for the X-Wing encryption scheme (`2`). */
|
|
21
58
|
export type XwingScheme = EncryptionSchemes['xwing'];
|
|
59
|
+
/** Schema that validates a value is a known encryption scheme identifier. */
|
|
22
60
|
export declare const EncryptionScheme: Schema.Literal<[2]>;
|
|
61
|
+
/** An encryption scheme identifier (currently only X-Wing = `2`). */
|
|
23
62
|
export type EncryptionScheme = typeof EncryptionScheme.Type;
|
|
24
63
|
type DistType<P, S extends EncryptionScheme, T extends SupportedFheType> = P extends any ? P & {
|
|
25
64
|
scheme: S;
|
|
26
65
|
type: T;
|
|
27
66
|
} : never;
|
|
67
|
+
/** Schema for an ENCRYPTION ciphertext: encryption scheme, ENCRYPTION type, and the encrypted value as a hex string. */
|
|
28
68
|
export declare const Ciphertext: Schema.Struct<{
|
|
29
69
|
scheme: Schema.Literal<[2]>;
|
|
30
70
|
type: Schema.SchemaClass<0 | 5 | 7 | 8, 0 | 5 | 7 | 8, never>;
|
|
31
71
|
value: Schema.TemplateLiteral<`0x${string}`>;
|
|
32
72
|
}>;
|
|
73
|
+
/** An ENCRYPTION ciphertext containing the encryption scheme, ENCRYPTION type, and encrypted hex value. */
|
|
33
74
|
export type Ciphertext = typeof Ciphertext.Type;
|
|
34
75
|
export type CiphertextOf<S extends EncryptionScheme, T extends SupportedFheType> = DistType<Ciphertext, S, T>;
|
|
76
|
+
/** Schema for a ciphertext paired with the {@link InputContext} it was encrypted under. */
|
|
35
77
|
export declare const CiphertextWithContext: Schema.Struct<{
|
|
36
78
|
ciphertext: Schema.Struct<{
|
|
37
79
|
scheme: Schema.Literal<[2]>;
|
|
@@ -46,10 +88,12 @@ export declare const CiphertextWithContext: Schema.Struct<{
|
|
|
46
88
|
version: typeof Schema.Number;
|
|
47
89
|
}>;
|
|
48
90
|
}>;
|
|
91
|
+
/** A ciphertext paired with the {@link InputContext} it was encrypted under. */
|
|
49
92
|
export type CiphertextWithContext = typeof CiphertextWithContext.Type;
|
|
50
93
|
export type CiphertextWithContextOf<S extends EncryptionScheme, T extends SupportedFheType> = CiphertextWithContext & {
|
|
51
94
|
ciphertext: CiphertextOf<S, T>;
|
|
52
95
|
};
|
|
96
|
+
/** Schema for the result of an encryption operation: ciphertext, input context, prehandle, and final handle. */
|
|
53
97
|
export declare const EncryptResult: Schema.Struct<{
|
|
54
98
|
ciphertext: Schema.Struct<{
|
|
55
99
|
scheme: Schema.Literal<[2]>;
|
|
@@ -66,10 +110,16 @@ export declare const EncryptResult: Schema.Struct<{
|
|
|
66
110
|
prehandle: Schema.brand<Schema.filter<Schema.transformOrFail<Schema.Union<[typeof Schema.String, Schema.refine<object & Uint8Array<ArrayBufferLike>, Schema.Schema<object, object, never>>]>, Schema.TemplateLiteral<`0x${string}`>, never>>, "Bytes32">;
|
|
67
111
|
handle: Schema.brand<Schema.filter<Schema.transformOrFail<Schema.Union<[typeof Schema.String, Schema.refine<object & Uint8Array<ArrayBufferLike>, Schema.Schema<object, object, never>>]>, Schema.TemplateLiteral<`0x${string}`>, never>>, "Bytes32">;
|
|
68
112
|
}>;
|
|
113
|
+
/** The result of an encryption operation, containing the ciphertext, context, prehandle, and deterministic handle. */
|
|
69
114
|
export type EncryptResult = typeof EncryptResult.Type;
|
|
70
115
|
export type EncryptResultOf<S extends EncryptionScheme, T extends SupportedFheType> = EncryptResult & {
|
|
71
116
|
ciphertext: CiphertextOf<S, T>;
|
|
72
117
|
};
|
|
118
|
+
/**
|
|
119
|
+
* Schema for an ENCRYPTION plaintext value. The `value` field type depends on the ENCRYPTION type:
|
|
120
|
+
* - `euint64` / `euint160` / `euint256`: `bigint`
|
|
121
|
+
* - `ebool`: `boolean`
|
|
122
|
+
*/
|
|
73
123
|
export declare const Plaintext: Schema.Union<[Schema.Struct<{
|
|
74
124
|
scheme: Schema.Literal<[2]>;
|
|
75
125
|
type: Schema.Literal<[5, 7, 8]>;
|
|
@@ -79,8 +129,10 @@ export declare const Plaintext: Schema.Union<[Schema.Struct<{
|
|
|
79
129
|
type: Schema.Literal<[0]>;
|
|
80
130
|
value: typeof Schema.Boolean;
|
|
81
131
|
}>]>;
|
|
132
|
+
/** An ENCRYPTION plaintext value — `bigint` for integer types, `boolean` for `ebool`. */
|
|
82
133
|
export type Plaintext = typeof Plaintext.Type;
|
|
83
134
|
export type PlaintextOf<S extends EncryptionScheme, T extends SupportedFheType> = DistType<Plaintext, S, T>;
|
|
135
|
+
/** Schema for a plaintext paired with the {@link InputContext} it will be encrypted under. */
|
|
84
136
|
export declare const PlaintextWithContext: Schema.Struct<{
|
|
85
137
|
plaintext: Schema.Union<[Schema.Struct<{
|
|
86
138
|
scheme: Schema.Literal<[2]>;
|
|
@@ -99,20 +151,83 @@ export declare const PlaintextWithContext: Schema.Struct<{
|
|
|
99
151
|
version: typeof Schema.Number;
|
|
100
152
|
}>;
|
|
101
153
|
}>;
|
|
154
|
+
/** A plaintext paired with the {@link InputContext} it will be encrypted under. */
|
|
102
155
|
export type PlaintextWithContext = typeof PlaintextWithContext.Type;
|
|
103
156
|
export type PlaintextWithContextOf<S extends EncryptionScheme, T extends SupportedFheType> = PlaintextWithContext & {
|
|
104
157
|
plaintext: PlaintextOf<S, T>;
|
|
105
158
|
};
|
|
159
|
+
/**
|
|
160
|
+
* Converts a `bigint` to a typed {@link Plaintext} value.
|
|
161
|
+
*
|
|
162
|
+
* For integer ENCRYPTION types (`euint64`, `euint160`, `euint256`) the value is passed through as-is.
|
|
163
|
+
* For `ebool`, non-zero values become `true`.
|
|
164
|
+
*
|
|
165
|
+
* @param scheme - The encryption scheme identifier.
|
|
166
|
+
* @param type - The ENCRYPTION type to interpret the value as.
|
|
167
|
+
* @param bigPt - The raw bigint plaintext value.
|
|
168
|
+
* @returns A typed `Plaintext` matching the given scheme and ENCRYPTION type.
|
|
169
|
+
* @throws If `type` is not a supported ENCRYPTION type.
|
|
170
|
+
*/
|
|
106
171
|
export declare function bigintToPlaintext<S extends EncryptionScheme, T extends SupportedFheType>(scheme: S, type: T, bigPt: bigint): PlaintextOf<S, T>;
|
|
172
|
+
/**
|
|
173
|
+
* Decodes an ABI-encoded ciphertext input into its components.
|
|
174
|
+
*
|
|
175
|
+
* The input format is: `4-byte version || abi.encode(bytes32 handle, bytes ciphertext)`.
|
|
176
|
+
*
|
|
177
|
+
* @param input - The raw encoded input as a byte array or hex string.
|
|
178
|
+
* @returns The decoded version, handle, and ciphertext.
|
|
179
|
+
*/
|
|
107
180
|
export declare function decodeCiphertextInput(input: ByteArray | Hex): {
|
|
108
181
|
version: number;
|
|
109
182
|
handle: Hex;
|
|
110
183
|
ciphertext: Hex;
|
|
111
184
|
};
|
|
185
|
+
/**
|
|
186
|
+
* Encodes a handle and ciphertext into the on-chain input format.
|
|
187
|
+
*
|
|
188
|
+
* The output format is: `4-byte version || abi.encode(bytes32 handle, bytes ciphertext)`.
|
|
189
|
+
*
|
|
190
|
+
* @param version - The encoding version (int32).
|
|
191
|
+
* @param handle - The 32-byte handle as a hex string.
|
|
192
|
+
* @param ciphertext - The ciphertext as a hex string.
|
|
193
|
+
* @returns The ABI-encoded input as a hex string.
|
|
194
|
+
*/
|
|
112
195
|
export declare function encodeCiphertextInput(version: number, handle: Hex, ciphertext: Hex): Hex;
|
|
196
|
+
/**
|
|
197
|
+
* Converts a {@link Plaintext} to its `bigint` representation.
|
|
198
|
+
*
|
|
199
|
+
* Integer types pass through directly; `ebool` maps `true` → `1n`, `false` → `0n`.
|
|
200
|
+
*
|
|
201
|
+
* @param plaintext - The plaintext to convert.
|
|
202
|
+
* @returns The bigint representation of the plaintext value.
|
|
203
|
+
*/
|
|
113
204
|
export declare function plaintextToBigint(plaintext: Plaintext): bigint;
|
|
205
|
+
/**
|
|
206
|
+
* Converts a {@link Plaintext} to a {@link Bytes32} hex string.
|
|
207
|
+
* @param plaintext - The plaintext to convert.
|
|
208
|
+
* @returns A 32-byte hex representation of the plaintext value.
|
|
209
|
+
*/
|
|
114
210
|
export declare function plaintextToBytes32(plaintext: Plaintext): Bytes32;
|
|
211
|
+
/**
|
|
212
|
+
* Converts a {@link Plaintext} to a 32-byte `Buffer`.
|
|
213
|
+
* @param plaintext - The plaintext to convert.
|
|
214
|
+
* @returns A 32-byte big-endian buffer of the plaintext value.
|
|
215
|
+
*/
|
|
115
216
|
export declare function plaintextToBytes(plaintext: Plaintext): Buffer;
|
|
217
|
+
/**
|
|
218
|
+
* Parses a {@link Bytes32} hex string into a typed {@link Plaintext}.
|
|
219
|
+
* @param plaintext - The 32-byte hex value to interpret.
|
|
220
|
+
* @param scheme - The encryption scheme identifier.
|
|
221
|
+
* @param type - The ENCRYPTION type to interpret the bytes as.
|
|
222
|
+
* @returns A typed `Plaintext` value.
|
|
223
|
+
*/
|
|
116
224
|
export declare function bytes32ToPlaintext(plaintext: Bytes32, scheme: EncryptionScheme, type: SupportedFheType): Plaintext;
|
|
225
|
+
/**
|
|
226
|
+
* Parses a `Uint8Array` into a typed {@link Plaintext}.
|
|
227
|
+
* @param plaintext - The byte array to interpret.
|
|
228
|
+
* @param scheme - The encryption scheme identifier.
|
|
229
|
+
* @param type - The ENCRYPTION type to interpret the bytes as.
|
|
230
|
+
* @returns A typed `Plaintext` value.
|
|
231
|
+
*/
|
|
117
232
|
export declare function bytesToPlaintext(plaintext: Uint8Array, scheme: EncryptionScheme, type: SupportedFheType): Plaintext;
|
|
118
233
|
export {};
|
|
@@ -2,7 +2,11 @@ import { Schema } from 'effect';
|
|
|
2
2
|
import { bytesToHex, decodeAbiParameters, encodeAbiParameters, hexToBytes, } from 'viem';
|
|
3
3
|
import { bigintToBytes, bigintToBytes32, Bytes32, bytesToBigInt, HexString, } from '../binary.js';
|
|
4
4
|
import { handleTypes, InputContext } from '../handle.js';
|
|
5
|
-
|
|
5
|
+
/**
|
|
6
|
+
* Subset of ENCRYPTION types currently supported for encryption/decryption.
|
|
7
|
+
*
|
|
8
|
+
* @remarks TODO: review need of `euint64` and `euint160` — `Lib.sol` only supports `euint256` and `ebool`.
|
|
9
|
+
*/
|
|
6
10
|
export const supportedFheTypes = {
|
|
7
11
|
euint64: handleTypes.euint64,
|
|
8
12
|
euint160: handleTypes.euint160,
|
|
@@ -10,12 +14,21 @@ export const supportedFheTypes = {
|
|
|
10
14
|
ebool: handleTypes.ebool,
|
|
11
15
|
};
|
|
12
16
|
const supportedFheTypeNames = Object.keys(supportedFheTypes);
|
|
17
|
+
/** Schema that validates a string is one of the supported ENCRYPTION type names. */
|
|
13
18
|
export const SupportedFheTypeName = Schema.Literal(...supportedFheTypeNames);
|
|
19
|
+
/** Schema that validates a number is one of the supported ENCRYPTION type integer identifiers. */
|
|
14
20
|
// TODO: extend to all types
|
|
15
21
|
export const SupportedFheType = Schema.Literal(...Object.values(supportedFheTypes));
|
|
22
|
+
/** Map of encryption scheme names to their integer identifiers. Currently only X-Wing (2) is supported. */
|
|
16
23
|
export const encryptionSchemes = {
|
|
17
24
|
xwing: 2,
|
|
18
25
|
};
|
|
26
|
+
/**
|
|
27
|
+
* Returns the human-readable name of an encryption scheme.
|
|
28
|
+
* @param scheme - The encryption scheme integer identifier.
|
|
29
|
+
* @returns The scheme name (e.g. `"X-Wing"`).
|
|
30
|
+
* @throws If the scheme identifier is unknown.
|
|
31
|
+
*/
|
|
19
32
|
export function getEncryptionSchemeName(scheme) {
|
|
20
33
|
switch (scheme) {
|
|
21
34
|
case encryptionSchemes.xwing:
|
|
@@ -24,22 +37,31 @@ export function getEncryptionSchemeName(scheme) {
|
|
|
24
37
|
throw new Error(`Unknown encryption scheme: ${scheme}`);
|
|
25
38
|
}
|
|
26
39
|
}
|
|
40
|
+
/** Schema that validates a value is a known encryption scheme identifier. */
|
|
27
41
|
export const EncryptionScheme = Schema.Literal(encryptionSchemes.xwing);
|
|
42
|
+
/** Schema for an ENCRYPTION ciphertext: encryption scheme, ENCRYPTION type, and the encrypted value as a hex string. */
|
|
28
43
|
export const Ciphertext = Schema.Struct({
|
|
29
44
|
scheme: EncryptionScheme,
|
|
30
45
|
type: SupportedFheType,
|
|
31
46
|
value: HexString,
|
|
32
47
|
});
|
|
48
|
+
/** Schema for a ciphertext paired with the {@link InputContext} it was encrypted under. */
|
|
33
49
|
export const CiphertextWithContext = Schema.Struct({
|
|
34
50
|
ciphertext: Ciphertext,
|
|
35
51
|
context: InputContext,
|
|
36
52
|
});
|
|
53
|
+
/** Schema for the result of an encryption operation: ciphertext, input context, prehandle, and final handle. */
|
|
37
54
|
export const EncryptResult = Schema.Struct({
|
|
38
55
|
ciphertext: Ciphertext,
|
|
39
56
|
context: InputContext,
|
|
40
57
|
prehandle: Bytes32,
|
|
41
58
|
handle: Bytes32,
|
|
42
59
|
});
|
|
60
|
+
/**
|
|
61
|
+
* Schema for an ENCRYPTION plaintext value. The `value` field type depends on the ENCRYPTION type:
|
|
62
|
+
* - `euint64` / `euint160` / `euint256`: `bigint`
|
|
63
|
+
* - `ebool`: `boolean`
|
|
64
|
+
*/
|
|
43
65
|
export const Plaintext = Schema.Union(Schema.Struct({
|
|
44
66
|
scheme: EncryptionScheme,
|
|
45
67
|
type: Schema.Literal(handleTypes.euint64, handleTypes.euint160, handleTypes.euint256),
|
|
@@ -49,10 +71,23 @@ export const Plaintext = Schema.Union(Schema.Struct({
|
|
|
49
71
|
type: Schema.Literal(handleTypes.ebool),
|
|
50
72
|
value: Schema.Boolean,
|
|
51
73
|
}));
|
|
74
|
+
/** Schema for a plaintext paired with the {@link InputContext} it will be encrypted under. */
|
|
52
75
|
export const PlaintextWithContext = Schema.Struct({
|
|
53
76
|
plaintext: Plaintext,
|
|
54
77
|
context: InputContext,
|
|
55
78
|
});
|
|
79
|
+
/**
|
|
80
|
+
* Converts a `bigint` to a typed {@link Plaintext} value.
|
|
81
|
+
*
|
|
82
|
+
* For integer ENCRYPTION types (`euint64`, `euint160`, `euint256`) the value is passed through as-is.
|
|
83
|
+
* For `ebool`, non-zero values become `true`.
|
|
84
|
+
*
|
|
85
|
+
* @param scheme - The encryption scheme identifier.
|
|
86
|
+
* @param type - The ENCRYPTION type to interpret the value as.
|
|
87
|
+
* @param bigPt - The raw bigint plaintext value.
|
|
88
|
+
* @returns A typed `Plaintext` matching the given scheme and ENCRYPTION type.
|
|
89
|
+
* @throws If `type` is not a supported ENCRYPTION type.
|
|
90
|
+
*/
|
|
56
91
|
export function bigintToPlaintext(scheme, type, bigPt) {
|
|
57
92
|
switch (type) {
|
|
58
93
|
case handleTypes.euint64:
|
|
@@ -74,15 +109,29 @@ export function bigintToPlaintext(scheme, type, bigPt) {
|
|
|
74
109
|
}
|
|
75
110
|
throw new Error(`Unsupported Encryption Scheme type: ${type}`);
|
|
76
111
|
}
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
112
|
+
/**
|
|
113
|
+
* ABI parameter schema for on-chain ciphertext input encoding.
|
|
114
|
+
*
|
|
115
|
+
* Compatible with Solidity's `abi.decode(input, (bytes32, bytes))`:
|
|
116
|
+
* ```solidity
|
|
117
|
+
* (bytes32 externalHandle, bytes memory ciphertext) = abi.decode(input, (bytes32, bytes));
|
|
118
|
+
* ```
|
|
119
|
+
*
|
|
120
|
+
* This is the source of truth for the encoding format and does not refer to
|
|
121
|
+
* an actual contract or Solidity type's ABI.
|
|
122
|
+
*/
|
|
82
123
|
const ciphertextInputAbi = [
|
|
83
124
|
{ name: 'handle', type: 'bytes32' },
|
|
84
125
|
{ name: 'ciphertext', type: 'bytes' },
|
|
85
126
|
];
|
|
127
|
+
/**
|
|
128
|
+
* Decodes an ABI-encoded ciphertext input into its components.
|
|
129
|
+
*
|
|
130
|
+
* The input format is: `4-byte version || abi.encode(bytes32 handle, bytes ciphertext)`.
|
|
131
|
+
*
|
|
132
|
+
* @param input - The raw encoded input as a byte array or hex string.
|
|
133
|
+
* @returns The decoded version, handle, and ciphertext.
|
|
134
|
+
*/
|
|
86
135
|
export function decodeCiphertextInput(input) {
|
|
87
136
|
const bytes = typeof input === 'string' ? hexToBytes(input) : input;
|
|
88
137
|
const buf = new Uint8Array(bytes).buffer;
|
|
@@ -90,6 +139,16 @@ export function decodeCiphertextInput(input) {
|
|
|
90
139
|
const [handle, ciphertext] = decodeAbiParameters(ciphertextInputAbi, bytes.slice(4));
|
|
91
140
|
return { version, handle, ciphertext };
|
|
92
141
|
}
|
|
142
|
+
/**
|
|
143
|
+
* Encodes a handle and ciphertext into the on-chain input format.
|
|
144
|
+
*
|
|
145
|
+
* The output format is: `4-byte version || abi.encode(bytes32 handle, bytes ciphertext)`.
|
|
146
|
+
*
|
|
147
|
+
* @param version - The encoding version (int32).
|
|
148
|
+
* @param handle - The 32-byte handle as a hex string.
|
|
149
|
+
* @param ciphertext - The ciphertext as a hex string.
|
|
150
|
+
* @returns The ABI-encoded input as a hex string.
|
|
151
|
+
*/
|
|
93
152
|
export function encodeCiphertextInput(version, handle, ciphertext) {
|
|
94
153
|
const rest = hexToBytes(encodeAbiParameters(ciphertextInputAbi, [handle, ciphertext]));
|
|
95
154
|
const buf = new ArrayBuffer(rest.length + 4);
|
|
@@ -99,6 +158,14 @@ export function encodeCiphertextInput(version, handle, ciphertext) {
|
|
|
99
158
|
view.set(rest, 4);
|
|
100
159
|
return bytesToHex(view);
|
|
101
160
|
}
|
|
161
|
+
/**
|
|
162
|
+
* Converts a {@link Plaintext} to its `bigint` representation.
|
|
163
|
+
*
|
|
164
|
+
* Integer types pass through directly; `ebool` maps `true` → `1n`, `false` → `0n`.
|
|
165
|
+
*
|
|
166
|
+
* @param plaintext - The plaintext to convert.
|
|
167
|
+
* @returns The bigint representation of the plaintext value.
|
|
168
|
+
*/
|
|
102
169
|
export function plaintextToBigint(plaintext) {
|
|
103
170
|
switch (plaintext.type) {
|
|
104
171
|
case handleTypes.euint64:
|
|
@@ -109,16 +176,40 @@ export function plaintextToBigint(plaintext) {
|
|
|
109
176
|
return plaintext.value ? 1n : 0n;
|
|
110
177
|
}
|
|
111
178
|
}
|
|
179
|
+
/**
|
|
180
|
+
* Converts a {@link Plaintext} to a {@link Bytes32} hex string.
|
|
181
|
+
* @param plaintext - The plaintext to convert.
|
|
182
|
+
* @returns A 32-byte hex representation of the plaintext value.
|
|
183
|
+
*/
|
|
112
184
|
export function plaintextToBytes32(plaintext) {
|
|
113
185
|
return bigintToBytes32(plaintextToBigint(plaintext));
|
|
114
186
|
}
|
|
187
|
+
/**
|
|
188
|
+
* Converts a {@link Plaintext} to a 32-byte `Buffer`.
|
|
189
|
+
* @param plaintext - The plaintext to convert.
|
|
190
|
+
* @returns A 32-byte big-endian buffer of the plaintext value.
|
|
191
|
+
*/
|
|
115
192
|
export function plaintextToBytes(plaintext) {
|
|
116
193
|
return bigintToBytes(plaintextToBigint(plaintext));
|
|
117
194
|
}
|
|
195
|
+
/**
|
|
196
|
+
* Parses a {@link Bytes32} hex string into a typed {@link Plaintext}.
|
|
197
|
+
* @param plaintext - The 32-byte hex value to interpret.
|
|
198
|
+
* @param scheme - The encryption scheme identifier.
|
|
199
|
+
* @param type - The ENCRYPTION type to interpret the bytes as.
|
|
200
|
+
* @returns A typed `Plaintext` value.
|
|
201
|
+
*/
|
|
118
202
|
export function bytes32ToPlaintext(plaintext, scheme, type) {
|
|
119
203
|
return bigintToPlaintext(scheme, type, BigInt(plaintext));
|
|
120
204
|
}
|
|
205
|
+
/**
|
|
206
|
+
* Parses a `Uint8Array` into a typed {@link Plaintext}.
|
|
207
|
+
* @param plaintext - The byte array to interpret.
|
|
208
|
+
* @param scheme - The encryption scheme identifier.
|
|
209
|
+
* @param type - The ENCRYPTION type to interpret the bytes as.
|
|
210
|
+
* @returns A typed `Plaintext` value.
|
|
211
|
+
*/
|
|
121
212
|
export function bytesToPlaintext(plaintext, scheme, type) {
|
|
122
213
|
return bigintToPlaintext(scheme, type, BigInt(bytesToBigInt(plaintext)));
|
|
123
214
|
}
|
|
124
|
-
//# sourceMappingURL=data:application/json;base64,
|
|
215
|
+
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiZW5jcnlwdGlvbi5qcyIsInNvdXJjZVJvb3QiOiIiLCJzb3VyY2VzIjpbIi4uLy4uLy4uL3NyYy9lbmNyeXB0aW9uL2VuY3J5cHRpb24udHMiXSwibmF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0FBQyxLQUFLLENBQUMsQ0FBQyxDQUFDLEVBQUUsQ0FBQyxDQUFDLENBQUMsRUFBRSxDQUFDO0lBQ3JDLENBQUM7QUFDSCxDQUFDO0FBRUQ7Ozs7R0FJRztBQUNILE1BQU0sVUFBVSxrQkFBa0IsQ0FBQyxTQUFvQjtJQUNyRCxPQUFPLGVBQWUsQ0FBQyxpQkFBaUIsQ0FBQyxTQUFTLENBQUMsQ0FBQyxDQUFDO0FBQ3ZELENBQUM7QUFFRDs7OztHQUlHO0FBQ0gsTUFBTSxVQUFVLGdCQUFnQixDQUFDLFNBQW9CO0lBQ25ELE9BQU8sYUFBYSxDQUFDLGlCQUFpQixDQUFDLFNBQVMsQ0FBQyxDQUFDLENBQUM7QUFDckQsQ0FBQztBQUVEOzs7Ozs7R0FNRztBQUNILE1BQU0sVUFBVSxrQkFBa0IsQ0FDaEMsU0FBa0IsRUFDbEIsTUFBd0IsRUFDeEIsSUFBc0I7SUFFdEIsT0FBTyxpQkFBaUIsQ0FBQyxNQUFNLEVBQUUsSUFBSSxFQUFFLE1BQU0sQ0FBQyxTQUFTLENBQUMsQ0FBQyxDQUFDO0FBQzVELENBQUM7QUFFRDs7Ozs7O0dBTUc7QUFDSCxNQUFNLFVBQVUsZ0JBQWdCLENBQzlCLFNBQXFCLEVBQ3JCLE1BQXdCLEVBQ3hCLElBQXNCO0lBRXRCLE9BQU8saUJBQWlCLENBQUMsTUFBTSxFQUFFLElBQUksRUFBRSxNQUFNLENBQUMsYUFBYSxDQUFDLFNBQVMsQ0FBQyxDQUFDLENBQUMsQ0FBQztBQUMzRSxDQUFDIn0=
|
|
@@ -19,9 +19,9 @@ export declare const localNodeLightningConfig: {
|
|
|
19
19
|
readonly executorAddress: "0xDF3830489208461f72Df6E45D0e6cbF9DBB74fe1";
|
|
20
20
|
readonly chainId: 31337;
|
|
21
21
|
readonly covalidatorUrls: readonly ["http://localhost:50055"];
|
|
22
|
-
readonly signers: readonly ["
|
|
22
|
+
readonly signers: readonly ["0x16844D5b82DdA890dc4DFc1407Ce4b1ebFdF6a93"];
|
|
23
23
|
readonly hostChainRpcUrl: "http://localhost:8545";
|
|
24
|
-
readonly senderPrivateKey: "
|
|
24
|
+
readonly senderPrivateKey: "0x3f9007c8e8de6208a5af414990834e80920255754b247f8a2cdeb293587cab1b";
|
|
25
25
|
};
|
|
26
26
|
readonly alphanet: {
|
|
27
27
|
readonly executorAddress: "0xc0d693DeEF0A91CE39208676b6da09B822abd199";
|
|
@@ -47,10 +47,10 @@ export const localNodeLightningConfig = {
|
|
|
47
47
|
"http://localhost:50055"
|
|
48
48
|
],
|
|
49
49
|
"signers": [
|
|
50
|
-
"
|
|
50
|
+
"0x16844D5b82DdA890dc4DFc1407Ce4b1ebFdF6a93"
|
|
51
51
|
],
|
|
52
52
|
"hostChainRpcUrl": "http://localhost:8545",
|
|
53
|
-
"senderPrivateKey": "
|
|
53
|
+
"senderPrivateKey": "0x3f9007c8e8de6208a5af414990834e80920255754b247f8a2cdeb293587cab1b"
|
|
54
54
|
},
|
|
55
55
|
"alphanet": {
|
|
56
56
|
"executorAddress": "0xc0d693DeEF0A91CE39208676b6da09B822abd199",
|
package/dist/esm/handle.d.ts
CHANGED
|
@@ -1,6 +1,14 @@
|
|
|
1
1
|
import { Schema } from 'effect';
|
|
2
2
|
import { HexString } from './binary.js';
|
|
3
|
+
/** Current handle version byte, appended as the last byte of every handle. */
|
|
3
4
|
export declare const HANDLE_VERSION = 0;
|
|
5
|
+
/**
|
|
6
|
+
* Map of ENCRYPTION type names to their integer identifiers (matching tfhe-rs `FheType`).
|
|
7
|
+
*
|
|
8
|
+
* Keys provide a semantic interpretation over the underlying integral type.
|
|
9
|
+
* For example, `ebool` interprets a `uint16` as a boolean, `ebytes64` interprets
|
|
10
|
+
* a `uint512` as a 64-byte array, etc.
|
|
11
|
+
*/
|
|
4
12
|
export declare const handleTypes: Readonly<{
|
|
5
13
|
ebool: 0;
|
|
6
14
|
euint4: 1;
|
|
@@ -15,11 +23,21 @@ export declare const handleTypes: Readonly<{
|
|
|
15
23
|
ebytes128: 10;
|
|
16
24
|
ebytes256: 11;
|
|
17
25
|
}>;
|
|
26
|
+
/** Schema that validates a string is one of the known ENCRYPTION type names (e.g. `"ebool"`, `"euint256"`). */
|
|
18
27
|
export declare const HandleTypeName: Schema.SchemaClass<"ebool" | "euint4" | "euint8" | "euint16" | "euint32" | "euint64" | "euint128" | "euint160" | "euint256" | "ebytes64" | "ebytes128" | "ebytes256", "ebool" | "euint4" | "euint8" | "euint16" | "euint32" | "euint64" | "euint128" | "euint160" | "euint256" | "ebytes64" | "ebytes128" | "ebytes256", never>;
|
|
28
|
+
/** A valid ENCRYPTION type name string (e.g. `"ebool"`, `"euint32"`, `"ebytes64"`). */
|
|
19
29
|
export type HandleTypeName = typeof HandleTypeName.Type;
|
|
30
|
+
/** The typeof {@link handleTypes} — mapping from ENCRYPTION type names to integer IDs. */
|
|
20
31
|
export type HandleTypes = typeof handleTypes;
|
|
32
|
+
/** Integer identifier of an ENCRYPTION type (0–11), corresponding to a {@link HandleTypeName}. */
|
|
21
33
|
export type FheType = HandleTypes[keyof HandleTypes];
|
|
34
|
+
/**
|
|
35
|
+
* Type guard that checks whether a number is a valid {@link FheType} identifier.
|
|
36
|
+
* @param value - The number to check.
|
|
37
|
+
* @returns `true` if `value` is a known ENCRYPTION type integer (0–11).
|
|
38
|
+
*/
|
|
22
39
|
export declare function isFheType(value: number): value is FheType;
|
|
40
|
+
/** Schema for the context required to compute a deterministic ENCRYPTION handle from a prehandle. */
|
|
23
41
|
export declare const InputContext: Schema.Struct<{
|
|
24
42
|
hostChainId: typeof Schema.BigInt;
|
|
25
43
|
aclAddress: Schema.TemplateLiteral<`0x${string}`>;
|
|
@@ -27,7 +45,15 @@ export declare const InputContext: Schema.Struct<{
|
|
|
27
45
|
contractAddress: Schema.TemplateLiteral<`0x${string}`>;
|
|
28
46
|
version: typeof Schema.Number;
|
|
29
47
|
}>;
|
|
48
|
+
/** Context fields (chain ID, ACL address, user/contract addresses, version) used to derive a deterministic handle. */
|
|
30
49
|
export type InputContext = typeof InputContext.Type;
|
|
50
|
+
/**
|
|
51
|
+
* A 32-byte hex string representing an ENCRYPTION ciphertext handle.
|
|
52
|
+
*
|
|
53
|
+
* Format: `keccak_hash[0:29] || index_handle || handle_type || handle_version`
|
|
54
|
+
*
|
|
55
|
+
* @see {@link https://github.com/Inco-fhevm/inco-monorepo/blob/636756f512ae16535bdcb9a43df76bf14fbd6ba7/pkg/fhe/handle.md Handle format spec}
|
|
56
|
+
*/
|
|
31
57
|
export type Handle = HexString;
|
|
32
58
|
/**
|
|
33
59
|
* Expected length of a handle in bytes.
|
|
@@ -41,6 +67,13 @@ export declare const HANDLE_LENGTH_BYTES = 32;
|
|
|
41
67
|
* @throws Error if handle is malformed (wrong format, length, or unsupported version)
|
|
42
68
|
*/
|
|
43
69
|
export declare function validateHandle(handle: HexString): void;
|
|
70
|
+
/**
|
|
71
|
+
* Extracts the {@link FheType} from byte 30 of a validated handle.
|
|
72
|
+
* @param handle - A 32-byte hex handle string.
|
|
73
|
+
* @returns The ENCRYPTION type identifier stored in the handle.
|
|
74
|
+
* @throws If the handle is malformed or contains an invalid ENCRYPTION type.
|
|
75
|
+
* @see {@link https://github.com/Inco-fhevm/inco-monorepo/blob/636756f512ae16535bdcb9a43df76bf14fbd6ba7/pkg/fhe/handle.md Handle format spec}
|
|
76
|
+
*/
|
|
44
77
|
export declare function getHandleType(handle: HexString): FheType;
|
|
45
78
|
/**
|
|
46
79
|
* Computes the prehandle hash for an input based on the ciphertext
|
|
@@ -67,4 +100,12 @@ export declare function computeHandle({ prehandle, context, }: {
|
|
|
67
100
|
prehandle: Uint8Array;
|
|
68
101
|
context: InputContext;
|
|
69
102
|
}): Buffer;
|
|
103
|
+
/**
|
|
104
|
+
* Computes the Keccak-256 hash of an ABI-packed {@link InputContext}.
|
|
105
|
+
*
|
|
106
|
+
* The context is packed as: `"evm/" || chainId || aclAddress || userAddress || contractAddress || version`.
|
|
107
|
+
*
|
|
108
|
+
* @param context - The input context to hash.
|
|
109
|
+
* @returns A 32-byte `Buffer` containing the Keccak-256 digest.
|
|
110
|
+
*/
|
|
70
111
|
export declare function hashInputContext(context: InputContext): Buffer;
|
package/dist/esm/handle.js
CHANGED
|
@@ -3,10 +3,15 @@ import { Schema } from 'effect';
|
|
|
3
3
|
import { Keccak } from 'sha3';
|
|
4
4
|
import { encodePacked, hexToBytes, toHex } from 'viem';
|
|
5
5
|
import { HexString } from './binary.js';
|
|
6
|
+
/** Current handle version byte, appended as the last byte of every handle. */
|
|
6
7
|
export const HANDLE_VERSION = 0;
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
8
|
+
/**
|
|
9
|
+
* Map of ENCRYPTION type names to their integer identifiers (matching tfhe-rs `FheType`).
|
|
10
|
+
*
|
|
11
|
+
* Keys provide a semantic interpretation over the underlying integral type.
|
|
12
|
+
* For example, `ebool` interprets a `uint16` as a boolean, `ebytes64` interprets
|
|
13
|
+
* a `uint512` as a 64-byte array, etc.
|
|
14
|
+
*/
|
|
10
15
|
export const handleTypes = Object.freeze({
|
|
11
16
|
ebool: 0,
|
|
12
17
|
euint4: 1,
|
|
@@ -22,10 +27,17 @@ export const handleTypes = Object.freeze({
|
|
|
22
27
|
ebytes256: 11,
|
|
23
28
|
});
|
|
24
29
|
const handleNames = Object.keys(handleTypes);
|
|
30
|
+
/** Schema that validates a string is one of the known ENCRYPTION type names (e.g. `"ebool"`, `"euint256"`). */
|
|
25
31
|
export const HandleTypeName = Schema.Literal(...handleNames);
|
|
32
|
+
/**
|
|
33
|
+
* Type guard that checks whether a number is a valid {@link FheType} identifier.
|
|
34
|
+
* @param value - The number to check.
|
|
35
|
+
* @returns `true` if `value` is a known ENCRYPTION type integer (0–11).
|
|
36
|
+
*/
|
|
26
37
|
export function isFheType(value) {
|
|
27
38
|
return Object.values(handleTypes).includes(value);
|
|
28
39
|
}
|
|
40
|
+
/** Schema for the context required to compute a deterministic ENCRYPTION handle from a prehandle. */
|
|
29
41
|
export const InputContext = Schema.Struct({
|
|
30
42
|
hostChainId: Schema.BigInt,
|
|
31
43
|
aclAddress: HexString,
|
|
@@ -57,8 +69,13 @@ export function validateHandle(handle) {
|
|
|
57
69
|
throw new Error(`Unsupported handle version: expected ${HANDLE_VERSION}, got ${handleVersion} for handle ${handle}`);
|
|
58
70
|
}
|
|
59
71
|
}
|
|
60
|
-
|
|
61
|
-
|
|
72
|
+
/**
|
|
73
|
+
* Extracts the {@link FheType} from byte 30 of a validated handle.
|
|
74
|
+
* @param handle - A 32-byte hex handle string.
|
|
75
|
+
* @returns The ENCRYPTION type identifier stored in the handle.
|
|
76
|
+
* @throws If the handle is malformed or contains an invalid ENCRYPTION type.
|
|
77
|
+
* @see {@link https://github.com/Inco-fhevm/inco-monorepo/blob/636756f512ae16535bdcb9a43df76bf14fbd6ba7/pkg/fhe/handle.md Handle format spec}
|
|
78
|
+
*/
|
|
62
79
|
export function getHandleType(handle) {
|
|
63
80
|
// Validate handle integrity before extracting type
|
|
64
81
|
validateHandle(handle);
|
|
@@ -129,6 +146,14 @@ export function computeHandle({ prehandle, context, }) {
|
|
|
129
146
|
prehandle.slice(29, 32),
|
|
130
147
|
]);
|
|
131
148
|
}
|
|
149
|
+
/**
|
|
150
|
+
* Computes the Keccak-256 hash of an ABI-packed {@link InputContext}.
|
|
151
|
+
*
|
|
152
|
+
* The context is packed as: `"evm/" || chainId || aclAddress || userAddress || contractAddress || version`.
|
|
153
|
+
*
|
|
154
|
+
* @param context - The input context to hash.
|
|
155
|
+
* @returns A 32-byte `Buffer` containing the Keccak-256 digest.
|
|
156
|
+
*/
|
|
132
157
|
export function hashInputContext(context) {
|
|
133
158
|
const packed = encodePacked(['string', 'uint256', 'address', 'address', 'address', 'uint16'], [
|
|
134
159
|
// Note: The x/hostchain spec requires the chain ID to be prefixed with 'evm/' for EVM chains
|
|
@@ -146,4 +171,4 @@ function assertUint8(value) {
|
|
|
146
171
|
throw new Error(`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