Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Suggestion: detect network version when loading private key from WIF into a BTCKey #92

Open
crazyquark opened this issue Oct 18, 2017 · 2 comments

Comments

@crazyquark
Copy link

crazyquark commented Oct 18, 2017

I made a category for BTCKey that automatically detect the network the wallet is for; use if you find it useful:

#import <CoreBitcoin/BTCKey.h>

#import "BTCAddressBcyTestnet.h"

@interface BTCKey (BcyTestnet)

@property(nonatomic, readonly) BTCPublicKeyAddressBcyTestnet* addressByNetwork;

- (id) initWithWIF:(NSString*)wifString detectNetwork:(BOOL)detect;

@end


#import <CoreBitcoin/BTCData.h>
#import <CoreBitcoin/BTCBase58.h>

#import "BTCKey+BcyTestnet.h"

@implementation BTCKey (BcyTestnet)

uint8_t version;

- (id) initWithWIF:(NSString*)wifString detectNetwork:(BOOL)detect {
    if (!detect) {
        return [self initWithWIF:wifString];
    }
    
    const char* addressString = [wifString cStringUsingEncoding:NSASCIIStringEncoding];
    
    NSMutableData* composedData = BTCDataFromBase58CheckCString(addressString);
    if (!composedData) return nil;
    if (composedData.length < 2) return nil;
    
    // TODO: unsafe
    version = ((unsigned char*)composedData.bytes)[0];
    
    BTCPrivateKeyAddress* addr = [BTCPrivateKeyAddress addressWithString:wifString];
    if (![addr isKindOfClass:[BTCPrivateKeyAddress class]]) {
        return nil;
    }
    return [self initWithPrivateKeyAddress:addr];
}

- (BTCAddress*) addressByNetwork {
    NSData* pubkey = [self publicKey];
    
    // TODO: could cache this value but meh...
    if (version == BTCBcyPrivateKeyAddressVersion) {
        return [BTCPublicKeyAddressBcyTestnet addressWithData:BTCHash160(pubkey)];
    } else if (version == BTCBcyPrivateKeyAddressVersion) {
        return [BTCPrivateKeyAddressTestnet addressWithData:BTCHash160(pubkey)];
    } else {
        return [BTCPrivateKeyAddress addressWithData:BTCHash160(pubkey)];
    }
}

@end
@crazyquark crazyquark changed the title Suggestion: detect network version when loading private key from WIF Suggestion: detect network version when loading private key from WIF into a BTCKey Oct 18, 2017
@crazyquark
Copy link
Author

Sorry for the bad formatting

@crazyquark
Copy link
Author

crazyquark commented Oct 18, 2017

I also created this class to support the BlockCypher testnet:

#import <CoreBitcoin/BTCAddress.h>

enum {
    BTCBcyAddressVersion = 27,
    BTCBcyPrivateKeyAddressVersion = 73
};

@interface BTCPublicKeyAddressBcyTestnet : BTCPublicKeyAddressTestnet

@end

@interface BTCPrivateKeyAddressBcyTestnet : BTCPrivateKeyAddressTestnet

@end

#import "BTCAddressBcyTestnet.h"

@implementation BTCPublicKeyAddressBcyTestnet

+ (void) load {
    [BTCAddress registerAddressClass:self version:[self BTCVersionPrefix]];
}

+ (uint8_t) BTCVersionPrefix {
    return BTCBcyAddressVersion;
}

@end

@implementation BTCPrivateKeyAddressBcyTestnet

+ (void) load {
    [BTCAddress registerAddressClass:self version:[self BTCVersionPrefix]];
}

+ (uint8_t) BTCVersionPrefix {
    return BTCBcyPrivateKeyAddressVersion;
}

@end

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant