@@ -13,7 +13,10 @@ import {
1313 NoAmountSpecifiedError ,
1414 PendingFundsError
1515} from 'edge-core-js/types'
16- import type { TransactionDirection } from 'react-native-monero-lwsf'
16+ import type {
17+ TransactionDirection ,
18+ WalletBackend
19+ } from 'react-native-monero-lwsf'
1720import { base64 , base64url } from 'rfc4648'
1821
1922import { CurrencyEngine } from '../common/CurrencyEngine'
@@ -36,6 +39,7 @@ import {
3639 asMoneroPrivateKeys ,
3740 asMoneroUserSettings ,
3841 asMoneroWalletOtherData ,
42+ asMoneroWalletSettings ,
3943 asSafeMoneroWalletInfo ,
4044 EDGE_MONERO_LWS_SERVER ,
4145 LoginResponse ,
@@ -44,6 +48,7 @@ import {
4448 MoneroPrivateKeys ,
4549 MoneroUserSettings ,
4650 MoneroWalletOtherData ,
51+ MoneroWalletSettings ,
4752 SafeMoneroWalletInfo ,
4853 translateFee
4954} from './moneroTypes'
@@ -55,6 +60,7 @@ export class MoneroEngine extends CurrencyEngine<
5560> {
5661 networkInfo : MoneroNetworkInfo
5762 currentSettings : MoneroUserSettings
63+ currentWalletSettings : MoneroWalletSettings
5864 otherData ! : MoneroWalletOtherData
5965 initOptions : MoneroInitOptions
6066 unlockedBalance : string
@@ -77,7 +83,12 @@ export class MoneroEngine extends CurrencyEngine<
7783
7884 this . unlockedBalance = '0'
7985
86+ // Shared across all wallets using this engine:
8087 this . currentSettings = asMoneroUserSettings ( opts . userSettings )
88+ // Unique to this particular wallet instance:
89+ this . currentWalletSettings = asMoneroWalletSettings (
90+ opts . walletSettings ?? { }
91+ )
8192
8293 const keysPromise = new Promise < MoneroPrivateKeys > ( resolve => {
8394 this . sendKeysToNative = resolve
@@ -90,36 +101,47 @@ export class MoneroEngine extends CurrencyEngine<
90101 base64 . parse ( this . walletId )
91102 )
92103
104+ const { backend } = this . currentWalletSettings
105+ this . log . warn ( 'Using backend:' , backend )
93106 const defaults = asMoneroUserSettings ( { } )
94- const daemonAddress = this . currentSettings . enableCustomServers
95- ? this . currentSettings . moneroLightwalletServer
96- : defaults . moneroLightwalletServer
107+ const daemonAddress =
108+ backend === 'lws'
109+ ? this . currentSettings . enableCustomServers
110+ ? this . currentSettings . moneroLightwalletServer
111+ : defaults . moneroLightwalletServer
112+ : this . currentSettings . enableCustomMonerod
113+ ? this . currentSettings . monerodServer
114+ : defaults . monerodServer
97115
98116 try {
99- // LWS setup: API key and login
100- const isEdgeLws = daemonAddress === EDGE_MONERO_LWS_SERVER
117+ // LWS-specific setup: API key and login
101118 let loginResult : LoginResponse | undefined
102- await this . tools . cppBridge . setLwsApiKey (
103- isEdgeLws ? this . initOptions . edgeApiKey : ''
104- )
105- if ( isEdgeLws ) {
106- loginResult = await this . loginToLwsServer (
107- daemonAddress ,
108- this . walletInfo . keys . moneroAddress ,
109- this . walletInfo . keys . moneroViewKeyPrivate
119+ if ( backend === 'lws' ) {
120+ const isEdgeLws = daemonAddress === EDGE_MONERO_LWS_SERVER
121+ await this . tools . cppBridge . setLwsApiKey (
122+ isEdgeLws ? this . initOptions . edgeApiKey : ''
110123 )
124+ if ( isEdgeLws ) {
125+ loginResult = await this . loginToLwsServer (
126+ daemonAddress ,
127+ this . walletInfo . keys . moneroAddress ,
128+ this . walletInfo . keys . moneroViewKeyPrivate
129+ )
130+ }
111131 }
112132
113133 // Resolve birthday height (never open a wallet with height 0)
114134 const birthdayHeight = await this . resolveBirthdayHeight (
115135 keys . birthdayHeight ,
136+ backend ,
116137 daemonAddress ,
138+ defaults . moneroLightwalletServer ,
117139 loginResult
118140 )
119141
120142 await this . tools . cppBridge . openWallet (
121143 base64UrlWalletId ,
122- 'lws' ,
144+ backend ,
123145 keys . moneroKey ,
124146 base64url . stringify ( base64 . parse ( keys . dataKey ) ) ,
125147 this . networkInfo . networkType ,
@@ -179,7 +201,9 @@ export class MoneroEngine extends CurrencyEngine<
179201 */
180202 private async resolveBirthdayHeight (
181203 height : number ,
204+ backend : WalletBackend ,
182205 daemonAddress : string ,
206+ edgeLwsServer : string ,
183207 loginResult ?: LoginResponse
184208 ) : Promise < number > {
185209 if ( height !== 0 ) return height
@@ -189,12 +213,20 @@ export class MoneroEngine extends CurrencyEngine<
189213 return loginResult . start_height
190214 }
191215
192- // Fall back to getAddressInfo
216+ // For monerod wallets, fall back to the Edge LWS server
217+ const serverUrl = backend === 'lws' ? daemonAddress : edgeLwsServer
193218 const addressInfo = await this . getAddressInfo (
194- daemonAddress ,
219+ serverUrl ,
195220 this . walletInfo . keys . moneroAddress ,
196221 this . walletInfo . keys . moneroViewKeyPrivate
197222 )
223+
224+ if ( addressInfo . start_height === 0 ) {
225+ throw new Error (
226+ 'Cannot open wallet: birthdayHeight is 0. ' +
227+ 'The wallet creation height could not be determined.'
228+ )
229+ }
198230 return addressInfo . start_height
199231 }
200232
@@ -521,7 +553,7 @@ export class MoneroEngine extends CurrencyEngine<
521553 await this . clearBlockchainCache ( )
522554 await this . tools . cppBridge . deleteWallet (
523555 base64url . stringify ( base64 . parse ( this . walletId ) ) ,
524- 'lws'
556+ this . currentWalletSettings . backend
525557 )
526558 await this . startEngine ( )
527559 }
@@ -537,6 +569,21 @@ export class MoneroEngine extends CurrencyEngine<
537569 await this . startEngine ( )
538570 }
539571
572+ async changeWalletSettings ( walletSettings : JsonObject ) : Promise < void > {
573+ const newSettings = asMaybe ( asMoneroWalletSettings ) ( walletSettings )
574+ if (
575+ newSettings == null ||
576+ matchJson ( this . currentWalletSettings , newSettings )
577+ ) {
578+ return
579+ }
580+
581+ this . currentWalletSettings = newSettings
582+ await this . killEngine ( )
583+ await this . clearBlockchainCache ( )
584+ await this . startEngine ( )
585+ }
586+
540587 async getMaxSpendable ( edgeSpendInfo : EdgeSpendInfo ) : Promise < string > {
541588 const { tokenId } = edgeSpendInfo
542589
0 commit comments