Skip to content

Commit

Permalink
Small updates to nearlib + RPC accepts cross-domain requests (#375)
Browse files Browse the repository at this point in the history
* [nearlib] switch signing library

* Allow cross-site for RPC requests

* Add getAccounts to list all present accounts in key store, add createDefaultConfig nodeUrl param

* Adding build script and exports file for nearlib to standalone script

* Fix up the rest of RPC calls; adding Account to exports

* Return package lock
  • Loading branch information
ilblackdragon authored Jan 10, 2019
1 parent 1b96a45 commit 4c5b32f
Show file tree
Hide file tree
Showing 6 changed files with 83 additions and 36 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,8 @@ core/wasm/**/Cargo.lock
# Python env
.env

# JS build
nearlib/dist/

# Integration tests
tmp/
4 changes: 4 additions & 0 deletions nearlib/build.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#!/bin/sh

npx browserify exports.js -o dist/nearlib.js

16 changes: 16 additions & 0 deletions nearlib/exports.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
const Near = require('./near');
const NearClient = require('./nearclient');
const Account = require('./account');
const BrowserLocalStorageKeystore = require('./signing/browser_local_storage_keystore');
const LocalNodeConnection = require('./local_node_connection');


var nearLib = window.nearLib || {};

nearLib.Near = Near;
nearLib.NearClient = NearClient;
nearLib.Account = Account;
nearLib.BrowserLocalStorageKeystore = BrowserLocalStorageKeystore;
nearLib.LocalNodeConnection = LocalNodeConnection;

window.nearLib = nearLib;
5 changes: 2 additions & 3 deletions nearlib/near.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,10 @@ class Near {
/**
* Default setup for browser
*/
static createDefaultConfig() {
//.keyStore, nearConnection
static createDefaultConfig(nodeUrl = "http://localhost:3030") {
return new Near(new NearClient(
new BrowserLocalStorageKeystore(),
new LocalNodeConnection("http://localhost:3030")
new LocalNodeConnection(nodeUrl)
));
};

Expand Down
8 changes: 8 additions & 0 deletions nearlib/signing/browser_local_storage_keystore.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,14 @@ class BrowserLocalStorageKeystore {
BrowserLocalStorageKeystore.storageKeyForSecretKey(accountId))
};
};

static getAccounts() {
return Object.keys(window.localStorage).map(function(key) {
if (key.endsWith("_public")) {
return key.substr(0, key.length() - 7);
}
});
}
};

module.exports = BrowserLocalStorageKeystore;
83 changes: 50 additions & 33 deletions node/http/src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,30 +6,47 @@ use std::sync::Arc;

use futures::future;
use hyper::{Body, Method, Request, Response, Server, StatusCode};
use hyper::http::response::Builder;
use hyper::rt::{Future, Stream};
use hyper::service::service_fn;

use api::HttpApi;

type BoxFut = Box<Future<Item=Response<Body>, Error=hyper::Error> + Send>;

fn build_response() -> Builder {
let mut builder = Response::builder();
builder
.header("Access-Control-Allow-Origin", "*")

This comment has been minimized.

Copy link
@azban

azban Jan 11, 2019

Contributor

i don't think this is what we want by default and should be a configuration option

.header("Access-Control-Allow-Headers", "Content-Type, Access-Control-Allow-Headers, Authorization, X-Requested-With");
builder
}

