From ed059130c6b4cddd28965302eb3824529d1328ac Mon Sep 17 00:00:00 2001 From: jonesho <81145364+jonesho@users.noreply.github.com> Date: Tue, 22 Nov 2022 00:12:18 +0800 Subject: [PATCH] feat: temporarily reverted back to use StarkNet feeder gateway APIs as default provider (#39) --- packages/starknet-snap/index.html | 162 ++++++++++-------- packages/starknet-snap/snap.manifest.json | 2 +- packages/starknet-snap/src/index.ts | 8 +- packages/starknet-snap/src/types/snapApi.ts | 1 + packages/starknet-snap/src/utils/constants.ts | 20 ++- 5 files changed, 113 insertions(+), 80 deletions(-) diff --git a/packages/starknet-snap/index.html b/packages/starknet-snap/index.html index cfb3c97d..6fc8780c 100644 --- a/packages/starknet-snap/index.html +++ b/packages/starknet-snap/index.html @@ -32,8 +32,11 @@

Hello, Snaps!



- -
+
Recover User Accounts @@ -302,6 +305,19 @@

Hello, Snaps!

getTransactionsForm.addEventListener('submit', getTransactions) recoverUserAccountsForm.addEventListener('submit', recoverUserAccounts) + function getReqFlagsForTestnetType(testType) { + const rv = { + isDev: false, + isGoerli2: false, + } + if (testType === "testnet2") { + rv.isGoerli2 = true + } else if (testType === 'integration') { + rv.isDev = true + } + return rv + } + // here we get permissions to interact with and install the snap async function connect() { await ethereum.request({ @@ -350,7 +366,7 @@

Hello, Snaps!

const tokenAddress = document.getElementById('tokenAddress').value const userAddress = document.getElementById('userAccAddress').value const chainId = document.getElementById('targetChainId').value - const isDev = document.getElementById('isDev').checked + const goerliTestnetType = document.getElementById('goerliTestnetType').value try { const response = await ethereum.request({ @@ -361,7 +377,7 @@

Hello, Snaps!

tokenAddress, userAddress, chainId, - isDev, + ...getReqFlagsForTestnetType(goerliTestnetType), }, }] }) @@ -382,7 +398,7 @@

Hello, Snaps!

const contractCallData = document.getElementById('contractCallData').value const senderAddress = document.getElementById('feeSenderAddress').value const chainId = document.getElementById('targetChainId').value - const isDev = document.getElementById('isDev').checked + const goerliTestnetType = document.getElementById('goerliTestnetType').value try { const response = await ethereum.request({ @@ -395,7 +411,7 @@

Hello, Snaps!

contractCallData, senderAddress, chainId, - isDev, + ...getReqFlagsForTestnetType(goerliTestnetType), } }] }) @@ -417,7 +433,7 @@

Hello, Snaps!

const senderAddress = document.getElementById('senderAddress').value const maxFee = document.getElementById('transferMaxFee').value const chainId = document.getElementById('targetChainId').value - const isDev = document.getElementById('isDev').checked + const goerliTestnetType = document.getElementById('goerliTestnetType').value try { const response = await ethereum.request({ @@ -431,7 +447,7 @@

Hello, Snaps!

senderAddress, maxFee, chainId, - isDev, + ...getReqFlagsForTestnetType(goerliTestnetType), }, }] }) @@ -448,7 +464,7 @@

Hello, Snaps!

e.preventDefault() // to prevent default form behavior const chainId = document.getElementById('targetChainId').value - const isDev = document.getElementById('isDev').checked + const goerliTestnetType = document.getElementById('goerliTestnetType').value try { const response = await ethereum.request({ @@ -457,7 +473,7 @@

Hello, Snaps!

method: 'starkNet_getStoredUserAccounts', params: { chainId, - isDev, + ...getReqFlagsForTestnetType(goerliTestnetType) }, }] }) @@ -474,8 +490,8 @@

Hello, Snaps!

const transactionHash = document.getElementById('transactionHash').value const chainId = document.getElementById('targetChainId').value - const isDev = document.getElementById('isDev').checked - + const goerliTestnetType = document.getElementById('goerliTestnetType').value + try { const response = await ethereum.request({ method: 'wallet_invokeSnap', @@ -484,7 +500,7 @@

Hello, Snaps!

params: { transactionHash, chainId, - isDev, + ...getReqFlagsForTestnetType(goerliTestnetType), }, }] }) @@ -499,9 +515,9 @@

