Skip to content

Commit af963a4

Browse files
committed
Add RNS multichchain resolution in Transfer page
1 parent fa94606 commit af963a4

6 files changed

Lines changed: 88 additions & 2 deletions

File tree

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
"@metamask/contract-metadata": "^2.2.0",
3838
"@metamask/eth-sig-util": "^5.0.2",
3939
"@metamask/obs-store": "^7.0.0",
40+
"@rsksmart/rns-resolver.js": "^1.1.0",
4041
"@rsksmart/rsk-utils": "^1.1.0",
4142
"@sentry/browser": "^7.41.0",
4243
"@sentry/tracing": "^7.41.0",
@@ -83,6 +84,7 @@
8384
"lodash": "^4.17.21",
8485
"loglevel": "^1.8.1",
8586
"multi-token-standard-abi": "1.0.4",
87+
"node-fetch": "^2.0.0",
8688
"pify": "^5.0.0",
8789
"pump": "^3.0.0",
8890
"readable-stream": "^3.6.0",

src/components/WalletTransfer/AddContact/AddContact.vue

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,8 @@ import {
5454
GOOGLE_LABEL,
5555
REDDIT,
5656
REDDIT_LABEL,
57+
RNS,
58+
RNS_LABEL,
5759
TWITTER,
5860
TWITTER_LABEL,
5961
UNSTOPPABLE_DOMAINS,
@@ -68,6 +70,7 @@ const VERIFIER_LABELS = {
6870
[TWITTER]: TWITTER_LABEL,
6971
[GITHUB]: GITHUB_LABEL,
7072
[ENS]: ENS_LABEL,
73+
[RNS]: RNS_LABEL,
7174
[BIT]: BIT_LABEL,
7275
[UNSTOPPABLE_DOMAINS]: UNSTOPPABLE_DOMAINS_LABEL,
7376
}

src/containers/WalletTransfer/WalletTransfer.vue

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@
141141
:items="getToAddressComboboxItems"
142142
:placeholder="verifierPlaceholder"
143143
required
144-
:rules="[contactRule, rules.contactRequired, ensRule, unstoppableDomainsRule, bitRule, torusRule]"
144+
:rules="[contactRule, rules.contactRequired, ensRule, rnsRule, unstoppableDomainsRule, bitRule, torusRule]"
145145
outlined
146146
item-text="name"
147147
item-value="value"
@@ -544,6 +544,7 @@ import {
544544
MESSAGE_MODAL_TYPE_SUCCESS,
545545
OLD_ERC721_LIST,
546546
REDDIT,
547+
RNS,
547548
TRANSACTION_SPEED,
548549
TWITTER,
549550
UNSTOPPABLE_DOMAINS,
@@ -594,6 +595,7 @@ export default {
594595
toAddress: '',
595596
formValid: false,
596597
ensError: '',
598+
rnsError: '',
597599
bitError: '',
598600
torusError: '',
599601
unstoppableDomainsError: '',
@@ -848,7 +850,7 @@ export default {
848850
this.$vuetify.goTo(0)
849851
},
850852
methods: {
851-
...mapActions(['getTorusLookupAddress', 'getEnsOrUnstoppableAddress']),
853+
...mapActions(['getTorusLookupAddress', 'getEnsOrUnstoppableAddress', 'getRnsAddress']),
852854
startQrScanning() {
853855
this.camera = 'auto'
854856
this.showQrScanner = true
@@ -862,6 +864,8 @@ export default {
862864
this.selectedVerifier = GOOGLE
863865
} else if (/.eth$/.test(toAddress) || /.xyz$/.test(toAddress) || /.kred$/i.test(toAddress)) {
864866
this.selectedVerifier = ENS
867+
} else if (/.rsk$/.test(toAddress)) {
868+
this.selectedVerifier = RNS
865869
} else if (/.crypto$/.test(toAddress)) {
866870
this.selectedVerifier = UNSTOPPABLE_DOMAINS
867871
} else if (new RegExp(`${this.bitTail}$`).test(toAddress)) {
@@ -971,6 +975,9 @@ export default {
971975
ensRule() {
972976
return this.selectedVerifier === ENS && this.ensError ? this.ensError : true
973977
},
978+
rnsRule() {
979+
return this.selectedVerifier === RNS && this.rnsError ? this.rnsError : true
980+
},
974981
bitRule() {
975982
return this.selectedVerifier === BIT && this.bitError ? this.bitError : true
976983
},
@@ -1085,6 +1092,7 @@ export default {
10851092
}
10861093
}
10871094
this.ensError = ''
1095+
this.rnsError = ''
10881096
this.unstoppableDomainsError = ''
10891097
this.torusError = ''
10901098
@@ -1233,6 +1241,17 @@ export default {
12331241
this.ensError = 'walletSettings.invalidEns'
12341242
this.$refs.form.validate()
12351243
}
1244+
} else if (this.selectedVerifier === RNS) {
1245+
try {
1246+
const res = await this.getRnsAddress({ domain: this.toAddress, chainType: this.networkType.type, coinType: this.networkType.ticker })
1247+
log.info(res)
1248+
toAddress = res.toLowerCase()
1249+
this.rnsError = ''
1250+
} catch (error) {
1251+
log.error(error)
1252+
this.rnsError = 'walletSettings.invalidRns'
1253+
}
1254+
this.$refs.form.validate()
12361255
} else if (this.selectedVerifier === UNSTOPPABLE_DOMAINS) {
12371256
try {
12381257
const res = await this.getUnstoppableDomains(this.toAddress)

src/controllers/PreferencesController.js

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
import { hashPersonalMessage } from '@ethereumjs/util'
22
import { ObservableStore } from '@metamask/obs-store'
3+
import Resolver from '@rsksmart/rns-resolver.js'
34
import { SafeEventEmitter } from '@toruslabs/openlogin-jrpc'
45
import deepmerge from 'deepmerge'
56
import { cloneDeep } from 'lodash'
67
import log from 'loglevel'
8+
import nodeFetch from 'node-fetch'
79
import Web3 from 'web3'
810
import { isHexStrict, toHex } from 'web3-utils'
911

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

7276
this.interval = options.interval || DEFAULT_INTERVAL
7377
this.store = new ObservableStore({ selectedAddress: '' }) // Account specific object
@@ -826,6 +830,55 @@ class PreferencesController extends SafeEventEmitter {
826830
return this.api.get(url.href, this.headers(), { useAPIKey: true })
827831
}
828832

833+
// This approach considers resolution for tokens
834+
getRnsChainIdFromPluginId = (pluginId) => {
835+
switch (pluginId) {
836+
case 'RBTC':
837+
return 137
838+
case 'ETH':
839+
return 60
840+
case 'ETHC':
841+
return 61
842+
case 'BTC':
843+
return 0
844+
case 'LTC':
845+
return 2
846+
case 'dash':
847+
return 5
848+
case 'XRP':
849+
return 144
850+
case 'BCH':
851+
return 145
852+
case 'BNB':
853+
return 714
854+
case 'XLM':
855+
return 148
856+
case 'MATIC':
857+
return 966
858+
case 'AVAX':
859+
return 9000
860+
case 'GXC':
861+
return 2303
862+
case 'OKT':
863+
return 996
864+
case 'DAI':
865+
return 700
866+
default:
867+
return -1
868+
}
869+
}
870+
871+
getRnsAddress({ domain, chainType, coinType }) {
872+
if (chainType === 'rsk_testnet') {
873+
return this.rnsAddrResolverTestnet.addr(domain)
874+
}
875+
if (chainType === 'rsk_mainnet') {
876+
return this.rnsAddrResolverMainnet.addr(domain)
877+
}
878+
const pluginId = this.getRnsChainIdFromPluginId(coinType)
879+
return chainType.includes('mainnet') ? this.rnsAddrResolverMainnet.addr(domain, pluginId) : this.rnsAddrResolverTestnet.addr(domain, pluginId)
880+
}
881+
829882
async getTorusLookupAddress({ verifier, verifierId, walletVerifier, network }) {
830883
const url = new URL(`${config.api}/lookup/torus`)
831884
url.searchParams.append('verifier', verifier)

src/store/preferencesActions.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,9 @@ export default {
5353
getEnsOrUnstoppableAddress(_, payload) {
5454
return prefsController.getEnsOrUnstoppableAddress(payload)
5555
},
56+
getRnsAddress(_, payload) {
57+
return prefsController.getRnsAddress(payload)
58+
},
5659
getTorusLookupAddress(_, payload) {
5760
return prefsController.getTorusLookupAddress(payload)
5861
},

src/utils/enums.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -560,6 +560,7 @@ export const EMAIL_PASSWORDLESS_LOGIN_PROVIDER = 'email_passwordless'
560560
export const LINKED_VERIFIER_SUBIDENTIFIER = process.env.VUE_APP_LINKED_VERIFIER_SUBIDENTIFIER
561561

562562
export const ENS = 'ENS'
563+
export const RNS = 'RNS'
563564
export const UNSTOPPABLE_DOMAINS = 'Unstoppable_Domains'
564565
export const ETH_LABEL = 'walletSettings.ethAddress'
565566
export const GOOGLE_LABEL = 'walletSettings.googleId'
@@ -568,6 +569,7 @@ export const REDDIT_LABEL = 'walletSettings.redditId'
568569
export const DISCORD_LABEL = 'walletSettings.discordId'
569570
export const TWITCH_LABEL = 'walletSettings.twitchId'
570571
export const ENS_LABEL = 'walletSettings.ensId'
572+
export const RNS_LABEL = 'walletSettings.rnsId'
571573
export const UNSTOPPABLE_DOMAINS_LABEL = 'walletSettings.unstoppableDomainsId'
572574
export const TWITTER_LABEL = 'walletSettings.twitterId'
573575
export const GITHUB_LABEL = 'walletSettings.githubId'
@@ -621,6 +623,10 @@ export const ALLOWED_VERIFIERS = [
621623
name: ENS_LABEL,
622624
value: ENS,
623625
},
626+
{
627+
name: RNS_LABEL,
628+
value: RNS,
629+
},
624630
{
625631
name: TWITTER_LABEL,
626632
value: TWITTER,

0 commit comments

Comments
 (0)