fn serve(http_api: Arc<HttpApi>, req: Request<Body>) -> BoxFut {
match (req.method(), req.uri().path()) {
(&Method::OPTIONS, _) => {
// Pre-flight response for cross site access.
Box::new(req.into_body().concat2().map(move |_| {
build_response()
.body(Body::empty())
.unwrap()
}))
}
(&Method::POST, "/create_account") => {
Box::new(req.into_body().concat2().map(move |chunk| {
match serde_json::from_slice(&chunk) {
Ok(data) => {
match http_api.create_account(&data) {
Ok(response) => {
Response::builder()
build_response()
.body(Body::from(serde_json::to_string(&response).unwrap()))
.unwrap()
}
Err(_) => unreachable!()
}
}
Err(e) => {
Response::builder()
build_response()
.status(StatusCode::BAD_REQUEST)
.body(Body::from(e.to_string()))
.unwrap()
Expand All @@ -43,15 +60,15 @@ fn serve(http_api: Arc<HttpApi>, req: Request<Body>) -> BoxFut {
Ok(data) => {
match http_api.stake(&data) {
Ok(response) => {
Response::builder()
build_response()
.body(Body::from(serde_json::to_string(&response).unwrap()))
.unwrap()
}
Err(_) => unreachable!()
}
}
Err(e) => {
Response::builder()
build_response()
.status(StatusCode::BAD_REQUEST)
.body(Body::from(e.to_string()))
.unwrap()
Expand All @@ -65,15 +82,15 @@ fn serve(http_api: Arc<HttpApi>, req: Request<Body>) -> BoxFut {
Ok(data) => {
match http_api.swap_key(&data) {
Ok(response) => {
Response::builder()
build_response()
.body(Body::from(serde_json::to_string(&response).unwrap()))
.unwrap()
}
Err(_) => unreachable!()
}
}
Err(e) => {
Response::builder()
build_response()
.status(StatusCode::BAD_REQUEST)
.body(Body::from(e.to_string()))
.unwrap()
Expand All @@ -88,15 +105,15 @@ fn serve(http_api: Arc<HttpApi>, req: Request<Body>) -> BoxFut {
Ok(data) => {
match http_api.deploy_contract(data) {
Ok(response) => {
Response::builder()
build_response()
.body(Body::from(serde_json::to_string(&response).unwrap()))
.unwrap()
}
Err(_) => unreachable!()
}
}
Err(e) => {
Response::builder()
build_response()
.status(StatusCode::BAD_REQUEST)
.body(Body::from(e.to_string()))
.unwrap()
Expand All @@ -110,15 +127,15 @@ fn serve(http_api: Arc<HttpApi>, req: Request<Body>) -> BoxFut {
Ok(data) => {
match http_api.schedule_function_call(data) {
Ok(response) => {
Response::builder()
build_response()
.body(Body::from(serde_json::to_string(&response).unwrap()))
.unwrap()
}
Err(_) => unreachable!()
}
}
Err(e) => {
Response::builder()
build_response()
.status(StatusCode::BAD_REQUEST)
.body(Body::from(e.to_string()))
.unwrap()
Expand All @@ -132,15 +149,15 @@ fn serve(http_api: Arc<HttpApi>, req: Request<Body>) -> BoxFut {
Ok(data) => {
match http_api.send_money(&data) {
Ok(response) => {
Response::builder()
build_response()
.body(Body::from(serde_json::to_string(&response).unwrap()))
.unwrap()
}
Err(_) => unreachable!()
}
}
Err(e) => {
Response::builder()
build_response()
.status(StatusCode::BAD_REQUEST)
.body(Body::from(e.to_string()))
.unwrap()
Expand All @@ -154,20 +171,20 @@ fn serve(http_api: Arc<HttpApi>, req: Request<Body>) -> BoxFut {
Ok(data) => {
match http_api.submit_transaction(data) {
Ok(response) => {
Response::builder()
build_response()
.body(Body::from(serde_json::to_string(&response).unwrap()))
.unwrap()
}
Err(e) => {
Response::builder()
build_response()
.status(StatusCode::SERVICE_UNAVAILABLE)
.body(Body::from(e.to_string()))
.unwrap()
}
}
}
Err(e) => {
Response::builder()
build_response()
.status(StatusCode::BAD_REQUEST)
.body(Body::from(e.to_string()))
.unwrap()
Expand All @@ -182,20 +199,20 @@ fn serve(http_api: Arc<HttpApi>, req: Request<Body>) -> BoxFut {
Ok(data) => {
match http_api.call_view_function(&data) {
Ok(response) => {
Response::builder()
build_response()
.body(Body::from(serde_json::to_string(&response).unwrap()))
.unwrap()
}
Err(e) => {
Response::builder()
build_response()
.status(StatusCode::BAD_REQUEST)
.body(Body::from(e.to_string()))
.unwrap()
}
}
}
Err(e) => {
Response::builder()
build_response()
.status(StatusCode::BAD_REQUEST)
.body(Body::from(e.to_string()))
.unwrap()
Expand All @@ -209,20 +226,20 @@ fn serve(http_api: Arc<HttpApi>, req: Request<Body>) -> BoxFut {
Ok(data) => {
match http_api.view_account(&data) {
Ok(response) => {
Response::builder()
build_response()
.body(Body::from(serde_json::to_string(&response).unwrap()))
.unwrap()
}
Err(e) => {
Response::builder()
build_response()
.status(StatusCode::BAD_REQUEST)
.body(Body::from(e.to_string()))
.unwrap()
}
}
}
Err(e) => {
Response::builder()
build_response()
.status(StatusCode::BAD_REQUEST)
.body(Body::from(e.to_string()))
.unwrap()
Expand All @@ -236,15 +253,15 @@ fn serve(http_api: Arc<HttpApi>, req: Request<Body>) -> BoxFut {
Ok(data) => {
match http_api.view_state(&data) {
Ok(response) => {
Response::builder()
build_response()
.body(Body::from(serde_json::to_string(&response).unwrap()))
.unwrap()
}
Err(_) => unreachable!()
}
}
Err(e) => {
Response::builder()
build_response()
.status(StatusCode::BAD_REQUEST)
.body(Body::from(e.to_string()))
.unwrap()
Expand All @@ -256,7 +273,7 @@ fn serve(http_api: Arc<HttpApi>, req: Request<Body>) -> BoxFut {
Box::new(future::ok(
match http_api.view_latest_beacon_block() {
Ok(response) => {
Response::builder()
build_response()
.body(Body::from(serde_json::to_string(&response).unwrap()))
.unwrap()
}
Expand All @@ -270,20 +287,20 @@ fn serve(http_api: Arc<HttpApi>, req: Request<Body>) -> BoxFut {
Ok(data) => {
match http_api.get_beacon_block_by_hash(&data) {
Ok(response) => {
Response::builder()
build_response()
.body(Body::from(serde_json::to_string(&response).unwrap()))
.unwrap()
}
Err(e) => {
Response::builder()
build_response()
.status(StatusCode::BAD_REQUEST)
.body(Body::from(e.to_string()))
.unwrap()
}
}
}
Err(e) => {
Response::builder()
build_response()
.status(StatusCode::BAD_REQUEST)
.body(Body::from(e.to_string()))
.unwrap()
Expand All @@ -295,7 +312,7 @@ fn serve(http_api: Arc<HttpApi>, req: Request<Body>) -> BoxFut {
Box::new(future::ok(
match http_api.view_latest_shard_block() {
Ok(response) => {
Response::builder()
build_response()
.body(Body::from(serde_json::to_string(&response).unwrap()))
.unwrap()
}
Expand All @@ -309,20 +326,20 @@ fn serve(http_api: Arc<HttpApi>, req: Request<Body>) -> BoxFut {
Ok(data) => {
match http_api.get_shard_block_by_hash(&data) {
Ok(response) => {
Response::builder()
build_response()
.body(Body::from(serde_json::to_string(&response).unwrap()))
.unwrap()
}
Err(e) => {
Response::builder()
build_response()
.status(StatusCode::BAD_REQUEST)
.body(Body::from(e.to_string()))
.unwrap()
}
}
}
Err(e) => {
Response::builder()
build_response()
.status(StatusCode::BAD_REQUEST)
.body(Body::from(e.to_string()))
.unwrap()
Expand Down Expand Up @@ -362,7 +379,7 @@ fn serve(http_api: Arc<HttpApi>, req: Request<Body>) -> BoxFut {
Box::new(future::ok(
match http_api.view_latest_beacon_block() {
Ok(_) => {
Response::builder()
build_response()
.body(Body::from(""))
.unwrap()
}
Expand All @@ -373,7 +390,7 @@ fn serve(http_api: Arc<HttpApi>, req: Request<Body>) -> BoxFut {

_ => {
Box::new(future::ok(
Response::builder()
build_response()
.status(StatusCode::NOT_FOUND)
.body(Body::empty())
.unwrap()
Expand Down

0 comments on commit 4c5b32f

Please sign in to comment.