Skip to content

Conversation

@andozw
Copy link
Contributor

@andozw andozw commented Nov 15, 2024

A duplicate signature would pass the validation test. This fixes that bug and adds more test coverage for the validateAndSort method.

ivmaykov
ivmaykov previously approved these changes Nov 15, 2024
Copy link
Contributor

@ivmaykov ivmaykov left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Approving with some nits, please address before merging.

// This needs to be the same sort that ScriptBuilder.createRedeemScript does.
pubkeys.sort(ECKey.PUBKEY_COMPARATOR);
List<byte[]> sortedSigs = new ArrayList<>();
Set<String> seenSigs = new HashSet<>();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

super nit: I think this would work as Set<ByteString>, and then you don't have to do the byte string -> byte array -> hex string conversion on line 156 below. Not crucial though.

}

private ECKey newMockPubKey() {
return new ECKey();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

does this constructor generate a new random key pair? If so, it's not really a "Mock", it's a real key. So maybe call the method newRandomKeyPair() instead?

return new ECKey();
}

private Signature createValidMockSignature(byte[] hash, ECKey pubkey) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

naming nit: ECKey is a key pair (since it's being used to sign below), not a public key

naming nit: this is not a mock signature since nothing is being mocked. We're actually creating a real signature over the provided data with a real key. Maybe call it createValidSignature()?

pubkeys.sort(ECKey.PUBKEY_COMPARATOR);

byte[] hash = generateMockHash("mock input");
List<Signature> signatures = Arrays.asList(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

formatting nit: fix indent

Comment on lines 637 to 690
private Signature createInvalidMockSignature() {
return Signature.newBuilder()
.setHash(ByteString.copyFrom(new byte[]{0}))
.setDer(ByteString.copyFrom(new byte[]{0}))
.build();
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

naming nit: maybe call this createInvalidEmptySignature()?

Also could be interesting to have a test case where we flip a bit in a valid signature, you will probably want a separate test helper for that.

.build();
}

private byte[] generateMockHash(String inputData) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

naming nit: this is not a mock hash, there is no mocking going on. It's a real sha-256 hash of the input string. Maybe call it generateStringHash()?

Comment on lines 557 to 593
Signature validSig = createValidMockSignature(hash, pubkeys.get(0));
List<Signature> signatures = Arrays.asList(validSig, validSig);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: what happens if we sign twice with the same key rather than have 2 references to the same sig object? It should fail the same way because bitcoin signatures are deterministic, right? We should probably add that test case too.

@ivmaykov
Copy link
Contributor

@andozw you will have to sign your commit with a PGP key that's registered to your GH account in order to merge (repo setting). I can help you set that up on Monday if you don't have it configured.

@andozw andozw force-pushed the seana.20241115.subzero-utils-test branch from 401ad6d to 98c19a6 Compare November 19, 2024 21:07
ivmaykov
ivmaykov previously approved these changes Nov 19, 2024
Copy link
Contributor

@ivmaykov ivmaykov left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!

if (sig.hasHash()) {
if (!Arrays.equals(sig.getHash().toByteArray(), hash)) {
ByteString sigHash = sig.getHash();
ByteString expectedHash = ByteString.copyFrom(hash);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

super nit: we only need to create this once, before the loop.

A duplicate signature would pass the validation test. This fixes that
bug and adds more test coverage for the validateAndSort method.
@andozw andozw force-pushed the seana.20241115.subzero-utils-test branch from 98c19a6 to 0a5aea9 Compare November 19, 2024 21:33
Copy link
Contributor

@ivmaykov ivmaykov left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@andozw andozw merged commit 7d3c6c6 into master Nov 19, 2024
11 checks passed
@andozw andozw deleted the seana.20241115.subzero-utils-test branch November 19, 2024 21:55
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants