This repository has been archived by the owner on Dec 12, 2024. It is now read-only.
generated from TBD54566975/tbd-project-template
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
0062324
commit a919c58
Showing
8 changed files
with
110 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
44 changes: 44 additions & 0 deletions
44
src/main/kotlin/xyz/block/moneyaddress/typed/BtcLightningOffer.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package xyz.block.moneyaddress.typed | ||
|
||
import xyz.block.moneyaddress.BTC | ||
import xyz.block.moneyaddress.LIGHTNING_OFFER | ||
import xyz.block.moneyaddress.TypedMoneyAddressRegistry | ||
import xyz.block.moneyaddress.TypedMoneyAddress | ||
import xyz.block.moneyaddress.matches | ||
import xyz.block.moneyaddress.MoneyAddress as UntypedMoneyAddress | ||
|
||
/** | ||
* A typed representation of a Bitcoin Lightning Offer | ||
* i.e. a re-usable payment code from [BOLT 12](https://github.com/rustyrussell/lightning-rfc/blob/guilt/offers/12-offer-encoding.md) | ||
* | ||
* @property address - the string representation of the lightning offer. Not validated. | ||
* @property currency - always [BTC]. | ||
* @property protocol - always [LIGHTNING_OFFER]. | ||
*/ | ||
data class BtcLightningOffer( | ||
override val address: String, | ||
override val id: String, | ||
) : TypedMoneyAddress<String>(address, BTC, LIGHTNING_OFFER, id) { | ||
companion object { | ||
fun register(typedMoneyAddressRegistry: TypedMoneyAddressRegistry) { | ||
typedMoneyAddressRegistry.register(BTC, LIGHTNING_OFFER) { address -> from(address) } | ||
} | ||
|
||
/** | ||
* Converts an [UntypedMoneyAddress] to a [BtcLightningAddress]. | ||
* | ||
* @throws NotABtcLightningAddressException if the currency is not [BTC] or the protocol is not [LIGHTNING_OFFER]. | ||
*/ | ||
fun from(untypedMoneyAddress: UntypedMoneyAddress): BtcLightningOffer { | ||
if (!untypedMoneyAddress.matches(BTC, LIGHTNING_OFFER)) { | ||
throw NotABtcLightningOfferException(untypedMoneyAddress) | ||
} | ||
return BtcLightningOffer(untypedMoneyAddress.pss, untypedMoneyAddress.id) | ||
} | ||
|
||
fun UntypedMoneyAddress.toBtcLightningOffer(): BtcLightningOffer = from(this) | ||
} | ||
} | ||
|
||
class NotABtcLightningOfferException(moneyAddress: UntypedMoneyAddress) : | ||
Throwable("Not a bitcoin lightning offer [$moneyAddress]") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
23 changes: 23 additions & 0 deletions
23
src/test/kotlin/xyz/block/moneyaddress/typed/BtcLightningOfferTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package xyz.block.moneyaddress.typed | ||
|
||
import xyz.block.moneyaddress.MoneyAddressExamples.Companion.btcLightningOfferMoneyAddress1 | ||
import xyz.block.moneyaddress.typed.BtcLightningOffer.Companion.toBtcLightningOffer | ||
import kotlin.test.Test | ||
import kotlin.test.assertEquals | ||
|
||
class BtcLightningOfferTest { | ||
@Test | ||
fun toBtcLightningOffer() { | ||
assertEquals( | ||
BtcLightningOffer(btcLightningOfferMoneyAddress1.pss, btcLightningOfferMoneyAddress1.id), | ||
btcLightningOfferMoneyAddress1.toBtcLightningOffer() | ||
) | ||
} | ||
@Test | ||
fun fromUntypedMoneyOffer() { | ||
assertEquals( | ||
BtcLightningOffer(btcLightningOfferMoneyAddress1.pss, btcLightningOfferMoneyAddress1.id), | ||
BtcLightningOffer.from(btcLightningOfferMoneyAddress1) | ||
) | ||
} | ||
} |