Skip to content

Commit

Permalink
added flash toggle button to qr scanner
Browse files Browse the repository at this point in the history
  • Loading branch information
Aaron Voisine committed Apr 17, 2014
1 parent 2ceba44 commit e494de3
Show file tree
Hide file tree
Showing 13 changed files with 55 additions and 52 deletions.
9 changes: 0 additions & 9 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -1,15 +1,6 @@
[submodule "AFNetworking"]
path = AFNetworking
url = git://github.com/AFNetworking/AFNetworking.git
[submodule "ZBar"]
path = ZBar
url = git://github.com/ZBar/ZBar.git
[submodule "QR-Code-Encoder-for-Objective-C"]
path = QR-Code-Encoder-for-Objective-C
url = git://github.com/myang-git/QR-Code-Encoder-for-Objective-C.git
[submodule "MBProgressHUD"]
path = MBProgressHUD
url = git://github.com/matej/MBProgressHUD.git
[submodule "SocketRocket"]
path = SocketRocket
url = [email protected]:square/SocketRocket.git
4 changes: 4 additions & 0 deletions ZincWallet.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -724,6 +724,7 @@
759145B717EB7CE00040D496 /* [email protected] in Resources */ = {isa = PBXBuildFile; fileRef = 759145B617EB7CE00040D496 /* [email protected] */; };
759145B917EB87BF0040D496 /* [email protected] in Resources */ = {isa = PBXBuildFile; fileRef = 759145B817EB87BF0040D496 /* [email protected] */; };
75920F86174AB82100C1C03D /* NSMutableData+Bitcoin.m in Sources */ = {isa = PBXBuildFile; fileRef = 75920F85174AB82100C1C03D /* NSMutableData+Bitcoin.m */; };
759292F8190046FB00CDA950 /* [email protected] in Resources */ = {isa = PBXBuildFile; fileRef = 759292F7190046FB00CDA950 /* [email protected] */; };
7593BD0A17A24D1500F814F6 /* [email protected] in Resources */ = {isa = PBXBuildFile; fileRef = 7593BD0917A24D1500F814F6 /* [email protected] */; };
7597028117E15B1E00CDC14A /* ZNNavigationBar.m in Sources */ = {isa = PBXBuildFile; fileRef = 7597028017E15B1E00CDC14A /* ZNNavigationBar.m */; };
7597028417E15B5600CDC14A /* ZNStoryboardSegue.m in Sources */ = {isa = PBXBuildFile; fileRef = 7597028317E15B5600CDC14A /* ZNStoryboardSegue.m */; };
Expand Down Expand Up @@ -1778,6 +1779,7 @@
759145B817EB87BF0040D496 /* [email protected] */ = {isa = PBXFileReference; lastKnownFileType = image.png; path = "[email protected]"; sourceTree = "<group>"; };
75920F84174AB82100C1C03D /* NSMutableData+Bitcoin.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "NSMutableData+Bitcoin.h"; sourceTree = "<group>"; };
75920F85174AB82100C1C03D /* NSMutableData+Bitcoin.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "NSMutableData+Bitcoin.m"; sourceTree = "<group>"; };
759292F7190046FB00CDA950 /* [email protected] */ = {isa = PBXFileReference; lastKnownFileType = image.png; path = "[email protected]"; sourceTree = "<group>"; };
7593BD0917A24D1500F814F6 /* [email protected] */ = {isa = PBXFileReference; lastKnownFileType = image.png; path = "[email protected]"; sourceTree = "<group>"; };
7597027F17E15B1E00CDC14A /* ZNNavigationBar.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = ZNNavigationBar.h; sourceTree = "<group>"; };
7597028017E15B1E00CDC14A /* ZNNavigationBar.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = ZNNavigationBar.m; sourceTree = "<group>"; };
Expand Down Expand Up @@ -3317,6 +3319,7 @@
751E809E1789797500E3CFB2 /* [email protected] */,
759145B617EB7CE00040D496 /* [email protected] */,
759145B817EB87BF0040D496 /* [email protected] */,
759292F7190046FB00CDA950 /* [email protected] */,
75C2F3D3178D3061009F8CD3 /* [email protected] */,
75C2F3D1178D3061009F8CD3 /* [email protected] */,
75C2F3D2178D3061009F8CD3 /* [email protected] */,
Expand Down Expand Up @@ -3811,6 +3814,7 @@
75D8F38C173ABE3C0080D95E /* InfoPlist.strings in Resources */,
75D8F394173ABE3C0080D95E /* Default.png in Resources */,
75D8F396173ABE3C0080D95E /* [email protected] in Resources */,
759292F8190046FB00CDA950 /* [email protected] in Resources */,
75D8F398173ABE3C0080D95E /* [email protected] in Resources */,
75D8F39B173ABE3C0080D95E /* MainStoryboard_iPhone.storyboard in Resources */,
75F17C4417455F2D00D9F228 /* deletetoleft-inv.png in Resources */,
Expand Down
11 changes: 6 additions & 5 deletions ZincWallet/NSString+Base58.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,12 @@
#define BITCOIN_PRIVKEY 128
#define BITCOIN_PRIVKEY_TEST 239

