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: sf-542 block cairo0 #153

Merged
merged 8 commits into from
Oct 5, 2023
Merged
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 packages/starknet-snap/snap.manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"url": "https://github.com/ConsenSys/starknet-snap.git"
},
"source": {
"shasum": "6MAPpaepoUn4jcXUhYKIF+XnYV1SGbudlJ+Qa+knr5g=",
"shasum": "yqSLTJy0WyWa+iX4Yx61DMnY7hrA8BdnBJwNEOAy2I8=",
"location": {
"npm": {
"filePath": "dist/bundle.js",
Expand Down
25 changes: 15 additions & 10 deletions packages/starknet-snap/src/estimateFee.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
estimateFeeBulk,
addFeesFromAllTransactions,
isAccountDeployed,
isUpgradeRequired,
} from './utils/starknetUtils';

import { logger } from './utils/logger';
Expand All @@ -19,8 +20,13 @@ export async function estimateFee(params: ApiParams) {
try {
const { state, keyDeriver, requestParams } = params;
const requestParamsObj = requestParams as EstimateFeeRequestParams;
const contractAddress = requestParamsObj.contractAddress;
const contractFuncName = requestParamsObj.contractFuncName;
const contractCallData = getCallDataArray(requestParamsObj.contractCallData);
const senderAddress = requestParamsObj.senderAddress;
const network = getNetworkFromChainId(state, requestParamsObj.chainId);

if (!requestParamsObj.contractAddress || !requestParamsObj.senderAddress || !requestParamsObj.contractFuncName) {
if (!contractAddress || !requestParamsObj.senderAddress || !contractFuncName) {
throw new Error(
`The given contract address, sender address, and function name need to be non-empty string, got: ${toJson(
requestParamsObj,
Expand All @@ -29,21 +35,20 @@ export async function estimateFee(params: ApiParams) {
}

try {
validateAndParseAddress(requestParamsObj.contractAddress);
validateAndParseAddress(contractAddress);
} catch (err) {
throw new Error(`The given contract address is invalid: ${requestParamsObj.contractAddress}`);
throw new Error(`The given contract address is invalid: ${contractAddress}`);
}
try {
validateAndParseAddress(requestParamsObj.senderAddress);
validateAndParseAddress(senderAddress);
} catch (err) {
throw new Error(`The given sender address is invalid: ${requestParamsObj.senderAddress}`);
throw new Error(`The given sender address is invalid: ${senderAddress}`);
}

if (await isUpgradeRequired(network, senderAddress)) {
throw new Error('Upgrade required');
}

const contractAddress = requestParamsObj.contractAddress;
const contractFuncName = requestParamsObj.contractFuncName;
const contractCallData = getCallDataArray(requestParamsObj.contractCallData);
const senderAddress = requestParamsObj.senderAddress;
const network = getNetworkFromChainId(state, requestParamsObj.chainId);
const { privateKey: senderPrivateKey, publicKey } = await getKeysFromAddress(
keyDeriver,
network,
Expand Down
21 changes: 11 additions & 10 deletions packages/starknet-snap/src/extractPrivateKey.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { toJson } from './utils/serializer';
import { validateAndParseAddress } from '../src/utils/starknetUtils';
import { ApiParams, ExtractPrivateKeyRequestParams } from './types/snapApi';
import { getNetworkFromChainId } from './utils/snapUtils';
import { getKeysFromAddress } from './utils/starknetUtils';
import { getKeysFromAddress, isUpgradeRequired } from './utils/starknetUtils';
import { DialogType } from '@metamask/rpc-methods';
import { copyable, panel, text } from '@metamask/snaps-ui';
import { logger } from './utils/logger';
Expand All @@ -11,17 +11,20 @@ export async function extractPrivateKey(params: ApiParams) {
try {
const { state, wallet, keyDeriver, requestParams } = params;
const requestParamsObj = requestParams as ExtractPrivateKeyRequestParams;

if (!requestParamsObj.userAddress) {
throw new Error(
`The given user address need to be non-empty string, got: ${toJson(requestParamsObj.userAddress)}`,
);
const network = getNetworkFromChainId(state, requestParamsObj.chainId);
const userAddress = requestParamsObj.userAddress;
if (!userAddress) {
throw new Error(`The given user address need to be non-empty string, got: ${toJson(userAddress)}`);
}

try {
validateAndParseAddress(requestParamsObj.userAddress);
validateAndParseAddress(userAddress);
} catch (err) {
throw new Error(`The given user address is invalid: ${requestParamsObj.userAddress}`);
throw new Error(`The given user address is invalid: ${userAddress}`);
}

if (await isUpgradeRequired(network, userAddress)) {
throw new Error('Upgrade required');
}

const response = await wallet.request({
Expand All @@ -33,8 +36,6 @@ export async function extractPrivateKey(params: ApiParams) {
});

if (response === true) {
const userAddress = requestParamsObj.userAddress;
const network = getNetworkFromChainId(state, requestParamsObj.chainId);
const { privateKey: userPrivateKey } = await getKeysFromAddress(keyDeriver, network, state, userAddress);

await wallet.request({
Expand Down
6 changes: 5 additions & 1 deletion packages/starknet-snap/src/extractPublicKey.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { toJson } from './utils/serializer';
import { constants, num } from 'starknet';
import { validateAndParseAddress } from '../src/utils/starknetUtils';
import { validateAndParseAddress, isUpgradeRequired } from '../src/utils/starknetUtils';
import { ApiParams, ExtractPublicKeyRequestParams } from './types/snapApi';
import { getAccount, getNetworkFromChainId } from './utils/snapUtils';
import { getKeysFromAddress } from './utils/starknetUtils';
Expand All @@ -26,6 +26,10 @@ export async function extractPublicKey(params: ApiParams) {
throw new Error(`The given user address is invalid: ${requestParamsObj.userAddress}`);
}

if (await isUpgradeRequired(network, userAddress)) {
throw new Error('Upgrade required');
}

let userPublicKey;
const accContract = getAccount(state, userAddress, network.chainId);
if (!accContract?.publicKey || num.toBigInt(accContract.publicKey) === constants.ZERO) {
Expand Down
16 changes: 14 additions & 2 deletions packages/starknet-snap/src/sendTransaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,13 @@ import { validateAndParseAddress } from '../src/utils/starknetUtils';
import { estimateFee } from './estimateFee';
import { Transaction, TransactionStatus, VoyagerTransactionType } from './types/snapState';
import { getNetworkFromChainId, getSigningTxnText, upsertTransaction } from './utils/snapUtils';
import { getKeysFromAddress, getCallDataArray, executeTxn, isAccountDeployed } from './utils/starknetUtils';
import {
getKeysFromAddress,
getCallDataArray,
executeTxn,
isAccountDeployed,
isUpgradeRequired,
} from './utils/starknetUtils';
import { ApiParams, SendTransactionRequestParams } from './types/snapApi';
import { createAccount } from './createAccount';
import { DialogType } from '@metamask/rpc-methods';
Expand Down Expand Up @@ -40,6 +46,11 @@ export async function sendTransaction(params: ApiParams) {
const contractCallData = getCallDataArray(requestParamsObj.contractCallData);
const senderAddress = requestParamsObj.senderAddress;
const network = getNetworkFromChainId(state, requestParamsObj.chainId);

if (await isUpgradeRequired(network, senderAddress)) {
throw new Error('Upgrade required');
}

const {
privateKey: senderPrivateKey,
publicKey,
Expand Down Expand Up @@ -120,7 +131,8 @@ export async function sendTransaction(params: ApiParams) {
try {
return num.toHex(num.toBigInt(data));
} catch (e) {
throw new Error(`contractCallData could not be converted, ${e.message || e}`);
//data is already send to chain, hence we should not throw error
return '0x0';
}
}),
finalityStatus: TransactionStatus.RECEIVED,
Expand Down
29 changes: 15 additions & 14 deletions packages/starknet-snap/src/signMessage.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { toJson } from './utils/serializer';
import typedDataExample from './typedData/typedDataExample.json';
import { getTypedDataMessageSignature, getKeysFromAddress } from './utils/starknetUtils';
import { getTypedDataMessageSignature, getKeysFromAddress, isUpgradeRequired } from './utils/starknetUtils';
import { getNetworkFromChainId } from './utils/snapUtils';
import { ApiParams, SignMessageRequestParams } from './types/snapApi';
import { validateAndParseAddress } from '../src/utils/starknetUtils';
Expand All @@ -12,19 +12,6 @@ export async function signMessage(params: ApiParams) {
try {
const { state, wallet, keyDeriver, requestParams } = params;
const requestParamsObj = requestParams as SignMessageRequestParams;

if (!requestParamsObj.signerAddress) {
throw new Error(
`The given signer address need to be non-empty string, got: ${toJson(requestParamsObj.signerAddress)}`,
);
}

try {
validateAndParseAddress(requestParamsObj.signerAddress);
} catch (err) {
throw new Error(`The given signer address is invalid: ${requestParamsObj.signerAddress}`);
}

const signerAddress = requestParamsObj.signerAddress;
const typedDataMessage = requestParamsObj.typedDataMessage
? JSON.parse(requestParamsObj.typedDataMessage)
Expand All @@ -33,6 +20,20 @@ export async function signMessage(params: ApiParams) {

logger.log(`signMessage:\nsignerAddress: ${signerAddress}\ntypedDataMessage: ${toJson(typedDataMessage)}`);

if (!signerAddress) {
throw new Error(`The given signer address need to be non-empty string, got: ${toJson(signerAddress)}`);
}

try {
validateAndParseAddress(signerAddress);
} catch (err) {
throw new Error(`The given signer address is invalid: ${signerAddress}`);
}

if (await isUpgradeRequired(network, signerAddress)) {
throw new Error('Upgrade required');
}

const response = await wallet.request({
method: 'snap_dialog',
params: {
Expand Down
8 changes: 4 additions & 4 deletions packages/starknet-snap/src/utils/starknetUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -540,10 +540,10 @@ export const validateAndParseAddress = (address: num.BigNumberish, length = 63)
return _validateAndParseAddressFn(address);
};

export const isUpgradeRequired = async (network: Network, cairo0address: string) => {
export const isUpgradeRequired = async (network: Network, address: string) => {
try {
logger.log(`isUpgradeRequired: cairo0address = ${cairo0address}`);
const version = await getVersion(cairo0address, network);
logger.log(`isUpgradeRequired: address = ${address}`);
const version = await getVersion(address, network);
const versionArr = version.split('.');
return Number(versionArr[1]) < MIN_ACC_CONTRACT_VERSION[1];
} catch (err) {
Expand All @@ -562,7 +562,7 @@ export const getCorrectContractAddress = async (network: Network, publicKey: str
);
let pk = '';
logger.log(
`getContractAddressByKey: contractAddressCairo1 = ${contractAddress}\ncontractAddressCairo0 = ${contractAddressCairo0}\npublicKey = ${publicKey}`,
`getContractAddressByKey: contractAddressCario1 = ${contractAddress}\ncontractAddressCairo0 = ${contractAddressCairo0}\npublicKey = ${publicKey}`,
);
try {
pk = await getOwner(contractAddress, network);
Expand Down
30 changes: 17 additions & 13 deletions packages/starknet-snap/src/verifySignedMessage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
verifyTypedDataMessageSignature,
getFullPublicKeyPairFromPrivateKey,
getKeysFromAddress,
isUpgradeRequired,
} from './utils/starknetUtils';
import { getNetworkFromChainId } from './utils/snapUtils';
import { ApiParams, VerifySignedMessageRequestParams } from './types/snapApi';
Expand All @@ -14,19 +15,6 @@ export async function verifySignedMessage(params: ApiParams) {
try {
const { state, keyDeriver, requestParams } = params;
const requestParamsObj = requestParams as VerifySignedMessageRequestParams;

if (!requestParamsObj.signerAddress || !requestParamsObj.signature) {
throw new Error(
`The given signer address and signature need to be non-empty string, got: ${toJson(requestParamsObj)}`,
);
}

try {
validateAndParseAddress(requestParamsObj.signerAddress);
} catch (err) {
throw new Error(`The given signer address is invalid: ${requestParamsObj.signerAddress}`);
}

const verifySignerAddress = requestParamsObj.signerAddress;
const verifySignature = requestParamsObj.signature;
const verifyTypedDataMessage = requestParamsObj.typedDataMessage
Expand All @@ -40,6 +28,22 @@ export async function verifySignedMessage(params: ApiParams) {
)}`,
);

if (!verifySignerAddress || !verifySignature) {
throw new Error(
`The given signer address and signature need to be non-empty string, got: ${toJson(requestParamsObj)}`,
);
}

try {
validateAndParseAddress(verifySignerAddress);
} catch (err) {
throw new Error(`The given signer address is invalid: ${verifySignerAddress}`);
}

if (await isUpgradeRequired(network, verifySignerAddress)) {
throw new Error('Upgrade required');
}

const { privateKey: signerPrivateKey } = await getKeysFromAddress(keyDeriver, network, state, verifySignerAddress);

const fullPublicKey = getFullPublicKeyPairFromPrivateKey(signerPrivateKey);
Expand Down
Loading