11import os
22import sys
33import warnings
4-
4+ import qrcode
5+ import qrcode .image .svg
56
67MIN_PYTHON_VERSION = "3.6.1" # FIXME duplicated from setup.py
78_min_python_version_tuple = tuple (map (int , (MIN_PYTHON_VERSION .split ("." ))))
@@ -19,6 +20,7 @@ def check_imports():
1920 import ecdsa
2021 import certifi
2122 import qrcode
23+ import qrcode .image .svg
2224 import google .protobuf
2325 import jsonrpclib
2426 import aiorpcx
@@ -41,11 +43,13 @@ def check_imports():
4143from electrum import SimpleConfig
4244from electrum .wallet import Wallet
4345from electrum .storage import WalletStorage , get_derivation_used_for_hw_device_encryption
44- from electrum .util import print_msg , print_stderr , json_encode , json_decode , UserCancelled
46+ from electrum .util import print_msg , print_stderr , json_encode , json_decode , UserCancelled , bfh
4547from electrum .util import InvalidPassword
4648from electrum .commands import get_parser , known_commands , Commands , config_variables
4749from electrum import daemon
4850from electrum import keystore
51+ from electrum .transaction import Transaction
52+ from electrum .bitcoin import base_encode
4953
5054_logger = get_logger (__name__ )
5155
@@ -229,41 +233,62 @@ def init_plugins(config, gui_name):
229233
230234
231235class WalletScripting (object ):
236+ # Default configurations, overwrite from setup or call
237+ config_options = {
238+ 'verbosity' : '' ,
239+ 'verbosity_shortcuts' : '' ,
240+ 'portable' : False ,
241+ 'testnet' : False ,
242+ 'regtest' : False ,
243+ 'simnet' : False ,
244+ 'cwd' : os .getcwd ()
245+ }
246+
232247 @classmethod
233- def setup (cls ):
234- pass
248+ def setup (cls , ** kwargs ):
249+ cls . config_options . update ( kwargs )
235250
236251 @classmethod
237252 def call (cls , command , * args , ** kwargs ):
238253 # command line
239254 config_options = {
240- 'verbosity' : '' ,
241- 'verbosity_shortcuts' : '' ,
242- 'portable' : False ,
243- 'testnet' : False ,
244- 'regtest' : False ,
245- 'simnet' : False ,
246255 'cmd' : command
247256 }
248- config_options ['cwd' ] = os .getcwd ()
257+ config_options .update (cls .config_options )
258+ config_options .update (kwargs )
259+
260+ # print(config_options)
249261 config = SimpleConfig (config_options )
250262 cmdname = config .get ('cmd' )
251263
252264 server = daemon .get_server (config )
253265 init_cmdline (config_options , server )
266+
254267 if server is not None :
255- print ("goes online" )
256268 result = server .run_cmdline (config_options )
257- print (result )
258269 else :
259- print ("goes offline" )
260270 cmd = known_commands [cmdname ]
261271 if cmd .requires_network :
262272 print_msg ("Daemon not running; try 'electrum daemon start'" )
263273 sys .exit (1 )
264274 else :
265275 plugins = init_plugins (config , 'cmdline' )
266276 result = run_offline_command (config , config_options , plugins )
267- print (result )
277+
278+ return result
279+
280+ @classmethod
281+ def qr (cls , tx , filename ):
282+ if type (tx ) == dict and 'hex' in tx :
283+ tx = tx ['hex' ]
284+ tx = Transaction (tx )
285+ text = bfh (str (tx ))
286+ text = base_encode (text , base = 43 )
287+ img = qrcode .make (text , image_factory = qrcode .image .svg .SvgPathImage )
288+
289+ img .save (filename )
290+ print ("QR Image saved as " + filename )
291+
292+
268293
269294
0 commit comments