From cd10c75e965584160235db8abb79540d55ce9c80 Mon Sep 17 00:00:00 2001 From: thunderbiscuit Date: Thu, 23 Mar 2023 17:03:40 -0400 Subject: [PATCH] Add to_qr_uri() method on Address type --- .../src/main/kotlin/org/bitcoindevkit/bdk.kt | 24 +++++++++++++------ bdk-ffi/src/bdk.udl | 2 ++ bdk-ffi/src/lib.rs | 4 ++++ 3 files changed, 23 insertions(+), 7 deletions(-) diff --git a/api-docs/kotlin/src/main/kotlin/org/bitcoindevkit/bdk.kt b/api-docs/kotlin/src/main/kotlin/org/bitcoindevkit/bdk.kt index ebf2b3a..b96530c 100644 --- a/api-docs/kotlin/src/main/kotlin/org/bitcoindevkit/bdk.kt +++ b/api-docs/kotlin/src/main/kotlin/org/bitcoindevkit/bdk.kt @@ -798,11 +798,20 @@ class Address(address: String) { fun payload(): Payload /** Return the Network. */ - fun Network(): Network + fun network(): Network /** Return the ScriptPubKey. */ 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. @@ -810,17 +819,18 @@ class Address(address: String) { sealed class Payload { /** P2PKH address. */ data class PubkeyHash( - val `pubkeyHash`: List + val pubkeyHash: List ) : Payload() /** P2SH address. */ data class ScriptHash( - val `scriptHash`: List + val scriptHash: List ) : Payload() + /** Segwit address. */ data class WitnessProgram( - val `version`: WitnessVersion, - val `program`: List + val version: WitnessVersion, + val program: List ) : Payload() } @@ -833,7 +843,7 @@ sealed class Payload { * from 0 to 16 (inclusive). */ 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 } /** diff --git a/bdk-ffi/src/bdk.udl b/bdk-ffi/src/bdk.udl index 8ae88d0..a000ecb 100644 --- a/bdk-ffi/src/bdk.udl +++ b/bdk-ffi/src/bdk.udl @@ -440,6 +440,8 @@ interface Address { Network network(); Script script_pubkey(); + + string to_qr_uri(); }; [Enum] diff --git a/bdk-ffi/src/lib.rs b/bdk-ffi/src/lib.rs index 1e0e04b..1aecf49 100644 --- a/bdk-ffi/src/lib.rs +++ b/bdk-ffi/src/lib.rs @@ -381,6 +381,10 @@ impl Address { script: self.address.script_pubkey(), }) } + + fn to_qr_uri(&self) -> String { + self.address.to_qr_uri() + } } /// The method used to produce an address.