Skip to content

Commit

Permalink
proto.btc.rpc: allow multiple loaded coin daemon wallets
Browse files Browse the repository at this point in the history
As of this commit, non-MMGen coin daemon wallets may remain loaded when using
MMGen, and they’ll be safely ignored.

As before, the MMGen tracking wallet will be created automatically if it doesn’t
exist.

Use of the default wallet as tracking wallet is no longer supported.  In the
unlikely event you’re still using your coin daemon’s default `wallet.dat` as
your MMGen tracking wallet, you must perform the following steps before running
any MMGen command:

1) Stop the coin daemon.

2) Execute `mkdir -p /path-to-coind-datadir/wallets/mmgen-tracking-wallet`.

3) Move the default `wallet.dat` to the newly created directory.
  • Loading branch information
mmgen committed Jul 3, 2023
1 parent 77d313e commit b51868a
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 44 deletions.
2 changes: 1 addition & 1 deletion mmgen/data/version
@@ -1 +1 @@
14.0.dev0
14.0.dev1
55 changes: 13 additions & 42 deletions mmgen/proto/btc/rpc.py
Expand Up @@ -184,8 +184,11 @@ async def check_chainfork_mismatch(block0):
await self.check_or_create_daemon_wallet()

# for regtest, wallet path must remain '/' until Carol’s user wallet has been created
if cfg.regtest_user:
self.wallet_path = f'/wallet/{cfg.regtest_user}'
if self.chain == 'regtest':
if cfg.regtest_user:
self.wallet_path = f'/wallet/{cfg.regtest_user}'
else:
self.wallet_path = f'/wallet/{self.twname}'

def set_auth(self):
"""
Expand Down Expand Up @@ -223,53 +226,21 @@ async def tracking_wallet_exists(self):
wnames = [i['name'] for i in (await self.call('listwalletdir'))['wallets']]
return twname in wnames

async def check_or_create_daemon_wallet(self,called=[],wallet_create=True):
"""
Returns True if the correct tracking wallet is currently loaded or if a new one
is created, False otherwise
"""
async def check_or_create_daemon_wallet(self):

if called or (self.chain == 'regtest' and self.cfg.regtest_user != 'carol'):
return False
if self.chain == 'regtest' and self.cfg.regtest_user != 'carol':
return

twname = self.twname
twname = self.cfg.regtest_user or self.twname
loaded_wnames = await self.call('listwallets')
wnames = [i['name'] for i in (await self.call('listwalletdir'))['wallets']]
m = f'Please fix your {self.daemon.desc} wallet installation or cmdline options'
ret = False

if self.cfg.carol:
if 'carol' in loaded_wnames:
ret = True
elif wallet_create:
await self.icall('createwallet',wallet_name='carol')
ymsg(f'Created {self.daemon.coind_name} wallet {"carol"!r}')
ret = True
elif len(loaded_wnames) == 1:
loaded_wname = loaded_wnames[0]
if twname in wnames and loaded_wname != twname:
await self.call('unloadwallet',loaded_wname)
await self.call('loadwallet',twname)
elif loaded_wname == '':
ymsg(f'WARNING: use of default wallet as tracking wallet is not recommended!\n{m}')
elif loaded_wname != twname:
ymsg(f'WARNING: loaded wallet {loaded_wname!r} is not {twname!r}\n{m}')
ret = True
elif len(loaded_wnames) == 0:

if twname not in loaded_wnames:
wnames = [i['name'] for i in (await self.call('listwalletdir'))['wallets']]
if twname in wnames:
await self.call('loadwallet',twname)
ret = True
elif wallet_create:
else:
await self.icall('createwallet',wallet_name=twname)
ymsg(f'Created {self.daemon.coind_name} wallet {twname!r}')
ret = True
else: # support only one loaded wallet for now
die(4,f'ERROR: more than one {self.daemon.coind_name} wallet loaded: {loaded_wnames}')

if wallet_create:
called.append(True)

return ret

def get_daemon_cfg_fn(self):
# Use dirname() to remove 'bob' or 'alice' component
Expand Down
6 changes: 5 additions & 1 deletion mmgen/proto/btc/tw/json.py
Expand Up @@ -53,7 +53,11 @@ async def tracking_wallet_exists(self):
return await self.twctl.rpc.tracking_wallet_exists

async def create_tracking_wallet(self):
return await self.twctl.rpc.check_or_create_daemon_wallet(wallet_create=True)
try:
await self.twctl.rpc.check_or_create_daemon_wallet()
return True
except:
return False

async def get_entries(self):
entries_in = [self.entry_tuple_in(*e) for e in self.data['data']['entries']]
Expand Down

0 comments on commit b51868a

Please sign in to comment.