Skip to content

Commit

Permalink
feat(ton): Add Android tests
Browse files Browse the repository at this point in the history
  • Loading branch information
satoshiotomakan committed Sep 10, 2024
1 parent 93d661d commit efb6e22
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

package com.trustwallet.core.app.blockchains.theopennetwork

import com.trustwallet.core.app.utils.toHex
import com.trustwallet.core.app.utils.toHexByteArray
import org.junit.Assert.assertEquals
import org.junit.Test
Expand All @@ -26,4 +27,20 @@ class TestTheOpenNetworkWallet {
val expected = "te6cckECFgEAAwQAAgE0AQIBFP8A9KQT9LzyyAsDAFEAAAAAKamjF/IpqTcfp8IQiz2Q6iLJvnBf9dDP6u6cu5Nm/wFxV5NXQAIBIAQFAgFIBgcE+PKDCNcYINMf0x/THwL4I7vyZO1E0NMf0x/T//QE0VFDuvKhUVG68qIF+QFUEGT5EPKj+AAkpMjLH1JAyx9SMMv/UhD0AMntVPgPAdMHIcAAn2xRkyDXSpbTB9QC+wDoMOAhwAHjACHAAuMAAcADkTDjDQOkyMsfEssfy/8ICQoLAubQAdDTAyFxsJJfBOAi10nBIJJfBOAC0x8hghBwbHVnvSKCEGRzdHK9sJJfBeAD+kAwIPpEAcjKB8v/ydDtRNCBAUDXIfQEMFyBAQj0Cm+hMbOSXwfgBdM/yCWCEHBsdWe6kjgw4w0DghBkc3RyupJfBuMNDA0CASAODwBu0gf6ANTUIvkABcjKBxXL/8nQd3SAGMjLBcsCIs8WUAX6AhTLaxLMzMlz+wDIQBSBAQj0UfKnAgBwgQEI1xj6ANM/yFQgR4EBCPRR8qeCEG5vdGVwdIAYyMsFywJQBs8WUAT6AhTLahLLH8s/yXP7AAIAbIEBCNcY+gDTPzBSJIEBCPRZ8qeCEGRzdHJwdIAYyMsFywJQBc8WUAP6AhPLassfEss/yXP7AAAK9ADJ7VQAeAH6APQEMPgnbyIwUAqhIb7y4FCCEHBsdWeDHrFwgBhQBMsFJs8WWPoCGfQAy2kXyx9SYMs/IMmAQPsABgCKUASBAQj0WTDtRNCBAUDXIMgBzxb0AMntVAFysI4jghBkc3Rygx6xcIAYUAXLBVADzxYj+gITy2rLH8s/yYBA+wCSXwPiAgEgEBEAWb0kK29qJoQICga5D6AhhHDUCAhHpJN9KZEM5pA+n/mDeBKAG3gQFImHFZ8xhAIBWBITABG4yX7UTQ1wsfgAPbKd+1E0IEBQNch9AQwAsjKB8v/ydABgQEI9ApvoTGACASAUFQAZrc52omhAIGuQ64X/wAAZrx32omhAEGuQ64WPwEXtMkg="
assertEquals(stateInit, expected)
}

@Test
fun TheOpenNetworkWalletIsValidMnemonic() {
val validMnemonic = "sight shed side garbage illness clean health wet all win bench wide exist find galaxy drift task suggest portion fresh valve crime radar combine"
val noPassphrase = ""
val invalidPassphrase = "Expected empty passphrase"
assert(TONWallet.isValidMnemonic(validMnemonic, noPassphrase))
assert(!TONWallet.isValidMnemonic(validMnemonic, invalidPassphrase))
}

@Test
fun TheOpenNetworkWalletGetKey() {
val tonMnemonic = "sight shed side garbage illness clean health wet all win bench wide exist find galaxy drift task suggest portion fresh valve crime radar combine"
val wallet = TONWallet(tonMnemonic, "")
assertEquals(wallet.key.data().toHex(), "0xb471884e691a9f5bb641b14f33bb9e555f759c24e368c4c0d997db3a60704220")
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import org.junit.Assert.*
import org.junit.Test
import wallet.core.jni.StoredKey
import wallet.core.jni.CoinType
import wallet.core.jni.Derivation
import wallet.core.jni.StoredKeyEncryption

class TestKeyStore {
Expand All @@ -17,9 +18,12 @@ class TestKeyStore {
val keyStore = StoredKey("Test Wallet", "password".toByteArray())
val result = keyStore.decryptMnemonic("wrong".toByteArray())
val result2 = keyStore.decryptMnemonic("password".toByteArray())
val result3 = keyStore.decryptTONMnemonic("password".toByteArray())

assertNull(result)
assertNotNull(result2)
// StoredKey is an HD by default, so `decryptTONMnemonic` should return null.
assertNull(result3)
}

@Test
Expand Down Expand Up @@ -91,4 +95,48 @@ class TestKeyStore {
val privateKey = newKeyStore.decryptPrivateKey("".toByteArray())
assertNull(privateKey)
}

@Test
fun testImportTONWallet() {
val tonMnemonic = "laundry myself fitness beyond prize piano match acid vacuum already abandon dance occur pause grocery company inject excuse weasel carpet fog grunt trick spike"
val password = "password".toByteArray()

val keyStore = StoredKey.importTONWallet(tonMnemonic, "Test Wallet", password, CoinType.TON)

val decrypted1 = keyStore.decryptTONMnemonic("wrong".toByteArray())
val decrypted2 = keyStore.decryptTONMnemonic("password".toByteArray())
assertNull(decrypted1)
assertNotNull(decrypted2)

assertEquals(keyStore.accountCount(), 1)

// `StoredKey.account(index)` is only allowed.
// `StoredKey.accountForCoin(coin, wallet)` is not supported.
val tonAccount = keyStore.account(0)
assertEquals(tonAccount.address(), "UQDdB2lMwYM9Gxc-ln--Tu8cz-TYksQxYuUsMs2Pd4cHerYz")
assertEquals(tonAccount.coin(), CoinType.TON)
assertEquals(tonAccount.publicKey(), "c9af50596bd5c1c5a15fb32bef8d4f1ee5244b287aea1f49f6023a79f9b2f055")
assertEquals(tonAccount.extendedPublicKey(), "")
assertEquals(tonAccount.derivation(), Derivation.DEFAULT)
assertEquals(tonAccount.derivationPath(), "")

val privateKey = keyStore.privateKey(CoinType.TON, password)
assertEquals(privateKey.data().toHex(), "0x859cd74ab605afb7ce9f5316a1f6d59217a130b75b494efd249913be874c9d46")

// HD wallet is not supported for TON wallet
val hdWallet = keyStore.wallet(password)
assertNull(hdWallet)
}

@Test
fun testExportTONWallet() {
val tonMnemonic = "laundry myself fitness beyond prize piano match acid vacuum already abandon dance occur pause grocery company inject excuse weasel carpet fog grunt trick spike"
val password = "password".toByteArray()

val keyStore = StoredKey.importTONWallet(tonMnemonic, "Test Wallet", password, CoinType.TON)
val json = keyStore.exportJSON()

val newKeyStore = StoredKey.importJSON(json)
assertEquals(newKeyStore.decryptTONMnemonic(password), tonMnemonic)
}
}

0 comments on commit efb6e22

Please sign in to comment.