Skip to content

Commit

Permalink
refactor: change Auth.Signature.publicKey to String (#643)
Browse files Browse the repository at this point in the history
  • Loading branch information
overcat authored Oct 8, 2024
1 parent 0641dc4 commit b7b15ef
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 10 deletions.
11 changes: 6 additions & 5 deletions src/main/java/org/stellar/sdk/Auth.java
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ public static SorobanAuthorizationEntry authorizeEntry(
}
byte[] payload = Util.hash(data);
byte[] signature = signer.sign(payload);
return new Signature(signer.getPublicKey(), signature);
return new Signature(signer.getAccountId(), signature);
};

return authorizeEntry(entry, entrySigner, validUntilLedgerSeq, network);
Expand Down Expand Up @@ -196,7 +196,8 @@ public static SorobanAuthorizationEntry authorizeEntry(
throw new IllegalArgumentException("Unable to convert preimage to bytes", e);
}
byte[] payload = Util.hash(data);
if (!KeyPair.fromPublicKey(signature.publicKey).verify(payload, signature.signature)) {
KeyPair kp = KeyPair.fromAccountId(signature.publicKey);
if (!kp.verify(payload, signature.signature)) {
throw new IllegalArgumentException("signature does not match payload");
}

Expand All @@ -207,7 +208,7 @@ public static SorobanAuthorizationEntry authorizeEntry(
Scv.toMap(
new LinkedHashMap<SCVal, SCVal>() {
{
put(Scv.toSymbol("public_key"), Scv.toBytes(signature.getPublicKey()));
put(Scv.toSymbol("public_key"), Scv.toBytes(kp.getPublicKey()));
put(Scv.toSymbol("signature"), Scv.toBytes(signature.getSignature()));
}
});
Expand Down Expand Up @@ -247,7 +248,7 @@ public static SorobanAuthorizationEntry authorizeInvocation(
try {
byte[] payload = Util.hash(preimage.toXdrByteArray());
byte[] signature = signer.sign(payload);
return new Signature(signer.getPublicKey(), signature);
return new Signature(signer.getAccountId(), signature);
} catch (IOException e) {
throw new UnexpectedException(e);
}
Expand Down Expand Up @@ -309,7 +310,7 @@ public static SorobanAuthorizationEntry authorizeInvocation(
/** A signature, consisting of a public key and a signature. */
@Value
public static class Signature {
byte[] publicKey;
String publicKey;
byte[] signature;
}

Expand Down
10 changes: 5 additions & 5 deletions src/test/java/org/stellar/sdk/AuthTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ public void testSignAuthorizeEntryWithBase64EntryAndFunctionSigner() throws IOEx
}
byte[] payload = Util.hash(data);
byte[] signature = signer.sign(payload);
return new Auth.Signature(signer.getPublicKey(), signature);
return new Auth.Signature(signer.getAccountId(), signature);
};
long validUntilLedgerSeq = 654656L;
Network network = Network.TESTNET;
Expand Down Expand Up @@ -321,7 +321,7 @@ public void testSignAuthorizeEntryWithXdrEntryAndFunctionSigner() throws IOExcep
}
byte[] payload = Util.hash(data);
byte[] signature = signer.sign(payload);
return new Auth.Signature(signer.getPublicKey(), signature);
return new Auth.Signature(signer.getAccountId(), signature);
};
long validUntilLedgerSeq = 654656L;
Network network = Network.TESTNET;
Expand Down Expand Up @@ -455,7 +455,7 @@ public void testSignAuthorizeEntryWithSignatureMismatchThrows() throws IOExcepti
preimage -> {
byte[] invalidData = new byte[20];
byte[] signature = signer.sign(invalidData);
return new Auth.Signature(signer.getPublicKey(), signature);
return new Auth.Signature(signer.getAccountId(), signature);
};
long validUntilLedgerSeq = 654656L;
Network network = Network.TESTNET;
Expand Down Expand Up @@ -565,7 +565,7 @@ public void authorizeInvocationWithFunctionSigner() {
}
byte[] payload = Util.hash(data);
byte[] signature = signer.sign(payload);
return new Auth.Signature(signer.getPublicKey(), signature);
return new Auth.Signature(signer.getAccountId(), signature);
};
long validUntilLedgerSeq = 654656L;
Network network = Network.TESTNET;
Expand Down Expand Up @@ -720,7 +720,7 @@ public void testSignAuthorizeEntryWithFunctionSignerNotEqualCredentialAddress()
}
byte[] payload = Util.hash(data);
byte[] signature = signer.sign(payload);
return new Auth.Signature(signer.getPublicKey(), signature);
return new Auth.Signature(signer.getAccountId(), signature);
};
long validUntilLedgerSeq = 654656L;
Network network = Network.TESTNET;
Expand Down

0 comments on commit b7b15ef

Please sign in to comment.