Skip to content

Commit ff0e2ca

Browse files
committed
Fix buffer errors
1 parent bd77d12 commit ff0e2ca

File tree

2 files changed

+19
-69
lines changed

2 files changed

+19
-69
lines changed

src/modules/select/wallets/ledger.ts

Lines changed: 12 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ import TransportU2F from '@ledgerhq/hw-transport-u2f'
1212
import Eth from '@ledgerhq/hw-app-eth'
1313
import * as EthereumTx from 'ethereumjs-tx'
1414

15+
import buffer from 'buffer'
16+
1517
function ledger(options: LedgerOptions & CommonWalletOptions): WalletModule {
1618
const { rpcUrl, networkId, preferred, label, iconSrc, svg } = options
1719

@@ -36,23 +38,15 @@ function ledger(options: LedgerOptions & CommonWalletOptions): WalletModule {
3638
connect: provider.enable,
3739
disconnect: () => provider.stop(),
3840
address: {
39-
onChange: async func => {
40-
const address = await provider.getPrimaryAddress()
41-
func(address)
42-
provider.on('accountsChanged', (accounts: Array<string>) =>
43-
func(accounts[0])
44-
)
45-
}
41+
get: async () => provider.getPrimaryAddress()
4642
},
4743
network: {
48-
onChange: func => {
49-
func(networkId)
50-
}
44+
get: async () => networkId
5145
},
5246
balance: {
5347
get: async () => {
54-
const address = await provider.getPrimaryAddress()
55-
return provider.getBalance(address)
48+
const address = provider.getPrimaryAddress()
49+
return address && provider.getBalance(address)
5650
}
5751
}
5852
}
@@ -74,8 +68,6 @@ async function ledgerProvider(options: {
7468
const basePath = networkIdToDerivationPath(networkId)
7569

7670
let addressToPath = new Map()
77-
let listeners = new Map()
78-
7971
let enabled: boolean = false
8072

8173
const provider = createProvider({
@@ -92,34 +84,26 @@ async function ledgerProvider(options: {
9284
rpcUrl
9385
})
9486

95-
provider.on('error', (err: any) => console.log('provider error', err))
96-
9787
provider.getPrimaryAddress = getPrimaryAddress
9888
provider.getAllAccountsAndBalances = getAllAccountsAndBalances
9989
provider.enable = enable
100-
provider.on = on
10190
provider.setPrimaryAccount = setPrimaryAccount
10291
provider.getBalance = getBalance
10392
provider.send = provider.sendAsync
10493

10594
function enable() {
95+
const buff = buffer.Buffer.from('4')
96+
console.log({ buff })
10697
enabled = true
10798
return getAccounts(1)
10899
}
109100

110-
function on(
111-
event: 'accountsChanged',
112-
callback: (accounts: Array<string>) => void
113-
) {
114-
listeners.set(event, callback)
115-
}
116-
117101
function addresses() {
118102
return Array.from(addressToPath.keys())
119103
}
120104

121105
function getPrimaryAddress() {
122-
return addresses()[0]
106+
return enabled ? addresses()[0] : undefined
123107
}
124108

125109
async function getAllAccountsAndBalances(amountToGet: number = 5) {
@@ -145,10 +129,6 @@ async function ledgerProvider(options: {
145129
accounts.unshift(accounts.splice(accountIndex, 1)[0])
146130
// reassign addressToPath to new ordered accounts
147131
addressToPath = new Map(accounts)
148-
149-
// let listeners know of change
150-
const listener = listeners.get('accountsChanged')
151-
listener && listener(addresses())
152132
}
153133

154134
function getAccounts(
@@ -206,9 +186,6 @@ async function ledgerProvider(options: {
206186

207187
const allAddresses = addresses()
208188

209-
const listener = listeners.get('accountsChanged')
210-
listener && listener(allAddresses)
211-
212189
transport.close()
213190

214191
resolve(allAddresses)
@@ -263,9 +240,9 @@ async function ledgerProvider(options: {
263240
transaction.serialize().toString('hex')
264241
)
265242

266-
transaction.v = Buffer.from(ledgerResult.v, 'hex')
267-
transaction.r = Buffer.from(ledgerResult.r, 'hex')
268-
transaction.s = Buffer.from(ledgerResult.s, 'hex')
243+
transaction.v = buffer.Buffer.from(ledgerResult.v, 'hex')
244+
transaction.r = buffer.Buffer.from(ledgerResult.r, 'hex')
245+
transaction.s = buffer.Buffer.from(ledgerResult.s, 'hex')
269246

270247
return `0x${transaction.serialize().toString('hex')}`
271248
} catch (error) {

src/modules/select/wallets/trezor.ts

Lines changed: 7 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -48,23 +48,15 @@ function trezor(options: TrezorOptions & CommonWalletOptions): WalletModule {
4848
connect: provider.enable,
4949
disconnect: () => provider.stop(),
5050
address: {
51-
onChange: async func => {
52-
const address = await provider.getPrimaryAddress()
53-
func(address)
54-
provider.on('accountsChanged', (accounts: Array<string>) =>
55-
func(accounts[0])
56-
)
57-
}
51+
get: async () => provider.getPrimaryAddress()
5852
},
5953
network: {
60-
onChange: func => {
61-
func(networkId)
62-
}
54+
get: async () => networkId
6355
},
6456
balance: {
6557
get: async () => {
66-
const address = await provider.getPrimaryAddress()
67-
return provider.getBalance(address)
58+
const address = provider.getPrimaryAddress()
59+
return address && provider.getBalance(address)
6860
}
6961
}
7062
}
@@ -88,8 +80,6 @@ async function trezorProvider(options: {
8880
const basePath = networkIdToDerivationPath(networkId)
8981

9082
let addressToPath = new Map()
91-
let listeners = new Map()
92-
9383
let enabled: boolean = false
9484

9585
TrezorConnect.manifest({
@@ -99,8 +89,8 @@ async function trezorProvider(options: {
9989

10090
TrezorConnect.on(DEVICE_EVENT, (event: any) => {
10191
if (event.type === DEVICE.DISCONNECT) {
102-
const listener = listeners.get('accountsChanged')
103-
listener && listener([undefined])
92+
enabled = false
93+
addressToPath = new Map()
10494
}
10595
})
10696

@@ -118,12 +108,9 @@ async function trezorProvider(options: {
118108
rpcUrl
119109
})
120110

121-
provider.on('error', (err: any) => console.log('provider error', err))
122-
123111
provider.getPrimaryAddress = getPrimaryAddress
124112
provider.getAllAccountsAndBalances = getAllAccountsAndBalances
125113
provider.enable = enable
126-
provider.on = on
127114
provider.setPrimaryAccount = setPrimaryAccount
128115
provider.getBalance = getBalance
129116
provider.send = provider.sendAsync
@@ -133,19 +120,12 @@ async function trezorProvider(options: {
133120
return getAccounts(1)
134121
}
135122

136-
function on(
137-
event: 'accountsChanged',
138-
callback: (accounts: Array<string>) => void
139-
) {
140-
listeners.set(event, callback)
141-
}
142-
143123
function addresses() {
144124
return Array.from(addressToPath.keys())
145125
}
146126

147127
function getPrimaryAddress() {
148-
return addresses()[0]
128+
return enabled ? addresses()[0] : undefined
149129
}
150130

151131
async function getAllAccountsAndBalances(amountToGet: number = 10) {
@@ -171,10 +151,6 @@ async function trezorProvider(options: {
171151
accounts.unshift(accounts.splice(accountIndex, 1)[0])
172152
// reassign addressToPath to new ordered accounts
173153
addressToPath = new Map(accounts)
174-
175-
// let listeners know of change
176-
const listener = listeners.get('accountsChanged')
177-
listener && listener(addresses())
178154
}
179155

180156
async function getAccounts(accountsToGet: number = 10, getMore?: boolean) {
@@ -228,9 +204,6 @@ async function trezorProvider(options: {
228204

229205
const allAddresses = addresses()
230206

231-
const listener = listeners.get('accountsChanged')
232-
listener && listener(allAddresses)
233-
234207
return allAddresses
235208
}
236209

0 commit comments

Comments
 (0)