Skip to content

Commit

Permalink
fix: confirm invoice function ambiguous unit variable (#221)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
cameri committed Feb 20, 2023
1 parent e99ac5d commit fd32949
Show file tree
Hide file tree
Showing 16 changed files with 225 additions and 94 deletions.
2 changes: 2 additions & 0 deletions CONFIGURATION.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ The following environment variables can be set:

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

# Settings

Expand Down
58 changes: 37 additions & 21 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ NIPs with a relay-specific implementation are listed here.
- [x] NIP-04: Encrypted Direct Message
- [x] NIP-09: Event deletion
- [x] NIP-11: Relay information document
- [x] NIP-11a: Relay Information Document Extensions
- [x] NIP-12: Generic tag queries
- [x] NIP-13: Proof of Work
- [x] NIP-15: End of Stored Events Notice
Expand All @@ -60,7 +61,6 @@ NIPs with a relay-specific implementation are listed here.
- [x] NIP-28: Public Chat
- [x] NIP-33: Parameterized Replaceable Events
- [x] NIP-40: Expiration Timestamp
- [x] NIP-111: Relay Information Document Extensions

## Requirements

Expand All @@ -86,24 +86,6 @@ Install Docker from their [official guide](https://docs.docker.com/engine/instal
- [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/))
- [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/))

## Local Quick Start (Docker Compose)

Install Docker Desktop following the [official guide](https://docs.docker.com/desktop/).
You may have to uninstall Docker on your machine if you installed it using a different guide.

Clone repository and enter directory:
```
git clone git@github.com:Cameri/nostream.git
cd nostream
```

Start:
```
./scripts/start_local
```

This will run in the foreground of the terminal until you stop it with Ctrl+C.

## Quick Start (Docker Compose)

Install Docker following the [official guide](https://docs.docker.com/engine/install/).
Expand Down Expand Up @@ -184,15 +166,17 @@ Set the following environment variables:
```
DB_URI="postgresql://postgres:postgres@localhost:5432/nostr_ts_relay_test"
DB_USER=postgres
```
or
```
DB_HOST=localhost
DB_PORT=5432
DB_NAME=nostr_ts_relay
DB_USER=postgres
DB_PASSWORD=postgres
```

```
REDIS_URI="redis://default:nostr_ts_relay@localhost:6379"
REDIS_HOST=localhost
Expand All @@ -201,6 +185,19 @@ Set the following environment variables:
REDIS_PASSWORD=nostr_ts_relay
```

If enabling payments, generate a long random secret and set SECRET:
You may want to use `openssl rand -hex 128` to generate a secret.

```
SECRET=aaabbbccc...dddeeefff
# Secret shortened for brevity
```

In addition, if using Zebedee for payments, you must also set ZEBEDEE_API_KEY with
an API Key from one of your projects in your Zebedee Developer Dashboard. Contact
@foxp2zeb on Telegram or npub1rvg76s0gz535txd9ypg2dfqv0x7a80ar6e096j3v343xdxyrt4ksmkxrck on Nostr requesting
access to the Zebedee Developer Dashboard.

Create `nostr_ts_relay` database:

```
Expand Down Expand Up @@ -261,6 +258,24 @@ To clean up the build, coverage and test reports run:
```
npm run clean
```
## Development Quick Start (Docker Compose)

Install Docker Desktop following the [official guide](https://docs.docker.com/desktop/).
You may have to uninstall Docker on your machine if you installed it using a different guide.

Clone repository and enter directory:
```
git clone git@github.com:Cameri/nostream.git
cd nostream
```

Start:
```
./scripts/start_local
```

This will run in the foreground of the terminal until you stop it with Ctrl+C.

## Tests

### Unit tests
Expand Down Expand Up @@ -391,6 +406,7 @@ I'm Cameri on most social networks. You can find me on Nostr by npub1qqqqqqyz0la
- Saransh Sharma
- swissrouting
- André Neves
- Semisol

## License

Expand Down
2 changes: 1 addition & 1 deletion docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ services:
build: .
container_name: nostream
environment:
SECRET: changeme
SECRET: ${SECRET}
RELAY_PORT: 8008
# Master
NOSTR_CONFIG_DIR: /home/node/.nostr
Expand Down
62 changes: 62 additions & 0 deletions migrations/20230220_002700_fix_unit_confirm_invoice_func.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
exports.up = function (knex) {
return knex.schema
.raw('DROP FUNCTION confirm_invoice(invoice_id UUID, amount_received BIGINT, confirmation_date TIMESTAMP WITHOUT TIME ZONE)')
.raw('DROP FUNCTION confirm_invoice(invoice_id TEXT, amount_received BIGINT, confirmation_date TIMESTAMP WITHOUT TIME ZONE)')
.raw(`CREATE OR REPLACE FUNCTION confirm_invoice(invoice_id TEXT, amount_received BIGINT, confirmation_date TIMESTAMP WITHOUT TIME ZONE)
RETURNS INTEGER
LANGUAGE plpgsql
AS $$
DECLARE
payee BYTEA;
confirmed_date TIMESTAMP WITHOUT TIME ZONE;
invoice_unit TEXT;
BEGIN
PERFORM ASSERT_SERIALIZED();
SELECT "pubkey", "confirmed_at", "unit" INTO payee, confirmed_date, invoice_unit FROM "invoices" WHERE id = invoice_id;
IF confirmed_date IS NULL THEN
UPDATE invoices
SET
"confirmed_at" = confirmation_date,
"amount_paid" = amount_received,
"updated_at" = now_utc()
WHERE id = invoice_id;
IF invoice_unit = 'sats' THEN
UPDATE users SET balance = balance + amount_received * 1000 WHERE "pubkey" = payee;
ELSIF invoice_unit = 'msats' THEN
UPDATE users SET balance = balance + amount_received WHERE "pubkey" = payee;
ELSIF invoice_unit = 'btc' THEN
UPDATE users SET balance = balance + amount_received * 100000000 * 1000 WHERE "pubkey" = payee;
END IF;
END IF;
RETURN 0;
END;
$$;`)
}

exports.down = function (knex) {
return knex.schema
.raw(`CREATE OR REPLACE FUNCTION confirm_invoice(invoice_id TEXT, amount_received BIGINT, confirmation_date TIMESTAMP WITHOUT TIME ZONE)
RETURNS INTEGER
LANGUAGE plpgsql
AS $$
DECLARE
payee BYTEA;
confirmed_date TIMESTAMP WITHOUT TIME ZONE;
BEGIN
PERFORM ASSERT_SERIALIZED();
SELECT "pubkey", "confirmed_at" INTO payee, confirmed_date FROM "invoices" WHERE id = invoice_id;
IF confirmed_date IS NULL THEN
UPDATE invoices
SET
"confirmed_at" = confirmation_date,
"amount_paid" = amount_received,
"updated_at" = now_utc()
WHERE id = invoice_id;
UPDATE users SET balance = balance + amount_received WHERE "pubkey" = payee;
END IF;
RETURN 0;
END;
$$;`)
}
96 changes: 69 additions & 27 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit fd32949

Please sign in to comment.