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.
Files changed (46) hide show
  1. checksums.yaml +7 -0
  2. data/CHANGELOG.md +66 -0
  3. data/LICENSE +21 -0
  4. data/README.md +754 -0
  5. data/SECURITY.md +25 -0
  6. data/lib/sattleme/checkout/cancellation.rb +195 -0
  7. data/lib/sattleme/checkout/client.rb +1438 -0
  8. data/lib/sattleme/checkout/errors.rb +165 -0
  9. data/lib/sattleme/checkout/http_client.rb +294 -0
  10. data/lib/sattleme/checkout/internal/bolt11.rb +187 -0
  11. data/lib/sattleme/checkout/internal/channel.rb +147 -0
  12. data/lib/sattleme/checkout/internal/checkout_link.rb +294 -0
  13. data/lib/sattleme/checkout/internal/concurrency.rb +450 -0
  14. data/lib/sattleme/checkout/internal/crypto.rb +63 -0
  15. data/lib/sattleme/checkout/internal/currencies.rb +47 -0
  16. data/lib/sattleme/checkout/internal/declaration.rb +135 -0
  17. data/lib/sattleme/checkout/internal/derive.rb +153 -0
  18. data/lib/sattleme/checkout/internal/event.rb +110 -0
  19. data/lib/sattleme/checkout/internal/http.rb +129 -0
  20. data/lib/sattleme/checkout/internal/json.rb +457 -0
  21. data/lib/sattleme/checkout/internal/key.rb +116 -0
  22. data/lib/sattleme/checkout/internal/lnurl.rb +238 -0
  23. data/lib/sattleme/checkout/internal/nip44.rb +113 -0
  24. data/lib/sattleme/checkout/internal/onchain/address.rb +58 -0
  25. data/lib/sattleme/checkout/internal/onchain/basics.rb +352 -0
  26. data/lib/sattleme/checkout/internal/onchain/lease.rb +326 -0
  27. data/lib/sattleme/checkout/internal/onchain/merchant_acceptance.rb +168 -0
  28. data/lib/sattleme/checkout/internal/onchain/node.rb +285 -0
  29. data/lib/sattleme/checkout/internal/onchain/payment.rb +345 -0
  30. data/lib/sattleme/checkout/internal/onchain/rest.rb +323 -0
  31. data/lib/sattleme/checkout/internal/onchain/watch.rb +119 -0
  32. data/lib/sattleme/checkout/internal/onchain/witness.rb +260 -0
  33. data/lib/sattleme/checkout/internal/order.rb +228 -0
  34. data/lib/sattleme/checkout/internal/protocol.rb +260 -0
  35. data/lib/sattleme/checkout/internal/rates.rb +373 -0
  36. data/lib/sattleme/checkout/internal/record.rb +179 -0
  37. data/lib/sattleme/checkout/internal/relay_pool.rb +823 -0
  38. data/lib/sattleme/checkout/internal/sales.rb +238 -0
  39. data/lib/sattleme/checkout/internal/secp256k1.rb +234 -0
  40. data/lib/sattleme/checkout/internal/text.rb +144 -0
  41. data/lib/sattleme/checkout/internal/urls.rb +172 -0
  42. data/lib/sattleme/checkout/models.rb +247 -0
  43. data/lib/sattleme/checkout/version.rb +9 -0
  44. data/lib/sattleme/checkout/websocket_client.rb +487 -0
  45. data/lib/sattleme/checkout.rb +72 -0
  46. metadata +112 -0
