11import Transport from "@ledgerhq/hw-transport"
22import TransportWebUSB from "@ledgerhq/hw-transport-webusb"
3+ import { toChecksumAddress } from "@tallyho/hd-keyring"
34import Eth from "@ledgerhq/hw-app-eth"
45import { DeviceModelId } from "@ledgerhq/devices"
56import {
@@ -25,7 +26,11 @@ import { ServiceCreatorFunction, ServiceLifecycleEvents } from "../types"
2526import logger from "../../lib/logger"
2627import { getOrCreateDB , LedgerAccount , LedgerDatabase } from "./db"
2728import { ethersTransactionFromTransactionRequest } from "../chain/utils"
28- import { NETWORK_FOR_LEDGER_SIGNING } from "../../constants"
29+ import {
30+ NETWORK_SUPPORTED_BY_LEDGER ,
31+ ROOTSTOCK ,
32+ DEFAULT_DERIVATION_PATH as idDerivationPath ,
33+ } from "../../constants"
2934import { normalizeEVMAddress } from "../../lib/utils"
3035import { AddressOnNetwork } from "../../accounts"
3136
@@ -111,17 +116,25 @@ type Events = ServiceLifecycleEvents & {
111116 usbDeviceCount : number
112117}
113118
114- export const idDerivationPath = "44'/60'/0'/0/0"
115-
116119async function deriveAddressOnLedger ( path : string , eth : Eth ) {
117120 const derivedIdentifiers = await eth . getAddress ( path )
121+
122+ if (
123+ ROOTSTOCK . derivationPath &&
124+ path . includes ( ROOTSTOCK . derivationPath . slice ( 0 , 8 ) )
125+ ) {
126+ // ethersGetAddress rejects Rootstock addresses so using toChecksumAddress
127+ return toChecksumAddress ( derivedIdentifiers . address , + ROOTSTOCK . chainID )
128+ }
129+
118130 const address = ethersGetAddress ( derivedIdentifiers . address )
119131 return address
120132}
121133
122134async function generateLedgerId (
123135 transport : Transport ,
124- eth : Eth
136+ eth : Eth ,
137+ derivationPath : string
125138) : Promise < [ string | undefined , LedgerType ] > {
126139 let extensionDeviceType = LedgerType . UNKNOWN
127140
@@ -147,7 +160,7 @@ async function generateLedgerId(
147160 return [ undefined , extensionDeviceType ]
148161 }
149162
150- const address = await deriveAddressOnLedger ( idDerivationPath , eth )
163+ const address = await deriveAddressOnLedger ( derivationPath , eth )
151164
152165 return [ address , extensionDeviceType ]
153166}
@@ -172,6 +185,8 @@ async function generateLedgerId(
172185export default class LedgerService extends BaseService < Events > {
173186 #currentLedgerId: string | null = null
174187
188+ #derivationPath: string = idDerivationPath
189+
175190 transport : Transport | undefined = undefined
176191
177192 #lastOperationPromise = Promise . resolve ( )
@@ -209,7 +224,11 @@ export default class LedgerService extends BaseService<Events> {
209224
210225 const eth = new Eth ( this . transport )
211226
212- const [ id , type ] = await generateLedgerId ( this . transport , eth )
227+ const [ id , type ] = await generateLedgerId (
228+ this . transport ,
229+ eth ,
230+ this . #derivationPath
231+ )
213232
214233 if ( ! id ) {
215234 throw new Error ( "Can't derive meaningful identification address!" )
@@ -239,7 +258,7 @@ export default class LedgerService extends BaseService<Events> {
239258 this . emitter . emit ( "ledgerAdded" , {
240259 id : this . #currentLedgerId,
241260 type,
242- accountIDs : [ idDerivationPath ] ,
261+ accountIDs : [ this . #derivationPath ] ,
243262 metadata : {
244263 ethereumVersion : appData . version ,
245264 isArbitraryDataSigningEnabled : appData . arbitraryDataEnabled !== 0 ,
@@ -250,6 +269,10 @@ export default class LedgerService extends BaseService<Events> {
250269 } )
251270 }
252271
272+ setDefaultDerivationPath ( path : string ) : void {
273+ this . #derivationPath = path
274+ }
275+
253276 #handleUSBConnect = async ( event : USBConnectionEvent ) : Promise < void > => {
254277 this . emitter . emit (
255278 "usbDeviceCount" ,
@@ -540,7 +563,7 @@ export default class LedgerService extends BaseService<Events> {
540563 hexDataToSign : HexString
541564 ) : Promise < string > {
542565 if (
543- ! NETWORK_FOR_LEDGER_SIGNING . find ( ( supportedNetwork ) =>
566+ ! NETWORK_SUPPORTED_BY_LEDGER . find ( ( supportedNetwork ) =>
544567 sameNetwork ( network , supportedNetwork )
545568 )
546569 ) {
0 commit comments