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,487 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "openssl"
|
|
4
|
+
require "securerandom"
|
|
5
|
+
require "socket"
|
|
6
|
+
require "io/wait"
|
|
7
|
+
|
|
8
|
+
module Sattleme
|
|
9
|
+
module Checkout
|
|
10
|
+
# Opens the TCP connection, then TLS, of a WebSocket: to `host` and `port`, certificate and
|
|
11
|
+
# host name verified against the system's store, TLS 1.2 at least, the whole bounded by
|
|
12
|
+
# `timeout` seconds: the name's resolution, the connection to its addresses (one after the
|
|
13
|
+
# other) and the TLS handshake share one deadline (SPEC.md §13: the wait for the connection is
|
|
14
|
+
# part of a request). Inject another (WebSocketClient.new(connector: …)) to go through a proxy,
|
|
15
|
+
# or to reach a relay of your own network: anything that answers `call(host, port, timeout)`
|
|
16
|
+
# with an IO (read_nonblock, write_nonblock, to_io, close).
|
|
17
|
+
class TlsConnector
|
|
18
|
+
def initialize(ssl_context: nil)
|
|
19
|
+
@ssl_context = ssl_context || default_context
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
def call(host, port, timeout)
|
|
23
|
+
deadline = Internal::Clock.now + timeout
|
|
24
|
+
handshake(connect_tcp(host, port, deadline), host, deadline)
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
# A TCP connection to `host`: its name resolved, then its addresses tried in turn, by
|
|
28
|
+
# `deadline` (Internal::Clock). Raises the last failure.
|
|
29
|
+
def connect_tcp(host, port, deadline)
|
|
30
|
+
failure = nil
|
|
31
|
+
resolve(host, port, left(deadline, "Name resolution")).each do |address|
|
|
32
|
+
return connect_to(address, left(deadline, "TCP connection"))
|
|
33
|
+
rescue IOError, SystemCallError, SocketError => e
|
|
34
|
+
failure = e
|
|
35
|
+
end
|
|
36
|
+
raise failure || SocketError.new("#{host} has no address")
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
# TLS over `tcp`, a connection that leads to `host` (a proxy's tunnel to it too), by
|
|
40
|
+
# `deadline`: the certificate and the host name verified. `tcp` is closed when it fails.
|
|
41
|
+
def handshake(tcp, host, deadline)
|
|
42
|
+
ssl = OpenSSL::SSL::SSLSocket.new(tcp, @ssl_context)
|
|
43
|
+
ssl.hostname = host
|
|
44
|
+
ssl.sync_close = true
|
|
45
|
+
loop do
|
|
46
|
+
case ssl.connect_nonblock(exception: false)
|
|
47
|
+
when :wait_readable then wait(tcp, :wait_readable, deadline)
|
|
48
|
+
when :wait_writable then wait(tcp, :wait_writable, deadline)
|
|
49
|
+
else break
|
|
50
|
+
end
|
|
51
|
+
end
|
|
52
|
+
ssl.post_connection_check(host)
|
|
53
|
+
ssl
|
|
54
|
+
rescue StandardError
|
|
55
|
+
begin
|
|
56
|
+
(ssl || tcp).close
|
|
57
|
+
rescue StandardError
|
|
58
|
+
nil
|
|
59
|
+
end
|
|
60
|
+
raise
|
|
61
|
+
end
|
|
62
|
+
|
|
63
|
+
private
|
|
64
|
+
|
|
65
|
+
# The addresses of `host` (TCP), resolved within `timeout` seconds.
|
|
66
|
+
def resolve(host, port, timeout)
|
|
67
|
+
Addrinfo.getaddrinfo(host, port, nil, :STREAM, timeout: timeout)
|
|
68
|
+
end
|
|
69
|
+
|
|
70
|
+
# A TCP connection to `address`, made within `timeout` seconds.
|
|
71
|
+
def connect_to(address, timeout)
|
|
72
|
+
address.connect(timeout: timeout)
|
|
73
|
+
end
|
|
74
|
+
|
|
75
|
+
# The seconds left before `deadline`; IO::TimeoutError (`step` timed out) when none are.
|
|
76
|
+
def left(deadline, step)
|
|
77
|
+
seconds = Internal::Clock.left(deadline)
|
|
78
|
+
raise IO::TimeoutError, "#{step} timed out" if seconds.zero?
|
|
79
|
+
|
|
80
|
+
seconds
|
|
81
|
+
end
|
|
82
|
+
|
|
83
|
+
def wait(io, readiness, deadline)
|
|
84
|
+
left = Internal::Clock.left(deadline)
|
|
85
|
+
raise IO::TimeoutError, "TLS handshake timed out" if left.zero? || !io.public_send(readiness, left)
|
|
86
|
+
end
|
|
87
|
+
|
|
88
|
+
def default_context
|
|
89
|
+
context = OpenSSL::SSL::SSLContext.new
|
|
90
|
+
context.set_params(verify_mode: OpenSSL::SSL::VERIFY_PEER, verify_hostname: true)
|
|
91
|
+
context.min_version = OpenSSL::SSL::TLS1_2_VERSION
|
|
92
|
+
context.alpn_protocols = ["http/1.1"]
|
|
93
|
+
context
|
|
94
|
+
end
|
|
95
|
+
end
|
|
96
|
+
|
|
97
|
+
# Opens the TCP connection of a WebSocket in clear (`ws://`), as TlsConnector does without its
|
|
98
|
+
# TLS: only to the merchant's Bitcoin node on a local network (SPEC.md §31), which a node URL
|
|
99
|
+
# allows in `http` (WebSocketClient checks it). Inject another (WebSocketClient.new(
|
|
100
|
+
# plain_connector: …)) as you would a TlsConnector.
|
|
101
|
+
class PlainConnector < TlsConnector
|
|
102
|
+
def call(host, port, timeout)
|
|
103
|
+
connect_tcp(host, port, Internal::Clock.now + timeout)
|
|
104
|
+
end
|
|
105
|
+
end
|
|
106
|
+
|
|
107
|
+
# The platform's WebSocket client (RFC 6455), for the relays and the explorer's WebSocket
|
|
108
|
+
# (SPEC.md §13, §31), over TLS (TlsConnector), or in clear (`ws://`, PlainConnector) to the
|
|
109
|
+
# merchant's Bitcoin node on a local network alone: the handshake bounded in time, carrying the
|
|
110
|
+
# headers it is given (the User-Agent of SPEC.md §15) and following no redirect (a 3xx is a
|
|
111
|
+
# failed connection, as in HTTP: it would skip the checks of the URL); no extension offered,
|
|
112
|
+
# so no compression (permessage-deflate: a few compressed bytes would unpack into many large
|
|
113
|
+
# messages, and sealed records do not compress); a message of more than `max_message_bytes`
|
|
114
|
+
# (1 MiB) fails the connection before it is read.
|
|
115
|
+
#
|
|
116
|
+
# Inject another as `websocket_client:` of a Client: anything that answers
|
|
117
|
+
# `connect(url, headers:, timeout:, cancellation:)` (`timeout` in seconds, the whole handshake)
|
|
118
|
+
# with a socket that answers `send_text(text)`, `receive` (the next text message, a String; nil
|
|
119
|
+
# once the connection is closed; raises when it fails) and `close`. It gets a `ws://` URL only
|
|
120
|
+
# for the WebSocket of a merchant's node on a local network: connect in clear, or fail (raise),
|
|
121
|
+
# and the node's fallback applies.
|
|
122
|
+
class WebSocketClient
|
|
123
|
+
GUID = "258EAFA5-E914-47DA-95CA-C5AB0DC85B11"
|
|
124
|
+
# The longest handshake answer read: its status line and headers.
|
|
125
|
+
MAX_HANDSHAKE_BYTES = 16_384
|
|
126
|
+
HEADER_NAME = /\A[!#$%&'*+\-.^_`|~0-9A-Za-z]+\z/
|
|
127
|
+
HEADER_VALUE = /\A[\t\x20-\x7E]*\z/
|
|
128
|
+
|
|
129
|
+
# The handshake failed, or the server broke the protocol.
|
|
130
|
+
class ProtocolError < StandardError; end
|
|
131
|
+
|
|
132
|
+
# Where a WebSocket connects: its host, its port, the request target of its handshake, and
|
|
133
|
+
# whether over TLS.
|
|
134
|
+
Endpoint = Data.define(:host, :port, :target, :tls)
|
|
135
|
+
private_constant :Endpoint
|
|
136
|
+
|
|
137
|
+
def initialize(connector: TlsConnector.new, plain_connector: PlainConnector.new,
|
|
138
|
+
max_message_bytes: Internal::Protocol::MAX_RELAY_MESSAGE_BYTES)
|
|
139
|
+
@connector = connector
|
|
140
|
+
@plain_connector = plain_connector
|
|
141
|
+
@max_message_bytes = max_message_bytes
|
|
142
|
+
end
|
|
143
|
+
|
|
144
|
+
# A WebSocket to `url` (`wss://host[:port][/path]`, or `ws://` to the merchant's Bitcoin node
|
|
145
|
+
# on a local network: its scheme and authority a node URL's in `http`), open: its handshake
|
|
146
|
+
# (the connection included) bounded by `timeout` seconds, given up when `cancellation` is
|
|
147
|
+
# cancelled (Cancelled).
|
|
148
|
+
def connect(url, timeout:, headers: {}, cancellation: Cancellation::NONE)
|
|
149
|
+
holder = []
|
|
150
|
+
endpoint, connector = endpoint_of(url)
|
|
151
|
+
deadline = Internal::Clock.now + timeout
|
|
152
|
+
handle = cancellation.on_cancel { holder.each { |io| close_quietly(io) } }
|
|
153
|
+
io = connector.call(endpoint.host, endpoint.port, timeout)
|
|
154
|
+
holder << io
|
|
155
|
+
# Cancelled while the connector ran (a name's resolution does not stop at once): Cancelled.
|
|
156
|
+
cancellation.raise_if_cancelled
|
|
157
|
+
key = [SecureRandom.random_bytes(16)].pack("m0")
|
|
158
|
+
io_write(io, request(endpoint, key, headers), deadline)
|
|
159
|
+
rest = read_answer(io, key, deadline)
|
|
160
|
+
Session.new(io, rest, @max_message_bytes, timeout)
|
|
161
|
+
rescue StandardError
|
|
162
|
+
holder.each { |opened| close_quietly(opened) }
|
|
163
|
+
raise
|
|
164
|
+
ensure
|
|
165
|
+
cancellation.remove(handle)
|
|
166
|
+
end
|
|
167
|
+
|
|
168
|
+
# Writes all of `data` to `io` before `deadline` (Clock), else IO::TimeoutError. A connection
|
|
169
|
+
# the peer closed fails (EOFError, or the error the write raises), never tried again until the
|
|
170
|
+
# deadline: nothing written is no progress.
|
|
171
|
+
def self.write_all(io, data, deadline)
|
|
172
|
+
data = data.b
|
|
173
|
+
until data.empty?
|
|
174
|
+
written = io.write_nonblock(data, exception: false)
|
|
175
|
+
if written.is_a?(Integer) && written.positive?
|
|
176
|
+
data = data.byteslice(written..)
|
|
177
|
+
next
|
|
178
|
+
end
|
|
179
|
+
raise EOFError, "The connection was closed while it was written to" unless WAITS.include?(written)
|
|
180
|
+
|
|
181
|
+
left = Internal::Clock.left(deadline)
|
|
182
|
+
ready = left.positive? && io.to_io.public_send(written, left)
|
|
183
|
+
raise IO::TimeoutError, "Writing to the WebSocket timed out" unless ready
|
|
184
|
+
end
|
|
185
|
+
end
|
|
186
|
+
|
|
187
|
+
# What a write that cannot go on yet answers: wait until the socket is ready.
|
|
188
|
+
WAITS = %i[wait_readable wait_writable].freeze
|
|
189
|
+
private_constant :WAITS
|
|
190
|
+
|
|
191
|
+
private
|
|
192
|
+
|
|
193
|
+
# Where `url` connects, and how: a relay's `wss://` address (Relays.endpoint) over TLS; or a
|
|
194
|
+
# `ws://` URL of the merchant's Bitcoin node on a local network, in clear. ArgumentError for
|
|
195
|
+
# any other.
|
|
196
|
+
def endpoint_of(url)
|
|
197
|
+
unless url.to_s.start_with?("ws://")
|
|
198
|
+
relay = Internal::Relays.endpoint(url)
|
|
199
|
+
return [Endpoint.new(host: relay.host, port: relay.port, target: relay.target, tls: true), @connector]
|
|
200
|
+
end
|
|
201
|
+
|
|
202
|
+
node = Internal::NodeUrl.origin_of(url)
|
|
203
|
+
unless node&.local? && !node.tls?
|
|
204
|
+
raise ArgumentError, "Not a wss:// address, nor a ws:// one of a merchant's node on a local network"
|
|
205
|
+
end
|
|
206
|
+
|
|
207
|
+
target = Internal::Urls.request_target(url.sub(%r{\Aws://[^/?#]*}, ""))
|
|
208
|
+
[Endpoint.new(host: node.host, port: node.port, target: target, tls: false), @plain_connector]
|
|
209
|
+
end
|
|
210
|
+
|
|
211
|
+
def request(endpoint, key, headers)
|
|
212
|
+
default_port = endpoint.tls ? 443 : 80
|
|
213
|
+
host = endpoint.port == default_port ? endpoint.host : "#{endpoint.host}:#{endpoint.port}"
|
|
214
|
+
lines = ["GET #{endpoint.target} HTTP/1.1", "Host: #{host}", "Upgrade: websocket", "Connection: Upgrade",
|
|
215
|
+
"Sec-WebSocket-Key: #{key}", "Sec-WebSocket-Version: 13"]
|
|
216
|
+
headers.each do |name, value|
|
|
217
|
+
unless name.to_s.match?(HEADER_NAME) && value.to_s.match?(HEADER_VALUE)
|
|
218
|
+
raise ArgumentError, "Not an HTTP header: #{name}"
|
|
219
|
+
end
|
|
220
|
+
|
|
221
|
+
lines << "#{name}: #{value}"
|
|
222
|
+
end
|
|
223
|
+
"#{lines.join("\r\n")}\r\n\r\n"
|
|
224
|
+
end
|
|
225
|
+
|
|
226
|
+
# Reads the handshake's answer: 101 with the headers RFC 6455 asks, no extension nor protocol
|
|
227
|
+
# (none was offered). What follows its head is the beginning of the frames.
|
|
228
|
+
def read_answer(io, key, deadline)
|
|
229
|
+
buffer = +"".b
|
|
230
|
+
until (head_end = buffer.index("\r\n\r\n"))
|
|
231
|
+
raise ProtocolError, "Handshake answer too long" if buffer.bytesize > MAX_HANDSHAKE_BYTES
|
|
232
|
+
|
|
233
|
+
chunk = io_read(io, deadline)
|
|
234
|
+
raise ProtocolError, "Connection closed during the handshake" if chunk.nil?
|
|
235
|
+
|
|
236
|
+
buffer << chunk
|
|
237
|
+
end
|
|
238
|
+
raise ProtocolError, "Handshake answer too long" if head_end > MAX_HANDSHAKE_BYTES
|
|
239
|
+
|
|
240
|
+
lines = buffer.byteslice(0, head_end).split("\r\n")
|
|
241
|
+
status = lines.shift.to_s[%r{\AHTTP/1\.1 ([0-9]{3})(?: |\z)}, 1]
|
|
242
|
+
raise ProtocolError, "Not an HTTP answer to the handshake" unless status
|
|
243
|
+
raise ProtocolError, "The handshake answered a redirect (HTTP #{status})" if status.start_with?("3")
|
|
244
|
+
raise ProtocolError, "The handshake was refused (HTTP #{status})" unless status == "101"
|
|
245
|
+
|
|
246
|
+
check_headers(headers_of(lines), key)
|
|
247
|
+
buffer.byteslice((head_end + 4)..)
|
|
248
|
+
end
|
|
249
|
+
|
|
250
|
+
def headers_of(lines)
|
|
251
|
+
lines.each_with_object(Hash.new { |hash, name| hash[name] = [] }) do |line, headers|
|
|
252
|
+
name, value = line.split(":", 2)
|
|
253
|
+
raise ProtocolError, "Malformed handshake header" if value.nil? || !name.match?(HEADER_NAME)
|
|
254
|
+
|
|
255
|
+
headers[name.downcase] << value.strip
|
|
256
|
+
end
|
|
257
|
+
end
|
|
258
|
+
|
|
259
|
+
def check_headers(headers, key)
|
|
260
|
+
accept = [OpenSSL::Digest::SHA1.digest(key + GUID)].pack("m0")
|
|
261
|
+
unless headers["upgrade"].size == 1 && headers["upgrade"][0].casecmp?("websocket")
|
|
262
|
+
raise ProtocolError, "The handshake did not upgrade to a WebSocket"
|
|
263
|
+
end
|
|
264
|
+
|
|
265
|
+
tokens = headers["connection"].flat_map { |value| value.split(",") }
|
|
266
|
+
upgraded = tokens.any? { |token| token.strip.casecmp?("upgrade") }
|
|
267
|
+
raise ProtocolError, "The handshake did not upgrade the connection" unless upgraded
|
|
268
|
+
raise ProtocolError, "Wrong Sec-WebSocket-Accept" unless headers["sec-websocket-accept"] == [accept]
|
|
269
|
+
raise ProtocolError, "An extension that was not offered" unless headers["sec-websocket-extensions"].empty?
|
|
270
|
+
raise ProtocolError, "A subprotocol that was not offered" unless headers["sec-websocket-protocol"].empty?
|
|
271
|
+
end
|
|
272
|
+
|
|
273
|
+
def io_read(io, deadline)
|
|
274
|
+
loop do
|
|
275
|
+
chunk = io.read_nonblock(16_384, exception: false)
|
|
276
|
+
return chunk unless %i[wait_readable wait_writable].include?(chunk)
|
|
277
|
+
|
|
278
|
+
left = Internal::Clock.left(deadline)
|
|
279
|
+
ready = left.positive? && io.to_io.public_send(chunk, left)
|
|
280
|
+
raise IO::TimeoutError, "The handshake timed out" unless ready
|
|
281
|
+
end
|
|
282
|
+
end
|
|
283
|
+
|
|
284
|
+
def io_write(io, data, deadline)
|
|
285
|
+
WebSocketClient.write_all(io, data, deadline)
|
|
286
|
+
end
|
|
287
|
+
|
|
288
|
+
def close_quietly(io)
|
|
289
|
+
io.close
|
|
290
|
+
rescue StandardError
|
|
291
|
+
nil
|
|
292
|
+
end
|
|
293
|
+
|
|
294
|
+
# An open WebSocket: text messages in and out. `receive` from one thread; `send_text` and
|
|
295
|
+
# `close` from any.
|
|
296
|
+
class Session
|
|
297
|
+
OPCODES = { continuation: 0x0, text: 0x1, binary: 0x2, close: 0x8, ping: 0x9, pong: 0xA }.freeze
|
|
298
|
+
|
|
299
|
+
def initialize(io, buffered, max_message_bytes, write_timeout)
|
|
300
|
+
@io = io
|
|
301
|
+
@buffer = buffered.b
|
|
302
|
+
@max = max_message_bytes
|
|
303
|
+
@write_timeout = write_timeout
|
|
304
|
+
@write_mutex = Mutex.new
|
|
305
|
+
@closed = false
|
|
306
|
+
end
|
|
307
|
+
|
|
308
|
+
# Sends a text message.
|
|
309
|
+
def send_text(text)
|
|
310
|
+
frame(OPCODES[:text], text.b)
|
|
311
|
+
nil
|
|
312
|
+
end
|
|
313
|
+
|
|
314
|
+
# The next text message (a binary one as its bytes); nil once the connection is closed.
|
|
315
|
+
# Answers pings, and the server's close. Raises WebSocketClient::ProtocolError when the
|
|
316
|
+
# server breaks the protocol (a frame masked or reserved bits set, a message too large,
|
|
317
|
+
# a text that is not UTF-8…), IOError or a SystemCallError when the connection fails.
|
|
318
|
+
def receive
|
|
319
|
+
message = nil
|
|
320
|
+
kind = nil
|
|
321
|
+
loop do
|
|
322
|
+
fin, opcode, payload = read_frame(message ? message.bytesize : 0)
|
|
323
|
+
return nil if payload.nil?
|
|
324
|
+
|
|
325
|
+
case opcode
|
|
326
|
+
when OPCODES[:ping]
|
|
327
|
+
# Between the fragments of a message too (RFC 6455 §5.4): the message goes on.
|
|
328
|
+
frame(OPCODES[:pong], payload)
|
|
329
|
+
next
|
|
330
|
+
when OPCODES[:pong] then next
|
|
331
|
+
when OPCODES[:close]
|
|
332
|
+
answer_close(payload)
|
|
333
|
+
return nil
|
|
334
|
+
when OPCODES[:continuation]
|
|
335
|
+
raise ProtocolError, "A continuation without a message" if message.nil?
|
|
336
|
+
|
|
337
|
+
message << payload
|
|
338
|
+
when OPCODES[:text], OPCODES[:binary]
|
|
339
|
+
raise ProtocolError, "A message inside another" if message
|
|
340
|
+
|
|
341
|
+
message = payload
|
|
342
|
+
kind = opcode
|
|
343
|
+
else raise ProtocolError, "Unknown opcode #{opcode}"
|
|
344
|
+
end
|
|
345
|
+
next unless fin && message
|
|
346
|
+
|
|
347
|
+
return kind == OPCODES[:text] ? text_of(message) : message
|
|
348
|
+
end
|
|
349
|
+
rescue IOError, SystemCallError, OpenSSL::SSL::SSLError
|
|
350
|
+
return nil if @closed
|
|
351
|
+
|
|
352
|
+
raise
|
|
353
|
+
end
|
|
354
|
+
|
|
355
|
+
# Closes the connection (a close frame first, when it can be written at once).
|
|
356
|
+
def close
|
|
357
|
+
return if @closed
|
|
358
|
+
|
|
359
|
+
@closed = true
|
|
360
|
+
# A close frame when no other write holds the socket (one blocked would delay the close).
|
|
361
|
+
if @write_mutex.try_lock
|
|
362
|
+
begin
|
|
363
|
+
write_frame(OPCODES[:close], [1000].pack("n"), 0.2)
|
|
364
|
+
rescue StandardError
|
|
365
|
+
nil
|
|
366
|
+
ensure
|
|
367
|
+
@write_mutex.unlock
|
|
368
|
+
end
|
|
369
|
+
end
|
|
370
|
+
begin
|
|
371
|
+
@io.close
|
|
372
|
+
rescue StandardError
|
|
373
|
+
nil
|
|
374
|
+
end
|
|
375
|
+
nil
|
|
376
|
+
end
|
|
377
|
+
|
|
378
|
+
def closed?
|
|
379
|
+
@closed
|
|
380
|
+
end
|
|
381
|
+
|
|
382
|
+
private
|
|
383
|
+
|
|
384
|
+
def text_of(bytes)
|
|
385
|
+
text = bytes.force_encoding(Encoding::UTF_8)
|
|
386
|
+
raise ProtocolError, "A text message that is not UTF-8" unless text.valid_encoding?
|
|
387
|
+
|
|
388
|
+
text
|
|
389
|
+
end
|
|
390
|
+
|
|
391
|
+
def answer_close(payload)
|
|
392
|
+
code = payload.bytesize >= 2 ? payload.byteslice(0, 2) : "".b
|
|
393
|
+
begin
|
|
394
|
+
frame(OPCODES[:close], code, 1.0)
|
|
395
|
+
rescue StandardError
|
|
396
|
+
nil
|
|
397
|
+
end
|
|
398
|
+
@closed = true
|
|
399
|
+
@io.close
|
|
400
|
+
rescue StandardError
|
|
401
|
+
nil
|
|
402
|
+
end
|
|
403
|
+
|
|
404
|
+
# The next frame: [fin, opcode, payload]; nil payload when the connection ended. A data
|
|
405
|
+
# frame that would make its message longer than the limit fails before it is read.
|
|
406
|
+
def read_frame(message_bytes)
|
|
407
|
+
head = take(2)
|
|
408
|
+
return [false, nil, nil] if head.nil?
|
|
409
|
+
|
|
410
|
+
first, second = head.unpack("CC")
|
|
411
|
+
raise ProtocolError, "Reserved bits set (no extension was negotiated)" unless first.nobits?(0x70)
|
|
412
|
+
raise ProtocolError, "A masked frame from the server" if second.anybits?(0x80)
|
|
413
|
+
|
|
414
|
+
fin = first.anybits?(0x80)
|
|
415
|
+
opcode = first & 0x0F
|
|
416
|
+
length = second & 0x7F
|
|
417
|
+
length = take!(2).unpack1("n") if length == 126
|
|
418
|
+
if length == 127
|
|
419
|
+
length = take!(8).unpack1("Q>")
|
|
420
|
+
raise ProtocolError, "A frame length above 2^63" if length >= 2**63
|
|
421
|
+
end
|
|
422
|
+
if opcode >= 0x8
|
|
423
|
+
raise ProtocolError, "A control frame fragmented or too long" unless fin && length <= 125
|
|
424
|
+
elsif message_bytes + length > @max
|
|
425
|
+
raise ProtocolError, "A message of more than #{@max} bytes"
|
|
426
|
+
end
|
|
427
|
+
|
|
428
|
+
[fin, opcode, take!(length)]
|
|
429
|
+
end
|
|
430
|
+
|
|
431
|
+
def take!(count)
|
|
432
|
+
take(count) || raise(ProtocolError, "The connection ended within a frame")
|
|
433
|
+
end
|
|
434
|
+
|
|
435
|
+
# `count` bytes of the stream; nil when it ends before.
|
|
436
|
+
def take(count)
|
|
437
|
+
while @buffer.bytesize < count
|
|
438
|
+
chunk = read_chunk
|
|
439
|
+
return nil if chunk.nil?
|
|
440
|
+
|
|
441
|
+
@buffer << chunk
|
|
442
|
+
end
|
|
443
|
+
taken = @buffer.byteslice(0, count)
|
|
444
|
+
@buffer = @buffer.byteslice(count..)
|
|
445
|
+
taken
|
|
446
|
+
end
|
|
447
|
+
|
|
448
|
+
def read_chunk
|
|
449
|
+
loop do
|
|
450
|
+
chunk = @io.read_nonblock(65_536, exception: false)
|
|
451
|
+
return chunk&.b unless %i[wait_readable wait_writable].include?(chunk)
|
|
452
|
+
|
|
453
|
+
@io.to_io.public_send(chunk)
|
|
454
|
+
end
|
|
455
|
+
end
|
|
456
|
+
|
|
457
|
+
def frame(opcode, payload, timeout = @write_timeout)
|
|
458
|
+
@write_mutex.synchronize { write_frame(opcode, payload, timeout) }
|
|
459
|
+
end
|
|
460
|
+
|
|
461
|
+
def write_frame(opcode, payload, timeout)
|
|
462
|
+
length = payload.bytesize
|
|
463
|
+
header = [0x80 | opcode].pack("C")
|
|
464
|
+
header << if length <= 125
|
|
465
|
+
[0x80 | length].pack("C")
|
|
466
|
+
elsif length <= 0xFFFF
|
|
467
|
+
[0x80 | 126, length].pack("Cn")
|
|
468
|
+
else
|
|
469
|
+
[0x80 | 127, length].pack("CQ>")
|
|
470
|
+
end
|
|
471
|
+
mask = SecureRandom.random_bytes(4)
|
|
472
|
+
WebSocketClient.write_all(@io, header << mask << masked(payload, mask), Internal::Clock.now + timeout)
|
|
473
|
+
end
|
|
474
|
+
|
|
475
|
+
# `payload` XOR the masking key repeated (RFC 6455 §5.3), four bytes at a time.
|
|
476
|
+
def masked(payload, mask)
|
|
477
|
+
return payload if payload.empty?
|
|
478
|
+
|
|
479
|
+
padding = (-payload.bytesize) % 4
|
|
480
|
+
key = mask.unpack1("N")
|
|
481
|
+
words = (payload + ("\0" * padding)).unpack("N*").map { |word| word ^ key }
|
|
482
|
+
words.pack("N*").byteslice(0, payload.bytesize)
|
|
483
|
+
end
|
|
484
|
+
end
|
|
485
|
+
end
|
|
486
|
+
end
|
|
487
|
+
end
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
# The sattle.me checkout SDK: take Lightning and on-chain payments for a sattle.me merchant from an
|
|
4
|
+
# online shop's server (README.md). Sattleme::Checkout::Client is where to start.
|
|
5
|
+
|
|
6
|
+
require_relative "checkout/version"
|
|
7
|
+
require_relative "checkout/errors"
|
|
8
|
+
require_relative "checkout/cancellation"
|
|
9
|
+
require_relative "checkout/internal/concurrency"
|
|
10
|
+
require_relative "checkout/internal/protocol"
|
|
11
|
+
require_relative "checkout/internal/secp256k1"
|
|
12
|
+
require_relative "checkout/internal/json"
|
|
13
|
+
require_relative "checkout/internal/text"
|
|
14
|
+
require_relative "checkout/internal/crypto"
|
|
15
|
+
require_relative "checkout/internal/nip44"
|
|
16
|
+
require_relative "checkout/internal/urls"
|
|
17
|
+
require_relative "checkout/internal/key"
|
|
18
|
+
require_relative "checkout/internal/event"
|
|
19
|
+
require_relative "checkout/internal/record"
|
|
20
|
+
require_relative "checkout/internal/derive"
|
|
21
|
+
require_relative "checkout/internal/bolt11"
|
|
22
|
+
require_relative "checkout/internal/currencies"
|
|
23
|
+
require_relative "checkout/models"
|
|
24
|
+
require_relative "checkout/internal/http"
|
|
25
|
+
require_relative "checkout/internal/rates"
|
|
26
|
+
require_relative "checkout/internal/onchain/address"
|
|
27
|
+
require_relative "checkout/internal/onchain/basics"
|
|
28
|
+
require_relative "checkout/internal/onchain/node"
|
|
29
|
+
require_relative "checkout/internal/lnurl"
|
|
30
|
+
require_relative "checkout/internal/order"
|
|
31
|
+
require_relative "checkout/internal/checkout_link"
|
|
32
|
+
require_relative "checkout/internal/channel"
|
|
33
|
+
require_relative "checkout/internal/declaration"
|
|
34
|
+
require_relative "checkout/internal/onchain/lease"
|
|
35
|
+
require_relative "checkout/internal/onchain/witness"
|
|
36
|
+
require_relative "checkout/internal/onchain/merchant_acceptance"
|
|
37
|
+
require_relative "checkout/internal/onchain/rest"
|
|
38
|
+
require_relative "checkout/internal/onchain/payment"
|
|
39
|
+
require_relative "checkout/internal/onchain/watch"
|
|
40
|
+
require_relative "checkout/internal/sales"
|
|
41
|
+
require_relative "checkout/internal/relay_pool"
|
|
42
|
+
require_relative "checkout/websocket_client"
|
|
43
|
+
require_relative "checkout/http_client"
|
|
44
|
+
require_relative "checkout/client"
|
|
45
|
+
|
|
46
|
+
module Sattleme
|
|
47
|
+
# The sattle.me checkout SDK (README.md): Client makes invoices and checks their payments; every
|
|
48
|
+
# error it raises is an Error, but for a Cancelled call.
|
|
49
|
+
module Checkout
|
|
50
|
+
# The rate sources the gem knows by name (SPEC.md §26), and the list asked when the client is
|
|
51
|
+
# given none, in its order.
|
|
52
|
+
RATE_SOURCES = Internal::Rates::NAMES
|
|
53
|
+
DEFAULT_RATE_SOURCES = Internal::Rates::DEFAULT_SOURCES
|
|
54
|
+
|
|
55
|
+
# The decimals of the currency whose code is exactly `code`, of those an order may be priced in
|
|
56
|
+
# (ISO 4217 in upper case, the gem's table: SPEC.md §4 and §25), an Integer; nil for any other
|
|
57
|
+
# text, and for anything that is not a String. `code` is read as create_invoice reads an
|
|
58
|
+
# order's currency (a String in any encoding, as its text), so that it says whether an order
|
|
59
|
+
# may be priced in it, and in how many decimals: whatever the platform's locale data says.
|
|
60
|
+
def self.currency_digits(code)
|
|
61
|
+
Internal::Currencies.digits(Internal::Text.caller_text(code))
|
|
62
|
+
end
|
|
63
|
+
|
|
64
|
+
# The sats an order priced `fiat` (`{ amount: "24.50", currency: "CHF" }`) asks at `price`, the
|
|
65
|
+
# price of one bitcoin in its currency (a decimal String): exact, rounded up to the next whole
|
|
66
|
+
# sat, as create_invoice converts it. InvalidOrderError (`fiat`) for an invalid price in a
|
|
67
|
+
# currency, RateError for an invalid price of one bitcoin.
|
|
68
|
+
def self.fiat_to_sats(fiat, price)
|
|
69
|
+
Internal::Rates.fiat_to_sats(fiat, price)
|
|
70
|
+
end
|
|
71
|
+
end
|
|
72
|
+
end
|
metadata
ADDED
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: sattleme-checkout
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
version: 1.0.0
|
|
5
|
+
platform: ruby
|
|
6
|
+
authors:
|
|
7
|
+
- sattle.me
|
|
8
|
+
bindir: bin
|
|
9
|
+
cert_chain: []
|
|
10
|
+
date: 1980-01-02 00:00:00.000000000 Z
|
|
11
|
+
dependencies:
|
|
12
|
+
- !ruby/object:Gem::Dependency
|
|
13
|
+
name: ffi
|
|
14
|
+
requirement: !ruby/object:Gem::Requirement
|
|
15
|
+
requirements:
|
|
16
|
+
- - ">="
|
|
17
|
+
- !ruby/object:Gem::Version
|
|
18
|
+
version: 1.17.4
|
|
19
|
+
- - "<"
|
|
20
|
+
- !ruby/object:Gem::Version
|
|
21
|
+
version: '2'
|
|
22
|
+
type: :runtime
|
|
23
|
+
prerelease: false
|
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
25
|
+
requirements:
|
|
26
|
+
- - ">="
|
|
27
|
+
- !ruby/object:Gem::Version
|
|
28
|
+
version: 1.17.4
|
|
29
|
+
- - "<"
|
|
30
|
+
- !ruby/object:Gem::Version
|
|
31
|
+
version: '2'
|
|
32
|
+
description: 'Take Lightning and on-chain payments for a sattle.me merchant from an
|
|
33
|
+
online shop''s server: invoices, deposit addresses, registry declarations, payment
|
|
34
|
+
checks. Implements the sattle.me checkout specification.'
|
|
35
|
+
executables: []
|
|
36
|
+
extensions: []
|
|
37
|
+
extra_rdoc_files: []
|
|
38
|
+
files:
|
|
39
|
+
- CHANGELOG.md
|
|
40
|
+
- LICENSE
|
|
41
|
+
- README.md
|
|
42
|
+
- SECURITY.md
|
|
43
|
+
- lib/sattleme/checkout.rb
|
|
44
|
+
- lib/sattleme/checkout/cancellation.rb
|
|
45
|
+
- lib/sattleme/checkout/client.rb
|
|
46
|
+
- lib/sattleme/checkout/errors.rb
|
|
47
|
+
- lib/sattleme/checkout/http_client.rb
|
|
48
|
+
- lib/sattleme/checkout/internal/bolt11.rb
|
|
49
|
+
- lib/sattleme/checkout/internal/channel.rb
|
|
50
|
+
- lib/sattleme/checkout/internal/checkout_link.rb
|
|
51
|
+
- lib/sattleme/checkout/internal/concurrency.rb
|
|
52
|
+
- lib/sattleme/checkout/internal/crypto.rb
|
|
53
|
+
- lib/sattleme/checkout/internal/currencies.rb
|
|
54
|
+
- lib/sattleme/checkout/internal/declaration.rb
|
|
55
|
+
- lib/sattleme/checkout/internal/derive.rb
|
|
56
|
+
- lib/sattleme/checkout/internal/event.rb
|
|
57
|
+
- lib/sattleme/checkout/internal/http.rb
|
|
58
|
+
- lib/sattleme/checkout/internal/json.rb
|
|
59
|
+
- lib/sattleme/checkout/internal/key.rb
|
|
60
|
+
- lib/sattleme/checkout/internal/lnurl.rb
|
|
61
|
+
- lib/sattleme/checkout/internal/nip44.rb
|
|
62
|
+
- lib/sattleme/checkout/internal/onchain/address.rb
|
|
63
|
+
- lib/sattleme/checkout/internal/onchain/basics.rb
|
|
64
|
+
- lib/sattleme/checkout/internal/onchain/lease.rb
|
|
65
|
+
- lib/sattleme/checkout/internal/onchain/merchant_acceptance.rb
|
|
66
|
+
- lib/sattleme/checkout/internal/onchain/node.rb
|
|
67
|
+
- lib/sattleme/checkout/internal/onchain/payment.rb
|
|
68
|
+
- lib/sattleme/checkout/internal/onchain/rest.rb
|
|
69
|
+
- lib/sattleme/checkout/internal/onchain/watch.rb
|
|
70
|
+
- lib/sattleme/checkout/internal/onchain/witness.rb
|
|
71
|
+
- lib/sattleme/checkout/internal/order.rb
|
|
72
|
+
- lib/sattleme/checkout/internal/protocol.rb
|
|
73
|
+
- lib/sattleme/checkout/internal/rates.rb
|
|
74
|
+
- lib/sattleme/checkout/internal/record.rb
|
|
75
|
+
- lib/sattleme/checkout/internal/relay_pool.rb
|
|
76
|
+
- lib/sattleme/checkout/internal/sales.rb
|
|
77
|
+
- lib/sattleme/checkout/internal/secp256k1.rb
|
|
78
|
+
- lib/sattleme/checkout/internal/text.rb
|
|
79
|
+
- lib/sattleme/checkout/internal/urls.rb
|
|
80
|
+
- lib/sattleme/checkout/models.rb
|
|
81
|
+
- lib/sattleme/checkout/version.rb
|
|
82
|
+
- lib/sattleme/checkout/websocket_client.rb
|
|
83
|
+
homepage: https://sattle.me
|
|
84
|
+
licenses:
|
|
85
|
+
- MIT
|
|
86
|
+
metadata:
|
|
87
|
+
homepage_uri: https://sattle.me
|
|
88
|
+
source_code_uri: https://github.com/sattleme/checkout-ruby
|
|
89
|
+
changelog_uri: https://github.com/sattleme/checkout-ruby/blob/main/CHANGELOG.md
|
|
90
|
+
bug_tracker_uri: https://github.com/sattleme/checkout-ruby/issues
|
|
91
|
+
documentation_uri: https://github.com/sattleme/checkout-ruby#readme
|
|
92
|
+
rubygems_mfa_required: 'true'
|
|
93
|
+
allowed_push_host: https://rubygems.org
|
|
94
|
+
rdoc_options: []
|
|
95
|
+
require_paths:
|
|
96
|
+
- lib
|
|
97
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
|
98
|
+
requirements:
|
|
99
|
+
- - ">="
|
|
100
|
+
- !ruby/object:Gem::Version
|
|
101
|
+
version: '3.3'
|
|
102
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
103
|
+
requirements:
|
|
104
|
+
- - ">="
|
|
105
|
+
- !ruby/object:Gem::Version
|
|
106
|
+
version: '0'
|
|
107
|
+
requirements: []
|
|
108
|
+
rubygems_version: 4.0.20
|
|
109
|
+
specification_version: 4
|
|
110
|
+
summary: Take Lightning and on-chain payments for a sattle.me merchant from an online
|
|
111
|
+
shop's server.
|
|
112
|
+
test_files: []
|