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 Mar 9, 2023
1 parent 01be0ed commit cce05ec
Show file tree
Hide file tree
Showing 5 changed files with 127 additions and 2 deletions.
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, 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
94 changes: 94 additions & 0 deletions src/controllers/PreferencesController.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { ObservableStore } from '@metamask/obs-store'
import { SafeEventEmitter } from '@toruslabs/openlogin-jrpc'
import deepmerge from 'deepmerge'
import { ethErrors } from 'eth-rpc-errors'
import { ethers } from 'ethers'
import { cloneDeep } from 'lodash'
import log from 'loglevel'
import Web3 from 'web3'
Expand All @@ -19,7 +20,9 @@ import {
ETHERSCAN_SUPPORTED_NETWORKS,
MESSAGE_TYPE,
RPC,
RSK_MAINNET,
SUCCESS_TIME,
SUPPORTED_NETWORK_TYPES,
THEME_LIGHT_BLUE_NAME,
} from '../utils/enums'
import { notifyUser } from '../utils/notifications'
Expand Down Expand Up @@ -73,6 +76,8 @@ class PreferencesController extends SafeEventEmitter {
this.signMessage = signMessage
this.addChainRequests = []
this.switchChainRequests = []
this.rskProvider = null
this.rnsRegistryContract = null

this.interval = options.interval || DEFAULT_INTERVAL
this.store = new ObservableStore({
Expand Down Expand Up @@ -1011,6 +1016,95 @@ 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
}
}

async getRnsAddress({ domain, coinType }) {
try {
const pluginId = this.getRnsChainIdFromPluginId(coinType)
if (pluginId === -1) {
return null
}
if (!this.rskProvider) {
const rskRpcServerUrl = SUPPORTED_NETWORK_TYPES[RSK_MAINNET].rpcUrl
this.rskProvider = new ethers.providers.JsonRpcProvider(rskRpcServerUrl)
}
if (!this.rnsRegistryContract) {
// REF: https://developers.rsk.co/rif/rns/architecture/registry/
const RNS_REGISTRY_ADDRESS = '0xcb868aeabd31e2b66f74e9a55cf064abb31a4ad5' // hardcoded RNS registry address
this.rnsRegistryContract = new ethers.Contract(
RNS_REGISTRY_ADDRESS,
['function resolver(bytes32 node) public view returns (address)'],
this.rskProvider
)
}
const nameHash = ethers.utils.namehash(domain)
const resolverAddress = await this.rnsRegistryContract.resolver(nameHash)
if (resolverAddress === ethers.constants.AddressZero) {
return null
}
// Check if current selected currency is on RSK chain 137 if not check for multichain address
let address = null
if (pluginId === 137) {
const addrResolverContract = new ethers.Contract(
resolverAddress,
['function addr(bytes32 node) public view returns (address)'],
this.rskProvider
)
address = await addrResolverContract.addr(nameHash)
} else {
const multichainAddrResolverContract = new ethers.Contract(
resolverAddress,
['function addr(bytes32 node, uint coinType) external view returns(bytes memory)'],
this.rskProvider
)
address = await multichainAddrResolverContract.addr(nameHash, pluginId)
}
if (address === undefined || address === null) {
return null
}
return address.toLowerCase()
} catch (error) {
log.error(error)
return null
}
}

async getTorusLookupAddress({ verifier, verifierId, walletVerifier, network }) {
const url = new URL(`${config.api}/lookup/torus`)
url.searchParams.append('verifier', verifier)
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 @@ -562,6 +562,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 @@ -570,6 +571,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 @@ -623,6 +625,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 cce05ec

Please sign in to comment.