Skip to content

Commit

Permalink
refactor to use same method
Browse files Browse the repository at this point in the history
  • Loading branch information
alfonsobries committed Aug 22, 2024
1 parent 4a98eb7 commit 9d6709e
Showing 1 changed file with 23 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -41,14 +41,14 @@ public void computeId() {
}

public String getId() {
return Hex.encode(Sha256Hash.hash(Serializer.serialize(this)));
return Hex.encode(Sha256Hash.hash(this.serialize()));
}

public Transaction sign(String passphrase) {
ECKey privateKey = PrivateKey.fromPassphrase(passphrase);

this.senderPublicKey = privateKey.getPublicKeyAsHex();
Sha256Hash hash = Sha256Hash.of(Serializer.serialize(this, true, true, false));
Sha256Hash hash = Sha256Hash.of(this.serialize(true, true, false));

this.signature = Hex.encode(signer().sign(hash.getBytes(), privateKey));

Expand All @@ -58,7 +58,7 @@ public Transaction sign(String passphrase) {
public Transaction secondSign(String passphrase) {
ECKey privateKey = PrivateKey.fromPassphrase(passphrase);

Sha256Hash hash = Sha256Hash.of(Serializer.serialize(this, false, true, false));
Sha256Hash hash = Sha256Hash.of(this.serialize(false, true));

this.secondSignature = Hex.encode(signer().sign(hash.getBytes(), privateKey));

Expand Down Expand Up @@ -89,7 +89,7 @@ public boolean verify() {
ECKey keys = ECKey.fromPublicOnly(Hex.decode(this.senderPublicKey));

byte[] signature = Hex.decode(this.signature);
byte[] hash = Sha256Hash.hash(Serializer.serialize(this, true, true, false));
byte[] hash = Sha256Hash.hash(this.serialize(true, true, false));

return verifier().verify(hash, keys, signature);
}
Expand All @@ -98,7 +98,7 @@ public boolean secondVerify(String secondPublicKey) {
ECKey keys = ECKey.fromPublicOnly(Hex.decode(secondPublicKey));

byte[] signature = Hex.decode(this.secondSignature);
byte[] hash = Sha256Hash.hash(Serializer.serialize(this, false, true, false));
byte[] hash = Sha256Hash.hash(this.serialize(false, true, false));

return verifier().verify(hash, keys, signature);
}
Expand Down Expand Up @@ -145,6 +145,24 @@ public HashMap toHashMap() {
return map;
}

public byte[] serialize(
boolean skipSignature, boolean skipSecondSignature, boolean skipMultiSignature) {
return Serializer.serialize(this, skipSignature, skipSecondSignature, skipMultiSignature);
}

public byte[] serialize(boolean skipSignature, boolean skipSecondSignature) {
return serialize(skipSignature, skipSecondSignature, false);
}

public byte[] serialize(boolean skipSignature) {
return serialize(skipSignature, false, false);
}

// Overloaded method with no parameters, defaulting all to false
public byte[] serialize() {
return serialize(false, false, false);
}

public abstract byte[] serializeData();

public abstract void deserializeData(ByteBuffer buffer);
Expand Down

0 comments on commit 9d6709e

Please sign in to comment.