Add to_qr_uri() method on Address type

This commit is contained in:
thunderbiscuit 2023-03-23 17:03:40 -04:00
parent cbd44249f3
commit cd10c75e96
No known key found for this signature in database
GPG Key ID: 88253696EB836462
3 changed files with 23 additions and 7 deletions

View File

@ -798,11 +798,20 @@ class Address(address: String) {
fun payload(): Payload fun payload(): Payload
/** Return the Network. */ /** Return the Network. */
fun Network(): Network fun network(): Network
/** Return the ScriptPubKey. */ /** Return the ScriptPubKey. */
fun scriptPubkey(): Script fun scriptPubkey(): Script
}
/**
* Creates a URI string bitcoin:address optimized to be encoded in QR codes.
*
* If the address is bech32, both the schema and the address become uppercase. If the address is base58, the schema is lowercase and the address is left mixed case.
*
* Quoting BIP 173 "inside QR codes uppercase SHOULD be used, as those permit the use of alphanumeric mode, which is 45% more compact than the normal byte mode."
*/
fun toQrUri(): String
}}
/** /**
* The method used to produce an address. * The method used to produce an address.
@ -810,17 +819,18 @@ class Address(address: String) {
sealed class Payload { sealed class Payload {
/** P2PKH address. */ /** P2PKH address. */
data class PubkeyHash( data class PubkeyHash(
val `pubkeyHash`: List<UByte> val pubkeyHash: List<UByte>
) : Payload() ) : Payload()
/** P2SH address. */ /** P2SH address. */
data class ScriptHash( data class ScriptHash(
val `scriptHash`: List<UByte> val scriptHash: List<UByte>
) : Payload() ) : Payload()
/** Segwit address. */ /** Segwit address. */
data class WitnessProgram( data class WitnessProgram(
val `version`: WitnessVersion, val version: WitnessVersion,
val `program`: List<UByte> val program: List<UByte>
) : Payload() ) : Payload()
} }
@ -833,7 +843,7 @@ sealed class Payload {
* from 0 to 16 (inclusive). * from 0 to 16 (inclusive).
*/ */
enum class WitnessVersion { enum class WitnessVersion {
V0,V1,V2,V3,V4,V5,V6,V7,V8,V9,V10,V11,V12,V13,V14,V15,V16; V0, V1, V2, V3, V4, V5, V6, V7, V8, V9, V10, V11, V12, V13, V14, V15, V16
} }
/** /**

View File

@ -440,6 +440,8 @@ interface Address {
Network network(); Network network();
Script script_pubkey(); Script script_pubkey();
string to_qr_uri();
}; };
[Enum] [Enum]

View File

@ -381,6 +381,10 @@ impl Address {
script: self.address.script_pubkey(), script: self.address.script_pubkey(),
}) })
} }
fn to_qr_uri(&self) -> String {
self.address.to_qr_uri()
}
} }
/// The method used to produce an address. /// The method used to produce an address.