             // add Date header if necessary
             mutableRequest.setValue(NSDate().RFC1123String(), forHTTPHeaderField: "Date")
         }
-        if let manager = BRWalletManager.sharedInstance(), tokenData = manager.userAccount,
-            token = tokenData["token"], authKey = getAuthKey() {
-            let sha = buildRequestSigningString(mutableRequest).dataUsingEncoding(NSUTF8StringEncoding)!.SHA256_2()
-            let sig = authKey.compactSign(sha)!.base58String()
-            mutableRequest.setValue("bread \(token):\(sig)", forHTTPHeaderField: "Authorization")
+        if let manager = BRWalletManager.sharedInstance(),
+            tokenData = manager.userAccount,
+            token = tokenData["token"],
+            authKey = getAuthKey(),
+            signingData = buildRequestSigningString(mutableRequest).dataUsingEncoding(NSUTF8StringEncoding),
+            sig = authKey.compactSign(signingData.SHA256_2()) {
+            mutableRequest.setValue("bread \(token):\(sig.base58String())", forHTTPHeaderField: "Authorization")
         }
         return mutableRequest.copy() as! NSURLRequest
     }

        req.setValue("application/json", forHTTPHeaderField: "Content-Type")
        req.setValue("application/json", forHTTPHeaderField: "Accept")
        let reqJson = [
            "pubKey": getAuthKey()!.publicKey!.base58String(),
            "deviceID": getDeviceId()
        ]
        do {
            let dat = try NSJSONSerialization.dataWithJSONObject(reqJson, options: .PrettyPrinted)
            req.HTTPBody = dat
        } catch (let e) {
            log("JSON Serialization error \(e)")
            return handler(NSError(domain: BRAPIClientErrorDomain, code: 500, userInfo: [
                NSLocalizedDescriptionKey: NSLocalizedString("JSON Serialization Error", comment: "")]))
        }
        let task = session.dataTaskWithRequest(req) { (data, resp, err) -> Void in
            if let httpResp = resp as? NSHTTPURLResponse {
                // unsuccessful response from the server
                if httpResp.statusCode != 200 {
                    if let data = data {
                        if let s = String(data: data, encoding: NSUTF8StringEncoding) {
                            self.log("Token error: \(s)")
                        }
                    }
                    return handler(NSError(domain: BRAPIClientErrorDomain, code: httpResp.statusCode, userInfo: [
                            NSLocalizedDescriptionKey: NSLocalizedString("Unable to retrieve API token", comment: "")]))
                }
            }
            if let data = data {
                do {
                    let json = try NSJSONSerialization.JSONObjectWithData(data, options: .AllowFragments)
                    self.log("POST /token: \(json)")
                    if let topObj = json as? NSDictionary,
                        tok = topObj["token"] as? NSString,
                        uid = topObj["userID"] as? NSString {
                        // success! store it in the keychain
                        let kcData = ["token": tok, "userID": uid]
                        BRWalletManager.sharedInstance()!.userAccount = kcData
                    }
                } catch let e {
                    self.log("JSON Deserialization error \(e)")
                }
            }
            handler(err)
        }
        task.resume()
    }
    
    // MARK: URLSession Delegate



@objc public class BRAPIClient: NSObject, NSURLSessionDelegate, NSURLSessionTaskDelegate, BRAPIAdaptor {
    // BRAPIClient is intended to be used as a singleton so this is the instance you should use
    static let sharedClient = BRAPIClient()
    
    // whether or not to emit log messages from this instance of the client
    var logEnabled = true
    
    // proto is the transport protocol to use for talking to the API (either http or https)
    var proto = "https"
    
    // host is the server(s) on which the API is hosted
    var host = "api.breadwallet.com"
    
    // isFetchingAuth is set to true when a request is currently trying to renew authentication (the token)
    // it is useful because fetching auth is not idempotent and not reentrant, so at most one auth attempt
    // can take place at any one time
    private var isFetchingAuth = false
    
    // used when requests are waiting for authentication to be fetched
    private var authFetchGroup: dispatch_group_t = dispatch_group_create()
    
    // storage for the session constructor below
    private var _session: NSURLSession? = nil
    
    // the NSURLSession on which all NSURLSessionTasks are executed
    private var session: NSURLSession {
        if _session == nil {
            let config = NSURLSessionConfiguration.defaultSessionConfiguration()
            _session = NSURLSession(configuration: config, delegate: self, delegateQueue: queue)
        }
        return _session!
    }
    
    // the queue on which the NSURLSession operates
    private var queue = NSOperationQueue()
    
    // convenience getter for the API endpoint
    var baseUrl: String {
        return "\(proto)://\(host)"
    }
    
    // prints whatever you give it if logEnabled is true
    func log(s: String) {
        if !logEnabled {
            return