#define BIP38_NOEC_PREFIX 0x0142
#define BIP38_EC_PREFIX 0x0143
#define BIP38_NOEC_FLAG (0x80 | 0x40)
#define BIP38_LOTSEQUENCE_FLAG 0x04
#define BIP38_INVALID_FLAG (0x10 | 0x08 | 0x02 | 0x01)
#define BIP38_NOEC_PREFIX 0x0142
#define BIP38_EC_PREFIX 0x0143
#define BIP38_NOEC_FLAG (0x80 | 0x40)
#define BIP38_COMPRESSED_FLAG 0x20
#define BIP38_LOTSEQUENCE_FLAG 0x04
#define BIP38_INVALID_FLAG (0x10 | 0x08 | 0x02 | 0x01)

CFAllocatorRef SecureAllocator();

Expand Down
6 changes: 5 additions & 1 deletion ZincWallet/NSString+Base58.m
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ + (NSString *)base58WithData:(NSData *)d
BN_CTX *ctx = BN_CTX_new();
BIGNUM base, x, r;

BN_CTX_start(ctx);
BN_init(&base);
BN_init(&x);
BN_init(&r);
Expand All @@ -120,6 +121,7 @@ + (NSString *)base58WithData:(NSData *)d
BN_clear_free(&r);
BN_clear_free(&x);
BN_free(&base);
BN_CTX_end(ctx);
BN_CTX_free(ctx);

NSString *ret = CFBridgingRelease(CFStringCreateWithCString(SecureAllocator(), &s[i], kCFStringEncodingUTF8));
Expand All @@ -143,7 +145,8 @@ - (NSData *)base58ToData
unsigned int b;
BN_CTX *ctx = BN_CTX_new();
BIGNUM base, x, y;


BN_CTX_start(ctx);
BN_init(&base);
BN_init(&x);
BN_init(&y);
Expand Down Expand Up @@ -198,6 +201,7 @@ - (NSData *)base58ToData
BN_clear_free(&y);
BN_clear_free(&x);
BN_free(&base);
BN_CTX_end(ctx);
BN_CTX_free(ctx);

