@cofhe/abi 0.0.0-beta-20260216104742
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/CHANGELOG.md +20 -0
- package/README.md +184 -0
- package/dist/index.cjs +331 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +210 -0
- package/dist/index.d.ts +210 -0
- package/dist/index.js +304 -0
- package/dist/index.js.map +1 -0
- package/package.json +54 -0
- package/src/encryptedInputs.ts +451 -0
- package/src/encryptedReturnTypes.ts +279 -0
- package/src/fhenixMap.ts +162 -0
- package/src/index.ts +6 -0
- package/src/mockEncrypt.ts +86 -0
- package/src/mockEncryptedInput.ts +139 -0
- package/src/utils.ts +205 -0
package/CHANGELOG.md
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
# @cofhe/abi Changelog
|
|
2
|
+
|
|
3
|
+
## 0.0.0-beta-20260216104742
|
|
4
|
+
|
|
5
|
+
### Patch Changes
|
|
6
|
+
|
|
7
|
+
- be9bfd9: Fix deployment include `dist` folder.
|
|
8
|
+
- @cofhe/sdk@0.0.0-beta-20260216104742
|
|
9
|
+
|
|
10
|
+
## 0.2.0
|
|
11
|
+
|
|
12
|
+
### Patch Changes
|
|
13
|
+
|
|
14
|
+
- Updated dependencies [8fda09a]
|
|
15
|
+
- Updated dependencies [4057a76]
|
|
16
|
+
- Updated dependencies [dba2759]
|
|
17
|
+
- Updated dependencies [e0caeca]
|
|
18
|
+
- Updated dependencies [7c861af]
|
|
19
|
+
- Updated dependencies [2a9d6c5]
|
|
20
|
+
- @cofhe/sdk@0.2.0
|
package/README.md
ADDED
|
@@ -0,0 +1,184 @@
|
|
|
1
|
+
# @cofhe/abi
|
|
2
|
+
|
|
3
|
+
Type-safe ABI utilities for Fhenix fully homomorphic encryption (FHE) smart contracts. Provides type-safe transformations between encrypted input/output types and their underlying primitive values.
|
|
4
|
+
|
|
5
|
+
## Overview
|
|
6
|
+
|
|
7
|
+
Fhenix contracts use encrypted types (e.g., `euint32`, `ebool`) that are represented as structs in ABIs. This package bridges the gap between:
|
|
8
|
+
|
|
9
|
+
- **Encrypted types** (`struct InEuint32`, `euint32`) - the ABI representation
|
|
10
|
+
- **Primitive types** (`bigint`, `boolean`, `string`) - developer-friendly values
|
|
11
|
+
- **Encryptable types** (`EncryptableItem`) - intermediate encryption format
|
|
12
|
+
|
|
13
|
+
The package provides compile-time type safety through TypeScript generics, ensuring encrypted values are correctly extracted, transformed, and inserted based on ABI definitions.
|
|
14
|
+
|
|
15
|
+
## Installation
|
|
16
|
+
|
|
17
|
+
```bash
|
|
18
|
+
npm install @cofhe/abi
|
|
19
|
+
# or
|
|
20
|
+
pnpm add @cofhe/abi
|
|
21
|
+
# or
|
|
22
|
+
yarn add @cofhe/abi
|
|
23
|
+
```
|
|
24
|
+
|
|
25
|
+
**Peer Dependencies:**
|
|
26
|
+
|
|
27
|
+
- `@cofhe/sdk` - Core encryption types and utilities
|
|
28
|
+
- `abitype` - ABI type utilities
|
|
29
|
+
|
|
30
|
+
## Quick Start
|
|
31
|
+
|
|
32
|
+
```typescript
|
|
33
|
+
import { extractEncryptableValues, insertEncryptedValues, transformEncryptedReturnTypes } from '@cofhe/abi';
|
|
34
|
+
import type { CofheInputArgs, CofheInputArgsPreTransform, CofheReturnType } from '@cofhe/abi';
|
|
35
|
+
import { encrypt } from '@cofhe/sdk';
|
|
36
|
+
|
|
37
|
+
const abi = [
|
|
38
|
+
{
|
|
39
|
+
type: 'function',
|
|
40
|
+
name: 'add',
|
|
41
|
+
inputs: [
|
|
42
|
+
{ name: 'a', type: 'uint256', internalType: 'uint256' },
|
|
43
|
+
{ name: 'b', type: 'tuple', internalType: 'struct InEuint32', components: [...] }
|
|
44
|
+
],
|
|
45
|
+
outputs: [{ name: '', type: 'uint256', internalType: 'euint32' }],
|
|
46
|
+
stateMutability: 'nonpayable'
|
|
47
|
+
}
|
|
48
|
+
] as const;
|
|
49
|
+
|
|
50
|
+
// 1. Prepare arguments with primitive values
|
|
51
|
+
const args: CofheInputArgsPreTransform<typeof abi, 'add'> = [100n, 200n];
|
|
52
|
+
|
|
53
|
+
// 2. Extract encryptable values
|
|
54
|
+
const encryptables = extractEncryptableValues(abi, 'add', args);
|
|
55
|
+
// ^? [Encryptable.uint32(200n)]
|
|
56
|
+
|
|
57
|
+
// 3. Encrypt the values
|
|
58
|
+
const encrypted = await encrypt(encryptables);
|
|
59
|
+
// ^? [EncryptedUint32Input]
|
|
60
|
+
|
|
61
|
+
// 4. Insert encrypted values back into arguments
|
|
62
|
+
const encryptedArgs: CofheInputArgs<typeof abi, 'add'> = insertEncryptedValues(abi, 'add', args, encrypted);
|
|
63
|
+
// ^? [100n, EncryptedUint32Input]
|
|
64
|
+
|
|
65
|
+
// 5. Call contract and transform return value
|
|
66
|
+
const result = await contract.add(...encryptedArgs);
|
|
67
|
+
// ^? bigint
|
|
68
|
+
|
|
69
|
+
// 6. Transform raw return value into correct encrypted value type
|
|
70
|
+
const transformed: CofheReturnType<typeof abi, 'add'> = transformEncryptedReturnTypes(abi, 'add', result);
|
|
71
|
+
// ^? Euint32
|
|
72
|
+
```
|
|
73
|
+
|
|
74
|
+
## Public API Reference
|
|
75
|
+
|
|
76
|
+
### Return Types
|
|
77
|
+
|
|
78
|
+
#### `CofheReturnType<abi, functionName, args?>`
|
|
79
|
+
|
|
80
|
+
Type-level utility that infers the return type of a function, transforming encrypted return types to their typed representations.
|
|
81
|
+
|
|
82
|
+
**Type Parameters:**
|
|
83
|
+
|
|
84
|
+
- `abi` - Contract ABI (must be `const`-asserted for type inference)
|
|
85
|
+
- `functionName` - Function name (string literal)
|
|
86
|
+
- `args` - Optional function arguments for overload disambiguation
|
|
87
|
+
|
|
88
|
+
**Returns:**
|
|
89
|
+
|
|
90
|
+
- Transformed return type where encrypted types (`euint32`, `ebool`, etc.) are converted to typed objects (`{ ctHash: bigint, utype: FheTypes }`)
|
|
91
|
+
- Non-encrypted types remain unchanged
|
|
92
|
+
- Supports single values, tuples, arrays, and nested structures
|
|
93
|
+
|
|
94
|
+
**Supported Encrypted Return Types:**
|
|
95
|
+
|
|
96
|
+
- `ebool` → `{ ctHash: bigint; utype: FheTypes.Bool }`
|
|
97
|
+
- `euint8` → `{ ctHash: bigint; utype: FheTypes.Uint8 }`
|
|
98
|
+
- `euint16` → `{ ctHash: bigint; utype: FheTypes.Uint16 }`
|
|
99
|
+
- `euint32` → `{ ctHash: bigint; utype: FheTypes.Uint32 }`
|
|
100
|
+
- `euint64` → `{ ctHash: bigint; utype: FheTypes.Uint64 }`
|
|
101
|
+
- `euint128` → `{ ctHash: bigint; utype: FheTypes.Uint128 }`
|
|
102
|
+
- `eaddress` → `{ ctHash: bigint; utype: FheTypes.Uint160 }`
|
|
103
|
+
|
|
104
|
+
#### `transformEncryptedReturnTypes(abi, functionName, data)`
|
|
105
|
+
|
|
106
|
+
Runtime function that transforms contract return values from `bigint` ciphertext hashes to typed encrypted return objects.
|
|
107
|
+
|
|
108
|
+
**Parameters:**
|
|
109
|
+
|
|
110
|
+
- `abi` - Contract ABI
|
|
111
|
+
- `functionName` - Function name
|
|
112
|
+
- `data` - Raw return value(s) from contract call (single value or array)
|
|
113
|
+
|
|
114
|
+
**Returns:**
|
|
115
|
+
Transformed return value matching `CofheReturnType<abi, functionName>`
|
|
116
|
+
Works with multiple return values, nested structures and arrays.
|
|
117
|
+
|
|
118
|
+
### Encrypted Inputs
|
|
119
|
+
|
|
120
|
+
#### `CofheInputArgs<abi, functionName>`
|
|
121
|
+
|
|
122
|
+
Type-level utility that infers function input arguments with encrypted types represented as encrypted input structs.
|
|
123
|
+
|
|
124
|
+
**Type Parameters:**
|
|
125
|
+
|
|
126
|
+
- `abi` - Contract ABI (must be `const`-asserted for type inference)
|
|
127
|
+
- `functionName` - Function name (string literal)
|
|
128
|
+
|
|
129
|
+
**Returns:**
|
|
130
|
+
|
|
131
|
+
- Tuple type where encrypted inputs are represented as encrypted input structs (`EncryptedUint32Input`, `EncryptedBoolInput`, etc.)
|
|
132
|
+
- Non-encrypted types use their primitive representations
|
|
133
|
+
|
|
134
|
+
#### `CofheInputArgsPreTransform<abi, functionName>`
|
|
135
|
+
|
|
136
|
+
Type-level utility that infers function input arguments with encrypted types represented as their underlying primitive values (before encryption).
|
|
137
|
+
|
|
138
|
+
**Type Parameters:**
|
|
139
|
+
|
|
140
|
+
- `abi` - Contract ABI (must be `const`-asserted for type inference)
|
|
141
|
+
- `functionName` - Function name (string literal)
|
|
142
|
+
|
|
143
|
+
**Returns:**
|
|
144
|
+
|
|
145
|
+
- Tuple type where encrypted inputs use primitive types (`bigint` for uints, `boolean` for bool, `string` for address)
|
|
146
|
+
- This is the format you provide to `extractEncryptableValues`
|
|
147
|
+
|
|
148
|
+
**Supported Encrypted Input Types:**
|
|
149
|
+
|
|
150
|
+
- `struct InEbool` → `boolean` (pre-transform) → `EncryptedBoolInput` (post-transform)
|
|
151
|
+
- `struct InEuint8` → `bigint | string` → `EncryptedUint8Input`
|
|
152
|
+
- `struct InEuint16` → `bigint | string` → `EncryptedUint16Input`
|
|
153
|
+
- `struct InEuint32` → `bigint | string` → `EncryptedUint32Input`
|
|
154
|
+
- `struct InEuint64` → `bigint | string` → `EncryptedUint64Input`
|
|
155
|
+
- `struct InEuint128` → `bigint | string` → `EncryptedUint128Input`
|
|
156
|
+
- `struct InEaddress` → `string | bigint` → `EncryptedAddressInput`
|
|
157
|
+
|
|
158
|
+
#### `extractEncryptableValues(abi, functionName, args)`
|
|
159
|
+
|
|
160
|
+
Extracts encryptable values from function arguments, converting primitive values to `EncryptableItem` objects ready for encryption.
|
|
161
|
+
|
|
162
|
+
**Parameters:**
|
|
163
|
+
|
|
164
|
+
- `abi` - Contract ABI
|
|
165
|
+
- `functionName` - Function name
|
|
166
|
+
- `args` - Function arguments in `CofheInputArgsPreTransform` format (primitives)
|
|
167
|
+
|
|
168
|
+
**Returns:**
|
|
169
|
+
Flat array of `EncryptableItem` objects in the order they appear in the ABI (depth-first traversal).
|
|
170
|
+
Works with arrays (fixed length or unbounded) and nested structures.
|
|
171
|
+
|
|
172
|
+
#### `insertEncryptedValues(abi, functionName, args, encryptedValues)`
|
|
173
|
+
|
|
174
|
+
Re-inserts encrypted values back into function arguments, replacing primitive values with encrypted input structs.
|
|
175
|
+
|
|
176
|
+
**Parameters:**
|
|
177
|
+
|
|
178
|
+
- `abi` - Contract ABI
|
|
179
|
+
- `functionName` - Function name
|
|
180
|
+
- `args` - Original function arguments in `CofheInputArgsPreTransform` format
|
|
181
|
+
- `encryptedValues` - Array of encrypted values in the same order as returned by `extractEncryptableValues`
|
|
182
|
+
|
|
183
|
+
**Returns:**
|
|
184
|
+
Function arguments in `CofheInputArgs` format (ready for contract calls)
|
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,331 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
|
|
20
|
+
// src/index.ts
|
|
21
|
+
var index_exports = {};
|
|
22
|
+
__export(index_exports, {
|
|
23
|
+
extractEncryptableValues: () => extractEncryptableValues,
|
|
24
|
+
insertEncryptedValues: () => insertEncryptedValues,
|
|
25
|
+
transformEncryptedReturnTypes: () => transformEncryptedReturnTypes
|
|
26
|
+
});
|
|
27
|
+
module.exports = __toCommonJS(index_exports);
|
|
28
|
+
|
|
29
|
+
// src/encryptedInputs.ts
|
|
30
|
+
var import_sdk = require("@cofhe/sdk");
|
|
31
|
+
|
|
32
|
+
// src/utils.ts
|
|
33
|
+
function extractArrayParameterType(type) {
|
|
34
|
+
if (type == null) return [type, void 0];
|
|
35
|
+
const match = type.match(/^(.+)\[(\d*)\]$/);
|
|
36
|
+
if (!match) {
|
|
37
|
+
return [type, void 0];
|
|
38
|
+
}
|
|
39
|
+
const head = match[1];
|
|
40
|
+
const size = match[2];
|
|
41
|
+
return [head, size === "" ? "" : size];
|
|
42
|
+
}
|
|
43
|
+
function isAbiFunction(item) {
|
|
44
|
+
if (typeof item !== "object" || item === null) return false;
|
|
45
|
+
return "type" in item && item.type === "function" && "name" in item && "outputs" in item;
|
|
46
|
+
}
|
|
47
|
+
function getAbiFunction(abi, functionName) {
|
|
48
|
+
return abi.find((item) => {
|
|
49
|
+
const isFunction = isAbiFunction(item);
|
|
50
|
+
if (!isFunction) return false;
|
|
51
|
+
return item.name === functionName;
|
|
52
|
+
});
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
// src/encryptedInputs.ts
|
|
56
|
+
var ENCRYPTED_INPUT_INTERNAL_TYPES = [
|
|
57
|
+
"struct InEbool",
|
|
58
|
+
"struct InEuint8",
|
|
59
|
+
"struct InEuint16",
|
|
60
|
+
"struct InEuint32",
|
|
61
|
+
"struct InEuint64",
|
|
62
|
+
"struct InEuint128",
|
|
63
|
+
"struct InEaddress"
|
|
64
|
+
];
|
|
65
|
+
function transformSingleEncryptedInputToEncryptable(internalType, data) {
|
|
66
|
+
switch (internalType) {
|
|
67
|
+
case "struct InEbool":
|
|
68
|
+
return import_sdk.Encryptable.bool(data);
|
|
69
|
+
case "struct InEuint8":
|
|
70
|
+
return import_sdk.Encryptable.uint8(data);
|
|
71
|
+
case "struct InEuint16":
|
|
72
|
+
return import_sdk.Encryptable.uint16(data);
|
|
73
|
+
case "struct InEuint32":
|
|
74
|
+
return import_sdk.Encryptable.uint32(data);
|
|
75
|
+
case "struct InEuint64":
|
|
76
|
+
return import_sdk.Encryptable.uint64(data);
|
|
77
|
+
case "struct InEuint128":
|
|
78
|
+
return import_sdk.Encryptable.uint128(data);
|
|
79
|
+
case "struct InEaddress":
|
|
80
|
+
return import_sdk.Encryptable.address(data);
|
|
81
|
+
default:
|
|
82
|
+
throw new Error(`Unknown encrypted input type: ${internalType}`);
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
function transformArrayOfEncryptedInputsToEncryptables(internalType, size, data) {
|
|
86
|
+
if (!Array.isArray(data)) {
|
|
87
|
+
throw new Error("Data must be an array");
|
|
88
|
+
}
|
|
89
|
+
if (size != null && size !== "" && parseInt(size) !== data.length) {
|
|
90
|
+
throw new Error(`Array size mismatch: ${size} !== ${data.length}`);
|
|
91
|
+
}
|
|
92
|
+
return data.map((item) => transformSingleEncryptedInputToEncryptable(internalType, item));
|
|
93
|
+
}
|
|
94
|
+
function internalTypeIsEncryptedInput(internalType) {
|
|
95
|
+
return ENCRYPTED_INPUT_INTERNAL_TYPES.includes(internalType);
|
|
96
|
+
}
|
|
97
|
+
function extractEncryptableValues(abi, functionName, args) {
|
|
98
|
+
const abiFunction = getAbiFunction(abi, functionName);
|
|
99
|
+
const inputs = abiFunction?.inputs;
|
|
100
|
+
if (abiFunction == null || inputs == null) {
|
|
101
|
+
throw new Error(`Function ${functionName} not found in ABI`);
|
|
102
|
+
}
|
|
103
|
+
if (!Array.isArray(args)) {
|
|
104
|
+
throw new Error("Arguments must be an array");
|
|
105
|
+
}
|
|
106
|
+
const encryptableItems = [];
|
|
107
|
+
function processParameter(param, value) {
|
|
108
|
+
const [typeHead, typeSize] = extractArrayParameterType(param.type);
|
|
109
|
+
const [internalTypeHead, internalTypeSize] = extractArrayParameterType(param.internalType);
|
|
110
|
+
if (typeHead === "tuple" && typeSize == null && internalTypeHead != null && internalTypeIsEncryptedInput(internalTypeHead)) {
|
|
111
|
+
const encryptable = transformSingleEncryptedInputToEncryptable(internalTypeHead, value);
|
|
112
|
+
encryptableItems.push(encryptable);
|
|
113
|
+
return;
|
|
114
|
+
}
|
|
115
|
+
if (typeHead === "tuple" && typeSize != null && internalTypeHead != null && internalTypeIsEncryptedInput(internalTypeHead)) {
|
|
116
|
+
const encryptables = transformArrayOfEncryptedInputsToEncryptables(internalTypeHead, typeSize, value);
|
|
117
|
+
encryptableItems.push(...encryptables);
|
|
118
|
+
return;
|
|
119
|
+
}
|
|
120
|
+
if (typeHead === "tuple" && (internalTypeHead == null || !internalTypeIsEncryptedInput(internalTypeHead))) {
|
|
121
|
+
if ("components" in param && Array.isArray(param.components)) {
|
|
122
|
+
param.components.forEach((component) => {
|
|
123
|
+
processParameter(component, value[component.name]);
|
|
124
|
+
});
|
|
125
|
+
}
|
|
126
|
+
return;
|
|
127
|
+
}
|
|
128
|
+
return;
|
|
129
|
+
}
|
|
130
|
+
inputs.forEach((input, index) => {
|
|
131
|
+
const arg = args[index];
|
|
132
|
+
if (arg == null) {
|
|
133
|
+
throw new Error(`Argument ${index} is undefined`);
|
|
134
|
+
}
|
|
135
|
+
processParameter(input, arg);
|
|
136
|
+
});
|
|
137
|
+
return encryptableItems;
|
|
138
|
+
}
|
|
139
|
+
function insertEncryptedValues(abi, functionName, args, encryptedValues) {
|
|
140
|
+
const abiFunction = getAbiFunction(abi, functionName);
|
|
141
|
+
const inputs = abiFunction?.inputs;
|
|
142
|
+
if (abiFunction == null || inputs == null) {
|
|
143
|
+
throw new Error(`Function ${functionName} not found in ABI`);
|
|
144
|
+
}
|
|
145
|
+
if (!Array.isArray(args)) {
|
|
146
|
+
throw new Error("Arguments must be an array");
|
|
147
|
+
}
|
|
148
|
+
let encryptedIndex = 0;
|
|
149
|
+
function processParameter(param, value) {
|
|
150
|
+
const [typeHead, typeSize] = extractArrayParameterType(param.type);
|
|
151
|
+
const [internalTypeHead, internalTypeSize] = extractArrayParameterType(param.internalType);
|
|
152
|
+
if (typeHead === "tuple" && typeSize == null && internalTypeHead != null && internalTypeIsEncryptedInput(internalTypeHead)) {
|
|
153
|
+
if (encryptedIndex >= encryptedValues.length) {
|
|
154
|
+
throw new Error(
|
|
155
|
+
`Not enough encrypted values: expected at least ${encryptedIndex + 1}, got ${encryptedValues.length}`
|
|
156
|
+
);
|
|
157
|
+
}
|
|
158
|
+
const encryptedValue = encryptedValues[encryptedIndex];
|
|
159
|
+
encryptedIndex++;
|
|
160
|
+
return encryptedValue;
|
|
161
|
+
}
|
|
162
|
+
if (typeHead === "tuple" && typeSize != null && internalTypeHead != null && internalTypeIsEncryptedInput(internalTypeHead)) {
|
|
163
|
+
const arrayLength = Array.isArray(value) ? value.length : 0;
|
|
164
|
+
if (arrayLength === 0) {
|
|
165
|
+
return [];
|
|
166
|
+
}
|
|
167
|
+
if (encryptedIndex + arrayLength > encryptedValues.length) {
|
|
168
|
+
throw new Error(
|
|
169
|
+
`Not enough encrypted values: expected at least ${encryptedIndex + arrayLength}, got ${encryptedValues.length}`
|
|
170
|
+
);
|
|
171
|
+
}
|
|
172
|
+
const encryptedArray = encryptedValues.slice(encryptedIndex, encryptedIndex + arrayLength);
|
|
173
|
+
encryptedIndex += arrayLength;
|
|
174
|
+
return encryptedArray;
|
|
175
|
+
}
|
|
176
|
+
if (typeHead === "tuple" && (internalTypeHead == null || !internalTypeIsEncryptedInput(internalTypeHead))) {
|
|
177
|
+
if ("components" in param && Array.isArray(param.components)) {
|
|
178
|
+
const valueObj = value;
|
|
179
|
+
const result2 = {};
|
|
180
|
+
param.components.forEach((component) => {
|
|
181
|
+
const componentName = component.name;
|
|
182
|
+
if (componentName) {
|
|
183
|
+
const componentValue = valueObj[componentName];
|
|
184
|
+
if (componentValue !== void 0) {
|
|
185
|
+
result2[componentName] = processParameter(component, componentValue);
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
});
|
|
189
|
+
return result2;
|
|
190
|
+
}
|
|
191
|
+
return value;
|
|
192
|
+
}
|
|
193
|
+
return value;
|
|
194
|
+
}
|
|
195
|
+
const result = inputs.map((input, index) => {
|
|
196
|
+
const arg = args[index];
|
|
197
|
+
if (arg == null) {
|
|
198
|
+
throw new Error(`Argument ${index} is undefined`);
|
|
199
|
+
}
|
|
200
|
+
return processParameter(input, arg);
|
|
201
|
+
});
|
|
202
|
+
if (encryptedIndex !== encryptedValues.length) {
|
|
203
|
+
throw new Error(
|
|
204
|
+
`Mismatch in encrypted values count: used ${encryptedIndex}, but provided ${encryptedValues.length}`
|
|
205
|
+
);
|
|
206
|
+
}
|
|
207
|
+
return result;
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
// src/encryptedReturnTypes.ts
|
|
211
|
+
var import_sdk2 = require("@cofhe/sdk");
|
|
212
|
+
var ENCRYPTED_RETURN_TYPE_INTERNAL_TYPES = [
|
|
213
|
+
"ebool",
|
|
214
|
+
"euint8",
|
|
215
|
+
"euint16",
|
|
216
|
+
"euint32",
|
|
217
|
+
"euint64",
|
|
218
|
+
"euint128",
|
|
219
|
+
"eaddress"
|
|
220
|
+
];
|
|
221
|
+
function transformSingleEncryptedReturnTypeToReturnType(internalType, data) {
|
|
222
|
+
switch (internalType) {
|
|
223
|
+
case "ebool":
|
|
224
|
+
return {
|
|
225
|
+
ctHash: data,
|
|
226
|
+
utype: import_sdk2.FheTypes.Bool
|
|
227
|
+
};
|
|
228
|
+
case "euint8":
|
|
229
|
+
return {
|
|
230
|
+
ctHash: data,
|
|
231
|
+
utype: import_sdk2.FheTypes.Uint8
|
|
232
|
+
};
|
|
233
|
+
case "euint16":
|
|
234
|
+
return {
|
|
235
|
+
ctHash: data,
|
|
236
|
+
utype: import_sdk2.FheTypes.Uint16
|
|
237
|
+
};
|
|
238
|
+
case "euint32":
|
|
239
|
+
return {
|
|
240
|
+
ctHash: data,
|
|
241
|
+
utype: import_sdk2.FheTypes.Uint32
|
|
242
|
+
};
|
|
243
|
+
case "euint64":
|
|
244
|
+
return {
|
|
245
|
+
ctHash: data,
|
|
246
|
+
utype: import_sdk2.FheTypes.Uint64
|
|
247
|
+
};
|
|
248
|
+
case "euint128":
|
|
249
|
+
return {
|
|
250
|
+
ctHash: data,
|
|
251
|
+
utype: import_sdk2.FheTypes.Uint128
|
|
252
|
+
};
|
|
253
|
+
case "eaddress":
|
|
254
|
+
return {
|
|
255
|
+
ctHash: data,
|
|
256
|
+
utype: import_sdk2.FheTypes.Uint160
|
|
257
|
+
};
|
|
258
|
+
default:
|
|
259
|
+
throw new Error(`Unknown encrypted return type: ${internalType}`);
|
|
260
|
+
}
|
|
261
|
+
}
|
|
262
|
+
function transformArrayOfEncryptedReturnTypesToReturnTypes(internalType, size, data) {
|
|
263
|
+
if (!Array.isArray(data)) {
|
|
264
|
+
throw new Error("Data must be an array");
|
|
265
|
+
}
|
|
266
|
+
if (size != null && size !== "" && parseInt(size) !== data.length) {
|
|
267
|
+
throw new Error(`Array size mismatch: ${size} !== ${data.length}`);
|
|
268
|
+
}
|
|
269
|
+
return data.map((item) => transformSingleEncryptedReturnTypeToReturnType(internalType, item));
|
|
270
|
+
}
|
|
271
|
+
function internalTypeIsEncryptedReturnType(internalType) {
|
|
272
|
+
return ENCRYPTED_RETURN_TYPE_INTERNAL_TYPES.includes(internalType);
|
|
273
|
+
}
|
|
274
|
+
function transformEncryptedReturnTypes(abi, functionName, data) {
|
|
275
|
+
const abiFunction = getAbiFunction(abi, functionName);
|
|
276
|
+
const outputs = abiFunction?.outputs;
|
|
277
|
+
if (abiFunction == null || outputs == null) {
|
|
278
|
+
throw new Error(`Function ${functionName} not found in ABI`);
|
|
279
|
+
}
|
|
280
|
+
const outputsLength = outputs.length;
|
|
281
|
+
function processParameter(param, value) {
|
|
282
|
+
const [typeHead, typeSize] = extractArrayParameterType(param.type);
|
|
283
|
+
const [internalTypeHead, internalTypeSize] = extractArrayParameterType(param.internalType);
|
|
284
|
+
if (internalTypeHead != null && internalTypeIsEncryptedReturnType(internalTypeHead)) {
|
|
285
|
+
if (internalTypeSize == null) {
|
|
286
|
+
return transformSingleEncryptedReturnTypeToReturnType(internalTypeHead, value);
|
|
287
|
+
}
|
|
288
|
+
if (internalTypeSize != null) {
|
|
289
|
+
console.log("array of encrypted return types", internalTypeHead, internalTypeSize, value);
|
|
290
|
+
return transformArrayOfEncryptedReturnTypesToReturnTypes(internalTypeHead, internalTypeSize, value);
|
|
291
|
+
}
|
|
292
|
+
}
|
|
293
|
+
if (typeHead === "tuple") {
|
|
294
|
+
if ("components" in param && Array.isArray(param.components)) {
|
|
295
|
+
const valueObj = value;
|
|
296
|
+
const result = {};
|
|
297
|
+
param.components.forEach((component) => {
|
|
298
|
+
const componentName = component.name;
|
|
299
|
+
if (componentName) {
|
|
300
|
+
const componentValue = valueObj[componentName];
|
|
301
|
+
if (componentValue !== void 0) {
|
|
302
|
+
result[componentName] = processParameter(component, componentValue);
|
|
303
|
+
}
|
|
304
|
+
}
|
|
305
|
+
});
|
|
306
|
+
return result;
|
|
307
|
+
}
|
|
308
|
+
return value;
|
|
309
|
+
}
|
|
310
|
+
return value;
|
|
311
|
+
}
|
|
312
|
+
if (outputsLength > 1) {
|
|
313
|
+
if (!Array.isArray(data)) {
|
|
314
|
+
throw new Error("Data must be an array");
|
|
315
|
+
}
|
|
316
|
+
if (outputsLength !== data.length) {
|
|
317
|
+
throw new Error(`Mismatch in outputs length: ${outputsLength} !== ${data.length}`);
|
|
318
|
+
}
|
|
319
|
+
return data.map((item, index) => {
|
|
320
|
+
return processParameter(outputs[index], item);
|
|
321
|
+
});
|
|
322
|
+
}
|
|
323
|
+
return processParameter(outputs[0], data);
|
|
324
|
+
}
|
|
325
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
326
|
+
0 && (module.exports = {
|
|
327
|
+
extractEncryptableValues,
|
|
328
|
+
insertEncryptedValues,
|
|
329
|
+
transformEncryptedReturnTypes
|
|
330
|
+
});
|
|
331
|
+
//# sourceMappingURL=index.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/index.ts","../src/encryptedInputs.ts","../src/utils.ts","../src/encryptedReturnTypes.ts"],"sourcesContent":["export * from './encryptedInputs';\nexport * from './encryptedReturnTypes';\nexport * from './fhenixMap';\nexport type { Abi } from 'abitype';\nexport type { ContractReturnType } from './utils';\nexport { transformEncryptedReturnTypes } from './encryptedReturnTypes';\n","import {\n Encryptable,\n type EncryptableAddress,\n type EncryptableBool,\n type EncryptableItem,\n type EncryptableUint128,\n type EncryptableUint16,\n type EncryptableUint32,\n type EncryptableUint64,\n type EncryptableUint8,\n type EncryptedAddressInput,\n type EncryptedBoolInput,\n type EncryptedItemInput,\n type EncryptedUint128Input,\n type EncryptedUint16Input,\n type EncryptedUint32Input,\n type EncryptedUint64Input,\n type EncryptedUint8Input,\n type LiteralToPrimitive,\n type Primitive,\n} from '@cofhe/sdk';\nimport type { Abi, AbiFunction, AbiParameter, ExtractAbiFunction } from 'abitype';\nimport type { CofheAbiParametersToPrimitiveTypes } from './fhenixMap';\nimport { extractArrayParameterType, getAbiFunction, type MaybePartialBy, type ReadonlyWiden } from './utils';\n\nexport type CofheInputArgs<abi extends Abi | readonly unknown[] = Abi, functionName extends string = string> = GetArgs<\n abi,\n functionName\n>['args'];\n\ntype EncryptedInputToInputMap<E extends EncryptedItemInput> = E extends EncryptedBoolInput\n ? EncryptableBool['data']\n : E extends EncryptedUint8Input\n ? EncryptableUint8['data']\n : E extends EncryptedUint16Input\n ? EncryptableUint16['data']\n : E extends EncryptedUint32Input\n ? EncryptableUint32['data']\n : E extends EncryptedUint64Input\n ? EncryptableUint64['data']\n : E extends EncryptedUint128Input\n ? EncryptableUint128['data']\n : E extends EncryptedAddressInput\n ? EncryptableAddress['data']\n : never;\n\ntype EncryptedInputsToInputs<T> = T extends Primitive\n ? LiteralToPrimitive<T>\n : T extends EncryptedItemInput\n ? EncryptedInputToInputMap<T>\n : {\n [K in keyof T]: EncryptedInputsToInputs<T[K]>;\n };\n\nexport type CofheInputArgsPreTransform<\n abi extends Abi | readonly unknown[] = Abi,\n functionName extends string = string,\n> = EncryptedInputsToInputs<CofheInputArgs<abi, functionName>>;\n\n/// GetArgs from abitype (not exported from abitype)\ntype GetArgs<\n abi extends Abi | readonly unknown[] = Abi, // `readonly unknown[]` allows for non-const asserted types\n functionName extends string = string,\n args extends readonly unknown[] | undefined = readonly [],\n ///\n abiFunction extends AbiFunction = abi extends Abi ? ExtractAbiFunction<abi, functionName> : AbiFunction,\n primitiveTypes = CofheAbiParametersToPrimitiveTypes<abiFunction['inputs']>,\n args_ =\n | primitiveTypes // show all values\n | (abi extends Abi\n ? args extends primitiveTypes // infer value (if valid)\n ? primitiveTypes extends args // make sure `args` exactly matches `primitiveTypes` (e.g. avoid `args: readonly [{ foo: string; bar: number; }] | readonly [{ foo: string; }]`)\n ? // make inferred value of `args` match `primitiveTypes` (e.g. avoid union `args: readonly [123n] | readonly [bigint]`)\n ReadonlyWiden<args>\n : never\n : never\n : never)\n | (Abi extends abi ? readonly unknown[] : never), // fallback if `abi` is declared as `Abi`\n> = MaybePartialBy<{ args: args_ }, readonly [] extends primitiveTypes ? 'args' : Abi extends abi ? 'args' : string>;\n\n// Runtime helper to check if an internalType is an encrypted input type\nconst ENCRYPTED_INPUT_INTERNAL_TYPES = [\n 'struct InEbool',\n 'struct InEuint8',\n 'struct InEuint16',\n 'struct InEuint32',\n 'struct InEuint64',\n 'struct InEuint128',\n 'struct InEaddress',\n] as const;\ntype EncryptedInputInternalType = (typeof ENCRYPTED_INPUT_INTERNAL_TYPES)[number];\n\nfunction transformSingleEncryptedInputToEncryptable(\n internalType: EncryptedInputInternalType,\n data: unknown\n): EncryptableItem {\n switch (internalType) {\n case 'struct InEbool':\n return Encryptable.bool(data as boolean);\n case 'struct InEuint8':\n return Encryptable.uint8(data as string | bigint);\n case 'struct InEuint16':\n return Encryptable.uint16(data as string | bigint);\n case 'struct InEuint32':\n return Encryptable.uint32(data as string | bigint);\n case 'struct InEuint64':\n return Encryptable.uint64(data as string | bigint);\n case 'struct InEuint128':\n return Encryptable.uint128(data as string | bigint);\n case 'struct InEaddress':\n return Encryptable.address(data as string | bigint);\n default:\n throw new Error(`Unknown encrypted input type: ${internalType}`);\n }\n}\n\nfunction transformArrayOfEncryptedInputsToEncryptables(\n internalType: EncryptedInputInternalType,\n size: string | undefined,\n data: unknown\n): EncryptableItem[] {\n // Ensure data is an array\n if (!Array.isArray(data)) {\n throw new Error('Data must be an array');\n }\n\n // Ensure length of fixed length tuple matches the length of the data array\n if (size != null && size !== '' && parseInt(size) !== data.length) {\n throw new Error(`Array size mismatch: ${size} !== ${data.length}`);\n }\n\n // Transform each item in the data array into an encryptable item\n return data.map((item) => transformSingleEncryptedInputToEncryptable(internalType, item));\n}\n\n// Pathways\n// - type is a tuple && internalType is exactly an encrypted input type (struct InEbool)\n// - - extract the base encrypted type (InEbool) and convert into an encryptable item\n// - type is a tuple && internalType is a tuple of encrypted input types (struct InEuint32[2] / struct InEuint32[])\n// - - extract the base encrypted type and length, and convert into an array of encryptable items\n// - type is a tuple && internalType is not an encrypted input type\n// - - iterate components\n// - type is not a tuple\n// - - throw away\n\nfunction internalTypeIsEncryptedInput(internalType: string): internalType is EncryptedInputInternalType {\n return ENCRYPTED_INPUT_INTERNAL_TYPES.includes(internalType as any);\n}\n\n/**\n * Extracts encryptable values from function arguments based on the ABI.\n * Transforms raw data values into EncryptableItem objects that can be passed to the encrypt function.\n *\n * @param abi - The ABI containing the function definition\n * @param functionName - Name of the function\n * @param args - Function arguments in the format of CofheInputArgsPreTransform (raw data values)\n * @returns Array of EncryptableItem objects ready for encryption\n */\nexport function extractEncryptableValues<TAbi extends Abi, TFunctionName extends string>(\n abi: TAbi,\n functionName: TFunctionName,\n args: CofheInputArgsPreTransform<TAbi, TFunctionName>\n): EncryptableItem[] {\n const abiFunction = getAbiFunction(abi, functionName);\n const inputs = abiFunction?.inputs;\n if (abiFunction == null || inputs == null) {\n throw new Error(`Function ${functionName} not found in ABI`);\n }\n\n if (!Array.isArray(args)) {\n throw new Error('Arguments must be an array');\n }\n\n // Collect encrypted values as EncryptableItem objects in order (flat array)\n const encryptableItems: EncryptableItem[] = [];\n\n // Process each parameter - transforms raw values into EncryptableItem objects\n function processParameter(param: AbiParameter, value: unknown): void {\n const [typeHead, typeSize] = extractArrayParameterType(param.type);\n const [internalTypeHead, internalTypeSize] = extractArrayParameterType(param.internalType);\n\n /** \n {\n name: 'inNumber',\n type: 'tuple',\n internalType: 'struct InEuint32',\n components: [\n {\n name: 'ctHash',\n type: 'uint256',\n internalType: 'uint256',\n },\n {\n name: 'securityZone',\n type: 'uint8',\n internalType: 'uint8',\n },\n {\n name: 'utype',\n type: 'uint8',\n internalType: 'uint8',\n },\n {\n name: 'signature',\n type: 'bytes',\n internalType: 'bytes',\n },\n ],\n }\n */\n if (\n typeHead === 'tuple' &&\n typeSize == null &&\n internalTypeHead != null &&\n internalTypeIsEncryptedInput(internalTypeHead)\n ) {\n const encryptable = transformSingleEncryptedInputToEncryptable(internalTypeHead, value);\n encryptableItems.push(encryptable);\n return;\n }\n\n /**\n {\n name: 'inEuint32Array',\n type: 'tuple[2]',\n internalType: 'struct InEuint32[2]',\n components: [\n {\n name: 'ctHash',\n type: 'uint256',\n internalType: 'uint256',\n },\n {\n name: 'securityZone',\n type: 'uint8',\n internalType: 'uint8',\n },\n {\n name: 'utype',\n type: 'uint8',\n internalType: 'uint8',\n },\n {\n name: 'signature',\n type: 'bytes',\n internalType: 'bytes',\n },\n ],\n }\n */\n if (\n typeHead === 'tuple' &&\n typeSize != null &&\n internalTypeHead != null &&\n internalTypeIsEncryptedInput(internalTypeHead)\n ) {\n const encryptables = transformArrayOfEncryptedInputsToEncryptables(internalTypeHead, typeSize, value);\n encryptableItems.push(...encryptables);\n return;\n }\n\n /**\n {\n name: 'containsEncryptedInput',\n type: 'tuple',\n internalType: 'struct ABITest.ContainsEncryptedInput',\n components: [\n {\n name: 'value',\n type: 'uint256',\n internalType: 'uint256',\n },\n {\n name: 'encryptedInput',\n type: 'tuple',\n internalType: 'struct InEuint32',\n components: [\n {\n name: 'ctHash',\n type: 'uint256',\n internalType: 'uint256',\n },\n {\n name: 'securityZone',\n type: 'uint8',\n internalType: 'uint8',\n },\n {\n name: 'utype',\n type: 'uint8',\n internalType: 'uint8',\n },\n {\n name: 'signature',\n type: 'bytes',\n internalType: 'bytes',\n },\n ],\n },\n ],\n }\n */\n if (typeHead === 'tuple' && (internalTypeHead == null || !internalTypeIsEncryptedInput(internalTypeHead))) {\n if ('components' in param && Array.isArray(param.components)) {\n param.components.forEach((component) => {\n processParameter(component, (value as Record<string, unknown>)[component.name]);\n });\n }\n return;\n }\n\n // param.type is not a tuple, so cannot be an encrypted input or contain encrypted inputs\n return;\n }\n\n // Process all inputs\n inputs.forEach((input, index) => {\n const arg = args[index];\n if (arg == null) {\n throw new Error(`Argument ${index} is undefined`);\n }\n processParameter(input, arg);\n });\n\n return encryptableItems;\n}\n\n/**\n * Re-inserts encrypted values back into function arguments based on the ABI.\n * Takes the extracted values (now encrypted) and the original args structure, and replaces\n * the encryptable values with their encrypted counterparts.\n *\n * This function mirrors the extraction logic in extractEncryptableValues, ensuring values\n * are inserted in the exact same order and locations where they were extracted.\n *\n * @param abi - The ABI containing the function definition\n * @param functionName - Name of the function\n * @param args - Original function arguments in the format of CofheInputArgsPreTransform\n * @param encryptedValues - Encrypted values in the same order as returned by extractEncryptableValues\n * @returns Function arguments with encrypted values inserted (format of CofheInputArgs)\n */\nexport function insertEncryptedValues<TAbi extends Abi, TFunctionName extends string>(\n abi: TAbi,\n functionName: TFunctionName,\n args: CofheInputArgsPreTransform<TAbi, TFunctionName>,\n encryptedValues: readonly EncryptedItemInput[]\n): CofheInputArgs<TAbi, TFunctionName> {\n const abiFunction = getAbiFunction(abi, functionName);\n const inputs = abiFunction?.inputs;\n if (abiFunction == null || inputs == null) {\n throw new Error(`Function ${functionName} not found in ABI`);\n }\n\n if (!Array.isArray(args)) {\n throw new Error('Arguments must be an array');\n }\n\n // Track position in encrypted values array\n let encryptedIndex = 0;\n\n // Process each parameter - replaces raw values with encrypted values\n function processParameter(param: AbiParameter, value: unknown): unknown {\n const [typeHead, typeSize] = extractArrayParameterType(param.type);\n const [internalTypeHead, internalTypeSize] = extractArrayParameterType(param.internalType);\n\n // Single encrypted input (tuple with encrypted input type)\n if (\n typeHead === 'tuple' &&\n typeSize == null &&\n internalTypeHead != null &&\n internalTypeIsEncryptedInput(internalTypeHead)\n ) {\n if (encryptedIndex >= encryptedValues.length) {\n throw new Error(\n `Not enough encrypted values: expected at least ${encryptedIndex + 1}, got ${encryptedValues.length}`\n );\n }\n const encryptedValue = encryptedValues[encryptedIndex];\n encryptedIndex++;\n return encryptedValue;\n }\n\n // Array of encrypted inputs (tuple[2] or tuple[] with encrypted input type)\n if (\n typeHead === 'tuple' &&\n typeSize != null &&\n internalTypeHead != null &&\n internalTypeIsEncryptedInput(internalTypeHead)\n ) {\n // Determine how many encrypted values we need\n const arrayLength = Array.isArray(value) ? value.length : 0;\n if (arrayLength === 0) {\n return [];\n }\n\n if (encryptedIndex + arrayLength > encryptedValues.length) {\n throw new Error(\n `Not enough encrypted values: expected at least ${encryptedIndex + arrayLength}, got ${encryptedValues.length}`\n );\n }\n\n // Extract the required number of encrypted values\n const encryptedArray = encryptedValues.slice(encryptedIndex, encryptedIndex + arrayLength);\n encryptedIndex += arrayLength;\n return encryptedArray;\n }\n\n // Tuple recursive case (not encrypted input)\n if (typeHead === 'tuple' && (internalTypeHead == null || !internalTypeIsEncryptedInput(internalTypeHead))) {\n if ('components' in param && Array.isArray(param.components)) {\n const valueObj = value as Record<string, unknown>;\n const result: Record<string, unknown> = {};\n\n // Process each component in order\n param.components.forEach((component) => {\n const componentName = component.name;\n if (componentName) {\n const componentValue = valueObj[componentName];\n if (componentValue !== undefined) {\n result[componentName] = processParameter(component, componentValue);\n }\n }\n });\n\n return result;\n }\n return value;\n }\n\n // Not an encrypted input, return original value\n return value;\n }\n\n // Process all inputs in order\n const result = inputs.map((input, index) => {\n const arg = args[index];\n if (arg == null) {\n throw new Error(`Argument ${index} is undefined`);\n }\n return processParameter(input, arg);\n });\n\n // Verify we used all encrypted values\n if (encryptedIndex !== encryptedValues.length) {\n throw new Error(\n `Mismatch in encrypted values count: used ${encryptedIndex}, but provided ${encryptedValues.length}`\n );\n }\n\n return result as CofheInputArgs<TAbi, TFunctionName>;\n}\n","import type {\n Abi,\n AbiConstructor,\n AbiError,\n AbiEvent,\n AbiFallback,\n AbiFunction,\n AbiParameter,\n AbiParametersToPrimitiveTypes,\n AbiReceive,\n AbiType,\n Address,\n ExtractAbiFunction,\n ResolvedRegister,\n SolidityArray,\n SolidityFixedArrayRange,\n SolidityTuple,\n} from 'abitype';\n\nexport type ContractReturnType<\n abi extends Abi | readonly unknown[] = Abi,\n functionName extends string = string,\n args extends readonly unknown[] | undefined = readonly unknown[] | undefined,\n ///\n abiFunction extends AbiFunction = (\n abi extends Abi ? ExtractAbiFunction<abi, functionName> : AbiFunction\n ) extends infer abiFunction_ extends AbiFunction\n ? IsUnion<abiFunction_> extends true // narrow overloads by `args` by converting to tuple and filtering out overloads that don't match\n ? UnionToTuple<abiFunction_> extends infer abiFunctions extends readonly AbiFunction[]\n ? {\n [K in keyof abiFunctions]: (\n readonly unknown[] | undefined extends args // for functions that don't have inputs, `args` can be `undefined` so fallback to `readonly []`\n ? readonly []\n : args\n ) extends AbiParametersToPrimitiveTypes<abiFunctions[K]['inputs'], 'inputs'>\n ? abiFunctions[K]\n : never;\n }[number] // convert back to union (removes `never` tuple entries: `['foo', never, 'bar'][number]` => `'foo' | 'bar'`)\n : never\n : abiFunction_\n : never,\n outputs extends readonly AbiParameter[] = abiFunction['outputs'],\n primitiveTypes extends readonly unknown[] = AbiParametersToPrimitiveTypes<outputs, 'outputs', true>,\n> = [abiFunction] extends [never]\n ? unknown // `abiFunction` was not inferrable (e.g. `abi` declared as `Abi`)\n : readonly unknown[] extends primitiveTypes\n ? unknown // `abiFunction` was not inferrable (e.g. `abi` not const-asserted)\n : primitiveTypes extends readonly [] // unwrap `primitiveTypes`\n ? // biome-ignore lint/suspicious/noConfusingVoidType: <explanation>\n void // no outputs\n : primitiveTypes extends readonly [infer primitiveType]\n ? primitiveType // single output\n : primitiveTypes;\n\nexport type Error<messages extends string | string[]> = messages extends string\n ? [\n // Surrounding with array to prevent `messages` from being widened to `string`\n `Error: ${messages}`,\n ]\n : {\n [key in keyof messages]: messages[key] extends infer message extends string ? `Error: ${message}` : never;\n };\n\ntype PartialBy<TType, TKeys extends keyof TType> = ExactPartial<Pick<TType, TKeys>> & Omit<TType, TKeys>;\ntype ExactPartial<T> = { [K in keyof T]?: T[K] | undefined };\n\nexport type MaybePartialBy<TType, TKeys extends string> = TKeys extends keyof TType ? PartialBy<TType, TKeys> : TType;\n\nexport type ReadonlyWiden<TType> =\n | (TType extends Function ? TType : never)\n | (TType extends ResolvedRegister['bigIntType'] ? bigint : never)\n | (TType extends boolean ? boolean : never)\n | (TType extends ResolvedRegister['intType'] ? number : never)\n | (TType extends string\n ? TType extends Address\n ? Address\n : TType extends ResolvedRegister['bytesType']['inputs']\n ? ResolvedRegister['bytesType']\n : string\n : never)\n | (TType extends readonly [] ? readonly [] : never)\n | (TType extends Record<string, unknown> ? { [K in keyof TType]: ReadonlyWiden<TType[K]> } : never)\n | (TType extends { length: number }\n ? {\n [K in keyof TType]: ReadonlyWiden<TType[K]>;\n } extends infer Val extends unknown[]\n ? readonly [...Val]\n : never\n : never);\n\nexport type AbiBasicType = Exclude<AbiType, SolidityTuple | SolidityArray>;\n\n/**\n * First, infer `Head` against a known size type (either fixed-length array value or `\"\"`).\n *\n * | Input | Head |\n * | --------------- | ------------ |\n * | `string[]` | `string` |\n * | `string[][][3]` | `string[][]` |\n */\nexport type MaybeExtractArrayParameterType<type> = type extends `${infer head}[${'' | `${SolidityFixedArrayRange}`}]`\n ? // * Then, infer in the opposite direction, using the known `head` to infer the exact `size` value.\n // *\n // * | Input | Size |\n // * | ------------ | ---- |\n // * | `${head}[]` | `\"\"` |\n // * | `${head}[3]` | `3` |\n // */\n type extends `${head}[${infer size}]`\n ? [head, size]\n : undefined\n : undefined;\n\nexport type IsUnion<T, C = T> = T extends C ? ([C] extends [T] ? false : true) : never;\nexport type UnionToTuple<U, Last = LastInUnion<U>> = [U] extends [never]\n ? []\n : [...UnionToTuple<Exclude<U, Last>>, Last];\ntype LastInUnion<U> =\n UnionToIntersection<U extends unknown ? (x: U) => 0 : never> extends (x: infer L) => 0 ? L : never;\ntype UnionToIntersection<U> = (U extends unknown ? (arg: U) => 0 : never) extends (arg: infer I) => 0 ? I : never;\n\n/**\n * Create tuple of {@link type} type with {@link size} size\n *\n * @param Type - Type of tuple\n * @param Size - Size of tuple\n * @returns Tuple of {@link type} type with {@link size} size\n *\n * @example\n * type Result = Tuple<string, 2>\n * // ^? type Result = [string, string]\n */\n// https://github.com/Microsoft/TypeScript/issues/26223#issuecomment-674500430\nexport type Tuple<type, size extends number> = size extends size\n ? number extends size\n ? type[]\n : _TupleOf<type, size, []>\n : never;\ntype _TupleOf<length, size extends number, acc extends readonly unknown[]> = acc['length'] extends size\n ? acc\n : _TupleOf<length, size, readonly [length, ...acc]>;\n\n/**\n * Merges two object types into new type\n *\n * @param object1 - Object to merge into\n * @param object2 - Object to merge and override keys from {@link object1}\n * @returns New object type with keys from {@link object1} and {@link object2}. If a key exists in both {@link object1} and {@link object2}, the key from {@link object2} will be used.\n *\n * @example\n * type Result = Merge<{ foo: string }, { foo: number; bar: string }>\n * // ^? type Result = { foo: number; bar: string }\n */\nexport type Merge<object1, object2> = Omit<object1, keyof object2> & object2;\n\n// FUNCTIONS\n\n/**\n * Extracts array parameter type information from a type string.\n * Always returns a tuple, with undefined for non-array types.\n *\n * @param type - Type string like \"tuple\", \"tuple[]\", or \"tuple[2]\"\n * @returns [head, size] where:\n * - head is the base type\n * - size is undefined for non-arrays, \"\" for dynamic arrays, or the number as a string for fixed arrays\n *\n * @example\n * extractArrayParameterType(\"tuple\") // [\"tuple\", undefined]\n * extractArrayParameterType(\"tuple[]\") // [\"tuple\", \"\"]\n * extractArrayParameterType(\"tuple[2]\") // [\"tuple\", \"2\"]\n */\nexport function extractArrayParameterType<T extends string | undefined>(type: T): [T, string | undefined] {\n if (type == null) return [type, undefined];\n\n const match = type.match(/^(.+)\\[(\\d*)\\]$/);\n\n if (!match) {\n // Not an array type, return [type, undefined]\n return [type, undefined];\n }\n\n const head = match[1];\n const size = match[2]; // Empty string for dynamic arrays, or digits for fixed arrays\n\n // Return empty string for dynamic arrays, or the size string for fixed arrays\n return [head as T, size === '' ? '' : size];\n}\n\ntype AbiItem = AbiConstructor | AbiError | AbiEvent | AbiFallback | AbiFunction | AbiReceive;\n\nexport function isAbiFunction(item: AbiItem | unknown): item is AbiFunction {\n if (typeof item !== 'object' || item === null) return false;\n return 'type' in item && item.type === 'function' && 'name' in item && 'outputs' in item;\n}\n\nexport function getAbiFunction<TAbi extends Abi | readonly unknown[], TFunctionName extends string>(\n abi: TAbi,\n functionName: TFunctionName\n): AbiFunction | undefined {\n return abi.find((item) => {\n const isFunction = isAbiFunction(item);\n if (!isFunction) return false;\n return item.name === functionName;\n }) as AbiFunction | undefined;\n}\n","import { type Primitive, type LiteralToPrimitive, FheTypes } from '@cofhe/sdk';\nimport type { Abi, AbiFunction, ExtractAbiFunction, AbiParameter } from 'abitype';\nimport type {\n EncryptedReturnType,\n CofheAbiParametersToPrimitiveTypes,\n EBool,\n EAddress,\n EUint128,\n EUint16,\n EUint32,\n EUint64,\n EUint8,\n} from './fhenixMap';\nimport {\n getAbiFunction,\n type IsUnion,\n type UnionToTuple,\n type ContractReturnType,\n extractArrayParameterType,\n} from './utils';\n\ntype FirstCofheReturnValue<T> = [T] extends [void]\n ? never\n : T extends readonly [infer Head, ...(readonly unknown[])]\n ? Head\n : T;\n\ntype FheTypeFromCofheReturnValue<T> = T extends { utype: infer U }\n ? U\n : T extends readonly (infer Element)[]\n ? FheTypeFromCofheReturnValue<Element>\n : never;\n\n/**\n * Extracts the `FheTypes` of the first returned value of a CoFHE call.\n *\n * Examples:\n * - `returns (ebool)` -> `FheTypes.Bool`\n * - `returns (euint8, euint64)` -> `FheTypes.Uint8`\n * - `returns (euint16[])` -> `FheTypes.Uint16`\n */\nexport type CofheFirstReturnFheType<\n abi extends Abi | readonly unknown[] = Abi,\n functionName extends string = string,\n args extends readonly unknown[] | undefined = readonly unknown[] | undefined,\n> = FheTypeFromCofheReturnValue<FirstCofheReturnValue<CofheReturnType<abi, functionName, args>>>;\n\nexport type CofheReturnType<\n abi extends Abi | readonly unknown[] = Abi,\n functionName extends string = string,\n args extends readonly unknown[] | undefined = readonly unknown[] | undefined,\n ///\n abiFunction extends AbiFunction = (\n abi extends Abi ? ExtractAbiFunction<abi, functionName> : AbiFunction\n ) extends infer abiFunction_ extends AbiFunction\n ? IsUnion<abiFunction_> extends true // narrow overloads by `args` by converting to tuple and filtering out overloads that don't match\n ? UnionToTuple<abiFunction_> extends infer abiFunctions extends readonly AbiFunction[]\n ? {\n [K in keyof abiFunctions]: (\n readonly unknown[] | undefined extends args // for functions that don't have inputs, `args` can be `undefined` so fallback to `readonly []`\n ? readonly []\n : args\n ) extends CofheAbiParametersToPrimitiveTypes<abiFunctions[K]['inputs'], 'inputs'>\n ? abiFunctions[K]\n : never;\n }[number] // convert back to union (removes `never` tuple entries: `['foo', never, 'bar'][number]` => `'foo' | 'bar'`)\n : never\n : abiFunction_\n : never,\n outputs extends readonly AbiParameter[] = abiFunction['outputs'],\n primitiveTypes extends readonly unknown[] = CofheAbiParametersToPrimitiveTypes<outputs, 'outputs'>,\n> = [abiFunction] extends [never]\n ? unknown // `abiFunction` was not inferrable (e.g. `abi` declared as `Abi`)\n : readonly unknown[] extends primitiveTypes\n ? unknown // `abiFunction` was not inferrable (e.g. `abi` not const-asserted)\n : primitiveTypes extends readonly [] // unwrap `primitiveTypes`\n ? // biome-ignore lint/suspicious/noConfusingVoidType: <explanation>\n void // no outputs\n : primitiveTypes extends readonly [infer primitiveType]\n ? primitiveType // single output\n : primitiveTypes;\n\ntype EncryptedReturnTypeToReturnTypeMap<E extends EncryptedReturnType> = E extends EBool\n ? boolean\n : E extends EUint8\n ? bigint\n : E extends EUint16\n ? bigint\n : E extends EUint32\n ? bigint\n : E extends EUint64\n ? bigint\n : E extends EUint128\n ? bigint\n : E extends EAddress\n ? string\n : never;\n\ntype EncryptedReturnTypesToReturnTypes<T> = T extends Primitive\n ? LiteralToPrimitive<T>\n : T extends EncryptedReturnType\n ? EncryptedReturnTypeToReturnTypeMap<T>\n : {\n [K in keyof T]: EncryptedReturnTypesToReturnTypes<T[K]>;\n };\n\nexport type CofheReturnTypePostTransform<\n abi extends Abi | readonly unknown[] = Abi,\n functionName extends string = string,\n args extends readonly unknown[] | undefined = readonly unknown[] | undefined,\n> = EncryptedReturnTypesToReturnTypes<CofheReturnType<abi, functionName, args>>;\n\nconst ENCRYPTED_RETURN_TYPE_INTERNAL_TYPES = [\n 'ebool',\n 'euint8',\n 'euint16',\n 'euint32',\n 'euint64',\n 'euint128',\n 'eaddress',\n] as const;\ntype EncryptedReturnTypeInternalType = (typeof ENCRYPTED_RETURN_TYPE_INTERNAL_TYPES)[number];\n\ntype EncryptedReturnTypeInternalTypeToReturnTypeMap<T extends EncryptedReturnTypeInternalType> = T extends 'ebool'\n ? EBool\n : T extends 'euint8'\n ? EUint8\n : T extends 'euint16'\n ? EUint16\n : T extends 'euint32'\n ? EUint32\n : T extends 'euint64'\n ? EUint64\n : T extends 'euint128'\n ? EUint128\n : T extends 'eaddress'\n ? EAddress\n : never;\n\nfunction transformSingleEncryptedReturnTypeToReturnType<T extends EncryptedReturnTypeInternalType>(\n internalType: T,\n data: bigint\n): EncryptedReturnTypeInternalTypeToReturnTypeMap<T> {\n switch (internalType) {\n case 'ebool':\n return {\n ctHash: data,\n utype: FheTypes.Bool,\n } as EncryptedReturnTypeInternalTypeToReturnTypeMap<T>;\n case 'euint8':\n return {\n ctHash: data,\n utype: FheTypes.Uint8,\n } as EncryptedReturnTypeInternalTypeToReturnTypeMap<T>;\n case 'euint16':\n return {\n ctHash: data,\n utype: FheTypes.Uint16,\n } as EncryptedReturnTypeInternalTypeToReturnTypeMap<T>;\n case 'euint32':\n return {\n ctHash: data,\n utype: FheTypes.Uint32,\n } as EncryptedReturnTypeInternalTypeToReturnTypeMap<T>;\n case 'euint64':\n return {\n ctHash: data,\n utype: FheTypes.Uint64,\n } as EncryptedReturnTypeInternalTypeToReturnTypeMap<T>;\n case 'euint128':\n return {\n ctHash: data,\n utype: FheTypes.Uint128,\n } as EncryptedReturnTypeInternalTypeToReturnTypeMap<T>;\n case 'eaddress':\n return {\n ctHash: data,\n utype: FheTypes.Uint160,\n } as EncryptedReturnTypeInternalTypeToReturnTypeMap<T>;\n default:\n throw new Error(`Unknown encrypted return type: ${internalType}`);\n }\n}\n\nfunction transformArrayOfEncryptedReturnTypesToReturnTypes<T extends EncryptedReturnTypeInternalType>(\n internalType: T,\n size: string | undefined,\n data: bigint[]\n): EncryptedReturnTypeInternalTypeToReturnTypeMap<T>[] {\n // Ensure data is an array\n if (!Array.isArray(data)) {\n throw new Error('Data must be an array');\n }\n\n // Ensure length of fixed length tuple matches the length of the data array\n if (size != null && size !== '' && parseInt(size) !== data.length) {\n throw new Error(`Array size mismatch: ${size} !== ${data.length}`);\n }\n\n // Transform each item in the data array into an return type\n return data.map((item) => transformSingleEncryptedReturnTypeToReturnType(internalType, item));\n}\n\nfunction internalTypeIsEncryptedReturnType(internalType: string): internalType is EncryptedReturnTypeInternalType {\n return ENCRYPTED_RETURN_TYPE_INTERNAL_TYPES.includes(internalType as any);\n}\n\nexport function transformEncryptedReturnTypes<TAbi extends Abi, TFunctionName extends string>(\n abi: TAbi,\n functionName: TFunctionName,\n data: ContractReturnType<TAbi, TFunctionName>\n): CofheReturnType<TAbi, TFunctionName> {\n const abiFunction = getAbiFunction(abi, functionName);\n const outputs = abiFunction?.outputs;\n if (abiFunction == null || outputs == null) {\n throw new Error(`Function ${functionName} not found in ABI`);\n }\n const outputsLength = outputs.length;\n\n function processParameter(param: AbiParameter, value: unknown): unknown {\n const [typeHead, typeSize] = extractArrayParameterType(param.type);\n const [internalTypeHead, internalTypeSize] = extractArrayParameterType(param.internalType);\n\n // Is encrypted return type\n if (internalTypeHead != null && internalTypeIsEncryptedReturnType(internalTypeHead)) {\n // Is single encrypted return type\n if (internalTypeSize == null) {\n return transformSingleEncryptedReturnTypeToReturnType(internalTypeHead, value as bigint);\n }\n\n // Is array of encrypted return types\n if (internalTypeSize != null) {\n console.log('array of encrypted return types', internalTypeHead, internalTypeSize, value);\n return transformArrayOfEncryptedReturnTypesToReturnTypes(internalTypeHead, internalTypeSize, value as bigint[]);\n }\n }\n\n // Tuple but not an encrypted return type (recursive case)\n if (typeHead === 'tuple') {\n if ('components' in param && Array.isArray(param.components)) {\n const valueObj = value as Record<string, unknown>;\n const result: Record<string, unknown> = {};\n\n param.components.forEach((component) => {\n const componentName = component.name;\n if (componentName) {\n const componentValue = valueObj[componentName];\n if (componentValue !== undefined) {\n result[componentName] = processParameter(component, componentValue);\n }\n }\n });\n\n return result;\n }\n\n return value;\n }\n\n // Not an encrypted return type, return original value\n return value;\n }\n\n // Process results (array case)\n if (outputsLength > 1) {\n if (!Array.isArray(data)) {\n throw new Error('Data must be an array');\n }\n if (outputsLength !== data.length) {\n throw new Error(`Mismatch in outputs length: ${outputsLength} !== ${data.length}`);\n }\n return data.map((item, index) => {\n return processParameter(outputs[index], item);\n }) as CofheReturnType<TAbi, TFunctionName>;\n }\n\n // Process retuls (single item case)\n return processParameter(outputs[0], data) as CofheReturnType<TAbi, TFunctionName>;\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACAA,iBAoBO;;;ACuJA,SAAS,0BAAwD,MAAkC;AACxG,MAA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