Hello, Snaps!

e.preventDefault() // to prevent default form behavior const addressIndex = document.getElementById('addressIndex').value - const chainId = document.getElementById('targetChainId').value; - const isDev = document.getElementById('isDev').checked; - + const chainId = document.getElementById('targetChainId').value + const goerliTestnetType = document.getElementById('goerliTestnetType').value + try { const response = await ethereum.request({ method: 'wallet_invokeSnap', @@ -510,7 +526,7 @@

Hello, Snaps!

params: { addressIndex, chainId, - isDev, + ...getReqFlagsForTestnetType(goerliTestnetType), }, }] }) @@ -524,10 +540,10 @@

Hello, Snaps!

async function retrievePrivateKeyFromAddress(e) { e.preventDefault() // to prevent default form behavior - const userAddress = document.getElementById('userAddress').value; - const chainId = document.getElementById('targetChainId').value; - const isDev = document.getElementById('isDev').checked; - + const userAddress = document.getElementById('userAddress').value + const chainId = document.getElementById('targetChainId').value + const goerliTestnetType = document.getElementById('goerliTestnetType').value + try { const response = await ethereum.request({ method: 'wallet_invokeSnap', @@ -536,7 +552,7 @@

Hello, Snaps!

params: { userAddress, chainId, - isDev, + ...getReqFlagsForTestnetType(goerliTestnetType), }, }] }) @@ -550,10 +566,10 @@

Hello, Snaps!

async function retrievePublicKeyFromAddress(e) { e.preventDefault() // to prevent default form behavior - const userAddress = document.getElementById('pkUserAddress').value; - const chainId = document.getElementById('targetChainId').value; - const isDev = document.getElementById('isDev').checked; - + const userAddress = document.getElementById('pkUserAddress').value + const chainId = document.getElementById('targetChainId').value + const goerliTestnetType = document.getElementById('goerliTestnetType').value + try { const response = await ethereum.request({ method: 'wallet_invokeSnap', @@ -562,7 +578,7 @@

Hello, Snaps!

params: { userAddress, chainId, - isDev, + ...getReqFlagsForTestnetType(goerliTestnetType), }, }] }) @@ -576,11 +592,11 @@

Hello, Snaps!

async function signTypedDataMessage(e) { e.preventDefault() // to prevent default form behavior - const typedDataMessage = document.getElementById('typedDataMessage').value; - const signerAddress = document.getElementById('signerUserAddress').value; - const chainId = document.getElementById('targetChainId').value; - const isDev = document.getElementById('isDev').checked; - + const typedDataMessage = document.getElementById('typedDataMessage').value + const signerAddress = document.getElementById('signerUserAddress').value + const chainId = document.getElementById('targetChainId').value + const goerliTestnetType = document.getElementById('goerliTestnetType').value + try { const response = await ethereum.request({ method: 'wallet_invokeSnap', @@ -590,7 +606,7 @@

Hello, Snaps!

typedDataMessage, signerAddress, chainId, - isDev, + ...getReqFlagsForTestnetType(goerliTestnetType), }, }] }) @@ -604,12 +620,12 @@

Hello, Snaps!

async function verifyTypedDataSignature(e) { e.preventDefault() // to prevent default form behavior - const typedDataMessage = document.getElementById('verifyTypedDataMessage').value; - const signerAddress = document.getElementById('verifySignerUserAddress').value; - const signature = document.getElementById('verifySignature').value; - const chainId = document.getElementById('targetChainId').value; - const isDev = document.getElementById('isDev').checked; - + const typedDataMessage = document.getElementById('verifyTypedDataMessage').value + const signerAddress = document.getElementById('verifySignerUserAddress').value + const signature = document.getElementById('verifySignature').value + const chainId = document.getElementById('targetChainId').value + const goerliTestnetType = document.getElementById('goerliTestnetType').value + try { const response = await ethereum.request({ method: 'wallet_invokeSnap', @@ -620,7 +636,7 @@

Hello, Snaps!

signerAddress, signature, chainId, - isDev, + ...getReqFlagsForTestnetType(goerliTestnetType), }, }] }) @@ -634,13 +650,13 @@

