Skip to content

Commit

Permalink
Merge pull request #140 from walt-id/fix/evm-metadata
Browse files Browse the repository at this point in the history
Fix/evm metadata
  • Loading branch information
SuperBatata authored Aug 31, 2023
2 parents 7d46c94 + f3753f7 commit 93e6f43
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 23 deletions.
2 changes: 1 addition & 1 deletion src/main/kotlin/id/walt/nftkit/opa/DynamicPolicy.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
29 changes: 25 additions & 4 deletions src/main/kotlin/id/walt/nftkit/services/NftService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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.*
Expand All @@ -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(
Expand All @@ -49,8 +57,9 @@ data class NftMetadata(
@Serializable
data class Attributes(
val trait_type: String,
var value: String,
//val display_type: DisplayType?

var value: JsonPrimitive? = null

)
}

Expand Down Expand Up @@ -488,7 +497,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))
Expand Down Expand Up @@ -637,7 +648,17 @@ object NftService {
var attributes: List<NftMetadata.Attributes>?=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 ?: "")
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(
Expand Down
34 changes: 17 additions & 17 deletions src/main/kotlin/id/walt/nftkit/services/VerificationService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -120,13 +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;
Expand All @@ -151,10 +153,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
}
Expand Down Expand Up @@ -400,12 +401,11 @@ 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(
propertyValue,
true
)) || (propertyValue == null && propertyKey.equals(it.trait_type))
}.size > 0) {
if ((metadata.attributes != null) && metadata.attributes.filter {
(it.trait_type.equals(propertyKey) && it.value?.equals(
propertyValue
) != false) || ((propertyValue == null) && propertyKey.equals(it.trait_type))
}.isNotEmpty()) {
return true
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}


Expand Down

0 comments on commit 93e6f43

Please sign in to comment.