mirror of
https://github.com/vitorpamplona/amethyst.git
synced 2024-09-29 16:30:49 +00:00
Moves Bech Utility methods from CryptoUtils to BechUtils
This commit is contained in:
parent
69eea8824a
commit
7923f5d854
@ -29,9 +29,9 @@ public typealias Int5 = Byte
|
||||
* See https://github.com/bitcoin/bips/blob/master/bip-0173.mediawiki and https://github.com/bitcoin/bips/blob/master/bip-0350.mediawiki.
|
||||
*/
|
||||
object Bech32 {
|
||||
public const val alphabet: String = "qpzry9x8gf2tvdw0s3jn54khce6mua7l"
|
||||
const val alphabet: String = "qpzry9x8gf2tvdw0s3jn54khce6mua7l"
|
||||
|
||||
public enum class Encoding(public val constant: Int) {
|
||||
enum class Encoding(public val constant: Int) {
|
||||
Bech32(1),
|
||||
Bech32m(0x2bc830a3),
|
||||
Beck32WithoutChecksum(0)
|
||||
@ -198,3 +198,16 @@ object Bech32 {
|
||||
return output.toByteArray()
|
||||
}
|
||||
}
|
||||
|
||||
fun ByteArray.toNsec() = Bech32.encodeBytes(hrp = "nsec", this, Bech32.Encoding.Bech32)
|
||||
fun ByteArray.toNpub() = Bech32.encodeBytes(hrp = "npub", this, Bech32.Encoding.Bech32)
|
||||
|
||||
fun String.bechToBytes(hrp: String? = null): ByteArray {
|
||||
val decodedForm = Bech32.decodeBytes(this)
|
||||
hrp?.also {
|
||||
if (it != decodedForm.first) {
|
||||
throw IllegalArgumentException("Expected $it but obtained ${decodedForm.first}")
|
||||
}
|
||||
}
|
||||
return decodedForm.second
|
||||
}
|
||||
|
@ -72,16 +72,3 @@ fun Int.toByteArray(): ByteArray {
|
||||
}
|
||||
return bytes
|
||||
}
|
||||
|
||||
fun ByteArray.toNsec() = Bech32.encodeBytes(hrp = "nsec", this, Bech32.Encoding.Bech32)
|
||||
fun ByteArray.toNpub() = Bech32.encodeBytes(hrp = "npub", this, Bech32.Encoding.Bech32)
|
||||
|
||||
fun String.bechToBytes(hrp: String? = null): ByteArray {
|
||||
val decodedForm = Bech32.decodeBytes(this)
|
||||
hrp?.also {
|
||||
if (it != decodedForm.first) {
|
||||
throw IllegalArgumentException("Expected $it but obtained ${decodedForm.first}")
|
||||
}
|
||||
}
|
||||
return decodedForm.second
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user