Skip to content

Commit

Permalink
Add RNS multichchain resolution in Transfer page
Browse files Browse the repository at this point in the history
  • Loading branch information
adil651 committed Dec 20, 2022
1 parent f1f43cc commit ae2e9ca
Show file tree
Hide file tree
Showing 8 changed files with 395 additions and 747 deletions.
1,049 changes: 306 additions & 743 deletions package-lock.json

Large diffs are not rendered by default.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
"@metamask/contract-metadata": "^2.0.0",
"@metamask/eth-sig-util": "^4.0.1",
"@metamask/obs-store": "^7.0.0",
"@rsksmart/rns-resolver.js": "^1.1.0",
"@rsksmart/rsk-utils": "^1.1.0",
"@sentry/browser": "^7.20.1",
"@sentry/tracing": "^7.20.1",
Expand Down Expand Up @@ -76,6 +77,7 @@
"lodash": "^4.17.21",
"loglevel": "^1.8.1",
"multi-token-standard-abi": "1.0.4",
"node-fetch": "^2.0.0",
"pify": "^5.0.0",
"pump": "^3.0.0",
"readable-stream": "^3.6.0",
Expand Down Expand Up @@ -147,7 +149,6 @@
"luxon": "^3.1.0",
"mocha": "^10.1.0",
"nock": "13.2.9",
"node-fetch": "^3.3.0",
"nyc": "^15.1.0",
"os-browserify": "^0.3.0",
"prettier": "^2.7.1",
Expand Down
3 changes: 3 additions & 0 deletions src/components/WalletTransfer/AddContact/AddContact.vue
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ import {
GOOGLE_LABEL,
REDDIT,
REDDIT_LABEL,
RNS,
RNS_LABEL,
TWITTER,
TWITTER_LABEL,
UNSTOPPABLE_DOMAINS,
Expand All @@ -68,6 +70,7 @@ const VERIFIER_LABELS = {
[TWITTER]: TWITTER_LABEL,
[GITHUB]: GITHUB_LABEL,
[ENS]: ENS_LABEL,
[RNS]: RNS_LABEL,
[BIT]: BIT_LABEL,
[UNSTOPPABLE_DOMAINS]: UNSTOPPABLE_DOMAINS_LABEL,
}
Expand Down
23 changes: 21 additions & 2 deletions src/containers/WalletTransfer/WalletTransfer.vue
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@
:items="getToAddressComboboxItems"
:placeholder="verifierPlaceholder"
required
:rules="[contactRule, rules.contactRequired, ensRule, unstoppableDomainsRule, bitRule, torusRule]"
:rules="[contactRule, rules.contactRequired, ensRule, rnsRule, unstoppableDomainsRule, bitRule, torusRule]"
outlined
item-text="name"
item-value="value"
Expand Down Expand Up @@ -544,6 +544,7 @@ import {
MESSAGE_MODAL_TYPE_SUCCESS,
OLD_ERC721_LIST,
REDDIT,
RNS,
TRANSACTION_SPEED,
TWITTER,
UNSTOPPABLE_DOMAINS,
Expand Down Expand Up @@ -594,6 +595,7 @@ export default {
toAddress: '',
formValid: false,
ensError: '',
rnsError: '',
bitError: '',
torusError: '',
unstoppableDomainsError: '',
Expand Down Expand Up @@ -848,7 +850,7 @@ export default {
this.$vuetify.goTo(0)
},
methods: {
...mapActions(['getTorusLookupAddress', 'getEnsOrUnstoppableAddress']),
...mapActions(['getTorusLookupAddress', 'getEnsOrUnstoppableAddress', 'getRnsAddress']),
startQrScanning() {
this.camera = 'auto'
this.showQrScanner = true
Expand All @@ -862,6 +864,8 @@ export default {
this.selectedVerifier = GOOGLE
} else if (/.eth$/.test(toAddress) || /.xyz$/.test(toAddress) || /.kred$/i.test(toAddress)) {
this.selectedVerifier = ENS
} else if (/.rsk$/.test(toAddress)) {
this.selectedVerifier = RNS
} else if (/.crypto$/.test(toAddress)) {
this.selectedVerifier = UNSTOPPABLE_DOMAINS
} else if (new RegExp(`${this.bitTail}$`).test(toAddress)) {
Expand Down Expand Up @@ -971,6 +975,9 @@ export default {
ensRule() {
return this.selectedVerifier === ENS && this.ensError ? this.ensError : true
},
rnsRule() {
return this.selectedVerifier === RNS && this.rnsError ? this.rnsError : true
},
bitRule() {
return this.selectedVerifier === BIT && this.bitError ? this.bitError : true
},
Expand Down Expand Up @@ -1085,6 +1092,7 @@ export default {
}
}
this.ensError = ''
this.rnsError = ''
this.unstoppableDomainsError = ''
this.torusError = ''
Expand Down Expand Up @@ -1233,6 +1241,17 @@ export default {
this.ensError = 'walletSettings.invalidEns'
this.$refs.form.validate()
}
} else if (this.selectedVerifier === RNS) {
try {
const res = await this.getRnsAddress({ domain: this.toAddress, chainType: this.networkType.type, coinType: this.networkType.ticker })
log.info(res)
toAddress = res.toLowerCase()
this.rnsError = ''
} catch (error) {
log.error(error)
this.rnsError = 'walletSettings.invalidRns'
}
this.$refs.form.validate()
} else if (this.selectedVerifier === UNSTOPPABLE_DOMAINS) {
try {
const res = await this.getUnstoppableDomains(this.toAddress)
Expand Down
53 changes: 53 additions & 0 deletions src/controllers/PreferencesController.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import { ObservableStore } from '@metamask/obs-store'
import Resolver from '@rsksmart/rns-resolver.js'
import { SafeEventEmitter } from '@toruslabs/openlogin-jrpc'
import deepmerge from 'deepmerge'
import { hashPersonalMessage } from 'ethereumjs-util'
import { cloneDeep } from 'lodash'
import log from 'loglevel'
import nodeFetch from 'node-fetch'
import Web3 from 'web3'
import { isHexStrict, toHex } from 'web3-utils'

Expand Down Expand Up @@ -68,6 +70,8 @@ class PreferencesController extends SafeEventEmitter {
this.web3 = new Web3(provider)
this.api = new ApiHelpers(options.storeDispatch)
this.signMessage = signMessage
this.rnsAddrResolverMainnet = Resolver.forRskMainnet({ fetch: nodeFetch })
this.rnsAddrResolverTestnet = Resolver.forRskTestnet({ fetch: nodeFetch })

this.interval = options.interval || DEFAULT_INTERVAL
this.store = new ObservableStore({ selectedAddress: '' }) // Account specific object
Expand Down Expand Up @@ -838,6 +842,55 @@ class PreferencesController extends SafeEventEmitter {
return this.api.get(url.href, this.headers(), { useAPIKey: true })
}

// This approach considers resolution for tokens
getRnsChainIdFromPluginId = (pluginId) => {
switch (pluginId) {
case 'RBTC':
return 137
case 'ETH':
return 60
case 'ETHC':
return 61
case 'BTC':
return 0
case 'LTC':
return 2
case 'dash':
return 5
case 'XRP':
return 144
case 'BCH':
return 145
case 'BNB':
return 714
case 'XLM':
return 148
case 'MATIC':
return 966
case 'AVAX':
return 9000
case 'GXC':
return 2303
case 'OKT':
return 996
case 'DAI':
return 700
default:
return -1
}
}

getRnsAddress({ domain, chainType, coinType }) {
if (chainType === 'rsk_testnet') {
return this.rnsAddrResolverTestnet.addr(domain)
}
if (chainType === 'rsk_mainnet') {
return this.rnsAddrResolverMainnet.addr(domain)
}
const pluginId = this.getRnsChainIdFromPluginId(coinType)
return chainType.includes('mainnet') ? this.rnsAddrResolverMainnet.addr(domain, pluginId) : this.rnsAddrResolverTestnet.addr(domain, pluginId)
}

async getTorusLookupAddress({ verifier, verifierId, walletVerifier, network }) {
const url = new URL(`${config.api}/lookup/torus`)
url.searchParams.append('verifier', verifier)
Expand Down
2 changes: 1 addition & 1 deletion src/registerServiceWorker.js
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ function checkValidServiceWorker(swUrl, config) {
const contentType = response.headers.get('content-type')
if (response.status === 404 || (contentType != null && !contentType.includes('javascript'))) {
// No service worker found. Probably a different app. Reload the page.
// eslint-disable-next-line promise/no-nesting

navigator.serviceWorker.ready
.then((registration) => registration.unregister())
.then(() => {
Expand Down
3 changes: 3 additions & 0 deletions src/store/preferencesActions.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,9 @@ export default {
getEnsOrUnstoppableAddress(_, payload) {
return prefsController.getEnsOrUnstoppableAddress(payload)
},
getRnsAddress(_, payload) {
return prefsController.getRnsAddress(payload)
},
getTorusLookupAddress(_, payload) {
return prefsController.getTorusLookupAddress(payload)
},
Expand Down
6 changes: 6 additions & 0 deletions src/utils/enums.js
Original file line number Diff line number Diff line change
Expand Up @@ -560,6 +560,7 @@ export const EMAIL_PASSWORDLESS_LOGIN_PROVIDER = 'email_passwordless'
export const LINKED_VERIFIER_SUBIDENTIFIER = process.env.VUE_APP_LINKED_VERIFIER_SUBIDENTIFIER

export const ENS = 'ENS'
export const RNS = 'RNS'
export const UNSTOPPABLE_DOMAINS = 'Unstoppable_Domains'
export const ETH_LABEL = 'walletSettings.ethAddress'
export const GOOGLE_LABEL = 'walletSettings.googleId'
Expand All @@ -568,6 +569,7 @@ export const REDDIT_LABEL = 'walletSettings.redditId'
export const DISCORD_LABEL = 'walletSettings.discordId'
export const TWITCH_LABEL = 'walletSettings.twitchId'
export const ENS_LABEL = 'walletSettings.ensId'
export const RNS_LABEL = 'walletSettings.rnsId'
export const UNSTOPPABLE_DOMAINS_LABEL = 'walletSettings.unstoppableDomainsId'
export const TWITTER_LABEL = 'walletSettings.twitterId'
export const GITHUB_LABEL = 'walletSettings.githubId'
Expand Down Expand Up @@ -621,6 +623,10 @@ export const ALLOWED_VERIFIERS = [
name: ENS_LABEL,
value: ENS,
},
{
name: RNS_LABEL,
value: RNS,
},
{
name: TWITTER_LABEL,
value: TWITTER,
Expand Down

0 comments on commit ae2e9ca

Please sign in to comment.