Skip to content

Commit b124425

Browse files
1.29.0-0.5.3: [feature] Adds ens support (#618)
* Adds ens support * Fix PR template typo
1 parent 34f3e42 commit b124425

File tree

9 files changed

+1779
-60
lines changed

9 files changed

+1779
-60
lines changed

package.json

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "bnc-onboard",
3-
"version": "1.29.0-0.4.3",
3+
"version": "1.29.0-0.5.3",
44
"description": "Onboard users to web3 by allowing them to select a wallet, get that wallet ready to transact and have access to synced wallet state.",
55
"keywords": [
66
"ethereum",
@@ -54,11 +54,12 @@
5454
},
5555
"dependencies": {
5656
"@cvbb/eth-keyring": "^1.1.0",
57+
"@ensdomains/ensjs": "^2.0.1",
58+
"@ethereumjs/common": "^2.0.0",
59+
"@ethereumjs/tx": "^3.0.0",
5760
"@gnosis.pm/safe-apps-provider": "^0.5.0",
5861
"@gnosis.pm/safe-apps-sdk": "^3.0.0",
5962
"@ledgerhq/hw-app-eth": "^5.49.0",
60-
"@ethereumjs/common": "^2.0.0",
61-
"@ethereumjs/tx": "^3.0.0",
6263
"@ledgerhq/hw-transport-u2f": "^5.21.0",
6364
"@ledgerhq/hw-transport-webusb": "5.53.0",
6465
"@portis/web3": "^4.0.0",

pull_request_template.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,4 @@
55
- [ ] The version field in `package.json` is incremented following [semantic versioning](https://semver.org/)
66
- [ ] The box that allows repo maintainers to update this PR is checked
77
- [ ] I tested locally to make sure this feature/fix works
8-
- [ ] This PR passes the Cicle CI checks
8+
- [ ] This PR passes the Circle CI checks

rollup.config.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@ export default {
9090
'@shapeshiftoss/hdwallet-keepkey-webusb',
9191
'@shapeshiftoss/hdwallet-core',
9292
'@gnosis.pm/safe-apps-sdk',
93-
'@gnosis.pm/safe-apps-provider'
93+
'@gnosis.pm/safe-apps-provider',
94+
'@ensdomains/ensjs'
9495
]
9596
}

src/@types/index.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ declare module '@ledgerhq/hw-app-eth'
1616
declare module '@ledgerhq/hw-transport-u2f'
1717
declare module '@ledgerhq/hw-transport-webusb'
1818
declare module 'eth-provider'
19+
declare module '@ensdomains/ensjs'
1920

2021
declare module '@shapeshiftoss/hdwallet-core'
2122
declare module '@shapeshiftoss/hdwallet-keepkey-webusb'

src/interfaces.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ export interface Initialization {
1313

1414
export interface Subscriptions {
1515
address?: (address: string) => void
16+
ens?: (ens: Ens) => void
1617
network?: (networkId: number) => void
1718
balance?: (balance: string) => void
1819
wallet?: (wallet: Wallet) => void
@@ -559,3 +560,9 @@ export interface TermsAgreementState {
559560
terms?: boolean
560561
privacy?: boolean
561562
}
563+
564+
export interface Ens {
565+
name?: string
566+
contentHash?: string
567+
getText?: (key: string) => Promise<string | undefined>
568+
}

src/onboard.ts

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import 'regenerator-runtime/runtime'
22

3-
import { get } from 'svelte/store'
3+
import { get, derived } from 'svelte/store'
44

55
import Onboard from './views/Onboard.svelte'
66

@@ -16,7 +16,7 @@ import {
1616
initializeStores
1717
} from './stores'
1818

19-
import { getDeviceInfo } from './utilities'
19+
import { getDeviceInfo, getEns } from './utilities'
2020
import { validateInit, validateConfig, isWalletInit } from './validation'
2121

2222
import { version } from '../package.json'
@@ -28,7 +28,8 @@ import {
2828
ConfigOptions,
2929
UserState,
3030
Wallet,
31-
WalletInitOptions
31+
WalletInitOptions,
32+
Ens
3233
} from './interfaces'
3334

3435
import initializeModules from './modules'
@@ -145,6 +146,21 @@ function init(initialization: Initialization): API {
145146
})
146147
}
147148

149+
if (subscriptions.ens) {
150+
derived([address, wallet], ([$address, $wallet], set) => {
151+
if ($address && $wallet && $wallet.provider) {
152+
getEns($wallet.provider, $address).then(set)
153+
} else {
154+
// Wallet not selected or reset
155+
set(undefined)
156+
}
157+
}).subscribe(ens => {
158+
if (ens !== null) {
159+
subscriptions.ens && subscriptions.ens(ens as Ens)
160+
}
161+
})
162+
}
163+
148164
if (subscriptions.network) {
149165
network.subscribe((networkId: number | null) => {
150166
if (networkId !== null) {

src/utilities.ts

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
import bowser from 'bowser'
22
import BigNumber from 'bignumber.js'
33
import { get } from 'svelte/store'
4+
import ENS, { getEnsAddress } from '@ensdomains/ensjs'
45

56
import { app } from './stores'
6-
import { WalletInterface } from './interfaces'
7+
import { WalletInterface, Ens } from './interfaces'
78

89
export function getNetwork(provider: any): Promise<number | any> {
910
return new Promise((resolve, reject) => {
@@ -67,6 +68,27 @@ export function getAddress(provider: any): Promise<string | any> {
6768
})
6869
}
6970

71+
export async function getEns(provider: any, address: string): Promise<Ens> {
72+
const { networkId } = get(app)
73+
const ens = new ENS({ provider, ensAddress: getEnsAddress(networkId) })
74+
let name
75+
let nameInterface
76+
let contentHash
77+
try {
78+
;({ name } = await ens.getName(address))
79+
nameInterface = await ens.name(name)
80+
contentHash = await nameInterface?.getContent()
81+
} catch (e) {
82+
// Error getting ens name
83+
}
84+
85+
return {
86+
name,
87+
contentHash,
88+
getText: nameInterface?.getText.bind(nameInterface)
89+
}
90+
}
91+
7092
export function getBalance(
7193
provider: any,
7294
address?: string

src/validation.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,8 @@ export function validateInit(init: Initialization): never | void {
161161
}
162162

163163
function validateSubscriptions(subscriptions: Subscriptions): never | void {
164-
const { address, network, balance, wallet, ...otherParams } = subscriptions
164+
const { address, ens, network, balance, wallet, ...otherParams } =
165+
subscriptions
165166

166167
invalidParams(otherParams, validSubscriptionKeys, 'subscriptions')
167168

@@ -172,6 +173,13 @@ function validateSubscriptions(subscriptions: Subscriptions): never | void {
172173
optional: true
173174
})
174175

176+
validateType({
177+
name: 'subscriptions.ens',
178+
value: ens,
179+
type: 'function',
180+
optional: true
181+
})
182+
175183
validateType({
176184
name: 'subscriptions.network',
177185
value: network,

0 commit comments

Comments
 (0)