return d;
Expand Down
2 changes: 1 addition & 1 deletion ZincWallet/ZNElectrumSequence.m
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ - (NSArray *)privateKeys:(NSArray *)n internal:(BOOL)internal fromSeed:(NSData *
NSData *_mpk = [[ZNKey keyWithSecret:secexp compressed:NO] publicKey];
NSData *mpk = [NSData dataWithBytesNoCopy:(unsigned char *)_mpk.bytes + 1 length:_mpk.length - 1 freeWhenDone:NO];
BN_CTX *ctx = BN_CTX_new();
__block BIGNUM sequencebn, secexpbn, order;
BIGNUM sequencebn, secexpbn, order;
EC_GROUP *group = EC_GROUP_new_by_curve_name(NID_secp256k1);

BN_CTX_start(ctx);
Expand Down
38 changes: 15 additions & 23 deletions ZincWallet/ZNKey+BIP38.m
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,10 @@
#import "NSString+Base58.h"
#import "NSData+Hash.h"
#import "NSMutableData+Bitcoin.h"
#import <CommonCrypto/CommonKeyDerivation.h>
#import <CommonCrypto/CommonCryptor.h>
#import <openssl/crypto.h>
#import <CommonCrypto/CommonCrypto.h>
#import <openssl/ecdsa.h>
#import <openssl/obj_mac.h>

#define BIP38_COMPRESSED_FLAG 0x20
#define BIP38_SCRYPT_N 16384
#define BIP38_SCRYPT_R 8
#define BIP38_SCRYPT_P 8
Expand Down Expand Up @@ -157,17 +154,17 @@ static void getPassfactor(BIGNUM *passfactor, uint8_t flag, uint64_t entropy, NS
else BN_bin2bn(prefactor.bytes, (int)prefactor.length, passfactor); // passfactor = prefactor
}

static NSData *getPasspoint(const BIGNUM *passfactor, BN_CTX *ctx)
static NSData *factorPoint(const BIGNUM *factor, BN_CTX *ctx)
{
NSMutableData *passpoint = [NSMutableData secureDataWithLength:33];
NSMutableData *point = [NSMutableData secureDataWithLength:33];
EC_GROUP *group = EC_GROUP_new_by_curve_name(NID_secp256k1);
EC_POINT *p = EC_POINT_new(group);

EC_POINT_mul(group, p, passfactor, NULL, NULL, ctx); // passpoint = elliptic curve point G*passfactor
EC_POINT_point2oct(group, p, POINT_CONVERSION_COMPRESSED, passpoint.mutableBytes, passpoint.length, ctx);
EC_POINT_mul(group, p, factor, NULL, NULL, ctx); // point = elliptic curve point G*factor
EC_POINT_point2oct(group, p, POINT_CONVERSION_COMPRESSED, point.mutableBytes, point.length, ctx);
EC_POINT_clear_free(p);
EC_GROUP_free(group);
return passpoint;
return point;
}

static NSData *getDerived(NSData *passpoint, uint32_t addresshash, uint64_t entropy)
Expand Down Expand Up @@ -204,7 +201,7 @@ + (NSString *)BIP38IntermediateCodeWithSalt:(uint64_t)salt andPassphrase:(NSStri

[code appendBytes:"\x2C\xE9\xB3\xE1\xFF\x39\xE2\x53" length:8];
[code appendBytes:&salt length:sizeof(salt)];
[code appendData:getPasspoint(&passfactor, ctx)];
[code appendData:factorPoint(&passfactor, ctx)]; // passpoint = G*passfactor

BN_clear_free(&passfactor);
BN_CTX_end(ctx);
Expand Down Expand Up @@ -235,7 +232,7 @@ + (NSString *)BIP38IntermediateCodeWithLot:(uint32_t)lot sequence:(uint16_t)sequ

[code appendBytes:"\x2C\xE9\xB3\xE1\xFF\x39\xE2\x51" length:8];
[code appendData:entropy];
[code appendData:getPasspoint(&passfactor, ctx)];
[code appendData:factorPoint(&passfactor, ctx)]; // passpoint = G*passfactor

BN_clear_free(&passfactor);
BN_CTX_end(ctx);
Expand Down Expand Up @@ -270,6 +267,8 @@ + (NSString *)BIP38KeyWithIntermediateCode:(NSString *)code seedb:(NSData *)seed
pubKey.length = EC_POINT_point2oct(group, pubKeyPoint, form, NULL, 0, ctx);
EC_POINT_point2oct(group, pubKeyPoint, form, pubKey.mutableBytes, pubKey.length, ctx);
EC_POINT_clear_free(pubKeyPoint);
EC_POINT_clear_free(p);
EC_GROUP_free(group);

uint16_t prefix = CFSwapInt16HostToBig(BIP38_EC_PREFIX);
uint8_t flag = compressed ? BIP38_COMPRESSED_FLAG : 0;
Expand Down Expand Up @@ -305,11 +304,8 @@ + (NSString *)BIP38KeyWithIntermediateCode:(NSString *)code seedb:(NSData *)seed
[key appendData:encrypted2];

if (confcode) {
NSMutableData *pointb = [NSMutableData secureDataWithLength:33], *pointbx1, *pointbx2, *c;

EC_POINT_mul(group, p, &factorb, NULL, NULL, ctx); // pointb = elliptic curve point G*factorb
EC_POINT_point2oct(group, p, POINT_CONVERSION_COMPRESSED, pointb.mutableBytes, pointb.length, ctx);

NSData *pointb = factorPoint(&factorb, ctx); // pointb = G*factorb
NSMutableData *pointbx1, *pointbx2, *c;
uint8_t pointbprefix = ((const uint8_t *)pointb.bytes)[0] ^ (((const uint8_t *)derived2)[31] & 0x01);

// pointbx1 = AES256Encrypt(pointb[1...16] xor derived1[0...15], derived2)
Expand Down Expand Up @@ -337,8 +333,6 @@ + (NSString *)BIP38KeyWithIntermediateCode:(NSString *)code seedb:(NSData *)seed
*confcode = [NSString base58checkWithData:c];
}

EC_POINT_clear_free(p);
EC_GROUP_free(group);
BN_clear_free(&factorb);
BN_CTX_end(ctx);
BN_CTX_free(ctx);
Expand Down Expand Up @@ -368,7 +362,7 @@ + (BOOL)confirmWithBIP38ConfirmationCode:(NSString *)code address:(NSString *)ad
BN_init(&passfactor);
getPassfactor(&passfactor, flag, entropy, passphrase);

NSData *passpoint = getPasspoint(&passfactor, ctx);
NSData *passpoint = factorPoint(&passfactor, ctx); // passpoint = G*passfactor
NSData *derived = getDerived(passpoint, addresshash, entropy);
const uint64_t *derived1 = (const uint64_t *)derived.bytes, *derived2 = &derived1[4];
NSMutableData *pointb = [NSMutableData secureDataWithLength:33], *pubKey = [NSMutableData secureData];
Expand Down Expand Up @@ -401,9 +395,7 @@ + (BOOL)confirmWithBIP38ConfirmationCode:(NSString *)code address:(NSString *)ad
BN_CTX_end(ctx);
BN_CTX_free(ctx);

if (! [[[ZNKey keyWithPublicKey:pubKey] address] isEqual:address]) return NO;

return YES;
return ([[[ZNKey keyWithPublicKey:pubKey] address] isEqual:address]) ? YES : NO;
}

- (instancetype)initWithBIP38Key:(NSString *)key andPassphrase:(NSString *)passphrase
Expand Down Expand Up @@ -447,7 +439,7 @@ - (instancetype)initWithBIP38Key:(NSString *)key andPassphrase:(NSString *)passp
BN_init(&passfactor);
getPassfactor(&passfactor, flag, entropy, passphrase);

NSData *passpoint = getPasspoint(&passfactor, ctx);
NSData *passpoint = factorPoint(&passfactor, ctx); // passpoint = G*passfactor
NSData *derived = getDerived(passpoint, addresshash, entropy);
const uint64_t *derived1 = (const uint64_t *)derived.bytes, *derived2 = &derived1[4];
NSMutableData *seedb = [NSMutableData secureDataWithLength:24], *o = [NSMutableData secureDataWithLength:16];
Expand Down
4 changes: 2 additions & 2 deletions ZincWallet/ZNMerkleBlock.m
Original file line number Diff line number Diff line change
Expand Up @@ -158,10 +158,10 @@ - (instancetype)initWithBlockHash:(NSData *)blockHash version:(uint32_t)version

// true if merkle tree and timestamp are valid, and proof-of-work matches the stated difficulty target
// NOTE: this only checks if the block difficulty matches the difficulty target in the header, it does not check if the
// target is correct for the block's height in the chain, use verifyDifficultyAtHeight: for that
// target is correct for the block's height in the chain, use verifyDifficultyFromPreviousBlock: for that
- (BOOL)isValid
{
__block NSMutableData *d = [NSMutableData data];
NSMutableData *d = [NSMutableData data];
BIGNUM target, maxTarget, hash;
int hashIdx = 0, flagIdx = 0;
NSData *merkleRoot =
Expand Down
25 changes: 18 additions & 7 deletions ZincWallet/ZNPayViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
#import "ZNBubbleView.h"
#import "NSString+Base58.h"
#import <QuartzCore/QuartzCore.h>
#import <AVFoundation/AVFoundation.h>

//#define BT_CONNECT_TIMEOUT 5.0
#define BUTTON_HEIGHT 44.0
Expand Down Expand Up @@ -115,7 +116,7 @@ - (void)viewDidLoad
}
// else if ([url.scheme isEqual:@"zinc"] && [url.host isEqual:@"x-callback-url"]) {
// if ([url.path isEqual:@"/tx"]) {
// __block NSString *status = nil;
// NSString *status = nil;
//
// for (NSString *arg in [url.query componentsSeparatedByString:@"&"]) {
// NSArray *pair = [arg componentsSeparatedByString:@"="];
Expand Down Expand Up @@ -465,7 +466,7 @@ - (void)confirmSweep:(NSString *)privKey
[self reset:nil];
}
else if (tx) {
__block uint64_t fee = tx.standardFee, amount = fee;
uint64_t fee = tx.standardFee, amount = fee;

for (NSNumber *amt in tx.outputAmounts) {
amount += amt.unsignedLongLongValue;
Expand Down Expand Up @@ -550,16 +551,19 @@ - (IBAction)doIt:(id)sender
NSUInteger idx = [self.requestButtons indexOfObject:sender];

if ([self.requestIDs indexOfObject:QR_ID] == idx) {
//TODO: XXXX add an option to disable flash
[self.navigationController presentViewController:self.zbarController animated:YES completion:^{
NSLog(@"present qr reader complete");
}];

// hide zbarController.view info button

BOOL hasFlash = [[AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo] hasTorch];
UIBarButtonItem *flashButton = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"flash.png"]
style:UIBarButtonItemStylePlain target:self action:@selector(flash:)];

// replace zbarController.view info button with flash toggle
for (UIView *v in self.zbarController.view.subviews) {
for (id t in v.subviews) {
if ([t isKindOfClass:[UIToolbar class]] && [[t items] count] > 0) {
[t setItems:@[[t items][0]]];
if ([t isKindOfClass:[UIToolbar class]] && [[t items] count] > 1) {
[t setItems:hasFlash ? @[[t items][0], [t items][1], flashButton] : @[[t items][0], [t items][1]]];
}
}
}
Expand Down Expand Up @@ -601,6 +605,13 @@ - (IBAction)cancel:(id)sender
if (self.navigationController.topViewController == self.parentViewController) [self reset:sender];
}

- (IBAction)flash:(id)sender
{
AVCaptureDevice *device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];

device.torchMode = device.torchActive ? AVCaptureTorchModeOff : AVCaptureTorchModeOn;
}

