Skip to content

Commit ea56cbe

Browse files
committed
fixed getTransactionHash bug
1 parent 18d6007 commit ea56cbe

File tree

1 file changed

+15
-12
lines changed

1 file changed

+15
-12
lines changed

src/model/transaction/Transaction.ts

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -87,14 +87,17 @@ export abstract class Transaction {
8787
* @returns {string} Returns Transaction Payload hash
8888
*/
8989
public static createTransactionHash(transactionPayload: string, generationHashBuffer: number[], networkType: NetworkType): string {
90+
const type = parseInt(Convert.uint8ToHex(Convert.hexToUint8(transactionPayload.substring(220, 224)).reverse()), 16);
9091
const byteBuffer = Array.from(Convert.hexToUint8(transactionPayload));
92+
const byteBufferWithoutHeader = byteBuffer.slice(4 + 64 + 32 + 8);
93+
const dataBytes = type === TransactionType.AGGREGATE_BONDED || type === TransactionType.AGGREGATE_COMPLETE ?
94+
generationHashBuffer.concat(byteBufferWithoutHeader.slice(0, 52)) :
95+
generationHashBuffer.concat(byteBufferWithoutHeader);
9196
const signingBytes = byteBuffer
92-
.slice(8, 40)
93-
.concat(byteBuffer
94-
.slice(4 + 4 + 64, 8 + 64 + 32 + 4))
95-
.concat(generationHashBuffer)
97+
.slice(8, 40) // first half of signature
9698
.concat(byteBuffer
97-
.splice(4 + 64 + 32 + 8, byteBuffer.length));
99+
.slice(4 + 4 + 64, 8 + 64 + 32)) // signer
100+
.concat(dataBytes);
98101

99102
const hash = new Uint8Array(32);
100103
const signSchema = SHA3Hasher.resolveSignSchema(networkType);
@@ -124,7 +127,7 @@ export abstract class Transaction {
124127
const generationHashBytes = Array.from(Convert.hexToUint8(generationHash));
125128
const signSchema = SHA3Hasher.resolveSignSchema(account.networkType);
126129
const byteBuffer = Array.from(this.generateBytes());
127-
const signingBytes = this.getSigningByte(generationHashBytes);
130+
const signingBytes = this.getSigningBytes(byteBuffer, generationHashBytes);
128131
const keyPairEncoded = KeyPair.createKeyPairFromPrivateKeyString(account.privateKey, signSchema);
129132
const signature = Array.from(KeyPair.sign(account, new Uint8Array(signingBytes), signSchema));
130133
const signedTransactionBuffer = byteBuffer
@@ -146,15 +149,15 @@ export abstract class Transaction {
146149
/**
147150
* @internal
148151
* Generate signing bytes
149-
* @param generationHashByte GenerationHashBuffer
152+
* @param payloadBytes Payload buffer
153+
* @param generationHashBytes GenerationHash buffer
150154
*/
151-
private getSigningByte(generationHashByte: number[]) {
152-
const byteBuffer = Array.from(this.generateBytes());
153-
const byteBufferWithoutHeader = byteBuffer.slice(4 + 64 + 32 + 8);
155+
protected getSigningBytes(payloadBytes: number[], generationHashBytes: number[]) {
156+
const byteBufferWithoutHeader = payloadBytes.slice(4 + 64 + 32 + 8);
154157
if (this.type === TransactionType.AGGREGATE_BONDED || this.type === TransactionType.AGGREGATE_COMPLETE) {
155-
return generationHashByte.concat(byteBufferWithoutHeader.slice(0, 52));
158+
return generationHashBytes.concat(byteBufferWithoutHeader.slice(0, 52));
156159
} else {
157-
return generationHashByte.concat(byteBufferWithoutHeader);
160+
return generationHashBytes.concat(byteBufferWithoutHeader);
158161
}
159162
}
160163

0 commit comments

Comments
 (0)