Skip to content

Commit

Permalink
JSON gateway for mobilecoind (#195)
Browse files Browse the repository at this point in the history
* JSON gateway for mobilecoind

* fmt and clippy

* First round of fixes based on comments

* Move verbs to the end

* Allow JSON from transfer call to be passed directly to status

* Change call name

* Updates README to match new endpoints

* Return and parse full receipt

* Separates reading request code from transfer, other cleanups

* Add sender_tx_receipt as part of transfer result
  • Loading branch information
tsegaran authored Jun 4, 2020
1 parent ade05bc commit 6f0b4b4
Show file tree
Hide file tree
Showing 6 changed files with 564 additions and 0 deletions.
18 changes: 18 additions & 0 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ members = [
"ledger/from-archive",
"ledger/sync",
"mobilecoind",
"mobilecoind-json",
"mobilecoind/api",
"peers",
"peers/test-utils",
Expand Down
8 changes: 8 additions & 0 deletions api/src/conversions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -740,6 +740,14 @@ pub fn block_num_to_s3block_path(block_index: mc_transaction_core::BlockIndex) -
path
}

impl From<Vec<u8>> for external::KeyImage {
fn from(src: Vec<u8>) -> Self {
let mut key_image = external::KeyImage::new();
key_image.set_data(src);
key_image
}
}

#[cfg(test)]
mod conversion_tests {
use super::*;
Expand Down
19 changes: 19 additions & 0 deletions mobilecoind-json/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
[package]
name = "mobilecoind-json"
version = "0.1.0"
authors = ["MobileCoin"]
edition = "2018"

[dependencies]
grpcio = "0.5.1"
rocket = { version = "0.4.4", default-features = false }
rocket_contrib = { version = "0.4.4", default-features = false, features = ["json"] }
hex = "0.4"
mc-api = { path = "../api" }
mc-common = { path = "../common", features = ["log"] }
mc-util-grpc = { path = "../util/grpc" }
mc-mobilecoind-api = { path = "../mobilecoind/api" }
protobuf = "2.12"
serde = "1.0"
serde_derive = "1.0"
structopt = "0.3"
74 changes: 74 additions & 0 deletions mobilecoind-json/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
## mobilecoind-json

This is a standalone executable which provides a simple HTTP JSON API wrapping the [mobilecoind](../mobilecoind) gRPC API.

It should be run alongside `mobilecoind`.

### Launching
Since it is just web server converting JSON requests to gRPC, and it's set up
with the mobilecoind defaults, it can simply be launched with:
```
cargo run
```

Options are:

- `--listen_host` - hostname for webserver, default `127.0.0.1`
- `--listen_port` - port for webserver, default `9090`
- `--mobilecoind_host` - hostname:port for mobilecoind gRPC, default `127.0.0.1:4444`
- `--use_ssl` - connect to mobilecoind using SSL, default is false

### Usage with cURL

#### Generate a new master key
```
$ curl localhost:9090/entropy
{"entropy":"706db549844bc7b5c8328368d4b8276e9aa03a26ac02474d54aa99b7c3369e2e"}
```
#### Add a monitor for a key over a range of subaddress indices
```
$ curl localhost:9090/monitors -d '{"entropy": "706db549844bc7b5c8328368d4b8276e9aa03a26ac02474d54aa99b7c3369e2e", "first_subaddress": 0, "num_subaddresses": 10}' -X POST -H 'Content-Type: application/json'
{"monitor_id":"fca4ffa1a1b1faf8ad775d0cf020426ba7f161720403a76126bc8e40550d9872"}
```

#### Get the status of an existing monitor
```
$ curl localhost:9090/monitors/fca4ffa1a1b1faf8ad775d0cf020426ba7f161720403a76126bc8e40550d9872
{"first_subaddress":0,"num_subaddresses":10,"first_block":0,"next_block":2068}
```

#### Check the balance for a monitor and subaddress index
```
$ curl localhost:9090/monitors/fca4ffa1a1b1faf8ad775d0cf020426ba7f161720403a76126bc8e40550d9872/0/balance
{"balance":199999999999990}
```
#### Generate a request code for a monitor and subaddress
```
$ curl localhost:9090/monitors/fca4ffa1a1b1faf8ad775d0cf020426ba7f161720403a76126bc8e40550d9872/0/request-code -X POST -d '{"value": 10, "memo": "Please pay me"}' -H 'Content-Type: application/json'
{"request_code":"HUGpTreNKe4ziGAwDNYeW1iayWJgZ4DgiYRk9fw8E7f21PXQRUt4kbFsWBxzcJj12K6atUMuAyRNnwCybw5oJcm6xYXazdZzx4Tc5QuKdFdH2XSuUYM8pgQ1jq2ZBBi"}
```

```
$ curl localhost:9090/monitors/fca4ffa1a1b1faf8ad775d0cf020426ba7f161720403a76126bc8e40550d9872/1/request-code -X POST -d '{}' -H 'Content-Type: application/json'
{"request_code":"2dmFbXtoY78h6K5xsK1NyTHmVGk6oiqBaEYGvJeSLFsCxkL4Ed1vjxEjtwg65QWR8nBdyXnwjyFo6rHEiHmFcsFysjapemAgxWyTda9FVsSFEF"}
```

#### Read all the information in a request code
```
$ curl localhost:9090/read-request/HUGpTreNKe4ziGAwDNYeW1iayWJgZ4DgiYRk9fw8E7f21PXQRUt4kbFsWBxzcJj12K6atUMuAyRNnwCybw5oJcm6xYXazdZzx4Tc5QuKdFdH2XSuUYM8pgQ1jq2ZBBi
{"receiver":{"view_public_key":"40f884563ff10fb1b37b589036db9abbf1ab7afcf88f17a4ea6ec0077e883263","spend_public_key":"ecf9f2fdb8714afd16446d530cf27f2775d9e356e17a6bba8ad395d16d1bbd45","fog_fqdn":""},"value":"10","memo":"Please pay me"}
```
This JSON can be passed directly to `transfer` or you can change the amount if desired.

#### Transfer money from a monitor/subaddress to a request code
Using the information in the `read-request`, make a transfer to another address.
```
$ curl localhost:9090//monitors/fca4ffa1a1b1faf8ad775d0cf020426ba7f161720403a76126bc8e40550d9872/0/transfer -d '{"receiver":{"view_public_key":"40f884563ff10fb1b37b589036db9abbf1ab7afcf88f17a4ea6ec0077e883263","spend_public_key":"ecf9f2fdb8714afd16446d530cf27f2775d9e356e17a6bba8ad395d16d1bbd45","fog_fqdn":""},"value":"10","memo":"Please pay me"}' -X POST -H 'Content-Type: application/json'
{"sender_tx_receipt":{"key_images":["dc8a91dbacad97b59e9709379c279a28b3c35262f6744226d15ee87be6bbf132","7e22679d8e3c14ba9c6c45256902e7af8e82644618e65a4589bab268bfde4b61"],"tombstone":2121}}
```
#### Check the status of a transfer with a key image and tombstone block
The return value from `transfer` can be passed directly directly to `get-transfer-status`
```
$ curl localhost:9090/check-transfer-status -d '{"sender_tx_receipt":{"key_images":["dc8a91dbacad97b59e9709379c279a28b3c35262f6744226d15ee87be6bbf132","7e22679d8e3c14ba9c6c45256902e7af8e82644618e65a4589bab268bfde4b61"],"tombstone":2121}}' -X POST -H 'Content-Type: application/json'
{"status":"verified"}
```
Loading

0 comments on commit 6f0b4b4

Please sign in to comment.