From cd42c69be4d079b5f752668c9ea5f48d4d474c58 Mon Sep 17 00:00:00 2001 From: SuperBatata Date: Thu, 31 Aug 2023 11:15:26 +0100 Subject: [PATCH 1/4] fix: support int for evm metadata's trait values --- .../id/walt/nftkit/opa/DynamicPolicy.kt | 2 +- .../id/walt/nftkit/services/NftService.kt | 8 +++-- .../nftkit/services/VerificationService.kt | 33 +++++++++---------- 3 files changed, 22 insertions(+), 21 deletions(-) diff --git a/src/main/kotlin/id/walt/nftkit/opa/DynamicPolicy.kt b/src/main/kotlin/id/walt/nftkit/opa/DynamicPolicy.kt index 2d3d8b01..5aa688b3 100644 --- a/src/main/kotlin/id/walt/nftkit/opa/DynamicPolicy.kt +++ b/src/main/kotlin/id/walt/nftkit/opa/DynamicPolicy.kt @@ -22,7 +22,7 @@ object DynamicPolicy { if(nftMetadata.evmNftMetadata != null){ data.put("name", nftMetadata.evmNftMetadata.name) data.put("description", nftMetadata.evmNftMetadata.description) - nftMetadata.evmNftMetadata.attributes?.forEach { data.put(it.trait_type, it.value) } + nftMetadata.evmNftMetadata.attributes?.forEach { data.put(it.trait_type, it.value.toString()) } }else if(nftMetadata.tezosNftMetadata != null){ data.put("name", nftMetadata.tezosNftMetadata.name) data.put("description", nftMetadata.tezosNftMetadata.description) diff --git a/src/main/kotlin/id/walt/nftkit/services/NftService.kt b/src/main/kotlin/id/walt/nftkit/services/NftService.kt index efc1522e..30d7f26d 100644 --- a/src/main/kotlin/id/walt/nftkit/services/NftService.kt +++ b/src/main/kotlin/id/walt/nftkit/services/NftService.kt @@ -49,7 +49,7 @@ data class NftMetadata( @Serializable data class Attributes( val trait_type: String, - var value: String, + var value: JsonElement? = null, //val display_type: DisplayType? ) } @@ -488,7 +488,9 @@ object NftService { }else{ metadata.attributes?.filter { it.trait_type.equals(key, true) - }?.map { it.value= value } + }?.map { + it.value= JsonPrimitive(value) + } } val oldUri= getMetadatUri(chain, contractAddress, BigInteger(tokenId)) val metadataUri: MetadataUri = MetadataUriFactory.getMetadataUri(Common.getMetadataType(oldUri)) @@ -637,7 +639,7 @@ object NftService { var attributes: List?=null if(nft.get("attributes")?.metaInfo.equals("kotlinx.serialization.json.JsonArray.class")){ attributes= nft.get("attributes")?.jsonArray?.map { - NftMetadata.Attributes(it.jsonObject.get("trait_type")?.jsonPrimitive?.content ?: "", it.jsonObject.get("value")?.jsonPrimitive?.content ?: "") + NftMetadata.Attributes(it.jsonObject.get("trait_type")?.jsonPrimitive?.content ?: "", it.jsonObject.get("value") ?: JsonPrimitive("")) } } return NftMetadata( diff --git a/src/main/kotlin/id/walt/nftkit/services/VerificationService.kt b/src/main/kotlin/id/walt/nftkit/services/VerificationService.kt index 77ac6efe..c386c31c 100644 --- a/src/main/kotlin/id/walt/nftkit/services/VerificationService.kt +++ b/src/main/kotlin/id/walt/nftkit/services/VerificationService.kt @@ -120,14 +120,14 @@ object VerificationService { val ownership= NFTsEvmOwnershipVerification(EVMChain.valueOf(chain.toString()), contractAddress, account, BigInteger(tokenId)) if(ownership){ val metadata= NftService.getNftMetadata(EVMChain.valueOf(chain.toString()), contractAddress, BigInteger( tokenId)) - if(metadata!!.attributes?.filter { - (it.trait_type.equals(traitType) && it.value.equals( - traitValue, - true - )) || (traitValue == null && traitType.equals(it.trait_type)) - }!!.isNotEmpty()){ - return true - } + if(metadata!!.attributes?.filter { + (it.trait_type.equals(traitType) && it.value?.equals( + traitValue, + + ) == true) || (traitValue == null && traitType.equals(it.trait_type)) + }!!.isNotEmpty()){ + return true + } } return false; } @@ -151,10 +151,9 @@ object VerificationService { if(ownership){ val metadata= NftService.getNftMetadata(EVMChain.valueOf(chain.toString()), contractAddress, BigInteger( tokenId)) if(metadata!!.attributes?.filter { - (it.trait_type.equals(traitType) && it.value.equals( - traitValue, - true - )) || (traitValue == null && traitType.equals(it.trait_type)) + (it.trait_type.equals(traitType) && it.value?.equals( + traitValue + ) != false) || ((traitValue == null) && traitType.equals(it.trait_type)) }!!.isNotEmpty()){ return true } @@ -400,12 +399,12 @@ object VerificationService { }else if(compareStrings(propertyKey,"external_url")){ return compareStrings(propertyValue, metadata.external_url) }else { - if (metadata.attributes != null && metadata.attributes.filter { - (it.trait_type.equals(propertyKey) && it.value.equals( + if ((metadata.attributes != null) && metadata.attributes.filter { + (it.trait_type.equals(propertyKey) && it.value?.equals( propertyValue, - true - )) || (propertyValue == null && propertyKey.equals(it.trait_type)) - }.size > 0) { + + ) != false) || ((propertyValue == null) && propertyKey.equals(it.trait_type)) + }.isNotEmpty()) { return true } } From b08ad135bf75a81689b3d4b102b8bcf8fbc5206c Mon Sep 17 00:00:00 2001 From: SuperBatata Date: Thu, 31 Aug 2023 13:25:37 +0100 Subject: [PATCH 2/4] fix: support int for evm metadata's trait values --- .../id/walt/nftkit/services/NftService.kt | 25 ++++++++++++++++--- .../nftkit/services/VerificationService.kt | 20 +++++++-------- 2 files changed, 31 insertions(+), 14 deletions(-) diff --git a/src/main/kotlin/id/walt/nftkit/services/NftService.kt b/src/main/kotlin/id/walt/nftkit/services/NftService.kt index 30d7f26d..4dbf857a 100644 --- a/src/main/kotlin/id/walt/nftkit/services/NftService.kt +++ b/src/main/kotlin/id/walt/nftkit/services/NftService.kt @@ -21,6 +21,7 @@ import io.ktor.client.request.* import io.ktor.http.* import io.ktor.serialization.kotlinx.json.* import kotlinx.coroutines.runBlocking +import kotlinx.serialization.Contextual import kotlinx.serialization.Serializable import kotlinx.serialization.decodeFromString import kotlinx.serialization.json.* @@ -34,6 +35,13 @@ import org.web3j.protocol.core.methods.response.Log import org.web3j.protocol.core.methods.response.TransactionReceipt import java.math.BigInteger +@Serializable +sealed class Value { + + data class StringValue(val value: String) : Value() + + data class NumberValue(val value: Int) : Value() +} @Serializable data class NftMetadata( @@ -49,8 +57,9 @@ data class NftMetadata( @Serializable data class Attributes( val trait_type: String, - var value: JsonElement? = null, - //val display_type: DisplayType? + + var value: JsonPrimitive? = null + ) } @@ -639,7 +648,17 @@ object NftService { var attributes: List?=null if(nft.get("attributes")?.metaInfo.equals("kotlinx.serialization.json.JsonArray.class")){ attributes= nft.get("attributes")?.jsonArray?.map { - NftMetadata.Attributes(it.jsonObject.get("trait_type")?.jsonPrimitive?.content ?: "", it.jsonObject.get("value") ?: JsonPrimitive("")) + val trait_type= it.jsonObject.get("trait_type")?.jsonPrimitive?.content + + val value= it.jsonObject.get("value")?.jsonPrimitive?.content + + // verify is value is number + if(value?.toIntOrNull() != null){ + NftMetadata.Attributes(trait_type!!, JsonPrimitive(value.toInt())) + }else{ + NftMetadata.Attributes(trait_type!!, JsonPrimitive(value)) + } + } } return NftMetadata( diff --git a/src/main/kotlin/id/walt/nftkit/services/VerificationService.kt b/src/main/kotlin/id/walt/nftkit/services/VerificationService.kt index c386c31c..c21b5d52 100644 --- a/src/main/kotlin/id/walt/nftkit/services/VerificationService.kt +++ b/src/main/kotlin/id/walt/nftkit/services/VerificationService.kt @@ -120,14 +120,13 @@ object VerificationService { val ownership= NFTsEvmOwnershipVerification(EVMChain.valueOf(chain.toString()), contractAddress, account, BigInteger(tokenId)) if(ownership){ val metadata= NftService.getNftMetadata(EVMChain.valueOf(chain.toString()), contractAddress, BigInteger( tokenId)) - if(metadata!!.attributes?.filter { - (it.trait_type.equals(traitType) && it.value?.equals( - traitValue, - - ) == true) || (traitValue == null && traitType.equals(it.trait_type)) - }!!.isNotEmpty()){ - return true - } + if(metadata!!.attributes?.filter { + (it.trait_type.equals(traitType) && it.value?.equals( + traitValue + ) ?: true) || ((traitValue == null) && traitType.equals(it.trait_type)) + }!!.isNotEmpty()){ + return true + } } return false; } @@ -401,9 +400,8 @@ object VerificationService { }else { if ((metadata.attributes != null) && metadata.attributes.filter { (it.trait_type.equals(propertyKey) && it.value?.equals( - propertyValue, - - ) != false) || ((propertyValue == null) && propertyKey.equals(it.trait_type)) + propertyValue + ) != false) || ((propertyValue == null) && propertyKey.equals(it.trait_type)) }.isNotEmpty()) { return true } From da592afbff60a292bbf0baf860f75f19be5656c2 Mon Sep 17 00:00:00 2001 From: SuperBatata Date: Thu, 31 Aug 2023 14:43:32 +0100 Subject: [PATCH 3/4] fixed tests for ownership verification --- .../nftkit/blockchainReadingOperations/evm/Erc721Standard.kt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/test/kotlin/id/walt/nftkit/blockchainReadingOperations/evm/Erc721Standard.kt b/src/test/kotlin/id/walt/nftkit/blockchainReadingOperations/evm/Erc721Standard.kt index a8c071bf..639d675e 100644 --- a/src/test/kotlin/id/walt/nftkit/blockchainReadingOperations/evm/Erc721Standard.kt +++ b/src/test/kotlin/id/walt/nftkit/blockchainReadingOperations/evm/Erc721Standard.kt @@ -67,7 +67,7 @@ class Erc721Standard : StringSpec({ result.image shouldBe "string" result.external_url shouldBe "string" result.attributes?.get(0)!!.trait_type shouldBe "string" - result.attributes?.get(0)!!.value shouldBe "15/7/2022 10:30:07" + (result.attributes?.get(0)!!.value?.content ?: "") shouldBe "15/7/2022 10:30:07" } From f3753f728705047da957f40fe68629d4d73d3d57 Mon Sep 17 00:00:00 2001 From: SuperBatata Date: Thu, 31 Aug 2023 14:43:55 +0100 Subject: [PATCH 4/4] fixed ownership verification with traits for evm chains --- .../walt/nftkit/services/VerificationService.kt | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/src/main/kotlin/id/walt/nftkit/services/VerificationService.kt b/src/main/kotlin/id/walt/nftkit/services/VerificationService.kt index c21b5d52..ed14b404 100644 --- a/src/main/kotlin/id/walt/nftkit/services/VerificationService.kt +++ b/src/main/kotlin/id/walt/nftkit/services/VerificationService.kt @@ -12,6 +12,7 @@ import io.ktor.http.* import io.ktor.util.* import kotlinx.coroutines.runBlocking import kotlinx.serialization.Serializable +import kotlinx.serialization.json.JsonPrimitive import org.web3j.tx.exceptions.ContractCallException import java.math.BigInteger @@ -120,12 +121,14 @@ object VerificationService { val ownership= NFTsEvmOwnershipVerification(EVMChain.valueOf(chain.toString()), contractAddress, account, BigInteger(tokenId)) if(ownership){ val metadata= NftService.getNftMetadata(EVMChain.valueOf(chain.toString()), contractAddress, BigInteger( tokenId)) - if(metadata!!.attributes?.filter { - (it.trait_type.equals(traitType) && it.value?.equals( - traitValue - ) ?: true) || ((traitValue == null) && traitType.equals(it.trait_type)) - }!!.isNotEmpty()){ - return true + + metadata.attributes?.map { + println(it.value?.content) + println(traitValue) + if (it.trait_type == traitType && it.value?.content.equals(traitValue)) { + + return true + } } } return false;