diff --git a/javatest/src/test/java/com/lightspark/javatest/LightsparkSyncClientTest.java b/javatest/src/test/java/com/lightspark/javatest/LightsparkSyncClientTest.java index 48b7b2d5..9aa193a6 100644 --- a/javatest/src/test/java/com/lightspark/javatest/LightsparkSyncClientTest.java +++ b/javatest/src/test/java/com/lightspark/javatest/LightsparkSyncClientTest.java @@ -1,6 +1,7 @@ package com.lightspark.javatest; import static com.lightspark.sdk.core.util.PlatformKt.getPlatform; +import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertNotNull; import static org.junit.jupiter.api.Assertions.assertTrue; @@ -10,14 +11,22 @@ import com.lightspark.sdk.auth.AccountApiTokenAuthProvider; import com.lightspark.sdk.core.requester.Query; import com.lightspark.sdk.core.requester.ServerEnvironment; +import com.lightspark.sdk.crypto.PasswordRecoverySigningKeyLoader; import com.lightspark.sdk.graphql.AccountDashboard; import com.lightspark.sdk.model.Account; import com.lightspark.sdk.model.AccountToNodesConnection; import com.lightspark.sdk.model.AccountToTransactionsConnection; +import com.lightspark.sdk.model.ApiToken; import com.lightspark.sdk.model.BitcoinNetwork; +import com.lightspark.sdk.model.CreateApiTokenOutput; import com.lightspark.sdk.model.CurrencyAmount; +import com.lightspark.sdk.model.FeeEstimate; +import com.lightspark.sdk.model.IncomingPayment; +import com.lightspark.sdk.model.Invoice; import com.lightspark.sdk.model.LightsparkNode; +import com.lightspark.sdk.model.OutgoingPayment; import com.lightspark.sdk.model.Transaction; +import com.lightspark.sdk.model.TransactionStatus; import com.lightspark.sdk.model.WithdrawalMode; import org.junit.jupiter.api.Test; @@ -30,6 +39,7 @@ public class LightsparkSyncClientTest { private static final String API_TOKEN_CLIENT_ID = getPlatform().getEnv("LIGHTSPARK_API_TOKEN_CLIENT_ID"); private static final String API_TOKEN_CLIENT_SECRET = getPlatform().getEnv("LIGHTSPARK_API_TOKEN_CLIENT_SECRET"); + private static final String NODE_PASSWORD = getPlatform().getEnv("LIGHTSPARK_TEST_NODE_PASSWORD"); private final ClientConfig config = new ClientConfig() .setAuthProvider(new AccountApiTokenAuthProvider(API_TOKEN_CLIENT_ID, API_TOKEN_CLIENT_SECRET)) .setDefaultBitcoinNetwork(BitcoinNetwork.REGTEST) @@ -41,6 +51,8 @@ public class LightsparkSyncClientTest { public void testAccountDashboard() throws Exception { AccountDashboard dashboard = client.getFullAccountDashboard(BitcoinNetwork.REGTEST, null); assertNotNull(dashboard); + assertNotNull(dashboard.getNodeConnection()); + assertNotNull(dashboard.getNodeConnection().getNodes()); System.out.println(dashboard); } @@ -54,6 +66,56 @@ public void testNodeQueries() throws Exception { System.out.println(nodes.get(0)); } + @Test + public void testCreateAndDeleteApiTokens() throws Exception { + Account account = client.getCurrentAccount(); + List tokens = Objects.requireNonNull(client.executeQuery(account.getApiTokensQuery())).getEntities(); + assertNotNull(tokens); + + CreateApiTokenOutput token = client.createApiToken("test token", false, true); + assertNotNull(token); + List newTokens = Objects.requireNonNull(client.executeQuery(account.getApiTokensQuery())).getEntities(); + assertNotNull(newTokens); + assertEquals(tokens.size() + 1, newTokens.size()); + + client.deleteApiToken(token.getApiToken().getId()); + List finalTokens = Objects.requireNonNull(client.executeQuery(account.getApiTokensQuery())).getEntities(); + assertNotNull(finalTokens); + assertTrue(finalTokens.size() > 0); + assertEquals(finalTokens.size(), tokens.size()); + } + + @Test + public void testGetAccountConductivity() throws Exception { + Account account = client.getCurrentAccount(); + Integer conductivity = client.executeQuery(account.getConductivityQuery()); + assertNotNull(conductivity); + System.out.println("Conductivity: " + conductivity); + } + + @Test + public void testGetBalances() throws Exception { + Account account = client.getCurrentAccount(); + CurrencyAmount localBalance = client.executeQuery(account.getLocalBalanceQuery()); + CurrencyAmount remoteBalance = client.executeQuery(account.getRemoteBalanceQuery()); + assertNotNull(localBalance); + assertNotNull(remoteBalance); + System.out.println("Local balance: " + localBalance); + System.out.println("Remote balance: " + remoteBalance); + } + + @Test + public void testGetMostRecentTransactionDetails() throws Exception { + Account account = client.getCurrentAccount(); + AccountToTransactionsConnection transactions = client.executeQuery(account.getTransactionsQuery(1)); + assertNotNull(transactions); + assertTrue(transactions.getEntities().size() > 0); + Transaction transaction = transactions.getEntities().get(0); + Transaction details = client.executeQuery(Transaction.getTransactionQuery(transaction.getId())); + assertNotNull(details); + System.out.println("Transactions details: " + details); + } + @Test public void testRawQuery() throws Exception { Map variables = new HashMap<>(); @@ -136,14 +198,75 @@ public void testGetWithdrawalFeeEstimate() throws Exception { System.out.println("Withdrawal fee estimate: " + estimate); } - private String getNodeId() throws Exception { + @Test + public void testGetBitcoinFeeEstimates() throws Exception { + FeeEstimate estimate = client.getBitcoinFeeEstimate(); + assertNotNull(estimate); + assertNotNull(estimate.getFeeFast()); + assertNotNull(estimate.getFeeMin()); + System.out.println("Bitcoin fee estimate: " + estimate); + } + + @Test + public void testCreateLnurlInvoice() throws Exception { + LightsparkNode node = getFirstNode(); + String metadata = "[[\\\"text/plain\\\",\\\"Pay to domain.org user ktfan98\\\"],[\\\"text/identifier\\\",\\\"ktfan98@domain.org\\\"]]"; + Invoice paymentRequest = client.createLnurlInvoice(node.getId(), 1000, metadata); + + System.out.println("encoded invoice: " + paymentRequest.getData().getEncodedPaymentRequest()); + } + + @Test + public void testCreateAndCancelInvoice() throws Exception{ + LightsparkNode node = getFirstNode(); + Invoice invoice = client.createInvoice(node.getId(), 1000); + System.out.println("encoded invoice: " + invoice.getData().getEncodedPaymentRequest()); + + Invoice cancelledInvoice = client.cancelInvoice(invoice.getId()); + assertNotNull(cancelledInvoice); + System.out.println("cancelled invoice: " + cancelledInvoice); + } + + @Test + public void testPayingTestModeInvoice() throws Exception { + LightsparkNode node = getFirstNode(); + client.loadNodeSigningKey(node.getId(), new PasswordRecoverySigningKeyLoader(node.getId(), NODE_PASSWORD)); + String invoice = client.createTestModeInvoice(node.getId(), 100_000, "test invoice"); + OutgoingPayment outgoingPayment = client.payInvoice(node.getId(), invoice, 100_000); + assertNotNull(outgoingPayment); + while (outgoingPayment.getStatus() == TransactionStatus.PENDING) { + Thread.sleep(500); + outgoingPayment = client.executeQuery(OutgoingPayment.getOutgoingPaymentQuery(outgoingPayment.getId())); + System.out.println("Payment status: " + outgoingPayment.getStatus()); + } + assertEquals(outgoingPayment.getStatus(), TransactionStatus.SUCCESS); + } + + @Test + public void testCreatingTestModePayment() throws Exception { + LightsparkNode node = getFirstNode(); + client.loadNodeSigningKey(node.getId(), new PasswordRecoverySigningKeyLoader(node.getId(), NODE_PASSWORD)); + Invoice invoice = client.createInvoice(node.getId(), 100_000, "test invoice"); + IncomingPayment incomingPayment = client.createTestModePayment(node.getId(), invoice.getData().getEncodedPaymentRequest()); + assertNotNull(incomingPayment); + while (incomingPayment.getStatus() == TransactionStatus.PENDING) { + Thread.sleep(500); + incomingPayment = client.executeQuery(IncomingPayment.getIncomingPaymentQuery(incomingPayment.getId())); + System.out.println("Payment status: " + incomingPayment.getStatus()); + } + assertEquals(incomingPayment.getStatus(), TransactionStatus.SUCCESS); + } + + private LightsparkNode getFirstNode() throws Exception { Account account = client.getCurrentAccount(); assertNotNull(account); - AccountToNodesConnection connection = client.executeQuery( - account.getNodesQuery(1) - ); + AccountToNodesConnection connection = client.executeQuery(account.getNodesQuery(1)); assertNotNull(connection); - return connection.getEntities().get(0).getId(); + return connection.getEntities().get(0); + } + + private String getNodeId() throws Exception { + return getFirstNode().getId(); } } diff --git a/lightspark-sdk/src/commonMain/kotlin/com/lightspark/sdk/crypto/PasswordRecoverySigningKeyLoader.kt b/lightspark-sdk/src/commonMain/kotlin/com/lightspark/sdk/crypto/PasswordRecoverySigningKeyLoader.kt index 07bbff3b..d0891885 100644 --- a/lightspark-sdk/src/commonMain/kotlin/com/lightspark/sdk/crypto/PasswordRecoverySigningKeyLoader.kt +++ b/lightspark-sdk/src/commonMain/kotlin/com/lightspark/sdk/crypto/PasswordRecoverySigningKeyLoader.kt @@ -14,7 +14,7 @@ import kotlinx.serialization.json.jsonObject import kotlinx.serialization.json.jsonPrimitive import kotlinx.serialization.json.put -class PasswordRecoverySigningKeyLoader( +class PasswordRecoverySigningKeyLoader @JvmOverloads constructor( private val nodeId: String, private val nodePassword: String, private val keyDecryptor: SigningKeyDecryptor = SigningKeyDecryptor(),