Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Chain type #117

Merged
merged 1 commit into from
Feb 13, 2024
Merged
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
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)
}
}
Loading