Skip to content

Commit 6630eb5

Browse files
authored
Merge pull request #289 from blocknative/develop
Release 1.7.1
2 parents 077d561 + e902c32 commit 6630eb5

File tree

4 files changed

+114
-10
lines changed

4 files changed

+114
-10
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "bnc-onboard",
3-
"version": "1.7.0",
3+
"version": "1.7.1",
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",

src/modules/select/wallets/ledger.ts

Lines changed: 38 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import { generateAddresses, isValidPath } from './hd-wallet'
1212
import TransportU2F from '@ledgerhq/hw-transport-u2f'
1313
import Eth from '@ledgerhq/hw-app-eth'
1414
import * as EthereumTx from 'ethereumjs-tx'
15+
import * as ethUtil from 'ethereumjs-util'
1516

1617
import buffer from 'buffer'
1718

@@ -102,6 +103,26 @@ async function ledgerProvider(options: {
102103
.then((res: string) => callback(null, res))
103104
.catch(err => callback(err, null))
104105
},
106+
processMessage: (messageData: any, callback: any) => {
107+
signMessage(messageData)
108+
.then((res: string) => callback(null, res))
109+
.catch(err => callback(err, null))
110+
},
111+
processPersonalMessage: (messageData: any, callback: any) => {
112+
signMessage(messageData)
113+
.then((res: string) => callback(null, res))
114+
.catch(err => callback(err, null))
115+
},
116+
signMessage: (messageData: any, callback: any) => {
117+
signMessage(messageData)
118+
.then((res: string) => callback(null, res))
119+
.catch(err => callback(err, null))
120+
},
121+
signPersonalMessage: (messageData: any, callback: any) => {
122+
signMessage(messageData)
123+
.then((res: string) => callback(null, res))
124+
.catch(err => callback(err, null))
125+
},
105126
rpcUrl
106127
})
107128

@@ -336,14 +357,25 @@ async function ledgerProvider(options: {
336357
}
337358
}
338359

339-
return provider
340-
}
360+
async function signMessage(message: { data: string }): Promise<string> {
361+
if (addressToPath.size === 0) {
362+
await enable()
363+
}
364+
365+
const path = [...addressToPath.values()][0]
341366

342-
function networkIdToDerivationPath(networkId: number) {
343-
switch (networkId) {
344-
default:
345-
return `m/44'/60'`
367+
return eth
368+
.signPersonalMessage(path, ethUtil.stripHexPrefix(message.data))
369+
.then((result: any) => {
370+
let v = (result['v'] - 27).toString(16)
371+
if (v.length < 2) {
372+
v = '0' + v
373+
}
374+
return `0x${result['r']}${result['s']}${v}`
375+
})
346376
}
377+
378+
return provider
347379
}
348380

349381
export default ledger

src/modules/select/wallets/providerEngine.ts

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,29 @@ import SubscriptionSubprovider from 'web3-provider-engine/subproviders/subscript
55
import FilterSubprovider from 'web3-provider-engine/subproviders/filters'
66

77
function createProvider(config: any) {
8-
const { getAccounts, signTransaction, rpcUrl } = config
8+
const {
9+
getAccounts,
10+
signTransaction,
11+
rpcUrl,
12+
processMessage,
13+
processPersonalMessage,
14+
signMessage,
15+
signPersonalMessage
16+
} = config
917

1018
const idMgmt =
11-
getAccounts && new HookedWalletSubprovider({ getAccounts, signTransaction })
19+
getAccounts &&
20+
new HookedWalletSubprovider({
21+
getAccounts,
22+
signTransaction,
23+
processMessage,
24+
processPersonalMessage,
25+
signMessage,
26+
signPersonalMessage
27+
})
1228

1329
const rpcSubProvider = new RpcSource({
14-
rpcUrl: rpcUrl.includes('http') ? rpcUrl : `https://${rpcUrl}`,
30+
rpcUrl: rpcUrl.includes('http') ? rpcUrl : `https://${rpcUrl}`
1531
})
1632

1733
const provider = new Web3ProviderEngine()

src/modules/select/wallets/trezor.ts

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import { generateAddresses, isValidPath } from './hd-wallet'
1111

1212
import * as TrezorConnectLibrary from 'trezor-connect'
1313
import * as EthereumTx from 'ethereumjs-tx'
14+
import * as ethUtil from 'ethereumjs-util'
1415

1516
const { default: TrezorConnect, DEVICE_EVENT, DEVICE } = TrezorConnectLibrary
1617

@@ -115,6 +116,26 @@ async function trezorProvider(options: {
115116
.then((res: string) => callback(null, res))
116117
.catch(err => callback(err, null))
117118
},
119+
processMessage: (messageData: any, callback: any) => {
120+
signMessage(messageData)
121+
.then((res: string) => callback(null, res))
122+
.catch(err => callback(err, null))
123+
},
124+
processPersonalMessage: (messageData: any, callback: any) => {
125+
signMessage(messageData)
126+
.then((res: string) => callback(null, res))
127+
.catch(err => callback(err, null))
128+
},
129+
signMessage: (messageData: any, callback: any) => {
130+
signMessage(messageData)
131+
.then((res: string) => callback(null, res))
132+
.catch(err => callback(err, null))
133+
},
134+
signPersonalMessage: (messageData: any, callback: any) => {
135+
signMessage(messageData)
136+
.then((res: string) => callback(null, res))
137+
.catch(err => callback(err, null))
138+
},
118139
rpcUrl
119140
})
120141

@@ -332,6 +353,10 @@ async function trezorProvider(options: {
332353
}
333354

334355
async function signTransaction(transactionData: any) {
356+
if (addressToPath.size === 0) {
357+
await enable()
358+
}
359+
335360
const path = [...addressToPath.values()][0]
336361

337362
const transaction = new EthereumTx.Transaction(transactionData, {
@@ -352,6 +377,37 @@ async function trezorProvider(options: {
352377
return `0x${transaction.serialize().toString('hex')}`
353378
}
354379

380+
async function signMessage(message: { data: string }): Promise<string> {
381+
if (addressToPath.size === 0) {
382+
await enable()
383+
}
384+
385+
const [address, path] = [...addressToPath.entries()][0]
386+
387+
return new Promise((resolve, reject) => {
388+
TrezorConnect.ethereumSignMessage({
389+
path,
390+
message: ethUtil.stripHexPrefix(message.data),
391+
hex: true
392+
}).then((response: any) => {
393+
if (response.success) {
394+
if (response.payload.address !== ethUtil.toChecksumAddress(address)) {
395+
reject(new Error('signature doesnt match the right address'))
396+
}
397+
const signature = `0x${response.payload.signature}`
398+
resolve(signature)
399+
} else {
400+
reject(
401+
new Error(
402+
(response.payload && response.payload.error) ||
403+
'There was an error signing a message'
404+
)
405+
)
406+
}
407+
})
408+
})
409+
}
410+
355411
return provider
356412
}
357413

0 commit comments

Comments
 (0)