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

fix: sf 639 update contract upgrade message #256

Merged
merged 39 commits into from
Jun 20, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
39 commits
Select commit Hold shift + click to select a range
9fe75f1
feat: change account contract to cairo 1 (#152)
stanleyyconsensys Oct 4, 2023
1db6955
feat: sf-542 block cairo0 (#153)
stanleyyconsensys Oct 5, 2023
42a1f90
chore(deps): bump word-wrap from 1.2.3 to 1.2.5 (#131)
dependabot[bot] Oct 5, 2023
1a32b29
chore(deps): bump @adobe/css-tools from 4.2.0 to 4.3.1 (#132)
dependabot[bot] Oct 5, 2023
be880ca
chore(deps): bump semver from 7.3.8 to 7.5.2 (#130)
dependabot[bot] Oct 5, 2023
101804d
Merge branch 'main' into releases/2.3.0
stanleyyconsensys Oct 13, 2023
62f589e
feat: merge main
stanleyyconsensys Nov 17, 2023
2ee8e1b
feat: simplify snap logic
stanleyyconsensys Nov 17, 2023
ba81932
Merge branch 'main' into releases/2.3.0
stanleyyconsensys Nov 21, 2023
b87e23d
fix: replace hardcode class hash
stanleyyconsensys Nov 21, 2023
ef4eb37
fix: get version function
stanleyyconsensys Nov 21, 2023
bd45f53
fix: recover account for getting upgraded cairo 1 address
stanleyyconsensys Nov 21, 2023
c166dde
feat: sf 549 update all method to support upgraded address (#188)
stanleyyconsensys Jan 5, 2024
eb5f4d7
feat: sf-540 add account contract upgrade api (#189)
stanleyyconsensys Feb 21, 2024
34a11a4
Merge branch 'main' into releases/2.3.0
stanleyyconsensys Feb 28, 2024
c9270ac
feat: sf-539 UI upgrade account (#191)
stanleyyconsensys Feb 28, 2024
b0c4817
fix: test (#203)
stanleyyconsensys Feb 28, 2024
e84dd4e
Merge branch 'main' into releases/2.3.0
stanleyyconsensys Mar 22, 2024
c8276d0
chore: rebase
stanleyyconsensys Mar 22, 2024
065cc40
Merge branch 'main' into releases/2.3.0
stanleyyconsensys Apr 10, 2024
213b2e2
chore: update conflict
stanleyyconsensys Apr 10, 2024
63e405a
fix: lint issue
stanleyyconsensys Apr 11, 2024
29f7a29
feat: sf 614 rebase (#246)
stanleyyconsensys Jun 6, 2024
61d6a0f
Merge branch 'main' into releases/2.3.0
stanleyyconsensys Jun 12, 2024
2fedd78
chore: lint style
stanleyyconsensys Jun 12, 2024
b5cd8c6
fix: lint issue
stanleyyconsensys Jun 12, 2024
17f5975
fix: yarn lock
stanleyyconsensys Jun 12, 2024
fe5631d
Merge branch 'main' into releases/2.3.0
stanleyyconsensys Jun 12, 2024
3209fe3
chore: add update text
stanleyyconsensys Jun 13, 2024
d695ead
chore: update snap file
stanleyyconsensys Jun 13, 2024
47ffbb5
chore: update yarn.lock
stanleyyconsensys Jun 13, 2024
378cc71
fix: update waiting upgrade text
stanleyyconsensys Jun 18, 2024
0af0afb
chore: update snap sdk
stanleyyconsensys Jun 18, 2024
20f4fac
chore: update snap sdk
stanleyyconsensys Jun 18, 2024
3776158
Merge branch 'releases/2.3.0' into fix/sf-639-update-contract-upgrade…
khanti42 Jun 18, 2024
f77eb06
fix: building and unit tests
khanti42 Jun 19, 2024
69ccd17
fix: manifest
khanti42 Jun 19, 2024
918b651
Merge branch 'releases/2.3.0' into fix/sf-639-update-contract-upgrade…
khanti42 Jun 19, 2024
4649a82
Merge branch 'main' into fix/sf-639-update-contract-upgrade-message
stanleyyconsensys Jun 20, 2024
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
Prev Previous commit
Next Next commit
feat: simplify snap logic
  • Loading branch information
stanleyyconsensys committed Nov 17, 2023
commit 2ee8e1b0af58b2a4a9c02676ff368ddb862af31e
4 changes: 2 additions & 2 deletions packages/starknet-snap/src/createAccount.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {
deployAccount,
getBalance,
estimateAccountDeployFee,
isAccountAddressDeployed,
isAccountDeployed,
} from './utils/starknetUtils';
import {
getEtherErc20Token,
Expand Down Expand Up @@ -72,7 +72,7 @@ export async function createAccount(params: ApiParams, silentMode = false) {
};
}

const signerAssigned = await isAccountAddressDeployed(network, contractAddress);
const signerAssigned = await isAccountDeployed(network, contractAddress);

if (!signerAssigned) {
try {
Expand Down
2 changes: 1 addition & 1 deletion packages/starknet-snap/src/estimateFee.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ export async function estimateFee(params: ApiParams) {
logger.log(`estimateFee:\ntxnInvocation: ${toJson(txnInvocation)}`);

//Estimate deploy account fee if the signer has not been deployed yet
const accountDeployed = await isAccountDeployed(network, publicKey);
const accountDeployed = await isAccountDeployed(network, senderAddress);
let bulkTransactions: Invocations = [
{
type: TransactionType.INVOKE,
Expand Down
13 changes: 7 additions & 6 deletions packages/starknet-snap/src/sendTransaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,12 @@ export async function sendTransaction(params: ApiParams) {
throw new Error('Upgrade required');
}

const {
privateKey: senderPrivateKey,
publicKey,
addressIndex,
} = await getKeysFromAddress(keyDeriver, network, state, senderAddress);
const { privateKey: senderPrivateKey, addressIndex } = await getKeysFromAddress(
keyDeriver,
network,
state,
senderAddress,
);
let maxFee = requestParamsObj.maxFee ? num.toBigInt(requestParamsObj.maxFee) : constants.ZERO;
if (maxFee === constants.ZERO) {
const { suggestedMaxFee } = await estimateFee(params);
Expand Down Expand Up @@ -88,7 +89,7 @@ export async function sendTransaction(params: ApiParams) {

logger.log(`sendTransaction:\ntxnInvocation: ${toJson(txnInvocation)}\nmaxFee: ${maxFee.toString()}}`);

const accountDeployed = await isAccountDeployed(network, publicKey);
const accountDeployed = await isAccountDeployed(network, senderAddress);
if (!accountDeployed) {
//Deploy account before sending the transaction
logger.log('sendTransaction:\nFirst transaction : send deploy transaction');
Expand Down
1 change: 0 additions & 1 deletion packages/starknet-snap/src/types/snapState.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ export interface Network {
nodeUrl: string;
voyagerUrl: string;
accountClassHash: string; // in hex
accountClassHashV0?: string; // in hex
useOldAccounts?: boolean;
}

Expand Down
9 changes: 3 additions & 6 deletions packages/starknet-snap/src/utils/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,7 @@ export const STARKNET_MAINNET_NETWORK: Network = {
baseUrl: 'https://alpha-mainnet.starknet.io',
nodeUrl: 'https://starknet-mainnet.infura.io/v3/60c7253fb48147658095fe0460ac9ee9',
voyagerUrl: 'https://voyager.online',
accountClassHashV0: '0x033434ad846cdd5f23eb73ff09fe6fddd568284a0fb7d1be20ee482f044dabe2', // from argent-x repo
accountClassHash: '0x1a736d6ed154502257f02b1ccdf4d9d1089f80811cd6acad48e6b6a9d1f2003',
accountClassHash: '',
};

export const STARKNET_TESTNET_NETWORK: Network = {
Expand All @@ -32,8 +31,7 @@ export const STARKNET_TESTNET_NETWORK: Network = {
baseUrl: 'https://alpha4.starknet.io',
nodeUrl: 'https://starknet-goerli.infura.io/v3/60c7253fb48147658095fe0460ac9ee9',
voyagerUrl: 'https://goerli.voyager.online',
accountClassHashV0: '0x033434ad846cdd5f23eb73ff09fe6fddd568284a0fb7d1be20ee482f044dabe2', // from argent-x repo
accountClassHash: '0x1a736d6ed154502257f02b1ccdf4d9d1089f80811cd6acad48e6b6a9d1f2003',
accountClassHash: '',
};

export const STARKNET_INTEGRATION_NETWORK: Network = {
Expand All @@ -42,8 +40,7 @@ export const STARKNET_INTEGRATION_NETWORK: Network = {
baseUrl: 'https://external.integration.starknet.io',
nodeUrl: '',
voyagerUrl: '',
accountClassHashV0: '0x033434ad846cdd5f23eb73ff09fe6fddd568284a0fb7d1be20ee482f044dabe2', // from argent-x repo
accountClassHash: '0x1a736d6ed154502257f02b1ccdf4d9d1089f80811cd6acad48e6b6a9d1f2003',
accountClassHash: '',
};

export const ETHER_MAINNET: Erc20Token = {
Expand Down
72 changes: 47 additions & 25 deletions packages/starknet-snap/src/utils/starknetUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -421,6 +421,12 @@ export const getNextAddressIndex = (chainId: string, state: SnapState, derivatio
return uninitializedAccount?.addressIndex ?? accounts.length;
};

/**
* calculate contract address by publicKey, supported for carioVersions [1]
*
* @param publicKey - address's publicKey.
* @returns - address and calldata.
*/
export const getAccContractAddressAndCallData = (publicKey) => {
const callData = CallData.compile({
signer: publicKey,
Expand All @@ -438,6 +444,12 @@ export const getAccContractAddressAndCallData = (publicKey) => {
};
};

/**
* calculate contract address by publicKey, supported for carioVersions [0]
*
* @param publicKey - address's publicKey.
* @returns - address and calldata.
*/
export const getAccContractAddressAndCallDataCairo0 = (publicKey) => {
const callData = CallData.compile({
implementation: ACCOUNT_CLASS_HASH_V0,
Expand Down Expand Up @@ -496,37 +508,23 @@ export const getKeysFromAddressIndex = async (
};
};

export const isAccountDeployedCairo0 = async (network: Network, publicKey: string) => {
const { address } = getAccContractAddressAndCallDataCairo0(publicKey);
return isAccountAddressDeployed(network, address, 0);
};

export const isAccountDeployed = async (network: Network, publicKey: string) => {
const { address } = getAccContractAddressAndCallData(publicKey);
return isAccountAddressDeployed(network, address, 1);
};

export const isAccountAddressDeployed = async (network: Network, address: string, cairoVersion = 1) => {
let accountDeployed = true;
/**
* Check address is deployed by using getVersion, supported for carioVersions [0,1]
*
* @param network - Network.
* @param address - Input address.
* @returns - boolean.
*/
export const isAccountDeployed = async (network: Network, address: string) => {
try {
switch (cairoVersion) {
case 0:
await getSigner(address, network);
break;
case 1:
await getOwner(address, network);
break;
default:
throw new Error(`Not supported cairo version ${cairoVersion}`);
}
await getVersion(address, network);
return true;
} catch (err) {
if (!err.message.includes('Contract not found')) {
throw err;
}
accountDeployed = false;
return false;
}

return accountDeployed;
};

export const addFeesFromAllTransactions = (fees: EstimateFee[]): EstimateFee => {
Expand All @@ -552,6 +550,16 @@ export const validateAndParseAddress = (address: num.BigNumberish, length = 63)
return _validateAndParseAddressFn(address);
};

/**
* Find address index from the keyDeriver, supported for carioVersions [0,1]
*
* @param chainId - Network ChainId.
* @param address - Input address.
* @param keyDeriver - keyDeriver from MetaMask wallet.
* @param state - MetaMask Snap state.
* @param maxScan - Number of scaning in the keyDeriver.
* @returns - address index and cairoVersion.
*/
export const findAddressIndex = async (
chainId: string,
address: string,
Expand All @@ -575,6 +583,13 @@ export const findAddressIndex = async (
throw new Error(`Address not found: ${address}`);
};

/**
* Check address needed upgrade by using getVersion and compare with MIN_ACC_CONTRACT_VERSION, supported for carioVersions [0,1]
*
* @param network - Network.
* @param address - Input address.
* @returns - boolean.
*/
export const isUpgradeRequired = async (network: Network, address: string) => {
try {
logger.log(`isUpgradeRequired: address = ${address}`);
Expand All @@ -590,6 +605,13 @@ export const isUpgradeRequired = async (network: Network, address: string) => {
}
};

/**
* Get user address by public key, return address if the address has deployed, prioritize cario 1 over cario 0, supported for carioVersions [0,1]
*
* @param network - Network.
* @param publicKey - address's public key.
* @returns - address and address's public key.
*/
export const getCorrectContractAddress = async (network: Network, publicKey: string) => {
const { address: contractAddress } = getAccContractAddressAndCallData(publicKey);
const { address: contractAddressCairo0 } = getAccContractAddressAndCallDataCairo0(publicKey);
Expand Down
1 change: 0 additions & 1 deletion packages/starknet-snap/test/constants.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ export const invalidNetwork: Network = {
nodeUrl: '',
voyagerUrl: '',
accountClassHash: '',
accountClassHashV0: '',
};

export const account1: AccContract = {
Expand Down
24 changes: 12 additions & 12 deletions packages/starknet-snap/test/src/createAccount.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ describe('Test function: createAccount', function () {
});

it('should only return derived address without sending deploy txn correctly in mainnet if deploy is false', async function () {
sandbox.stub(utils, 'isAccountAddressDeployed').resolves(false);
sandbox.stub(utils, 'isAccountDeployed').resolves(false);
const requestObject: CreateAccountRequestParams = {
chainId: STARKNET_MAINNET_NETWORK.chainId,
};
Expand All @@ -86,7 +86,7 @@ describe('Test function: createAccount', function () {
sandbox.stub(utils, 'deployAccount').callsFake(async () => {
return createAccountProxyMainnetResp;
});
sandbox.stub(utils, 'isAccountAddressDeployed').resolves(false);
sandbox.stub(utils, 'isAccountDeployed').resolves(false);
sandbox.stub(utils, 'getBalance').callsFake(async () => {
return getBalanceResp[0];
});
Expand Down Expand Up @@ -122,7 +122,7 @@ describe('Test function: createAccount', function () {
sandbox.stub(utils, 'deployAccount').callsFake(async () => {
return createAccountProxyMainnetResp2;
});
sandbox.stub(utils, 'isAccountAddressDeployed').resolves(false);
sandbox.stub(utils, 'isAccountDeployed').resolves(false);
sandbox.stub(utils, 'getBalance').callsFake(async () => {
return getBalanceResp[0];
});
Expand Down Expand Up @@ -157,7 +157,7 @@ describe('Test function: createAccount', function () {
sandbox.stub(utils, 'deployAccount').callsFake(async () => {
return createAccountProxyResp;
});
sandbox.stub(utils, 'isAccountAddressDeployed').resolves(false);
sandbox.stub(utils, 'isAccountDeployed').resolves(false);
sandbox.stub(utils, 'getBalance').callsFake(async () => {
return getBalanceResp[0];
});
Expand Down Expand Up @@ -206,7 +206,7 @@ describe('Test function: createAccount', function () {
sandbox.stub(utils, 'deployAccount').callsFake(async () => {
return createAccountProxyResp;
});
sandbox.stub(utils, 'isAccountAddressDeployed').resolves(true);
sandbox.stub(utils, 'isAccountDeployed').resolves(true);
sandbox.stub(utils, 'getBalance').callsFake(async () => {
return getBalanceResp[0];
});
Expand All @@ -227,7 +227,7 @@ describe('Test function: createAccount', function () {
sandbox.stub(utils, 'deployAccount').callsFake(async () => {
return createAccountProxyResp;
});
sandbox.stub(utils, 'isAccountAddressDeployed').resolves(false);
sandbox.stub(utils, 'isAccountDeployed').resolves(false);
sandbox.stub(utils, 'getBalance').callsFake(async () => {
return getBalanceResp[0];
});
Expand All @@ -248,7 +248,7 @@ describe('Test function: createAccount', function () {
sandbox.stub(utils, 'deployAccount').callsFake(async () => {
return createAccountProxyResp;
});
sandbox.stub(utils, 'isAccountAddressDeployed').resolves(false);
sandbox.stub(utils, 'isAccountDeployed').resolves(false);
sandbox.stub(utils, 'getBalance').callsFake(async () => {
return getBalanceResp[0];
});
Expand All @@ -269,7 +269,7 @@ describe('Test function: createAccount', function () {
sandbox.stub(utils, 'deployAccount').callsFake(async () => {
return createAccountProxyResp;
});
sandbox.stub(utils, 'isAccountAddressDeployed').resolves(false);
sandbox.stub(utils, 'isAccountDeployed').resolves(false);
sandbox.stub(utils, 'getBalance').throws(new Error());
sandbox.stub(utils, 'estimateAccountDeployFee').callsFake(async () => {
return estimateDeployFeeResp2;
Expand All @@ -288,7 +288,7 @@ describe('Test function: createAccount', function () {
sandbox.stub(utils, 'deployAccount').callsFake(async () => {
return createAccountFailedProxyResp;
});
sandbox.stub(utils, 'isAccountAddressDeployed').resolves(false);
sandbox.stub(utils, 'isAccountDeployed').resolves(false);
sandbox.stub(utils, 'callContract').resolves(getBalanceResp);
sandbox.stub(utils, 'getSigner').throws(new Error());
sandbox.stub(utils, 'estimateAccountDeployFee').callsFake(async () => {
Expand All @@ -309,7 +309,7 @@ describe('Test function: createAccount', function () {
sandbox.stub(utils, 'deployAccount').callsFake(async () => {
return createAccountProxyResp;
});
sandbox.stub(utils, 'isAccountAddressDeployed').resolves(false);
sandbox.stub(utils, 'isAccountDeployed').resolves(false);
sandbox.stub(utils, 'callContract').resolves(getBalanceResp);
sandbox.stub(utils, 'getSigner').throws(new Error());
sandbox.stub(utils, 'estimateAccountDeployFee').callsFake(async () => {
Expand All @@ -328,8 +328,8 @@ describe('Test function: createAccount', function () {
}
});

it('should throw error if isAccountAddressDeployed failed', async function () {
const isAccountAddressDeployedStub = sandbox.stub(utils, 'isAccountAddressDeployed').throws(new Error());
it('should throw error if isAccountDeployed failed', async function () {
const isAccountAddressDeployedStub = sandbox.stub(utils, 'isAccountDeployed').throws(new Error());
const deployAccountStub = sandbox.stub(utils, 'deployAccount');
const estimateAccountDeployFeeStub = sandbox.stub(utils, 'estimateAccountDeployFee');
const getBalanceStub = sandbox.stub(utils, 'getBalance');
Expand Down
6 changes: 3 additions & 3 deletions packages/starknet-snap/test/src/sendTransaction.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ describe('Test function: sendTransaction', function () {
});
describe('when account is deployed', function () {
beforeEach(async function () {
sandbox.stub(utils, 'isAccountAddressDeployed').resolves(true);
sandbox.stub(utils, 'isAccountDeployed').resolves(true);
});

it('should send a transaction for transferring 10 tokens correctly', async function () {
Expand Down Expand Up @@ -268,7 +268,7 @@ describe('Test function: sendTransaction', function () {

describe('when account is deployed', function () {
beforeEach(async function () {
sandbox.stub(utils, 'isAccountAddressDeployed').resolves(true);
sandbox.stub(utils, 'isAccountDeployed').resolves(true);
});

it('should send a transaction for transferring 10 tokens correctly', async function () {
Expand Down Expand Up @@ -410,7 +410,7 @@ describe('Test function: sendTransaction', function () {

describe('when account is not deployed', function () {
beforeEach(async function () {
sandbox.stub(utils, 'isAccountAddressDeployed').resolves(false);
sandbox.stub(utils, 'isAccountDeployed').resolves(false);
});

it('should send a transaction for transferring 10 tokens and a transaction for deploy correctly', async function () {
Expand Down