Skip to content

Commit 1c1667c

Browse files
committed
Trezor implementation working
1 parent 499bdbc commit 1c1667c

File tree

4 files changed

+74
-53
lines changed

4 files changed

+74
-53
lines changed

src/modules/check/accounts.ts

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,15 @@ function accountSelect(): WalletCheckModule {
1414
return async (
1515
stateAndHelpers: StateAndHelpers
1616
): Promise<WalletCheckModal | undefined> => {
17-
const { wallet, BigNumber, address, balance } = stateAndHelpers
17+
const { wallet, BigNumber } = stateAndHelpers
1818
const { provider, type } = wallet
1919

2020
if (type === 'hardware' && !completed) {
21-
if (accountsAndBalances.length <= 1 && !loadingAccounts) {
22-
accountsAndBalances = [{ address, balance }]
21+
if (accountsAndBalances.length === 0) {
22+
loadingAccounts = true
23+
const accounts = await provider.getAccounts()
24+
accountsAndBalances = await provider.getBalances(accounts)
25+
loadingAccounts = false
2326
}
2427

2528
const deleteWindowProperties = () => {
@@ -29,8 +32,7 @@ function accountSelect(): WalletCheckModule {
2932

3033
const loadMoreAccounts = async () => {
3134
loadingAccounts = true
32-
const moreAccounts = await provider.getAllAccountsAndBalances()
33-
accountsAndBalances = moreAccounts
35+
accountsAndBalances = await provider.getMoreAccounts()
3436
loadingAccounts = false
3537
}
3638

src/modules/check/derivation-path.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,14 @@ const baseStyles = `
2323
background: inherit;
2424
font-size: 0.889em;
2525
font-family: inherit;
26-
border: 1px solid #282828;
26+
border-width: 1px;
27+
border-style: solid;
28+
border-color: inherit;
2729
border-radius: 40px;
2830
margin-top: 0.5rem;
2931
padding: 0.55em 1.4em;
3032
cursor: pointer;
31-
color: #282828;
33+
color: inherit;
3234
font-family: inherit;
3335
transition: background 150ms ease-in-out;
3436
line-height: 1.15;

src/modules/select/wallets/hd-wallet.ts

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,24 +2,29 @@ import HDKey from 'hdkey'
22
import { publicToAddress, toChecksumAddress } from 'ethereumjs-util'
33
import buffer from 'buffer'
44

5+
const numberToGet = 30
6+
57
export function generateAddresses(
6-
publicKey: string,
7-
chainCode: string,
8-
basePath: string,
9-
amount: number = 30
8+
account: {
9+
publicKey: string
10+
chainCode: string
11+
path: string
12+
},
13+
offset: number
1014
) {
15+
const { publicKey, chainCode, path } = account
1116
const hdk = new HDKey()
1217

1318
hdk.publicKey = new buffer.Buffer(publicKey, 'hex')
1419
hdk.chainCode = new buffer.Buffer(chainCode, 'hex')
1520

1621
const addresses = []
1722

18-
for (let i = 0; i < amount; i++) {
23+
for (let i = offset; i < numberToGet + offset; i++) {
1924
const dkey = hdk.deriveChild(i)
2025
const address = publicToAddress(dkey.publicKey, true).toString('hex')
2126
addresses.push({
22-
dPath: `${basePath}/${i}`,
27+
dPath: `${path}/${i}`,
2328
address: toChecksumAddress(address)
2429
})
2530
}

src/modules/select/wallets/trezor.ts

Lines changed: 52 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,8 @@ function trezor(options: TrezorOptions & CommonWalletOptions): WalletModule {
5656
},
5757
balance: {
5858
get: async () => {
59-
const address = provider.getPrimaryAddress()
60-
return address && provider.getBalance(address)
59+
const accounts = provider.getPrimaryAddress()
60+
return accounts[0] && provider.getBalance(accounts[0])
6161
}
6262
}
6363
}
@@ -84,6 +84,10 @@ async function trezorProvider(options: {
8484
let addressToPath = new Map()
8585
let enabled: boolean = false
8686

87+
let account:
88+
| undefined
89+
| { publicKey: string; chainCode: string; path: string }
90+
8791
TrezorConnect.manifest({
8892
email,
8993
appUrl
@@ -110,17 +114,18 @@ async function trezorProvider(options: {
110114
rpcUrl
111115
})
112116

113-
provider.getPrimaryAddress = getPrimaryAddress
114-
provider.getAllAccountsAndBalances = getAllAccountsAndBalances
115117
provider.setPath = setPath
116118
provider.dPath = dPath
117119
provider.enable = enable
118120
provider.setPrimaryAccount = setPrimaryAccount
121+
provider.getPrimaryAddress = getPrimaryAddress
122+
provider.getAccounts = getAccounts
123+
provider.getMoreAccounts = getMoreAccounts
119124
provider.getBalance = getBalance
125+
provider.getBalances = getBalances
120126
provider.send = provider.sendAsync
121127

122128
function setPath(path: string) {
123-
console.log({ path })
124129
dPath = path
125130
}
126131

@@ -133,23 +138,6 @@ async function trezorProvider(options: {
133138
return Array.from(addressToPath.keys())
134139
}
135140

136-
function getPrimaryAddress() {
137-
return enabled ? addresses()[0] : undefined
138-
}
139-
140-
async function getAllAccountsAndBalances() {
141-
const accounts = await getAccounts()
142-
return Promise.all(
143-
accounts.map(
144-
address =>
145-
new Promise(async resolve => {
146-
const balance = await getBalance(address)
147-
resolve({ address, balance })
148-
})
149-
)
150-
)
151-
}
152-
153141
function setPrimaryAccount(address: string) {
154142
// make a copy and put in an array
155143
const accounts = [...addressToPath.entries()]
@@ -162,25 +150,44 @@ async function trezorProvider(options: {
162150
addressToPath = new Map(accounts)
163151
}
164152

165-
async function getAccounts() {
153+
async function getPublicKey() {
154+
const result = await TrezorConnect.getPublicKey({
155+
path: dPath,
156+
coin: 'eth'
157+
})
158+
159+
account = {
160+
publicKey: result.payload.publicKey,
161+
chainCode: result.payload.chainCode,
162+
path: result.payload.serializedPath
163+
}
164+
165+
return account
166+
}
167+
168+
function getPrimaryAddress() {
169+
return enabled ? addresses()[0] : undefined
170+
}
171+
172+
async function getMoreAccounts() {
173+
const accounts = await getAccounts(true)
174+
return getBalances(accounts)
175+
}
176+
177+
async function getAccounts(getMore?: boolean) {
166178
if (!enabled) {
167179
return [undefined]
168180
}
169181

170-
if (addressToPath.size > 0) {
182+
if (addressToPath.size > 0 && !getMore) {
171183
return addresses()
172184
}
173185

174-
const trezorAccount = await TrezorConnect.getPublicKey({
175-
path: dPath,
176-
coin: 'eth'
177-
})
186+
const accountInfo = account || (await getPublicKey())
178187

179-
const addressInfo = generateAddresses(
180-
trezorAccount.payload.publicKey,
181-
trezorAccount.payload.chainCode,
182-
trezorAccount.payload.serializedPath
183-
)
188+
const addressInfo = generateAddresses(accountInfo, addressToPath.size)
189+
190+
console.log({ addressInfo })
184191

185192
addressInfo.forEach(({ dPath, address }) => {
186193
addressToPath.set(address, dPath)
@@ -189,6 +196,18 @@ async function trezorProvider(options: {
189196
return addresses()
190197
}
191198

199+
function getBalances(addresses: Array<string>) {
200+
return Promise.all(
201+
addresses.map(
202+
address =>
203+
new Promise(async resolve => {
204+
const balance = await getBalance(address)
205+
resolve({ address, balance })
206+
})
207+
)
208+
)
209+
}
210+
192211
function getBalance(address: string) {
193212
return new Promise((resolve, reject) => {
194213
provider.sendAsync(
@@ -253,11 +272,4 @@ async function trezorProvider(options: {
253272
return provider
254273
}
255274

256-
function networkIdToDerivationPath(networkId: number) {
257-
switch (networkId) {
258-
default:
259-
return `m/44'/60'`
260-
}
261-
}
262-
263275
export default trezor

0 commit comments

Comments
 (0)