organised project structure
This commit is contained in:
parent
c44ec9696c
commit
f18efe7110
6
.gitignore
vendored
6
.gitignore
vendored
@ -1,6 +1,4 @@
|
||||
examples.mk
|
||||
json-rpc/
|
||||
supernode/websocket_chat
|
||||
supernode/floaddress.org.html
|
||||
supernode/Makefile
|
||||
playground
|
||||
supernode/playground/
|
||||
playground
|
||||
|
||||
3
supernode/Makefile
Normal file
3
supernode/Makefile
Normal file
@ -0,0 +1,3 @@
|
||||
PROG = websocket_chat
|
||||
MODULE_CFLAGS = -DMG_ENABLE_FILESYSTEM=1
|
||||
include ../examples.mk
|
||||
@ -1,196 +0,0 @@
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<title>WebCrypto API Demo: ECDSA Generate Keys Sign Verify Message </title>
|
||||
|
||||
<script type="text/javascript">
|
||||
var privateKey;
|
||||
var publicKey;
|
||||
|
||||
|
||||
|
||||
var iv;
|
||||
|
||||
|
||||
function asciiToUint8Array(str) {
|
||||
var chars = [];
|
||||
for (var i = 0; i < str.length; ++i)
|
||||
chars.push(str.charCodeAt(i));
|
||||
return new Uint8Array(chars);
|
||||
}
|
||||
|
||||
function ECDSA_Sign() {
|
||||
|
||||
var cryptoObj = window.crypto || window.msCrypto;
|
||||
|
||||
if(!cryptoObj)
|
||||
{
|
||||
alert("Crypto API is not supported by the Browser");
|
||||
return;
|
||||
}
|
||||
|
||||
var plainText = document.getElementById("plainText").value;
|
||||
var curve = document.getElementById("curve").value;
|
||||
|
||||
window.crypto.subtle.generateKey({
|
||||
name: "ECDSA",
|
||||
namedCurve: curve, //can be "P-256", "P-384", or "P-521"
|
||||
},
|
||||
true, //whether the key is extractable (i.e. can be used in exportKey)
|
||||
["sign", "verify"] //can be any combination of "sign" and "verify"
|
||||
)
|
||||
.then(function(key) {
|
||||
|
||||
publicKey = key.publicKey;
|
||||
privateKey = key.privateKey;
|
||||
// For Demo Purpos Only Exported in JWK format
|
||||
window.crypto.subtle.exportKey("jwk", key.publicKey).then(
|
||||
function(keydata) {
|
||||
publicKeyhold = keydata;
|
||||
publicKeyJson = JSON.stringify(publicKeyhold);
|
||||
document.getElementById("ecdsapublic").value = publicKeyJson;
|
||||
}
|
||||
);
|
||||
|
||||
window.crypto.subtle.exportKey("jwk", key.privateKey).then(
|
||||
function(keydata) {
|
||||
privateKeyhold = keydata;
|
||||
privateKeyJson = JSON.stringify(privateKeyhold);
|
||||
document.getElementById("ecdsaprivate").value = privateKeyJson;
|
||||
}
|
||||
);
|
||||
|
||||
window.crypto.subtle.sign({
|
||||
name: "ECDSA",
|
||||
hash: {
|
||||
name: "SHA-256"
|
||||
}, //can be "SHA-1", "SHA-256", "SHA-384", or "SHA-512"
|
||||
},
|
||||
privateKey, //from generateKey or importKey above
|
||||
asciiToUint8Array(plainText) //ArrayBuffer of data you want to sign
|
||||
)
|
||||
.then(function(signature) {
|
||||
//returns an ArrayBuffer containing the signature
|
||||
document.getElementById("cipherText").value = bytesToHexString(signature);
|
||||
})
|
||||
.catch(function(err) {
|
||||
console.error(err);
|
||||
});
|
||||
|
||||
|
||||
})
|
||||
.catch(function(err) {
|
||||
console.error(err);
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
|
||||
function ECDSA_Verify() {
|
||||
|
||||
var cryptoObj = window.crypto || window.msCrypto;
|
||||
|
||||
if(!cryptoObj)
|
||||
{
|
||||
alert("Crypto API is not supported by the Browser");
|
||||
return;
|
||||
}
|
||||
|
||||
var cipherText = document.getElementById("cipherText").value;
|
||||
var plainText = document.getElementById("plainText").value;
|
||||
|
||||
if(!publicKey)
|
||||
{
|
||||
alert("Generate ECDSA Keys First")
|
||||
return;
|
||||
}
|
||||
|
||||
window.crypto.subtle.verify({
|
||||
name: "ECDSA",
|
||||
hash: {name: "SHA-256"}, //can be "SHA-1", "SHA-256", "SHA-384", or "SHA-512"
|
||||
},
|
||||
publicKey, //from generateKey or importKey above
|
||||
hexStringToUint8Array(cipherText), //ArrayBuffer of the data
|
||||
asciiToUint8Array(plainText)
|
||||
)
|
||||
.then(function(decrypted) {
|
||||
alert("Verified " + decrypted);
|
||||
})
|
||||
.catch(function(err) {
|
||||
console.error(err);
|
||||
});
|
||||
|
||||
}
|
||||
|
||||
function bytesToASCIIString(bytes) {
|
||||
return String.fromCharCode.apply(null, new Uint8Array(bytes));
|
||||
}
|
||||
|
||||
function bytesToHexString(bytes) {
|
||||
if (!bytes)
|
||||
return null;
|
||||
|
||||
bytes = new Uint8Array(bytes);
|
||||
var hexBytes = [];
|
||||
|
||||
for (var i = 0; i < bytes.length; ++i) {
|
||||
var byteString = bytes[i].toString(16);
|
||||
if (byteString.length < 2)
|
||||
byteString = "0" + byteString;
|
||||
hexBytes.push(byteString);
|
||||
}
|
||||
|
||||
return hexBytes.join("");
|
||||
}
|
||||
|
||||
function hexStringToUint8Array(hexString) {
|
||||
if (hexString.length % 2 != 0)
|
||||
throw "Invalid hexString";
|
||||
var arrayBuffer = new Uint8Array(hexString.length / 2);
|
||||
|
||||
for (var i = 0; i < hexString.length; i += 2) {
|
||||
var byteValue = parseInt(hexString.substr(i, 2), 16);
|
||||
if (byteValue == NaN)
|
||||
throw "Invalid hexString";
|
||||
arrayBuffer[i / 2] = byteValue;
|
||||
}
|
||||
|
||||
return arrayBuffer;
|
||||
}
|
||||
|
||||
|
||||
function failAndLog(error) {
|
||||
console.log(error);
|
||||
}
|
||||
</script>
|
||||
</head>
|
||||
<body>
|
||||
<h1>Generate ECDSA Public Private Keys and perform Sign and Verify Message </h1>
|
||||
|
||||
<div>
|
||||
|
||||
Named Curve <select name="curve" id="curve">
|
||||
<option value="P-256">P-256</option>
|
||||
<option value="P-384">P-384</option>
|
||||
<option value="P-521">P-521</option>
|
||||
</select>
|
||||
|
||||
Input Text to Signed<input type="text" name="plainText" value="Hello 8gwifi" id="plainText">
|
||||
|
||||
<button type="button" onclick="ECDSA_Sign()">Generate ECDSA keys and Perform Sign </button>
|
||||
|
||||
<button type="button" onclick="ECDSA_Verify()">Verify ECDSA Signature </button>
|
||||
</div>
|
||||
|
||||
<div>
|
||||
Signature Output (Hex) <input type="text" size="30" name="cipherText" id="cipherText" readonly="true"><br>
|
||||
ECDSA Public Key (JWK) : <textarea name="ecdsapublic" id="ecdsapublic" cols="30" rows="10"> </textarea><br>
|
||||
ECDSA Private Key (JWK): <textarea name="ecdsaprivate" id="ecdsaprivate" cols="30" rows="10"></textarea>
|
||||
</div>
|
||||
|
||||
|
||||
<br> Thanks for downloading the code, if you like it Support <a href="8gwifi.org">8gwifi.org </a> By purchasing The Modern Cryptography Cookbook
|
||||
<iframe width='160' height='400' src='https://leanpub.com/crypto/embed' frameborder='0' allowtransparency='true'></iframe>
|
||||
|
||||
</body>
|
||||
</html>
|
||||
@ -1,85 +0,0 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="utf-8" />
|
||||
<title>WebSocket Test</title>
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1" />
|
||||
<style type="text/css">
|
||||
body {
|
||||
background-color: #789; margin: 0;
|
||||
padding: 0; font: 14px Helvetica, Arial, sans-serif;
|
||||
}
|
||||
div.content {
|
||||
width: 800px; margin: 2em auto; padding: 20px 50px;
|
||||
background-color: #fff; border-radius: 1em;
|
||||
}
|
||||
#messages {
|
||||
border: 2px solid #fec; border-radius: 1em;
|
||||
height: 10em; overflow: scroll; padding: 0.5em 1em;
|
||||
}
|
||||
a:link, a:visited { color: #69c; text-decoration: none; }
|
||||
@media (max-width: 700px) {
|
||||
body { background-color: #fff; }
|
||||
div.content {
|
||||
width: auto; margin: 0 auto; border-radius: 0;
|
||||
padding: 1em;
|
||||
}
|
||||
}
|
||||
</style>
|
||||
|
||||
<script language="javascript" type="text/javascript">
|
||||
|
||||
var rooms = [];
|
||||
var ws = new WebSocket('ws://' + location.host);
|
||||
|
||||
if (!window.console) { window.console = { log: function() {} } };
|
||||
|
||||
ws.onopen = function(ev) { console.log(ev); };
|
||||
ws.onerror = function(ev) { console.log(ev); };
|
||||
ws.onclose = function(ev) { console.log(ev); };
|
||||
ws.onmessage = function(ev) {
|
||||
console.log(ev);
|
||||
var div = document.createElement('div');
|
||||
div.innerHTML = ev.data;
|
||||
document.getElementById('messages').appendChild(div);
|
||||
|
||||
};
|
||||
|
||||
window.onload = function() {
|
||||
document.getElementById('send_button').onclick = function(ev) {
|
||||
var msg = document.getElementById('send_input').value;
|
||||
document.getElementById('send_input').value = '';
|
||||
ws.send(msg);
|
||||
};
|
||||
document.getElementById('send_input').onkeypress = function(ev) {
|
||||
if (ev.keyCode == 13 || ev.which == 13) { //13 is enter key
|
||||
document.getElementById('send_button').click();
|
||||
}
|
||||
};
|
||||
};
|
||||
</script>
|
||||
</head>
|
||||
<body>
|
||||
<div class="content">
|
||||
<h1>Websocket PubSub Demonstration</h1>
|
||||
|
||||
<p>
|
||||
This page demonstrates how Mongoose could be used to implement
|
||||
<a href="http://en.wikipedia.org/wiki/Publish%E2%80%93subscribe_pattern">
|
||||
publish–subscribe pattern</a>. Open this page in several browser
|
||||
windows. Each window initiates persistent
|
||||
<a href="http://en.wikipedia.org/wiki/WebSocket">WebSocket</a>
|
||||
connection with the server, making each browser window a websocket client.
|
||||
Send messages, and see messages sent by other clients.
|
||||
</p>
|
||||
|
||||
<div id="messages">
|
||||
</div>
|
||||
|
||||
<p>
|
||||
<input type="text" id="send_input" />
|
||||
<button id="send_button">Send Message</button>
|
||||
</p>
|
||||
</div>
|
||||
</body>
|
||||
</html>
|
||||
@ -1,189 +0,0 @@
|
||||
/*
|
||||
The MIT License (MIT)
|
||||
|
||||
Copyright (c) 2014 Oliver Moran
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy of
|
||||
this software and associated documentation files (the "Software"), to deal in
|
||||
the Software without restriction, including without limitation the rights to
|
||||
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
|
||||
of the Software, and to permit persons to whom the Software is furnished to do
|
||||
so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
||||
*/
|
||||
|
||||
var JSON_RPC = {};
|
||||
|
||||
|
||||
|
||||
var id = 0, callbacks = {};
|
||||
|
||||
/**
|
||||
* Constructs a new JSON-RPC Request
|
||||
* @param method A String containing the name of the method to be invoked.
|
||||
* @param params (optional) A Structured value that holds the parameter values to be used during the invocation of the method.
|
||||
*/
|
||||
JSON_RPC.Request = function (method, params) {
|
||||
this.jsonrpc = "2.0";
|
||||
this.method = method;
|
||||
if (typeof params !== "undefined") {
|
||||
this.params = params;
|
||||
}
|
||||
|
||||
this.id = id++;
|
||||
};
|
||||
|
||||
// Implements getters and setters for the result of a JSON-RPC Request.
|
||||
// The result may be an any Object or primitive
|
||||
Object.defineProperty(JSON_RPC.Request.prototype, "result", {
|
||||
get: function () { return this._result; },
|
||||
set: function (result) {
|
||||
delete this.method; // remove the method name
|
||||
delete this.params; // remove the params
|
||||
delete this.error; // remove error state if it exists
|
||||
this._result = result;
|
||||
}
|
||||
});
|
||||
|
||||
// Implements getters and setters for the error state of a JSON-RPC Request.
|
||||
// Error should be a JSON_RPC.Error object
|
||||
Object.defineProperty(JSON_RPC.Request.prototype, "error", {
|
||||
get: function () { return this._error; },
|
||||
set: function (error) {
|
||||
delete this.method; // remove the method name
|
||||
delete this.params; // remove the params
|
||||
delete this.result; // remove result state if it exists
|
||||
this._error = error;
|
||||
}
|
||||
});
|
||||
|
||||
/**
|
||||
* Returns a String representation of a JSON-RPC Request
|
||||
* @returns A JSON String
|
||||
*/
|
||||
JSON_RPC.Request.prototype.toString = function () {
|
||||
var rpc = {
|
||||
jsonrpc: this.jsonrpc,
|
||||
id: this.id
|
||||
};
|
||||
|
||||
if (this.method !== undefined) rpc.method = this.method;
|
||||
if (this.params !== undefined) rpc.params = this.params;
|
||||
if (this.result !== undefined) rpc.result = this.result;
|
||||
if (this.error !== undefined) rpc.error = this.error;
|
||||
|
||||
return JSON.stringify(rpc);
|
||||
};
|
||||
|
||||
/**
|
||||
* Constructs a new JSON-RPC Notification
|
||||
* @param method A String containing the name of the method to be invoked.
|
||||
* @param params (optional) A Structured value that holds the parameter values to be used during the invocation of the method.
|
||||
*/
|
||||
JSON_RPC.Notification = function (method, params) {
|
||||
this.jsonrpc = "2.0";
|
||||
this.method = method;
|
||||
if (typeof params !== "undefined") {
|
||||
this.params = params;
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* Returns a String representation of a JSON-RPC Notification
|
||||
* @returns A JSON String
|
||||
*/
|
||||
JSON_RPC.Notification.prototype.toString = function () {
|
||||
var rpc = {
|
||||
jsonrpc: this.jsonrpc,
|
||||
method: this.method,
|
||||
params: this.params
|
||||
};
|
||||
|
||||
return JSON.stringify(rpc);
|
||||
};
|
||||
|
||||
/**
|
||||
* Constructs a new JSON-RPC Errror object
|
||||
* @params code A Number that indicates the error type that occurred. -32768 to -32000 are reserved.
|
||||
* @param message (optional) A String providing a short description of the error.
|
||||
* @param data (optional) A Primitive or Structured value that contains additional information about the error.
|
||||
*/
|
||||
JSON_RPC.Error = function (code, message, data) {
|
||||
this.code = code;
|
||||
if (typeof message == "string") this.message = message;
|
||||
if (data !== undefined) this.data = data;
|
||||
};
|
||||
|
||||
// stock errors
|
||||
JSON_RPC.PARSE_ERROR = new JSON_RPC.Error(-32700, "An error occurred on the server while parsing the JSON text.");
|
||||
JSON_RPC.INVALID_REQUEST = new JSON_RPC.Error(-32600, "The JSON sent is not a valid Request object.");
|
||||
JSON_RPC.METHOD_NOT_FOUND = new JSON_RPC.Error(-32601, "The method does not exist / is not available.");
|
||||
JSON_RPC.INVALID_PARAMS = new JSON_RPC.Error(-32602, "Invalid method parameter(s).");
|
||||
JSON_RPC.INTERNAL_ERROR = new JSON_RPC.Error(-32603, "Internal JSON-RPC error.");
|
||||
|
||||
/**
|
||||
* Parses a JSON-RPC string and converts to a JSON-RPC object or an Array of such strings.
|
||||
* @params rpc A String or Array to parse to a JSON-RPC object.
|
||||
*/
|
||||
JSON_RPC.parse = function (rpc) {
|
||||
// batch?
|
||||
if (rpc.constructor === Array) {
|
||||
var arr = [];
|
||||
rpc.forEach(function (el) {
|
||||
arr.push(JSON_RPC.parse(el));
|
||||
});
|
||||
return arr;
|
||||
}
|
||||
|
||||
// parsable?
|
||||
var rpc;
|
||||
try {
|
||||
rpc = JSON.parse(rpc);
|
||||
} catch (err) {
|
||||
var obj = new JSON_RPC.Request();
|
||||
obj.result = JSON_RPC.PARSE_ERROR;
|
||||
obj.id = null;
|
||||
return obj;
|
||||
}
|
||||
|
||||
// 2.0?
|
||||
if (rpc.jsonrpc !== "2.0") {
|
||||
var obj = new JSON_RPC.Request();
|
||||
obj.result = JSON_RPC.INVALID_REQUEST;
|
||||
obj.id = null;
|
||||
return obj;
|
||||
}
|
||||
|
||||
// request or notification?
|
||||
var obj = (rpc.id === undefined)
|
||||
? new JSON_RPC.Notification(rpc.method, rpc.params)
|
||||
: new JSON_RPC.Request(rpc.method, rpc.params);
|
||||
// have an ID?
|
||||
if (rpc.id !== undefined) obj.id = rpc.id;
|
||||
// is it a result?
|
||||
if (rpc.result !== undefined) obj.result = rpc.result;
|
||||
// is it a error?
|
||||
if (rpc.error !== undefined) {
|
||||
obj.error = new JSON_RPC.Error(
|
||||
rpc.error.code,
|
||||
rpc.error.message,
|
||||
rpc.error.data
|
||||
);
|
||||
}
|
||||
|
||||
// parsed :-)
|
||||
return obj;
|
||||
};
|
||||
|
||||
|
||||
|
||||
BIN
supernode/websocket_chat
Executable file
BIN
supernode/websocket_chat
Executable file
Binary file not shown.
@ -1,296 +0,0 @@
|
||||
<!DOCTYPE html>
|
||||
<meta charset="utf-8" />
|
||||
<title>LocalBitcoin++ Prototype</title>
|
||||
<script language="javascript" type="text/javascript">
|
||||
|
||||
|
||||
/* JSON RPC Library Starts */
|
||||
|
||||
/*
|
||||
The MIT License (MIT)
|
||||
|
||||
Copyright (c) 2014 Oliver Moran
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy of
|
||||
this software and associated documentation files (the "Software"), to deal in
|
||||
the Software without restriction, including without limitation the rights to
|
||||
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
|
||||
of the Software, and to permit persons to whom the Software is furnished to do
|
||||
so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
||||
*/
|
||||
|
||||
var JSON_RPC = {};
|
||||
|
||||
|
||||
|
||||
var id = 0, callbacks = {};
|
||||
|
||||
/**
|
||||
* Constructs a new JSON-RPC Request
|
||||
* @param method A String containing the name of the method to be invoked.
|
||||
* @param params (optional) A Structured value that holds the parameter values to be used during the invocation of the method.
|
||||
*/
|
||||
JSON_RPC.Request = function (method, params) {
|
||||
this.jsonrpc = "2.0";
|
||||
this.method = method;
|
||||
if (typeof params !== "undefined") {
|
||||
this.params = params;
|
||||
}
|
||||
|
||||
this.id = id++;
|
||||
};
|
||||
|
||||
// Implements getters and setters for the result of a JSON-RPC Request.
|
||||
// The result may be an any Object or primitive
|
||||
Object.defineProperty(JSON_RPC.Request.prototype, "result", {
|
||||
get: function () { return this._result; },
|
||||
set: function (result) {
|
||||
delete this.method; // remove the method name
|
||||
delete this.params; // remove the params
|
||||
delete this.error; // remove error state if it exists
|
||||
this._result = result;
|
||||
}
|
||||
});
|
||||
|
||||
// Implements getters and setters for the error state of a JSON-RPC Request.
|
||||
// Error should be a JSON_RPC.Error object
|
||||
Object.defineProperty(JSON_RPC.Request.prototype, "error", {
|
||||
get: function () { return this._error; },
|
||||
set: function (error) {
|
||||
delete this.method; // remove the method name
|
||||
delete this.params; // remove the params
|
||||
delete this.result; // remove result state if it exists
|
||||
this._error = error;
|
||||
}
|
||||
});
|
||||
|
||||
/**
|
||||
* Returns a String representation of a JSON-RPC Request
|
||||
* @returns A JSON String
|
||||
*/
|
||||
JSON_RPC.Request.prototype.toString = function () {
|
||||
var rpc = {
|
||||
jsonrpc: this.jsonrpc,
|
||||
id: this.id
|
||||
};
|
||||
|
||||
if (this.method !== undefined) rpc.method = this.method;
|
||||
if (this.params !== undefined) rpc.params = this.params;
|
||||
if (this.result !== undefined) rpc.result = this.result;
|
||||
if (this.error !== undefined) rpc.error = this.error;
|
||||
|
||||
return JSON.stringify(rpc);
|
||||
};
|
||||
|
||||
/**
|
||||
* Constructs a new JSON-RPC Notification
|
||||
* @param method A String containing the name of the method to be invoked.
|
||||
* @param params (optional) A Structured value that holds the parameter values to be used during the invocation of the method.
|
||||
*/
|
||||
JSON_RPC.Notification = function (method, params) {
|
||||
this.jsonrpc = "2.0";
|
||||
this.method = method;
|
||||
if (typeof params !== "undefined") {
|
||||
this.params = params;
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* Returns a String representation of a JSON-RPC Notification
|
||||
* @returns A JSON String
|
||||
*/
|
||||
JSON_RPC.Notification.prototype.toString = function () {
|
||||
var rpc = {
|
||||
jsonrpc: this.jsonrpc,
|
||||
method: this.method,
|
||||
params: this.params
|
||||
};
|
||||
|
||||
return JSON.stringify(rpc);
|
||||
};
|
||||
|
||||
/**
|
||||
* Constructs a new JSON-RPC Errror object
|
||||
* @params code A Number that indicates the error type that occurred. -32768 to -32000 are reserved.
|
||||
* @param message (optional) A String providing a short description of the error.
|
||||
* @param data (optional) A Primitive or Structured value that contains additional information about the error.
|
||||
*/
|
||||
JSON_RPC.Error = function (code, message, data) {
|
||||
this.code = code;
|
||||
if (typeof message == "string") this.message = message;
|
||||
if (data !== undefined) this.data = data;
|
||||
};
|
||||
|
||||
// stock errors
|
||||
JSON_RPC.PARSE_ERROR = new JSON_RPC.Error(-32700, "An error occurred on the server while parsing the JSON text.");
|
||||
JSON_RPC.INVALID_REQUEST = new JSON_RPC.Error(-32600, "The JSON sent is not a valid Request object.");
|
||||
JSON_RPC.METHOD_NOT_FOUND = new JSON_RPC.Error(-32601, "The method does not exist / is not available.");
|
||||
JSON_RPC.INVALID_PARAMS = new JSON_RPC.Error(-32602, "Invalid method parameter(s).");
|
||||
JSON_RPC.INTERNAL_ERROR = new JSON_RPC.Error(-32603, "Internal JSON-RPC error.");
|
||||
|
||||
/**
|
||||
* Parses a JSON-RPC string and converts to a JSON-RPC object or an Array of such strings.
|
||||
* @params rpc A String or Array to parse to a JSON-RPC object.
|
||||
*/
|
||||
JSON_RPC.parse = function (rpc) {
|
||||
// batch?
|
||||
if (rpc.constructor === Array) {
|
||||
var arr = [];
|
||||
rpc.forEach(function (el) {
|
||||
arr.push(JSON_RPC.parse(el));
|
||||
});
|
||||
return arr;
|
||||
}
|
||||
|
||||
// parsable?
|
||||
var rpc;
|
||||
try {
|
||||
rpc = JSON.parse(rpc);
|
||||
} catch (err) {
|
||||
var obj = new JSON_RPC.Request();
|
||||
obj.result = JSON_RPC.PARSE_ERROR;
|
||||
obj.id = null;
|
||||
return obj;
|
||||
}
|
||||
|
||||
// 2.0?
|
||||
if (rpc.jsonrpc !== "2.0") {
|
||||
var obj = new JSON_RPC.Request();
|
||||
obj.result = JSON_RPC.INVALID_REQUEST;
|
||||
obj.id = null;
|
||||
return obj;
|
||||
}
|
||||
|
||||
// request or notification?
|
||||
var obj = (rpc.id === undefined)
|
||||
? new JSON_RPC.Notification(rpc.method, rpc.params)
|
||||
: new JSON_RPC.Request(rpc.method, rpc.params);
|
||||
// have an ID?
|
||||
if (rpc.id !== undefined) obj.id = rpc.id;
|
||||
// is it a result?
|
||||
if (rpc.result !== undefined) obj.result = rpc.result;
|
||||
// is it a error?
|
||||
if (rpc.error !== undefined) {
|
||||
obj.error = new JSON_RPC.Error(
|
||||
rpc.error.code,
|
||||
rpc.error.message,
|
||||
rpc.error.data
|
||||
);
|
||||
}
|
||||
|
||||
// parsed :-)
|
||||
return obj;
|
||||
};
|
||||
|
||||
/* JSON RPC Library Ends */
|
||||
|
||||
|
||||
/* Custom Localbitcoin++ JSON-RPC code starts here */
|
||||
|
||||
|
||||
|
||||
// var request = new JSON_RPC.Request("SignMessage", "[1,2]");
|
||||
// var id = request.id;
|
||||
// var initialJSONSend = request.toString();
|
||||
|
||||
|
||||
|
||||
|
||||
var request = new JSON_RPC.parse('{"jsonrpc":"2.0","id":0,"method":"SignMessage","params":"[3,4]"}'); //Request is Websocket data received -- websocket.onmessage
|
||||
var methodToExecute = request.method; // if successful
|
||||
executeJSONRequest(methodToExecute);
|
||||
var initialJSONSend = request.toString(); // return to client
|
||||
|
||||
|
||||
|
||||
|
||||
function executeJSONRequest(methodToExecute){
|
||||
if(methodToExecute=="SignMessage") { console.log("SignMessage Executed") };
|
||||
}
|
||||
|
||||
// Start building all functions here
|
||||
|
||||
|
||||
|
||||
/* Custom JSON-RPC code ends */
|
||||
|
||||
|
||||
/* Websocket Code Starts here */
|
||||
|
||||
var wsUri = "ws://localhost:9000/";
|
||||
var output;
|
||||
|
||||
|
||||
|
||||
function init()
|
||||
{
|
||||
output = document.getElementById("output");
|
||||
testWebSocket();
|
||||
}
|
||||
|
||||
function testWebSocket()
|
||||
{
|
||||
websocket = new WebSocket(wsUri);
|
||||
websocket.onopen = function(evt) { onOpen(evt) };
|
||||
websocket.onclose = function(evt) { onClose(evt) };
|
||||
websocket.onmessage = function(evt) { onMessage(evt) };
|
||||
websocket.onerror = function(evt) { onError(evt) };
|
||||
}
|
||||
|
||||
function onOpen(evt)
|
||||
{
|
||||
writeToScreen("CONNECTED");
|
||||
doSend("Intial Hello Message: WebSocket rocks");
|
||||
}
|
||||
|
||||
function onClose(evt)
|
||||
{
|
||||
writeToScreen("DISCONNECTED");
|
||||
}
|
||||
|
||||
function onMessage(evt)
|
||||
{
|
||||
writeToScreen('<span style="color: blue;">RESPONSE: ' + evt.data+'</span>');
|
||||
//websocket.close();
|
||||
}
|
||||
|
||||
function onError(evt)
|
||||
{
|
||||
writeToScreen('<span style="color: red;">ERROR:</span> ' + evt.data);
|
||||
}
|
||||
|
||||
function doSend(message)
|
||||
{
|
||||
writeToScreen("SENT: " + message);
|
||||
websocket.send(message);
|
||||
websocket.send(initialJSONSend);
|
||||
}
|
||||
|
||||
function writeToScreen(message)
|
||||
{
|
||||
var pre = document.createElement("p");
|
||||
pre.style.wordWrap = "break-word";
|
||||
pre.innerHTML = message;
|
||||
output.appendChild(pre);
|
||||
}
|
||||
|
||||
window.addEventListener("load", init, false);
|
||||
|
||||
/* Websocket Code Ends Here*/
|
||||
|
||||
</script>
|
||||
|
||||
<h2>Localbitcoin++ Prototype</h2>
|
||||
|
||||
<div id="output"></div>
|
||||
Loading…
Reference in New Issue
Block a user