Hello, Snaps!

async function addErc20Token(e) { e.preventDefault() // to prevent default form behavior - const tokenAddress = document.getElementById('addTokenAddress').value; - const tokenName = document.getElementById('addTokenName').value; - const tokenSymbol = document.getElementById('addTokenSymbol').value; - const tokenDecimals = document.getElementById('addTokenDecimals').value; - const chainId = document.getElementById('targetChainId').value; - const isDev = document.getElementById('isDev').checked; - + const tokenAddress = document.getElementById('addTokenAddress').value + const tokenName = document.getElementById('addTokenName').value + const tokenSymbol = document.getElementById('addTokenSymbol').value + const tokenDecimals = document.getElementById('addTokenDecimals').value + const chainId = document.getElementById('targetChainId').value + const goerliTestnetType = document.getElementById('goerliTestnetType').value + try { const response = await ethereum.request({ method: 'wallet_invokeSnap', @@ -652,7 +668,7 @@

Hello, Snaps!

tokenSymbol, tokenDecimals, chainId, - isDev, + ...getReqFlagsForTestnetType(goerliTestnetType), }, }] }) @@ -666,8 +682,8 @@

Hello, Snaps!

async function getStoredErc20Tokens(e) { e.preventDefault() // to prevent default form behavior - const chainId = document.getElementById('targetChainId').value; - const isDev = document.getElementById('isDev').checked; + const chainId = document.getElementById('targetChainId').value + const goerliTestnetType = document.getElementById('goerliTestnetType').value try { const response = await ethereum.request({ @@ -676,7 +692,7 @@

Hello, Snaps!

method: 'starkNet_getStoredErc20Tokens', params: { chainId, - isDev, + ...getReqFlagsForTestnetType(goerliTestnetType), }, }] }) @@ -696,7 +712,7 @@

Hello, Snaps!

const senderAddress = document.getElementById('txnSenderAddress').value const maxFee = document.getElementById('txnMaxFee').value const chainId = document.getElementById('targetChainId').value - const isDev = document.getElementById('isDev').checked + const goerliTestnetType = document.getElementById('goerliTestnetType').value try { const response = await ethereum.request({ @@ -710,7 +726,7 @@

Hello, Snaps!

senderAddress, maxFee, chainId, - isDev, + ...getReqFlagsForTestnetType(goerliTestnetType), }, }] }) @@ -730,7 +746,7 @@

Hello, Snaps!

const contractFuncName = document.getElementById('callContractFuncName').value const contractCallData = document.getElementById('callContractCallData').value const chainId = document.getElementById('targetChainId').value - const isDev = document.getElementById('isDev').checked + const goerliTestnetType = document.getElementById('goerliTestnetType').value try { const response = await ethereum.request({ @@ -742,7 +758,7 @@

Hello, Snaps!

contractFuncName, contractCallData, chainId, - isDev, + ...getReqFlagsForTestnetType(goerliTestnetType), }, }] }) @@ -758,14 +774,14 @@

Hello, Snaps!

async function addNetwork(e) { e.preventDefault() // to prevent default form behavior - const networkName = document.getElementById('addNetworkName').value; - const networkChainId = document.getElementById('addNetworkChainId').value; - const networkBaseUrl = document.getElementById('addNetworkBaseUrl').value; - const networkNodeUrl = document.getElementById('addNetworkNodeUrl').value; - const networkVoyagerUrl = document.getElementById('addNetworkVoyagerUrl').value; - const accountClassHash = document.getElementById('addNetworkAccClassHash').value; - const isDev = document.getElementById('isDev').checked; - + const networkName = document.getElementById('addNetworkName').value + const networkChainId = document.getElementById('addNetworkChainId').value + const networkBaseUrl = document.getElementById('addNetworkBaseUrl').value + const networkNodeUrl = document.getElementById('addNetworkNodeUrl').value + const networkVoyagerUrl = document.getElementById('addNetworkVoyagerUrl').value + const accountClassHash = document.getElementById('addNetworkAccClassHash').value + const goerliTestnetType = document.getElementById('goerliTestnetType').value + try { const response = await ethereum.request({ method: 'wallet_invokeSnap', @@ -778,7 +794,7 @@

Hello, Snaps!

networkNodeUrl, networkVoyagerUrl, accountClassHash, - isDev, + ...getReqFlagsForTestnetType(goerliTestnetType), }, }] }) @@ -792,16 +808,12 @@

