Skip to content

Commit

Permalink
Add Chain type (#117)
Browse files Browse the repository at this point in the history
  • Loading branch information
sstone authored Feb 13, 2024
1 parent 7d9f499 commit 44a4e57
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 0 deletions.
14 changes: 14 additions & 0 deletions src/commonMain/kotlin/fr/acinq/bitcoin/Bitcoin.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
8 changes: 8 additions & 0 deletions src/commonTest/kotlin/fr/acinq/bitcoin/BitcoinTestsCommon.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}

0 comments on commit 44a4e57

Please sign in to comment.