sattleme-checkout 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/CHANGELOG.md +66 -0
- data/LICENSE +21 -0
- data/README.md +754 -0
- data/SECURITY.md +25 -0
- data/lib/sattleme/checkout/cancellation.rb +195 -0
- data/lib/sattleme/checkout/client.rb +1438 -0
- data/lib/sattleme/checkout/errors.rb +165 -0
- data/lib/sattleme/checkout/http_client.rb +294 -0
- data/lib/sattleme/checkout/internal/bolt11.rb +187 -0
- data/lib/sattleme/checkout/internal/channel.rb +147 -0
- data/lib/sattleme/checkout/internal/checkout_link.rb +294 -0
- data/lib/sattleme/checkout/internal/concurrency.rb +450 -0
- data/lib/sattleme/checkout/internal/crypto.rb +63 -0
- data/lib/sattleme/checkout/internal/currencies.rb +47 -0
- data/lib/sattleme/checkout/internal/declaration.rb +135 -0
- data/lib/sattleme/checkout/internal/derive.rb +153 -0
- data/lib/sattleme/checkout/internal/event.rb +110 -0
- data/lib/sattleme/checkout/internal/http.rb +129 -0
- data/lib/sattleme/checkout/internal/json.rb +457 -0
- data/lib/sattleme/checkout/internal/key.rb +116 -0
- data/lib/sattleme/checkout/internal/lnurl.rb +238 -0
- data/lib/sattleme/checkout/internal/nip44.rb +113 -0
- data/lib/sattleme/checkout/internal/onchain/address.rb +58 -0
- data/lib/sattleme/checkout/internal/onchain/basics.rb +352 -0
- data/lib/sattleme/checkout/internal/onchain/lease.rb +326 -0
- data/lib/sattleme/checkout/internal/onchain/merchant_acceptance.rb +168 -0
- data/lib/sattleme/checkout/internal/onchain/node.rb +285 -0
- data/lib/sattleme/checkout/internal/onchain/payment.rb +345 -0
- data/lib/sattleme/checkout/internal/onchain/rest.rb +323 -0
- data/lib/sattleme/checkout/internal/onchain/watch.rb +119 -0
- data/lib/sattleme/checkout/internal/onchain/witness.rb +260 -0
- data/lib/sattleme/checkout/internal/order.rb +228 -0
- data/lib/sattleme/checkout/internal/protocol.rb +260 -0
- data/lib/sattleme/checkout/internal/rates.rb +373 -0
- data/lib/sattleme/checkout/internal/record.rb +179 -0
- data/lib/sattleme/checkout/internal/relay_pool.rb +823 -0
- data/lib/sattleme/checkout/internal/sales.rb +238 -0
- data/lib/sattleme/checkout/internal/secp256k1.rb +234 -0
- data/lib/sattleme/checkout/internal/text.rb +144 -0
- data/lib/sattleme/checkout/internal/urls.rb +172 -0
- data/lib/sattleme/checkout/models.rb +247 -0
- data/lib/sattleme/checkout/version.rb +9 -0
- data/lib/sattleme/checkout/websocket_client.rb +487 -0
- data/lib/sattleme/checkout.rb +72 -0
- metadata +112 -0
|
@@ -0,0 +1,172 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Sattleme
|
|
4
|
+
module Checkout
|
|
5
|
+
module Internal
|
|
6
|
+
# Hosts and https URLs, in their one spelling (SPEC.md §9, §15): what the gem asks over the
|
|
7
|
+
# network (relays, the Lightning address's pay URL, its callback and verify URLs) and what a
|
|
8
|
+
# checkout link sends the customer to. Nothing is repaired: a URL that is not so is refused,
|
|
9
|
+
# so that no request goes to an IP address, to the machine itself (`localhost` and the names
|
|
10
|
+
# under it, RFC 6761) or to a host other than the one checked.
|
|
11
|
+
module Urls
|
|
12
|
+
# A host: labels of `a–z 0–9 -` separated by `.`, the last of at least two letters `a–z`.
|
|
13
|
+
HOST = /\A[a-z0-9-]+(?:\.[a-z0-9-]+)*\.[a-z]{2,}\z/
|
|
14
|
+
# The characters of a redirect URL and of an https URL: RFC 3986's, and `%` with two hex
|
|
15
|
+
# digits.
|
|
16
|
+
URL_CHARACTERS = %r{\A(?:[A-Za-z0-9\-._~:/?#\[\]@!$&'()*+,;=]|%[0-9A-Fa-f]{2})+\z}
|
|
17
|
+
# `https://`, the authority (up to the first `/`, `?` or `#`), then anything.
|
|
18
|
+
HTTPS = %r{\Ahttps://([^/?#]*)(?:[/?#].*)?\z}m
|
|
19
|
+
# An authority: a host and an optional port of 1 to 5 digits.
|
|
20
|
+
AUTHORITY = /\A([^:]*)(?::([0-9]{1,5}))?\z/
|
|
21
|
+
|
|
22
|
+
# An https URL's host and port: 443 when it names none.
|
|
23
|
+
HttpsUrl = Data.define(:host, :port)
|
|
24
|
+
|
|
25
|
+
module_function
|
|
26
|
+
|
|
27
|
+
# Whether `host` is a host as in a relay address: lower case, at most 253 characters, so
|
|
28
|
+
# never an IP address; its last label not `localhost`, nor a name under it.
|
|
29
|
+
def host?(host)
|
|
30
|
+
Text.well_formed?(host) && host.length <= Protocol::MAX_HOST_LENGTH && host.match?(HOST) &&
|
|
31
|
+
!host.match?(/(?:\A|\.)localhost\z/)
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
# Whether `port` (its digits, nil when the URL has none) is absent or 1 to 65535.
|
|
35
|
+
def port?(port)
|
|
36
|
+
port.nil? || port.to_i.between?(1, 65_535)
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
# `value` as an https URL (rule 1 of a redirect URL): 1 to 2048 characters of a redirect
|
|
40
|
+
# URL, `https://`, a host, an optional port, then nothing or `/`, `?` or `#` and anything.
|
|
41
|
+
# So no upper case in the scheme or the host, no user name nor password, no IP address, no
|
|
42
|
+
# whitespace, no `\`. Nil when it is not one.
|
|
43
|
+
def https_url(value)
|
|
44
|
+
return nil unless Text.match?(value, URL_CHARACTERS) && value.length <= Protocol::MAX_HTTPS_URL_LENGTH
|
|
45
|
+
|
|
46
|
+
authority = AUTHORITY.match(HTTPS.match(value)&.[](1) || "")
|
|
47
|
+
return nil unless authority && host?(authority[1]) && port?(authority[2])
|
|
48
|
+
|
|
49
|
+
HttpsUrl.new(host: authority[1], port: authority[2].nil? ? 443 : authority[2].to_i)
|
|
50
|
+
end
|
|
51
|
+
|
|
52
|
+
# Whether two https URLs are on the same host: their hosts and their ports are equal.
|
|
53
|
+
def same_host?(first, second)
|
|
54
|
+
first.host == second.host && first.port == second.port
|
|
55
|
+
end
|
|
56
|
+
|
|
57
|
+
# The host of a URL, for messages: never its path (a Lightning address's name is in it).
|
|
58
|
+
def host_of(url)
|
|
59
|
+
match = %r{\A[a-z][a-z0-9+.-]*://([^/?#]*)}.match(url.to_s)
|
|
60
|
+
match ? match[1] : "(invalid URL)"
|
|
61
|
+
end
|
|
62
|
+
|
|
63
|
+
# The request target (path and query) of a URL whose authority ends at `start`: what a
|
|
64
|
+
# browser asks of it (WHATWG): `\` is a separator, `.` and `..` segments resolved (`%2e`
|
|
65
|
+
# is a dot), what a request line cannot hold percent-encoded; the fragment dropped.
|
|
66
|
+
def request_target(rest)
|
|
67
|
+
path, query = rest.sub(/#.*\z/m, "").split("?", 2)
|
|
68
|
+
parts = path.to_s.tr("\\", "/").split("/", -1).drop(1)
|
|
69
|
+
segments = []
|
|
70
|
+
parts.each_with_index do |segment, index|
|
|
71
|
+
last = index == parts.size - 1
|
|
72
|
+
case segment.downcase
|
|
73
|
+
when ".", "%2e"
|
|
74
|
+
segments << "" if last
|
|
75
|
+
when "..", ".%2e", "%2e.", "%2e%2e"
|
|
76
|
+
segments.pop
|
|
77
|
+
segments << "" if last
|
|
78
|
+
else
|
|
79
|
+
segments << segment
|
|
80
|
+
end
|
|
81
|
+
end
|
|
82
|
+
target = "/#{segments.map { |segment| percent_encode(segment, PATH_UNSAFE) }.join("/")}"
|
|
83
|
+
query ? "#{target}?#{percent_encode(query, QUERY_UNSAFE)}" : target
|
|
84
|
+
end
|
|
85
|
+
|
|
86
|
+
# WHATWG's path percent-encode set and special-query percent-encode set (with `#`, which the
|
|
87
|
+
# fragment's removal leaves out), every byte beyond ASCII included.
|
|
88
|
+
PATH_UNSAFE = /[\x00-\x20"#<>?`{}\x7F-\xFF]/n
|
|
89
|
+
QUERY_UNSAFE = /[\x00-\x20"#<>'\x7F-\xFF]/n
|
|
90
|
+
|
|
91
|
+
def percent_encode(text, unsafe)
|
|
92
|
+
text.b.gsub(unsafe) { |byte| format("%%%02X", byte.ord) }.force_encoding(Encoding::UTF_8)
|
|
93
|
+
end
|
|
94
|
+
end
|
|
95
|
+
|
|
96
|
+
# Lists of Nostr relays, as the merchant's app writes them (SPEC.md §9): every address a
|
|
97
|
+
# `wss://` URL in its one spelling, 1 to 10 of them, none twice. A list that is not so is
|
|
98
|
+
# refused, never repaired.
|
|
99
|
+
module Relays
|
|
100
|
+
PREFIX = "wss://"
|
|
101
|
+
# ECMAScript's whitespace (`\s`), as the merchant's app's rule reads it.
|
|
102
|
+
WHITESPACE = "\\t\\n\\v\\f\\r\\u0020\\u00A0\\u1680\\u2000-\\u200A\\u2028\\u2029\\u202F\\u205F\\u3000\\uFEFF"
|
|
103
|
+
# A lowercase host name, an optional port, an optional path without whitespace, query,
|
|
104
|
+
# fragment nor comma: the merchant's app's rule.
|
|
105
|
+
ADDRESS = %r{\A([a-z0-9-]+(?:\.[a-z0-9-]+)*\.[a-z]{2,})(:[0-9]{1,5})?(/[^#{WHITESPACE}?\#,]*)?\z}
|
|
106
|
+
|
|
107
|
+
# Where a relay address connects: its host, its port (443 when it names none) and the request
|
|
108
|
+
# target of its WebSocket handshake.
|
|
109
|
+
Endpoint = Data.define(:host, :port, :target)
|
|
110
|
+
|
|
111
|
+
module_function
|
|
112
|
+
|
|
113
|
+
# Whether `url` is a relay address in its one spelling: `wss://`, a lowercase host name (not
|
|
114
|
+
# `localhost` nor a name under it), an optional port (1 to 65535) and an optional path
|
|
115
|
+
# without a trailing slash, 120 code points at most.
|
|
116
|
+
def url?(url)
|
|
117
|
+
return false unless Text.well_formed?(url) && url.start_with?(PREFIX)
|
|
118
|
+
|
|
119
|
+
match = ADDRESS.match(url.delete_prefix(PREFIX))
|
|
120
|
+
return false unless match
|
|
121
|
+
|
|
122
|
+
path = match[3] || ""
|
|
123
|
+
Urls.host?(match[1]) && Urls.port?(match[2]&.delete_prefix(":")) && !path.end_with?("/") &&
|
|
124
|
+
Text.code_point_length(url) <= Protocol::MAX_RELAY_URL_LENGTH
|
|
125
|
+
end
|
|
126
|
+
|
|
127
|
+
# A valid list (1 to MAX_RELAYS addresses in their one spelling, none twice), else nil.
|
|
128
|
+
def parse_list(value)
|
|
129
|
+
return nil unless value.is_a?(Array) && value.size.between?(1, Protocol::MAX_RELAYS)
|
|
130
|
+
|
|
131
|
+
list = []
|
|
132
|
+
value.each do |item|
|
|
133
|
+
return nil unless url?(item) && !list.include?(item)
|
|
134
|
+
|
|
135
|
+
list << item
|
|
136
|
+
end
|
|
137
|
+
list.freeze
|
|
138
|
+
end
|
|
139
|
+
|
|
140
|
+
# The list of a key's part: hosts without `wss://`, joined by commas, in base64url.
|
|
141
|
+
def decode_list(text)
|
|
142
|
+
bytes = Base64.decode_url(text)
|
|
143
|
+
return nil if bytes.nil? || bytes.empty?
|
|
144
|
+
|
|
145
|
+
# Strict UTF-8: a byte order mark stays U+FEFF, which no host holds (SPEC.md §8).
|
|
146
|
+
decoded = Text.decode_utf8(bytes)
|
|
147
|
+
return nil if decoded.nil?
|
|
148
|
+
|
|
149
|
+
parse_list(decoded.split(",", -1).map { |host| PREFIX + host })
|
|
150
|
+
end
|
|
151
|
+
|
|
152
|
+
# The part of a key or a checkout link naming `relays`.
|
|
153
|
+
def encode_list(relays)
|
|
154
|
+
Base64.encode_url(relays.map { |relay| relay.delete_prefix(PREFIX) }.join(",").b)
|
|
155
|
+
end
|
|
156
|
+
|
|
157
|
+
def same_list?(first, second)
|
|
158
|
+
first.size == second.size && first.zip(second).all? { |a, b| a == b }
|
|
159
|
+
end
|
|
160
|
+
|
|
161
|
+
# Where the relay address `url` connects (a relay address, or the explorer's WebSocket).
|
|
162
|
+
def endpoint(url)
|
|
163
|
+
match = %r{\Awss://([^/:]+)(?::([0-9]{1,5}))?(/.*)?\z}m.match(url)
|
|
164
|
+
raise ArgumentError, "Not a wss:// address" unless match
|
|
165
|
+
|
|
166
|
+
Endpoint.new(host: match[1], port: match[2] ? match[2].to_i : 443,
|
|
167
|
+
target: Urls.request_target(match[3] || "/"))
|
|
168
|
+
end
|
|
169
|
+
end
|
|
170
|
+
end
|
|
171
|
+
end
|
|
172
|
+
end
|
|
@@ -0,0 +1,247 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Sattleme
|
|
4
|
+
module Checkout
|
|
5
|
+
# What the gem gives and takes (SPEC.md §3). Each is an immutable value whose JSON is exactly
|
|
6
|
+
# the specification's, whatever the language: an invoice stored by one implementation is
|
|
7
|
+
# checked by another. Every amount is a string of digits (sats, minor units), never a float;
|
|
8
|
+
# every date an Integer of Unix seconds; an absent value nil (JSON null). Readers are Ruby's
|
|
9
|
+
# names (`payment_hash`); `to_h`, `as_json` and `to_json` give the JSON's (`paymentHash`).
|
|
10
|
+
module Model
|
|
11
|
+
# The JSON names of a model's members, in the specification's order.
|
|
12
|
+
def self.json_names(members)
|
|
13
|
+
members.to_h { |member| [member, member.to_s.gsub(/_([a-z0-9])/) { Regexp.last_match(1).upcase }] }
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
# What every model gives: its JSON (a Hash with the specification's names, JSON text).
|
|
17
|
+
module Json
|
|
18
|
+
def to_h
|
|
19
|
+
self.class::JSON_NAMES.to_h do |member, name|
|
|
20
|
+
value = public_send(member)
|
|
21
|
+
[name, value.respond_to?(:as_json) && !value.is_a?(Array) ? value.as_json : value]
|
|
22
|
+
end
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
def as_json(*)
|
|
26
|
+
to_h
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
def to_json(*)
|
|
30
|
+
Internal::Json.generate(to_h)
|
|
31
|
+
end
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
# What every model reads: a Hash of its JSON (String or Symbol names), or JSON text. Every
|
|
35
|
+
# member is there, null or not (SPEC.md §2: an absent member is never null): ArgumentError
|
|
36
|
+
# names one that is not.
|
|
37
|
+
module Reader
|
|
38
|
+
def from_json(text)
|
|
39
|
+
from_h(Internal::Json.parse(text))
|
|
40
|
+
rescue Internal::Json::Invalid
|
|
41
|
+
raise ArgumentError, "Not JSON"
|
|
42
|
+
end
|
|
43
|
+
|
|
44
|
+
def from_h(hash)
|
|
45
|
+
new(**Model.members(hash, self::JSON_NAMES))
|
|
46
|
+
end
|
|
47
|
+
end
|
|
48
|
+
|
|
49
|
+
# The members of `hash` named `names` (member → JSON name), each by its JSON name as a String
|
|
50
|
+
# or a Symbol (Model.member). ArgumentError when `hash` is no Hash, or lacks one of them but
|
|
51
|
+
# those of `optional`, which read as nil when absent.
|
|
52
|
+
def self.members(hash, names, optional = [])
|
|
53
|
+
raise ArgumentError, "Expected a Hash" unless hash.is_a?(Hash)
|
|
54
|
+
|
|
55
|
+
names.to_h do |member, name|
|
|
56
|
+
unless hash.key?(name) || hash.key?(name.to_sym) || optional.include?(member)
|
|
57
|
+
raise ArgumentError, "No #{name}: a member is null only when it is there (SPEC.md §2)"
|
|
58
|
+
end
|
|
59
|
+
|
|
60
|
+
[member, member(hash, name)]
|
|
61
|
+
end
|
|
62
|
+
end
|
|
63
|
+
|
|
64
|
+
# A member of a Hash by its JSON name, as a String or a Symbol (nil when absent). A JSON
|
|
65
|
+
# number of an integer, which JSON text reads as a Float, is the Integer it writes: every
|
|
66
|
+
# number of a model is one (dates, digits, confirmations).
|
|
67
|
+
def self.member(hash, name)
|
|
68
|
+
value = hash.key?(name) ? hash[name] : hash[name.to_sym]
|
|
69
|
+
value.is_a?(Float) && Internal::Json.safe_integer?(value) ? value.to_i : value
|
|
70
|
+
end
|
|
71
|
+
|
|
72
|
+
# A model class: its members' JSON names, its JSON, its reading.
|
|
73
|
+
def self.define(*members, &)
|
|
74
|
+
Data.define(*members) do
|
|
75
|
+
const_set(:JSON_NAMES, Model.json_names(members).freeze)
|
|
76
|
+
include Model::Json
|
|
77
|
+
extend Model::Reader
|
|
78
|
+
|
|
79
|
+
class_eval(&) if block_given?
|
|
80
|
+
end
|
|
81
|
+
end
|
|
82
|
+
end
|
|
83
|
+
|
|
84
|
+
# A price in a currency: `minor` units with `digits` decimals ("2450", 2, "CHF" is 24.50 CHF).
|
|
85
|
+
# `amount` is written with exactly `digits` decimals.
|
|
86
|
+
Fiat = Model.define(:amount, :currency, :minor, :digits)
|
|
87
|
+
|
|
88
|
+
# The rate an amount in a currency was converted at: its source ("coingecko", "mempool",
|
|
89
|
+
# "opennode", "coinbase", "kraken" or "custom"), the price of one bitcoin (an exact decimal
|
|
90
|
+
# string), when the gem read it, and when the source says it was set (nil when it does not).
|
|
91
|
+
RateInfo = Model.define(:source, :currency, :price, :fetched_at, :time)
|
|
92
|
+
|
|
93
|
+
# The invoice of an order (create_invoice): store it with the order. The shop's page needs
|
|
94
|
+
# `payment_uri` (or `bolt11` and `onchain_address`) and `expires_at` only; with a checkout
|
|
95
|
+
# link, the shop sends its customer to `checkout_url` instead. At least one of `bolt11` and
|
|
96
|
+
# `onchain_address` is not nil. Read back (from_json, from_h), every member is there but
|
|
97
|
+
# `onchainAddress`, which an invoice stored without an address may leave out (SPEC.md §22).
|
|
98
|
+
Invoice = Model.define(:ref, :bolt11, :payment_hash, :verify_url, :onchain_address, :payment_uri,
|
|
99
|
+
:sats, :fiat, :rate, :note, :created_at, :expires_at, :checkout_url,
|
|
100
|
+
:redirect_url) do
|
|
101
|
+
def self.from_h(hash)
|
|
102
|
+
fields = Model.members(hash, self::JSON_NAMES, %i[onchain_address])
|
|
103
|
+
fields[:fiat] = Fiat.from_h(fields[:fiat]) if fields[:fiat].is_a?(Hash)
|
|
104
|
+
fields[:rate] = RateInfo.from_h(fields[:rate]) if fields[:rate].is_a?(Hash)
|
|
105
|
+
new(**fields)
|
|
106
|
+
end
|
|
107
|
+
end
|
|
108
|
+
|
|
109
|
+
# An invoice declared for an order, as the merchant's registry holds it (find_invoices,
|
|
110
|
+
# InvoicePendingError, OrderPaidError): the members of an Invoice but `rate`, `checkout_url` and
|
|
111
|
+
# `redirect_url`, which only the shop keeps, and `declared_at`: the date of its declaration
|
|
112
|
+
# (without `bolt11`, of its lease's mark). Read back, every member is there but
|
|
113
|
+
# `onchainAddress`, as an Invoice's.
|
|
114
|
+
DeclaredInvoice = Model.define(:ref, :bolt11, :payment_hash, :verify_url, :onchain_address,
|
|
115
|
+
:payment_uri, :sats, :fiat, :note, :created_at, :expires_at,
|
|
116
|
+
:declared_at) do
|
|
117
|
+
def self.from_h(hash)
|
|
118
|
+
fields = Model.members(hash, self::JSON_NAMES, %i[onchain_address])
|
|
119
|
+
fields[:fiat] = Fiat.from_h(fields[:fiat]) if fields[:fiat].is_a?(Hash)
|
|
120
|
+
new(**fields)
|
|
121
|
+
end
|
|
122
|
+
end
|
|
123
|
+
|
|
124
|
+
# What is left to pay of an invoice underpaid on-chain (PaymentStatus `open`): `sats`, a
|
|
125
|
+
# Lightning invoice of exactly that, declared for the order (nil while there is none), and what
|
|
126
|
+
# to show as one QR code (`bitcoin:…` from 1 000 sats, `lightning:…` below; nil with neither).
|
|
127
|
+
Rest = Model.define(:sats, :bolt11, :payment_uri)
|
|
128
|
+
|
|
129
|
+
# Where the payment of an invoice is (SPEC.md §3): `state` "open", "unconfirmed", "paid" or
|
|
130
|
+
# "expired", and the members of that state (the others nil). `payment_method` is the JSON's
|
|
131
|
+
# `method`: "lightning", "onchain" or "mixed", of a paid invoice.
|
|
132
|
+
#
|
|
133
|
+
# - open: not paid yet; it may still be, until `expires_at`, and is reported up to 120 s later.
|
|
134
|
+
# `received` above "0": an on-chain underpayment, whose `rest` is still payable.
|
|
135
|
+
# - unconfirmed: enough was received in time (the amount, or less within the merchant's
|
|
136
|
+
# tolerance or down to what the merchant accepted), on-chain by `txids`, not 3 confirmations
|
|
137
|
+
# deep yet: not paid yet.
|
|
138
|
+
# - paid in Lightning: `preimage`, the proof of payment, to keep with the order; on-chain:
|
|
139
|
+
# `received` and `txids`; mixed: `received`, `txids` and the `preimages` of the invoices of
|
|
140
|
+
# the rest.
|
|
141
|
+
# - expired: never paid, for good. `received` above "0": the merchant received that much, less
|
|
142
|
+
# than the amount (settle it with the customer).
|
|
143
|
+
class PaymentStatus < Data.define(:state, :payment_method, :expires_at, :received, :rest,
|
|
144
|
+
:confirmations, :txids, :preimage, :preimages)
|
|
145
|
+
STATES = %w[open unconfirmed paid expired].freeze
|
|
146
|
+
METHODS = %w[lightning onchain mixed].freeze
|
|
147
|
+
|
|
148
|
+
# The JSON members of each state, in the specification's order.
|
|
149
|
+
MEMBERS = {
|
|
150
|
+
"open" => %i[state expires_at received rest],
|
|
151
|
+
"unconfirmed" => %i[state received confirmations txids],
|
|
152
|
+
"lightning" => %i[state payment_method preimage],
|
|
153
|
+
"onchain" => %i[state payment_method received txids],
|
|
154
|
+
"mixed" => %i[state payment_method received txids preimages],
|
|
155
|
+
"expired" => %i[state received]
|
|
156
|
+
}.freeze
|
|
157
|
+
JSON_NAMES = { state: "state", payment_method: "method", expires_at: "expiresAt",
|
|
158
|
+
received: "received", rest: "rest", confirmations: "confirmations",
|
|
159
|
+
txids: "txids", preimage: "preimage", preimages: "preimages" }.freeze
|
|
160
|
+
|
|
161
|
+
def self.open(expires_at:, received:, rest:)
|
|
162
|
+
new(state: "open", expires_at: expires_at, received: received, rest: rest)
|
|
163
|
+
end
|
|
164
|
+
|
|
165
|
+
def self.unconfirmed(received:, confirmations:, txids:)
|
|
166
|
+
new(state: "unconfirmed", received: received, confirmations: confirmations, txids: txids)
|
|
167
|
+
end
|
|
168
|
+
|
|
169
|
+
def self.paid_lightning(preimage)
|
|
170
|
+
new(state: "paid", payment_method: "lightning", preimage: preimage)
|
|
171
|
+
end
|
|
172
|
+
|
|
173
|
+
def self.paid_onchain(received:, txids:)
|
|
174
|
+
new(state: "paid", payment_method: "onchain", received: received, txids: txids)
|
|
175
|
+
end
|
|
176
|
+
|
|
177
|
+
def self.paid_mixed(received:, txids:, preimages:)
|
|
178
|
+
new(state: "paid", payment_method: "mixed", received: received, txids: txids, preimages: preimages)
|
|
179
|
+
end
|
|
180
|
+
|
|
181
|
+
def self.expired(received:)
|
|
182
|
+
new(state: "expired", received: received)
|
|
183
|
+
end
|
|
184
|
+
|
|
185
|
+
# The status a Hash of its JSON gives: the members of its state, each there (SPEC.md §2).
|
|
186
|
+
def self.from_h(hash)
|
|
187
|
+
raise ArgumentError, "Expected a Hash" unless hash.is_a?(Hash)
|
|
188
|
+
|
|
189
|
+
state = Model.member(hash, "state")
|
|
190
|
+
method = Model.member(hash, "method")
|
|
191
|
+
members = if state == "paid"
|
|
192
|
+
MEMBERS[method] if METHODS.include?(method)
|
|
193
|
+
elsif STATES.include?(state)
|
|
194
|
+
MEMBERS[state]
|
|
195
|
+
end
|
|
196
|
+
raise ArgumentError, "Not a PaymentStatus: no state of SPEC.md §3, or no method of a paid one" unless members
|
|
197
|
+
|
|
198
|
+
fields = Model.members(hash, JSON_NAMES.slice(*members))
|
|
199
|
+
fields[:rest] = Rest.from_h(fields[:rest]) if fields[:rest].is_a?(Hash)
|
|
200
|
+
new(**fields)
|
|
201
|
+
end
|
|
202
|
+
|
|
203
|
+
def self.from_json(text)
|
|
204
|
+
from_h(Internal::Json.parse(text))
|
|
205
|
+
rescue Internal::Json::Invalid
|
|
206
|
+
raise ArgumentError, "Not JSON"
|
|
207
|
+
end
|
|
208
|
+
|
|
209
|
+
def initialize(state:, payment_method: nil, expires_at: nil, received: nil, rest: nil, confirmations: nil,
|
|
210
|
+
txids: nil, preimage: nil, preimages: nil)
|
|
211
|
+
super(state: state, payment_method: payment_method, expires_at: expires_at, received: received,
|
|
212
|
+
rest: rest, confirmations: confirmations, txids: txids&.dup&.freeze, preimage: preimage,
|
|
213
|
+
preimages: preimages&.dup&.freeze)
|
|
214
|
+
end
|
|
215
|
+
|
|
216
|
+
def open? = state == "open"
|
|
217
|
+
def unconfirmed? = state == "unconfirmed"
|
|
218
|
+
def paid? = state == "paid"
|
|
219
|
+
def expired? = state == "expired"
|
|
220
|
+
|
|
221
|
+
# Whether it is final: paid or expired.
|
|
222
|
+
def final? = paid? || expired?
|
|
223
|
+
|
|
224
|
+
# The JSON of this state's members.
|
|
225
|
+
def to_h
|
|
226
|
+
MEMBERS.fetch(paid? ? payment_method : state).to_h do |member|
|
|
227
|
+
value = public_send(member)
|
|
228
|
+
[JSON_NAMES.fetch(member), value.is_a?(Rest) ? value.to_h : value]
|
|
229
|
+
end
|
|
230
|
+
end
|
|
231
|
+
|
|
232
|
+
def as_json(*)
|
|
233
|
+
to_h
|
|
234
|
+
end
|
|
235
|
+
|
|
236
|
+
def to_json(*)
|
|
237
|
+
Internal::Json.generate(to_h)
|
|
238
|
+
end
|
|
239
|
+
end
|
|
240
|
+
|
|
241
|
+
# The merchant, as the integration's channel, Lightning address and lot give it: its business's
|
|
242
|
+
# name, the bounds of its Lightning address in sats, and how many deposit addresses the
|
|
243
|
+
# integration's lot holds, leased or not (0: no on-chain payment yet). Never its Lightning
|
|
244
|
+
# address.
|
|
245
|
+
Merchant = Model.define(:business_name, :min_sats, :max_sats, :onchain_addresses)
|
|
246
|
+
end
|
|
247
|
+
end
|