@@ -0,0 +1,179 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Sattleme
4
+ module Checkout
5
+ module Internal
6
+ # Sealed records, as the merchant's app writes and reads them (SPEC.md §12): one addressable
7
+ # event of kind 30078 (NIP-78), an opaque `d` tag, NIP-44 v2 content whose plaintext
8
+ # `{"t": <type>, "v": <value>}` is padded with spaces to a multiple of 256 bytes.
9
+ module Records
10
+ # A record of a newer format than this gem reads: ignored, never written over.
11
+ class Newer < StandardError; end
12
+
13
+ # A record opened: its event and value, or authentic but of a newer format (`newer`).
14
+ Opened = Data.define(:status, :event, :value) do
15
+ def ok? = status == :ok
16
+ def newer? = status == :newer
17
+ end
18
+
19
+ # The newest of the records relays sent: its event and value (nil when none), and the
20
+ # `created_at` of the newest copy of a newer format (nil when none).
21
+ Newest = Data.define(:event, :value, :newest_unreadable)
22
+
23
+ module_function
24
+
25
+ # The signed event of a record: `value` (JSON) of `type`, sealed with `encryption_key`, at
26
+ # `address` (its `d` tag, then `tags`), signed by `signer` at `created_at`. Encryption and
27
+ # signature drawn from `random`.
28
+ def seal(type:, value:, signer:, encryption_key:, address:, created_at:, random:, tags: [])
29
+ text = Json.generate({ "t" => type, "v" => value })
30
+ length = text.bytesize
31
+ # Spaces after the JSON: whitespace when it is parsed.
32
+ padded = text + (" " * ((-length) % Protocol::RECORD_BLOCK_BYTES))
33
+ Events.sign(signer, random, created_at: created_at, kind: Protocol::RECORD_KIND,
34
+ tags: [["d", address], *tags],
35
+ content: Nip44.encrypt(padded, encryption_key, random))
36
+ end
37
+
38
+ # What a relay sent, if it is a record of `type` at `address` by `author` sealed with
39
+ # `encryption_key`: kind, author, `d` tag, then a content that opens with the key and holds a
40
+ # value of the type, then the signature; `parse` validates the value (nil when invalid; it
41
+ # may raise Newer). Anything else is nil: a relay is never trusted.
42
+ def open(value, type:, encryption_key:, address:, author:, &parse)
43
+ claimed = Events.fields(value)
44
+ return nil unless claimed.is_a?(Hash) && claimed["kind"] == Protocol::RECORD_KIND
45
+ return nil unless claimed["pubkey"] == author && claimed["content"].is_a?(String)
46
+ return nil unless tagged?(claimed["tags"], address)
47
+
48
+ newer = false
49
+ payload = nil
50
+ begin
51
+ payload = read_content(claimed["content"], type, encryption_key)
52
+ return nil if payload.equal?(NONE)
53
+ rescue Newer
54
+ newer = true
55
+ end
56
+ return nil unless Events.valid?(claimed)
57
+
58
+ event = Events.from(claimed)
59
+ return Opened.new(status: :newer, event: event, value: nil) if newer
60
+
61
+ begin
62
+ parsed = parse.call(payload, event)
63
+ parsed.nil? ? nil : Opened.new(status: :ok, event: event, value: parsed)
64
+ rescue Newer
65
+ Opened.new(status: :newer, event: event, value: nil)
66
+ rescue StandardError
67
+ # A value no check expected stops nothing: it is no record, as anything else a relay sends.
68
+ nil
69
+ end
70
+ end
71
+
72
+ # Whether `tags` (a relay's) hold the `d` tag `address`, as one of them.
73
+ def tagged?(tags, address)
74
+ tags.is_a?(Array) && tags.any? { |tag| tag.is_a?(Array) && tag[0] == "d" && tag[1] == address }
75
+ end
76
+
77
+ # What a record's content holds when it does not open, or holds another type.
78
+ NONE = Object.new.freeze
79
+
80
+ # The value inside a record's content; NONE when it does not open or is of another type.
81
+ # Raises Newer for a content of a newer NIP-44 version.
82
+ def read_content(content, type, encryption_key)
83
+ begin
84
+ text = Nip44.decrypt(content, encryption_key)
85
+ rescue Nip44::UnknownVersion
86
+ raise Newer
87
+ rescue Nip44::Invalid
88
+ return NONE
89
+ end
90
+ envelope = Json.parse_or(text, NONE)
91
+ return NONE unless envelope.is_a?(Hash) && envelope.key?("v")
92
+
93
+ envelope["t"] == type ? envelope["v"] : NONE
94
+ end
95
+
96
+ # What a read of sealed records keeps of each event (SPEC.md §13: the event, when it opens):
97
+ # an event of kind 30078 whose content decrypts with the key `key_of` gives for its first `d`
98
+ # tag (hex32, else nil) and its author (NIP-44 v2, its MAC checked), or is of a newer NIP-44
99
+ # version, and that checks out (§11), its signature included: anybody may send a copy of a
100
+ # record under an id of its own, its content and signature left as they are, and only the
101
+ # signature tells it from the record. Checked once per id and signature, whatever relays
102
+ # send copies. `key_of` gives nil for an author or an address the read does not open. Its
103
+ # value is not checked here: the read opens what it keeps. Of the event, only its NIP-01
104
+ # members are kept: a relay may add any to a record that checks out (its id and signature
105
+ # cover none of it). Thread-safe: every relay's reader calls it.
106
+ def that_open(&key_of)
107
+ verified = {}
108
+ mutex = Mutex.new
109
+ lambda do |value|
110
+ next nil unless value.is_a?(Hash) && value["kind"] == Protocol::RECORD_KIND
111
+ next nil unless value["content"].is_a?(String) && value["pubkey"].is_a?(String)
112
+
113
+ key = key_of.call(d_tag_of(value), value["pubkey"])
114
+ next nil if key.nil?
115
+
116
+ begin
117
+ Nip44.decrypt(value["content"], key)
118
+ rescue Nip44::UnknownVersion
119
+ nil
120
+ rescue Nip44::Invalid
121
+ next nil
122
+ end
123
+ next nil unless Events.valid_id?(value)
124
+
125
+ copy = "#{value["id"]}/#{value["sig"]}"
126
+ valid = mutex.synchronize { verified[copy] }
127
+ if valid.nil?
128
+ valid = Events.signature_checks?(value)
129
+ mutex.synchronize { verified[copy] = valid }
130
+ end
131
+ valid ? Events.nip01_members(value) : nil
132
+ end
133
+ end
134
+
135
+ # The `d` tag of a value a relay sent, when it is hex32 (its first `d` tag); nil otherwise.
136
+ def d_tag_of(value)
137
+ tags = Events.fields(value).then { |fields| fields.is_a?(Hash) ? fields["tags"] : nil }
138
+ return nil unless tags.is_a?(Array)
139
+
140
+ tag = tags.find { |item| item.is_a?(Array) && item[0] == "d" }
141
+ d = tag&.[](1)
142
+ Text.hex32?(d) ? d : nil
143
+ end
144
+
145
+ # Whether `a` replaces `b` as relays keep them: a later `created_at`, then the lowest id.
146
+ def replaces?(first, second)
147
+ first.created_at > second.created_at ||
148
+ (first.created_at == second.created_at && first.id < second.id)
149
+ end
150
+
151
+ # The newest record of what relays sent (`answers`: relay → values), each copy opened with
152
+ # `open` once per event id that checked out (a copy with the id of one already checked is
153
+ # the same event); the newest by `created_at` wins (on a tie, the lowest event id).
154
+ def newest(answers, &open)
155
+ checked = {}
156
+ newest = nil
157
+ newest_unreadable = nil
158
+ answers.each_value do |events|
159
+ events.each do |value|
160
+ fields = Events.fields(value)
161
+ next if fields.is_a?(Hash) && fields["id"].is_a?(String) && checked.key?(fields["id"])
162
+
163
+ copy = open.call(value)
164
+ next unless copy
165
+
166
+ checked[copy.event.id] = true
167
+ if copy.newer?
168
+ newest_unreadable = [newest_unreadable || -1, copy.event.created_at].max
169
+ next
170
+ end
171
+ newest = copy if newest.nil? || replaces?(copy.event, newest.event)
172
+ end
173
+ end
174
+ Newest.new(event: newest&.event, value: newest&.value, newest_unreadable: newest_unreadable)
175
+ end
176
+ end
177
+ end
178
+ end
179
+ end