import { init , ScryptParams } from "@keplr-wallet/background" ; import { RNEnv , RNMessageRequesterInternalToUI , RNRouterBackground , } from "../router" ; import { AsyncKVStore } from "../common" ; import scrypt from "react-native-scrypt" ; import { Buffer } from "buffer/" ; import TransportBLE from "@ledgerhq/react-native-hw-transport-ble" ; import { getRandomBytesAsync } from "../common" ; import { BACKGROUND_PORT } from "@keplr-wallet/router" ; import { EmbedChainInfos } from "../config" ; import { getLastUsedLedgerDeviceId , setLastUsedLedgerDeviceId , } from "../utils/ledger" ; const router = new RNRouterBackground ( RNEnv . produceEnv ) ; init ( router , ( prefix ) => new AsyncKVStore ( prefix ) , new RNMessageRequesterInternalToUI ( ) , EmbedChainInfos , [ "https://app.osmosis.zone" ] , getRandomBytesAsync , { scrypt : async ( text , params ) => { return Buffer . from ( await scrypt ( Buffer . from ( text ) . toString ( "hex" ) , params . salt , params . n , params . r , params . p , params . dklen , "hex" ) , "hex" ) ; } , } , { create : ( params ) => { console . log ( `Notification: ${ params . title }, ${ params . message }` ) ; } , } , { defaultMode : "ble" , transportIniters : { ble : async ( deviceId ? ) => { const lastDeviceId = await getLastUsedLedgerDeviceId ( ) ; if ( ! deviceId && ! lastDeviceId ) { throw new Error ( "Device id is empty" ) ; } if ( ! deviceId ) { deviceId = lastDeviceId ; } if ( deviceId && deviceId !== lastDeviceId ) { await setLastUsedLedgerDeviceId ( deviceId ) ; } return await TransportBLE . open ( deviceId ) ; } , } , } ) ; router . listen ( BACKGROUND_PORT ) ;