Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: sip30 stx call contract #6060

Draft
wants to merge 1 commit into
base: dev
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,7 @@
"@leather.io/eslint-config": "0.7.0",
"@leather.io/panda-preset": "0.8.0",
"@leather.io/prettier-config": "0.6.0",
"@leather.io/rpc": "2.4.0",
"@leather.io/rpc": "2.5.2",
"@ls-lint/ls-lint": "2.2.3",
"@mdx-js/loader": "3.0.0",
"@pandacss/dev": "0.46.1",
Expand Down
25 changes: 23 additions & 2 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import * as yup from 'yup';
import { HIGH_FEE_WARNING_LEARN_MORE_URL_STX } from '@leather.io/constants';
import { FeeTypes } from '@leather.io/models';
import {
defaultStacksFees,
useCalculateStacksTxFees,
useNextNonce,
useStxCryptoAssetBalance,
Expand Down Expand Up @@ -40,7 +41,7 @@ import { MinimalErrorMessage } from './minimal-error-message';
import { StacksTxSubmitAction } from './submit-action';

interface StacksTransactionSignerProps {
stacksTransaction: StacksTransaction;
stacksTransaction?: StacksTransaction;
disableFeeSelection?: boolean;
disableNonceSelection?: boolean;
isMultisig: boolean;
Expand Down Expand Up @@ -123,7 +124,7 @@ export function StacksTransactionSigner({

{!isNonceAlreadySet && <NonceSetter />}
<FeeForm
fees={stxFees}
fees={stxFees ?? defaultStacksFees}
sbtcSponsorshipEligibility={{ isEligible: false }}
defaultFeeValue={Number(transactionRequest?.fee || 0)}
disableFeeSelection={disableFeeSelection}
Expand Down
23 changes: 23 additions & 0 deletions src/app/pages/rpc-stx-call-contract/rpc-stx-call-contract.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { StacksHighFeeWarningContainer } from '@app/features/stacks-high-fee-warning/stacks-high-fee-warning-container';
import { StacksTransactionSigner } from '@app/features/stacks-transaction-request/stacks-transaction-signer';
import { useBreakOnNonCompliantEntity } from '@app/query/common/compliance-checker/compliance-checker.query';

import { useRpcStxCallContract } from './use-rpc-stx-call-contract';

export function RpcStxCallContract() {
const { onSignStacksTransaction, onCancel, stacksTransaction, txSender } =
useRpcStxCallContract();

useBreakOnNonCompliantEntity(txSender);

return (
<StacksHighFeeWarningContainer>
<StacksTransactionSigner
onSignStacksTransaction={onSignStacksTransaction}
onCancel={onCancel}
isMultisig={false}
stacksTransaction={stacksTransaction}
/>
</StacksHighFeeWarningContainer>
);
}
107 changes: 107 additions & 0 deletions src/app/pages/rpc-stx-call-contract/use-rpc-stx-call-contract.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
import { useMemo } from 'react';
import { useAsync } from 'react-async-hook';

import { bytesToHex } from '@stacks/common';
import type { TransactionPayload } from '@stacks/connect';

import { RpcErrorCode } from '@leather.io/rpc';

import { logger } from '@shared/logger';
import { makeRpcErrorResponse, makeRpcSuccessResponse } from '@shared/rpc/rpc-methods';
import { closeWindow } from '@shared/utils';
import { getPayloadFromToken } from '@shared/utils/requests';

import { useDefaultRequestParams } from '@app/common/hooks/use-default-request-search-params';
import { initialSearchParams } from '@app/common/initial-search-params';
import {
type GenerateUnsignedTransactionOptions,
generateUnsignedTransaction,
} from '@app/common/transactions/stacks/generate-unsigned-txs';
import { getTxSenderAddress } from '@app/common/transactions/stacks/transaction.utils';
import { useCurrentStacksAccount } from '@app/store/accounts/blockchain/stacks/stacks-account.hooks';
import { useSignStacksTransaction } from '@app/store/transactions/transaction.hooks';

function useRpcStxCallContractParams() {
const { origin, tabId } = useDefaultRequestParams();
const requestId = initialSearchParams.get('requestId');
const request = initialSearchParams.get('request');

if (!origin || !request || !requestId) throw new Error('Invalid params');

return useMemo(
() => ({
origin,
tabId: tabId ?? 0,
request: getPayloadFromToken(request),
requestId,
}),
[origin, tabId, request, requestId]
);
}

function useUnsignedStacksTransactionFromRequest(request: TransactionPayload) {
const account = useCurrentStacksAccount();

const tx = useAsync(async () => {
if (!account) return;

const options: GenerateUnsignedTransactionOptions = {
publicKey: account.stxPublicKey,
txData: request,
fee: request.fee ?? 0,
nonce: request.nonce,
};
return generateUnsignedTransaction(options);
}, [account]);

return tx.result;
}

export function useRpcStxCallContract() {
const { origin, request, requestId, tabId } = useRpcStxCallContractParams();
const signStacksTx = useSignStacksTransaction();
const stacksTransaction = useUnsignedStacksTransactionFromRequest(request);

return {
origin,
txSender: stacksTransaction ? getTxSenderAddress(stacksTransaction) : '',
stacksTransaction,
async onSignStacksTransaction(fee: number, nonce: number) {
if (!stacksTransaction) {
return logger.error('No stacks transaction to sign');
}

stacksTransaction.setFee(fee);
stacksTransaction.setNonce(nonce);

const signedTransaction = await signStacksTx(stacksTransaction);
if (!signedTransaction) {
throw new Error('Error signing stacks transaction');
}

chrome.tabs.sendMessage(
tabId,
makeRpcSuccessResponse('stx_callContract', {
id: requestId,
result: {
txid: '', // Broadcast transaction?
transaction: bytesToHex(signedTransaction.serialize()),
} as any, // Fix this
})
);
closeWindow();
},
onCancel() {
chrome.tabs.sendMessage(
tabId,
makeRpcErrorResponse('stx_callContract', {
id: requestId,
error: {
message: 'User denied signing stacks transaction',
code: RpcErrorCode.USER_REJECTION,
},
})
);
},
};
}
13 changes: 13 additions & 0 deletions src/app/routes/rpc-routes.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import { RpcSignPsbt } from '@app/pages/rpc-sign-psbt/rpc-sign-psbt';
import { RpcSignPsbtSummary } from '@app/pages/rpc-sign-psbt/rpc-sign-psbt-summary';
import { RpcStacksMessageSigning } from '@app/pages/rpc-sign-stacks-message/rpc-sign-stacks-message';
import { RpcSignStacksTransaction } from '@app/pages/rpc-sign-stacks-transaction/rpc-sign-stacks-transaction';
import { RpcStxCallContract } from '@app/pages/rpc-stx-call-contract/rpc-stx-call-contract';
import { AccountGate } from '@app/routes/account-gate';

import { SuspenseLoadingSpinner } from './app-routes';
Expand Down Expand Up @@ -83,5 +84,17 @@ export const rpcRequestRoutes = (
{ledgerStacksTxSigningRoutes}
<Route path={RouteUrls.EditNonce} element={<EditNonceSheet />} />
</Route>

<Route
path={RouteUrls.RpcStxCallContract}
element={
<AccountGate>
<RpcStxCallContract />
</AccountGate>
}
>
{ledgerStacksTxSigningRoutes}
<Route path={RouteUrls.EditNonce} element={<EditNonceSheet />} />
</Route>
</>
);
7 changes: 7 additions & 0 deletions src/background/messaging/messaging-utils.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import type { To } from 'react-router-dom';

import { bytesToHex } from '@stacks/common';
import { type PostCondition, serializePostCondition } from '@stacks/transactions';

import { InternalMethods } from '@shared/message-types';
import { sendMessage } from '@shared/messages';
import { RouteUrls } from '@shared/route-urls';
Expand Down Expand Up @@ -65,6 +68,10 @@ export function makeSearchParamsWithDefaults(
return { urlParams, origin, tabId };
}

export function encodePostConditions(postConditions: PostCondition[]) {
return postConditions.map(pc => bytesToHex(serializePostCondition(pc)));
}

const IS_TEST_ENV = process.env.TEST_ENV === 'true';

export async function triggerRequestWindowOpen(path: RouteUrls, urlParams: URLSearchParams) {
Expand Down
6 changes: 6 additions & 0 deletions src/background/messaging/rpc-message-handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import { rpcSendTransfer } from './rpc-methods/send-transfer';
import { rpcSignMessage } from './rpc-methods/sign-message';
import { rpcSignPsbt } from './rpc-methods/sign-psbt';
import { rpcSignStacksMessage } from './rpc-methods/sign-stacks-message';
import { rpcStxCallContract } from './rpc-methods/stx-call-contract';
import { rpcSupportedMethods } from './rpc-methods/supported-methods';

export async function rpcMessageHandler(message: WalletRequests, port: chrome.runtime.Port) {
Expand Down Expand Up @@ -47,6 +48,11 @@ export async function rpcMessageHandler(message: WalletRequests, port: chrome.ru
break;
}

case 'stx_callContract': {
await rpcStxCallContract(message, port);
break;
}

case 'stx_signTransaction': {
await rpcSignStacksTransaction(message, port);
break;
Expand Down
Loading
Loading