TransactionOutput: make field scriptBytes immutable

This commit is contained in:
Andreas Schildbach 2025-02-05 16:32:16 +01:00
parent acd0faaec7
commit b237a34d8b
2 changed files with 8 additions and 6 deletions

View file

@ -1387,8 +1387,9 @@ public class Transaction extends BaseMessage {
BigInteger.valueOf(output.getValue().getValue()),
bosHashOutputs
);
bosHashOutputs.write(VarInt.of(output.getScriptBytes().length).serialize());
bosHashOutputs.write(output.getScriptBytes());
byte[] scriptBytes = output.getScriptBytes();
bosHashOutputs.write(VarInt.of(scriptBytes.length).serialize());
bosHashOutputs.write(scriptBytes);
}
hashOutputs = Sha256Hash.hashTwice(bosHashOutputs.toByteArray());
} else if (basicSigHashType == SigHash.SINGLE.value && inputIndex < outputs.size()) {
@ -1397,8 +1398,9 @@ public class Transaction extends BaseMessage {
BigInteger.valueOf(this.outputs.get(inputIndex).getValue().getValue()),
bosHashOutputs
);
bosHashOutputs.write(VarInt.of(this.outputs.get(inputIndex).getScriptBytes().length).serialize());
bosHashOutputs.write(this.outputs.get(inputIndex).getScriptBytes());
byte[] scriptBytes = this.outputs.get(inputIndex).getScriptBytes();
bosHashOutputs.write(VarInt.of(scriptBytes.length).serialize());
bosHashOutputs.write(scriptBytes);
hashOutputs = Sha256Hash.hashTwice(bosHashOutputs.toByteArray());
}
writeInt32LE(version, bos);

View file

@ -61,7 +61,7 @@ public class TransactionOutput {
// A transaction output has a script used for authenticating that the redeemer is allowed to spend
// this output.
private byte[] scriptBytes;
private final byte[] scriptBytes;
// The script bytes are parsed and turned into a Script on demand.
private Script scriptPubKey;
@ -308,7 +308,7 @@ public class TransactionOutput {
* @return the scriptBytes
*/
public byte[] getScriptBytes() {
return scriptBytes;
return Arrays.copyOf(scriptBytes, scriptBytes.length);
}
/**