Skip to content

Commit

Permalink
Merge release/lightspark-sdk-v0.14.0 into main (#183)
Browse files Browse the repository at this point in the history
* lnurlp fallback

* annotation and tests

* fallback sender side

* fix serialization

* unused import

* comments

* fixes

* Try to fix docs publish job my running on main

* [gha] Use environment for doc-publish

* Fix sender vasp utxo callback

* Switch to a full 64bit nonce

* Use json for parsing in demo vasp (#163)

* Update the demo VASP's SDK version to 0.8.0

* Update the demo VASP's SDK version to 0.8.1

* Accept gzip responses, compress request with deflate (#172)

Install `ContentEncoding` plugin to handle gzip responses. Compress
requests larger than 1K using `Deflater`. Change
`addSigningDataIfNeeded()` to return serialised bytes so that we only
serialise once.

* Regenerate SDK (#173)

* Regenerate SDK

* no op vls

* Update macos environment version (#175)

* Uma data visibility (#174)

* fix typo (#176)

* encode hex (#177)

* Bump lightspark-sdk to version 0.14.0

* Load signing key

* Revert "Merge release/lightspark-sdk-v0.14.0 into develop" (#181)

* Bump lightspark-sdk to version 0.14.0

---------

Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
Co-authored-by: shreyav <[email protected]>
Co-authored-by: Jeremy Klein <[email protected]>
Co-authored-by: Michael Gorven <[email protected]>
Co-authored-by: Michael Gorven <[email protected]>
Co-authored-by: runner <[email protected]>
Co-authored-by: runner <[email protected]>
  • Loading branch information
7 people authored Jul 9, 2024
1 parent 64e7e5e commit 104c0c5
Show file tree
Hide file tree
Showing 159 changed files with 2,158 additions and 560 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/core-build.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ on:

jobs:
build:
runs-on: macos-11
runs-on: macos-12

steps:
- uses: actions/checkout@v3
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/core-publish.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ on:
jobs:
publish-core-sdk:
if: ${{ startsWith(github.event.release.tag_name, 'core-') }}
runs-on: macos-11
runs-on: macos-12

steps:
- uses: actions/checkout@v3
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/crypto-build.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ jobs:
build:
# No point in running this build on a core cut because it will fail until the deploy is done.
if: github.event.base_ref == null || !startsWith(github.event.base_ref, 'release/core-')
runs-on: macos-11
runs-on: macos-12

steps:
- uses: actions/checkout@v3
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/crypto-publish.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ on:
jobs:
publish-crypto-sdk:
if: ${{ startsWith(github.event.release.tag_name, 'crypto-') }}
runs-on: macos-11
runs-on: macos-12

steps:
- uses: actions/checkout@v3
Expand Down
6 changes: 5 additions & 1 deletion .github/workflows/docs-publish.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
name: Publish SDK Docs

on:
workflow_dispatch: {}
workflow_run:
workflows:
[
Expand All @@ -15,13 +16,16 @@ on:
jobs:
publish-docs:
if: ${{ github.event.workflow_run.conclusion == 'success' }}
runs-on: macos-11
runs-on: macos-12
environment: "docs"
permissions:
id-token: write
contents: read

steps:
- uses: actions/checkout@v3
with:
ref: "main"
- name: Set up JDK 17
uses: actions/setup-java@v3
with:
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/lightspark-sdk-build.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ jobs:
build:
# No point in running this build on a core cut because it will fail until the deploy is done.
if: github.event.base_ref == null || !startsWith(github.event.base_ref, 'release/core-')
runs-on: macos-11
runs-on: macos-12

steps:
- uses: actions/checkout@v3
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/lightspark-sdk-publish.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ on:
jobs:
publish-lightspark-sdk:
if: ${{ startsWith(github.event.release.tag_name, 'lightspark-sdk-') }}
runs-on: macos-11
runs-on: macos-12

steps:
- uses: actions/checkout@v3
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/release-branch-cut.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ on:

jobs:
bump-versions:
runs-on: macos-11
runs-on: macos-12
permissions:
contents: write
pull-requests: write
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/wallet-build.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ jobs:
build:
# No point in running this build on a core cut because it will fail until the deploy is done.
if: github.event.base_ref == null || !startsWith(github.event.base_ref, 'release/core-')
runs-on: macos-11
runs-on: macos-12

steps:
- uses: actions/checkout@v3
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/wallet-publish.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ on:
jobs:
publish-wallet-sdk:
if: ${{ startsWith(github.event.release.tag_name, 'wallet-sdk-') }}
runs-on: macos-11
runs-on: macos-12

steps:
- uses: actions/checkout@v3
Expand Down
1 change: 1 addition & 0 deletions core/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ kotlin {
api(libs.kotlinx.datetime)
implementation(libs.kotlinx.coroutines.core)
implementation(libs.ktor.client.core)
implementation(libs.ktor.client.encoding)
// implementation(project(":crypto"))
implementation(libs.lightspark.crypto)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@ package com.lightspark.sdk.core.crypto

import java.security.SecureRandom

internal actual fun nextInt(): Int {
return SecureRandom().nextInt()
internal actual fun nextLong(): Long {
return SecureRandom().nextLong()
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@

package com.lightspark.sdk.core.crypto

internal expect fun nextInt(): Int
internal expect fun nextLong(): Long
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,10 @@ import com.lightspark.sdk.core.LightsparkException
import com.lightspark.sdk.core.auth.AuthProvider
import com.lightspark.sdk.core.crypto.MissingKeyException
import com.lightspark.sdk.core.crypto.NodeKeyCache
import com.lightspark.sdk.core.crypto.nextInt
import com.lightspark.sdk.core.crypto.nextLong
import com.lightspark.sdk.core.util.getPlatform
import io.ktor.client.HttpClient
import io.ktor.client.plugins.compression.ContentEncoding
import io.ktor.client.plugins.websocket.WebSockets
import io.ktor.client.request.headers
import io.ktor.client.request.post
Expand All @@ -21,6 +22,7 @@ import kotlin.collections.component1
import kotlin.collections.component2
import kotlin.collections.set
import kotlin.coroutines.cancellation.CancellationException
import kotlin.math.absoluteValue
import kotlin.time.Duration.Companion.hours
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.map
Expand All @@ -34,6 +36,7 @@ import kotlinx.serialization.json.buildJsonObject
import kotlinx.serialization.json.jsonArray
import kotlinx.serialization.json.jsonObject
import kotlinx.serialization.json.jsonPrimitive
import java.util.zip.Deflater

private const val DEFAULT_BASE_URL = "api.lightspark.com"

Expand All @@ -45,6 +48,9 @@ class Requester constructor(
private val baseUrl: String = DEFAULT_BASE_URL,
) {
private val httpClient = HttpClient {
install(ContentEncoding) {
gzip() // Switch to deflate() when https://youtrack.jetbrains.com/issue/KTOR-6999 is fixed
}
install(WebSockets)
}
private val userAgent =
Expand Down Expand Up @@ -87,15 +93,15 @@ class Requester constructor(
)
}
val operation = operationMatch.groupValues[2]
var bodyData = buildJsonObject {
var payload = buildJsonObject {
put("query", JsonPrimitive(queryPayload))
variables?.let { put("variables", it) }
put("operationName", JsonPrimitive(operation))
}
var headers = defaultHeaders + authProvider.getCredentialHeaders()
var headers = defaultHeaders + authProvider.getCredentialHeaders() + mapOf("X-GraphQL-Operation" to operation)
val signedBodyAndHeaders = try {
addSigningDataIfNeeded(
bodyData,
payload,
headers,
signingNodeId,
)
Expand All @@ -106,11 +112,25 @@ class Requester constructor(
e,
)
}
bodyData = signedBodyAndHeaders.first
var body = signedBodyAndHeaders.first
headers = signedBodyAndHeaders.second

if (body.size > 1024) {
val output = ByteArray(body.size)
val deflater = Deflater(Deflater.BEST_SPEED)
deflater.setInput(body)
deflater.finish()
val length = deflater.deflate(output)
deflater.end()
body = output.copyOfRange(0, length)

headers = headers.toMutableMap().apply {
this["Content-Encoding"] = "deflate"
}
}

val response = httpClient.post("https://$baseUrl/$schemaEndpoint") {
setBody(bodyData.toString())
setBody(body)
headers {
headers.forEach { (key, value) ->
append(key, value)
Expand Down Expand Up @@ -193,27 +213,27 @@ class Requester constructor(
bodyData: JsonObject,
headers: Map<String, String>,
signingNodeId: String?,
): Pair<JsonObject, Map<String, String>> {
): Pair<ByteArray, Map<String, String>> {
if (signingNodeId == null) {
return bodyData to headers
return bodyData.toString().encodeToByteArray() to headers
}
if (!nodeKeyCache.contains(signingNodeId)) {
throw MissingKeyException(signingNodeId)
}

val newBodyData = bodyData.toMutableMap().apply {
// Note: The nonce is a 64-bit unsigned integer, but the Kotlin random number generator wants to
// spit out a signed int, which the backend can't decode.
put("nonce", JsonPrimitive(nextInt().toUInt().toLong()))
// spit out a signed int, which the backend can't decode, so we take the absolute value.
put("nonce", JsonPrimitive(nextLong().absoluteValue))
put("expires_at", JsonPrimitive(anHourFromNowISOString()))
}.let { JsonObject(it) }
val newBodyString = Json.encodeToString(newBodyData)
val signature = nodeKeyCache[signingNodeId].sign(newBodyString.encodeToByteArray())
val newBodyString = newBodyData.toString().encodeToByteArray()
val signature = nodeKeyCache[signingNodeId].sign(newBodyString)
val newHeaders = headers.toMutableMap().apply {
this["X-LIGHTSPARK-SIGNING"] =
"{\"v\":1,\"signature\":\"${signature.base64Encoded}\"}"
}
return newBodyData to newHeaders
return newBodyString to newHeaders
}

private fun anHourFromNowISOString() = Clock.System.now().plus(1.hours).toString()
Expand Down
3 changes: 2 additions & 1 deletion gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ ktor-client-serialization = { module = "io.ktor:ktor-client-serialization", vers
ktor-client-okhttp = { module = "io.ktor:ktor-client-okhttp", version.ref = "ktor" }
ktor-client-darwin = { module = "io.ktor:ktor-client-darwin", version.ref = "ktor" }
ktor-client-websockets = { module = "io.ktor:ktor-client-websockets", version.ref = "ktor" }
ktor-client-encoding = { module = "io.ktor:ktor-client-encoding", version.ref = "ktor" }

kotest-assertions = { module = "io.kotest:kotest-assertions-core", version.ref = "kotest" }

Expand Down Expand Up @@ -140,4 +141,4 @@ ktlint = { id = "org.jlleitschuh.gradle.ktlint", version.ref = "ktlint" }
mavenPublish = { id = "com.vanniktech.maven.publish", version.ref = "mavenPublish" }
dokka = { id = "org.jetbrains.dokka", version.ref = "dokka" }
gradleS3 = { id = "com.mgd.core.gradle.s3", version.ref = "gradleS3" }
downloadFile = { id = "de.undercouch.download", version.ref = "downloadFile" }
downloadFile = { id = "de.undercouch.download", version.ref = "downloadFile" }
Loading

0 comments on commit 104c0c5

Please sign in to comment.