Skip to content

Commit

Permalink
fix: tx hashing + add simple unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
pankcuf committed Nov 5, 2024
1 parent 399b88c commit cbae8e9
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#import "DSChain.h"
#import "DSTransactionFactory.h"
#import "NSData+Dash.h"
#import "NSMutableData+Dash.h"

@implementation DSAssetUnlockTransaction

Expand All @@ -31,7 +32,7 @@ - (instancetype)initWithMessage:(NSData *)message onChain:(DSChain *)chain {

if (length - off < 1) return nil;
NSNumber *payloadLengthSize = nil;
__unused uint64_t payloadLength = [message varIntAtOffset:off length:&payloadLengthSize];
uint64_t payloadLength = [message varIntAtOffset:off length:&payloadLengthSize];
off += payloadLengthSize.unsignedLongValue;

if (length - off < 1) return nil;
Expand All @@ -55,10 +56,40 @@ - (instancetype)initWithMessage:(NSData *)message onChain:(DSChain *)chain {
off += 96;

self.payloadOffset = off;
if ([self payloadData].length != payloadLength) return nil;
self.txHash = self.data.SHA256_2;

return self;

}
- (BOOL)transactionTypeRequiresInputs {
return NO;
}
- (NSData *)payloadData {
return [self basePayloadData];
}

- (NSData *)basePayloadData {
NSMutableData *data = [NSMutableData data];
[data appendUInt8:self.specialTransactionVersion];
[data appendUInt64:self.index];
[data appendUInt32:self.fee];
[data appendUInt32:self.requestedHeight];
[data appendUInt256:self.quorumHash];
[data appendUInt768:self.quorumSignature];
return data;
}

- (NSData *)toDataWithSubscriptIndex:(NSUInteger)subscriptIndex {
@synchronized(self) {
NSMutableData *data = [[super toDataWithSubscriptIndex:subscriptIndex] mutableCopy];
[data appendCountedData:[self payloadData]];
if (subscriptIndex != NSNotFound) [data appendUInt32:SIGHASH_ALL];
return data;
}
}
- (size_t)size {
return [super size] + [self payloadData].length;
}

@end
3 changes: 2 additions & 1 deletion DashSync/shared/Models/Transactions/Base/DSTransaction.m
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@

#import "DSAccount.h"
#import "DSAddressEntity+CoreDataClass.h"
#import "DSAssetUnlockTransaction.h"
#import "DSChain.h"
#import "DSChainEntity+CoreDataClass.h"
#import "DSChainManager.h"
Expand Down Expand Up @@ -408,7 +409,7 @@ - (NSData *)toDataWithSubscriptIndex:(NSUInteger)subscriptIndex {
NSArray<DSTransactionOutput *> *outputs = self.outputs;
NSUInteger inputsCount = inputs.count;
NSUInteger outputsCount = outputs.count;
BOOL forSigHash = ([self isMemberOfClass:[DSTransaction class]] || [self isMemberOfClass:[DSCreditFundingTransaction class]]) && subscriptIndex != NSNotFound;
BOOL forSigHash = ([self isMemberOfClass:[DSTransaction class]] || [self isMemberOfClass:[DSCreditFundingTransaction class]] || [self isMemberOfClass:[DSAssetUnlockTransaction class]]) && subscriptIndex != NSNotFound;
NSUInteger dataSize = 8 + [NSMutableData sizeOfVarInt:inputsCount] + [NSMutableData sizeOfVarInt:outputsCount] + TX_INPUT_SIZE * inputsCount + TX_OUTPUT_SIZE * outputsCount + (forSigHash ? 4 : 0);

NSMutableData *d = [NSMutableData dataWithCapacity:dataSize];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -160,13 +160,13 @@ - (BOOL)registerTransaction:(DSTransaction *)transaction saveImmediately:(BOOL)s
}
} else if ([transaction isMemberOfClass:[DSAssetLockTransaction class]]) {
DSAssetLockTransaction *assetLockTransaction = (DSAssetLockTransaction *)transaction;
if (![self.assetLockTransactions objectForKey:uint256_data(assetLockTransaction.creditBurnIdentityIdentifier)]) {
if (![self.assetLockTransactions objectForKey:uint256_data(assetLockTransaction.txHash)]) {
[self.assetLockTransactions setObject:transaction forKey:uint256_data(assetLockTransaction.txHash)];
added = TRUE;
}
} else if ([transaction isMemberOfClass:[DSAssetUnlockTransaction class]]) {
DSAssetUnlockTransaction *assetUnlockTransaction = (DSAssetUnlockTransaction *)transaction;
if (![self.assetUnlockTransactions objectForKey:uint256_data(assetUnlockTransaction.creditBurnIdentityIdentifier)]) {
if (![self.assetUnlockTransactions objectForKey:uint256_data(assetUnlockTransaction.txHash)]) {
[self.assetUnlockTransactions setObject:transaction forKey:uint256_data(assetUnlockTransaction.txHash)];
added = TRUE;
}
Expand Down
6 changes: 6 additions & 0 deletions Example/Tests/DSTransactionTests.m
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

#import <XCTest/XCTest.h>

#import "DSAssetUnlockTransaction.h"
#import "DSAuthenticationKeysDerivationPath.h"
#import "DSBlockchainIdentityCloseTransition.h"
#import "DSBlockchainIdentityRegistrationTransition.h"
Expand Down Expand Up @@ -91,6 +92,11 @@ - (void)testTransaction {
XCTAssertEqualObjects(d, tx.data, @"[DSTransaction transactionWithMessage:]");
processor_destroy_opaque_key(k);
}
- (void)testAssetUnlockTx {
NSData *transactionData = @"030009000001a02ffa0d000000001976a9146641c13e0ee2ce2cdf70852bb7ae9853c01f29a988ac0000000091014e00000000000000be000000273e11004130304f40d1820b5e239baecd35249263b1206a1c76e66053ec39a04501000093d3851b6bda0518da51ff8932ef3570be20e7978369dd312947326e135004915c10b5fe0e31e572c40f41cdd941bed8115e314573faf472e1065ca370bdff486db8eaa6bbcba3943e6e5ada6a3c30dee70e39811814e59e1ffc54f3c9fca04f".hexToData;
DSAssetUnlockTransaction *tx = [[DSAssetUnlockTransaction alloc] initWithMessage:transactionData onChain:[DSChain testnet]];
XCTAssert(tx, @"Bad Asset Unlock Tx");
}

- (void)testBlockchainIdentityFundingTransactionUniqueId {
NSData *transactionData = @"0300000002b74030bbda6edd804d4bfb2bdbbb7c207a122f3af2f6283de17074a42c6a5417020000006b483045022100815b175ab1a8fde7d651d78541ba73d2e9b297e6190f5244e1957004aa89d3c902207e1b164499569c1f282fe5533154495186484f7db22dc3dc1ccbdc9b47d997250121027f69794d6c4c942392b1416566aef9eaade43fbf07b63323c721b4518127baadffffffffb74030bbda6edd804d4bfb2bdbbb7c207a122f3af2f6283de17074a42c6a5417010000006b483045022100a7c94fe1bb6ffb66d2bb90fd8786f5bd7a0177b0f3af20342523e64291f51b3e02201f0308f1034c0f6024e368ca18949be42a896dda434520fa95b5651dc5ad3072012102009e3f2eb633ee12c0143f009bf773155a6c1d0f14271d30809b1dc06766aff0ffffffff031027000000000000166a1414ec6c36e6c39a9181f3a261a08a5171425ac5e210270000000000001976a91414ec6c36e6c39a9181f3a261a08a5171425ac5e288acc443953b000000001976a9140d1775b9ed85abeb19fd4a7d8cc88b08a29fe6de88ac00000000".hexToData;
Expand Down

0 comments on commit cbae8e9

Please sign in to comment.