Created
March 8, 2023 19:06
-
-
Save 90K2/a43e419c19cce2b579001408fdf8b9ae to your computer and use it in GitHub Desktop.
ton proof kotlin validation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Test | |
fun `ton proof validation test`() { | |
val payload = """ | |
{ | |
"proof": { | |
"timestamp": 1678206305, | |
"domain": { | |
"lengthBytes": 10, | |
"value": "tonplay.io" | |
}, | |
"signature": "VJPpOKHfg79Z1We7SCnI+qwSShIm8uyFpUM5H5tnTxKzafsHrXkg2ivoouXeCSlgZoHj/y9utZh9vEh4oo5LAQ==", | |
"payload": "nmqQIzwMkZaOWonYxHXFtrXyziCbxOj9zlUfR7x5B1sQCUtG8Bk5zmMPkcbEzmCJ" | |
}, | |
"address": "EQBDMWTZnPVO5ccZIb6ttsVArQa1_Pwe47fBggvhG8OtgOf0" | |
} | |
""".trimIndent() | |
val d = ObjectMapper().readValue(payload, TonConnectRequestDTO::class.java) | |
println(d) | |
val pubKey = runBlocking { | |
liteContract.getWalletPublicKey(AddrStd(d.address)) | |
}.let { key -> | |
CellBuilder.createCell { | |
storeUInt(key, 256) | |
}.bits | |
} | |
val address = AddrStd(d.address) | |
val tonProofPrefix = "ton-proof-item-v2/" | |
val tonConnectPrefix = "ton-connect" | |
val wc = ByteBuffer.allocate(4).order(ByteOrder.BIG_ENDIAN).putInt(address.workchain_id).array() | |
val addr = ByteBuffer.allocate(32).order(ByteOrder.BIG_ENDIAN).put(address.address.toByteArray()).array() | |
val ts = ByteBuffer.allocate(8).order(ByteOrder.LITTLE_ENDIAN).putLong(d.proof.timestamp).array() | |
val dl = ByteBuffer.allocate(4).order(ByteOrder.LITTLE_ENDIAN).putInt(d.proof.domain.lengthBytes).array() | |
assertEquals(0, hex(wc).toInt()) | |
val message = tonProofPrefix.toByteArray() + | |
wc + | |
addr + | |
dl + | |
d.proof.domain.value.toByteArray() + | |
ts + | |
d.proof.payload.toByteArray() | |
val messageHash = hash256(message) | |
val fullMessage = byteArrayOf( (0xff).toByte(), (0xff).toByte() ) + | |
tonConnectPrefix.toByteArray() + | |
messageHash | |
val fullMessageHash = hash256(fullMessage) | |
val result = PublicKeyEd25519(pubKey.toByteArray()).verify( | |
fullMessageHash, Base64Utils.decodeFromString(d.proof.signature) | |
) | |
assertTrue(result) | |
} | |
fun hash256(x: ByteArray) = | |
MessageDigest.getInstance("sha256").digest(x) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment