Skip to content

Commit

Permalink
[macOS] Added QR scanner facility using platform-native helper app.
Browse files Browse the repository at this point in the history
  • Loading branch information
cculianu authored and ecdsa committed Nov 29, 2018
1 parent d0e6b8c commit db89286
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 2 deletions.
3 changes: 3 additions & 0 deletions .gitmodules
Expand Up @@ -4,3 +4,6 @@
[submodule "contrib/deterministic-build/electrum-locale"]
path = contrib/deterministic-build/electrum-locale
url = https://github.com/spesmilo/electrum-locale
[submodule "contrib/CalinsQRReader"]
path = contrib/CalinsQRReader
url = https://github.com/spesmilo/CalinsQRReader
1 change: 1 addition & 0 deletions contrib/CalinsQRReader
Submodule CalinsQRReader added at 201891
2 changes: 1 addition & 1 deletion contrib/build-osx/base.sh
Expand Up @@ -21,7 +21,7 @@ function DoCodeSignMaybe { # ARGS: infoName fileOrDirName codesignIdentity
identity="$3"
deep=""
if [ -z "$identity" ]; then
# we are ok with them not passing anything -- master script calls us always even if no identity is specified
# we are ok with them not passing anything; master script calls us unconditionally even if no identity is specified
return
fi
if [ -d "$file" ]; then
Expand Down
9 changes: 9 additions & 0 deletions contrib/build-osx/make_osx
Expand Up @@ -16,6 +16,7 @@ export PYTHONHASHSEED=22
VERSION=`git describe --tags --dirty --always`

which brew > /dev/null 2>&1 || fail "Please install brew from https://brew.sh/ to continue"
which xcodebuild > /dev/null 2>&1 || fail "Please install Xcode and xcode command line tools to continue"

# Code Signing: See https://developer.apple.com/library/archive/documentation/Security/Conceptual/CodeSigningGuide/Procedures/Procedures.html
APP_SIGN=""
Expand Down Expand Up @@ -87,6 +88,14 @@ popd
cp $BUILDDIR/secp256k1/.libs/libsecp256k1.0.dylib contrib/build-osx
DoCodeSignMaybe "libsecp256k1" "contrib/build-osx/libsecp256k1.0.dylib" "$APP_SIGN" # If APP_SIGN is empty will be a noop

info "Building CalinsQRReader..."
d=contrib/CalinsQRReader
pushd $d
rm -fr build
xcodebuild || fail "Could not build CalinsQRReader"
popd
DoCodeSignMaybe "CalinsQRReader.app" "${d}/build/Release/CalinsQRReader.app" "$APP_SIGN" # If APP_SIGN is empty will be a noop


info "Installing requirements..."
python3 -m pip install -Ir ./contrib/deterministic-build/requirements.txt --user && \
Expand Down
3 changes: 3 additions & 0 deletions contrib/build-osx/osx.spec
Expand Up @@ -41,6 +41,9 @@ datas += collect_data_files('btchip')
datas += collect_data_files('keepkeylib')
datas += collect_data_files('ckcc')

# Add the QR Scanner helper app
datas += [(electrum + "contrib/CalinsQRReader/build/Release/CalinsQRReader.app", "./contrib/CalinsQRReader/build/Release/CalinsQRReader.app")]

# Add libusb so Trezor and Safe-T mini will work
binaries = [(electrum + "contrib/build-osx/libusb-1.0.dylib", ".")]
binaries += [(electrum + "contrib/build-osx/libsecp256k1.0.dylib", ".")]
Expand Down
25 changes: 24 additions & 1 deletion electrum/qrscanner.py
Expand Up @@ -40,7 +40,7 @@
libzbar = None


def scan_barcode(device='', timeout=-1, display=True, threaded=False, try_again=True):
def scan_barcode_ctypes(device='', timeout=-1, display=True, threaded=False, try_again=True):
if libzbar is None:
raise RuntimeError("Cannot start QR scanner; zbar not available.")
libzbar.zbar_symbol_get_data.restype = ctypes.c_char_p
Expand Down Expand Up @@ -69,6 +69,29 @@ def scan_barcode(device='', timeout=-1, display=True, threaded=False, try_again=
data = libzbar.zbar_symbol_get_data(symbol)
return data.decode('utf8')

def scan_barcode_osx(*args_ignored, **kwargs_ignored):
import subprocess
# NOTE: This code needs to be modified if the positions of this file changes with respect to the helper app!
# This assumes the built macOS .app bundle which ends up putting the helper app in
# .app/contrib/CalinsQRReader/build/Release/CalinsQRReader.app.
root_ec_dir = os.path.abspath(os.path.dirname(__file__) + "/../")
prog = root_ec_dir + "/" + "contrib/CalinsQRReader/build/Release/CalinsQRReader.app/Contents/MacOS/CalinsQRReader"
if not os.path.exists(prog):
raise RuntimeError("Cannot start QR scanner; helper app not found.")
data = ''
try:
# This will run the "CalinsQRReader" helper app (which also gets bundled with the built .app)
# Just like the zbar implementation -- the main app will hang until the QR window returns a QR code
# (or is closed). Communication with the subprocess is done via stdout.
# See contrib/CalinsQRReader for the helper app source code.
with subprocess.Popen([prog], stdout=subprocess.PIPE) as p:
data = p.stdout.read().decode('utf-8').strip()
return data
except OSError as e:
raise RuntimeError("Cannot start camera helper app; {}".format(e.strerror))

scan_barcode = scan_barcode_osx if sys.platform == 'darwin' else scan_barcode_ctypes

def _find_system_cameras():
device_root = "/sys/class/video4linux"
devices = {} # Name -> device
Expand Down

0 comments on commit db89286

Please sign in to comment.