Skip to content

Commit

Permalink
Update nft.kt
Browse files Browse the repository at this point in the history
  • Loading branch information
companyAbcDev authored Sep 4, 2023
1 parent 493752f commit bea90b9
Showing 1 changed file with 76 additions and 3 deletions.
79 changes: 76 additions & 3 deletions app/src/main/java/io/kthulu/sdk/nft.kt
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,7 @@ import org.web3j.abi.datatypes.generated.Bytes4
import org.web3j.abi.datatypes.generated.Uint256
import org.web3j.abi.datatypes.generated.Uint32
import org.web3j.abi.datatypes.generated.Uint8
import org.web3j.crypto.Credentials
import org.web3j.crypto.RawTransaction
import org.web3j.crypto.TransactionEncoder
import org.web3j.crypto.*
import org.web3j.protocol.Web3j
import org.web3j.protocol.core.DefaultBlockParameterName
import org.web3j.protocol.core.methods.request.Transaction
Expand Down Expand Up @@ -3316,3 +3314,78 @@ suspend fun chkNFTHolder(
}
}

suspend fun signMessage(
network: String,
fromAddress: String,
collection_id: String,
token_id: String,
prefix: String
): String {
val getAddressInfo = getAccountInfoAsync(fromAddress)
val privateKey = runCatching {
getAddressInfo.getJSONArray("value")
.getJSONObject(0)
.getString("private")
}.getOrElse {
// handle error here
println("Error while fetching the private key: ${it.message}")
null
}
var message = ""
val credentials = Credentials.create(privateKey)
val str = prefix+network+fromAddress+collection_id+token_id
val hash = Hash.sha3(Numeric.toHexStringNoPrefix(str.toByteArray()))
// println("Hash$hash")
if(network == "cypress") {
message = """
\x19Klaytn Signed Message:
${hash.length}$hash
""".trimIndent()
} else {
message = """
\x19Ethereum Signed Message:
${hash.length}$hash
""".trimIndent()
}
val data = message.toByteArray()
val signature = Sign.signPrefixedMessage(data, credentials.ecKeyPair)
val r = Numeric.toHexStringNoPrefix(signature.r)
val s = Numeric.toHexStringNoPrefix(signature.s)
val v = Numeric.toHexStringNoPrefix(signature.v)
return r + s + v
}

suspend fun getSignerAddressFromSignature(
network: String,
signature: String,
fromAddress: String,
collection_id: String,
token_id: String,
prefix: String
): String {
var message = ""
val str = prefix+network+fromAddress+collection_id+token_id
val hash = Hash.sha3(Numeric.toHexStringNoPrefix(str.toByteArray()))
// println("Hash$hash")

if(network == "cypress") {
message = """
\x19Klaytn Signed Message:
${hash.length}$hash
""".trimIndent()
} else {
message = """
\x19Ethereum Signed Message:
${hash.length}$hash
""".trimIndent()
}
val r = Numeric.hexStringToByteArray(signature.substring(0, 64))
val s = Numeric.hexStringToByteArray(signature.substring(64, 128))
val v = BigInteger(signature.substring(128), 16).toByte()

val signData = Sign.SignatureData(v, r, s)
val pubKey = Sign.signedPrefixedMessageToKey(message.toByteArray(Charsets.UTF_8), signData)
return "0x" + Keys.getAddress(pubKey)
}


0 comments on commit bea90b9

Please sign in to comment.