@cofhe/abi 0.0.0-alpha-20260409113701
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 +73 -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 +55 -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,73 @@
|
|
|
1
|
+
# @cofhe/abi Changelog
|
|
2
|
+
|
|
3
|
+
## 0.0.0-alpha-20260409113701
|
|
4
|
+
|
|
5
|
+
### Patch Changes
|
|
6
|
+
|
|
7
|
+
- Updated dependencies [788a6e2]
|
|
8
|
+
- Updated dependencies [9a06012]
|
|
9
|
+
- Updated dependencies [503536a]
|
|
10
|
+
- @cofhe/sdk@0.0.0-alpha-20260409113701
|
|
11
|
+
|
|
12
|
+
## 0.4.0
|
|
13
|
+
|
|
14
|
+
### Patch Changes
|
|
15
|
+
|
|
16
|
+
- Updated dependencies [e446642]
|
|
17
|
+
- @cofhe/sdk@0.4.0
|
|
18
|
+
|
|
19
|
+
## 0.3.2
|
|
20
|
+
|
|
21
|
+
### Patch Changes
|
|
22
|
+
|
|
23
|
+
- d4e86ea: Aligns with CTA encrypted variables bytes32 representation.
|
|
24
|
+
|
|
25
|
+
- **@cofhe/hardhat-plugin**: `hre.cofhe.mocks.getTestBed()`, `getMockTaskManager()`, `getMockACL()`, `getMockThresholdNetwork()`, and `getMockZkVerifier()` now return typed contracts (typechain interfaces) instead of untyped `Contract`. `getPlaintext(ctHash)` and `expectPlaintext(ctHash, value)` now accept bytes32 ctHashes as `string` support cofhe-contracts 0.1.0 CTA changes.
|
|
26
|
+
- **@cofhe/mock-contracts**: Export typechain-generated contract types (`TestBed`, `MockACL`, `MockTaskManager`, `MockZkVerifier`, `MockThresholdNetwork`) for use with the hardhat plugin. Typechain is run from artifact ABIs only; factory files are not generated.
|
|
27
|
+
- **@cofhe/abi**: CTA-related types use `bytes32` (string) instead of `uint256`. Decryption and return-type helpers aligned with cofhe-contracts 0.1.0.
|
|
28
|
+
- **@cofhe/sdk**: Decryption APIs (`decryptForTx`, `decryptForView`, and related builders) now also accept `string` for ciphertext hashes (bytes32) as well as `bigint`.
|
|
29
|
+
|
|
30
|
+
- Updated dependencies [d4e86ea]
|
|
31
|
+
- Updated dependencies [0feaf3f]
|
|
32
|
+
- @cofhe/sdk@0.3.2
|
|
33
|
+
|
|
34
|
+
## 0.3.1
|
|
35
|
+
|
|
36
|
+
### Patch Changes
|
|
37
|
+
|
|
38
|
+
- 370f0c7: no-op
|
|
39
|
+
- Updated dependencies [370f0c7]
|
|
40
|
+
- @cofhe/sdk@0.3.1
|
|
41
|
+
|
|
42
|
+
## 0.3.0
|
|
43
|
+
|
|
44
|
+
### Patch Changes
|
|
45
|
+
|
|
46
|
+
- Updated dependencies [35024b6]
|
|
47
|
+
- Updated dependencies [5467d77]
|
|
48
|
+
- Updated dependencies [73b1502]
|
|
49
|
+
- Updated dependencies [29c2401]
|
|
50
|
+
- Updated dependencies [650ea48]
|
|
51
|
+
- @cofhe/sdk@0.3.0
|
|
52
|
+
|
|
53
|
+
## 0.2.1
|
|
54
|
+
|
|
55
|
+
### Patch Changes
|
|
56
|
+
|
|
57
|
+
- be9bfd9: Fix deployment include `dist` folder.
|
|
58
|
+
- Updated dependencies [409bfdf]
|
|
59
|
+
- Updated dependencies [ac47e2f]
|
|
60
|
+
- Updated dependencies [8af1b70]
|
|
61
|
+
- @cofhe/sdk@0.2.1
|
|
62
|
+
|
|
63
|
+
## 0.2.0
|
|
64
|
+
|
|
65
|
+
### Patch Changes
|
|
66
|
+
|
|
67
|
+
- Updated dependencies [8fda09a]
|
|
68
|
+
- Updated dependencies [4057a76]
|
|
69
|
+
- Updated dependencies [dba2759]
|
|
70
|
+
- Updated dependencies [e0caeca]
|
|
71
|
+
- Updated dependencies [7c861af]
|
|
72
|
+
- Updated dependencies [2a9d6c5]
|
|
73
|
+
- @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
|