Browse Source

mmgen-xmrwallet: add 'new' operation for new address creation

The MMGen Project 2 years ago
parent
commit
34f30fbf35
5 changed files with 110 additions and 10 deletions
  1. 1 1
      mmgen/data/release_date
  2. 1 1
      mmgen/data/version
  3. 21 1
      mmgen/main_xmrwallet.py
  4. 42 7
      mmgen/xmrwallet.py
  5. 45 0
      test/test_py_d/ts_xmrwallet.py

+ 1 - 1
mmgen/data/release_date

@@ -1 +1 @@
-May 2022
+June 2022

+ 1 - 1
mmgen/data/version

@@ -1 +1 @@
-13.2.dev4
+13.2.dev5

+ 21 - 1
mmgen/main_xmrwallet.py

@@ -31,6 +31,7 @@ opts_data = {
 		'usage2': [
 		'usage2': [
 			'[opts] create   <xmr_keyaddrfile> [wallets]',
 			'[opts] create   <xmr_keyaddrfile> [wallets]',
 			'[opts] sync     <xmr_keyaddrfile> [wallets]',
 			'[opts] sync     <xmr_keyaddrfile> [wallets]',
+			'[opts] new      <xmr_keyaddrfile> NEW_ADDRESS_SPEC',
 			'[opts] transfer <xmr_keyaddrfile> TRANSFER_SPEC',
 			'[opts] transfer <xmr_keyaddrfile> TRANSFER_SPEC',
 			'[opts] sweep    <xmr_keyaddrfile> SWEEP_SPEC',
 			'[opts] sweep    <xmr_keyaddrfile> SWEEP_SPEC',
 			'[opts] relay    <TX_file>',
 			'[opts] relay    <TX_file>',
@@ -75,6 +76,7 @@ plain HTTP is not supported.
 
 
 create    - create wallet for all or specified addresses in key-address file
 create    - create wallet for all or specified addresses in key-address file
 sync      - sync wallet for all or specified addresses in key-address file
 sync      - sync wallet for all or specified addresses in key-address file
+new       - create a new account in a wallet, or a new address in an account
 transfer  - transfer specified XMR amount to specified address from specified
 transfer  - transfer specified XMR amount to specified address from specified
             wallet:account
             wallet:account
 sweep     - sweep funds in specified wallet:account to new address in same
 sweep     - sweep funds in specified wallet:account to new address in same
@@ -91,6 +93,18 @@ in the specified key-address file, each corresponding to a Monero wallet
 to be created or synced.  If omitted, all wallets are operated upon.
 to be created or synced.  If omitted, all wallets are operated upon.
 
 
 
 
+                           'NEW' OPERATION NOTES
+
+This operation takes a NEW_ADDRESS_SPEC arg with the following format:
+
+    WALLET[:ACCOUNT][,"label text"]
+
+where WALLET is a wallet number and ACCOUNT an account index.  If ACCOUNT is
+omitted, a new account will be created in the wallet, otherwise a new address
+will be created in the specified account.  An optional label text may be
+appended to the spec following a comma.
+
+
                          'TRANSFER' OPERATION NOTES
                          'TRANSFER' OPERATION NOTES
 
 
 The transfer operation takes a TRANSFER_SPEC arg with the following format:
 The transfer operation takes a TRANSFER_SPEC arg with the following format:
@@ -167,6 +181,12 @@ $ mmgen-xmrwallet --do-not-relay sweep *.akeys.mmenc 2:0
 
 
 Relay the created sweep transaction via a host on the Tor network:
 Relay the created sweep transaction via a host on the Tor network:
 $ mmgen-xmrwallet --tx-relay-daemon=abcdefghijklmnop.onion:127.0.0.1:9050 relay *XMR*.sigtx
 $ mmgen-xmrwallet --tx-relay-daemon=abcdefghijklmnop.onion:127.0.0.1:9050 relay *XMR*.sigtx
+
+Create a new account in wallet 2:
+$ mmgen-xmrwallet new *.akeys.mmenc 2
+
+Create a new address in account 1 of wallet 2, with label:
+$ mmgen-xmrwallet new *.akeys.mmenc 2:1,"from ABC exchange"
 """
 """
 	},
 	},
 	'code': {
 	'code': {
@@ -199,7 +219,7 @@ elif op in ('create','sync'):
 		opts.usage()
 		opts.usage()
 	if cmd_args:
 	if cmd_args:
 		wallets = cmd_args[0]
 		wallets = cmd_args[0]
-elif op in ('transfer','sweep'):
+elif op in ('new','transfer','sweep'):
 	if len(cmd_args) != 1:
 	if len(cmd_args) != 1:
 		opts.usage()
 		opts.usage()
 	spec = cmd_args[0]
 	spec = cmd_args[0]

+ 42 - 7
mmgen/xmrwallet.py

@@ -38,6 +38,7 @@ xmrwallet_uarg_info = (
 	lambda e,hp: {
 	lambda e,hp: {
 		'daemon':          e('HOST:PORT', hp),
 		'daemon':          e('HOST:PORT', hp),
 		'tx_relay_daemon': e('HOST:PORT[:PROXY_HOST:PROXY_PORT]', rf'({hp})(?::({hp}))?'),
 		'tx_relay_daemon': e('HOST:PORT[:PROXY_HOST:PROXY_PORT]', rf'({hp})(?::({hp}))?'),
+		'newaddr_spec':    e('WALLET_NUM[:ACCOUNT][,"label text"]', rf'(\d+)(?::(\d+))?(?:,(.*))?'),
 		'transfer_spec':   e('SOURCE_WALLET_NUM:ACCOUNT:ADDRESS,AMOUNT', rf'(\d+):(\d+):([{b58a}]+),([0-9.]+)'),
 		'transfer_spec':   e('SOURCE_WALLET_NUM:ACCOUNT:ADDRESS,AMOUNT', rf'(\d+):(\d+):([{b58a}]+),([0-9.]+)'),
 		'sweep_spec':      e('SOURCE_WALLET_NUM:ACCOUNT[,DEST_WALLET_NUM]', r'(\d+):(\d+)(?:,(\d+))?'),
 		'sweep_spec':      e('SOURCE_WALLET_NUM:ACCOUNT[,DEST_WALLET_NUM]', r'(\d+):(\d+)(?:,(\d+))?'),
 	})(
 	})(
@@ -221,7 +222,7 @@ class MoneroMMGenTX:
 
 
 class MoneroWalletOps:
 class MoneroWalletOps:
 
 
-	ops = ('create','sync','transfer','sweep','relay')
+	ops = ('create','sync','new','transfer','sweep','relay')
 	opts = (
 	opts = (
 		'wallet_dir',
 		'wallet_dir',
 		'daemon',
 		'daemon',
@@ -455,12 +456,14 @@ class MoneroWalletOps:
 					self.print_accts(data,addrs_data)
 					self.print_accts(data,addrs_data)
 				return ( data, addrs_data )
 				return ( data, addrs_data )
 
 
-			async def create_acct(self):
+			async def create_acct(self,label=None):
 				msg('\n    Creating new account...')
 				msg('\n    Creating new account...')
 				ret = await self.c.call(
 				ret = await self.c.call(
 					'create_account',
 					'create_account',
-					label = f'Sweep from {self.parent.source.idx}:{self.parent.account} [{make_timestr()}]'
-				)
+					label = label or 'Sweep from {}:{} [{}]'.format(
+						self.parent.source.idx,
+						self.parent.account,
+						make_timestr() ))
 				msg('      Index:   {}'.format( pink(str(ret['account_index'])) ))
 				msg('      Index:   {}'.format( pink(str(ret['account_index'])) ))
 				msg('      Address: {}'.format( cyan(ret['address']) ))
 				msg('      Address: {}'.format( cyan(ret['address']) ))
 				return (ret['account_index'], ret['address'])
 				return (ret['account_index'], ret['address'])
@@ -488,12 +491,12 @@ class MoneroWalletOps:
 						e['used']
 						e['used']
 					))
 					))
 
 
-			async def create_new_addr(self,account):
+			async def create_new_addr(self,account,label=None):
 				msg_r('\n    Creating new address: ')
 				msg_r('\n    Creating new address: ')
 				ret = await self.c.call(
 				ret = await self.c.call(
 					'create_address',
 					'create_address',
 					account_index = account,
 					account_index = account,
-					label         = f'Sweep from this account [{make_timestr()}]',
+					label         = label or f'Sweep from this account [{make_timestr()}]',
 				)
 				)
 				msg(cyan(ret['address']))
 				msg(cyan(ret['address']))
 				return ret['address']
 				return ret['address']
@@ -732,11 +735,13 @@ class MoneroWalletOps:
 							yield res
 							yield res
 
 
 			self.addr_data = list(gen())
 			self.addr_data = list(gen())
-			self.account = int(m[2])
+			self.account = None if m[2] is None else int(m[2])
 
 
 			if self.name == 'transfer':
 			if self.name == 'transfer':
 				self.dest_addr = CoinAddr(self.proto,m[3])
 				self.dest_addr = CoinAddr(self.proto,m[3])
 				self.amount = self.proto.coin_amt(m[4])
 				self.amount = self.proto.coin_amt(m[4])
+			elif self.name == 'new':
+				self.label = m[3]
 
 
 		def init_tx_relay_daemon(self):
 		def init_tx_relay_daemon(self):
 
 
@@ -847,6 +852,36 @@ class MoneroWalletOps:
 		spec_id = 'transfer_spec'
 		spec_id = 'transfer_spec'
 		spec_key = ( (1,'source'), )
 		spec_key = ( (1,'source'), )
 
 
+	class new(sweep):
+		name    = 'new'
+		desc    = 'New'
+		past    = 'address created'
+		spec_id = 'newaddr_spec'
+		spec_key = ( (1,'source'), )
+
+		async def main(self):
+			h = self.rpc(self,self.source)
+			await h.open_wallet('Monero',refresh=True)
+			label = '{a} [{b}]'.format(
+				a = self.label or f"xmrwallet new {'account' if self.account == None else 'address'}",
+				b = make_timestr() )
+			if self.account == None:
+				acct,addr = await h.create_acct(label=label)
+			else:
+				msg_r('\n    Account index: {}'.format( pink(str(self.account)) ))
+				addr = await h.create_new_addr(self.account,label=label)
+
+			accts_data = (await h.get_accts())[0]
+
+			if self.account != None:
+				await h.print_addrs(accts_data,self.account)
+
+			# wallet must be left open: otherwise the 'stop_wallet' RPC call used to stop the daemon will fail
+			if uopt.no_stop_wallet_daemon:
+				await h.close_wallet('Monero')
+
+			msg('')
+
 	class relay(base):
 	class relay(base):
 		name = 'relay'
 		name = 'relay'
 		desc = 'Relay'
 		desc = 'Relay'

+ 45 - 0
test/test_py_d/ts_xmrwallet.py

@@ -53,6 +53,10 @@ class TestSuiteXMRWallet(TestSuiteBase):
 		('fund_alice',                'sending funds'),
 		('fund_alice',                'sending funds'),
 
 
 		('sync_wallets_all',          'syncing all wallets'),
 		('sync_wallets_all',          'syncing all wallets'),
+		('new_account_alice',         'creating a new account (Alice)'),
+		('new_account_alice_label',   'creating a new account (Alice, with label)'),
+		('new_address_alice',         'creating a new address (Alice)'),
+		('new_address_alice_label',   'creating a new address (Alice, with label)'),
 		('sync_wallets_selected',     'syncing selected wallets'),
 		('sync_wallets_selected',     'syncing selected wallets'),
 
 
 		('sweep_to_address_proxy',    'sweeping to new address (via TX relay + proxy)'),
 		('sweep_to_address_proxy',    'sweeping to new address (via TX relay + proxy)'),
@@ -304,6 +308,47 @@ class TestSuiteXMRWallet(TestSuiteBase):
 			t.expect('Address: ')
 			t.expect('Address: ')
 		return t
 		return t
 
 
+	def new_addr_alice(self,spec,cfg,expect):
+		data = self.users['alice']
+		t = self.spawn(
+			'mmgen-xmrwallet',
+			self.extra_opts +
+			[f'--wallet-dir={data.udir}'] +
+			[f'--daemon=localhost:{data.md.rpc_port}'] +
+			(['--no-start-wallet-daemon'] if cfg in ('continue','stop') else []) +
+			(['--no-stop-wallet-daemon'] if cfg in ('start','continue') else []) +
+			[ '--alice', 'new', data.kafile, spec ] )
+		res = strip_ansi_escapes(t.read()).replace('\r','')
+		m = re.search(expect,res,re.DOTALL)
+		assert m, m
+		return t
+
+	na_idx = 1
+
+	def new_account_alice(self):
+		return self.new_addr_alice(
+			'4',
+			'start',
+			fr'Creating new account.*Index:\s+{self.na_idx}\s')
+
+	def new_account_alice_label(self):
+		return self.new_addr_alice(
+			'4,Alice’s new account',
+			'continue',
+			fr'Creating new account.*Index:\s+{self.na_idx+1}\s.*Alice’s new account')
+
+	def new_address_alice(self):
+		return self.new_addr_alice(
+			'4:2',
+			'continue',
+			fr'Account index:\s+2\s+Creating new address' )
+
+	def new_address_alice_label(self):
+		return self.new_addr_alice(
+			'4:2,Alice’s new address',
+			'stop',
+			fr'Account index:\s+2\s+Creating new address.*Alice’s new address' )
+
 	async def mine_initial_coins(self):
 	async def mine_initial_coins(self):
 		await self.open_wallet_user('miner',1)
 		await self.open_wallet_user('miner',1)
 		return await self.mine_chk('miner',1,0,lambda x: x > 20,'unlocked balance > 20')
 		return await self.mine_chk('miner',1,0,lambda x: x > 20,'unlocked balance > 20')