x402-rack 0.5.0 → 0.5.1

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: '04961db3ca1280873324aa191fc39d968e52b79ff90819569851811354106398'
4
- data.tar.gz: 4a90ebc4e88fc7c55412059687d19751ffb58db90767973a18a666d560a6020d
3
+ metadata.gz: 98f4be71b3158fb9a60f48c7fd22fab18bf11f7e299bbd482a34ed1ff0d43f5b
4
+ data.tar.gz: 52abd02a198410c35b10d07247a290c1e2d1ec6c971758b160be7c5956d01fa7
5
5
  SHA512:
6
- metadata.gz: 7996d498aa4230ad84a2b02912c05347ac53830dd159ff36666a92268f6eb4dcbcf5e9acacb60c1be8fc8f1b930d6e7a942f852c6e4f87c4908022e314b59130
7
- data.tar.gz: a75ee1bd1e2017b53aab6a204164825c6ffc06bd9ee6f312f2b5c7f560178e96afaf220199bf51b6fbc63f1fa7243e3a2360426774b899418ff408e66826c7e8
6
+ metadata.gz: 22f092945c96c25969074be4f70d9af80ced8e2a60fff3ff02d4b851e77758a754cfca4b337e341abcb7fcb6c0eb8623ee0aa9b46526991ea406579ed9c3e483
7
+ data.tar.gz: ce2373b9e168bf1dea3d5f39a97b870007f58d1b74fe6cec8c7dd172b10c4daa4dda1bbe9156fe94ca43cd1dc3399db67f92db1b4e92e860d0ac982f682cff1f
data/CHANGELOG.md CHANGED
@@ -5,6 +5,12 @@ All notable changes to this project will be documented in this file.
5
5
  The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/),
6
6
  and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
7
7
 
8
+ ## [0.5.1] - 2026-04-04
9
+
10
+ ### Fixed
11
+
12
+ - **PaymentObserver now protocol-agnostic** — `extract_and_validate` no longer hardcodes the Coinbase v2 envelope format. Pluggable `extractor:` parameter (duck-typed `#call(proof_payload)` → `Transaction` or `nil`) enables BRC-105 and custom payment formats. Default `CoinbaseV2Extractor` preserves backwards compatibility. (#84)
13
+
8
14
  ## [0.5.0] - 2026-04-04
9
15
 
10
16
  ### Changed
@@ -127,6 +133,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
127
133
  - **Operations docs** — deployment, performance, treasury/nonce lifecycle.
128
134
  - **Ecosystem docs** — Coinbase v2, merkleworks, BRC-105 positioning and header namespace reservations.
129
135
 
136
+ [0.5.1]: https://github.com/sgbett/x402-rack/compare/v0.5.0...v0.5.1
130
137
  [0.5.0]: https://github.com/sgbett/x402-rack/compare/v0.4.0...v0.5.0
131
138
  [0.4.0]: https://github.com/sgbett/x402-rack/compare/v0.3.0...v0.4.0
132
139
  [0.3.0]: https://github.com/sgbett/x402-rack/compare/v0.2.0...v0.3.0
@@ -39,14 +39,18 @@ module X402
39
39
  # @param payee_locking_script_hex [String, nil] static payee script hex
40
40
  # @param recogniser [#ours?, nil] object that recognises derived payment addresses.
41
41
  # Takes precedence over +payee_locking_script_hex+ when both are provided.
42
+ # @param extractor [#call, nil] extracts a +BSV::Transaction::Transaction+ from a
43
+ # raw proof header value. Returns a Transaction or +nil+ to skip.
44
+ # Defaults to +CoinbaseV2Extractor+ (Base64-JSON envelope with +payload.rawtx+).
42
45
  # @param proof_headers [Array<String>] HTTP header names to watch for payments
43
46
  # @param on_payment [#call, nil] optional callback invoked with the raw tx
44
47
  # binary after successful enqueue, for application-level tracking
45
48
  def initialize(app, worker:, payee_locking_script_hex: nil, recogniser: nil,
46
- proof_headers: DEFAULT_PROOF_HEADERS, on_payment: nil)
49
+ extractor: nil, proof_headers: DEFAULT_PROOF_HEADERS, on_payment: nil)
47
50
  @app = app
48
51
  @worker = worker
49
52
  @recogniser = build_recogniser(recogniser, payee_locking_script_hex)
53
+ @extractor = build_extractor(extractor)
50
54
  @proof_headers = proof_headers
51
55
  @on_payment = on_payment
52
56
  end
@@ -75,6 +79,17 @@ module X402
75
79
  StaticRecogniser.new(payee_hex)
76
80
  end
77
81
 
82
+ def build_extractor(extractor)
83
+ return CoinbaseV2Extractor.new unless extractor
84
+
85
+ unless extractor.respond_to?(:call)
86
+ raise ConfigurationError,
87
+ "PaymentObserver extractor must respond to #call(proof_payload)"
88
+ end
89
+
90
+ extractor
91
+ end
92
+
78
93
  def observe_payment(env)
79
94
  @proof_headers.each do |header_name|
80
95
  rack_key = "HTTP_#{header_name.upcase.tr("-", "_")}"
@@ -97,16 +112,11 @@ module X402
97
112
  end
98
113
 
99
114
  def extract_and_validate(proof_payload)
100
- json = Base64.strict_decode64(proof_payload)
101
- payload = JSON.parse(json)
102
- rawtx_hex = payload.dig("payload", "rawtx")
103
- return unless rawtx_hex
104
-
105
- tx_binary = [rawtx_hex].pack("H*")
106
- tx = ::BSV::Transaction::Transaction.from_binary(tx_binary)
107
- return unless recognised?(tx)
115
+ transaction = @extractor.call(proof_payload)
116
+ return unless transaction
117
+ return unless recognised?(transaction)
108
118
 
109
- tx_binary
119
+ transaction.to_binary
110
120
  rescue StandardError
111
121
  nil
112
122
  end
@@ -117,6 +127,23 @@ module X402
117
127
  end
118
128
  end
119
129
 
130
+ # Built-in extractor for the Coinbase v2 envelope format.
131
+ # Decodes +Base64(JSON({ payload: { rawtx: "hex" } }))+.
132
+ # Returns a +BSV::Transaction::Transaction+ or +nil+.
133
+ class CoinbaseV2Extractor
134
+ def call(proof_payload)
135
+ json = Base64.strict_decode64(proof_payload)
136
+ payload = JSON.parse(json)
137
+ rawtx_hex = payload.dig("payload", "rawtx")
138
+ return unless rawtx_hex
139
+
140
+ tx_binary = [rawtx_hex].pack("H*")
141
+ ::BSV::Transaction::Transaction.from_binary(tx_binary)
142
+ rescue StandardError
143
+ nil
144
+ end
145
+ end
146
+
120
147
  # Built-in recogniser for a single static payee address.
121
148
  # Used when +payee_locking_script_hex+ is provided without a custom recogniser.
122
149
  class StaticRecogniser
data/lib/x402/version.rb CHANGED
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module X402
4
- VERSION = "0.5.0"
4
+ VERSION = "0.5.1"
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: x402-rack
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.5.0
4
+ version: 0.5.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Simon Bettison