Skip to content

Commit fd32949

Browse files
authored
fix: confirm invoice function ambiguous unit variable (#221)
* fix: dont crash when SECRET is not set * docs: add semisol to contributors * docs: improve readme * docs: add payment info to readme * docs: add zebedee_api_key to configuration.md * fix: confirm_invoice unit var * chore: remove unused code * chore: improve error logging for payments * chore: use instead of changeme * chore: fix typo * chore: improve get invoice status ctrl * fix: csp bug * chore: remove rate limits * chore: improve invoice page logging * chore: prevent root with start_local * chore: revert to redis 4.5.1
1 parent e99ac5d commit fd32949

File tree

16 files changed

+225
-94
lines changed

16 files changed

+225
-94
lines changed

CONFIGURATION.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ The following environment variables can be set:
66

77
| Name | Description | Default |
88
|----------------------------------|--------------------------------|------------------------|
9+
| SECRET | Long random secret. | changeme |
910
| RELAY_PORT | Relay's server port | 8008 |
1011
| RELAY_PRIVATE_KEY | Relay's private key in hex | (auto-generated) |
1112
| WORKER_COUNT | Number of workers override | No. of available CPUs |
@@ -38,6 +39,7 @@ The following environment variables can be set:
3839
| REDIS_PASSWORD | Redis Password | nostr_ts_relay |
3940
| NOSTR_CONFIG_DIR | Configuration directory | <project_root>/.nostr/ |
4041
| DEBUG | Debugging filter | |
42+
| ZEBEDEE_API_KEY | Zebedee Project API Key | |
4143

4244
# Settings
4345

README.md

Lines changed: 37 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ NIPs with a relay-specific implementation are listed here.
5050
- [x] NIP-04: Encrypted Direct Message
5151
- [x] NIP-09: Event deletion
5252
- [x] NIP-11: Relay information document
53+
- [x] NIP-11a: Relay Information Document Extensions
5354
- [x] NIP-12: Generic tag queries
5455
- [x] NIP-13: Proof of Work
5556
- [x] NIP-15: End of Stored Events Notice
@@ -60,7 +61,6 @@ NIPs with a relay-specific implementation are listed here.
6061
- [x] NIP-28: Public Chat
6162
- [x] NIP-33: Parameterized Replaceable Events
6263
- [x] NIP-40: Expiration Timestamp
63-
- [x] NIP-111: Relay Information Document Extensions
6464

6565
## Requirements
6666

@@ -86,24 +86,6 @@ Install Docker from their [official guide](https://docs.docker.com/engine/instal
8686
- [Set up a Paid Nostr relay with Nostream and ZBD](https://andreneves.xyz/p/how-to-setup-a-paid-nostr-relay) by [André Neves](https://snort.social/p/npub1rvg76s0gz535txd9ypg2dfqv0x7a80ar6e096j3v343xdxyrt4ksmkxrck) (CTO & Co-Founder at [ZEBEDEE](https://zebedee.io/))
8787
- [Set up a Nostr relay in under 5 minutes](https://andreneves.xyz/p/set-up-a-nostr-relay-server-in-under) by [André Neves](https://twitter.com/andreneves) (CTO & Co-Founder at [ZEBEDEE](https://zebedee.io/))
8888

89-
## Local Quick Start (Docker Compose)
90-
91-
Install Docker Desktop following the [official guide](https://docs.docker.com/desktop/).
92-
You may have to uninstall Docker on your machine if you installed it using a different guide.
93-
94-
Clone repository and enter directory:
95-
```
96-
git clone [email protected]:Cameri/nostream.git
97-
cd nostream
98-
```
99-
100-
Start:
101-
```
102-
./scripts/start_local
103-
```
104-
105-
This will run in the foreground of the terminal until you stop it with Ctrl+C.
106-
10789
## Quick Start (Docker Compose)
10890

10991
Install Docker following the [official guide](https://docs.docker.com/engine/install/).
@@ -184,15 +166,17 @@ Set the following environment variables:
184166
```
185167
DB_URI="postgresql://postgres:postgres@localhost:5432/nostr_ts_relay_test"
186168
DB_USER=postgres
187-
169+
```
188170
or
189-
171+
```
190172
DB_HOST=localhost
191173
DB_PORT=5432
192174
DB_NAME=nostr_ts_relay
193175
DB_USER=postgres
194176
DB_PASSWORD=postgres
177+
```
195178

179+
```
196180
REDIS_URI="redis://default:nostr_ts_relay@localhost:6379"
197181
198182
REDIS_HOST=localhost
@@ -201,6 +185,19 @@ Set the following environment variables:
201185
REDIS_PASSWORD=nostr_ts_relay
202186
```
203187

188+
If enabling payments, generate a long random secret and set SECRET:
189+
You may want to use `openssl rand -hex 128` to generate a secret.
190+
191+
```
192+
SECRET=aaabbbccc...dddeeefff
193+
# Secret shortened for brevity
194+
```
195+
196+
In addition, if using Zebedee for payments, you must also set ZEBEDEE_API_KEY with
197+
an API Key from one of your projects in your Zebedee Developer Dashboard. Contact
198+
@foxp2zeb on Telegram or npub1rvg76s0gz535txd9ypg2dfqv0x7a80ar6e096j3v343xdxyrt4ksmkxrck on Nostr requesting
199+
access to the Zebedee Developer Dashboard.
200+
204201
Create `nostr_ts_relay` database:
205202

206203
```
@@ -261,6 +258,24 @@ To clean up the build, coverage and test reports run:
261258
```
262259
npm run clean
263260
```
261+
## Development Quick Start (Docker Compose)
262+
263+
Install Docker Desktop following the [official guide](https://docs.docker.com/desktop/).
264+
You may have to uninstall Docker on your machine if you installed it using a different guide.
265+
266+
Clone repository and enter directory:
267+
```
268+
git clone [email protected]:Cameri/nostream.git
269+
cd nostream
270+
```
271+
272+
Start:
273+
```
274+
./scripts/start_local
275+
```
276+
277+
This will run in the foreground of the terminal until you stop it with Ctrl+C.
278+
264279
## Tests
265280

266281
### Unit tests
@@ -391,6 +406,7 @@ I'm Cameri on most social networks. You can find me on Nostr by npub1qqqqqqyz0la
391406
- Saransh Sharma
392407
- swissrouting
393408
- André Neves
409+
- Semisol
394410

395411
## License
396412

docker-compose.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ services:
33
build: .
44
container_name: nostream
55
environment:
6-
SECRET: changeme
6+
SECRET: ${SECRET}
77
RELAY_PORT: 8008
88
# Master
99
NOSTR_CONFIG_DIR: /home/node/.nostr
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
exports.up = function (knex) {
2+
return knex.schema
3+
.raw('DROP FUNCTION confirm_invoice(invoice_id UUID, amount_received BIGINT, confirmation_date TIMESTAMP WITHOUT TIME ZONE)')
4+
.raw('DROP FUNCTION confirm_invoice(invoice_id TEXT, amount_received BIGINT, confirmation_date TIMESTAMP WITHOUT TIME ZONE)')
5+
.raw(`CREATE OR REPLACE FUNCTION confirm_invoice(invoice_id TEXT, amount_received BIGINT, confirmation_date TIMESTAMP WITHOUT TIME ZONE)
6+
RETURNS INTEGER
7+
LANGUAGE plpgsql
8+
AS $$
9+
DECLARE
10+
payee BYTEA;
11+
confirmed_date TIMESTAMP WITHOUT TIME ZONE;
12+
invoice_unit TEXT;
13+
BEGIN
14+
PERFORM ASSERT_SERIALIZED();
15+
16+
SELECT "pubkey", "confirmed_at", "unit" INTO payee, confirmed_date, invoice_unit FROM "invoices" WHERE id = invoice_id;
17+
IF confirmed_date IS NULL THEN
18+
UPDATE invoices
19+
SET
20+
"confirmed_at" = confirmation_date,
21+
"amount_paid" = amount_received,
22+
"updated_at" = now_utc()
23+
WHERE id = invoice_id;
24+
IF invoice_unit = 'sats' THEN
25+
UPDATE users SET balance = balance + amount_received * 1000 WHERE "pubkey" = payee;
26+
ELSIF invoice_unit = 'msats' THEN
27+
UPDATE users SET balance = balance + amount_received WHERE "pubkey" = payee;
28+
ELSIF invoice_unit = 'btc' THEN
29+
UPDATE users SET balance = balance + amount_received * 100000000 * 1000 WHERE "pubkey" = payee;
30+
END IF;
31+
END IF;
32+
RETURN 0;
33+
END;
34+
$$;`)
35+
}
36+
37+
exports.down = function (knex) {
38+
return knex.schema
39+
.raw(`CREATE OR REPLACE FUNCTION confirm_invoice(invoice_id TEXT, amount_received BIGINT, confirmation_date TIMESTAMP WITHOUT TIME ZONE)
40+
RETURNS INTEGER
41+
LANGUAGE plpgsql
42+
AS $$
43+
DECLARE
44+
payee BYTEA;
45+
confirmed_date TIMESTAMP WITHOUT TIME ZONE;
46+
BEGIN
47+
PERFORM ASSERT_SERIALIZED();
48+
49+
SELECT "pubkey", "confirmed_at" INTO payee, confirmed_date FROM "invoices" WHERE id = invoice_id;
50+
IF confirmed_date IS NULL THEN
51+
UPDATE invoices
52+
SET
53+
"confirmed_at" = confirmation_date,
54+
"amount_paid" = amount_received,
55+
"updated_at" = now_utc()
56+
WHERE id = invoice_id;
57+
UPDATE users SET balance = balance + amount_received WHERE "pubkey" = payee;
58+
END IF;
59+
RETURN 0;
60+
END;
61+
$$;`)
62+
}

package-lock.json

Lines changed: 69 additions & 27 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)