#pragma mark - ZNAmountViewControllerDelegate

- (void)amountViewController:(ZNAmountViewController *)amountViewController selectedAmount:(uint64_t)amount
Expand Down
2 changes: 1 addition & 1 deletion ZincWallet/ZNSettingsViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(N
static NSString *disclosureIdent = @"ZNDisclosureCell", *transactionIdent = @"ZNTransactionCell",
*actionIdent = @"ZNActionCell", *restoreIdent = @"ZNRestoreCell";
UITableViewCell *cell = nil;
__block UILabel *textLabel, *detailTextLabel, *unconfirmedLabel, *sentLabel, *noTxLabel, *localCurrencyLabel;
UILabel *textLabel, *detailTextLabel, *unconfirmedLabel, *sentLabel, *noTxLabel, *localCurrencyLabel;

switch (indexPath.section) {
case 0:
Expand Down
2 changes: 1 addition & 1 deletion ZincWallet/ZNTransactionEntity.m
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ - (instancetype)setAttributesFromTx:(ZNTransaction *)tx

- (ZNTransaction *)transaction
{
__block ZNTransaction *tx = [ZNTransaction new];
ZNTransaction *tx = [ZNTransaction new];

[[self managedObjectContext] performBlockAndWait:^{
tx.txHash = self.txHash;
Expand Down
2 changes: 1 addition & 1 deletion ZincWallet/ZNWallet.m
Original file line number Diff line number Diff line change
Expand Up @@ -281,7 +281,7 @@ - (BOOL)containsAddress:(NSString *)address
// returns an unsigned transaction that sends the specified amount from the wallet to the given address
- (ZNTransaction *)transactionFor:(uint64_t)amount to:(NSString *)address withFee:(BOOL)fee
{
__block uint64_t balance = 0, standardFee = 0;
uint64_t balance = 0, standardFee = 0;
ZNTransaction *transaction = [ZNTransaction new];

[transaction addOutputAddress:address amount:amount];
Expand Down
Binary file added ZincWallet/[email protected]
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion ZincWalletTests/ZincWalletTests.m
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,7 @@ - (void)testKeyWithBIP38Key

// password NFC unicode normalization test
key = [ZNKey keyWithBIP38Key:@"6PRW5o9FLp4gJDDVqJQKJFTpMvdsSGJxMYHtHaQBF3ooa8mwD69bapcDQn"
andPassphrase:@"\u03D2\u0301\x00\U00010400\U0001F4A9"];
andPassphrase:@"\u03D2\u0301\0\U00010400\U0001F4A9"];
NSLog(@"privKey = %@", key.privateKey);
XCTAssertEqualObjects(@"5Jajm8eQ22H3pGWLEVCXyvND8dQZhiQhoLJNKjYXk9roUFTMSZ4", key.privateKey,
@"[ZNKey keyWithBIP38Key:andPassphrase:]");
Expand Down

0 comments on commit e494de3

Please sign in to comment.