xmrwallet.py 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889
  1. #!/usr/bin/env python3
  2. #
  3. # mmgen = Multi-Mode GENerator, command-line Bitcoin cold storage solution
  4. # Copyright (C)2013-2021 The MMGen Project <mmgen@tuta.io>
  5. #
  6. # This program is free software: you can redistribute it and/or modify
  7. # it under the terms of the GNU General Public License as published by
  8. # the Free Software Foundation, either version 3 of the License, or
  9. # (at your option) any later version.
  10. #
  11. # This program is distributed in the hope that it will be useful,
  12. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. # GNU General Public License for more details.
  15. #
  16. # You should have received a copy of the GNU General Public License
  17. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. """
  19. xmrwallet.py - MoneroWalletOps class
  20. """
  21. import os,re,time,json
  22. from collections import namedtuple
  23. from .common import *
  24. from .addr import KeyAddrList,AddrIdxList
  25. from .rpc import MoneroRPCClientRaw,MoneroWalletRPCClient,json_encoder
  26. from .daemon import MoneroWalletDaemon
  27. from .protocol import _b58a,init_proto
  28. from .obj import CoinAddr,CoinTxID,SeedID,AddrIdx,Hilite,InitErrors
  29. xmrwallet_uarg_info = (
  30. lambda e,hp: {
  31. 'daemon': e('HOST:PORT', hp),
  32. 'tx_relay_daemon': e('HOST:PORT[:PROXY_HOST:PROXY_PORT]', rf'({hp})(?::({hp}))?'),
  33. 'transfer_spec': e('SOURCE_WALLET_NUM:ACCOUNT:ADDRESS,AMOUNT', rf'(\d+):(\d+):([{_b58a}]+),([0-9.]+)'),
  34. 'sweep_spec': e('SOURCE_WALLET_NUM:ACCOUNT[,DEST_WALLET_NUM]', r'(\d+):(\d+)(?:,(\d+))?'),
  35. })(
  36. namedtuple('uarg_info_entry',['annot','pat']),
  37. r'(?:[^:]+):(?:\d+)'
  38. )
  39. class XMRWalletAddrSpec(str,Hilite,InitErrors,MMGenObject):
  40. color = 'cyan'
  41. width = 0
  42. trunc_ok = False
  43. min_len = 5 # 1:0:0
  44. max_len = 14 # 9999:9999:9999
  45. def __new__(cls,arg1,arg2=None,arg3=None):
  46. if type(arg1) == cls:
  47. return arg1
  48. try:
  49. if isinstance(arg1,str):
  50. me = str.__new__(cls,arg1)
  51. m = re.fullmatch( '({n}):({n}):({n}|None)'.format(n=r'[0-9]{1,4}'), arg1 )
  52. assert m is not None, f'{arg1!r}: invalid XMRWalletAddrSpec'
  53. for e in m.groups():
  54. if len(e) != 1 and e[0] == '0':
  55. die(2,f'{e}: leading zeroes not permitted in XMRWalletAddrSpec element')
  56. me.wallet = AddrIdx(m[1])
  57. me.account = int(m[2])
  58. me.account_address = None if m[3] == 'None' else int(m[3])
  59. else:
  60. me = str.__new__(cls,f'{arg1}:{arg2}:{arg3}')
  61. for arg in [arg1,arg2] + ([] if arg3 is None else [arg3]):
  62. assert isinstance(arg,int), f'{arg}: XMRWalletAddrSpec component not of type int'
  63. assert arg is None or arg <= 9999, f'{arg}: XMRWalletAddrSpec component greater than 9999'
  64. me.wallet = AddrIdx(arg1)
  65. me.account = arg2
  66. me.account_address = arg3
  67. return me
  68. except Exception as e:
  69. return cls.init_fail(e,me)
  70. class MoneroMMGenTX:
  71. class Base:
  72. def make_chksum(self,keys=None):
  73. res = json.dumps(
  74. dict( (k,v) for k,v in self.data._asdict().items() if (not keys or k in keys) ),
  75. cls = json_encoder
  76. )
  77. return make_chksum_6(res)
  78. @property
  79. def base_chksum(self):
  80. return self.make_chksum(
  81. ('op','create_time','network','seed_id','source','dest','amount')
  82. )
  83. @property
  84. def full_chksum(self):
  85. return self.make_chksum(set(self.data._fields) - {'metadata'})
  86. xmrwallet_tx_data = namedtuple('xmrwallet_tx_data',[
  87. 'op',
  88. 'create_time',
  89. 'sign_time',
  90. 'network',
  91. 'seed_id',
  92. 'source',
  93. 'dest',
  94. 'dest_address',
  95. 'txid',
  96. 'amount',
  97. 'fee',
  98. 'blob',
  99. 'metadata',
  100. ])
  101. def get_info(self,indent=''):
  102. d = self.data
  103. if d.dest:
  104. to_entry = f'\n{indent} To: ' + (
  105. 'Wallet {}, account {}, address {}'.format(
  106. d.dest.wallet.hl(),
  107. red(f'#{d.dest.account}'),
  108. red(f'#{d.dest.account_address}')
  109. )
  110. )
  111. return fmt("""
  112. Transaction info [Seed ID: {}. Network: {}]:
  113. TxID: {}
  114. Type: {}
  115. From: Wallet {}, account {}{}
  116. Amt: {} XMR
  117. Fee: {} XMR
  118. Dest: {}
  119. """,strip_char='\t',indent=indent).format(
  120. d.seed_id.hl(), d.network.upper(),
  121. d.txid.hl(),
  122. blue(capfirst(d.op)),
  123. d.source.wallet.hl(),
  124. red(f'#{d.source.account}'),
  125. to_entry if d.dest else '',
  126. d.amount.hl(),
  127. d.fee.hl(),
  128. d.dest_address.hl()
  129. )
  130. def write(self,delete_metadata=False):
  131. dict_data = self.data._asdict()
  132. if delete_metadata:
  133. dict_data['metadata'] = None
  134. out = json.dumps(
  135. { 'MoneroMMGenTX': {
  136. 'base_chksum': self.base_chksum,
  137. 'full_chksum': self.full_chksum,
  138. 'data': dict_data,
  139. }
  140. },
  141. cls = json_encoder,
  142. )
  143. fn = '{}{}-XMR[{!s}]{}.sigtx'.format(
  144. self.base_chksum.upper(),
  145. (lambda s: f'-{s.upper()}' if s else '')(self.full_chksum),
  146. self.data.amount,
  147. (lambda s: '' if s == 'mainnet' else f'.{s}')(self.data.network),
  148. )
  149. write_data_to_file(fn,out,desc='MoneroMMGenTX data',ask_write=True,ask_write_default_yes=False)
  150. class NewSigned(Base):
  151. def __init__(self,*args,**kwargs):
  152. assert not args, 'Non-keyword args not permitted'
  153. d = namedtuple('kwargs_tuple',kwargs)(**kwargs)
  154. proto = init_proto('xmr',network=d.network)
  155. now = int(time.time())
  156. self.data = self.xmrwallet_tx_data(
  157. op = d.op,
  158. create_time = now,
  159. sign_time = now,
  160. network = d.network,
  161. seed_id = SeedID(sid=d.seed_id),
  162. source = XMRWalletAddrSpec(d.source),
  163. dest = None if d.dest is None else XMRWalletAddrSpec(d.dest),
  164. dest_address = CoinAddr(proto,d.dest_address),
  165. txid = CoinTxID(d.txid),
  166. amount = proto.coin_amt(d.amount,from_unit='atomic'),
  167. fee = proto.coin_amt(d.fee,from_unit='atomic'),
  168. blob = d.blob,
  169. metadata = d.metadata,
  170. )
  171. class Signed(Base):
  172. def __init__(self,fn):
  173. self.fn = fn
  174. d_wrap = json.loads(get_data_from_file(fn))['MoneroMMGenTX']
  175. d = self.xmrwallet_tx_data(**d_wrap['data'])
  176. proto = init_proto('xmr',network=d.network)
  177. self.data = self.xmrwallet_tx_data(
  178. op = d.op,
  179. create_time = d.create_time,
  180. sign_time = d.sign_time,
  181. network = d.network,
  182. seed_id = SeedID(sid=d.seed_id),
  183. source = XMRWalletAddrSpec(d.source),
  184. dest = None if d.dest is None else XMRWalletAddrSpec(d.dest),
  185. dest_address = CoinAddr(proto,d.dest_address),
  186. txid = CoinTxID(d.txid),
  187. amount = proto.coin_amt(d.amount),
  188. fee = proto.coin_amt(d.fee),
  189. blob = d.blob,
  190. metadata = d.metadata,
  191. )
  192. for k in ('base_chksum','full_chksum'):
  193. a = getattr(self,k)
  194. b = d_wrap[k]
  195. assert a == b, f'{k} mismatch: {a} != {b}'
  196. class MoneroWalletOps:
  197. ops = ('create','sync','transfer','sweep','relay')
  198. opts = (
  199. 'wallet_dir',
  200. 'daemon',
  201. 'tx_relay_daemon',
  202. 'use_internal_keccak_module',
  203. 'hash_preset',
  204. 'restore_height',
  205. 'no_start_wallet_daemon',
  206. 'no_stop_wallet_daemon',
  207. 'do_not_relay',
  208. )
  209. pat_opts = ('daemon','tx_relay_daemon')
  210. class base(MMGenObject):
  211. opts = ('wallet_dir',)
  212. def __init__(self,uarg_tuple,uopt_tuple):
  213. def gen_classes():
  214. for cls in type(self).__mro__:
  215. yield cls
  216. if cls.__name__ == 'base':
  217. break
  218. classes = tuple(gen_classes())
  219. self.opts = tuple(set(opt for cls in classes for opt in cls.opts))
  220. global uarg, uopt, uarg_info, fmt_amt, hl_amt
  221. uarg = uarg_tuple
  222. uopt = uopt_tuple
  223. uarg_info = xmrwallet_uarg_info
  224. def fmt_amt(amt):
  225. return self.proto.coin_amt(amt,from_unit='atomic').fmt(fs='5.12',color=True)
  226. def hl_amt(amt):
  227. return self.proto.coin_amt(amt,from_unit='atomic').hl()
  228. id_cur = None
  229. for cls in classes:
  230. if id(cls.check_uopts) != id_cur:
  231. cls.check_uopts(self)
  232. id_cur = id(cls.check_uopts)
  233. self.proto = init_proto('xmr',testnet=g.testnet)
  234. def check_uopts(self):
  235. def check_pat_opt(name):
  236. val = getattr(uopt,name)
  237. if not re.fullmatch(uarg_info[name].pat,val,re.ASCII):
  238. die(1,'{!r}: invalid value for --{}: it must have format {!r}'.format(
  239. val,
  240. name.replace('_','-'),
  241. uarg_info[name].annot
  242. ))
  243. for opt in uopt._asdict():
  244. if getattr(uopt,opt) and not opt in self.opts:
  245. die(1,'Option --{} not supported for {!r} operation'.format(
  246. opt.replace('_','-'),
  247. uarg.op
  248. ))
  249. for opt in MoneroWalletOps.pat_opts:
  250. if getattr(uopt,opt):
  251. check_pat_opt(opt)
  252. def display_tx_relay_info(self,indent=''):
  253. m = re.fullmatch(uarg_info['tx_relay_daemon'].pat,uopt.tx_relay_daemon,re.ASCII)
  254. msg(fmt(f"""
  255. TX relay info:
  256. Host: {blue(m[1])}
  257. Proxy: {blue(m[2] or 'None')}
  258. """,strip_char='\t',indent=indent))
  259. def post_main(self): pass
  260. def stop_daemons(self): pass
  261. class wallet(base):
  262. opts = (
  263. 'use_internal_keccak_module',
  264. 'hash_preset',
  265. 'daemon',
  266. 'no_start_wallet_daemon',
  267. 'no_stop_wallet_daemon',
  268. )
  269. wallet_exists = True
  270. def __init__(self,uarg_tuple,uopt_tuple):
  271. def wallet_exists(fn):
  272. try: os.stat(fn)
  273. except: return False
  274. else: return True
  275. def check_wallets():
  276. for d in self.addr_data:
  277. fn = self.get_wallet_fn(d)
  278. exists = wallet_exists(fn)
  279. if exists and not self.wallet_exists:
  280. die(1,f'Wallet {fn!r} already exists!')
  281. elif not exists and self.wallet_exists:
  282. die(1,f'Wallet {fn!r} not found!')
  283. super().__init__(uarg_tuple,uopt_tuple)
  284. self.kal = KeyAddrList(self.proto,uarg.infile)
  285. self.create_addr_data()
  286. check_wallets()
  287. self.wd = MoneroWalletDaemon(
  288. proto = self.proto,
  289. wallet_dir = uopt.wallet_dir or '.',
  290. test_suite = g.test_suite,
  291. daemon_addr = uopt.daemon or None,
  292. )
  293. if not uopt.no_start_wallet_daemon:
  294. self.wd.restart()
  295. self.c = MoneroWalletRPCClient(
  296. host = self.wd.host,
  297. port = self.wd.rpc_port,
  298. user = self.wd.user,
  299. passwd = self.wd.passwd
  300. )
  301. def create_addr_data(self):
  302. if uarg.wallets:
  303. idxs = AddrIdxList(uarg.wallets)
  304. self.addr_data = [d for d in self.kal.data if d.idx in idxs]
  305. if len(self.addr_data) != len(idxs):
  306. die(1,f'List {uarg.wallets!r} contains addresses not present in supplied key-address file')
  307. else:
  308. self.addr_data = self.kal.data
  309. def stop_daemons(self):
  310. if not uopt.no_stop_wallet_daemon:
  311. self.wd.stop()
  312. if uopt.tx_relay_daemon and hasattr(self,'wd2'):
  313. self.wd2.stop()
  314. def get_wallet_fn(self,d):
  315. return os.path.join(
  316. uopt.wallet_dir or '.','{}-{}-MoneroWallet{}{}'.format(
  317. self.kal.al_id.sid,
  318. d.idx,
  319. '.testnet' if g.testnet else '',
  320. '-α' if g.debug_utf8 else '' ))
  321. async def main(self):
  322. gmsg('\n{}ing {} wallet{}'.format(
  323. self.desc,
  324. len(self.addr_data),
  325. suf(self.addr_data) ))
  326. processed = 0
  327. for n,d in enumerate(self.addr_data): # [d.sec,d.addr,d.wallet_passwd,d.viewkey]
  328. fn = self.get_wallet_fn(d)
  329. gmsg('\n{}ing wallet {}/{} ({})'.format(
  330. self.desc,
  331. n+1,
  332. len(self.addr_data),
  333. os.path.basename(fn),
  334. ))
  335. processed += await self.process_wallet(d,fn)
  336. gmsg(f'\n{processed} wallet{suf(processed)} {self.past}')
  337. return processed
  338. class rpc:
  339. def __init__(self,parent,d):
  340. self.parent = parent
  341. self.c = parent.c
  342. self.d = d
  343. self.fn = parent.get_wallet_fn(d)
  344. async def open_wallet(self,desc,refresh=True):
  345. gmsg_r(f'\n Opening {desc} wallet...')
  346. await self.c.call( # returns {}
  347. 'open_wallet',
  348. filename=os.path.basename(self.fn),
  349. password=self.d.wallet_passwd )
  350. gmsg('done')
  351. if refresh:
  352. gmsg_r(f' Refreshing {desc} wallet...')
  353. ret = await self.c.call('refresh')
  354. gmsg('done')
  355. if ret['received_money']:
  356. msg(' Wallet has received funds')
  357. async def close_wallet(self,desc):
  358. gmsg_r(f'\n Closing {desc} wallet...')
  359. await self.c.call('close_wallet')
  360. gmsg_r('done')
  361. def print_accts(self,data,addrs_data,indent=' '):
  362. d = data['subaddress_accounts']
  363. msg('\n' + indent + f'Accounts of wallet {os.path.basename(self.fn)}:')
  364. fs = indent + ' {:6} {:18} {:<6} {:%s} {}' % max(len(e['label']) for e in d)
  365. msg(fs.format('Index ','Base Address','nAddrs','Label','Unlocked Balance'))
  366. for i,e in enumerate(d):
  367. msg(fs.format(
  368. str(e['account_index']),
  369. e['base_address'][:15] + '...',
  370. len(addrs_data[i]['addresses']),
  371. e['label'],
  372. fmt_amt(e['unlocked_balance']),
  373. ))
  374. async def get_accts(self,print=True):
  375. data = await self.c.call('get_accounts')
  376. addrs_data = [
  377. await self.c.call('get_address',account_index=i)
  378. for i in range(len(data['subaddress_accounts']))
  379. ]
  380. if print:
  381. self.print_accts(data,addrs_data)
  382. return ( data, addrs_data )
  383. async def create_acct(self):
  384. msg('\n Creating new account...')
  385. ret = await self.c.call(
  386. 'create_account',
  387. label = f'Sweep from {self.parent.source.idx}:{self.parent.account}'
  388. )
  389. msg(' Index: {}'.format( pink(str(ret['account_index'])) ))
  390. msg(' Address: {}'.format( cyan(ret['address']) ))
  391. return (ret['account_index'], ret['address'])
  392. def get_last_acct(self,accts_data):
  393. msg('\n Getting last account...')
  394. ret = accts_data['subaddress_accounts'][-1]
  395. msg(' Index: {}'.format( pink(str(ret['account_index'])) ))
  396. msg(' Address: {}'.format( cyan(ret['base_address']) ))
  397. return (ret['account_index'], ret['base_address'])
  398. async def print_addrs(self,accts_data,account):
  399. ret = await self.c.call('get_address',account_index=account)
  400. d = ret['addresses']
  401. msg('\n Addresses of account #{} ({}):'.format(
  402. account,
  403. accts_data['subaddress_accounts'][account]['label']))
  404. fs = ' {:6} {:18} {:%s} {}' % max(len(e['label']) for e in d)
  405. msg(fs.format('Index ','Address','Label','Used'))
  406. for e in d:
  407. msg(fs.format(
  408. str(e['address_index']),
  409. e['address'][:15] + '...',
  410. e['label'],
  411. e['used']
  412. ))
  413. async def create_new_addr(self,account):
  414. msg_r('\n Creating new address: ')
  415. ret = await self.c.call(
  416. 'create_address',
  417. account_index = account,
  418. label = 'Sweep from this account',
  419. )
  420. msg(cyan(ret['address']))
  421. return ret['address']
  422. async def get_last_addr(self,account,display=True):
  423. if display:
  424. msg('\n Getting last address:')
  425. ret = (await self.c.call(
  426. 'get_address',
  427. account_index = account,
  428. ))['addresses']
  429. addr = ret[-1]['address']
  430. if display:
  431. msg(' ' + cyan(addr))
  432. return ( addr, len(ret) - 1 )
  433. async def make_transfer_tx(self,account,addr,amt):
  434. res = await self.c.call(
  435. 'transfer',
  436. account_index = account,
  437. destinations = [{
  438. 'amount': amt.to_unit('atomic'),
  439. 'address': addr
  440. }],
  441. do_not_relay = True,
  442. get_tx_hex = True,
  443. get_tx_metadata = True
  444. )
  445. return MoneroMMGenTX.NewSigned(
  446. op = uarg.op,
  447. network = self.parent.proto.network,
  448. seed_id = self.parent.kal.al_id.sid,
  449. source = XMRWalletAddrSpec(self.parent.source.idx,self.parent.account,None),
  450. dest = None,
  451. dest_address = addr,
  452. txid = res['tx_hash'],
  453. amount = res['amount'],
  454. fee = res['fee'],
  455. blob = res['tx_blob'],
  456. metadata = res['tx_metadata'],
  457. )
  458. async def make_sweep_tx(self,account,dest_acct,dest_addr_idx,addr):
  459. res = await self.c.call(
  460. 'sweep_all',
  461. address = addr,
  462. account_index = account,
  463. do_not_relay = True,
  464. get_tx_hex = True,
  465. get_tx_metadata = True
  466. )
  467. if len(res['tx_hash_list']) > 1:
  468. die(3,'More than one TX required. Cannot perform this sweep')
  469. return MoneroMMGenTX.NewSigned(
  470. op = uarg.op,
  471. network = self.parent.proto.network,
  472. seed_id = self.parent.kal.al_id.sid,
  473. source = XMRWalletAddrSpec(self.parent.source.idx,self.parent.account,None),
  474. dest = XMRWalletAddrSpec(
  475. (self.parent.dest or self.parent.source).idx,
  476. dest_acct,
  477. dest_addr_idx),
  478. dest_address = addr,
  479. txid = res['tx_hash_list'][0],
  480. amount = res['amount_list'][0],
  481. fee = res['fee_list'][0],
  482. blob = res['tx_blob_list'][0],
  483. metadata = res['tx_metadata_list'][0],
  484. )
  485. async def relay_tx(self,tx_hex):
  486. ret = await self.c.call('relay_tx',hex=tx_hex)
  487. try:
  488. msg('\n Relayed {}'.format( CoinTxID(ret['tx_hash']).hl() ))
  489. except:
  490. msg(f'\n Server returned: {ret!s}')
  491. class create(wallet):
  492. name = 'create'
  493. desc = 'Creat'
  494. past = 'created'
  495. wallet_exists = False
  496. opts = ('restore_height',)
  497. def check_uopts(self):
  498. if int(uopt.restore_height) < 0:
  499. die(1,f"{uopt.restore_height}: invalid value for --restore-height (less than zero)")
  500. async def process_wallet(self,d,fn):
  501. msg_r('') # for pexpect
  502. from .baseconv import baseconv
  503. ret = await self.c.call(
  504. 'restore_deterministic_wallet',
  505. filename = os.path.basename(fn),
  506. password = d.wallet_passwd,
  507. seed = baseconv.fromhex(d.sec,'xmrseed',tostr=True),
  508. restore_height = uopt.restore_height,
  509. language = 'English' )
  510. pp_msg(ret) if opt.debug else msg(' Address: {}'.format( ret['address'] ))
  511. return True
  512. class sync(wallet):
  513. name = 'sync'
  514. desc = 'Sync'
  515. past = 'synced'
  516. opts = ('rescan_blockchain',)
  517. def __init__(self,uarg_tuple,uopt_tuple):
  518. super().__init__(uarg_tuple,uopt_tuple)
  519. host,port = uopt.daemon.split(':') if uopt.daemon else ('localhost',self.wd.daemon_port)
  520. self.dc = MoneroRPCClientRaw(host=host, port=int(port), user=None, passwd=None)
  521. self.accts_data = {}
  522. async def process_wallet(self,d,fn):
  523. chain_height = (await self.dc.call('get_height'))['height']
  524. msg(f' Chain height: {chain_height}')
  525. t_start = time.time()
  526. msg_r(' Opening wallet...')
  527. await self.c.call(
  528. 'open_wallet',
  529. filename=os.path.basename(fn),
  530. password=d.wallet_passwd )
  531. msg('done')
  532. msg_r(' Getting wallet height (be patient, this could take a long time)...')
  533. wallet_height = (await self.c.call('get_height'))['height']
  534. msg_r('\r' + ' '*68 + '\r')
  535. msg(f' Wallet height: {wallet_height} ')
  536. behind = chain_height - wallet_height
  537. if behind > 1000:
  538. msg_r(f' Wallet is {behind} blocks behind chain tip. Please be patient. Syncing...')
  539. ret = await self.c.call('refresh')
  540. if behind > 1000:
  541. msg('done')
  542. if ret['received_money']:
  543. msg(' Wallet has received funds')
  544. for i in range(2):
  545. wallet_height = (await self.c.call('get_height'))['height']
  546. if wallet_height >= chain_height:
  547. break
  548. ymsg(f' Wallet failed to sync (wallet height [{wallet_height}] < chain height [{chain_height}])')
  549. if i or not uopt.rescan_blockchain:
  550. break
  551. msg_r(' Rescanning blockchain, please be patient...')
  552. await self.c.call('rescan_blockchain')
  553. await self.c.call('refresh')
  554. msg('done')
  555. t_elapsed = int(time.time() - t_start)
  556. bn = os.path.basename(fn)
  557. a,b = await self.rpc(self,d).get_accts(print=False)
  558. msg(' Balance: {} Unlocked balance: {}'.format(
  559. hl_amt(a['total_balance']),
  560. hl_amt(a['total_unlocked_balance']),
  561. ))
  562. self.accts_data[bn] = { 'accts': a, 'addrs': b }
  563. msg(f' Wallet height: {wallet_height}')
  564. msg(' Sync time: {:02}:{:02}'.format(
  565. t_elapsed // 60,
  566. t_elapsed % 60 ))
  567. await self.c.call('close_wallet')
  568. return wallet_height >= chain_height
  569. def post_main(self):
  570. d = self.accts_data
  571. for n,k in enumerate(d):
  572. ad = self.addr_data[n]
  573. self.rpc(self,ad).print_accts( d[k]['accts'], d[k]['addrs'], indent='')
  574. col1_w = max(map(len,d)) + 1
  575. fs = '{:%s} {} {}' % col1_w
  576. tbals = [0,0]
  577. msg('\n'+fs.format('Wallet','Balance ','Unlocked Balance'))
  578. for k in d:
  579. b = d[k]['accts']['total_balance']
  580. ub = d[k]['accts']['total_unlocked_balance']
  581. msg(fs.format( k + ':', fmt_amt(b), fmt_amt(ub) ))
  582. tbals[0] += b
  583. tbals[1] += ub
  584. msg(fs.format( '-'*col1_w, '-'*18, '-'*18 ))
  585. msg(fs.format( 'TOTAL:', fmt_amt(tbals[0]), fmt_amt(tbals[1]) ))
  586. class sweep(wallet):
  587. name = 'sweep'
  588. desc = 'Sweep'
  589. past = 'swept'
  590. spec_id = 'sweep_spec'
  591. spec_key = ( (1,'source'), (3,'dest') )
  592. opts = ('do_not_relay','tx_relay_daemon')
  593. def create_addr_data(self):
  594. m = re.fullmatch(uarg_info[self.spec_id].pat,uarg.spec,re.ASCII)
  595. if not m:
  596. fs = "{!r}: invalid {!r} arg: for {} operation, it must have format {!r}"
  597. die(1,fs.format( uarg.spec, self.spec_id, self.name, uarg_info[self.spec_id].annot ))
  598. def gen():
  599. for i,k in self.spec_key:
  600. if m[i] == None:
  601. setattr(self,k,None)
  602. else:
  603. idx = int(m[i])
  604. try:
  605. res = [d for d in self.kal.data if d.idx == idx][0]
  606. except:
  607. die(1,'Supplied key-address file does not contain address {}:{}'.format(
  608. self.kal.al_id.sid,
  609. idx ))
  610. else:
  611. setattr(self,k,res)
  612. yield res
  613. self.addr_data = list(gen())
  614. self.account = int(m[2])
  615. if self.name == 'transfer':
  616. self.dest_addr = CoinAddr(self.proto,m[3])
  617. self.amount = self.proto.coin_amt(m[4])
  618. def init_tx_relay_daemon(self):
  619. m = re.fullmatch(uarg_info['tx_relay_daemon'].pat,uopt.tx_relay_daemon,re.ASCII)
  620. self.wd2 = MoneroWalletDaemon(
  621. proto = self.proto,
  622. wallet_dir = uopt.wallet_dir or '.',
  623. test_suite = g.test_suite,
  624. daemon_addr = m[1],
  625. proxy = m[2],
  626. port_shift = 16 )
  627. if g.test_suite:
  628. self.wd2.usr_daemon_args = ['--daemon-ssl-allow-any-cert']
  629. self.wd2.start()
  630. self.c = MoneroWalletRPCClient(
  631. host = self.wd2.host,
  632. port = self.wd2.rpc_port,
  633. user = self.wd2.user,
  634. passwd = self.wd2.passwd
  635. )
  636. async def main(self):
  637. gmsg(f'\n{self.desc}ing account #{self.account} of wallet {self.source.idx}' + (
  638. f': {self.amount} XMR to {self.dest_addr}' if self.name == 'transfer'
  639. else ' to new address' if self.dest == None
  640. else f' to new account in wallet {self.dest.idx}' ))
  641. h = self.rpc(self,self.source)
  642. await h.open_wallet('source')
  643. accts_data = (await h.get_accts())[0]
  644. max_acct = len(accts_data['subaddress_accounts']) - 1
  645. if self.account > max_acct:
  646. die(1,f'{self.account}: requested account index out of bounds (>{max_acct})')
  647. await h.print_addrs(accts_data,self.account)
  648. if self.name == 'transfer':
  649. dest_addr = self.dest_addr
  650. elif self.dest == None:
  651. dest_acct = self.account
  652. if keypress_confirm(f'\nCreate new address for account #{self.account}?'):
  653. dest_addr_chk = await h.create_new_addr(self.account)
  654. elif keypress_confirm(f'Sweep to last existing address of account #{self.account}?'):
  655. dest_addr_chk = None
  656. else:
  657. die(1,'Exiting at user request')
  658. dest_addr,dest_addr_idx = await h.get_last_addr(self.account,display=not dest_addr_chk)
  659. assert dest_addr_chk in (None,dest_addr), 'dest_addr_chk1'
  660. await h.print_addrs(accts_data,self.account)
  661. else:
  662. await h.close_wallet('source')
  663. bn = os.path.basename(self.get_wallet_fn(self.dest))
  664. h2 = self.rpc(self,self.dest)
  665. await h2.open_wallet('destination')
  666. accts_data = (await h2.get_accts())[0]
  667. if keypress_confirm(f'\nCreate new account for wallet {bn!r}?'):
  668. dest_acct,dest_addr = await h2.create_acct()
  669. dest_addr_idx = 0
  670. await h2.get_accts()
  671. elif keypress_confirm(f'Sweep to last existing account of wallet {bn!r}?'):
  672. dest_acct,dest_addr_chk = h2.get_last_acct(accts_data)
  673. dest_addr,dest_addr_idx = await h2.get_last_addr(dest_acct,display=False)
  674. assert dest_addr_chk == dest_addr, 'dest_addr_chk2'
  675. else:
  676. die(1,'Exiting at user request')
  677. await h2.close_wallet('destination')
  678. await h.open_wallet('source',refresh=False)
  679. msg(f'\n Creating {self.name} transaction...')
  680. if self.name == 'transfer':
  681. new_tx = await h.make_transfer_tx(self.account,dest_addr,self.amount)
  682. elif self.name == 'sweep':
  683. new_tx = await h.make_sweep_tx(self.account,dest_acct,dest_addr_idx,dest_addr)
  684. msg('\n' + new_tx.get_info(indent=' '))
  685. if uopt.tx_relay_daemon:
  686. self.display_tx_relay_info(indent=' ')
  687. if uopt.do_not_relay:
  688. msg('Saving TX data to file')
  689. new_tx.write(delete_metadata=True)
  690. elif keypress_confirm(f'Relay {self.name} transaction?'):
  691. w_desc = 'source'
  692. if uopt.tx_relay_daemon:
  693. await h.close_wallet('source')
  694. msg('')
  695. self.init_tx_relay_daemon()
  696. h = self.rpc(self,self.source)
  697. w_desc = 'TX relay source'
  698. await h.open_wallet(w_desc,refresh=False)
  699. msg_r(f'\n Relaying {self.name} transaction...')
  700. await h.relay_tx(new_tx.data.metadata)
  701. await h.close_wallet(w_desc)
  702. gmsg('\n\nAll done')
  703. else:
  704. await h.close_wallet('source')
  705. die(1,'\nExiting at user request')
  706. return True
  707. class transfer(sweep):
  708. name = 'transfer'
  709. desc = 'Transfer'
  710. past = 'transferred'
  711. spec_id = 'transfer_spec'
  712. spec_key = ( (1,'source'), )
  713. class relay(base):
  714. name = 'relay'
  715. desc = 'Relay'
  716. past = 'relayed'
  717. opts = ('tx_relay_daemon',)
  718. def __init__(self,uarg_tuple,uopt_tuple):
  719. super().__init__(uarg_tuple,uopt_tuple)
  720. if uopt.tx_relay_daemon:
  721. m = re.fullmatch(uarg_info['tx_relay_daemon'].pat,uopt.tx_relay_daemon,re.ASCII)
  722. host,port = m[1].split(':')
  723. proxy = m[2]
  724. else:
  725. from .daemon import CoinDaemon
  726. md = CoinDaemon('xmr',test_suite=g.test_suite)
  727. host,port = md.host,md.rpc_port
  728. proxy = None
  729. self.dc = MoneroRPCClientRaw(
  730. host = host,
  731. port = int(port),
  732. user = None,
  733. passwd = None,
  734. proxy = proxy )
  735. self.tx = MoneroMMGenTX.Signed(uarg.infile)
  736. async def main(self):
  737. msg('\n' + self.tx.get_info())
  738. if uopt.tx_relay_daemon:
  739. self.display_tx_relay_info()
  740. if keypress_confirm('Relay transaction?'):
  741. res = await self.dc.call(
  742. 'send_raw_transaction',
  743. tx_as_hex = self.tx.data.blob
  744. )
  745. if res['status'] == 'OK':
  746. msg('Status: ' + green('OK'))
  747. if res['not_relayed']:
  748. ymsg('Transaction not relayed')
  749. return True
  750. else:
  751. raise RPCFailure(repr(res))
  752. else:
  753. die(1,'Exiting at user request')