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,147 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Sattleme
|
|
4
|
+
module Checkout
|
|
5
|
+
module Internal
|
|
6
|
+
# The integration's channel (SPEC.md §14): one record signed by the team, which the merchant's
|
|
7
|
+
# app publishes on the account's relays and which only the integration's key opens. It gives
|
|
8
|
+
# the merchant's Lightning address, registry and relays, or says that the integration was
|
|
9
|
+
# removed.
|
|
10
|
+
module Channels
|
|
11
|
+
# The channel, as far as the server uses it. Active: the business's name, the Lightning
|
|
12
|
+
# address (never shown to the integrator by the app: the gem never returns it either), the
|
|
13
|
+
# registry, the account's relays when the channel names them (nil otherwise), the web app of
|
|
14
|
+
# checkout links (nil when none or an invalid one), the merchant's tolerance (0 when none),
|
|
15
|
+
# the merchant's Bitcoin node (BitcoinNodes::Node, nil when none or an invalid one), and the
|
|
16
|
+
# record's `created_at`. Removed: `status` :removed and `updated_at`.
|
|
17
|
+
Channel = Data.define(:status, :business_name, :lightning_address, :registry_key, :registry_reader, :relays,
|
|
18
|
+
:checkout_origin, :tolerance, :bitcoin_node, :updated_at) do
|
|
19
|
+
# It holds the registry key.
|
|
20
|
+
include Redacted
|
|
21
|
+
|
|
22
|
+
def active? = status == :active
|
|
23
|
+
|
|
24
|
+
def inspect = "#<Channel #{status} updated_at=#{updated_at}>"
|
|
25
|
+
alias_method :to_s, :inspect
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
# What the relays of one list hold: :found (with the channel), :not_found (with whether a
|
|
29
|
+
# copy of a newer format was seen), or :unreachable (fewer than a majority answered).
|
|
30
|
+
Lookup = Data.define(:status, :channel, :newer) do
|
|
31
|
+
def unreachable? = status == :unreachable
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
# The channel, active, and the relays it was read on.
|
|
35
|
+
Read = Data.define(:channel, :relays)
|
|
36
|
+
|
|
37
|
+
module_function
|
|
38
|
+
|
|
39
|
+
# A channel's value, as the merchant's app reads it, for what the server uses: version 1,
|
|
40
|
+
# `updatedAt` = `created_at`, then removed, or active with the role of an integration, the
|
|
41
|
+
# business's name, a Lightning address, the registry and an optional valid relay list, the
|
|
42
|
+
# web app of checkout links when it gives a valid one (an invalid one gives none, and leaves
|
|
43
|
+
# the channel valid), the merchant's tolerance of on-chain underpayments (absent or invalid:
|
|
44
|
+
# none, the channel valid), and the merchant's Bitcoin node (`bitcoinNode`, SPEC.md §31:
|
|
45
|
+
# absent or invalid in any way, none, the server reads mempool.space, the channel valid; a
|
|
46
|
+
# field a reader that does not know it ignores, no new version of the channel). The other
|
|
47
|
+
# fields are the app's own and not read here, so that a change to them never stops a shop's
|
|
48
|
+
# server. Raises Records::Newer for a newer version.
|
|
49
|
+
def parse(value, created_at)
|
|
50
|
+
return nil unless value.is_a?(Hash)
|
|
51
|
+
|
|
52
|
+
version = value["version"]
|
|
53
|
+
raise Records::Newer if Json.number?(version) && version > Protocol::CHANNEL_VERSION
|
|
54
|
+
return nil unless version == Protocol::CHANNEL_VERSION && value["updatedAt"] == created_at
|
|
55
|
+
return removed(created_at) if value["status"] == "removed"
|
|
56
|
+
return nil unless value["status"] == "active" && value["role"] == "integration"
|
|
57
|
+
|
|
58
|
+
profile = value["profile"]
|
|
59
|
+
return nil unless profile.is_a?(Hash)
|
|
60
|
+
|
|
61
|
+
business_name, lightning_address = profile.values_at("businessName", "lightningAddress")
|
|
62
|
+
return nil unless CheckoutLinks.business_name?(business_name)
|
|
63
|
+
return nil unless Lnurl.address_url(lightning_address)
|
|
64
|
+
# The reading key must be a point of the curve: every declaration's inside is sealed to it.
|
|
65
|
+
return nil unless Text.hex32?(value["registryKey"]) && Events.public_key?(value["registryReader"])
|
|
66
|
+
|
|
67
|
+
relays = value.key?("relays") ? Relays.parse_list(value["relays"]) : nil
|
|
68
|
+
return nil if value.key?("relays") && relays.nil?
|
|
69
|
+
|
|
70
|
+
Channel.new(status: :active, business_name: business_name, lightning_address: lightning_address,
|
|
71
|
+
registry_key: value["registryKey"], registry_reader: value["registryReader"], relays: relays,
|
|
72
|
+
checkout_origin: CheckoutLinks.origin_of(value["checkout"]),
|
|
73
|
+
# Absent or invalid, none: the channel stays valid.
|
|
74
|
+
tolerance: Tolerance.of(value["onchainTolerance"]),
|
|
75
|
+
bitcoin_node: BitcoinNodes.of(value["bitcoinNode"]), updated_at: created_at)
|
|
76
|
+
end
|
|
77
|
+
|
|
78
|
+
def removed(created_at)
|
|
79
|
+
Channel.new(status: :removed, business_name: nil, lightning_address: nil, registry_key: nil,
|
|
80
|
+
registry_reader: nil, relays: nil, checkout_origin: nil, tolerance: 0, bitcoin_node: nil,
|
|
81
|
+
updated_at: created_at)
|
|
82
|
+
end
|
|
83
|
+
|
|
84
|
+
# The newest copy of the integration's channel on `relays`, signed by its team: every relay
|
|
85
|
+
# at once, read as soon as a majority of them answered (the app publishes the channel, and
|
|
86
|
+
# its removal, on a majority of them: any two majorities share a relay). Raises only
|
|
87
|
+
# Cancelled.
|
|
88
|
+
def lookup(pool, key, relays, random:, logger:, stop:)
|
|
89
|
+
address, encryption_key = Derive.channel_keys(key.key)
|
|
90
|
+
team = key.team_public_key
|
|
91
|
+
filter = { "kinds" => [Protocol::RECORD_KIND], "authors" => [team], "#d" => [address] }
|
|
92
|
+
keep = Records.that_open { |_, author| author == team ? encryption_key : nil }
|
|
93
|
+
# A relay that asks to authenticate (NIP-42) gets a key of this read alone.
|
|
94
|
+
result = pool.query_relays(relays, filter, random.call(32), paged: false, stop: stop, keep: keep)
|
|
95
|
+
return Lookup.new(status: :unreachable, channel: nil, newer: false) unless result.answered
|
|
96
|
+
|
|
97
|
+
found = Records.newest(result.answers) do |value|
|
|
98
|
+
Records.open(value, type: Protocol::CHANNEL_RECORD_TYPE, encryption_key: encryption_key, address: address,
|
|
99
|
+
author: team) { |channel, event| parse(channel, event.created_at) }
|
|
100
|
+
end
|
|
101
|
+
if found.newest_unreadable && found.newest_unreadable > (found.event&.created_at || -1)
|
|
102
|
+
logger.warn("sattleme-checkout: the integration's channel has a newer format than this version " \
|
|
103
|
+
"reads; update the gem")
|
|
104
|
+
end
|
|
105
|
+
return Lookup.new(status: :found, channel: found.value, newer: false) if found.value
|
|
106
|
+
|
|
107
|
+
Lookup.new(status: :not_found, channel: nil, newer: !found.newest_unreadable.nil?)
|
|
108
|
+
end
|
|
109
|
+
|
|
110
|
+
# The integration's channel, active, on the account's current relays: read on the key's
|
|
111
|
+
# relays, then where the channel says the account moved (MAX_RELAY_MOVES at most), since only
|
|
112
|
+
# the new list gets its later changes, its removal included. RevokedError when it is
|
|
113
|
+
# removed, NetworkError when fewer than a majority of a list answered, ChannelNotFoundError
|
|
114
|
+
# when a majority of the key's relays answered without it.
|
|
115
|
+
def read(pool, key, random:, logger:, stop:)
|
|
116
|
+
relays = key.relays
|
|
117
|
+
found = lookup(pool, key, relays, random: random, logger: logger, stop: stop)
|
|
118
|
+
raise NetworkError, "Fewer than a majority of the integration key's relays answered" if found.unreachable?
|
|
119
|
+
raise ChannelNotFoundError.new(newer_format: found.newer) if found.status == :not_found
|
|
120
|
+
|
|
121
|
+
channel = found.channel
|
|
122
|
+
Protocol::MAX_RELAY_MOVES.times do
|
|
123
|
+
break unless channel.active? && channel.relays && !Relays.same_list?(channel.relays, relays)
|
|
124
|
+
|
|
125
|
+
following = channel.relays
|
|
126
|
+
found = lookup(pool, key, following, random: random, logger: logger, stop: stop)
|
|
127
|
+
raise NetworkError, "Fewer than a majority of the merchant's relays answered" if found.unreachable?
|
|
128
|
+
# Not copied there (yet): the channel read so far stands, and the declarations still go to
|
|
129
|
+
# the account's relays it names.
|
|
130
|
+
break if found.status == :not_found
|
|
131
|
+
|
|
132
|
+
channel = found.channel
|
|
133
|
+
relays = following
|
|
134
|
+
end
|
|
135
|
+
raise RevokedError unless channel.active?
|
|
136
|
+
|
|
137
|
+
Read.new(channel: channel, relays: relays)
|
|
138
|
+
end
|
|
139
|
+
|
|
140
|
+
# The merchant's relays, where declarations go: the channel's, else the key's.
|
|
141
|
+
def merchant_relays(channel, key)
|
|
142
|
+
channel.relays || key.relays
|
|
143
|
+
end
|
|
144
|
+
end
|
|
145
|
+
end
|
|
146
|
+
end
|
|
147
|
+
end
|
|
@@ -0,0 +1,294 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Sattleme
|
|
4
|
+
module Checkout
|
|
5
|
+
module Internal
|
|
6
|
+
# Checkout links (SPEC.md §30): a URL of the merchant's web app,
|
|
7
|
+
# `<origin>/pay#c.<author>.<key>.<relays>`, whose page reads the invoice from the merchant's
|
|
8
|
+
# relays, shows it, checks its payment from the customer's browser and, once it is paid, sends
|
|
9
|
+
# the customer to the order's redirect URL. The record is sealed with a key of the link alone
|
|
10
|
+
# (in the URL's fragment, which browsers never send to the web app's host) and signed by an
|
|
11
|
+
# author of the link alone, whose secret is forgotten once it is published: nobody, the
|
|
12
|
+
# integrator included, can change a link afterwards. The gem writes links; it also reads them
|
|
13
|
+
# (fragments, records), as the page does, for the conformance vectors.
|
|
14
|
+
module CheckoutLinks
|
|
15
|
+
# The authority of an https URL as written: its host and its port.
|
|
16
|
+
HTTPS_AUTHORITY = %r{\Ahttps://([^/?#]*)}
|
|
17
|
+
APP_URL = %r{\A([a-z][a-z0-9+.-]*)://}
|
|
18
|
+
# The first part of a newer link's fragment: 1 to 8 characters `a–z 0–9`.
|
|
19
|
+
MARKER = /\A[a-z0-9]{1,8}\z/
|
|
20
|
+
|
|
21
|
+
# A link's keys (from its link key, 32 bytes): its `d` tag (hex) and its NIP-44
|
|
22
|
+
# conversation key.
|
|
23
|
+
Keys = Data.define(:address, :encryption_key) do
|
|
24
|
+
include Redacted
|
|
25
|
+
|
|
26
|
+
def inspect = "#<CheckoutLinks::Keys address=#{address}>"
|
|
27
|
+
alias_method :to_s, :inspect
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
# A new link, before it is published: its URL, its event, and its author's secret, which
|
|
31
|
+
# answers the relays' authentication while the event is published, then is dropped: never
|
|
32
|
+
# stored, returned to the caller nor logged.
|
|
33
|
+
NewLink = Data.define(:url, :event, :secret_key) do
|
|
34
|
+
include Redacted
|
|
35
|
+
|
|
36
|
+
def inspect = "#<CheckoutLinks::NewLink event=#{event.id}>"
|
|
37
|
+
alias_method :to_s, :inspect
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
# What a fragment names (SPEC.md §30, "Reading the fragment"): a link (its author, link key
|
|
41
|
+
# and relays), a link of a newer format (the page asks to be updated), an invalid link, or
|
|
42
|
+
# another link of the page.
|
|
43
|
+
Fragment = Data.define(:status, :author, :link_key, :relays) do
|
|
44
|
+
include Redacted
|
|
45
|
+
|
|
46
|
+
def inspect = "#<CheckoutLinks::Fragment #{status}>"
|
|
47
|
+
alias_method :to_s, :inspect
|
|
48
|
+
end
|
|
49
|
+
|
|
50
|
+
# A link's record, checked: what the page shows. `rest` is [rest author, rest key] of a
|
|
51
|
+
# record of version 2 that carries them, else nil; `bitcoin_node` the merchant's Bitcoin node
|
|
52
|
+
# the page reads the chain from, with its fallback (BitcoinNodes::RecordNode) of a record of
|
|
53
|
+
# version 2 whose `bitcoinNode` is valid, else nil (mempool.space).
|
|
54
|
+
Record = Data.define(:business_name, :invoice, :payment_hash, :verify_url, :onchain_address, :sats,
|
|
55
|
+
:created_at, :expires_at, :fiat, :note, :redirect_url, :redirect_shown, :rest,
|
|
56
|
+
:bitcoin_node)
|
|
57
|
+
|
|
58
|
+
module_function
|
|
59
|
+
|
|
60
|
+
# Whether `value` is a checkout origin: `https://`, a host as in a relay address, an
|
|
61
|
+
# optional port, nothing else (SPEC.md §30, "The web app's origin").
|
|
62
|
+
def origin?(value)
|
|
63
|
+
!Urls.https_url(value).nil? && value.match?(%r{\Ahttps://[^/?#]*\z})
|
|
64
|
+
end
|
|
65
|
+
|
|
66
|
+
# The origin a channel's `checkout` field gives: nil when it gives none or an invalid one.
|
|
67
|
+
def origin_of(checkout)
|
|
68
|
+
checkout.is_a?(Hash) && origin?(checkout["origin"]) ? checkout["origin"] : nil
|
|
69
|
+
end
|
|
70
|
+
|
|
71
|
+
# What the page shows of a redirect URL ("Redirecting to …"): the host and port of an https
|
|
72
|
+
# URL as written, the scheme and `://` of an app's link. Nil when `value` is not a redirect
|
|
73
|
+
# URL (SPEC.md §30, "Redirect URLs"): nothing is repaired.
|
|
74
|
+
def redirect_shown(value)
|
|
75
|
+
unless Text.match?(value, Urls::URL_CHARACTERS) && value.length <= Protocol::MAX_REDIRECT_URL_LENGTH
|
|
76
|
+
return nil
|
|
77
|
+
end
|
|
78
|
+
return Urls.https_url(value) ? HTTPS_AUTHORITY.match(value)[1] : nil if value.start_with?("https://")
|
|
79
|
+
|
|
80
|
+
app = APP_URL.match(value)
|
|
81
|
+
return nil unless app
|
|
82
|
+
|
|
83
|
+
scheme = app[1]
|
|
84
|
+
if scheme.length > Protocol::MAX_REDIRECT_SCHEME_LENGTH || scheme == "https" ||
|
|
85
|
+
Protocol::REFUSED_REDIRECT_SCHEMES.include?(scheme)
|
|
86
|
+
return nil
|
|
87
|
+
end
|
|
88
|
+
|
|
89
|
+
"#{scheme}://"
|
|
90
|
+
end
|
|
91
|
+
|
|
92
|
+
def keys(link_key)
|
|
93
|
+
Keys.new(address: Text.hex(Derive.key(link_key, Protocol::CHECKOUT_SALT, Protocol::CHECKOUT_ADDRESS_INFO)),
|
|
94
|
+
encryption_key: Derive.key(link_key, Protocol::CHECKOUT_SALT, Protocol::CHECKOUT_ENCRYPTION_INFO))
|
|
95
|
+
end
|
|
96
|
+
|
|
97
|
+
# A link author's secret: 32 random bytes, drawn again until they are a secp256k1 secret key.
|
|
98
|
+
def author_secret(random)
|
|
99
|
+
loop do
|
|
100
|
+
secret = random.call(Protocol::KEY_BYTES)
|
|
101
|
+
return secret if Secp256k1.valid_secret?(secret)
|
|
102
|
+
end
|
|
103
|
+
end
|
|
104
|
+
|
|
105
|
+
# The URL of a link: `<origin>/pay#c.<author>.<key>.<relays>`.
|
|
106
|
+
def url(origin, author, link_key, relays)
|
|
107
|
+
parts = [Protocol::CHECKOUT_MARKER, Base64.encode_url(Text.hex_bytes(author)), Base64.encode_url(link_key),
|
|
108
|
+
Relays.encode_list(relays)]
|
|
109
|
+
"#{origin}#{Protocol::CHECKOUT_PATH}##{parts.join(".")}"
|
|
110
|
+
end
|
|
111
|
+
|
|
112
|
+
# What a link's record holds of an order's deposit address (version 2): the address, the
|
|
113
|
+
# invoice's amount and dates, its rest author and rest key (Derive::RestKeys), and the node
|
|
114
|
+
# its page reads (BitcoinNodes::RecordNode, nil: the field absent).
|
|
115
|
+
Onchain = Data.define(:address, :sats, :created_at, :expires_at, :rest, :bitcoin_node)
|
|
116
|
+
|
|
117
|
+
# What a link's record holds (SPEC.md §30, "The record"), as JSON: version 1 for an invoice
|
|
118
|
+
# without an address (`onchain` nil), version 2 for one with an address (`onchain`, an
|
|
119
|
+
# Onchain; its invoice and verify URL nil for an order paid on-chain only). `bitcoinNode` is
|
|
120
|
+
# the node of BitcoinNodes.record_node_for (the channel's with `payers` true, its `url` and
|
|
121
|
+
# `fallback`), absent without one: never in version 1.
|
|
122
|
+
def value(business_name:, invoice:, verify_url:, fiat:, note:, redirect_url:, onchain:)
|
|
123
|
+
if onchain.nil?
|
|
124
|
+
return { "version" => Protocol::CHECKOUT_VERSION, "businessName" => business_name, "invoice" => invoice,
|
|
125
|
+
"verifyUrl" => verify_url, "fiat" => fiat, "note" => note, "redirectUrl" => redirect_url }
|
|
126
|
+
end
|
|
127
|
+
|
|
128
|
+
value = { "version" => Protocol::CHECKOUT_ONCHAIN_VERSION, "businessName" => business_name,
|
|
129
|
+
"invoice" => invoice, "verifyUrl" => verify_url, "onchainAddress" => onchain.address,
|
|
130
|
+
"sats" => onchain.sats, "createdAt" => onchain.created_at, "expiresAt" => onchain.expires_at,
|
|
131
|
+
"fiat" => fiat, "note" => note, "redirectUrl" => redirect_url }
|
|
132
|
+
rest = onchain.rest
|
|
133
|
+
value.merge!("restAuthor" => rest.author, "restKey" => Text.hex(rest.key)) if rest
|
|
134
|
+
value["bitcoinNode"] = onchain.bitcoin_node.to_h if onchain.bitcoin_node
|
|
135
|
+
value
|
|
136
|
+
end
|
|
137
|
+
|
|
138
|
+
# A new link to the invoice `value` holds, ending `expires_at` (Unix seconds): a fresh link
|
|
139
|
+
# key and author, the record signed at `created_at`, dropped by the relays a week after the
|
|
140
|
+
# invoice's end, and the URL on `origin` naming `relays` (where it is to be published).
|
|
141
|
+
def new_link(origin:, relays:, value:, expires_at:, created_at:, random:)
|
|
142
|
+
link_key = random.call(Protocol::KEY_BYTES)
|
|
143
|
+
secret = author_secret(random)
|
|
144
|
+
link_keys = keys(link_key)
|
|
145
|
+
event = Records.seal(type: Protocol::CHECKOUT_RECORD_TYPE, value: value, signer: secret,
|
|
146
|
+
encryption_key: link_keys.encryption_key, address: link_keys.address,
|
|
147
|
+
created_at: created_at, random: random,
|
|
148
|
+
tags: [["expiration", (expires_at + Protocol::CHECKOUT_LIFE_SECONDS).to_s]])
|
|
149
|
+
NewLink.new(url: url(origin, Events.public_key(secret), link_key, relays), event: event, secret_key: secret)
|
|
150
|
+
end
|
|
151
|
+
|
|
152
|
+
# The link a URL's fragment (with or without "#") names.
|
|
153
|
+
def parse_fragment(fragment)
|
|
154
|
+
text = Text.well_formed?(fragment) ? fragment.delete_prefix("#") : ""
|
|
155
|
+
marker, author, key, relays, *rest = text.split(".", -1)
|
|
156
|
+
marker ||= ""
|
|
157
|
+
unless marker == Protocol::CHECKOUT_MARKER
|
|
158
|
+
return fragment_of(marker.match?(MARKER) ? :newer : :other)
|
|
159
|
+
end
|
|
160
|
+
return fragment_of(:invalid) if author.nil? || key.nil? || relays.nil? || !rest.empty?
|
|
161
|
+
|
|
162
|
+
author_bytes = Base64.decode_url(author)
|
|
163
|
+
link_key = Base64.decode_url(key)
|
|
164
|
+
list = Relays.decode_list(relays)
|
|
165
|
+
unless author_bytes&.bytesize == Protocol::KEY_BYTES && link_key&.bytesize == Protocol::KEY_BYTES && list
|
|
166
|
+
return fragment_of(:invalid)
|
|
167
|
+
end
|
|
168
|
+
|
|
169
|
+
author_hex = Text.hex(author_bytes)
|
|
170
|
+
return fragment_of(:invalid) unless Events.public_key?(author_hex)
|
|
171
|
+
|
|
172
|
+
Fragment.new(status: :link, author: author_hex, link_key: link_key, relays: list)
|
|
173
|
+
end
|
|
174
|
+
|
|
175
|
+
def fragment_of(status)
|
|
176
|
+
Fragment.new(status: status, author: nil, link_key: nil, relays: nil)
|
|
177
|
+
end
|
|
178
|
+
|
|
179
|
+
# A price as a link's record has it: nil, or `{minor, digits, currency}` with `minor` from 1,
|
|
180
|
+
# `digits` 0 to 8 and `currency` three letters other than SAT. ABSENT when invalid.
|
|
181
|
+
def price_of(value)
|
|
182
|
+
return nil if value.nil?
|
|
183
|
+
return Order::ABSENT unless value.is_a?(Hash)
|
|
184
|
+
|
|
185
|
+
minor, digits, currency = value.values_at("minor", "digits", "currency")
|
|
186
|
+
return Order::ABSENT unless Json.safe_integer?(minor) && minor >= 1
|
|
187
|
+
return Order::ABSENT unless Json.integer?(digits) && digits.between?(0, Protocol::MAX_DECLARED_DIGITS)
|
|
188
|
+
return Order::ABSENT unless Text.match?(currency, /\A[A-Z]{3}\z/) && currency != Protocol::SATS_CURRENCY
|
|
189
|
+
|
|
190
|
+
{ "minor" => minor.to_i, "digits" => digits.to_i, "currency" => currency }
|
|
191
|
+
end
|
|
192
|
+
|
|
193
|
+
# A record's value, checked as the page reads it (SPEC.md §30, "The record"): version 1 or 2.
|
|
194
|
+
# Nil when it is not valid; raises Records::Newer for a newer version.
|
|
195
|
+
def parse_value(value)
|
|
196
|
+
return nil unless value.is_a?(Hash)
|
|
197
|
+
|
|
198
|
+
version = value["version"]
|
|
199
|
+
raise Records::Newer if Json.number?(version) && version > Protocol::CHECKOUT_ONCHAIN_VERSION
|
|
200
|
+
return nil unless [Protocol::CHECKOUT_VERSION, Protocol::CHECKOUT_ONCHAIN_VERSION].include?(version)
|
|
201
|
+
return nil unless business_name?(value["businessName"])
|
|
202
|
+
|
|
203
|
+
onchain = version == Protocol::CHECKOUT_ONCHAIN_VERSION
|
|
204
|
+
invoice, verify_url = value.values_at("invoice", "verifyUrl")
|
|
205
|
+
lightning = nil
|
|
206
|
+
# Absent is not null: a record of an order paid on-chain only writes both as null.
|
|
207
|
+
if !onchain || !Json.null_member?(value, "invoice") || !Json.null_member?(value, "verifyUrl")
|
|
208
|
+
lightning = lightning_of(invoice, verify_url)
|
|
209
|
+
return nil unless lightning
|
|
210
|
+
end
|
|
211
|
+
terms = lightning
|
|
212
|
+
onchain_address = nil
|
|
213
|
+
rest = nil
|
|
214
|
+
bitcoin_node = nil
|
|
215
|
+
if onchain
|
|
216
|
+
terms = onchain_terms(value, lightning)
|
|
217
|
+
return nil unless terms
|
|
218
|
+
|
|
219
|
+
onchain_address = value["onchainAddress"]
|
|
220
|
+
rest = value.key?("restAuthor") || value.key?("restKey") ? rest_of(value) : nil
|
|
221
|
+
return nil if rest.equal?(Order::ABSENT)
|
|
222
|
+
|
|
223
|
+
# Set aside when it is not valid, the record valid all the same; ignored in version 1.
|
|
224
|
+
bitcoin_node = BitcoinNodes.record_node_of(value["bitcoinNode"])
|
|
225
|
+
end
|
|
226
|
+
# `fiat` and `redirectUrl` are null or valid: absent, the record is invalid.
|
|
227
|
+
return nil unless value.key?("fiat") && value.key?("redirectUrl")
|
|
228
|
+
|
|
229
|
+
fiat = price_of(value["fiat"])
|
|
230
|
+
return nil if fiat.equal?(Order::ABSENT)
|
|
231
|
+
|
|
232
|
+
note, redirect_url = value.values_at("note", "redirectUrl")
|
|
233
|
+
return nil unless note?(note)
|
|
234
|
+
|
|
235
|
+
shown = redirect_url.nil? ? nil : redirect_shown(redirect_url)
|
|
236
|
+
return nil if !redirect_url.nil? && shown.nil?
|
|
237
|
+
|
|
238
|
+
Record.new(business_name: value["businessName"], invoice: lightning ? invoice : nil,
|
|
239
|
+
payment_hash: lightning&.fetch(:payment_hash), verify_url: lightning ? verify_url : nil,
|
|
240
|
+
onchain_address: onchain_address, sats: terms[:sats], created_at: terms[:created_at],
|
|
241
|
+
expires_at: terms[:expires_at], fiat: fiat, note: note, redirect_url: redirect_url,
|
|
242
|
+
redirect_shown: shown, rest: rest, bitcoin_node: bitcoin_node)
|
|
243
|
+
end
|
|
244
|
+
|
|
245
|
+
def business_name?(name)
|
|
246
|
+
Text.well_formed?(name) && !Text.trim_ascii(name).empty? &&
|
|
247
|
+
Text.code_point_length(name) <= Protocol::MAX_BUSINESS_NAME_LENGTH
|
|
248
|
+
end
|
|
249
|
+
|
|
250
|
+
def note?(note)
|
|
251
|
+
Text.well_formed?(note) && !Text.control_character?(note) &&
|
|
252
|
+
Text.code_point_length(note).between?(1, Protocol::MAX_NOTE_LENGTH)
|
|
253
|
+
end
|
|
254
|
+
|
|
255
|
+
# The terms of a record's Lightning invoice: a string that decodes on network bc, with an
|
|
256
|
+
# amount above 0 and a whole number of sats, and an https verify URL. Nil otherwise.
|
|
257
|
+
def lightning_of(invoice, verify_url)
|
|
258
|
+
decoded = Text.well_formed?(invoice) ? Bolt11.decode(invoice) : nil
|
|
259
|
+
return nil unless decoded && decoded.network == "bc" && decoded.amount_msat
|
|
260
|
+
return nil unless decoded.amount_msat.positive? && (decoded.amount_msat % 1000).zero?
|
|
261
|
+
return nil unless Urls.https_url(verify_url)
|
|
262
|
+
|
|
263
|
+
{ payment_hash: decoded.payment_hash, sats: decoded.amount_msat / 1000, created_at: decoded.timestamp,
|
|
264
|
+
expires_at: decoded.expires_at }
|
|
265
|
+
end
|
|
266
|
+
|
|
267
|
+
# The terms of a record of version 2: its address, sats and dates, which its invoice's (when
|
|
268
|
+
# it has one) must equal. Nil when they are not valid.
|
|
269
|
+
def onchain_terms(value, lightning)
|
|
270
|
+
sats, created_at, expires_at = value.values_at("sats", "createdAt", "expiresAt")
|
|
271
|
+
return nil unless Addresses.deposit?(value["onchainAddress"])
|
|
272
|
+
return nil unless Json.safe_integer?(sats) && sats.to_i.between?(1, Protocol::MAX_SATS)
|
|
273
|
+
return nil unless Json.safe_integer?(created_at) && Json.safe_integer?(expires_at) && created_at < expires_at
|
|
274
|
+
|
|
275
|
+
terms = { sats: sats.to_i, created_at: created_at.to_i, expires_at: expires_at.to_i }
|
|
276
|
+
if lightning && (lightning[:sats] != terms[:sats] || lightning[:created_at] != terms[:created_at] ||
|
|
277
|
+
lightning[:expires_at] != terms[:expires_at])
|
|
278
|
+
return nil
|
|
279
|
+
end
|
|
280
|
+
|
|
281
|
+
terms
|
|
282
|
+
end
|
|
283
|
+
|
|
284
|
+
# The rest author and rest key of a record of version 2: both or neither (ABSENT otherwise).
|
|
285
|
+
def rest_of(value)
|
|
286
|
+
author, key = value.values_at("restAuthor", "restKey")
|
|
287
|
+
return Order::ABSENT unless Events.public_key?(author) && Text.hex32?(key)
|
|
288
|
+
|
|
289
|
+
[author, Text.hex_bytes(key)]
|
|
290
|
+
end
|
|
291
|
+
end
|
|
292
|
+
end
|
|
293
|
+
end
|
|
294
|
+
end
|