Skip to content

Commit f3d8f17

Browse files
Add backend switching with walletSettings
Co-authored-by: Cursor <cursoragent@cursor.com>
1 parent 1abcb05 commit f3d8f17

File tree

3 files changed

+73
-18
lines changed

3 files changed

+73
-18
lines changed

src/monero/MoneroEngine.ts

Lines changed: 65 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -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'
1720
import { base64, base64url } from 'rfc4648'
1821

1922
import { 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
@@ -76,7 +82,12 @@ export class MoneroEngine extends CurrencyEngine<
7682

7783
this.unlockedBalance = '0'
7884

85+
// Shared across all wallets using this engine:
7986
this.currentSettings = asMoneroUserSettings(opts.userSettings)
87+
// Unique to this particular wallet instance:
88+
this.currentWalletSettings = asMoneroWalletSettings(
89+
opts.walletSettings ?? {}
90+
)
8091

8192
const keysPromise = new Promise<MoneroPrivateKeys>(resolve => {
8293
this.sendKeysToNative = resolve
@@ -89,36 +100,47 @@ export class MoneroEngine extends CurrencyEngine<
89100
base64.parse(this.walletId)
90101
)
91102

103+
const { backend } = this.currentWalletSettings
104+
this.log.warn('Using backend:', backend)
92105
const defaults = asMoneroUserSettings({})
93-
const daemonAddress = this.currentSettings.enableCustomServers
94-
? this.currentSettings.moneroLightwalletServer
95-
: defaults.moneroLightwalletServer
106+
const daemonAddress =
107+
backend === 'lws'
108+
? this.currentSettings.enableCustomServers
109+
? this.currentSettings.moneroLightwalletServer
110+
: defaults.moneroLightwalletServer
111+
: this.currentSettings.enableCustomMonerod
112+
? this.currentSettings.monerodServer
113+
: defaults.monerodServer
96114

97115
try {
98-
// LWS setup: API key and login
99-
const isEdgeLws = daemonAddress === EDGE_MONERO_LWS_SERVER
116+
// LWS-specific setup: API key and login
100117
let loginResult: LoginResponse | undefined
101-
await this.tools.cppBridge.setLwsApiKey(
102-
isEdgeLws ? this.initOptions.edgeApiKey : ''
103-
)
104-
if (isEdgeLws) {
105-
loginResult = await this.loginToLwsServer(
106-
daemonAddress,
107-
this.walletInfo.keys.moneroAddress,
108-
this.walletInfo.keys.moneroViewKeyPrivate
118+
if (backend === 'lws') {
119+
const isEdgeLws = daemonAddress === EDGE_MONERO_LWS_SERVER
120+
await this.tools.cppBridge.setLwsApiKey(
121+
isEdgeLws ? this.initOptions.edgeApiKey : ''
109122
)
123+
if (isEdgeLws) {
124+
loginResult = await this.loginToLwsServer(
125+
daemonAddress,
126+
this.walletInfo.keys.moneroAddress,
127+
this.walletInfo.keys.moneroViewKeyPrivate
128+
)
129+
}
110130
}
111131

112132
// Resolve birthday height (never open a wallet with height 0)
113133
const birthdayHeight = await this.resolveBirthdayHeight(
114134
keys.birthdayHeight,
135+
backend,
115136
daemonAddress,
137+
defaults.moneroLightwalletServer,
116138
loginResult
117139
)
118140

119141
await this.tools.cppBridge.openWallet(
120142
base64UrlWalletId,
121-
'lws',
143+
backend,
122144
keys.moneroKey,
123145
base64url.stringify(base64.parse(keys.dataKey)),
124146
this.networkInfo.networkType,
@@ -170,7 +192,9 @@ export class MoneroEngine extends CurrencyEngine<
170192
*/
171193
private async resolveBirthdayHeight(
172194
height: number,
195+
backend: WalletBackend,
173196
daemonAddress: string,
197+
edgeLwsServer: string,
174198
loginResult?: LoginResponse
175199
): Promise<number> {
176200
if (height !== 0) return height
@@ -180,12 +204,20 @@ export class MoneroEngine extends CurrencyEngine<
180204
return loginResult.start_height
181205
}
182206

183-
// Fall back to getAddressInfo
207+
// For monerod wallets, fall back to the Edge LWS server
208+
const serverUrl = backend === 'lws' ? daemonAddress : edgeLwsServer
184209
const addressInfo = await this.getAddressInfo(
185-
daemonAddress,
210+
serverUrl,
186211
this.walletInfo.keys.moneroAddress,
187212
this.walletInfo.keys.moneroViewKeyPrivate
188213
)
214+
215+
if (addressInfo.start_height === 0) {
216+
throw new Error(
217+
'Cannot open wallet: birthdayHeight is 0. ' +
218+
'The wallet creation height could not be determined.'
219+
)
220+
}
189221
return addressInfo.start_height
190222
}
191223

@@ -509,7 +541,7 @@ export class MoneroEngine extends CurrencyEngine<
509541
await this.clearBlockchainCache()
510542
await this.tools.cppBridge.deleteWallet(
511543
base64url.stringify(base64.parse(this.walletId)),
512-
'lws'
544+
this.currentWalletSettings.backend
513545
)
514546
await this.startEngine()
515547
}
@@ -525,6 +557,21 @@ export class MoneroEngine extends CurrencyEngine<
525557
await this.startEngine()
526558
}
527559

560+
async changeWalletSettings(walletSettings: JsonObject): Promise<void> {
561+
const newSettings = asMaybe(asMoneroWalletSettings)(walletSettings)
562+
if (
563+
newSettings == null ||
564+
matchJson(this.currentWalletSettings, newSettings)
565+
) {
566+
return
567+
}
568+
569+
this.currentWalletSettings = newSettings
570+
await this.killEngine()
571+
await this.clearBlockchainCache()
572+
await this.startEngine()
573+
}
574+
528575
async getMaxSpendable(edgeSpendInfo: EdgeSpendInfo): Promise<string> {
529576
const { tokenId } = edgeSpendInfo
530577

src/monero/moneroInfo.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ export const currencyInfo: EdgeCurrencyInfo = {
4040
],
4141

4242
defaultSettings,
43+
hasWalletSettings: true,
4344

4445
unsafeSyncNetwork: true,
4546
chainDisplayName: 'Monero',

src/monero/moneroTypes.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,13 @@ import {
66
asObject,
77
asOptional,
88
asString,
9+
asValue,
910
Cleaner
1011
} from 'cleaners'
1112
import type {
1213
NetworkType,
1314
TransactionPriority,
15+
WalletBackend,
1416
WalletEventData
1517
} from 'react-native-monero-lwsf'
1618
import type { Subscriber } from 'yaob'
@@ -35,6 +37,11 @@ export const asMoneroUserSettings = asObject({
3537
})
3638
export type MoneroUserSettings = ReturnType<typeof asMoneroUserSettings>
3739

40+
export const asMoneroWalletSettings = asObject({
41+
backend: asMaybe<WalletBackend>(asValue('lws', 'monerod'), 'lws')
42+
})
43+
export type MoneroWalletSettings = ReturnType<typeof asMoneroWalletSettings>
44+
3845
export const asMoneroKeyOptions = asObject({
3946
birthdayHeight: asNumber
4047
})

0 commit comments

Comments
 (0)