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

Minor warning fix, and improvement to transaction signing API #94

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions CoreBitcoin/BTCBase58.m
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
__block BIGNUM bn; BN_init(&bn); BN_zero(&bn);
__block BIGNUM bnChar; BN_init(&bnChar);

void(^finish)() = ^{
void(^finish)(void) = ^{
if (pctx) BN_CTX_free(pctx);
BN_clear_free(&bn58);
BN_clear_free(&bn);
Expand Down Expand Up @@ -134,7 +134,7 @@
__block BIGNUM dv; BN_init(&dv); BN_zero(&dv);
__block BIGNUM rem; BN_init(&rem); BN_zero(&rem);

void(^finish)() = ^{
void(^finish)(void) = ^{
if (pctx) BN_CTX_free(pctx);
BN_clear_free(&bn58);
BN_clear_free(&bn0);
Expand Down
4 changes: 4 additions & 0 deletions CoreBitcoin/BTCTransaction.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ static const BTCAmount BTCTransactionDefaultFeeRate = 10000; // 10K satoshis per
@class BTCScript;
@class BTCTransactionInput;
@class BTCTransactionOutput;
@class BTCKey;

/*!
* Converts string transaction ID (reversed tx hash in hex format) to transaction hash.
Expand Down Expand Up @@ -128,6 +129,9 @@ NSString* BTCTransactionIDFromHash(NSData* txhash) DEPRECATED_ATTRIBUTE;
// You should supply the output script of the previous transaction, desired hash type and input index in this transaction.
- (NSData*) signatureHashForScript:(BTCScript*)subscript inputIndex:(uint32_t)inputIndex hashType:(BTCSignatureHashType)hashType error:(NSError**)errorOut;

// Attempts to sign the index at position i with key, returns failure / success
- (BOOL)attemptToSignInputAtIndex:(uint32_t)i withKey:(BTCKey *)key error:(NSError **)errorOut;

// Adds input script
- (void) addInput:(BTCTransactionInput*)input;

Expand Down
47 changes: 47 additions & 0 deletions CoreBitcoin/BTCTransaction.m
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
#import "BTCScript.h"
#import "BTCErrors.h"
#import "BTCHashID.h"
#import "BTCKey.h"
#import "BTCAddress.h"

NSData* BTCTransactionHashFromID(NSString* txid) {
return BTCHashFromID(txid);
Expand Down Expand Up @@ -335,7 +337,52 @@ - (BOOL) parseStream:(NSInputStream*)stream {

#pragma mark - Signing a transaction

// Attempts to sign the index at position i with key, returns failure / success
- (BOOL)attemptToSignInputAtIndex:(uint32_t)i withKey:(BTCKey *)key error:(NSError **)errorOut
{
BTCTransactionInput *txin = self.inputs[i];

// We stored output script here earlier.
BTCScript* outputScript = txin.signatureScript;

NSData* cpk = key.compressedPublicKey;
NSData* ucpk = key.uncompressedPublicKey;

BTCSignatureHashType hashtype = SIGHASH_ALL;

NSData* sighash = [self signatureHashForScript:[outputScript copy] inputIndex:i hashType:hashtype error:errorOut];
if (!sighash) {
return NO;
}

// Most common case: P2PKH with compressed pubkey (because of BIP32)
BTCScript* p2cpkhScript = [[BTCScript alloc] initWithAddress:[BTCPublicKeyAddress addressWithData:BTCHash160(cpk)]];
if ([outputScript.data isEqual:p2cpkhScript.data]) {
txin.signatureScript = [[[BTCScript new] appendData:[key signatureForHash:sighash hashType:hashtype]] appendData:cpk];
return YES;
}

// Less common case: P2PKH with uncompressed pubkey (when not using BIP32)
BTCScript* p2ucpkhScript = [[BTCScript alloc] initWithAddress:[BTCPublicKeyAddress addressWithData:BTCHash160(ucpk)]];
if ([outputScript.data isEqual:p2ucpkhScript.data]) {
txin.signatureScript = [[[BTCScript new] appendData:[key signatureForHash:sighash hashType:hashtype]] appendData:ucpk];
return YES;
}

BTCScript* p2cpkScript = [[[BTCScript new] appendData:cpk] appendOpcode:OP_CHECKSIG];
BTCScript* p2ucpkScript = [[[BTCScript new] appendData:ucpk] appendOpcode:OP_CHECKSIG];

if ([outputScript.data isEqual:p2cpkScript] ||
[outputScript.data isEqual:p2ucpkScript]) {
txin.signatureScript = [[BTCScript new] appendData:[key signatureForHash:sighash hashType:hashtype]];
return YES;
} else {
// Not supported script type.
// Try custom signature.
}

return NO;
}

// Hash for signing a transaction.
// You should supply the output script of the previous transaction, desired hash type and input index in this transaction.
Expand Down
36 changes: 1 addition & 35 deletions CoreBitcoin/BTCTransactionBuilder.m
Original file line number Diff line number Diff line change
Expand Up @@ -294,41 +294,7 @@ - (BOOL) attemptToSignTransactionInput:(BTCTransactionInput*)txin tx:(BTCTransac
}

if (key) {
NSData* cpk = key.compressedPublicKey;
NSData* ucpk = key.uncompressedPublicKey;

BTCSignatureHashType hashtype = SIGHASH_ALL;

NSData* sighash = [tx signatureHashForScript:[outputScript copy] inputIndex:i hashType:hashtype error:errorOut];
if (!sighash) {
return NO;
}

// Most common case: P2PKH with compressed pubkey (because of BIP32)
BTCScript* p2cpkhScript = [[BTCScript alloc] initWithAddress:[BTCPublicKeyAddress addressWithData:BTCHash160(cpk)]];
if ([outputScript.data isEqual:p2cpkhScript.data]) {
txin.signatureScript = [[[BTCScript new] appendData:[key signatureForHash:sighash hashType:hashtype]] appendData:cpk];
return YES;
}

// Less common case: P2PKH with uncompressed pubkey (when not using BIP32)
BTCScript* p2ucpkhScript = [[BTCScript alloc] initWithAddress:[BTCPublicKeyAddress addressWithData:BTCHash160(ucpk)]];
if ([outputScript.data isEqual:p2ucpkhScript.data]) {
txin.signatureScript = [[[BTCScript new] appendData:[key signatureForHash:sighash hashType:hashtype]] appendData:ucpk];
return YES;
}

BTCScript* p2cpkScript = [[[BTCScript new] appendData:cpk] appendOpcode:OP_CHECKSIG];
BTCScript* p2ucpkScript = [[[BTCScript new] appendData:ucpk] appendOpcode:OP_CHECKSIG];

if ([outputScript.data isEqual:p2cpkScript] ||
[outputScript.data isEqual:p2ucpkScript]) {
txin.signatureScript = [[BTCScript new] appendData:[key signatureForHash:sighash hashType:hashtype]];
return YES;
} else {
// Not supported script type.
// Try custom signature.
}
[tx attemptToSignInputAtIndex:i withKey:key error:errorOut];
} // if key

// Ask to sign the transaction input to sign this if that's some kind of special input or script.
Expand Down