@qorechain/evm 0.4.0 → 0.5.0
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 +201 -0
- package/dist/index.cjs +77 -4
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +130 -5
- package/dist/index.d.ts +130 -5
- package/dist/index.js +72 -4
- package/dist/index.js.map +1 -1
- package/package.json +9 -9
package/dist/index.d.ts
CHANGED
|
@@ -285,6 +285,18 @@ declare const PRECOMPILE_ADDRESSES: {
|
|
|
285
285
|
/** Consensus parameters (`IQoreConsensus.rlConsensusParams`). */
|
|
286
286
|
readonly rlConsensusParams: "0x0000000000000000000000000000000000000C01";
|
|
287
287
|
};
|
|
288
|
+
/**
|
|
289
|
+
* Fixed address of the AI transaction risk-score precompile (`IQoreAI.aiRiskScore`).
|
|
290
|
+
*
|
|
291
|
+
* Convenience alias of {@link PRECOMPILE_ADDRESSES.aiRiskScore}.
|
|
292
|
+
*/
|
|
293
|
+
declare const AI_RISK_SCORE_ADDRESS: Address;
|
|
294
|
+
/**
|
|
295
|
+
* Fixed address of the AI anomaly-check precompile (`IQoreAI.aiAnomalyCheck`).
|
|
296
|
+
*
|
|
297
|
+
* Convenience alias of {@link PRECOMPILE_ADDRESSES.aiAnomalyCheck}.
|
|
298
|
+
*/
|
|
299
|
+
declare const AI_ANOMALY_CHECK_ADDRESS: Address;
|
|
288
300
|
/** Arguments for {@link pqcVerify}. */
|
|
289
301
|
interface PqcVerifyArgs {
|
|
290
302
|
pubkey: Hex;
|
|
@@ -307,7 +319,7 @@ interface AiRiskScore {
|
|
|
307
319
|
level: number;
|
|
308
320
|
}
|
|
309
321
|
/** Compute an on-chain risk score for raw transaction data. */
|
|
310
|
-
declare function aiRiskScore(client: PublicClient, txData: Hex): Promise<AiRiskScore>;
|
|
322
|
+
declare function aiRiskScore$1(client: PublicClient, txData: Hex | Uint8Array): Promise<AiRiskScore>;
|
|
311
323
|
/** Arguments for {@link aiAnomalyCheck}. */
|
|
312
324
|
interface AiAnomalyCheckArgs {
|
|
313
325
|
sender: Address;
|
|
@@ -319,7 +331,7 @@ interface AiAnomalyCheck {
|
|
|
319
331
|
flagged: boolean;
|
|
320
332
|
}
|
|
321
333
|
/** Check whether a (sender, amount) pair is anomalous. */
|
|
322
|
-
declare function aiAnomalyCheck(client: PublicClient, { sender, amount }: AiAnomalyCheckArgs): Promise<AiAnomalyCheck>;
|
|
334
|
+
declare function aiAnomalyCheck$1(client: PublicClient, { sender, amount }: AiAnomalyCheckArgs): Promise<AiAnomalyCheck>;
|
|
323
335
|
/** Result of {@link rlConsensusParams}. */
|
|
324
336
|
interface ConsensusParams {
|
|
325
337
|
blockTime: bigint;
|
|
@@ -333,9 +345,122 @@ declare function rlConsensusParams(client: PublicClient): Promise<ConsensusParam
|
|
|
333
345
|
declare const precompiles: {
|
|
334
346
|
readonly pqcVerify: typeof pqcVerify;
|
|
335
347
|
readonly pqcKeyStatus: typeof pqcKeyStatus;
|
|
348
|
+
readonly aiRiskScore: typeof aiRiskScore$1;
|
|
349
|
+
readonly aiAnomalyCheck: typeof aiAnomalyCheck$1;
|
|
350
|
+
readonly rlConsensusParams: typeof rlConsensusParams;
|
|
351
|
+
};
|
|
352
|
+
|
|
353
|
+
/**
|
|
354
|
+
* AI pre-flight risk scoring — a QoreChain-unique capability.
|
|
355
|
+
*
|
|
356
|
+
* QoreChain ships an on-chain inference engine (QCAI) exposed to the EVM through
|
|
357
|
+
* two read-only precompiles. Any dApp can score a transaction *before* it is
|
|
358
|
+
* broadcast, using nothing but `eth_call`s, and combine that with a gas estimate
|
|
359
|
+
* to make an informed, policy-driven decision off-chain.
|
|
360
|
+
*
|
|
361
|
+
* - {@link aiRiskScore} — a model-derived risk score + bucketed level for raw
|
|
362
|
+
* transaction calldata (precompile `0x..0B01`).
|
|
363
|
+
* - {@link aiAnomalyCheck} — an anomaly score + boolean flag for a
|
|
364
|
+
* `(sender, amount)` pair (precompile `0x..0B02`).
|
|
365
|
+
* - {@link simulateWithRiskScore} — one call that bundles a gas estimate, a risk
|
|
366
|
+
* score, and an anomaly check into a single advisory verdict.
|
|
367
|
+
*
|
|
368
|
+
* These bindings re-use the precompile address constants and ABIs in
|
|
369
|
+
* `./precompiles` / `./abi`. They expose the *high-level, positional-argument*
|
|
370
|
+
* surface the SDK documents; the namespaced `precompiles.*` bindings remain the
|
|
371
|
+
* low-level form.
|
|
372
|
+
*
|
|
373
|
+
* Availability note: on a default or community node these precompiles may return
|
|
374
|
+
* a "not available" error; they are available on QoreChain network nodes. Treat a
|
|
375
|
+
* thrown error from any of these helpers as "feature not present on this node".
|
|
376
|
+
*/
|
|
377
|
+
|
|
378
|
+
/**
|
|
379
|
+
* Risk level at which {@link simulateWithRiskScore} stops reporting a transaction
|
|
380
|
+
* as `safe`. The chain's example policy uses `require(level < 3)`, so a level of
|
|
381
|
+
* `3` or above is treated as unsafe here.
|
|
382
|
+
*
|
|
383
|
+
* This threshold is **advisory**. The precompiles do not block anything on their
|
|
384
|
+
* own — an off-chain dApp should pick and enforce its own policy (and a contract
|
|
385
|
+
* can enforce its own on-chain `require`). It is exported so callers can reference
|
|
386
|
+
* the same default the SDK documents.
|
|
387
|
+
*/
|
|
388
|
+
declare const RISK_LEVEL_UNSAFE_THRESHOLD = 3;
|
|
389
|
+
/**
|
|
390
|
+
* Compute an on-chain risk score for raw transaction calldata.
|
|
391
|
+
*
|
|
392
|
+
* Issues an `eth_call` against the AI risk-score precompile, encoding
|
|
393
|
+
* `aiRiskScore(bytes)` and decoding the `(uint256 score, uint8 level)` tuple.
|
|
394
|
+
* Higher `level` means riskier (the chain's example policy uses `level < 3`).
|
|
395
|
+
*
|
|
396
|
+
* @param client - A viem public client bound to a QoreChain EVM endpoint.
|
|
397
|
+
* @param txData - The transaction calldata, as hex or raw bytes.
|
|
398
|
+
*/
|
|
399
|
+
declare function aiRiskScore(client: PublicClient, txData: Hex | Uint8Array): Promise<AiRiskScore>;
|
|
400
|
+
/**
|
|
401
|
+
* Check whether a `(sender, amount)` pair is anomalous.
|
|
402
|
+
*
|
|
403
|
+
* Issues an `eth_call` against the AI anomaly-check precompile, encoding
|
|
404
|
+
* `aiAnomalyCheck(address,uint256)` and decoding the
|
|
405
|
+
* `(uint256 anomalyScore, bool flagged)` tuple.
|
|
406
|
+
*
|
|
407
|
+
* @param client - A viem public client bound to a QoreChain EVM endpoint.
|
|
408
|
+
* @param sender - The transaction sender to evaluate.
|
|
409
|
+
* @param amount - The transferred value (in wei) to evaluate.
|
|
410
|
+
*/
|
|
411
|
+
declare function aiAnomalyCheck(client: PublicClient, sender: Address, amount: bigint): Promise<AiAnomalyCheck>;
|
|
412
|
+
/** A transaction shape for {@link simulateWithRiskScore}. */
|
|
413
|
+
interface PreflightTx {
|
|
414
|
+
/** The sender address (used for the anomaly check and gas estimate). */
|
|
415
|
+
from: Address;
|
|
416
|
+
/** The destination address (optional for contract-creation txs). */
|
|
417
|
+
to?: Address;
|
|
418
|
+
/** The transaction calldata, if any. */
|
|
419
|
+
data?: Hex;
|
|
420
|
+
/** The value to transfer, in wei. Defaults to `0`. */
|
|
421
|
+
value?: bigint;
|
|
422
|
+
}
|
|
423
|
+
/** The combined result of {@link simulateWithRiskScore}. */
|
|
424
|
+
interface PreflightResult {
|
|
425
|
+
/** Estimated gas for the transaction (`eth_estimateGas`). */
|
|
426
|
+
gas: bigint;
|
|
427
|
+
/** On-chain risk score for the transaction calldata. */
|
|
428
|
+
risk: AiRiskScore;
|
|
429
|
+
/** On-chain anomaly check for `(from, value)`. */
|
|
430
|
+
anomaly: AiAnomalyCheck;
|
|
431
|
+
/**
|
|
432
|
+
* Advisory verdict: `risk.level < RISK_LEVEL_UNSAFE_THRESHOLD && !anomaly.flagged`.
|
|
433
|
+
*
|
|
434
|
+
* This is a convenience signal only; enforce your own policy (see
|
|
435
|
+
* {@link RISK_LEVEL_UNSAFE_THRESHOLD}).
|
|
436
|
+
*/
|
|
437
|
+
safe: boolean;
|
|
438
|
+
}
|
|
439
|
+
/**
|
|
440
|
+
* Run an AI pre-flight on a transaction: estimate gas, score its risk, and check
|
|
441
|
+
* for anomalies — all read-only, before anything is signed or broadcast.
|
|
442
|
+
*
|
|
443
|
+
* This is a first-in-industry capability: the risk model runs *on-chain* and is
|
|
444
|
+
* reachable from any dApp via plain `eth_call`/`eth_estimateGas`. The returned
|
|
445
|
+
* `safe` flag is **advisory** — off-chain dApps should set and enforce their own
|
|
446
|
+
* policy (and contracts can `require` on the level on-chain).
|
|
447
|
+
*
|
|
448
|
+
* - `gas` — `eth_estimateGas` for the transaction.
|
|
449
|
+
* - `risk` — {@link aiRiskScore} over `tx.data` (or, when absent, the deployed
|
|
450
|
+
* bytecode at `tx.to`, so a plain transfer to a contract is still scored).
|
|
451
|
+
* - `anomaly` — {@link aiAnomalyCheck} over `(tx.from, tx.value ?? 0)`.
|
|
452
|
+
* - `safe` — `risk.level < {@link RISK_LEVEL_UNSAFE_THRESHOLD} && !anomaly.flagged`.
|
|
453
|
+
*
|
|
454
|
+
* @param client - A viem public client bound to a QoreChain EVM endpoint.
|
|
455
|
+
* @param tx - The transaction to evaluate.
|
|
456
|
+
*/
|
|
457
|
+
declare function simulateWithRiskScore(client: PublicClient, tx: PreflightTx): Promise<PreflightResult>;
|
|
458
|
+
/** Namespaced AI pre-flight helpers. */
|
|
459
|
+
declare const ai: {
|
|
336
460
|
readonly aiRiskScore: typeof aiRiskScore;
|
|
337
461
|
readonly aiAnomalyCheck: typeof aiAnomalyCheck;
|
|
338
|
-
readonly
|
|
462
|
+
readonly simulateWithRiskScore: typeof simulateWithRiskScore;
|
|
463
|
+
readonly RISK_LEVEL_UNSAFE_THRESHOLD: 3;
|
|
339
464
|
};
|
|
340
465
|
|
|
341
466
|
/**
|
|
@@ -1140,6 +1265,6 @@ declare function watchPendingTransactions(client: PublicClient, args: WatchPendi
|
|
|
1140
1265
|
* typed bindings for QoreChain's EVM precompiles.
|
|
1141
1266
|
*/
|
|
1142
1267
|
/** Package version. */
|
|
1143
|
-
declare const VERSION = "0.
|
|
1268
|
+
declare const VERSION = "0.5.0";
|
|
1144
1269
|
|
|
1145
|
-
export { type AiAnomalyCheck, type AiAnomalyCheckArgs, type AiRiskScore, type ConsensusParams, type CreateEvmClientOptions, type CreateEvmSubscriptionClientOptions, type DecodedEvmError, type DeployContractArgs, ERC1155_ABI, ERC20_ABI, ERC721_ABI, type Eip1193Provider, type Eip1559Fees, type Eip6963ProviderDetail, type Erc20Metadata, type Erc721Metadata, type EvmClient, type EvmEndpoints, type EvmNetworkInfo, type EvmWalletConnection, type EvmWsEndpoints, type GetEvmWalletClientOptions, IQORE_AI_ABI, IQORE_CONSENSUS_ABI, IQORE_PQC_ABI, PRECOMPILE_ADDRESSES, type PqcKeyStatus, type PqcVerifyArgs, type Unwatch, VERSION, addQoreChainNetwork, aiAnomalyCheck, aiRiskScore, allowance, approve$1 as approve, balanceOf$2 as balanceOf, createEvmClient, createEvmSubscriptionClient, decodeEvmError, deployContract, discoverEvmProviders, erc1155, erc20, erc721, estimateEip1559Fees, evmAccountFromPrivateKey, fees, gasPrice, getEvmWalletClient, metadata$1 as metadata, pqcKeyStatus, pqcVerify, precompiles, readContract, requestAccounts, rlConsensusParams, switchChain, toHexChainId, transfer, watchBlocks, watchContractEvent, watchEvent, watchPendingTransactions, writeContract };
|
|
1270
|
+
export { AI_ANOMALY_CHECK_ADDRESS, AI_RISK_SCORE_ADDRESS, type AiAnomalyCheck, type AiAnomalyCheckArgs, type AiRiskScore, type ConsensusParams, type CreateEvmClientOptions, type CreateEvmSubscriptionClientOptions, type DecodedEvmError, type DeployContractArgs, ERC1155_ABI, ERC20_ABI, ERC721_ABI, type Eip1193Provider, type Eip1559Fees, type Eip6963ProviderDetail, type Erc20Metadata, type Erc721Metadata, type EvmClient, type EvmEndpoints, type EvmNetworkInfo, type EvmWalletConnection, type EvmWsEndpoints, type GetEvmWalletClientOptions, IQORE_AI_ABI, IQORE_CONSENSUS_ABI, IQORE_PQC_ABI, PRECOMPILE_ADDRESSES, type PqcKeyStatus, type PqcVerifyArgs, type PreflightResult, type PreflightTx, RISK_LEVEL_UNSAFE_THRESHOLD, type Unwatch, VERSION, addQoreChainNetwork, ai, aiAnomalyCheck, aiRiskScore, allowance, approve$1 as approve, balanceOf$2 as balanceOf, createEvmClient, createEvmSubscriptionClient, decodeEvmError, deployContract, discoverEvmProviders, erc1155, erc20, erc721, estimateEip1559Fees, evmAccountFromPrivateKey, fees, gasPrice, getEvmWalletClient, metadata$1 as metadata, pqcKeyStatus, pqcVerify, precompiles, readContract, requestAccounts, rlConsensusParams, simulateWithRiskScore, switchChain, toHexChainId, transfer, watchBlocks, watchContractEvent, watchEvent, watchPendingTransactions, writeContract };
|
package/dist/index.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { http, createPublicClient, defineChain, createWalletClient, custom, BaseError, ContractFunctionRevertedError, decodeErrorResult,
|
|
1
|
+
import { http, createPublicClient, defineChain, encodeFunctionData, getAddress, decodeFunctionResult, createWalletClient, custom, BaseError, ContractFunctionRevertedError, decodeErrorResult, toHex, webSocket } from 'viem';
|
|
2
2
|
import { privateKeyToAccount } from 'viem/accounts';
|
|
3
3
|
|
|
4
4
|
// src/client.ts
|
|
@@ -724,6 +724,11 @@ var PRECOMPILE_ADDRESSES = {
|
|
|
724
724
|
/** Consensus parameters (`IQoreConsensus.rlConsensusParams`). */
|
|
725
725
|
rlConsensusParams: "0x0000000000000000000000000000000000000C01"
|
|
726
726
|
};
|
|
727
|
+
var AI_RISK_SCORE_ADDRESS = PRECOMPILE_ADDRESSES.aiRiskScore;
|
|
728
|
+
var AI_ANOMALY_CHECK_ADDRESS = PRECOMPILE_ADDRESSES.aiAnomalyCheck;
|
|
729
|
+
function toHexData(data) {
|
|
730
|
+
return typeof data === "string" ? data : toHex(data);
|
|
731
|
+
}
|
|
727
732
|
var addr = (a) => getAddress(a);
|
|
728
733
|
function pqcVerify(client, { pubkey, signature, message }) {
|
|
729
734
|
return client.readContract({
|
|
@@ -747,7 +752,7 @@ async function aiRiskScore(client, txData) {
|
|
|
747
752
|
address: addr(PRECOMPILE_ADDRESSES.aiRiskScore),
|
|
748
753
|
abi: IQORE_AI_ABI,
|
|
749
754
|
functionName: "aiRiskScore",
|
|
750
|
-
args: [txData]
|
|
755
|
+
args: [toHexData(txData)]
|
|
751
756
|
});
|
|
752
757
|
return { score, level };
|
|
753
758
|
}
|
|
@@ -775,6 +780,69 @@ var precompiles = {
|
|
|
775
780
|
aiAnomalyCheck,
|
|
776
781
|
rlConsensusParams
|
|
777
782
|
};
|
|
783
|
+
var RISK_LEVEL_UNSAFE_THRESHOLD = 3;
|
|
784
|
+
function toHexData2(data) {
|
|
785
|
+
return typeof data === "string" ? data : toHex(data);
|
|
786
|
+
}
|
|
787
|
+
async function aiRiskScore2(client, txData) {
|
|
788
|
+
const data = encodeFunctionData({
|
|
789
|
+
abi: IQORE_AI_ABI,
|
|
790
|
+
functionName: "aiRiskScore",
|
|
791
|
+
args: [toHexData2(txData)]
|
|
792
|
+
});
|
|
793
|
+
const result = await client.call({
|
|
794
|
+
to: getAddress(AI_RISK_SCORE_ADDRESS),
|
|
795
|
+
data
|
|
796
|
+
});
|
|
797
|
+
const [score, level] = decodeFunctionResult({
|
|
798
|
+
abi: IQORE_AI_ABI,
|
|
799
|
+
functionName: "aiRiskScore",
|
|
800
|
+
data: result.data ?? "0x"
|
|
801
|
+
});
|
|
802
|
+
return { score, level };
|
|
803
|
+
}
|
|
804
|
+
async function aiAnomalyCheck2(client, sender, amount) {
|
|
805
|
+
const data = encodeFunctionData({
|
|
806
|
+
abi: IQORE_AI_ABI,
|
|
807
|
+
functionName: "aiAnomalyCheck",
|
|
808
|
+
args: [sender, amount]
|
|
809
|
+
});
|
|
810
|
+
const result = await client.call({
|
|
811
|
+
to: getAddress(AI_ANOMALY_CHECK_ADDRESS),
|
|
812
|
+
data
|
|
813
|
+
});
|
|
814
|
+
const [anomalyScore, flagged] = decodeFunctionResult({
|
|
815
|
+
abi: IQORE_AI_ABI,
|
|
816
|
+
functionName: "aiAnomalyCheck",
|
|
817
|
+
data: result.data ?? "0x"
|
|
818
|
+
});
|
|
819
|
+
return { anomalyScore, flagged };
|
|
820
|
+
}
|
|
821
|
+
async function simulateWithRiskScore(client, tx) {
|
|
822
|
+
const value = tx.value ?? 0n;
|
|
823
|
+
let riskData = tx.data ?? "0x";
|
|
824
|
+
if ((riskData === "0x" || riskData.length <= 2) && tx.to) {
|
|
825
|
+
riskData = await client.getCode({ address: tx.to }).then((c) => c ?? "0x");
|
|
826
|
+
}
|
|
827
|
+
const [gas, risk, anomaly] = await Promise.all([
|
|
828
|
+
client.estimateGas({
|
|
829
|
+
account: tx.from,
|
|
830
|
+
to: tx.to,
|
|
831
|
+
data: tx.data,
|
|
832
|
+
value
|
|
833
|
+
}),
|
|
834
|
+
aiRiskScore2(client, riskData),
|
|
835
|
+
aiAnomalyCheck2(client, tx.from, value)
|
|
836
|
+
]);
|
|
837
|
+
const safe = risk.level < RISK_LEVEL_UNSAFE_THRESHOLD && !anomaly.flagged;
|
|
838
|
+
return { gas, risk, anomaly, safe };
|
|
839
|
+
}
|
|
840
|
+
var ai = {
|
|
841
|
+
aiRiskScore: aiRiskScore2,
|
|
842
|
+
aiAnomalyCheck: aiAnomalyCheck2,
|
|
843
|
+
simulateWithRiskScore,
|
|
844
|
+
RISK_LEVEL_UNSAFE_THRESHOLD
|
|
845
|
+
};
|
|
778
846
|
function resolveProvider(provider) {
|
|
779
847
|
if (provider) return provider;
|
|
780
848
|
if (typeof window === "undefined") {
|
|
@@ -960,8 +1028,8 @@ function watchPendingTransactions(client, args) {
|
|
|
960
1028
|
}
|
|
961
1029
|
|
|
962
1030
|
// src/index.ts
|
|
963
|
-
var VERSION = "0.
|
|
1031
|
+
var VERSION = "0.5.0";
|
|
964
1032
|
|
|
965
|
-
export { ERC1155_ABI, ERC20_ABI, ERC721_ABI, IQORE_AI_ABI, IQORE_CONSENSUS_ABI, IQORE_PQC_ABI, PRECOMPILE_ADDRESSES, VERSION, addQoreChainNetwork, aiAnomalyCheck, aiRiskScore, allowance, approve, balanceOf, createEvmClient, createEvmSubscriptionClient, decodeEvmError, deployContract, discoverEvmProviders, erc1155, erc20, erc721, estimateEip1559Fees, evmAccountFromPrivateKey, fees, gasPrice, getEvmWalletClient, metadata, pqcKeyStatus, pqcVerify, precompiles, readContract, requestAccounts, rlConsensusParams, switchChain, toHexChainId, transfer, watchBlocks, watchContractEvent, watchEvent, watchPendingTransactions, writeContract };
|
|
1033
|
+
export { AI_ANOMALY_CHECK_ADDRESS, AI_RISK_SCORE_ADDRESS, ERC1155_ABI, ERC20_ABI, ERC721_ABI, IQORE_AI_ABI, IQORE_CONSENSUS_ABI, IQORE_PQC_ABI, PRECOMPILE_ADDRESSES, RISK_LEVEL_UNSAFE_THRESHOLD, VERSION, addQoreChainNetwork, ai, aiAnomalyCheck2 as aiAnomalyCheck, aiRiskScore2 as aiRiskScore, allowance, approve, balanceOf, createEvmClient, createEvmSubscriptionClient, decodeEvmError, deployContract, discoverEvmProviders, erc1155, erc20, erc721, estimateEip1559Fees, evmAccountFromPrivateKey, fees, gasPrice, getEvmWalletClient, metadata, pqcKeyStatus, pqcVerify, precompiles, readContract, requestAccounts, rlConsensusParams, simulateWithRiskScore, switchChain, toHexChainId, transfer, watchBlocks, watchContractEvent, watchEvent, watchPendingTransactions, writeContract };
|
|
966
1034
|
//# sourceMappingURL=index.js.map
|
|
967
1035
|
//# sourceMappingURL=index.js.map
|