Skip to content
This repository has been archived by the owner on Jan 19, 2024. It is now read-only.

Commit

Permalink
Merge pull request #10 from zoedberg/0.2_protocol
Browse files Browse the repository at this point in the history
update proxy to protocol v0.2
  • Loading branch information
grunch authored Aug 30, 2023
2 parents 9b258e8 + 71f8ba6 commit de2616e
Show file tree
Hide file tree
Showing 8 changed files with 350 additions and 439 deletions.
69 changes: 35 additions & 34 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,29 +2,28 @@

![workflow](https://user-images.githubusercontent.com/31323835/172648333-efd666c0-d8c3-48d8-b290-117c590c684c.png)

RGB proxy server is intended to facilitate the relay of consignment data
RGB proxy server is intended to facilitate the relay of client-side data
between RGB wallets, enabling a better user experience for wallet users.

The API it implements adheres to the
[RGB HTTP JSON-RPC protocol](https://github.com/RGB-Tools/rgb-http-json-rpc).

The proxy server is designed to handle the following workflow:

- The payer of an RGB transfer posts to the proxy server the transfer
consignment file using as identifier of the file the blinded UTXO provided by
the payee in the invoice.
- The payee asks the proxy server for the consignment file associated to the
blinded UTXO previously provided in the invoice.
- If there is a file associated to such blinded UTXO, the server returns the
file to the payee.
- The payee validates the content of the consignment file.
- The payee posts to the server and ACK message if she is satisfied with the
content of the proposed consignment file, otherwise she can post a NACK
message to inform the payer that the RGB transfer should be considered as
failed.
- The payer of an RGB transfer posts the transfer
consignment file to the server, typically using the blinded UTXO (provided
by the payee in the invoice) as identifier for the file.
- The payee asks the server for the consignment file associated with the
identifier (e.g. the blinded UTXO).
- If there is a file associated to the provided identifier, the server returns
the file to the payee.
- The payee validates the retrieved consignment file.
- If the consignment is valid, the payee posts an ACK to the server, otherwise
a NACK is posted to inform the payer that the RGB transfer should be
considered as failed.
- The payer asks the server for the ACK/NACK status associated with the
consignment file previously posted. If the consignment as been ACKed by the
payee, the payer will proceed in broadcasting the Bitcoin transaction
previously posted consignment file. If the consignment has been ACKed by the
payee, the payer will proceed with broadcasting the Bitcoin transaction
containing the commitment to the RGB consignment.

The RGB proxy server does not need to be trusted by the users as it is only
Expand All @@ -49,30 +48,32 @@ npm run build
npm run start
```

## How to use it
## Example usage

The payee generates a blinded UTXO and sends it to the payer (not covered
here). Let's assume the blinded UTXO is `blindTest`.
The payee generates an RGB invoice and sends it to the payer (not covered
here). Let's assume the invoice contains the blinded UTXO `blindTest`.

The payer sends the consignment file for the blinded UTXO to the proxy server:
The payer prepares the transfer, then sends the consignment file and the
related txid to the proxy server, using the blinded UTXO from the invoice as
identifier:
```
# let's create a fake consignment file and send it
$ echo "consignment binary data" > consignment.rgb
$ curl -X POST -H 'Content-Type: multipart/form-data' \
-F 'jsonrpc=2.0' -F 'id="3"' -F 'method=consignment.post' \
-F 'params[blinded_utxo]=blindTest' -F '[email protected]' \
-F 'jsonrpc=2.0' -F 'id="1"' -F 'method=consignment.post' \
-F 'params[recipient_id]=blindTest' -F 'params[txid]=527f2b2ebb81c873f128848d7226ecdb7cb4a4025222c54bfec7c358d51b9207' -F '[email protected]' \
localhost:3000/json-rpc
{"jsonrpc":"2.0","id":"3","result":true}
{"jsonrpc":"2.0","id":"1","result":true}
```

The payee requests the consignment for the blinded UTXO:
```
$ curl -X POST -H 'Content-Type: application/json' \
-d '{"jsonrpc": "2.0", "id": "7", "method": "consignment.get", "params": {"blinded_utxo": "blindTest"} }' \
-d '{"jsonrpc": "2.0", "id": "2", "method": "consignment.get", "params": {"recipient_id": "blindTest"} }' \
localhost:3000/json-rpc
{"jsonrpc":"2.0","id":"7","result":"Y29uc2lnbm1lbnQgYmluYXJ5IGRhdGEK"}
{"jsonrpc":"2.0","id":"2","result": {"consignment": "Y29uc2lnbm1lbnQgYmluYXJ5IGRhdGEK", "txid": "527f2b2ebb81c873f128848d7226ecdb7cb4a4025222c54bfec7c358d51b9207"}}
```
The file is returned as a base64-encoded string:
Expand All @@ -81,38 +82,38 @@ $ echo 'Y29uc2lnbm1lbnQgYmluYXJ5IGRhdGEK' | base64 -d
consignment binary data
```

If ok with the consignment (validation passes), the payee calls:
If the consignment is valid, the payee ACKs it:
```
$ curl -X POST -H 'Content-Type: application/json' \
-d '{"jsonrpc": "2.0", "id": "9", "method": "ack.post", "params": {"blinded_utxo": "blindTest", "ack": true} }' \
-d '{"jsonrpc": "2.0", "id": "3", "method": "ack.post", "params": {"recipient_id": "blindTest", "ack": true} }' \
localhost:3000/json-rpc
{"jsonrpc":"2.0","id":"9","result":true}
{"jsonrpc":"2.0","id":"3","result":true}
```

If not ok with the consignment, the payee calls instead:
If the consignment is invalid, the payee NACKs it:
```
$ curl -X POST -H 'Content-Type: application/json' \
-d '{"jsonrpc": "2.0", "id": "8", "method": "ack.post", "params": {"blinded_utxo": "blindTest", "ack": false} }' \
-d '{"jsonrpc": "2.0", "id": "4", "method": "ack.post", "params": {"recipient_id": "blindTest", "ack": false} }' \
localhost:3000/json-rpc
{"jsonrpc":"2.0","id":"8","result":true}
{"jsonrpc":"2.0","id":"4","result":true}
```

The payer recieves the `ack` value (`null` if payee has not called `ack.post`
The payer requests the `ack` value (`null` if payee has not called `ack.post`
yet):
```
$ curl -X POST -H 'Content-Type: application/json' \
-d '{"jsonrpc": "2.0", "id": "1", "method": "ack.get", "params": {"blinded_utxo": "blindTest"} }' \
-d '{"jsonrpc": "2.0", "id": "5", "method": "ack.get", "params": {"recipient_id": "blindTest"} }' \
localhost:3000/json-rpc
{"jsonrpc":"2.0","id":"1","result":true}
{"jsonrpc":"2.0","id":"5","result":true}
```

In case of approval the transaction can be broadcast, otherwise the two parties
need to abort the transfer process and start from scratch.

The consignment or media file for any given blinded UTXO and the related
The consignment or media file for any given recipient ID and the related
approval cannot be changed once submitted.


Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "rgb-proxy-server",
"version": "0.1.0",
"version": "0.2.0",
"description": "",
"main": "index.js",
"scripts": {
Expand Down
22 changes: 1 addition & 21 deletions src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,13 @@ import cors from "cors";
import express from "express";
import httpContext from "express-http-context";
import morgan from "morgan";
import { homedir } from "os";
import path from "path";

import { logger, oldAPILogger } from "./logger";
import { setDir } from "./util";
import { APP_DIR } from "./vars";
import { logger } from "./logger";

let reqId = 0;
// Create Express server
const app = express();

// Create app directory if it doesn't exist
setDir(path.join(homedir(), APP_DIR));

// Allow request from any origin
app.use(cors());

Expand Down Expand Up @@ -53,19 +46,6 @@ app.use(
})
);

app.use(
morgan(":status", {
skip: (req, _res) => {
return req.originalUrl.startsWith("/json-rpc");
},
stream: {
write: (message: string) => {
oldAPILogger.notice(message.trim(), { isFromMorgan: true });
},
},
})
);

// Express configuration
app.set("port", process.env.PORT || 3000);
app.use(express.json());
Expand Down
Loading

0 comments on commit de2616e

Please sign in to comment.