fix: check self-call

This commit is contained in:
Alex 2025-04-11 15:33:54 +03:00
parent d9670b314a
commit 0a753b36d0
22 changed files with 36 additions and 14 deletions

View File

@ -9,6 +9,7 @@ export const constructAddWordUiCommand = ({ word, contractId }: { word: string;
methodName: PUBLIC_METHODS.ADD_WORD,
methodArgs: [[word] satisfies AddWordArgs],
cost: FEE.ADD_WORD,
withQueue: false,
}),
]);
};

View File

@ -4,6 +4,8 @@ import { readOp, storeOp } from 'cwait';
import { TypedClaim } from '../../../lib/dist/shared/types';
import { AddWordArgs, createWordKey, WordClaimBody } from '../offchain/shared';
import { extraLogic } from './extraLogic';
function hashCode(str: string): string {
let hash = 0;
for (let i = 0, len = str.length; i < len; i++) {
@ -21,7 +23,7 @@ export const addWord = async (...[word]: AddWordArgs) => {
await storeOp(constructClaim(createWordKey(id), { word }, '0x0'));
const wordClaim = await readOp<TypedClaim<WordClaimBody>>(createWordKey(id));
const wordClaim = await extraLogic(id);
const newWord1 = (wordClaim?.body.word ?? '') + '!';
const newId1 = hashCode(newWord1);

View File

@ -1,20 +1,16 @@
import { SELF_REGISTER_HANDLER_NAME, ContractHandlers as CKContractHandlers } from '@coinweb/contract-kit';
import { selfRegisterHandler } from '@coinweb/self-register';
import { addMethodHandler, ContractHandlers, executeHandler, executor, MethodCallback, selfCallWrapper } from 'cwait';
import { addMethodHandler, ContractHandlers, executeHandler, executor } from 'cwait';
import { queue } from 'lib/onchain';
import { PUBLIC_METHODS } from '../offchain/shared';
import { addWord } from './addWord';
const addWrappers = (method: MethodCallback): MethodCallback => {
return selfCallWrapper(method);
};
const createModule = (): ContractHandlers => {
const module: ContractHandlers = { handlers: {} };
addMethodHandler(module, PUBLIC_METHODS.ADD_WORD, addWrappers(executor(addWord)));
addMethodHandler(module, PUBLIC_METHODS.ADD_WORD, executor(addWord));
addMethodHandler(module, SELF_REGISTER_HANDLER_NAME, selfRegisterHandler);

View File

@ -0,0 +1,9 @@
import { readOp, TypedClaim } from 'cwait';
import { createWordKey, WordClaimBody } from '../offchain';
export const extraLogic = async (id: string) => {
const result = await readOp<TypedClaim<WordClaimBody>>(createWordKey(id));
return result;
};

View File

@ -6,14 +6,15 @@ import {
constructContinueTx,
constructContractRef,
constructDataUnverified,
isSelfCall,
} from '@coinweb/contract-kit';
import { getCallParameters, queue } from 'lib/onchain';
import { ResolvedOp, Task } from '../types';
import { context, getRawContext, setRawContext } from './context';
import { getAwaitedOps } from './promisifedOps/awaited';
import { pushResolvedOp } from './promisifedOps/resolved';
import { getAwaitedOps } from './promisifiedOps/awaited';
import { pushResolvedOp } from './promisifiedOps/resolved';
let abortExecution: ((result: boolean) => void) | null = null;
@ -29,7 +30,12 @@ const handleState = () => {
pushResolvedOp(allResolvedOps);
return { args: initialArgs, methodName: methodArgs[0] as string, ops: allResolvedOps };
return {
args: initialArgs,
methodName: methodArgs[0] as string,
ops: allResolvedOps,
isChildCall: previousOps.length > 0,
};
};
export const executor =
@ -39,7 +45,11 @@ export const executor =
console.log('executor-start');
setRawContext(ctx);
const { args, methodName, ops } = handleState();
const { args, methodName, ops, isChildCall } = handleState();
if (isChildCall && !isSelfCall(ctx)) {
throw new Error('Only contract itself can call it');
}
const execution = new Promise<boolean>((resolve, reject) => {
abortExecution = resolve;

View File

@ -1,4 +1,4 @@
import { markOpBatch } from './promisifedOps/awaited';
import { markOpBatch } from './promisifiedOps/awaited';
export const opMarker = Symbol('opMarker');

View File

@ -1,3 +1,3 @@
export * from './context';
export * from './executor';
export * from './promisifedOps';
export * from './promisifiedOps';

View File

@ -0,0 +1,2 @@
export * from './jumpTx';
export * from './tx';

View File

@ -0,0 +1 @@
export const jumpTx = () => null;

View File

@ -0,0 +1 @@
export const tx = () => null;

View File

@ -1,4 +1,4 @@
VITE_API_URL='https://api-cloud.coinweb.io/wallet'
VITE_EXPLORER_URL='https://explorer.coinweb.io'
VITE_CONTRACT_ADDRESS="0x049eedf15331c4c39155b286aa6be8ce50a0bcab15eac345913fd084a1f32bdd"
VITE_CONTRACT_ADDRESS="0xd0b364db47a35710320a815fbcae80435ae83e0a1730e70529f1b0b2a6b7bfd5"