Skip to content

Instantly share code, notes, and snippets.

@payden
Created March 12, 2014 04:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save payden/9500804 to your computer and use it in GitHub Desktop.
Save payden/9500804 to your computer and use it in GitHub Desktop.
//
// OBFViewController.m
// Cryptsy Balance
//
// Created by Payden Sutherland on 3/9/14.
// Copyright (c) 2014 Payden Sutherland. All rights reserved.
//
#import "OBFViewController.h"
#import "CommonCrypto/CommonDigest.h"
#import "CommonCrypto/CommonHMAC.h"
@interface OBFViewController ()
@end
@implementation OBFViewController
NSMutableData *receivedData;
NSString *public_key = @"c756cbc614fea6ebc77bd243ea839d4ba1646f6f";
NSString *private_key = @"861dd18c2d844fe1399ce9fa28bc2ca36fef3caa7cffbc75efdd85f5510ffd73c51154db4c50b7b4";
NSDictionary *json;
UITableView *tv;
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *tableID = @"SimpleTableID";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:tableID];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:tableID];
}
cell.textLabel.text = self.rowArr[indexPath.row];
return cell;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [self.rowArr count];
}
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSHTTPURLResponse *)response {
NSLog(@"Received response data: %ld", (long)[response statusCode]);
[receivedData setLength:0];
}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
NSString *dataString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
NSLog(@"Received data: %lu, %@", (unsigned long)[data length], dataString);
NSError *error;
json = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error];
NSDictionary *ret = [json objectForKey:@"return"];
NSDictionary *bal = [ret objectForKey:@"balances_available"];
NSArray *sorted_keys = [[bal allKeys] sortedArrayUsingComparator:^(id key, id key2) {
return [key compare:key2];
}];
for (NSString *coin in sorted_keys) {
NSLog(@"Coin: %@, balance: %@", coin, [bal objectForKey:coin]);
NSString *val = [bal objectForKey:coin];
NSString *newString = [[NSString alloc] initWithFormat:@"%@ - %.2f", coin, [val floatValue]];
[self.rowArr addObject:newString];
}
[self.tableViewOutlet reloadData];
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
NSLog(@"View did load....");
self.rowArr = [[NSMutableArray alloc] initWithCapacity:0];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"https://api.cryptsy.com/api"]];
NSString *dataToSign = @"method=getinfo&nonce=1";
const char *key = [private_key cStringUsingEncoding:NSASCIIStringEncoding];
const char *data = [dataToSign cStringUsingEncoding:NSASCIIStringEncoding];
unsigned char hash[CC_SHA512_DIGEST_LENGTH];
CCHmac(kCCHmacAlgSHA512, key, strlen(key), data, strlen(data), hash);
NSMutableString *hexForm = [[NSMutableString alloc] initWithCapacity:0];
for (int i = 0; i < sizeof(hash); i++) {
[hexForm appendFormat:@"%02x", hash[i] & 0xff];
}
[request setHTTPMethod:@"POST"];
[request setValue:hexForm forHTTPHeaderField:@"Sign"];
[request setValue:public_key forHTTPHeaderField:@"Key"];
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
[request setHTTPBody:[dataToSign dataUsingEncoding:NSUTF8StringEncoding]];
if (!receivedData) {
receivedData = [NSMutableData dataWithCapacity:0];
}
NSURLConnection *conn = [[NSURLConnection alloc] initWithRequest:request delegate:self];
if (!conn) {
NSLog(@"Cannot connect to Cryptsy");
receivedData = nil;
}
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment