Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions examples/with-vuejs-v2/src/components/HelloWorld.vue
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
<div class="avatar" />
<div class="details">
<div v-if="ens">{{ ens.name }}</div>
<div v-if="uns">{{ uns.name }}</div>
<div v-if="address">{{ address }}</div>

<span>Connected Wallet: {{ connectedWallet.label }}</span>
Expand Down Expand Up @@ -83,6 +84,14 @@ export default {
return trunc(this.connectedWallet.accounts[0].ens);
}
},
uns: function () {
if (
this.connectedWallet.accounts &&
this.connectedWallet.accounts[0].uns?.name
) {
return trunc(this.connectedWallet.accounts[0].uns);
}
},
},
};
</script>
Expand Down
3 changes: 3 additions & 0 deletions packages/core/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -510,6 +510,9 @@ type Account = {
contentHash?: string
getText?: (key: string) => Promise<string | undefined>
}
uns: {
name?: string
}
balance: Record<TokenSymbol, string>
}

Expand Down
6 changes: 4 additions & 2 deletions packages/core/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@web3-onboard/core",
"version": "2.12.1",
"version": "2.13.0-alpha.1",
"description": "Web3-Onboard makes it simple to connect Ethereum hardware and software wallets to your dapp. Features standardized spec compliant web3 providers for all supported wallets, framework agnostic modern javascript UI with code splitting, CSS customization, multi-chain and multi-account support, reactive wallet state subscriptions and real-time transaction state change notifications.",
"keywords": [
"Ethereum",
Expand Down Expand Up @@ -60,8 +60,9 @@
"license": "MIT",
"devDependencies": {
"@rollup-extras/plugin-copy": "~1.2.2",
"@rollup/plugin-commonjs": "^23.0.4",
"@rollup/plugin-json": "^4.1.0",
"@rollup/plugin-node-resolve": "^11.0.0",
"@rollup/plugin-node-resolve": "^13.0.6",
"@rollup/plugin-replace": "^3.0.0",
"@rollup/plugin-typescript": "^8.0.0",
"@tsconfig/svelte": "^2.0.0",
Expand All @@ -84,6 +85,7 @@
"typescript": "^4.5.5"
},
"dependencies": {
"@unstoppabledomains/resolution": "^8.0",
"@web3-onboard/common": "^2.2.3",
"bignumber.js": "^9.0.0",
"bnc-sdk": "^4.6.2",
Expand Down
4 changes: 4 additions & 0 deletions packages/core/rollup.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import json from '@rollup/plugin-json'
import sveltePreprocess from 'svelte-preprocess'
import typescript from '@rollup/plugin-typescript'
import copy from '@rollup-extras/plugin-copy'
import commonjs from '@rollup/plugin-commonjs'

const production = !process.env.ROLLUP_WATCH

Expand Down Expand Up @@ -35,6 +36,9 @@ export default {
sourceMap: !production,
inlineSources: !production
}),
commonjs({
include: /node_modules/
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@V-Shadbolt quick question here, can you provide a little background on the reason for this?
Also, can it be scoped to the ustoppabledomains package?
Thanks!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Adamj1232 Yup! This was added because rollup wasn't including the unstoppable domains node module when compiling which threw a bunch of errors when the functions were called.

The commonjs plugin instructs rollup to convert node modules to ES6 so that they can be interpreted by browsers and the include: /node_modules/ flag is for the directory of the modules to include during compilation

I don't believe this can be scoped back unless we can specify just the unstoppable module with something like this: include: /node_modules/@unstoppabledomains/.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah gotcha, thank you for the fast response! include: /node_modules/@unstoppabledomains/ is what I had in mind.
I can open a PR or you can but want to get your teams approval on the changes 🤓

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey! Managed to get it rescoped to just Unstoppable using the below snippet while retaining the original functionality of this PR.

commonjs({ namedExports: { '../../node_modules/@unstoppabledomains/resolution/build/index.js': [ 'Resolution' ] }, }),

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

PR opened to fix this issue: #1476

}),
copy({
src: 'src/i18n/en.json',
dest: 'i18n'
Expand Down
74 changes: 64 additions & 10 deletions packages/core/src/provider.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
import { fromEventPattern, Observable } from 'rxjs'
import { filter, takeUntil, take, share, switchMap } from 'rxjs/operators'
import partition from 'lodash.partition'
import { providers } from 'ethers'
import { providers, utils } from 'ethers'
import { weiToEth } from '@web3-onboard/common'
import { disconnectWallet$ } from './streams.js'
import { updateAccount, updateWallet } from './store/actions.js'
import { validEnsChain } from './utils.js'
import disconnect from './disconnect.js'
import { state } from './store/index.js'
import { getBNMulitChainSdk } from './services.js'
import { Resolution } from '@unstoppabledomains/resolution'

import type {
ChainId,
Expand All @@ -26,6 +27,7 @@ import type {
Address,
Balances,
Ens,
Uns,
WalletPermission,
WalletState
} from './types.js'
Expand Down Expand Up @@ -146,10 +148,15 @@ export function trackWallet(
account => account.address === address
)

// update accounts without ens and balance first
// update accounts without ens/uns and balance first
updateWallet(label, {
accounts: [
existingAccount || { address: address, ens: null, balance: null },
existingAccount || {
address: address,
ens: null,
uns: null,
balance: null
},
...restAccounts
]
})
Expand All @@ -176,7 +183,7 @@ export function trackWallet(
}
})

// also when accounts change, update Balance and ENS
// also when accounts change, update Balance and ENS/UNS
accountsChanged$
.pipe(
switchMap(async ([address]) => {
Expand Down Expand Up @@ -205,13 +212,23 @@ export function trackWallet(
? getEns(address, chain)
: Promise.resolve(null)

return Promise.all([Promise.resolve(address), balanceProm, ensProm])
const unsProm =
account && account.uns
? Promise.resolve(account.uns)
: getUns(address, chain)

return Promise.all([
Promise.resolve(address),
balanceProm,
ensProm,
unsProm
])
})
)
.subscribe(res => {
if (!res) return
const [address, balance, ens] = res
updateAccount(label, address, { balance, ens })
const [address, balance, ens, uns] = res
updateAccount(label, address, { balance, ens, uns })
})

const chainChanged$ = listenChainChanged({ provider, disconnected$ }).pipe(
Expand Down Expand Up @@ -264,6 +281,7 @@ export function trackWallet(
({
address,
ens: null,
uns: null,
balance: null
} as Account)
)
Expand All @@ -274,7 +292,7 @@ export function trackWallet(
})
})

// when chain changes get ens and balance for each account for wallet
// when chain changes get ens/uns and balance for each account for wallet
chainChanged$
.pipe(
switchMap(async chainId => {
Expand All @@ -293,12 +311,21 @@ export function trackWallet(
? getEns(address, chain)
: Promise.resolve(null)

const [balance, ens] = await Promise.all([balanceProm, ensProm])
const unsProm = validEnsChain(chainId)
? getUns(address, chain)
: Promise.resolve(null)

const [balance, ens, uns] = await Promise.all([
balanceProm,
ensProm,
unsProm
])

return {
address,
balance,
ens
ens,
uns
}
})
)
Expand Down Expand Up @@ -353,6 +380,33 @@ export async function getEns(
}
}

export async function getUns(
address: Address,
chain: Chain
): Promise<Uns | null> {
// check if address is valid ETH address before attempting to resolve
// chain we don't recognize and don't have a rpcUrl for requests
if (!utils.isAddress(address) || !chain) return null

const resolutionInstance = new Resolution()

try {
const name = await resolutionInstance.reverse(address)
let uns = null

if (name) {
uns = {
name
}
}

return uns
} catch (error) {
console.error(error)
return null
}
}

export async function getBalance(
address: string,
chain: Chain
Expand Down
5 changes: 5 additions & 0 deletions packages/core/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ export interface WalletState {
export type Account = {
address: Address
ens: Ens | null
uns: Uns | null
balance: Balances | null
}

Expand All @@ -117,6 +118,10 @@ export interface Ens {
getText: (key: string) => Promise<string | undefined>
}

export interface Uns {
name: string
}

export type Avatar = {
url: string
linkage: Array<{ type: string; content: string }>
Expand Down
6 changes: 4 additions & 2 deletions packages/core/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,10 @@ export function shortenAddress(add: string): string {
return `${add.slice(0, 6)}...${add.slice(-4)}`
}

export function shortenEns(ens: string): string {
return ens.length > 11 ? `${ens.slice(0, 4)}...${ens.slice(-6)}` : ens
export function shortenDomain(domain: string): string {
return domain.length > 11
? `${domain.slice(0, 4)}...${domain.slice(-6)}`
: domain
}

export async function copyWalletAddress(text: string): Promise<void> {
Expand Down
8 changes: 8 additions & 0 deletions packages/core/src/validation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,13 @@ const ens = Joi.any().allow(
null
)

const uns = Joi.any().allow(
Joi.object({
name: Joi.string().required()
}),
null
)

const balance = Joi.any().allow(
Joi.object({
eth: Joi.number()
Expand All @@ -56,6 +63,7 @@ const balance = Joi.any().allow(
const account = Joi.object({
address: Joi.string().required(),
ens,
uns,
balance
})

Expand Down
13 changes: 10 additions & 3 deletions packages/core/src/views/account-center/Minimized.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import {
getDefaultChainStyles,
shortenAddress,
shortenEns,
shortenDomain,
unrecognizedChainStyle
} from '../../utils.js'
import { updateAccountCenter } from '../../store/actions.js'
Expand All @@ -25,7 +25,10 @@
$: [firstAccount] = primaryWallet ? primaryWallet.accounts : []

$: ensName =
firstAccount && firstAccount.ens && shortenEns(firstAccount.ens.name)
firstAccount && firstAccount.ens && shortenDomain(firstAccount.ens.name)

$: unsName =
firstAccount && firstAccount.uns && shortenDomain(firstAccount.uns.name)

$: shortenedFirstAddress = firstAccount
? shortenAddress(firstAccount.address)
Expand Down Expand Up @@ -179,7 +182,11 @@
<!-- address and balance -->
<div class="flex flex-column" style="height: 40px;">
<div class="address">
{ensName ? shortenEns(ensName) : shortenedFirstAddress}
{ensName
? shortenDomain(ensName)
: unsName
? shortenDomain(unsName)
: shortenedFirstAddress}
</div>
{#if firstAddressBalance}
<div in:fade class="balance">
Expand Down
28 changes: 19 additions & 9 deletions packages/core/src/views/account-center/WalletRow.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,11 @@
import { fade } from 'svelte/transition'
import { ProviderRpcErrorCode } from '@web3-onboard/common'
import type { WalletState } from '../../types.js'
import { shortenAddress, shortenEns, copyWalletAddress } from '../../utils.js'
import {
shortenAddress,
shortenDomain,
copyWalletAddress
} from '../../utils.js'
import en from '../../i18n/en.json'
import SuccessStatusIcon from '../shared/SuccessStatusIcon.svelte'
import WalletAppBadge from '../shared/WalletAppBadge.svelte'
Expand Down Expand Up @@ -87,7 +91,7 @@
);
}

.address-ens {
.address-domain {
margin-left: 0.5rem;
font-weight: 700;
color: var(
Expand Down Expand Up @@ -156,7 +160,7 @@
}
</style>

{#each wallet.accounts as { address, ens, balance }, i}
{#each wallet.accounts as { address, ens, uns, balance }, i}
<div class="relative">
<div
on:click={() => setPrimaryWallet(wallet, address)}
Expand Down Expand Up @@ -188,9 +192,13 @@
{/if}
</div>

<!-- ADDRESS / ENS -->
<span class="address-ens"
>{ens ? shortenEns(ens.name) : shortenAddress(address)}</span
<!-- ADDRESS / DOMAIN -->
<span class="address-domain"
>{ens
? shortenDomain(ens.name)
: uns
? shortenDomain(uns.name)
: shortenAddress(address)}</span
>
</div>

Expand Down Expand Up @@ -249,9 +257,11 @@
</li>
<li
on:click|stopPropagation={() => {
copyWalletAddress(ens ? ens.name : address).then(() => {
changeText()
})
copyWalletAddress(ens ? ens.name : uns ? uns.name : address).then(
() => {
changeText()
}
)
}}
>
{en.accountCenter.copyAddress}
Expand Down
Loading