oleganza / CoreBitcoin

Awesome Bitcoin toolkit for ObjC and Swift
Do What The F*ck You Want To Public License
719 stars 255 forks source link

How to add multi address unspent output signature script in transaction input? #115

Closed tbsiosdev closed 6 years ago

tbsiosdev commented 6 years ago
   // Sign all inputs. We now have both inputs and outputs defined, so we can sign the transaction.
for (int i = 0; i < utTxOuts.count; i++) {
    BTCKey* key = [[BTCKey alloc] initWithPrivateKey:[keyArry objectAtIndex:i]];

    BTCTransactionOutput* txout = utTxOuts[i];
    BTCTransactionInput* txin = tx.inputs[i];

    BTCScript* sigScript = [[BTCScript alloc] init];

    NSData* d1 = tx.data;
    NSLog(@"%@",d1);
    BTCSignatureHashType hashtype = BTCSignatureHashTypeAll;

    NSData* hash = [tx signatureHashForScript:txout.script inputIndex:i hashType:hashtype error:nil];

    NSData* d2 = tx.data;
    NSLog(@"%@",d2);
    NSAssert([d1 isEqual:d2], @"Transaction must not change within signatureHashForScript!");

    NSLog(@"Hash for input %d: %@", i, BTCHexFromData(hash));
    if (!hash) {
        return @{@"Status" : @false,@"Amount": @"0", @"Message": @"0"};
    }

    NSData* signatureForScript = [key signatureForHash:hash hashType:hashtype];
    [sigScript appendData:signatureForScript];
    [sigScript appendData:key.compressedPublicKey];

// trim hashtype byte to check the signature.
    NSData* sig = [signatureForScript subdataWithRange:NSMakeRange(0, signatureForScript.length - 1)];
    NSAssert([key isValidSignature:sig hash:hash], @"Signature must be valid");
    NSLog(@"%@",sig);

    txin.signatureScript = sigScript;
}

first address signature script was adding successfully. when I am trying to add second address script. it is providing invalid transaction hash to me.

please help me out.