-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #136 from onflow/get-account-key-endpoints
Get account key endpoints
- Loading branch information
Showing
18 changed files
with
1,332 additions
and
411 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
59 changes: 59 additions & 0 deletions
59
...c/main/java/org/onflow/examples/java/getAccountKeys/GetAccountKeysAccessAPIConnector.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
package org.onflow.examples.java.getAccountKeys; | ||
|
||
import org.onflow.flow.sdk.FlowAccessApi; | ||
import org.onflow.flow.sdk.FlowAccountKey; | ||
import org.onflow.flow.sdk.FlowAddress; | ||
|
||
import java.util.List; | ||
|
||
public class GetAccountKeysAccessAPIConnector { | ||
private final FlowAccessApi accessAPI; | ||
|
||
public GetAccountKeysAccessAPIConnector(FlowAccessApi accessAPI) { | ||
this.accessAPI = accessAPI; | ||
} | ||
|
||
public FlowAccountKey getAccountKeyAtLatestBlock(FlowAddress address, int keyIndex) { | ||
FlowAccessApi.AccessApiCallResponse<FlowAccountKey> response = accessAPI.getAccountKeyAtLatestBlock(address, keyIndex); | ||
|
||
if (response instanceof FlowAccessApi.AccessApiCallResponse.Success) { | ||
return ((FlowAccessApi.AccessApiCallResponse.Success<FlowAccountKey>) response).getData(); | ||
} else { | ||
FlowAccessApi.AccessApiCallResponse.Error errorResponse = (FlowAccessApi.AccessApiCallResponse.Error) response; | ||
throw new RuntimeException(errorResponse.getMessage(), errorResponse.getThrowable()); | ||
} | ||
} | ||
|
||
public FlowAccountKey getAccountKeyAtBlockHeight(FlowAddress address, int keyIndex, long height) { | ||
FlowAccessApi.AccessApiCallResponse<FlowAccountKey> response = accessAPI.getAccountKeyAtBlockHeight(address, keyIndex, height); | ||
|
||
if (response instanceof FlowAccessApi.AccessApiCallResponse.Success) { | ||
return ((FlowAccessApi.AccessApiCallResponse.Success<FlowAccountKey>) response).getData(); | ||
} else { | ||
FlowAccessApi.AccessApiCallResponse.Error errorResponse = (FlowAccessApi.AccessApiCallResponse.Error) response; | ||
throw new RuntimeException(errorResponse.getMessage(), errorResponse.getThrowable()); | ||
} | ||
} | ||
|
||
public List<FlowAccountKey> getAccountKeysAtLatestBlock(FlowAddress address) { | ||
FlowAccessApi.AccessApiCallResponse<List<FlowAccountKey>> response = accessAPI.getAccountKeysAtLatestBlock(address); | ||
|
||
if (response instanceof FlowAccessApi.AccessApiCallResponse.Success) { | ||
return ((FlowAccessApi.AccessApiCallResponse.Success<List<FlowAccountKey>>) response).getData(); | ||
} else { | ||
FlowAccessApi.AccessApiCallResponse.Error errorResponse = (FlowAccessApi.AccessApiCallResponse.Error) response; | ||
throw new RuntimeException(errorResponse.getMessage(), errorResponse.getThrowable()); | ||
} | ||
} | ||
|
||
public List<FlowAccountKey> getAccountKeysAtBlockHeight(FlowAddress address, long height) { | ||
FlowAccessApi.AccessApiCallResponse<List<FlowAccountKey>> response = accessAPI.getAccountKeysAtBlockHeight(address, height); | ||
|
||
if (response instanceof FlowAccessApi.AccessApiCallResponse.Success) { | ||
return ((FlowAccessApi.AccessApiCallResponse.Success<List<FlowAccountKey>>) response).getData(); | ||
} else { | ||
FlowAccessApi.AccessApiCallResponse.Error errorResponse = (FlowAccessApi.AccessApiCallResponse.Error) response; | ||
throw new RuntimeException(errorResponse.getMessage(), errorResponse.getThrowable()); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
114 changes: 114 additions & 0 deletions
114
...st/java/org/onflow/examples/java/getAccountKeys/GetAccountKeysAccessAPIConnectorTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
package org.onflow.examples.java.getAccountKeys; | ||
|
||
import org.junit.jupiter.api.Assertions; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
import org.onflow.flow.common.test.FlowEmulatorProjectTest; | ||
import org.onflow.flow.common.test.FlowServiceAccountCredentials; | ||
import org.onflow.flow.common.test.FlowTestClient; | ||
import org.onflow.flow.sdk.FlowAccessApi; | ||
import org.onflow.flow.sdk.FlowAddress; | ||
import org.onflow.flow.sdk.FlowAccountKey; | ||
import org.onflow.flow.common.test.TestAccount; | ||
import org.onflow.flow.sdk.FlowBlock; | ||
|
||
import java.util.List; | ||
|
||
@FlowEmulatorProjectTest(flowJsonLocation = "../flow/flow.json") | ||
public class GetAccountKeysAccessAPIConnectorTest { | ||
@FlowTestClient | ||
private FlowAccessApi accessAPI; | ||
|
||
@FlowServiceAccountCredentials | ||
private TestAccount serviceAccount; | ||
|
||
private GetAccountKeysAccessAPIConnector keysAPIConnector; | ||
|
||
@BeforeEach | ||
public void setup() { | ||
keysAPIConnector = new GetAccountKeysAccessAPIConnector(accessAPI); | ||
} | ||
|
||
@Test | ||
public void testCanFetchAccountKeyAtLatestBlock() { | ||
FlowAddress address = serviceAccount.getFlowAddress(); | ||
int keyIndex = 0; | ||
|
||
FlowAccountKey accountKey = keysAPIConnector.getAccountKeyAtLatestBlock(address, keyIndex); | ||
|
||
Assertions.assertNotNull(accountKey, "Account key should not be null"); | ||
Assertions.assertEquals(keyIndex, accountKey.getSequenceNumber(), "Account key index should match the requested index"); | ||
Assertions.assertTrue(accountKey.getWeight() > 0, "Account key weight should be positive"); | ||
} | ||
|
||
@Test | ||
public void testCanFetchAccountKeyAtSpecificBlockHeight() { | ||
FlowAddress address = serviceAccount.getFlowAddress(); | ||
int keyIndex = 0; | ||
|
||
FlowAccessApi.AccessApiCallResponse<FlowBlock> latestBlockResponse = accessAPI.getLatestBlock(true, false); | ||
|
||
if (latestBlockResponse instanceof FlowAccessApi.AccessApiCallResponse.Success) { | ||
FlowBlock latestBlock = ((FlowAccessApi.AccessApiCallResponse.Success<FlowBlock>) latestBlockResponse).getData(); | ||
FlowAccountKey accountKey = keysAPIConnector.getAccountKeyAtBlockHeight(address, keyIndex, latestBlock.getHeight()); | ||
|
||
Assertions.assertNotNull(accountKey, "Account key at specific block height should not be null"); | ||
Assertions.assertEquals(keyIndex, accountKey.getSequenceNumber(), "Account key index at specific block height should match requested index"); | ||
Assertions.assertTrue(accountKey.getWeight() > 0, "Account key weight should be positive"); | ||
|
||
} else if (latestBlockResponse instanceof FlowAccessApi.AccessApiCallResponse.Error error) { | ||
throw new RuntimeException("Failed to retrieve the latest block: " + error.getMessage(), error.getThrowable()); | ||
} | ||
} | ||
|
||
@Test | ||
public void testCanFetchAllAccountKeysAtLatestBlock() { | ||
FlowAddress address = serviceAccount.getFlowAddress(); | ||
|
||
List<FlowAccountKey> accountKeys = keysAPIConnector.getAccountKeysAtLatestBlock(address); | ||
|
||
Assertions.assertNotNull(accountKeys, "Account keys list should not be null"); | ||
Assertions.assertFalse(accountKeys.isEmpty(), "Account keys list should not be empty"); | ||
accountKeys.forEach(key -> Assertions.assertTrue(key.getWeight() > 0, "Each account key weight should be positive")); | ||
} | ||
|
||
@Test | ||
public void testCanFetchAllAccountKeysAtSpecificBlockHeight() { | ||
FlowAddress address = serviceAccount.getFlowAddress(); | ||
|
||
FlowAccessApi.AccessApiCallResponse<FlowBlock> latestBlockResponse = accessAPI.getLatestBlock(true, false); | ||
|
||
if (latestBlockResponse instanceof FlowAccessApi.AccessApiCallResponse.Success) { | ||
FlowBlock latestBlock = ((FlowAccessApi.AccessApiCallResponse.Success<FlowBlock>) latestBlockResponse).getData(); | ||
List<FlowAccountKey> accountKeys = keysAPIConnector.getAccountKeysAtBlockHeight(address, latestBlock.getHeight()); | ||
|
||
Assertions.assertNotNull(accountKeys, "Account keys list at specific block height should not be null"); | ||
Assertions.assertFalse(accountKeys.isEmpty(), "Account keys list at specific block height should not be empty"); | ||
accountKeys.forEach(key -> Assertions.assertTrue(key.getWeight() > 0, "Each account key weight should be positive")); | ||
} else if (latestBlockResponse instanceof FlowAccessApi.AccessApiCallResponse.Error error) { | ||
throw new RuntimeException("Failed to retrieve the latest block: " + error.getMessage(), error.getThrowable()); | ||
} | ||
} | ||
|
||
@Test | ||
public void testAccountKeysMatchAtLatestBlockAndSpecificBlockHeight() { | ||
FlowAddress address = serviceAccount.getFlowAddress(); | ||
|
||
List<FlowAccountKey> keysAtLatestBlock = keysAPIConnector.getAccountKeysAtLatestBlock(address); | ||
|
||
FlowAccessApi.AccessApiCallResponse<FlowBlock> latestBlockResponse = accessAPI.getLatestBlock(true, false); | ||
|
||
if (latestBlockResponse instanceof FlowAccessApi.AccessApiCallResponse.Success) { | ||
FlowBlock latestBlock = ((FlowAccessApi.AccessApiCallResponse.Success<FlowBlock>) latestBlockResponse).getData(); | ||
List<FlowAccountKey> keysAtSpecificHeight = keysAPIConnector.getAccountKeysAtBlockHeight(address, latestBlock.getHeight()); | ||
|
||
Assertions.assertEquals(keysAtLatestBlock.size(), keysAtSpecificHeight.size(), "Number of account keys should match at latest block and specific block height"); | ||
|
||
for (int i = 0; i < keysAtLatestBlock.size(); i++) { | ||
Assertions.assertEquals(keysAtLatestBlock.get(i), keysAtSpecificHeight.get(i), "Account key at index " + i + " should match between latest block and specific block height"); | ||
} | ||
} else if (latestBlockResponse instanceof FlowAccessApi.AccessApiCallResponse.Error error) { | ||
throw new RuntimeException("Failed to retrieve the latest block: " + error.getMessage(), error.getThrowable()); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
31 changes: 31 additions & 0 deletions
31
...main/kotlin/org/onflow/examples/kotlin/getAccountKeys/GetAccountKeysAccessAPIConnector.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package org.onflow.examples.kotlin.getAccountKeys | ||
|
||
import org.onflow.flow.sdk.* | ||
|
||
internal class GetAccountKeysAccessAPIConnector( | ||
private val accessAPI: FlowAccessApi | ||
) { | ||
fun getAccountKeyAtLatestBlock(address: FlowAddress, keyIndex: Int): FlowAccountKey = | ||
when (val response = accessAPI.getAccountKeyAtLatestBlock(address, keyIndex)) { | ||
is FlowAccessApi.AccessApiCallResponse.Success -> response.data | ||
is FlowAccessApi.AccessApiCallResponse.Error -> throw Exception(response.message, response.throwable) | ||
} | ||
|
||
fun getAccountKeyAtBlockHeight(address: FlowAddress, keyIndex: Int, height: Long): FlowAccountKey = | ||
when (val response = accessAPI.getAccountKeyAtBlockHeight(address, keyIndex, height)) { | ||
is FlowAccessApi.AccessApiCallResponse.Success -> response.data | ||
is FlowAccessApi.AccessApiCallResponse.Error -> throw Exception(response.message, response.throwable) | ||
} | ||
|
||
fun getAccountKeysAtLatestBlock(address: FlowAddress): List<FlowAccountKey> = | ||
when (val response = accessAPI.getAccountKeysAtLatestBlock(address)) { | ||
is FlowAccessApi.AccessApiCallResponse.Success -> response.data | ||
is FlowAccessApi.AccessApiCallResponse.Error -> throw Exception(response.message, response.throwable) | ||
} | ||
|
||
fun getAccountKeysAtBlockHeight(address: FlowAddress, height: Long): List<FlowAccountKey> = | ||
when (val response = accessAPI.getAccountKeysAtBlockHeight(address, height)) { | ||
is FlowAccessApi.AccessApiCallResponse.Success -> response.data | ||
is FlowAccessApi.AccessApiCallResponse.Error -> throw Exception(response.message, response.throwable) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.