Hello, Snaps!

async function getStoredNetworks(e) { e.preventDefault() // to prevent default form behavior - const isDev = document.getElementById('isDev').checked; - try { const response = await ethereum.request({ method: 'wallet_invokeSnap', params: [snapId, { method: 'starkNet_getStoredNetworks', - params: { - isDev, - }, + params: {}, }] }) alert(JSON.stringify(response)); @@ -820,7 +832,7 @@

Hello, Snaps!

const txnType = document.getElementById('txnType').value const txnsInLastNumOfDays = document.getElementById('txnInLastNumOfDays').value const chainId = document.getElementById('targetChainId').value - const isDev = document.getElementById('isDev').checked + const goerliTestnetType = document.getElementById('goerliTestnetType').value try { const response = await ethereum.request({ @@ -833,7 +845,7 @@

Hello, Snaps!

txnType, txnsInLastNumOfDays, chainId, - isDev, + ...getReqFlagsForTestnetType(goerliTestnetType), }, }] }) @@ -854,7 +866,7 @@

Hello, Snaps!

const chainId = document.getElementById('targetChainId').value const onlyFromState = document.getElementById('getTxnsOnlyFromState').checked const withDeployTxn = document.getElementById('getTxnsWithDeployTxn').checked - const isDev = document.getElementById('isDev').checked + const goerliTestnetType = document.getElementById('goerliTestnetType').value try { const response = await ethereum.request({ @@ -869,7 +881,7 @@

Hello, Snaps!

onlyFromState, withDeployTxn, chainId, - isDev, + ...getReqFlagsForTestnetType(goerliTestnetType), }, }] }) @@ -887,7 +899,7 @@

Hello, Snaps!

