@sip-protocol/sdk 0.7.3 → 0.7.4
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/LICENSE +21 -0
- package/README.md +267 -0
- package/dist/{TransportWebUSB-TQ7WZ4LE.mjs → TransportWebUSB-YQMAGJAJ.mjs} +12 -9
- package/dist/browser.d.mts +10 -4
- package/dist/browser.d.ts +10 -4
- package/dist/browser.js +47556 -19603
- package/dist/browser.mjs +628 -48
- package/dist/chunk-4GRJ5MAW.mjs +152 -0
- package/dist/chunk-5D7A3L3W.mjs +717 -0
- package/dist/chunk-64AYA5F5.mjs +7834 -0
- package/dist/chunk-GMDGB22A.mjs +379 -0
- package/dist/chunk-I534WKN7.mjs +328 -0
- package/dist/chunk-IBZVA5Y7.mjs +1003 -0
- package/dist/chunk-PRRZAWJE.mjs +223 -0
- package/dist/{chunk-UJCSKKID.mjs → chunk-XGB3TDIC.mjs} +13 -1
- package/dist/{chunk-3M3HNQCW.mjs → chunk-YWGJ77A2.mjs} +28656 -13103
- package/dist/{chunk-6WGN57S2.mjs → chunk-Z3K7W5S3.mjs} +48 -0
- package/dist/constants-LHAAUC2T.mjs +51 -0
- package/dist/dist-2OGQ7FED.mjs +3957 -0
- package/dist/dist-IFHPYLDX.mjs +254 -0
- package/dist/fulfillment_proof-ANHVPKTB.mjs +21 -0
- package/dist/funding_proof-ICFZ5LHY.mjs +21 -0
- package/dist/{index-DIBZHOOQ.d.ts → index-DXh2IGkz.d.ts} +21239 -10304
- package/dist/{index-8MQz13eJ.d.mts → index-DeE1ZzA4.d.mts} +21239 -10304
- package/dist/index.d.mts +9 -3
- package/dist/index.d.ts +9 -3
- package/dist/index.js +48396 -19623
- package/dist/index.mjs +537 -19
- package/dist/interface-Bf7w1PLW.d.mts +679 -0
- package/dist/interface-Bf7w1PLW.d.ts +679 -0
- package/dist/{noir-DKfEzWy9.d.mts → noir-kzbLVTei.d.mts} +31 -21
- package/dist/{noir-DKfEzWy9.d.ts → noir-kzbLVTei.d.ts} +31 -21
- package/dist/proofs/halo2.d.mts +151 -0
- package/dist/proofs/halo2.d.ts +151 -0
- package/dist/proofs/halo2.js +350 -0
- package/dist/proofs/halo2.mjs +11 -0
- package/dist/proofs/kimchi.d.mts +160 -0
- package/dist/proofs/kimchi.d.ts +160 -0
- package/dist/proofs/kimchi.js +431 -0
- package/dist/proofs/kimchi.mjs +13 -0
- package/dist/proofs/noir.d.mts +1 -1
- package/dist/proofs/noir.d.ts +1 -1
- package/dist/proofs/noir.js +74 -18
- package/dist/proofs/noir.mjs +84 -24
- package/dist/solana-U3MEGU7W.mjs +280 -0
- package/dist/validity_proof-3POXLPNY.mjs +21 -0
- package/package.json +54 -21
- package/src/adapters/index.ts +41 -0
- package/src/adapters/jupiter.ts +571 -0
- package/src/adapters/near-intents.ts +135 -0
- package/src/advisor/advisor.ts +653 -0
- package/src/advisor/index.ts +54 -0
- package/src/advisor/tools.ts +303 -0
- package/src/advisor/types.ts +164 -0
- package/src/chains/ethereum/announcement.ts +536 -0
- package/src/chains/ethereum/bnb-optimizations.ts +474 -0
- package/src/chains/ethereum/commitment.ts +522 -0
- package/src/chains/ethereum/constants.ts +462 -0
- package/src/chains/ethereum/deployment.ts +596 -0
- package/src/chains/ethereum/gas-estimation.ts +538 -0
- package/src/chains/ethereum/index.ts +268 -0
- package/src/chains/ethereum/optimizations.ts +614 -0
- package/src/chains/ethereum/privacy-adapter.ts +855 -0
- package/src/chains/ethereum/registry.ts +584 -0
- package/src/chains/ethereum/rpc.ts +905 -0
- package/src/chains/ethereum/stealth.ts +491 -0
- package/src/chains/ethereum/token.ts +790 -0
- package/src/chains/ethereum/transfer.ts +637 -0
- package/src/chains/ethereum/types.ts +456 -0
- package/src/chains/ethereum/viewing-key.ts +455 -0
- package/src/chains/near/commitment.ts +608 -0
- package/src/chains/near/constants.ts +284 -0
- package/src/chains/near/function-call.ts +871 -0
- package/src/chains/near/history.ts +654 -0
- package/src/chains/near/implicit-account.ts +840 -0
- package/src/chains/near/index.ts +393 -0
- package/src/chains/near/native-transfer.ts +658 -0
- package/src/chains/near/nep141.ts +775 -0
- package/src/chains/near/privacy-adapter.ts +889 -0
- package/src/chains/near/resolver.ts +971 -0
- package/src/chains/near/rpc.ts +1016 -0
- package/src/chains/near/stealth.ts +419 -0
- package/src/chains/near/types.ts +317 -0
- package/src/chains/near/viewing-key.ts +876 -0
- package/src/chains/solana/anchor-transfer.ts +386 -0
- package/src/chains/solana/commitment.ts +577 -0
- package/src/chains/solana/constants.ts +126 -12
- package/src/chains/solana/ephemeral-keys.ts +543 -0
- package/src/chains/solana/index.ts +252 -1
- package/src/chains/solana/key-derivation.ts +418 -0
- package/src/chains/solana/kit-compat.ts +334 -0
- package/src/chains/solana/optimizations.ts +560 -0
- package/src/chains/solana/privacy-adapter.ts +605 -0
- package/src/chains/solana/providers/generic.ts +47 -6
- package/src/chains/solana/providers/helius-enhanced-types.ts +336 -0
- package/src/chains/solana/providers/helius-enhanced.ts +623 -0
- package/src/chains/solana/providers/helius.ts +186 -33
- package/src/chains/solana/providers/index.ts +31 -0
- package/src/chains/solana/providers/interface.ts +61 -18
- package/src/chains/solana/providers/quicknode.ts +409 -0
- package/src/chains/solana/providers/triton.ts +426 -0
- package/src/chains/solana/providers/webhook.ts +338 -67
- package/src/chains/solana/rpc-client.ts +1150 -0
- package/src/chains/solana/scan.ts +83 -66
- package/src/chains/solana/sol-transfer.ts +732 -0
- package/src/chains/solana/spl-transfer.ts +886 -0
- package/src/chains/solana/stealth-scanner.ts +703 -0
- package/src/chains/solana/sunspot-verifier.ts +453 -0
- package/src/chains/solana/transaction-builder.ts +755 -0
- package/src/chains/solana/transfer.ts +74 -5
- package/src/chains/solana/types.ts +57 -6
- package/src/chains/solana/utils.ts +110 -0
- package/src/chains/solana/viewing-key.ts +807 -0
- package/src/compliance/fireblocks.ts +921 -0
- package/src/compliance/index.ts +23 -0
- package/src/compliance/range-sas.ts +398 -33
- package/src/config/endpoints.ts +100 -0
- package/src/crypto.ts +11 -8
- package/src/errors.ts +82 -0
- package/src/evm/erc4337-relayer.ts +830 -0
- package/src/evm/index.ts +47 -0
- package/src/fees/calculator.ts +396 -0
- package/src/fees/index.ts +87 -0
- package/src/fees/near-contract.ts +429 -0
- package/src/fees/types.ts +268 -0
- package/src/index.ts +686 -1
- package/src/intent.ts +6 -3
- package/src/logger.ts +324 -0
- package/src/network/index.ts +80 -0
- package/src/network/proxy.ts +691 -0
- package/src/optimizations/index.ts +541 -0
- package/src/oracle/types.ts +1 -0
- package/src/privacy-backends/arcium-types.ts +727 -0
- package/src/privacy-backends/arcium.ts +719 -0
- package/src/privacy-backends/combined-privacy.ts +866 -0
- package/src/privacy-backends/cspl-token.ts +595 -0
- package/src/privacy-backends/cspl-types.ts +512 -0
- package/src/privacy-backends/cspl.ts +907 -0
- package/src/privacy-backends/health.ts +488 -0
- package/src/privacy-backends/inco-types.ts +323 -0
- package/src/privacy-backends/inco.ts +616 -0
- package/src/privacy-backends/index.ts +254 -4
- package/src/privacy-backends/interface.ts +649 -6
- package/src/privacy-backends/lru-cache.ts +343 -0
- package/src/privacy-backends/magicblock.ts +458 -0
- package/src/privacy-backends/mock.ts +258 -0
- package/src/privacy-backends/privacycash.ts +13 -17
- package/src/privacy-backends/private-swap.ts +570 -0
- package/src/privacy-backends/rate-limiter.ts +683 -0
- package/src/privacy-backends/registry.ts +414 -2
- package/src/privacy-backends/router.ts +283 -3
- package/src/privacy-backends/shadowwire.ts +449 -0
- package/src/privacy-backends/sip-native.ts +3 -0
- package/src/privacy-logger.ts +191 -0
- package/src/production-safety.ts +373 -0
- package/src/proofs/aggregator.ts +1029 -0
- package/src/proofs/browser-composer.ts +1150 -0
- package/src/proofs/browser.ts +113 -25
- package/src/proofs/cache/index.ts +127 -0
- package/src/proofs/cache/interface.ts +545 -0
- package/src/proofs/cache/key-generator.ts +188 -0
- package/src/proofs/cache/lru-cache.ts +481 -0
- package/src/proofs/cache/multi-tier-cache.ts +575 -0
- package/src/proofs/cache/persistent-cache.ts +788 -0
- package/src/proofs/compliance-proof.ts +872 -0
- package/src/proofs/composer/base.ts +923 -0
- package/src/proofs/composer/index.ts +25 -0
- package/src/proofs/composer/interface.ts +518 -0
- package/src/proofs/composer/types.ts +383 -0
- package/src/proofs/converters/halo2.ts +452 -0
- package/src/proofs/converters/index.ts +208 -0
- package/src/proofs/converters/interface.ts +363 -0
- package/src/proofs/converters/kimchi.ts +462 -0
- package/src/proofs/converters/noir.ts +451 -0
- package/src/proofs/fallback.ts +888 -0
- package/src/proofs/halo2.ts +42 -0
- package/src/proofs/index.ts +471 -0
- package/src/proofs/interface.ts +13 -0
- package/src/proofs/kimchi.ts +42 -0
- package/src/proofs/lazy.ts +1004 -0
- package/src/proofs/mock.ts +25 -1
- package/src/proofs/noir.ts +110 -29
- package/src/proofs/orchestrator.ts +960 -0
- package/src/proofs/parallel/concurrency.ts +297 -0
- package/src/proofs/parallel/dependency-graph.ts +602 -0
- package/src/proofs/parallel/executor.ts +420 -0
- package/src/proofs/parallel/index.ts +131 -0
- package/src/proofs/parallel/interface.ts +685 -0
- package/src/proofs/parallel/worker-pool.ts +644 -0
- package/src/proofs/providers/halo2.ts +560 -0
- package/src/proofs/providers/index.ts +34 -0
- package/src/proofs/providers/kimchi.ts +641 -0
- package/src/proofs/validator.ts +881 -0
- package/src/proofs/verifier.ts +867 -0
- package/src/quantum/index.ts +112 -0
- package/src/quantum/winternitz-vault.ts +639 -0
- package/src/quantum/wots.ts +611 -0
- package/src/settlement/backends/direct-chain.ts +1 -0
- package/src/settlement/index.ts +9 -0
- package/src/settlement/router.ts +732 -46
- package/src/solana/index.ts +72 -0
- package/src/solana/jito-relayer.ts +687 -0
- package/src/solana/noir-verifier-types.ts +430 -0
- package/src/solana/noir-verifier.ts +816 -0
- package/src/stealth/address-derivation.ts +193 -0
- package/src/stealth/ed25519.ts +431 -0
- package/src/stealth/index.ts +233 -0
- package/src/stealth/meta-address.ts +221 -0
- package/src/stealth/secp256k1.ts +368 -0
- package/src/stealth/utils.ts +194 -0
- package/src/stealth.ts +50 -1504
- package/src/sync/index.ts +106 -0
- package/src/sync/manager.ts +504 -0
- package/src/sync/mock-provider.ts +318 -0
- package/src/sync/oblivious.ts +625 -0
- package/src/tokens/index.ts +15 -0
- package/src/tokens/registry.ts +301 -0
- package/src/utils/deprecation.ts +94 -0
- package/src/utils/index.ts +9 -0
- package/src/wallet/ethereum/index.ts +68 -0
- package/src/wallet/ethereum/metamask-privacy.ts +420 -0
- package/src/wallet/ethereum/multi-wallet.ts +646 -0
- package/src/wallet/ethereum/privacy-adapter.ts +700 -0
- package/src/wallet/ethereum/types.ts +3 -1
- package/src/wallet/ethereum/walletconnect-adapter.ts +675 -0
- package/src/wallet/hardware/index.ts +10 -0
- package/src/wallet/hardware/ledger-privacy.ts +414 -0
- package/src/wallet/index.ts +71 -0
- package/src/wallet/near/adapter.ts +626 -0
- package/src/wallet/near/index.ts +86 -0
- package/src/wallet/near/meteor-wallet.ts +1153 -0
- package/src/wallet/near/my-near-wallet.ts +790 -0
- package/src/wallet/near/wallet-selector.ts +702 -0
- package/src/wallet/solana/adapter.ts +6 -4
- package/src/wallet/solana/index.ts +13 -0
- package/src/wallet/solana/privacy-adapter.ts +567 -0
- package/src/wallet/sui/types.ts +6 -4
- package/src/zcash/rpc-client.ts +13 -6
- package/dist/chunk-2XIVXWHA.mjs +0 -1930
- package/dist/chunk-3INS3PR5.mjs +0 -884
- package/dist/chunk-3OVABDRH.mjs +0 -17096
- package/dist/chunk-7RFRWDCW.mjs +0 -1504
- package/dist/chunk-DLDWZFYC.mjs +0 -1495
- package/dist/chunk-E6SZWREQ.mjs +0 -57
- package/dist/chunk-F6F73W35.mjs +0 -16166
- package/dist/chunk-G33LB27A.mjs +0 -16166
- package/dist/chunk-HGU6HZRC.mjs +0 -231
- package/dist/chunk-L2K34JCU.mjs +0 -1496
- package/dist/chunk-OFDBEIEK.mjs +0 -16166
- package/dist/chunk-SF7YSLF5.mjs +0 -1515
- package/dist/chunk-SN4ZDTVW.mjs +0 -16166
- package/dist/chunk-WWUSGOXE.mjs +0 -17129
- package/dist/constants-VOI7BSLK.mjs +0 -27
- package/dist/index-B71aXVzk.d.ts +0 -13264
- package/dist/index-BYZbDjal.d.ts +0 -11390
- package/dist/index-CHB3KuOB.d.mts +0 -11859
- package/dist/index-CzWPI6Le.d.ts +0 -11859
- package/dist/index-pOIIuwfV.d.mts +0 -13264
- package/dist/index-xbWjohNq.d.mts +0 -11390
- package/dist/solana-4O4K45VU.mjs +0 -46
- package/dist/solana-5EMCTPTS.mjs +0 -46
- package/dist/solana-NDABAZ6P.mjs +0 -56
- package/dist/solana-Q4NAVBTS.mjs +0 -46
- package/dist/solana-ZYO63LY5.mjs +0 -46
|
@@ -0,0 +1,616 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Inco Privacy Backend
|
|
3
|
+
*
|
|
4
|
+
* Implements the PrivacyBackend interface using Inco FHE (Fully Homomorphic Encryption).
|
|
5
|
+
* Inco provides compute privacy with encrypted on-chain state.
|
|
6
|
+
*
|
|
7
|
+
* ## Key Characteristics
|
|
8
|
+
*
|
|
9
|
+
* - **FHE Computation**: Compute over encrypted data without decryption
|
|
10
|
+
* - **Encrypted State**: Contract state remains encrypted on-chain
|
|
11
|
+
* - **Type System**: euint256, ebool, eaddress encrypted types
|
|
12
|
+
* - **Attestations**: Verified decryption mechanisms
|
|
13
|
+
*
|
|
14
|
+
* ## Trade-offs vs Other Backends
|
|
15
|
+
*
|
|
16
|
+
* | Feature | Inco (FHE) | Arcium (MPC) | SIP Native |
|
|
17
|
+
* |---------|------------|--------------|------------|
|
|
18
|
+
* | Hides sender | ❌ | ❌ | ✅ Stealth |
|
|
19
|
+
* | Hides amount | ✅ (in state) | ❌ | ✅ Pedersen |
|
|
20
|
+
* | Hides computation | ✅ FHE | ✅ MPC | ❌ |
|
|
21
|
+
* | Encrypted state | ✅ | ❌ | ❌ |
|
|
22
|
+
* | Setup required | ✅ Contract | ✅ Circuit | ❌ |
|
|
23
|
+
* | Latency | Medium | Slow | Fast |
|
|
24
|
+
*
|
|
25
|
+
* ## Use Cases
|
|
26
|
+
*
|
|
27
|
+
* - Private voting (encrypted tallies)
|
|
28
|
+
* - Private gaming (encrypted game state)
|
|
29
|
+
* - Confidential DeFi (encrypted positions)
|
|
30
|
+
* - Private NFTs (encrypted metadata)
|
|
31
|
+
*
|
|
32
|
+
* @example
|
|
33
|
+
* ```typescript
|
|
34
|
+
* import { IncoBackend, PrivacyBackendRegistry } from '@sip-protocol/sdk'
|
|
35
|
+
*
|
|
36
|
+
* const backend = new IncoBackend({
|
|
37
|
+
* rpcUrl: 'https://testnet.inco.org',
|
|
38
|
+
* network: 'testnet',
|
|
39
|
+
* })
|
|
40
|
+
*
|
|
41
|
+
* const registry = new PrivacyBackendRegistry()
|
|
42
|
+
* registry.register(backend, { priority: 65 })
|
|
43
|
+
*
|
|
44
|
+
* // Execute FHE computation
|
|
45
|
+
* const result = await backend.executeComputation({
|
|
46
|
+
* chain: 'ethereum',
|
|
47
|
+
* circuitId: 'private-vote-contract',
|
|
48
|
+
* encryptedInputs: [encryptedVote],
|
|
49
|
+
* })
|
|
50
|
+
*
|
|
51
|
+
* if (result.success) {
|
|
52
|
+
* console.log(`Computation ${result.computationId} completed`)
|
|
53
|
+
* }
|
|
54
|
+
* ```
|
|
55
|
+
*
|
|
56
|
+
* @see https://docs.inco.org
|
|
57
|
+
*/
|
|
58
|
+
|
|
59
|
+
import type {
|
|
60
|
+
PrivacyBackend,
|
|
61
|
+
BackendType,
|
|
62
|
+
BackendCapabilities,
|
|
63
|
+
TransferParams,
|
|
64
|
+
TransactionResult,
|
|
65
|
+
ComputationParams,
|
|
66
|
+
ComputationResult,
|
|
67
|
+
AvailabilityResult,
|
|
68
|
+
BackendParams,
|
|
69
|
+
ComputationStatus,
|
|
70
|
+
} from './interface'
|
|
71
|
+
|
|
72
|
+
import {
|
|
73
|
+
isComputationParams,
|
|
74
|
+
withTimeout,
|
|
75
|
+
ComputationTimeoutError,
|
|
76
|
+
deepFreeze,
|
|
77
|
+
} from './interface'
|
|
78
|
+
|
|
79
|
+
import type {
|
|
80
|
+
IncoProduct,
|
|
81
|
+
EncryptedType,
|
|
82
|
+
EncryptedValue,
|
|
83
|
+
FHEComputationInfo,
|
|
84
|
+
IIncoClient,
|
|
85
|
+
} from './inco-types'
|
|
86
|
+
|
|
87
|
+
import {
|
|
88
|
+
INCO_RPC_URLS,
|
|
89
|
+
INCO_SUPPORTED_CHAINS,
|
|
90
|
+
DEFAULT_FHE_TIMEOUT_MS,
|
|
91
|
+
ESTIMATED_FHE_TIME_MS,
|
|
92
|
+
BASE_FHE_COST_WEI,
|
|
93
|
+
COST_PER_ENCRYPTED_INPUT_WEI,
|
|
94
|
+
type IncoNetwork,
|
|
95
|
+
} from './inco-types'
|
|
96
|
+
|
|
97
|
+
/**
|
|
98
|
+
* Configuration options for Inco backend
|
|
99
|
+
*/
|
|
100
|
+
export interface IncoBackendConfig {
|
|
101
|
+
/** RPC endpoint URL */
|
|
102
|
+
rpcUrl?: string
|
|
103
|
+
/** Network type */
|
|
104
|
+
network?: IncoNetwork
|
|
105
|
+
/** Chain ID for EVM networks */
|
|
106
|
+
chainId?: number
|
|
107
|
+
/** Inco product (lightning or atlas) */
|
|
108
|
+
product?: IncoProduct
|
|
109
|
+
/** Computation timeout in milliseconds */
|
|
110
|
+
timeout?: number
|
|
111
|
+
/** Custom SDK client (for testing) */
|
|
112
|
+
client?: IIncoClient
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
/**
|
|
116
|
+
* Inco capabilities (static)
|
|
117
|
+
*
|
|
118
|
+
* Key difference from Arcium: Inco can hide amounts in encrypted state
|
|
119
|
+
*/
|
|
120
|
+
const INCO_CAPABILITIES: BackendCapabilities = {
|
|
121
|
+
hiddenAmount: true, // FHE can encrypt amounts in contract state
|
|
122
|
+
hiddenSender: false, // Transaction sender is visible
|
|
123
|
+
hiddenRecipient: false, // Transaction recipient is visible
|
|
124
|
+
hiddenCompute: true, // PRIMARY PURPOSE: Encrypted state computation
|
|
125
|
+
complianceSupport: false, // No viewing keys for FHE (yet)
|
|
126
|
+
anonymitySet: undefined, // Not applicable for FHE
|
|
127
|
+
setupRequired: true, // Contract must use Inco SDK
|
|
128
|
+
latencyEstimate: 'medium', // Faster than MPC, slower than plain
|
|
129
|
+
supportedTokens: 'all', // Can work with any token in encrypted state
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
/**
|
|
133
|
+
* Inco FHE Compute Privacy Backend
|
|
134
|
+
*
|
|
135
|
+
* Provides compute privacy through Fully Homomorphic Encryption.
|
|
136
|
+
* Use this backend for encrypted on-chain state and computation.
|
|
137
|
+
*/
|
|
138
|
+
export class IncoBackend implements PrivacyBackend {
|
|
139
|
+
readonly name = 'inco'
|
|
140
|
+
readonly type: BackendType = 'compute'
|
|
141
|
+
readonly chains: string[] = [...INCO_SUPPORTED_CHAINS, 'solana']
|
|
142
|
+
|
|
143
|
+
private config: Required<Omit<IncoBackendConfig, 'client'>> & {
|
|
144
|
+
client?: IIncoClient
|
|
145
|
+
}
|
|
146
|
+
private computationCache: Map<string, FHEComputationInfo> = new Map()
|
|
147
|
+
|
|
148
|
+
/**
|
|
149
|
+
* Create a new Inco backend
|
|
150
|
+
*
|
|
151
|
+
* @param config - Backend configuration
|
|
152
|
+
* @throws {Error} If network is invalid
|
|
153
|
+
*/
|
|
154
|
+
constructor(config: IncoBackendConfig = {}) {
|
|
155
|
+
// Validate network parameter if provided
|
|
156
|
+
if (config.network !== undefined) {
|
|
157
|
+
const validNetworks: IncoNetwork[] = ['testnet', 'mainnet']
|
|
158
|
+
if (!validNetworks.includes(config.network)) {
|
|
159
|
+
throw new Error(
|
|
160
|
+
`Invalid Inco network '${config.network}'. ` +
|
|
161
|
+
`Valid networks: ${validNetworks.join(', ')}`
|
|
162
|
+
)
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
this.config = {
|
|
167
|
+
rpcUrl: config.rpcUrl ?? INCO_RPC_URLS[config.network ?? 'testnet'],
|
|
168
|
+
network: config.network ?? 'testnet',
|
|
169
|
+
chainId: config.chainId ?? 9090,
|
|
170
|
+
product: config.product ?? 'lightning',
|
|
171
|
+
timeout: config.timeout ?? DEFAULT_FHE_TIMEOUT_MS,
|
|
172
|
+
client: config.client,
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
/**
|
|
177
|
+
* Check if backend is available for given parameters
|
|
178
|
+
*/
|
|
179
|
+
async checkAvailability(params: BackendParams): Promise<AvailabilityResult> {
|
|
180
|
+
// Must be computation params for Inco
|
|
181
|
+
if (!isComputationParams(params)) {
|
|
182
|
+
return {
|
|
183
|
+
available: false,
|
|
184
|
+
reason:
|
|
185
|
+
'Inco is a compute backend. Use ComputationParams with circuitId and encryptedInputs.',
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
return this.checkComputeAvailability(params)
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
/**
|
|
193
|
+
* Check availability for computation params
|
|
194
|
+
*/
|
|
195
|
+
private async checkComputeAvailability(
|
|
196
|
+
params: ComputationParams
|
|
197
|
+
): Promise<AvailabilityResult> {
|
|
198
|
+
// Validate chain - Inco supports EVM chains + Solana (beta)
|
|
199
|
+
const supportedChains = [...INCO_SUPPORTED_CHAINS, 'solana']
|
|
200
|
+
if (!supportedChains.includes(params.chain as typeof INCO_SUPPORTED_CHAINS[number])) {
|
|
201
|
+
return {
|
|
202
|
+
available: false,
|
|
203
|
+
reason: `Inco supports ${supportedChains.join(', ')}. Got: '${params.chain}'`,
|
|
204
|
+
}
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
// Validate circuitId (contract address for Inco)
|
|
208
|
+
if (!params.circuitId || params.circuitId.trim() === '') {
|
|
209
|
+
return {
|
|
210
|
+
available: false,
|
|
211
|
+
reason: 'circuitId (contract address) is required for Inco computations',
|
|
212
|
+
}
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
// Validate encrypted inputs
|
|
216
|
+
if (!params.encryptedInputs || params.encryptedInputs.length === 0) {
|
|
217
|
+
return {
|
|
218
|
+
available: false,
|
|
219
|
+
reason: 'encryptedInputs array is required and must not be empty',
|
|
220
|
+
}
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
// Validate each input is a valid Uint8Array
|
|
224
|
+
for (let i = 0; i < params.encryptedInputs.length; i++) {
|
|
225
|
+
const input = params.encryptedInputs[i]
|
|
226
|
+
if (!(input instanceof Uint8Array) || input.length === 0) {
|
|
227
|
+
return {
|
|
228
|
+
available: false,
|
|
229
|
+
reason: `encryptedInputs[${i}] must be a non-empty Uint8Array`,
|
|
230
|
+
}
|
|
231
|
+
}
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
// In production, would check:
|
|
235
|
+
// - Contract exists and uses Inco SDK
|
|
236
|
+
// - Network is reachable
|
|
237
|
+
// - User has necessary permissions
|
|
238
|
+
|
|
239
|
+
return {
|
|
240
|
+
available: true,
|
|
241
|
+
estimatedCost: this.estimateFHECost(params),
|
|
242
|
+
estimatedTime: ESTIMATED_FHE_TIME_MS,
|
|
243
|
+
}
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
/**
|
|
247
|
+
* Get backend capabilities
|
|
248
|
+
*/
|
|
249
|
+
getCapabilities(): BackendCapabilities {
|
|
250
|
+
return { ...INCO_CAPABILITIES }
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
/**
|
|
254
|
+
* Execute a privacy-preserving transfer
|
|
255
|
+
*
|
|
256
|
+
* Inco is a compute backend - this method returns an error
|
|
257
|
+
* directing users to use executeComputation() instead.
|
|
258
|
+
*/
|
|
259
|
+
async execute(_params: TransferParams): Promise<TransactionResult> {
|
|
260
|
+
return {
|
|
261
|
+
success: false,
|
|
262
|
+
error:
|
|
263
|
+
'Inco is a compute privacy backend for FHE operations. ' +
|
|
264
|
+
'Use executeComputation() for encrypted state computations. ' +
|
|
265
|
+
'For transaction privacy, use SIPNativeBackend or PrivacyCashBackend.',
|
|
266
|
+
backend: this.name,
|
|
267
|
+
metadata: {
|
|
268
|
+
hint: 'executeComputation',
|
|
269
|
+
paramsType: 'ComputationParams',
|
|
270
|
+
},
|
|
271
|
+
}
|
|
272
|
+
}
|
|
273
|
+
|
|
274
|
+
/**
|
|
275
|
+
* Execute a privacy-preserving computation via FHE
|
|
276
|
+
*
|
|
277
|
+
* This submits encrypted data to an Inco-enabled smart contract
|
|
278
|
+
* for computation over encrypted state.
|
|
279
|
+
*
|
|
280
|
+
* @param params - Computation parameters
|
|
281
|
+
* @returns Computation result with ID for tracking
|
|
282
|
+
*/
|
|
283
|
+
async executeComputation(
|
|
284
|
+
params: ComputationParams
|
|
285
|
+
): Promise<ComputationResult> {
|
|
286
|
+
// Validate availability first
|
|
287
|
+
const availability = await this.checkComputeAvailability(params)
|
|
288
|
+
if (!availability.available) {
|
|
289
|
+
return {
|
|
290
|
+
success: false,
|
|
291
|
+
error: availability.reason,
|
|
292
|
+
backend: this.name,
|
|
293
|
+
}
|
|
294
|
+
}
|
|
295
|
+
|
|
296
|
+
try {
|
|
297
|
+
// In a real implementation with the SDK:
|
|
298
|
+
// 1. const client = await this.getClient()
|
|
299
|
+
// 2. const computationId = await client.submitComputation({
|
|
300
|
+
// contractAddress: params.circuitId,
|
|
301
|
+
// functionName: params.options?.functionName ?? 'compute',
|
|
302
|
+
// encryptedInputs: params.encryptedInputs.map(i => handleFromBytes(i)),
|
|
303
|
+
// })
|
|
304
|
+
// 3. Optionally await completion
|
|
305
|
+
|
|
306
|
+
// Simulated result (SDK integration pending)
|
|
307
|
+
const simulatedComputationId = this.generateComputationId()
|
|
308
|
+
|
|
309
|
+
// Cache the computation info
|
|
310
|
+
const info: FHEComputationInfo = {
|
|
311
|
+
id: simulatedComputationId,
|
|
312
|
+
status: 'submitted',
|
|
313
|
+
contractAddress: params.circuitId,
|
|
314
|
+
functionName: (params.options?.functionName as string) ?? 'compute',
|
|
315
|
+
inputHandles: params.encryptedInputs.map((_, i) => `handle_${i}`),
|
|
316
|
+
submittedAt: Date.now(),
|
|
317
|
+
}
|
|
318
|
+
this.computationCache.set(simulatedComputationId, info)
|
|
319
|
+
|
|
320
|
+
return {
|
|
321
|
+
success: true,
|
|
322
|
+
computationId: simulatedComputationId,
|
|
323
|
+
backend: this.name,
|
|
324
|
+
status: 'submitted',
|
|
325
|
+
metadata: {
|
|
326
|
+
contractAddress: params.circuitId,
|
|
327
|
+
functionName: info.functionName,
|
|
328
|
+
inputCount: params.encryptedInputs.length,
|
|
329
|
+
network: this.config.network,
|
|
330
|
+
product: this.config.product,
|
|
331
|
+
chainId: this.config.chainId,
|
|
332
|
+
submittedAt: Date.now(),
|
|
333
|
+
warning: 'Simulated result - SDK integration pending',
|
|
334
|
+
},
|
|
335
|
+
}
|
|
336
|
+
} catch (error) {
|
|
337
|
+
return {
|
|
338
|
+
success: false,
|
|
339
|
+
error: error instanceof Error ? error.message : 'Unknown error',
|
|
340
|
+
backend: this.name,
|
|
341
|
+
status: 'failed',
|
|
342
|
+
}
|
|
343
|
+
}
|
|
344
|
+
}
|
|
345
|
+
|
|
346
|
+
/**
|
|
347
|
+
* Estimate cost for an operation
|
|
348
|
+
*/
|
|
349
|
+
async estimateCost(params: BackendParams): Promise<bigint> {
|
|
350
|
+
if (isComputationParams(params)) {
|
|
351
|
+
return this.estimateFHECost(params)
|
|
352
|
+
}
|
|
353
|
+
|
|
354
|
+
// Transfer params - return 0 as we don't support transfers
|
|
355
|
+
return BigInt(0)
|
|
356
|
+
}
|
|
357
|
+
|
|
358
|
+
/**
|
|
359
|
+
* Encrypt a value for FHE computation
|
|
360
|
+
*
|
|
361
|
+
* Helper method for encrypting plaintext values into FHE-compatible format.
|
|
362
|
+
*
|
|
363
|
+
* @param value - Value to encrypt
|
|
364
|
+
* @param type - Target encrypted type
|
|
365
|
+
* @returns Encrypted value with handle
|
|
366
|
+
*/
|
|
367
|
+
async encryptValue(
|
|
368
|
+
value: bigint | boolean | string,
|
|
369
|
+
type: EncryptedType
|
|
370
|
+
): Promise<EncryptedValue> {
|
|
371
|
+
// Validate type matches value
|
|
372
|
+
if (type === 'euint256' && typeof value !== 'bigint') {
|
|
373
|
+
throw new Error('euint256 requires a bigint value')
|
|
374
|
+
}
|
|
375
|
+
if (type === 'ebool' && typeof value !== 'boolean') {
|
|
376
|
+
throw new Error('ebool requires a boolean value')
|
|
377
|
+
}
|
|
378
|
+
if (type === 'eaddress' && typeof value !== 'string') {
|
|
379
|
+
throw new Error('eaddress requires a string value')
|
|
380
|
+
}
|
|
381
|
+
|
|
382
|
+
// In production, would use the SDK:
|
|
383
|
+
// const client = await this.getClient()
|
|
384
|
+
// return client.encrypt({ value, type })
|
|
385
|
+
|
|
386
|
+
// Simulated encryption
|
|
387
|
+
const handle = `inco_${type}_${Date.now().toString(36)}_${Math.random().toString(36).slice(2, 8)}`
|
|
388
|
+
const ciphertext = this.simulateEncryption(value, type)
|
|
389
|
+
|
|
390
|
+
return {
|
|
391
|
+
handle,
|
|
392
|
+
type,
|
|
393
|
+
ciphertext,
|
|
394
|
+
chainId: this.config.chainId,
|
|
395
|
+
}
|
|
396
|
+
}
|
|
397
|
+
|
|
398
|
+
/**
|
|
399
|
+
* Decrypt an encrypted value
|
|
400
|
+
*
|
|
401
|
+
* Requires proper authorization (attestation) in production.
|
|
402
|
+
*
|
|
403
|
+
* @param handle - Handle of the encrypted value
|
|
404
|
+
* @param type - Expected type (for validation)
|
|
405
|
+
* @returns Decrypted value
|
|
406
|
+
*/
|
|
407
|
+
async decryptValue(
|
|
408
|
+
handle: string,
|
|
409
|
+
type: EncryptedType
|
|
410
|
+
): Promise<bigint | boolean | string> {
|
|
411
|
+
// In production, would use the SDK with attestation:
|
|
412
|
+
// const client = await this.getClient()
|
|
413
|
+
// const result = await client.decrypt({ handle, type })
|
|
414
|
+
// return result.value
|
|
415
|
+
void handle // Mark as intentionally unused (for future SDK integration)
|
|
416
|
+
|
|
417
|
+
// Simulated decryption - return type-appropriate default
|
|
418
|
+
switch (type) {
|
|
419
|
+
case 'euint256':
|
|
420
|
+
return BigInt(0)
|
|
421
|
+
case 'ebool':
|
|
422
|
+
return false
|
|
423
|
+
case 'eaddress':
|
|
424
|
+
return '0x0000000000000000000000000000000000000000'
|
|
425
|
+
default:
|
|
426
|
+
throw new Error(`Unknown encrypted type: ${type}`)
|
|
427
|
+
}
|
|
428
|
+
}
|
|
429
|
+
|
|
430
|
+
/**
|
|
431
|
+
* Get computation status
|
|
432
|
+
*
|
|
433
|
+
* @param computationId - Computation to check
|
|
434
|
+
* @returns Current status or undefined if not found
|
|
435
|
+
*/
|
|
436
|
+
async getComputationStatus(
|
|
437
|
+
computationId: string
|
|
438
|
+
): Promise<ComputationStatus | undefined> {
|
|
439
|
+
// Check cache first
|
|
440
|
+
const cached = this.computationCache.get(computationId)
|
|
441
|
+
if (cached) {
|
|
442
|
+
return cached.status
|
|
443
|
+
}
|
|
444
|
+
|
|
445
|
+
// In production, would query the Inco network:
|
|
446
|
+
// const client = await this.getClient()
|
|
447
|
+
// return client.getComputationStatus(computationId)
|
|
448
|
+
|
|
449
|
+
return undefined
|
|
450
|
+
}
|
|
451
|
+
|
|
452
|
+
/**
|
|
453
|
+
* Get computation info
|
|
454
|
+
*
|
|
455
|
+
* @param computationId - Computation to query
|
|
456
|
+
* @returns Computation info or undefined if not found
|
|
457
|
+
*/
|
|
458
|
+
async getComputationInfo(
|
|
459
|
+
computationId: string
|
|
460
|
+
): Promise<FHEComputationInfo | undefined> {
|
|
461
|
+
// Check cache
|
|
462
|
+
const cached = this.computationCache.get(computationId)
|
|
463
|
+
if (cached) {
|
|
464
|
+
return cached
|
|
465
|
+
}
|
|
466
|
+
|
|
467
|
+
// In production, would query the Inco network
|
|
468
|
+
return undefined
|
|
469
|
+
}
|
|
470
|
+
|
|
471
|
+
/**
|
|
472
|
+
* Wait for computation to complete
|
|
473
|
+
*
|
|
474
|
+
* @param computationId - Computation to wait for
|
|
475
|
+
* @param timeout - Optional timeout override (defaults to config.timeout)
|
|
476
|
+
* @returns Computation result
|
|
477
|
+
* @throws {ComputationTimeoutError} If computation exceeds timeout
|
|
478
|
+
*/
|
|
479
|
+
async awaitComputation(
|
|
480
|
+
computationId: string,
|
|
481
|
+
timeout?: number
|
|
482
|
+
): Promise<ComputationResult> {
|
|
483
|
+
const timeoutMs = timeout ?? this.config.timeout
|
|
484
|
+
|
|
485
|
+
// Check if computation exists before waiting
|
|
486
|
+
const info = this.computationCache.get(computationId)
|
|
487
|
+
if (!info) {
|
|
488
|
+
return {
|
|
489
|
+
success: false,
|
|
490
|
+
error: `Computation ${computationId} not found`,
|
|
491
|
+
backend: this.name,
|
|
492
|
+
status: 'failed',
|
|
493
|
+
}
|
|
494
|
+
}
|
|
495
|
+
|
|
496
|
+
// Wrap the polling/waiting logic with timeout
|
|
497
|
+
return withTimeout(
|
|
498
|
+
this.pollComputationResult(computationId, info),
|
|
499
|
+
timeoutMs,
|
|
500
|
+
() => {
|
|
501
|
+
throw new ComputationTimeoutError(computationId, timeoutMs, this.name)
|
|
502
|
+
}
|
|
503
|
+
)
|
|
504
|
+
}
|
|
505
|
+
|
|
506
|
+
/**
|
|
507
|
+
* Poll for computation result (simulation)
|
|
508
|
+
*
|
|
509
|
+
* In production, this would poll the Inco network for completion.
|
|
510
|
+
* Currently simulates immediate completion for testing.
|
|
511
|
+
*/
|
|
512
|
+
private async pollComputationResult(
|
|
513
|
+
computationId: string,
|
|
514
|
+
info: FHEComputationInfo
|
|
515
|
+
): Promise<ComputationResult> {
|
|
516
|
+
// In production, would poll the Inco network:
|
|
517
|
+
// const client = await this.getClient()
|
|
518
|
+
// const output = await client.awaitFinalization(computationId)
|
|
519
|
+
|
|
520
|
+
// Simulated: Mark as completed immediately
|
|
521
|
+
info.status = 'completed'
|
|
522
|
+
info.completedAt = Date.now()
|
|
523
|
+
info.outputHandle = `output_${computationId}`
|
|
524
|
+
this.computationCache.set(computationId, info)
|
|
525
|
+
|
|
526
|
+
return {
|
|
527
|
+
success: true,
|
|
528
|
+
computationId,
|
|
529
|
+
output: new Uint8Array([0, 1, 2, 3]), // Simulated output
|
|
530
|
+
backend: this.name,
|
|
531
|
+
status: 'completed',
|
|
532
|
+
completedAt: info.completedAt,
|
|
533
|
+
metadata: {
|
|
534
|
+
contractAddress: info.contractAddress,
|
|
535
|
+
functionName: info.functionName,
|
|
536
|
+
outputHandle: info.outputHandle,
|
|
537
|
+
duration: info.completedAt - info.submittedAt,
|
|
538
|
+
warning: 'Simulated result - SDK integration pending',
|
|
539
|
+
},
|
|
540
|
+
}
|
|
541
|
+
}
|
|
542
|
+
|
|
543
|
+
// ─── Private Methods ─────────────────────────────────────────────────────────
|
|
544
|
+
|
|
545
|
+
/**
|
|
546
|
+
* Estimate cost for an FHE computation
|
|
547
|
+
*/
|
|
548
|
+
private estimateFHECost(params: ComputationParams): bigint {
|
|
549
|
+
let cost = BASE_FHE_COST_WEI
|
|
550
|
+
|
|
551
|
+
// More inputs = higher cost
|
|
552
|
+
const inputCost = BigInt(params.encryptedInputs.length) * COST_PER_ENCRYPTED_INPUT_WEI
|
|
553
|
+
cost += inputCost
|
|
554
|
+
|
|
555
|
+
// Larger inputs = higher cost (per KB)
|
|
556
|
+
const totalInputSize = params.encryptedInputs.reduce(
|
|
557
|
+
(sum, input) => sum + input.length,
|
|
558
|
+
0
|
|
559
|
+
)
|
|
560
|
+
const sizeCostPerKB = BigInt('100000000000000') // 0.0001 ETH per KB
|
|
561
|
+
const sizeCost = BigInt(Math.ceil(totalInputSize / 1000)) * sizeCostPerKB
|
|
562
|
+
cost += sizeCost
|
|
563
|
+
|
|
564
|
+
return cost
|
|
565
|
+
}
|
|
566
|
+
|
|
567
|
+
/**
|
|
568
|
+
* Generate a unique computation ID
|
|
569
|
+
*/
|
|
570
|
+
private generateComputationId(): string {
|
|
571
|
+
const timestamp = Date.now().toString(36)
|
|
572
|
+
const random = Math.random().toString(36).slice(2, 10)
|
|
573
|
+
return `inco_${timestamp}_${random}`
|
|
574
|
+
}
|
|
575
|
+
|
|
576
|
+
/**
|
|
577
|
+
* Simulate encryption (for testing without SDK)
|
|
578
|
+
*/
|
|
579
|
+
private simulateEncryption(
|
|
580
|
+
value: bigint | boolean | string,
|
|
581
|
+
type: EncryptedType
|
|
582
|
+
): Uint8Array {
|
|
583
|
+
// Create a deterministic but fake ciphertext
|
|
584
|
+
const encoder = new TextEncoder()
|
|
585
|
+
const valueStr = String(value)
|
|
586
|
+
const combined = `${type}:${valueStr}:${Date.now()}`
|
|
587
|
+
return encoder.encode(combined)
|
|
588
|
+
}
|
|
589
|
+
|
|
590
|
+
/**
|
|
591
|
+
* Clear computation cache
|
|
592
|
+
*/
|
|
593
|
+
clearComputationCache(): void {
|
|
594
|
+
this.computationCache.clear()
|
|
595
|
+
}
|
|
596
|
+
|
|
597
|
+
/**
|
|
598
|
+
* Get cached computation count
|
|
599
|
+
*/
|
|
600
|
+
getCachedComputationCount(): number {
|
|
601
|
+
return this.computationCache.size
|
|
602
|
+
}
|
|
603
|
+
|
|
604
|
+
/**
|
|
605
|
+
* Get current configuration (deeply frozen copy)
|
|
606
|
+
*/
|
|
607
|
+
getConfig(): Readonly<Omit<IncoBackendConfig, 'client'>> {
|
|
608
|
+
return deepFreeze({
|
|
609
|
+
rpcUrl: this.config.rpcUrl,
|
|
610
|
+
network: this.config.network,
|
|
611
|
+
chainId: this.config.chainId,
|
|
612
|
+
product: this.config.product,
|
|
613
|
+
timeout: this.config.timeout,
|
|
614
|
+
})
|
|
615
|
+
}
|
|
616
|
+
}
|