Skip to content
This repository has been archived by the owner on Feb 5, 2023. It is now read-only.

Commit

Permalink
feat: check ropsten only when register
Browse files Browse the repository at this point in the history
  • Loading branch information
Atlasoin committed Sep 6, 2021
1 parent 9025f67 commit f05906d
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 41 deletions.
2 changes: 1 addition & 1 deletion src/common/rns.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,8 @@ function sha3HexAddress(addr: string) {
}

export default {
// We have checked network and account in verifyRNS method in RNS.vue, so we don't need to check it here.
async register(name: string, speed: SPEED = 'average') {
await (window as any).ethereum?.enable();
return callRNSContract<ethers.providers.TransactionResponse>('token', 'web3', speed, 'register', name);
},
addr2Name(addr: string, speed: SPEED = 'average') {
Expand Down
27 changes: 0 additions & 27 deletions src/common/rss3.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,18 +24,6 @@ export interface IAssetProfile {
};
}

function validateNetwork(chain: number | null, cb?: (chain: number | null) => void) {
if (config.rns.test && chain !== 0x3) {
alert('Please switch to ropsten network.');
cb ? cb(chain) : '';
throw 'Network error';
} else if (!config.rns.test && chain !== 0x1) {
alert('Please switch to mainnet network.');
cb ? cb(chain) : '';
throw 'Network error';
}
}

async function walletConnect() {
provider = new WalletConnectProvider({
infuraId: config.infuraId,
Expand All @@ -60,17 +48,6 @@ async function walletConnect() {

const address = (await web3.eth.getAccounts())[0];

// Try to subscribe to chainId change
// But actually it doesn't work, so we need to
// disconnect the user if she uses the wrong network
// and ask her to reconnect
// provider.on('chainChanged', (chainId: number) => {
// console.log(chainId);
// });
const chain = await web3.eth.getChainId();
console.log(chain);
validateNetwork(chain, disconnect);

rss3 = new RSS3({
endpoint: config.rss3Endpoint,
address: address,
Expand Down Expand Up @@ -101,10 +78,6 @@ async function metamaskConnect() {
if (sessionStorage.getItem('lastConnect') === 'metamask' && sessionStorage.getItem('lastAddress')) {
address = <string>sessionStorage.getItem('lastAddress');
} else {
// check chainId
const chain: string | null = await metamaskEthereum.request({ method: 'eth_chainId' });
validateNetwork(Number(chain));

const accounts = await metamaskEthereum.request({
method: 'eth_requestAccounts',
});
Expand Down
48 changes: 35 additions & 13 deletions src/views/Setup/RNS.vue
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,18 @@ import RNSUtils from '@/common/rns';
import Modal from '@/components/Modal.vue';
import Input from '@/components/Input.vue';
import Loading from '@/components/Loading.vue';
import config from '@/config';
function validateNetwork(chain: number | null, cb?: (chain: number | null) => void) {
if (config.rns.test && chain !== 0x3) {
alert('Please switch to ropsten network.');
cb ? cb(chain) : '';
throw 'Network error';
} else if (!config.rns.test && chain !== 0x1) {
alert('Please switch to mainnet network.');
cb ? cb(chain) : '';
throw 'Network error';
}
}
@Options({
components: {
Input,
Expand All @@ -117,25 +129,16 @@ export default class RNS extends Vue {
isMobile: Boolean = /Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(navigator.userAgent);
async mounted() {
// console.log('atlas addr:', await RNSUtils.name2Addr('atlas.pass3.me'));
// console.log('rss3 addr:', await RNSUtils.name2Addr('rss3.pass3.me'));
// console.log(
// 'name of 0x8c23B96f2fb77AaE1ac2832debEE30f09da7af3C:',
// await RNSUtils.addr2Name('0x8c23B96f2fb77AaE1ac2832debEE30f09da7af3C'),
// );
// console.log('hanabi addr:', await RNSUtils.name2Addr('hanabi.pass3.me'));
// console.log(
// 'name of 0xc560eb6fd0c2eb80Df50E5e06715295AE1205049:',
// await RNSUtils.addr2Name('0xc560eb6fd0c2eb80Df50E5e06715295AE1205049'),
// );
// console.log('Balance of PASS3:', await RNSUtils.balanceOfPass3('0x8c23B96f2fb77AaE1ac2832debEE30f09da7af3C'));
// If redirect, return true. Otherwise return false.
async tryRedirect() {
if (!(await RSS3.reconnect())) {
sessionStorage.setItem('redirectFrom', this.$route.fullPath);
await this.$router.push('/');
return true;
} else {
if (this.isMobile) {
await this.$router.push('/gotopc');
return true;
}
this.rss3 = await RSS3.get();
if ((await RNSUtils.addr2Name((<IRSS3>this.rss3).account.address)).toString() !== '') {
Expand All @@ -153,8 +156,26 @@ export default class RNS extends Vue {
sessionStorage.removeItem('redirectFrom');
await this.$router.push(redirectFrom || '/home');
}
return true;
}
}
return false;
}
async refreshAccount() {
if (!(await this.tryRedirect())) {
const metamaskEthereum = (window as any).ethereum;
// this.rss3 object exists, doens't necessarily mean the account is connected
await metamaskEthereum.request({
method: 'eth_requestAccounts',
});
const chain: string | null = await metamaskEthereum.request({ method: 'eth_chainId' });
validateNetwork(Number(chain));
}
}
async mounted() {
await this.refreshAccount();
}
async back() {
Expand All @@ -169,6 +190,7 @@ export default class RNS extends Vue {
this.isDisabled = true;
return;
}
await this.refreshAccount();
this.rns = this.rns.toLowerCase();
if (this.rns.length < 3 || this.rns.length >= 15) {
this.notice = 'An RNS must have at least 3 characters and no more than 15';
Expand Down

0 comments on commit f05906d

Please sign in to comment.