Skip to content

Commit

Permalink
test: add tests for PublicKey.verifyTransaction()
Browse files Browse the repository at this point in the history
Signed-off-by: Daniel Akhterov <[email protected]>
  • Loading branch information
janaakhterov committed Dec 21, 2021
1 parent 63c9359 commit d4ac3dc
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 1 deletion.
9 changes: 8 additions & 1 deletion sdk/src/main/java/com/hedera/hashgraph/sdk/PublicKey.java
Original file line number Diff line number Diff line change
Expand Up @@ -91,10 +91,17 @@ public boolean verifyTransaction(Transaction<?> transaction) {
transaction.freeze();
}

for (var publicKey : transaction.publicKeys) {
if (publicKey.equals(this)) {
return true;
}
}

for (var signedTransaction : transaction.innerSignedTransactions) {
@Var var found = false;

for (var sigPair : signedTransaction.getSigMap().getSigPairList()) {
if (sigPair.getPubKeyPrefix().equals(ByteString.copyFrom(toBytes()))) {
if (sigPair.getPubKeyPrefix().equals(ByteString.copyFrom(toBytesRaw()))) {
found = true;

if (!verify(signedTransaction.getBodyBytes().toByteArray(), extractSignatureFromProtobuf(sigPair).toByteArray())) {
Expand Down
16 changes: 16 additions & 0 deletions sdk/src/test/java/com/hedera/hashgraph/sdk/ECDSAPublicKeyTest.java
Original file line number Diff line number Diff line change
@@ -1,15 +1,31 @@
package com.hedera.hashgraph.sdk;

import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;

import java.util.Collections;

import static org.junit.jupiter.api.Assertions.assertArrayEquals;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;

public class ECDSAPublicKeyTest {
@Test
void verifyTransaction() {
var transaction = new TransferTransaction()
.setNodeAccountIds(Collections.singletonList(new AccountId(3)))
.setTransactionId(TransactionId.generate(new AccountId(4)))
.freeze();

var key = PrivateKey.fromStringECDSA("8776c6b831a1b61ac10dac0304a2843de4716f54b1919bb91a2685d0fe3f3048");
key.signTransaction(transaction);

Assertions.assertTrue(key.getPublicKey().verifyTransaction(transaction));
}

@Test
@DisplayName("public key can be recovered from bytes")
void keyByteSerialization() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
package com.hedera.hashgraph.sdk;

import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;

import java.util.Collections;

import static org.junit.jupiter.api.Assertions.assertArrayEquals;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
Expand All @@ -13,6 +16,19 @@ class Ed25519PublicKeyTest {
private static final String TEST_KEY_STR = "302a300506032b6570032100e0c8ec2758a5879ffac226a13c0c516b799e72e35141a0dd828f94d37988a4b7";
private static final String TEST_KEY_STR_RAW = "e0c8ec2758a5879ffac226a13c0c516b799e72e35141a0dd828f94d37988a4b7";

@Test
void verifyTransaction() {
var transaction = new TransferTransaction()
.setNodeAccountIds(Collections.singletonList(new AccountId(3)))
.setTransactionId(TransactionId.generate(new AccountId(4)))
.freeze();

var key = PrivateKey.fromStringED25519("8776c6b831a1b61ac10dac0304a2843de4716f54b1919bb91a2685d0fe3f3048");
key.signTransaction(transaction);

Assertions.assertTrue(key.getPublicKey().verifyTransaction(transaction));
}

@Test
@DisplayName("public key can be recovered from bytes")
void keyByteSerialization() {
Expand Down

0 comments on commit d4ac3dc

Please sign in to comment.