bsv-wallet-postgres 0.1.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-wallet-postgres.md +55 -0
- data/LICENSE +86 -0
- data/lib/bsv/wallet_postgres/migrations/001_create_wallet_tables.rb +58 -0
- data/lib/bsv/wallet_postgres/postgres_store.rb +291 -0
- data/lib/bsv/wallet_postgres/version.rb +7 -0
- data/lib/bsv/wallet_postgres.rb +12 -0
- data/lib/bsv-wallet-postgres.rb +5 -0
- metadata +98 -0
checksums.yaml
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
---
|
|
2
|
+
SHA256:
|
|
3
|
+
metadata.gz: 9afef26a167d09125962a45afc6d24d0233de215847c34233346fc1dbba11f34
|
|
4
|
+
data.tar.gz: 453a4d2d63f1735beae93aeb4e32ef37c9a15ab09141865a7789b736824b4507
|
|
5
|
+
SHA512:
|
|
6
|
+
metadata.gz: 600ceec193391521686eae2a52201e905cd9d9ac495a07060747eb7e3b97b5d8bf1172774d1b67f16d15328d94e51e20eab2c1afd13c7e7042221f053fd99537
|
|
7
|
+
data.tar.gz: 60925f55c22d87445398817f1368096df57b639b2e439b60e09a40ecc716c6d70abaeee49294b8bef9f30d92bf41d88906a9e45000be8ca03d6c07214733bb64
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
# Changelog — bsv-wallet-postgres
|
|
2
|
+
|
|
3
|
+
All notable changes to the `bsv-wallet-postgres` gem are documented here.
|
|
4
|
+
|
|
5
|
+
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/)
|
|
6
|
+
and this gem adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
|
|
7
|
+
|
|
8
|
+
## 0.1.0 — 2026-04-09
|
|
9
|
+
|
|
10
|
+
Initial release of `bsv-wallet-postgres`, a PostgreSQL-backed
|
|
11
|
+
`BSV::Wallet::StorageAdapter` implementation. Unblocks production
|
|
12
|
+
deployments of `bsv-wallet` where state has to survive container
|
|
13
|
+
restarts, and makes multi-instance wallet services possible for the
|
|
14
|
+
first time.
|
|
15
|
+
|
|
16
|
+
### Added
|
|
17
|
+
|
|
18
|
+
- **`BSV::Wallet::PostgresStore`** — full
|
|
19
|
+
`StorageAdapter` implementation over Sequel + Postgres. Passes the
|
|
20
|
+
same shared conformance suite that MemoryStore and FileStore pass
|
|
21
|
+
(53 examples), plus 10 postgres-specific specs covering upsert
|
|
22
|
+
semantics, GIN tag queries, JSONB attribute containment, concurrent
|
|
23
|
+
inserts, and migration idempotency.
|
|
24
|
+
|
|
25
|
+
- **Shipped Sequel migration** at
|
|
26
|
+
`lib/bsv/wallet_postgres/migrations/001_create_wallet_tables.rb`.
|
|
27
|
+
Five tables (wallet_outputs, wallet_actions, wallet_certificates,
|
|
28
|
+
wallet_proofs, wallet_transactions) with JSONB data columns,
|
|
29
|
+
dedicated indexed columns for filter paths, and GIN indexes on the
|
|
30
|
+
`tags` / `labels` arrays.
|
|
31
|
+
|
|
32
|
+
- **`PostgresStore.migrate!(db)`** convenience
|
|
33
|
+
wrapper over `Sequel::Migrator.run` so consumers can apply the
|
|
34
|
+
shipped schema with a single call. Operators who prefer their own
|
|
35
|
+
migration framework can copy the migration file instead.
|
|
36
|
+
|
|
37
|
+
- **Docs** at `docs/guides/wallet-postgres.md` with
|
|
38
|
+
a 30-second quickstart, schema overview, and production
|
|
39
|
+
considerations (pool sizing, multi-instance, backups,
|
|
40
|
+
thread-safety).
|
|
41
|
+
|
|
42
|
+
### Infrastructure
|
|
43
|
+
|
|
44
|
+
- **CI postgres service**. The GitHub Actions test job now runs a
|
|
45
|
+
Postgres 16 container and exposes `DATABASE_URL` to rspec, so the
|
|
46
|
+
`:postgres`-tagged specs run against a live database on every
|
|
47
|
+
Ruby matrix row (2.7 → 3.4). Local developers without Postgres
|
|
48
|
+
still get a green suite — those specs skip gracefully.
|
|
49
|
+
|
|
50
|
+
### Dependencies
|
|
51
|
+
|
|
52
|
+
- `bsv-wallet-postgres` runtime: `bsv-wallet >= 0.3.4, < 1.0`,
|
|
53
|
+
`sequel ~> 5`, `pg ~> 1`. The wallet floor matches the pinning style
|
|
54
|
+
`bsv-wallet` uses for its `bsv-sdk` dependency so security releases
|
|
55
|
+
propagate.
|
data/LICENSE
ADDED
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
Open BSV License Version 5 – granted by BSV Association, Grafenauweg 6, 6300
|
|
2
|
+
Zug, Switzerland (CHE-427.008.338) ("Licensor"), to you as a user (henceforth
|
|
3
|
+
"You", "User" or "Licensee").
|
|
4
|
+
|
|
5
|
+
For the purposes of this license, the definitions below have the following
|
|
6
|
+
meanings:
|
|
7
|
+
|
|
8
|
+
"Bitcoin Protocol" means the protocol implementation, cryptographic rules,
|
|
9
|
+
network protocols, and consensus mechanisms in the Bitcoin White Paper as
|
|
10
|
+
described here https://protocol.bsvblockchain.org.
|
|
11
|
+
|
|
12
|
+
"Bitcoin White Paper" means the paper entitled 'Bitcoin: A Peer-to-Peer
|
|
13
|
+
Electronic Cash System' published by 'Satoshi Nakamoto' in October 2008.
|
|
14
|
+
|
|
15
|
+
"BSV Blockchains" means:
|
|
16
|
+
(a) the Bitcoin blockchain containing block height #556767 with the hash
|
|
17
|
+
"000000000000000001d956714215d96ffc00e0afda4cd0a96c96f8d802b1662b" and
|
|
18
|
+
that contains the longest honest persistent chain of blocks which has been
|
|
19
|
+
produced in a manner which is consistent with the rules set forth in the
|
|
20
|
+
Network Access Rules; and
|
|
21
|
+
(b) the test blockchains that contain the longest honest persistent chains of
|
|
22
|
+
blocks which has been produced in a manner which is consistent with the
|
|
23
|
+
rules set forth in the Network Access Rules.
|
|
24
|
+
|
|
25
|
+
"Network Access Rules" or "Rules" means the set of rules regulating the
|
|
26
|
+
relationship between BSV Association and the nodes on BSV based on the Bitcoin
|
|
27
|
+
Protocol rules and those set out in the Bitcoin White Paper, and available here
|
|
28
|
+
https://bsvblockchain.org/network-access-rules.
|
|
29
|
+
|
|
30
|
+
"Software" means the software the subject of this licence, including any/all
|
|
31
|
+
intellectual property rights therein and associated documentation files.
|
|
32
|
+
|
|
33
|
+
BSV Association grants permission, free of charge and on a non-exclusive and
|
|
34
|
+
revocable basis, to any person obtaining a copy of the Software to deal in the
|
|
35
|
+
Software without restriction, including without limitation the rights to use,
|
|
36
|
+
copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the
|
|
37
|
+
Software, and to permit persons to whom the Software is furnished to do so,
|
|
38
|
+
subject to and conditioned upon the following conditions:
|
|
39
|
+
|
|
40
|
+
1 - The text "© BSV Association," and this license shall be included in all
|
|
41
|
+
copies or substantial portions of the Software.
|
|
42
|
+
2 - The Software, and any software that is derived from the Software or parts
|
|
43
|
+
thereof, must only be used on the BSV Blockchains.
|
|
44
|
+
|
|
45
|
+
For the avoidance of doubt, this license is granted subject to and conditioned
|
|
46
|
+
upon your compliance with these terms only. In the event of non-compliance, the
|
|
47
|
+
license shall extinguish and you can be enjoined from violating BSV's
|
|
48
|
+
intellectual property rights (incl. damages and similar related claims).
|
|
49
|
+
|
|
50
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
51
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES REGARDING ENTITLEMENT,
|
|
52
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO
|
|
53
|
+
EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS THEREOF BE LIABLE FOR ANY CLAIM,
|
|
54
|
+
DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
|
|
55
|
+
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
|
|
56
|
+
DEALINGS IN THE SOFTWARE.
|
|
57
|
+
|
|
58
|
+
|
|
59
|
+
Version 0.1.1 of the Bitcoin SV software, and prior versions of software upon
|
|
60
|
+
which it was based, were licensed under the MIT License, which is included below.
|
|
61
|
+
|
|
62
|
+
The MIT License (MIT)
|
|
63
|
+
|
|
64
|
+
Copyright (c) 2009-2010 Satoshi Nakamoto
|
|
65
|
+
Copyright (c) 2009-2015 Bitcoin Developers
|
|
66
|
+
Copyright (c) 2009-2017 The Bitcoin Core developers
|
|
67
|
+
Copyright (c) 2017 The Bitcoin ABC developers
|
|
68
|
+
Copyright (c) 2018 Bitcoin Association for BSV
|
|
69
|
+
Copyright (c) 2023 BSV Association
|
|
70
|
+
|
|
71
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of
|
|
72
|
+
this software and associated documentation files (the "Software"), to deal in
|
|
73
|
+
the Software without restriction, including without limitation the rights to
|
|
74
|
+
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
|
|
75
|
+
the Software, and to permit persons to whom the Software is furnished to do so,
|
|
76
|
+
subject to the following conditions:
|
|
77
|
+
|
|
78
|
+
The above copyright notice and this permission notice shall be included in all
|
|
79
|
+
copies or substantial portions of the Software.
|
|
80
|
+
|
|
81
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
82
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
|
|
83
|
+
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
|
|
84
|
+
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
|
|
85
|
+
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
|
86
|
+
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
# Initial wallet schema for BSV::Wallet::PostgresStore.
|
|
4
|
+
#
|
|
5
|
+
# Five tables, all indexed for the query patterns the StorageAdapter
|
|
6
|
+
# interface exposes. JSONB blobs hold the full hash the SDK stored so
|
|
7
|
+
# adding fields to bsv-wallet's output/action/certificate records does
|
|
8
|
+
# not require a schema change — only the fields we actively filter on
|
|
9
|
+
# get dedicated indexed columns.
|
|
10
|
+
Sequel.migration do
|
|
11
|
+
change do
|
|
12
|
+
create_table(:wallet_outputs) do
|
|
13
|
+
primary_key :id
|
|
14
|
+
String :outpoint, null: false, unique: true
|
|
15
|
+
String :basket
|
|
16
|
+
column :tags, 'text[]'
|
|
17
|
+
TrueClass :spendable, null: false, default: true
|
|
18
|
+
jsonb :data, null: false
|
|
19
|
+
DateTime :created_at, null: false, default: Sequel::CURRENT_TIMESTAMP
|
|
20
|
+
index %i[basket spendable]
|
|
21
|
+
index :tags, type: :gin
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
create_table(:wallet_actions) do
|
|
25
|
+
primary_key :id
|
|
26
|
+
String :txid, null: false
|
|
27
|
+
column :labels, 'text[]'
|
|
28
|
+
jsonb :data, null: false
|
|
29
|
+
DateTime :created_at, null: false, default: Sequel::CURRENT_TIMESTAMP
|
|
30
|
+
index :labels, type: :gin
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
create_table(:wallet_certificates) do
|
|
34
|
+
primary_key :id
|
|
35
|
+
String :type, null: false
|
|
36
|
+
String :serial_number, null: false
|
|
37
|
+
String :certifier, null: false
|
|
38
|
+
String :subject
|
|
39
|
+
jsonb :data, null: false
|
|
40
|
+
DateTime :created_at, null: false, default: Sequel::CURRENT_TIMESTAMP
|
|
41
|
+
unique %i[type serial_number certifier], name: :wallet_certificates_natural_key
|
|
42
|
+
index :certifier
|
|
43
|
+
index :subject
|
|
44
|
+
end
|
|
45
|
+
|
|
46
|
+
create_table(:wallet_proofs) do
|
|
47
|
+
String :txid, primary_key: true
|
|
48
|
+
String :bump_hex, text: true, null: false
|
|
49
|
+
DateTime :created_at, null: false, default: Sequel::CURRENT_TIMESTAMP
|
|
50
|
+
end
|
|
51
|
+
|
|
52
|
+
create_table(:wallet_transactions) do
|
|
53
|
+
String :txid, primary_key: true
|
|
54
|
+
String :tx_hex, text: true, null: false
|
|
55
|
+
DateTime :created_at, null: false, default: Sequel::CURRENT_TIMESTAMP
|
|
56
|
+
end
|
|
57
|
+
end
|
|
58
|
+
end
|
|
@@ -0,0 +1,291 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require 'sequel'
|
|
4
|
+
require 'sequel/extensions/migration'
|
|
5
|
+
require 'json'
|
|
6
|
+
|
|
7
|
+
module BSV
|
|
8
|
+
module Wallet
|
|
9
|
+
# PostgreSQL-backed storage adapter for +BSV::Wallet+.
|
|
10
|
+
#
|
|
11
|
+
# Implements the full {StorageAdapter} interface against a Sequel
|
|
12
|
+
# +Database+ object. Survives process restarts, scales to multiple
|
|
13
|
+
# instances, and is thread-safe via Sequel's connection pool.
|
|
14
|
+
#
|
|
15
|
+
# @example Quickstart
|
|
16
|
+
# require 'bsv-wallet-postgres'
|
|
17
|
+
#
|
|
18
|
+
# db = Sequel.connect(ENV['DATABASE_URL'])
|
|
19
|
+
# BSV::Wallet::PostgresStore.migrate!(db)
|
|
20
|
+
#
|
|
21
|
+
# store = BSV::Wallet::PostgresStore.new(db)
|
|
22
|
+
# wallet = BSV::Wallet::WalletClient.new(key, storage: store)
|
|
23
|
+
#
|
|
24
|
+
# @example Bringing your own migration runner
|
|
25
|
+
# # Copy lib/bsv/wallet_postgres/migrations/001_create_wallet_tables.rb
|
|
26
|
+
# # into your own db/migrate directory, then run your framework's
|
|
27
|
+
# # migrator as normal. `migrate!` is a convenience — not a requirement.
|
|
28
|
+
#
|
|
29
|
+
# === Design notes
|
|
30
|
+
#
|
|
31
|
+
# * JSONB is the source of truth. Every row stores the full record
|
|
32
|
+
# hash in a +data+ jsonb column; dedicated indexed columns
|
|
33
|
+
# (+basket+, +tags+, +labels+, +certifier+, ...) exist only to make
|
|
34
|
+
# queries fast. Reads return the jsonb blob so adding fields to
|
|
35
|
+
# bsv-wallet's record hashes does not require a schema change.
|
|
36
|
+
#
|
|
37
|
+
# * Outputs upsert on +outpoint+ (unique); certificates upsert on the
|
|
38
|
+
# composite unique +(type, serial_number, certifier)+. Proofs and
|
|
39
|
+
# transactions upsert on their +txid+ primary key. Actions are
|
|
40
|
+
# append-only — the interface has no natural key for actions.
|
|
41
|
+
#
|
|
42
|
+
# * Pagination is ordered by insertion (+id ASC+) to match MemoryStore.
|
|
43
|
+
#
|
|
44
|
+
# * This class is thread-safe because Sequel is — the adapter itself
|
|
45
|
+
# holds no mutable state beyond the injected database handle.
|
|
46
|
+
class PostgresStore
|
|
47
|
+
include StorageAdapter
|
|
48
|
+
|
|
49
|
+
MIGRATIONS_DIR = File.expand_path('migrations', __dir__)
|
|
50
|
+
|
|
51
|
+
# Run the shipped wallet schema migrations against +db+.
|
|
52
|
+
#
|
|
53
|
+
# Uses Sequel's migrator so every schema change ships as a numbered
|
|
54
|
+
# migration file and the database tracks which ones have been
|
|
55
|
+
# applied. Safe to call repeatedly.
|
|
56
|
+
#
|
|
57
|
+
# Consumers who prefer their own migration framework can copy the
|
|
58
|
+
# migration file(s) out of +lib/bsv/wallet_postgres/migrations/+
|
|
59
|
+
# instead of calling this helper.
|
|
60
|
+
#
|
|
61
|
+
# @param db [Sequel::Database]
|
|
62
|
+
# @return [void]
|
|
63
|
+
def self.migrate!(db)
|
|
64
|
+
Sequel::Migrator.run(db, MIGRATIONS_DIR)
|
|
65
|
+
end
|
|
66
|
+
|
|
67
|
+
# Register the global Sequel query-builder helpers used by this class
|
|
68
|
+
# (+Sequel.pg_array_op+ / +Sequel.pg_jsonb_op+). Unlike the per-database
|
|
69
|
+
# +pg_array+ / +pg_json+ extensions loaded in +initialize+, +pg_array_ops+
|
|
70
|
+
# and +pg_json_ops+ are global — they mutate Sequel's top-level namespace
|
|
71
|
+
# the first time this class body is evaluated (typically on autoload).
|
|
72
|
+
# This is an intentional side effect: any consumer that has
|
|
73
|
+
# +require 'bsv-wallet-postgres'+ in their Gemfile has opted in.
|
|
74
|
+
Sequel.extension :pg_array_ops
|
|
75
|
+
Sequel.extension :pg_json_ops
|
|
76
|
+
|
|
77
|
+
# @param db [Sequel::Database] a Sequel database handle. The caller
|
|
78
|
+
# owns connection lifecycle, pool sizing, and migrations.
|
|
79
|
+
def initialize(db)
|
|
80
|
+
@db = db
|
|
81
|
+
@db.extension :pg_array
|
|
82
|
+
@db.extension :pg_json
|
|
83
|
+
end
|
|
84
|
+
|
|
85
|
+
# @return [Sequel::Database] the underlying database handle
|
|
86
|
+
attr_reader :db
|
|
87
|
+
|
|
88
|
+
# --- Actions ---
|
|
89
|
+
|
|
90
|
+
def store_action(action_data)
|
|
91
|
+
row = action_row(action_data)
|
|
92
|
+
@db[:wallet_actions].insert(row)
|
|
93
|
+
action_data
|
|
94
|
+
end
|
|
95
|
+
|
|
96
|
+
def find_actions(query)
|
|
97
|
+
ds = filter_actions(@db[:wallet_actions], query)
|
|
98
|
+
paginate(ds, query).map { |r| symbolise_keys(r[:data]) }
|
|
99
|
+
end
|
|
100
|
+
|
|
101
|
+
def count_actions(query)
|
|
102
|
+
filter_actions(@db[:wallet_actions], query).count
|
|
103
|
+
end
|
|
104
|
+
|
|
105
|
+
# --- Outputs ---
|
|
106
|
+
|
|
107
|
+
def store_output(output_data)
|
|
108
|
+
row = output_row(output_data)
|
|
109
|
+
@db[:wallet_outputs]
|
|
110
|
+
.insert_conflict(
|
|
111
|
+
target: :outpoint,
|
|
112
|
+
update: { basket: row[:basket], tags: row[:tags], spendable: row[:spendable], data: row[:data] }
|
|
113
|
+
)
|
|
114
|
+
.insert(row)
|
|
115
|
+
output_data
|
|
116
|
+
end
|
|
117
|
+
|
|
118
|
+
def find_outputs(query)
|
|
119
|
+
ds = filter_outputs(@db[:wallet_outputs], query)
|
|
120
|
+
paginate(ds, query).map { |r| symbolise_keys(r[:data]) }
|
|
121
|
+
end
|
|
122
|
+
|
|
123
|
+
def count_outputs(query)
|
|
124
|
+
filter_outputs(@db[:wallet_outputs], query).count
|
|
125
|
+
end
|
|
126
|
+
|
|
127
|
+
def delete_output(outpoint)
|
|
128
|
+
@db[:wallet_outputs].where(outpoint: outpoint).delete.positive?
|
|
129
|
+
end
|
|
130
|
+
|
|
131
|
+
# --- Certificates ---
|
|
132
|
+
|
|
133
|
+
def store_certificate(cert_data)
|
|
134
|
+
row = certificate_row(cert_data)
|
|
135
|
+
@db[:wallet_certificates]
|
|
136
|
+
.insert_conflict(
|
|
137
|
+
target: %i[type serial_number certifier],
|
|
138
|
+
update: { subject: row[:subject], data: row[:data] }
|
|
139
|
+
)
|
|
140
|
+
.insert(row)
|
|
141
|
+
cert_data
|
|
142
|
+
end
|
|
143
|
+
|
|
144
|
+
def find_certificates(query)
|
|
145
|
+
ds = filter_certificates(@db[:wallet_certificates], query)
|
|
146
|
+
paginate(ds, query).map { |r| symbolise_keys(r[:data]) }
|
|
147
|
+
end
|
|
148
|
+
|
|
149
|
+
def count_certificates(query)
|
|
150
|
+
filter_certificates(@db[:wallet_certificates], query).count
|
|
151
|
+
end
|
|
152
|
+
|
|
153
|
+
def delete_certificate(type:, serial_number:, certifier:)
|
|
154
|
+
@db[:wallet_certificates]
|
|
155
|
+
.where(type: type, serial_number: serial_number, certifier: certifier)
|
|
156
|
+
.delete
|
|
157
|
+
.positive?
|
|
158
|
+
end
|
|
159
|
+
|
|
160
|
+
# --- Proofs ---
|
|
161
|
+
|
|
162
|
+
def store_proof(txid, bump_hex)
|
|
163
|
+
@db[:wallet_proofs]
|
|
164
|
+
.insert_conflict(target: :txid, update: { bump_hex: bump_hex })
|
|
165
|
+
.insert(txid: txid, bump_hex: bump_hex)
|
|
166
|
+
end
|
|
167
|
+
|
|
168
|
+
def find_proof(txid)
|
|
169
|
+
@db[:wallet_proofs].where(txid: txid).get(:bump_hex)
|
|
170
|
+
end
|
|
171
|
+
|
|
172
|
+
# --- Transactions ---
|
|
173
|
+
|
|
174
|
+
def store_transaction(txid, tx_hex)
|
|
175
|
+
@db[:wallet_transactions]
|
|
176
|
+
.insert_conflict(target: :txid, update: { tx_hex: tx_hex })
|
|
177
|
+
.insert(txid: txid, tx_hex: tx_hex)
|
|
178
|
+
end
|
|
179
|
+
|
|
180
|
+
def find_transaction(txid)
|
|
181
|
+
@db[:wallet_transactions].where(txid: txid).get(:tx_hex)
|
|
182
|
+
end
|
|
183
|
+
|
|
184
|
+
private
|
|
185
|
+
|
|
186
|
+
# --- Row builders ---
|
|
187
|
+
|
|
188
|
+
def action_row(data)
|
|
189
|
+
{
|
|
190
|
+
txid: data[:txid],
|
|
191
|
+
labels: Sequel.pg_array(Array(data[:labels]), :text),
|
|
192
|
+
data: Sequel.pg_jsonb(data.to_h)
|
|
193
|
+
}
|
|
194
|
+
end
|
|
195
|
+
|
|
196
|
+
def output_row(data)
|
|
197
|
+
spendable = data[:spendable] != false # nil treated as spendable, like MemoryStore
|
|
198
|
+
{
|
|
199
|
+
outpoint: data[:outpoint],
|
|
200
|
+
basket: data[:basket],
|
|
201
|
+
tags: Sequel.pg_array(Array(data[:tags]), :text),
|
|
202
|
+
spendable: spendable,
|
|
203
|
+
data: Sequel.pg_jsonb(data.to_h)
|
|
204
|
+
}
|
|
205
|
+
end
|
|
206
|
+
|
|
207
|
+
def certificate_row(data)
|
|
208
|
+
{
|
|
209
|
+
type: data[:type],
|
|
210
|
+
serial_number: data[:serial_number],
|
|
211
|
+
certifier: data[:certifier],
|
|
212
|
+
subject: data[:subject],
|
|
213
|
+
data: Sequel.pg_jsonb(data.to_h)
|
|
214
|
+
}
|
|
215
|
+
end
|
|
216
|
+
|
|
217
|
+
# --- Filters ---
|
|
218
|
+
|
|
219
|
+
def filter_actions(ds, query)
|
|
220
|
+
apply_array_filter(ds, :labels, query[:labels], query[:label_query_mode])
|
|
221
|
+
end
|
|
222
|
+
|
|
223
|
+
def filter_outputs(ds, query)
|
|
224
|
+
ds = ds.where(outpoint: query[:outpoint]) if query[:outpoint]
|
|
225
|
+
ds = ds.where(basket: query[:basket]) if query[:basket]
|
|
226
|
+
ds = apply_array_filter(ds, :tags, query[:tags], query[:tag_query_mode])
|
|
227
|
+
ds = ds.where(spendable: true) unless query[:include_spent]
|
|
228
|
+
ds
|
|
229
|
+
end
|
|
230
|
+
|
|
231
|
+
def filter_certificates(ds, query)
|
|
232
|
+
ds = ds.where(certifier: query[:certifiers]) if query[:certifiers]
|
|
233
|
+
ds = ds.where(type: query[:types]) if query[:types]
|
|
234
|
+
ds = ds.where(subject: query[:subject]) if query[:subject]
|
|
235
|
+
ds = apply_attributes_filter(ds, query[:attributes]) if query[:attributes]
|
|
236
|
+
ds
|
|
237
|
+
end
|
|
238
|
+
|
|
239
|
+
def apply_array_filter(ds, column, values, mode)
|
|
240
|
+
return ds unless values
|
|
241
|
+
|
|
242
|
+
array = Sequel.pg_array(Array(values), :text)
|
|
243
|
+
op = Sequel.pg_array_op(column)
|
|
244
|
+
if mode == 'all'
|
|
245
|
+
ds.where(op.contains(array))
|
|
246
|
+
else
|
|
247
|
+
ds.where(op.overlaps(array))
|
|
248
|
+
end
|
|
249
|
+
end
|
|
250
|
+
|
|
251
|
+
def apply_attributes_filter(ds, attrs)
|
|
252
|
+
# Match certificates whose stored fields hash contains every
|
|
253
|
+
# key/value pair in +attrs+. Symbol keys stringify when the
|
|
254
|
+
# record is serialised to JSONB, so symbol/string keys both work.
|
|
255
|
+
fragment = attrs.each_with_object({}) { |(k, v), h| h[k.to_s] = v }
|
|
256
|
+
ds.where(Sequel.lit('data->\'fields\' @> ?::jsonb', fragment.to_json))
|
|
257
|
+
end
|
|
258
|
+
|
|
259
|
+
# --- Pagination ---
|
|
260
|
+
|
|
261
|
+
def paginate(ds, query)
|
|
262
|
+
ds.order(:id)
|
|
263
|
+
.offset(query[:offset] || 0)
|
|
264
|
+
.limit(query[:limit] || 10)
|
|
265
|
+
.all
|
|
266
|
+
end
|
|
267
|
+
|
|
268
|
+
# --- JSONB read helpers ---
|
|
269
|
+
|
|
270
|
+
# Recursively convert string-keyed hashes to symbol-keyed hashes so
|
|
271
|
+
# reads round-trip with MemoryStore's contract. pg_json returns
|
|
272
|
+
# string keys by default wrapped in +Sequel::Postgres::JSONBHash+
|
|
273
|
+
# / +JSONBArray+, which are +DelegateClass+-based — not Hash/Array
|
|
274
|
+
# subclasses — so the case statement uses +to_hash+ / +to_ary+
|
|
275
|
+
# coercion to handle them.
|
|
276
|
+
def symbolise_keys(obj)
|
|
277
|
+
case obj
|
|
278
|
+
when Hash
|
|
279
|
+
obj.each_with_object({}) { |(k, v), h| h[k.to_sym] = symbolise_keys(v) }
|
|
280
|
+
when Array
|
|
281
|
+
obj.map { |e| symbolise_keys(e) }
|
|
282
|
+
else
|
|
283
|
+
return symbolise_keys(obj.to_hash) if obj.respond_to?(:to_hash)
|
|
284
|
+
return symbolise_keys(obj.to_ary) if obj.respond_to?(:to_ary)
|
|
285
|
+
|
|
286
|
+
obj
|
|
287
|
+
end
|
|
288
|
+
end
|
|
289
|
+
end
|
|
290
|
+
end
|
|
291
|
+
end
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module BSV
|
|
4
|
+
# Top-level module for the bsv-wallet-postgres gem.
|
|
5
|
+
module WalletPostgres
|
|
6
|
+
autoload :VERSION, 'bsv/wallet_postgres/version'
|
|
7
|
+
end
|
|
8
|
+
|
|
9
|
+
module Wallet
|
|
10
|
+
autoload :PostgresStore, 'bsv/wallet_postgres/postgres_store'
|
|
11
|
+
end
|
|
12
|
+
end
|
metadata
ADDED
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: bsv-wallet-postgres
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
version: 0.1.0
|
|
5
|
+
platform: ruby
|
|
6
|
+
authors:
|
|
7
|
+
- Simon Bettison
|
|
8
|
+
bindir: bin
|
|
9
|
+
cert_chain: []
|
|
10
|
+
date: 2026-04-09 00:00:00.000000000 Z
|
|
11
|
+
dependencies:
|
|
12
|
+
- !ruby/object:Gem::Dependency
|
|
13
|
+
name: bsv-wallet
|
|
14
|
+
requirement: !ruby/object:Gem::Requirement
|
|
15
|
+
requirements:
|
|
16
|
+
- - ">="
|
|
17
|
+
- !ruby/object:Gem::Version
|
|
18
|
+
version: 0.3.4
|
|
19
|
+
- - "<"
|
|
20
|
+
- !ruby/object:Gem::Version
|
|
21
|
+
version: '1.0'
|
|
22
|
+
type: :runtime
|
|
23
|
+
prerelease: false
|
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
25
|
+
requirements:
|
|
26
|
+
- - ">="
|
|
27
|
+
- !ruby/object:Gem::Version
|
|
28
|
+
version: 0.3.4
|
|
29
|
+
- - "<"
|
|
30
|
+
- !ruby/object:Gem::Version
|
|
31
|
+
version: '1.0'
|
|
32
|
+
- !ruby/object:Gem::Dependency
|
|
33
|
+
name: pg
|
|
34
|
+
requirement: !ruby/object:Gem::Requirement
|
|
35
|
+
requirements:
|
|
36
|
+
- - "~>"
|
|
37
|
+
- !ruby/object:Gem::Version
|
|
38
|
+
version: '1'
|
|
39
|
+
type: :runtime
|
|
40
|
+
prerelease: false
|
|
41
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
42
|
+
requirements:
|
|
43
|
+
- - "~>"
|
|
44
|
+
- !ruby/object:Gem::Version
|
|
45
|
+
version: '1'
|
|
46
|
+
- !ruby/object:Gem::Dependency
|
|
47
|
+
name: sequel
|
|
48
|
+
requirement: !ruby/object:Gem::Requirement
|
|
49
|
+
requirements:
|
|
50
|
+
- - "~>"
|
|
51
|
+
- !ruby/object:Gem::Version
|
|
52
|
+
version: '5'
|
|
53
|
+
type: :runtime
|
|
54
|
+
prerelease: false
|
|
55
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
56
|
+
requirements:
|
|
57
|
+
- - "~>"
|
|
58
|
+
- !ruby/object:Gem::Version
|
|
59
|
+
version: '5'
|
|
60
|
+
description: Persistent Sequel/Postgres-backed BSV::Wallet::StorageAdapter implementation
|
|
61
|
+
for production BSV wallet deployments.
|
|
62
|
+
executables: []
|
|
63
|
+
extensions: []
|
|
64
|
+
extra_rdoc_files: []
|
|
65
|
+
files:
|
|
66
|
+
- CHANGELOG-wallet-postgres.md
|
|
67
|
+
- LICENSE
|
|
68
|
+
- lib/bsv-wallet-postgres.rb
|
|
69
|
+
- lib/bsv/wallet_postgres.rb
|
|
70
|
+
- lib/bsv/wallet_postgres/migrations/001_create_wallet_tables.rb
|
|
71
|
+
- lib/bsv/wallet_postgres/postgres_store.rb
|
|
72
|
+
- lib/bsv/wallet_postgres/version.rb
|
|
73
|
+
homepage: https://github.com/sgbett/bsv-ruby-sdk
|
|
74
|
+
licenses:
|
|
75
|
+
- LicenseRef-OpenBSV
|
|
76
|
+
metadata:
|
|
77
|
+
homepage_uri: https://github.com/sgbett/bsv-ruby-sdk
|
|
78
|
+
source_code_uri: https://github.com/sgbett/bsv-ruby-sdk
|
|
79
|
+
changelog_uri: https://github.com/sgbett/bsv-ruby-sdk/blob/master/CHANGELOG-wallet-postgres.md
|
|
80
|
+
rubygems_mfa_required: 'true'
|
|
81
|
+
rdoc_options: []
|
|
82
|
+
require_paths:
|
|
83
|
+
- lib
|
|
84
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
|
85
|
+
requirements:
|
|
86
|
+
- - ">="
|
|
87
|
+
- !ruby/object:Gem::Version
|
|
88
|
+
version: '2.7'
|
|
89
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
90
|
+
requirements:
|
|
91
|
+
- - ">="
|
|
92
|
+
- !ruby/object:Gem::Version
|
|
93
|
+
version: '0'
|
|
94
|
+
requirements: []
|
|
95
|
+
rubygems_version: 3.6.2
|
|
96
|
+
specification_version: 4
|
|
97
|
+
summary: PostgreSQL storage adapter for bsv-wallet
|
|
98
|
+
test_files: []
|