-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathClientIntegrationTests.kt
More file actions
434 lines (385 loc) · 17.3 KB
/
ClientIntegrationTests.kt
File metadata and controls
434 lines (385 loc) · 17.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
package com.lightspark.sdk
import com.lightspark.sdk.auth.AccountApiTokenAuthProvider
import com.lightspark.sdk.core.requester.ServerEnvironment
import com.lightspark.sdk.core.util.getPlatform
import com.lightspark.sdk.crypto.PasswordRecoverySigningKeyLoader
import com.lightspark.sdk.model.Account
import com.lightspark.sdk.model.BitcoinNetwork
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.PaymentRequestStatus
import com.lightspark.sdk.model.Transaction
import com.lightspark.sdk.model.TransactionStatus
import com.lightspark.sdk.model.WithdrawalMode
import io.kotest.matchers.collections.shouldHaveSize
import io.kotest.matchers.collections.shouldNotBeEmpty
import io.kotest.matchers.nulls.shouldNotBeNull
import io.kotest.matchers.shouldBe
import io.kotest.matchers.shouldNotBe
import kotlin.test.Test
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.delay
import kotlinx.coroutines.test.runTest
import kotlinx.datetime.Clock
import kotlinx.datetime.DateTimePeriod
import kotlinx.datetime.LocalDateTime
import kotlinx.datetime.TimeZone
import kotlinx.datetime.minus
import org.mockito.Mockito.spy
import org.mockito.Mockito.`when`
import java.security.MessageDigest
import kotlin.random.Random
@OptIn(ExperimentalCoroutinesApi::class)
class ClientIntegrationTests {
// Read the token ID and secrets from the environment.
private val API_TOKEN_CLIENT_ID = getPlatform().getEnv("LIGHTSPARK_API_TOKEN_CLIENT_ID")!!
private val API_TOKEN_CLIENT_SECRET = getPlatform().getEnv("LIGHTSPARK_API_TOKEN_CLIENT_SECRET")!!
private val NODE_PASSWORD = getPlatform().getEnv("LIGHTSPARK_TEST_NODE_PASSWORD") ?: "1234!@#$"
private val config = ClientConfig()
.setServerUrl(ServerEnvironment.DEV.graphQLUrl)
.setAuthProvider(
AccountApiTokenAuthProvider(
API_TOKEN_CLIENT_ID,
API_TOKEN_CLIENT_SECRET,
),
)
.setDefaultBitcoinNetwork(BitcoinNetwork.REGTEST)
private val client = spy(LightsparkCoroutinesClient(config))
@Test
fun `get full account dashboard`() = runTest {
val dashboard = client.getFullAccountDashboard()
dashboard.shouldNotBeNull()
val nodeConnection = dashboard.nodeConnection
nodeConnection.shouldNotBeNull()
nodeConnection.nodes.shouldNotBeEmpty()
}
@Test
fun testGetBitcoinFeeEstimates() = runTest {
val estimates = client.getBitcoinFeeEstimate()
estimates.shouldNotBeNull()
estimates.feeFast.shouldNotBeNull()
estimates.feeMin.shouldNotBeNull()
println("L1 fee estimate: $estimates")
}
@Test
fun `get withdrawal fee estimate`() = runTest {
val estimate = client.getWithdrawalFeeEstimate(getNodeId(), amountSats = 1000, WithdrawalMode.WALLET_ONLY)
estimate.shouldNotBeNull()
println("Withdrawal fee estimate: $estimate")
}
@Test
fun `list current account nodes`() = runTest {
val account = getCurrentAccount()
val nodes = account.getNodesQuery().execute(client)
nodes.shouldNotBeNull()
nodes.entities.shouldNotBeEmpty()
println("Got ${nodes.entities.size} nodes")
println("Node IDs: ${nodes.entities.map { it.id }}")
}
@Test
fun `create and delete api tokens`() = runTest {
val account = getCurrentAccount()
val tokens = account.getApiTokensQuery().execute(client)
tokens.shouldNotBeNull()
val token = client.createApiToken("test token", transact = false, testMode = true)
token.shouldNotBeNull()
val newTokens = account.getApiTokensQuery().execute(client)
newTokens.shouldNotBeNull()
newTokens.entities.shouldHaveSize(tokens.entities.size + 1)
client.deleteApiToken(token.apiToken.id)
val finalTokens = account.getApiTokensQuery().execute(client)
finalTokens.shouldNotBeNull()
finalTokens.entities.shouldNotBeEmpty()
finalTokens.entities.shouldHaveSize(tokens.entities.size)
}
@Test
fun `get account conductivity`() = runTest {
val account = getCurrentAccount()
val conductivity = account.getConductivityQuery().execute(client)
conductivity.shouldNotBeNull()
println("Conductivity: $conductivity")
}
@Test
fun `get balances`() = runTest {
val account = getCurrentAccount()
val localBalance = account.getLocalBalanceQuery().execute(client)
val remoteBalance = account.getRemoteBalanceQuery().execute(client)
localBalance.shouldNotBeNull()
remoteBalance.shouldNotBeNull()
println("Local balance: $localBalance")
println("Remote balance: $remoteBalance")
}
@Test
fun `get transactions`() = runTest {
val account = getCurrentAccount()
val pageSize = 10
var iterations = 0
var hasNext = true
var after: String? = null
while (hasNext && iterations < 30) {
iterations++
val transactions = account.getTransactionsQuery(
first = pageSize,
after = after,
).execute(client)
transactions.shouldNotBeNull()
transactions.entities.shouldNotBeEmpty()
println("Got ${transactions.entities.size} transactions")
if (transactions.pageInfo.hasNextPage == true) {
after = transactions.pageInfo.endCursor
hasNext = true
println(" and we have another page!")
} else {
hasNext = false
println(" and we're done!")
}
}
}
@Test
fun `get transactions in the last day`() = runTest {
val account = getCurrentAccount()
val transactions = account.getTransactionsQuery(
afterDate = Clock.System.now().minus(DateTimePeriod(days = 1), TimeZone.UTC),
).execute(client)
transactions.shouldNotBeNull()
println("Got ${transactions.entities.size} transactions in the last day")
}
@Test
fun `get most recent transaction details`() = runTest {
val account = getCurrentAccount()
val transactions = account.getTransactionsQuery(
first = 1,
).execute(client)
transactions.shouldNotBeNull()
transactions.entities.shouldNotBeEmpty()
val transaction = transactions.entities.first()
val details = Transaction.getTransactionQuery(transaction.id).execute(client)
details.shouldNotBeNull()
println("Transaction details: $details")
}
@Test
fun `create and decode a payment request`() = runTest {
val paymentRequest = client.createInvoice(
getNodeId(),
42000,
"Pizza!",
)
println("encoded invoice: ${paymentRequest.data.encodedPaymentRequest}")
val decoded = client.decodeInvoice(paymentRequest.data.encodedPaymentRequest)
decoded.shouldNotBeNull()
println("decoded invoice: $decoded")
}
@Test
fun `create an LNURL invoice`() = runTest {
val node = getFirstOskNode()
val metadata = "[[\\\"text/plain\\\",\\\"Pay to domain.org user ktfan98\\\"]," +
"[\\\"text/identifier\\\",\\\"ktfan98@domain.org\\\"]]"
val paymentRequest = client.createLnurlInvoice(node.id, 1000, metadata)
println("encoded invoice: ${paymentRequest.data.encodedPaymentRequest}")
}
@Test
fun `create and pay an UMA invoice`() = runTest {
val node = getFirstOskNode()
val metadata = "[[\\\"text/plain\\\",\\\"Pay to domain.org user ktfan98\\\"]," +
"[\\\"text/identifier\\\",\\\"ktfan98@domain.org\\\"]]"
val paymentRequest = client.createUmaInvoice(
node.id,
1000,
metadata,
signingPrivateKey = Random.nextBytes(5),
receiverIdentifier = "ktfan98@domain.org",
)
println("encoded invoice: ${paymentRequest.data.encodedPaymentRequest}")
client.loadNodeSigningKey(node.id, PasswordRecoverySigningKeyLoader(node.id, NODE_PASSWORD))
val payment = client.payUmaInvoice(
node.id,
paymentRequest.data.encodedPaymentRequest,
60,
signingPrivateKey = Random.nextBytes(5),
senderIdentifier = "sender@domain.org",
)
payment.shouldNotBeNull()
println("Payment: $payment")
}
@Test
fun `create and cancel an invoice`() = runTest {
val node = getFirstOskNode()
val invoice = client.createInvoice(node.id, 1000)
println("encoded invoice: $invoice.data.encodedPaymentRequest}")
val cancelledInvoice = client.cancelInvoice(invoice.id)
cancelledInvoice.shouldNotBeNull()
println("cancelled invoice: $cancelledInvoice")
}
@Test
fun `send a payment for an invoice`() = runTest {
val node = getFirstOskNode()
client.loadNodeSigningKey(node.id, PasswordRecoverySigningKeyLoader(node.id, NODE_PASSWORD))
// Just paying a pre-existing AMP invoice.
val ampInvoice =
"lnbcrt1pjr8xwypp5xqj2jfpkz095s8zu57ktsq8vt8yazwcmqpcke9pvl67ne9cpdr0qdqj2a5xzumnwd6hqurswqcqzpgxq9z0rgqs" +
"p55hfn0caa5sexea8u979cckkmwelw6h3zpwel5l8tn8s0elgwajss9q8pqqqssqefmmw79tknhl5xhnh7yfepzypxknwr9r4ya7" +
"ueqa6vz20axvys8se986hwj6gppeyzst44hm4yl04c4dqjjpqgtt0df254q087sjtfsq35yagj"
val payment = client.payInvoice(node.id, ampInvoice, maxFeesMsats = 100_000, amountMsats = 10_000)
payment.shouldNotBeNull()
println("Payment: $payment")
}
@Test
fun `get node channels`() = runTest {
val node = getFirstOskNode()
val channels = node.getChannelsQuery().execute(client)
channels.shouldNotBeNull()
channels.entities.shouldNotBeEmpty()
println("Got ${channels.entities.size} channels")
println("Channel IDs: ${channels.entities.map { it.id }}")
}
@Test
fun `test paying a test mode invoice`() = runTest {
val node = getFirstOskNode()
client.loadNodeSigningKey(node.id, PasswordRecoverySigningKeyLoader(node.id, NODE_PASSWORD))
val invoice = client.createTestModeInvoice(node.id, 100_000, "test invoice")
var outgoingPayment: OutgoingPayment? = client.payInvoice(node.id, invoice, maxFeesMsats = 100_000)
outgoingPayment.shouldNotBeNull()
while (outgoingPayment?.status == TransactionStatus.PENDING) {
delay(500)
outgoingPayment = OutgoingPayment.getOutgoingPaymentQuery(outgoingPayment.id).execute(client)
println("Payment status: ${outgoingPayment?.status}")
}
outgoingPayment?.status.shouldBe(TransactionStatus.SUCCESS)
}
@Test
fun `test creating a test mode payment`() = runTest {
val node = getFirstOskNode()
client.loadNodeSigningKey(node.id, PasswordRecoverySigningKeyLoader(node.id, NODE_PASSWORD))
val invoice = client.createInvoice(node.id, 100_000, "test invoice")
var payment: IncomingPayment? = client.createTestModePayment(node.id, invoice.data.encodedPaymentRequest)
payment.shouldNotBeNull()
while (payment?.status == TransactionStatus.PENDING) {
delay(500)
payment = IncomingPayment.getIncomingPaymentQuery(payment.id).execute(client)
println("Payment status: ${payment?.status}")
}
payment?.status.shouldBe(TransactionStatus.SUCCESS)
}
@Test
fun `test getIncomingPaymentsForPaymentHash`() = runTest {
val node = getFirstOskNode()
client.loadNodeSigningKey(node.id, PasswordRecoverySigningKeyLoader(node.id, NODE_PASSWORD))
val invoice = client.createInvoice(node.id, 100_000, "test invoice")
var payment: IncomingPayment? = client.createTestModePayment(node.id, invoice.data.encodedPaymentRequest)
payment.shouldNotBeNull()
while (payment?.status == TransactionStatus.PENDING) {
delay(500)
payment = IncomingPayment.getIncomingPaymentQuery(payment.id).execute(client)
println("Payment status: ${payment?.status}")
}
val payments = client.getIncomingPaymentsForPaymentHash(invoice.data.paymentHash)
payments.shouldNotBeNull()
payments.shouldHaveSize(1)
payments[0].id.shouldBe(payment?.id)
}
@Test
fun `test getOutgoingPaymentsForPaymentHash`() = runTest {
val node = getFirstOskNode()
client.loadNodeSigningKey(node.id, PasswordRecoverySigningKeyLoader(node.id, NODE_PASSWORD))
val invoice = client.createTestModeInvoice(node.id, 100_000, "test invoice")
var outgoingPayment: OutgoingPayment? = client.payInvoice(node.id, invoice, maxFeesMsats = 100_000)
outgoingPayment.shouldNotBeNull()
while (outgoingPayment?.status == TransactionStatus.PENDING) {
delay(500)
outgoingPayment = OutgoingPayment.getOutgoingPaymentQuery(outgoingPayment.id).execute(client)
println("Payment status: ${outgoingPayment?.status}")
}
outgoingPayment?.status.shouldBe(TransactionStatus.SUCCESS)
val payments = client.getOutgoingPaymentForPaymentHash(
client.decodeInvoice(invoice).paymentHash
)
payments.shouldNotBeNull()
payments.shouldHaveSize(1)
payments[0].id.shouldBe(outgoingPayment?.id!!)
}
@Test
fun `test getInvoiceForPaymentHash`() = runTest {
val node = getFirstOskNode()
client.loadNodeSigningKey(node.id, PasswordRecoverySigningKeyLoader(node.id, NODE_PASSWORD))
val testInvoice = client.createInvoice(node.id, 100_000, "test invoice")
var payment: IncomingPayment? = client.createTestModePayment(node.id, testInvoice.data.encodedPaymentRequest)
payment.shouldNotBeNull()
while (payment?.status == TransactionStatus.PENDING) {
delay(500)
payment = IncomingPayment.getIncomingPaymentQuery(payment.id).execute(client)
println("Payment status: ${payment?.status}")
}
val invoice = client.getInvoiceForPaymentHash(testInvoice.data.paymentHash)
invoice.shouldNotBeNull()
invoice.id.shouldBe(testInvoice.id)
}
@Test
fun `test uma identifier hashing`() = runTest {
val privKeyBytes = "xyz".toByteArray()
`when`(client.getUtcDateTime()).thenReturn(LocalDateTime(2021, 1, 1, 0, 0, 0))
val hashedUma = client.hashUmaIdentifier("user@domain.com", privKeyBytes)
val hashedUmaSameMonth = client.hashUmaIdentifier("user@domain.com", privKeyBytes)
hashedUmaSameMonth.shouldBe(hashedUma)
println(hashedUma)
`when`(client.getUtcDateTime()).thenReturn(LocalDateTime(2021, 2, 1, 0, 0, 0))
val hashedUmaNewMonth = client.hashUmaIdentifier("user@domain.com", privKeyBytes)
hashedUmaNewMonth.shouldNotBe(hashedUma)
println(hashedUmaNewMonth)
}
@Test
fun `create and settle a hold invoice`() = runTest {
val nodeA = getFirstOskNode()
val nodeB = getSecondOskNode()
val preimage = generateRandomHexString()
val paymentHash = sha256Hash(preimage)
var invoice = client.createInvoice(nodeA.id, 1000, paymentHash = paymentHash)
println("encoded invoice: $invoice.data.encodedPaymentRequest}")
client.loadNodeSigningKey(nodeB.id, PasswordRecoverySigningKeyLoader(nodeB.id, NODE_PASSWORD))
val outgoingPayment = client.payInvoice(nodeB.id, invoice.data.encodedPaymentRequest, maxFeesMsats = 100_000)
outgoingPayment.shouldNotBeNull()
println("outgoing payment: $outgoingPayment")
// client.releasePaymentPreimage(invoice.id, preimage)
while (invoice.status == PaymentRequestStatus.OPEN) {
delay(500)
invoice = Invoice.getInvoiceQuery(invoice.id).execute(client)!!
invoice.shouldNotBeNull()
println("Invoice status: ${invoice.status}")
}
}
// TODO: Add tests for withdrawals and deposits.
private suspend fun getFirstOskNode(): LightsparkNode {
val account = getCurrentAccount()
val nodes = account.getNodesQuery().execute(client)
nodes.shouldNotBeNull()
nodes.entities.shouldNotBeEmpty()
return nodes.entities.first { it.id.contains("OSK") }
}
private suspend fun getSecondOskNode(): LightsparkNode {
val account = getCurrentAccount()
val nodes = account.getNodesQuery().execute(client)
nodes.shouldNotBeNull()
nodes.entities.shouldNotBeEmpty()
return nodes.entities.get(1)
}
private suspend fun getNodeId(): String {
return getFirstOskNode().id
}
private suspend fun getCurrentAccount(): Account {
val account = client.getCurrentAccount()
account.shouldNotBeNull()
return account
}
private fun sha256Hash(input: String): String {
val bytes = input.toByteArray()
val digest = MessageDigest.getInstance("SHA-256").digest(bytes)
return digest.joinToString("") { "%02x".format(it) }
}
private fun generateRandomHexString(): String {
val allowedChars = ('a'..'f') + ('0'..'9')
return (1..64)
.map { allowedChars.random(Random.Default) }
.joinToString("")
}
}