Add description and metadata to pay_invoice command
Changelog-Added: Zap receiver information now included for outgoing zaps Closes: https://github.com/damus-io/damus/issues/2927 Signed-off-by: Daniel D’Aquino <daniel@daquino.me>
This commit is contained in:
@@ -13,7 +13,11 @@ extension WalletConnect {
|
||||
/// Pay an invoice
|
||||
case payInvoice(
|
||||
/// bolt-11 invoice string
|
||||
invoice: String
|
||||
invoice: String,
|
||||
/// The full description of the invoice (If description does not fit in the BOLT-11 invoice, this is the pre-image of the description hash)
|
||||
description: String?,
|
||||
/// Optional metadata object containing more information
|
||||
metadata: Metadata?
|
||||
)
|
||||
/// Get the current wallet balance
|
||||
case getBalance
|
||||
@@ -33,6 +37,38 @@ extension WalletConnect {
|
||||
type: String?
|
||||
)
|
||||
|
||||
static func payZapRequest(invoice: String, zapRequest: NostrEvent?) -> Self {
|
||||
guard let zapRequest, let zapRequestEncoded = encode_json(zapRequest) else {
|
||||
return WalletConnect.Request.payInvoice(
|
||||
invoice: invoice,
|
||||
description: nil,
|
||||
metadata: nil
|
||||
)
|
||||
}
|
||||
return WalletConnect.Request.payInvoice(
|
||||
invoice: invoice,
|
||||
description: zapRequestEncoded,
|
||||
metadata: .init(nostr: zapRequest)
|
||||
)
|
||||
}
|
||||
|
||||
struct Metadata: Codable, Equatable, Hashable {
|
||||
/// NIP-57-compliant `kind:9734` zap request event
|
||||
let nostr: NostrEvent?
|
||||
|
||||
init(nostr: NostrEvent?) {
|
||||
self.nostr = nostr
|
||||
}
|
||||
|
||||
init(from decoder: any Decoder) throws {
|
||||
let container: KeyedDecodingContainer<WalletConnect.Request.Metadata.CodingKeys> = try decoder.container(keyedBy: WalletConnect.Request.Metadata.CodingKeys.self)
|
||||
guard let decodedZapRequest = try? container.decodeIfPresent(NostrEvent.self, forKey: WalletConnect.Request.Metadata.CodingKeys.nostr) else {
|
||||
self.nostr = nil // Be lenient and fallback to nil if the NWC provider provided something invalid, since metadata is not strictly spec'd yet.
|
||||
return
|
||||
}
|
||||
self.nostr = decodedZapRequest
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: - Interface
|
||||
|
||||
@@ -61,7 +97,7 @@ extension WalletConnect {
|
||||
|
||||
/// Keys for the JSON inside the "params" object
|
||||
private enum ParamKeys: String, CodingKey {
|
||||
case invoice
|
||||
case invoice, description, metadata
|
||||
case from, until, limit, offset, unpaid, type
|
||||
}
|
||||
|
||||
@@ -82,7 +118,9 @@ extension WalletConnect {
|
||||
case Method.payInvoice.rawValue:
|
||||
let paramsContainer = try container.nestedContainer(keyedBy: ParamKeys.self, forKey: .params)
|
||||
let invoice = try paramsContainer.decode(String.self, forKey: .invoice)
|
||||
self = .payInvoice(invoice: invoice)
|
||||
let description: String? = try paramsContainer.decodeIfPresent(String.self, forKey: .description)
|
||||
let metadata: Metadata? = try paramsContainer.decodeIfPresent(Metadata.self, forKey: .metadata)
|
||||
self = .payInvoice(invoice: invoice, description: description, metadata: metadata)
|
||||
|
||||
case Method.getBalance.rawValue:
|
||||
// No params to decode
|
||||
@@ -112,10 +150,12 @@ extension WalletConnect {
|
||||
var container = encoder.container(keyedBy: CodingKeys.self)
|
||||
|
||||
switch self {
|
||||
case .payInvoice(let invoice):
|
||||
case .payInvoice(let invoice, let description, let metadata):
|
||||
try container.encode(Method.payInvoice.rawValue, forKey: .method)
|
||||
var paramsContainer = container.nestedContainer(keyedBy: ParamKeys.self, forKey: .params)
|
||||
try paramsContainer.encode(invoice, forKey: .invoice)
|
||||
try paramsContainer.encodeIfPresent(description, forKey: .description)
|
||||
try paramsContainer.encodeIfPresent(metadata, forKey: .metadata)
|
||||
|
||||
case .getBalance:
|
||||
try container.encode(Method.getBalance.rawValue, forKey: .method)
|
||||
|
||||
@@ -40,8 +40,9 @@ extension WalletConnect {
|
||||
/// - on_flush: A callback to call after the event has been flushed to the network
|
||||
/// - Returns: The Nostr Event that was sent to the network, representing the request that was made
|
||||
@discardableResult
|
||||
static func pay(url: WalletConnectURL, pool: RelayPool, post: PostBox, invoice: String, delay: TimeInterval? = 5.0, on_flush: OnFlush? = nil) -> NostrEvent? {
|
||||
let req = WalletConnect.Request.payInvoice(invoice: invoice)
|
||||
static func pay(url: WalletConnectURL, pool: RelayPool, post: PostBox, invoice: String, zap_request: NostrEvent?, delay: TimeInterval? = 5.0, on_flush: OnFlush? = nil) -> NostrEvent? {
|
||||
|
||||
let req = WalletConnect.Request.payZapRequest(invoice: invoice, zapRequest: zap_request)
|
||||
guard let ev = req.to_nostr_event(to_pk: url.pubkey, keypair: url.keypair) else {
|
||||
return nil
|
||||
}
|
||||
@@ -142,7 +143,7 @@ extension WalletConnect {
|
||||
}
|
||||
|
||||
print("damus-donation donating...")
|
||||
WalletConnect.pay(url: nwc, pool: pool, post: postbox, invoice: invoice, delay: nil)
|
||||
WalletConnect.pay(url: nwc, pool: pool, post: postbox, invoice: invoice, zap_request: nil, delay: nil)
|
||||
}
|
||||
|
||||
/// Handles a received Nostr Wallet Connect error
|
||||
|
||||
@@ -86,7 +86,7 @@ extension WalletConnect {
|
||||
let created_at: UInt64 // unixtimestamp, // invoice/payment creation time
|
||||
let expires_at: UInt64? // unixtimestamp, // invoice expiration time, optional if not applicable
|
||||
let settled_at: UInt64? // unixtimestamp, // invoice/payment settlement time, optional if unpaid
|
||||
//"metadata": {} // generic metadata that can be used to add things like zap/boostagram details for a payer name/comment/etc.
|
||||
let metadata: WalletConnect.Request.Metadata? // generic metadata that can be used to add things like zap/boostagram details for a payer name/comment/etc.
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user