Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
88 changes: 88 additions & 0 deletions app/src/androidTest/java/to/bitkit/services/OnchainServiceTests.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package to.bitkit.services

import androidx.test.ext.junit.runners.AndroidJUnit4
import com.synonym.bitkitcore.AccountType
import com.synonym.bitkitcore.AddressType
import kotlinx.coroutines.runBlocking
import org.junit.Before
Expand All @@ -11,6 +12,7 @@ import to.bitkit.models.toDerivationPath
import to.bitkit.test.annotations.CoreServiceIntegration
import to.bitkit.test.annotations.DeviceIntegration
import kotlin.test.assertEquals
import kotlin.test.assertFalse
import kotlin.test.assertNotNull
import kotlin.test.assertTrue

Expand Down Expand Up @@ -146,6 +148,86 @@ class OnchainServiceTests {
assertNotNull(result.publicKey)
}

@Test
fun testDeriveBitcoinDescriptorsForSupportedAccountTypes(): Unit = runBlocking {
val cases = listOf(
DescriptorCase(AccountType.LEGACY, "pkh([", "44"),
DescriptorCase(AccountType.WRAPPED_SEGWIT, "sh(wpkh([", "49"),
DescriptorCase(AccountType.NATIVE_SEGWIT, "wpkh([", "84"),
DescriptorCase(AccountType.TAPROOT, "tr([", "86"),
)

cases.forEach { descriptorCase ->
assertDescriptorShape(
descriptorCase = descriptorCase,
network = Network.BITCOIN,
coinType = "0",
publicKeyPrefix = "xpub",
)
}
}

@Test
fun testDeriveRegtestDescriptorsForSupportedAccountTypes(): Unit = runBlocking {
val cases = listOf(
DescriptorCase(AccountType.LEGACY, "pkh([", "44"),
DescriptorCase(AccountType.WRAPPED_SEGWIT, "sh(wpkh([", "49"),
DescriptorCase(AccountType.NATIVE_SEGWIT, "wpkh([", "84"),
DescriptorCase(AccountType.TAPROOT, "tr([", "86"),
)

cases.forEach { descriptorCase ->
assertDescriptorShape(
descriptorCase = descriptorCase,
network = Network.REGTEST,
coinType = "1",
publicKeyPrefix = "tpub",
)
}
}

@Test
fun testDeriveBitcoinNativeSegwitDescriptorWithPassphrase(): Unit = runBlocking {
val descriptor = onchainService.deriveOnchainDescriptor(
mnemonicPhrase = mnemonic,
network = Network.BITCOIN,
bip39Passphrase = "TREZOR",
accountType = AccountType.NATIVE_SEGWIT,
accountIndex = 0u,
)
val descriptorWithoutPassphrase = onchainService.deriveOnchainDescriptor(
mnemonicPhrase = mnemonic,
network = Network.BITCOIN,
bip39Passphrase = null,
accountType = AccountType.NATIVE_SEGWIT,
accountIndex = 0u,
)

assertTrue(descriptor.startsWith("wpkh(["))
assertTrue(descriptor.contains("/84'/0'/0']xpub"))
assertTrue(descriptor.contains("/0/*"))
assertFalse(descriptor == descriptorWithoutPassphrase)
}

private suspend fun assertDescriptorShape(
descriptorCase: DescriptorCase,
network: Network,
coinType: String,
publicKeyPrefix: String,
) {
val descriptor = onchainService.deriveOnchainDescriptor(
mnemonicPhrase = mnemonic,
network = network,
bip39Passphrase = null,
accountType = descriptorCase.accountType,
accountIndex = 0u,
)

assertTrue(descriptor.startsWith(descriptorCase.prefix))
assertTrue(descriptor.contains("/${descriptorCase.purpose}'/$coinType'/0']$publicKeyPrefix"))
assertTrue(descriptor.contains("/0/*"))
}

@Test
fun testDeriveAddresses(): Unit = runBlocking {
val derivationPath = AddressType.P2WPKH.toDerivationPath(network = Network.BITCOIN)
Expand Down Expand Up @@ -233,3 +315,9 @@ class OnchainServiceTests {
}

}

private data class DescriptorCase(
val accountType: AccountType,
val prefix: String,
val purpose: String,
)
17 changes: 17 additions & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,23 @@
<data android:scheme="pubkyauth" />
</intent-filter>

<!-- BTCPay wallet connection links -->
<intent-filter>
<action android:name="android.intent.action.VIEW" />

<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />

<data
android:scheme="https"
android:host="*"
android:pathPattern="/plugins/.*/samrock/protocol" />
<data
android:scheme="http"
android:host="*"
android:pathPattern="/plugins/.*/samrock/protocol" />
</intent-filter>

<!-- NFC -->
<intent-filter>
<action android:name="android.nfc.action.NDEF_DISCOVERED" />
Expand Down
Loading
Loading