const maxScanned = document.getElementById('maxScanned').value const maxMissed = document.getElementById('maxMissed').value const chainId = document.getElementById('targetChainId').value - const isDev = document.getElementById('isDev').checked + const goerliTestnetType = document.getElementById('goerliTestnetType').value try { const response = await ethereum.request({ @@ -899,7 +911,7 @@

Hello, Snaps!

maxScanned, maxMissed, chainId, - isDev, + ...getReqFlagsForTestnetType(goerliTestnetType), }, }] }) diff --git a/packages/starknet-snap/snap.manifest.json b/packages/starknet-snap/snap.manifest.json index 9b273e7c..46081bed 100644 --- a/packages/starknet-snap/snap.manifest.json +++ b/packages/starknet-snap/snap.manifest.json @@ -7,7 +7,7 @@ "url": "https://github.com/ConsenSys/starknet-snap.git" }, "source": { - "shasum": "5rkBzhGKEh+4fgRN3F5+bO46Bed5vqOgIUPxcStQxHw=", + "shasum": "kGCsvTBWWYBdH1OveJmPbVXKSXVuuRjegD8A1ULEc6M=", "location": { "npm": { "filePath": "dist/bundle.js", diff --git a/packages/starknet-snap/src/index.ts b/packages/starknet-snap/src/index.ts index 3b7759f5..dc21179f 100644 --- a/packages/starknet-snap/src/index.ts +++ b/packages/starknet-snap/src/index.ts @@ -19,6 +19,7 @@ import { STARKNET_INTEGRATION_NETWORK, STARKNET_MAINNET_NETWORK, STARKNET_TESTNET_NETWORK, + STARKNET_TESTNET2_NETWORK, } from './utils/constants'; import { upsertErc20Token, upsertNetwork } from './utils/snapUtils'; import { getStoredNetworks } from './getStoredNetworks'; @@ -35,6 +36,7 @@ const saveMutex = new Mutex(); export const onRpcRequest: OnRpcRequestHandler = async ({ origin, request }) => { const requestParams = request?.params as unknown as ApiRequestParams; const isDev = !!requestParams?.isDev; + const isGoerli2 = !!requestParams?.isGoerli2; // Switch statement for methods not requiring state to speed things up a bit console.log(origin, request); @@ -67,7 +69,11 @@ export const onRpcRequest: OnRpcRequestHandler = async ({ origin, request }) => if (isDev) { await upsertNetwork(STARKNET_INTEGRATION_NETWORK, wallet, saveMutex, state); } else { - await upsertNetwork(STARKNET_TESTNET_NETWORK, wallet, saveMutex, state); + if (isGoerli2) { + await upsertNetwork(STARKNET_TESTNET2_NETWORK, wallet, saveMutex, state); + } else { + await upsertNetwork(STARKNET_TESTNET_NETWORK, wallet, saveMutex, state); + } } for (const token of PRELOADED_TOKENS) { await upsertErc20Token(token, wallet, saveMutex, state); diff --git a/packages/starknet-snap/src/types/snapApi.ts b/packages/starknet-snap/src/types/snapApi.ts index bb7a98b2..73f0a5e5 100644 --- a/packages/starknet-snap/src/types/snapApi.ts +++ b/packages/starknet-snap/src/types/snapApi.ts @@ -32,6 +32,7 @@ export type ApiRequestParams = export interface BaseRequestParams { chainId?: string; isDev?: boolean; + isGoerli2?: boolean; } // eslint-disable-next-line @typescript-eslint/no-empty-interface diff --git a/packages/starknet-snap/src/utils/constants.ts b/packages/starknet-snap/src/utils/constants.ts index ee0da7aa..0e788741 100644 --- a/packages/starknet-snap/src/utils/constants.ts +++ b/packages/starknet-snap/src/utils/constants.ts @@ -17,7 +17,7 @@ export const STARKNET_MAINNET_NETWORK: Network = { name: 'StarkNet Mainnet', chainId: constants.StarknetChainId.MAINNET, baseUrl: 'https://alpha-mainnet.starknet.io', - nodeUrl: 'https://starknet-mainnet.infura.io/v3/60c7253fb48147658095fe0460ac9ee9', + nodeUrl: '', voyagerUrl: 'https://voyager.online', accountClassHash: '0x3e327de1c40540b98d05cbcb13552008e36f0ec8d61d46956d2f9752c294328', // from argent-x repo }; @@ -26,11 +26,20 @@ export const STARKNET_TESTNET_NETWORK: Network = { name: 'Goerli Testnet', chainId: constants.StarknetChainId.TESTNET, baseUrl: 'https://alpha4.starknet.io', - nodeUrl: 'https://starknet-goerli.infura.io/v3/60c7253fb48147658095fe0460ac9ee9', + nodeUrl: '', voyagerUrl: 'https://goerli.voyager.online', accountClassHash: '0x3e327de1c40540b98d05cbcb13552008e36f0ec8d61d46956d2f9752c294328', // from argent-x repo }; +export const STARKNET_TESTNET2_NETWORK: Network = { + name: 'Goerli Testnet 2', + chainId: constants.StarknetChainId.TESTNET, + baseUrl: 'https://alpha4-2.starknet.io', + nodeUrl: '', + voyagerUrl: 'https://goerli-2.voyager.online', + accountClassHash: '0x3e327de1c40540b98d05cbcb13552008e36f0ec8d61d46956d2f9752c294328', // from argent-x repo +}; + export const STARKNET_INTEGRATION_NETWORK: Network = { name: 'Goerli Integration', chainId: constants.StarknetChainId.TESTNET, @@ -113,7 +122,12 @@ export const TEST_TOKEN_TESTNET: Erc20Token = { }; export const PRELOADED_TOKENS = [ETHER_MAINNET, ETHER_TESTNET]; -export const PRELOADED_NETWORKS = [STARKNET_MAINNET_NETWORK, STARKNET_TESTNET_NETWORK, STARKNET_INTEGRATION_NETWORK]; +export const PRELOADED_NETWORKS = [ + STARKNET_MAINNET_NETWORK, + STARKNET_TESTNET_NETWORK, + STARKNET_TESTNET2_NETWORK, + STARKNET_INTEGRATION_NETWORK, +]; export const CONTRACT_ADDRESS_PREFIX = shortString.encodeShortString('STARKNET_CONTRACT_ADDRESS'); export const PROXY_CONTRACT_HASH = '0x25ec026985a3bf9d0cc1fe17326b245dfdc3ff89b8fde106542a3ea56c5a918'; // from argent-x repo