From 44a4e57fbd63f99503cd3d255102e6619fb27ac2 Mon Sep 17 00:00:00 2001 From: Fabrice Drouin Date: Tue, 13 Feb 2024 21:23:46 +0100 Subject: [PATCH] Add Chain type (#117) --- src/commonMain/kotlin/fr/acinq/bitcoin/Bitcoin.kt | 14 ++++++++++++++ .../kotlin/fr/acinq/bitcoin/BitcoinTestsCommon.kt | 8 ++++++++ 2 files changed, 22 insertions(+) diff --git a/src/commonMain/kotlin/fr/acinq/bitcoin/Bitcoin.kt b/src/commonMain/kotlin/fr/acinq/bitcoin/Bitcoin.kt index 8f29c6ad..ead4ece2 100644 --- a/src/commonMain/kotlin/fr/acinq/bitcoin/Bitcoin.kt +++ b/src/commonMain/kotlin/fr/acinq/bitcoin/Bitcoin.kt @@ -234,4 +234,18 @@ public object Bitcoin { } ) } + + public sealed class Chain(public val name: String, private val genesis: Block) { + public object Regtest : Chain("Regtest", Block.RegtestGenesisBlock) + public object Testnet : Chain("Testnet", Block.TestnetGenesisBlock) + public object Signet : Chain("Signet", Block.SignetGenesisBlock) + public object Mainnet : Chain("Mainnet", Block.LivenetGenesisBlock) + + public fun isMainnet(): Boolean = this is Mainnet + public fun isTestnet(): Boolean = this is Testnet + + public val chainHash: BlockHash get() = genesis.hash + + override fun toString(): String = name + } } \ No newline at end of file diff --git a/src/commonTest/kotlin/fr/acinq/bitcoin/BitcoinTestsCommon.kt b/src/commonTest/kotlin/fr/acinq/bitcoin/BitcoinTestsCommon.kt index 61affc40..3ecfdf0f 100644 --- a/src/commonTest/kotlin/fr/acinq/bitcoin/BitcoinTestsCommon.kt +++ b/src/commonTest/kotlin/fr/acinq/bitcoin/BitcoinTestsCommon.kt @@ -122,4 +122,12 @@ class BitcoinTestsCommon { assertEquals(Block.TestnetGenesisBlock.blockId, BlockId("000000000933ea01ad0ee984209779baaec3ced90fa3f408719526f8d77f4943")) assertEquals(Block.LivenetGenesisBlock.blockId, BlockId("000000000019d6689c085ae165831e934ff763ae46a2a6c172b3f1b60a8ce26f")) } + + @Test + fun `check Chain objects`() { + assertEquals(Block.RegtestGenesisBlock.hash, Bitcoin.Chain.Regtest.chainHash) + assertEquals(Block.SignetGenesisBlock.hash, Bitcoin.Chain.Signet.chainHash) + assertEquals(Block.TestnetGenesisBlock.hash, Bitcoin.Chain.Testnet.chainHash) + assertEquals(Block.LivenetGenesisBlock.hash, Bitcoin.Chain.Mainnet.chainHash) + } } \ No newline at end of file