Skip to content

Commit

Permalink
fix: various tweaks
Browse files Browse the repository at this point in the history
  • Loading branch information
AndrewWestberg committed Jan 3, 2025
1 parent b603097 commit 7790078
Show file tree
Hide file tree
Showing 9 changed files with 135 additions and 30 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ hs_err_pid*
#project files
.gradle
.idea
.kotlin

# Build files
build
Expand Down
1 change: 1 addition & 0 deletions buildSrc/src/main/kotlin/Dependencies.kt
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,7 @@ object Dependencies {
const val SECRETS_MANAGER = "software.amazon.awssdk:secretsmanager"
const val LAMBDA = "software.amazon.awssdk:lambda"
const val EC2 = "software.amazon.awssdk:ec2"
const val IMDS = "software.amazon.awssdk:imds"
}

object Zensum {
Expand Down
2 changes: 1 addition & 1 deletion buildSrc/src/main/kotlin/Versions.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ object Versions {
const val AWS = "2.29.16"
const val BOUNCY_CASTLE = "1.70"
const val CAFFEINE = "3.1.8"
const val CBOR = "0.2.2-NEWM"
const val CBOR = "0.4.1-NEWM"
const val CLOUDINARY = "1.39.0"
const val COROUTINES = "1.9.0"
const val EXPOSED = "0.56.0"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -422,7 +422,8 @@ class NewmChainService : NewmChainGrpcKt.NewmChainCoroutineImplBase() {
val calculateReferenceScriptsVersions: suspend (Set<Utxo>) -> Set<Int> = { utxos ->
utxos
.mapNotNull { utxo ->
val firstUtxo = ledgerRepository.queryUtxosByOutputRef(utxo.hash, utxo.ix.toInt()).firstOrNull()
val firstUtxo =
ledgerRepository.queryUtxosByOutputRef(utxo.hash, utxo.ix.toInt()).firstOrNull()
firstUtxo?.scriptRefVersion ?: firstUtxo?.scriptRef?.let { scriptRef ->
if (scriptRef.startsWith("010100")) {
3
Expand Down Expand Up @@ -698,6 +699,7 @@ class NewmChainService : NewmChainGrpcKt.NewmChainCoroutineImplBase() {
try {
return ledgerRepository.queryUtxoByNativeAsset(request.name, request.policy)?.let { utxo ->
utxo {
address = utxo.address
hash = utxo.hash
ix = utxo.ix
lovelace = utxo.lovelace.toString()
Expand Down
1 change: 1 addition & 0 deletions newm-server/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ dependencies {
implementation(Dependencies.Aws.LAMBDA)
implementation(Dependencies.Aws.CLOUDFRONT)
implementation(Dependencies.Aws.EC2)
implementation(Dependencies.Aws.IMDS)

implementation(Dependencies.Arweave.ARWEAVE4S)
implementation(Dependencies.Arweave.SCALA_JAVA8_COMPAT)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1986,9 +1986,9 @@ class EvearaDistributionRepositoryImpl(
}

// update the isrc from eveara
val getTrackResponse = getTracks(user, mutableSong.distributionTrackId!!)
val getTrackResponse = getTracks(user, mutableSong.distributionTrackId)
val stereoIsrc = getTrackResponse.trackData!!.first().stereoIsrc
require(stereoIsrc.length == 12) { "Invalid stereo isrc: $stereoIsrc" }
require(stereoIsrc.length == 12) { "Invalid stereo isrc: \"$stereoIsrc\"" }
if (mutableSong.isrc != stereoIsrc) {
// Save the isrc to the song
val isrcCountry = stereoIsrc.substring(0, 2)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@ package io.newm.server.logging
import io.ktor.server.application.Application
import io.ktor.server.application.log
import org.slf4j.MDC
import software.amazon.awssdk.regions.internal.util.EC2MetadataUtils
import software.amazon.awssdk.imds.Ec2MetadataClient

fun Application.initializeLogging() {
val instanceId = try {
EC2MetadataUtils.getInstanceId()
Ec2MetadataClient.create().use { ec2MetadataClient ->
ec2MetadataClient.get("/latest/meta-data/instance-id").asString()
}
} catch (e: Throwable) {
log.error("Failed to get instanceId from EC2MetadataUtils", e)
"instanceId-unknown"
Expand Down
11 changes: 10 additions & 1 deletion newm-shared/src/main/kotlin/io/newm/shared/ktx/StringExt.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package io.newm.shared.ktx

import at.favre.lib.crypto.bcrypt.BCrypt
import io.github.oshai.kotlinlogging.KotlinLogging
import java.net.URI
import java.net.URL
import java.net.URLConnection
Expand All @@ -10,6 +11,8 @@ import java.time.LocalDateTime
import java.time.format.DateTimeParseException
import java.util.UUID

private val log by lazy { KotlinLogging.logger {} }

/**
* Based on android.util.Patterns.EMAIL_ADDRESS
*/
Expand Down Expand Up @@ -51,7 +54,13 @@ private object DummyURLHandler : URLStreamHandler() {
*/
fun String.orNull(): String? = takeIf { isNotBlank() }?.trim()

fun String.toUUID(): UUID = UUID.fromString(this)
fun String.toUUID(): UUID =
try {
UUID.fromString(this)
} catch (e: Throwable) {
log.error { "Error converting string to UUID: \"$this\"" }
throw e
}

fun String.isValidName(): Boolean = !contains(INVALID_NAME_CHARS_REGEX)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,13 @@ import io.newm.kogmios.protocols.model.result.UtxoResultItem
import io.newm.txbuilder.TransactionBuilder
import io.newm.txbuilder.TransactionBuilder.Companion.transactionBuilder
import io.newm.txbuilder.ktx.toCborObject
import java.util.UUID
import kotlinx.coroutines.runBlocking
import kotlinx.serialization.json.Json
import kotlinx.serialization.json.jsonObject
import kotlinx.serialization.json.jsonPrimitive
import org.junit.jupiter.api.Disabled
import org.junit.jupiter.api.Test
import java.util.UUID

@Disabled("Disabled until we get demeter working again.")
class TransactionBuilderTest {
Expand Down Expand Up @@ -150,7 +150,8 @@ class TransactionBuilderTest {
fundsVkeyJsonElement.jsonObject["cborHex"]!!
.jsonPrimitive.content
.hexToByteArray()
val fundsVkeyBytes = (CborReader.createFromByteArray(fundsVkeyCbor).readDataItem() as CborByteString).byteArrayValue()[0]
val fundsVkeyBytes =
(CborReader.createFromByteArray(fundsVkeyCbor).readDataItem() as CborByteString).byteArrayValue()[0]

// get the skey for funds address
val fundsSkey = javaClass.getResource("/contract_chaining_preprod_test.skey")!!.readText()
Expand All @@ -159,7 +160,8 @@ class TransactionBuilderTest {
fundsSkeyJsonElement.jsonObject["cborHex"]!!
.jsonPrimitive.content
.hexToByteArray()
val fundsSkeyBytes = (CborReader.createFromByteArray(fundsSkeyCbor).readDataItem() as CborByteString).byteArrayValue()[0]
val fundsSkeyBytes =
(CborReader.createFromByteArray(fundsSkeyCbor).readDataItem() as CborByteString).byteArrayValue()[0]

val utxosResponse =
(client as StateQueryClient).utxo(
Expand Down Expand Up @@ -483,6 +485,54 @@ class TransactionBuilderTest {
}
}

@Test
fun `test non-inline datum`() =
runBlocking {
val datum = plutusData {
cborHex = "d8799f00d87980ff"
constr = 0
list = plutusDataList {
listItem.add(
plutusData {
int = 0
}
)
listItem.add(
plutusData {
constr = 0
list = plutusDataList { }
}
)
}
}

val datumHex = datum.toCborObject().toCborByteArray().toHexString()
assertThat(datumHex).isEqualTo("d8799f00d87980ff")

val datumWitnesses = CborArray.create(
listOf(datum.toCborObject()),
258,
)

// check to make sure we kept the indeterminate array value
assertThat(datumWitnesses.toCborByteArray().toHexString()).isEqualTo("d9010281d8799f00d87980ff")

// datum {
// cborHex: "d8799f00d87980ff"
// constr: 0
// list {
// list_item {
// int: 0
// }
// list_item {
// constr: 0
// list {
// }
// }
// }
// }
}

@Test
@Disabled("Disabled until fixing Unknown transaction input failure")
fun `test Mint NFT`() =
Expand Down Expand Up @@ -608,7 +658,8 @@ class TransactionBuilderTest {
add(
outputUtxo {
address = "addr_test1wpp50tptek3lv5tqst9609npw99cxmngawjfmx2zn4e7ajqvqrt3u"
lovelace = "1387820" // back to the contract. must match exactly what we started with
lovelace =
"1387820" // back to the contract. must match exactly what we started with
nativeAssets.add(
nativeAsset {
policy = "fd3a69817fe5b9ff39fb2fac2be2c7f2007746e827ee31868fe667cd"
Expand Down Expand Up @@ -1108,20 +1159,30 @@ class TransactionBuilderTest {
CborTextString.create(tokenName) to
CborMap.create(
mapOf(
CborTextString.create("album_title") to CborTextString.create("Indiclouds"),
CborTextString.create("album_title") to CborTextString.create(
"Indiclouds"
),
CborTextString.create("artists") to
CborArray.create(
listOf(
CborMap.create(
mapOf(
CborTextString.create("name") to CborTextString.create("CÜSI")
CborTextString.create("name") to CborTextString.create(
"CÜSI"
)
)
)
)
),
CborTextString.create("copyright") to CborTextString.create("℗ CÜSI"),
CborTextString.create("distributor") to CborTextString.create("https://newm.io"),
CborTextString.create("explicit") to CborTextString.create("true"),
CborTextString.create("copyright") to CborTextString.create(
"℗ CÜSI"
),
CborTextString.create("distributor") to CborTextString.create(
"https://newm.io"
),
CborTextString.create("explicit") to CborTextString.create(
"true"
),
CborTextString.create("files") to
CborArray.create(
listOf(
Expand Down Expand Up @@ -1170,7 +1231,9 @@ class TransactionBuilderTest {
"image"
) to CborTextString.create("ar://M5oVJOOoxlBlHCzlwHYHAhKwxrnd1jyrnsAWI7jw6XA"),
CborTextString.create("isrc") to CborTextString.create("QZ-NW7-23-46503"),
CborTextString.create("language") to CborTextString.create("en-US"),
CborTextString.create("language") to CborTextString.create(
"en-US"
),
CborTextString.create("links") to
CborMap.create(
mapOf(
Expand Down Expand Up @@ -1209,21 +1272,47 @@ class TransactionBuilderTest {
CborTextString.create(
"lyrics"
) to CborTextString.create("ar://TNHN0v2wTsUhNprgWvEHC_CZvGi5xgb_oU8MAWvqV0Y"),
CborTextString.create("mediaType") to CborTextString.create("image/webp"),
CborTextString.create("metadata_language") to CborTextString.create("en-US"),
CborTextString.create("mix_engineer") to CborTextString.create("Nathan Cusumano"),
CborTextString.create("mediaType") to CborTextString.create(
"image/webp"
),
CborTextString.create("metadata_language") to CborTextString.create(
"en-US"
),
CborTextString.create("mix_engineer") to CborTextString.create(
"Nathan Cusumano"
),
CborTextString.create("mood") to CborTextString.create("Inspiring"),
CborTextString.create("music_metadata_version") to CborInteger.create(1),
CborTextString.create("music_metadata_version") to CborInteger.create(
1
),
CborTextString.create("name") to CborTextString.create("CÜSI - Indiclouds"),
CborTextString.create("parental_advisory") to CborTextString.create("Explicit"),
CborTextString.create("producer") to CborTextString.create("Mchale Beats"),
CborTextString.create("recording_engineer") to CborTextString.create("Nathan Cusumano"),
CborTextString.create("release_date") to CborTextString.create("2022-03-17"),
CborTextString.create("release_type") to CborTextString.create("Single"),
CborTextString.create("series") to CborTextString.create("CÜSI WORLD"),
CborTextString.create("song_duration") to CborTextString.create("PT3M7S"),
CborTextString.create("song_title") to CborTextString.create("Indiclouds"),
CborTextString.create("track_number") to CborInteger.create(1),
CborTextString.create("parental_advisory") to CborTextString.create(
"Explicit"
),
CborTextString.create("producer") to CborTextString.create(
"Mchale Beats"
),
CborTextString.create("recording_engineer") to CborTextString.create(
"Nathan Cusumano"
),
CborTextString.create("release_date") to CborTextString.create(
"2022-03-17"
),
CborTextString.create("release_type") to CborTextString.create(
"Single"
),
CborTextString.create("series") to CborTextString.create(
"CÜSI WORLD"
),
CborTextString.create("song_duration") to CborTextString.create(
"PT3M7S"
),
CborTextString.create("song_title") to CborTextString.create(
"Indiclouds"
),
CborTextString.create("track_number") to CborInteger.create(
1
),
)
)
)
Expand Down

0 comments on commit 7790078

Please sign in to comment.