merge: merge old web-client
This commit is contained in:
@@ -243,6 +243,11 @@ function onExecuteResult(obj) {
|
||||
onExecuteResultInternal(obj);
|
||||
}
|
||||
|
||||
function onExecuteContractTrustfully(obj) {
|
||||
console.log(obj);
|
||||
$("#consistencyResult").html(global.executeResult.size + "个结果一致");
|
||||
}
|
||||
|
||||
function showContractResult(requestID) {
|
||||
var data = global.responseCollector[requestID][0];
|
||||
result = data;
|
||||
@@ -601,7 +606,7 @@ function renewList() {
|
||||
for (let i = 0; i < global.contracts.length; ++i) {
|
||||
const c = global.contracts[i];
|
||||
console.log(c);
|
||||
if (c.contractStatus=="KILLED") continue;
|
||||
if (c.contractStatus == "KILLED") continue;
|
||||
if (!c.id) {
|
||||
c.id = c.contractID;
|
||||
}
|
||||
|
||||
430
js/createWS.js
430
js/createWS.js
@@ -1,243 +1,241 @@
|
||||
var createWssocket = function(wsurl, onopen, handler) {
|
||||
console.log("[createWS.js] createWssocket : " + wsurl);
|
||||
var retsocket = {};
|
||||
retsocket.handlerList = [];
|
||||
if (handler != undefined)
|
||||
retsocket.handlerList.push(handler);
|
||||
var wssocket = new WebSocket(wsurl);
|
||||
wssocket.onerror = function(error) {
|
||||
console.log(error);
|
||||
};
|
||||
var createWssocket = function (wsurl, onopen, handler) {
|
||||
console.log("[createWS.js] createWssocket : " + wsurl);
|
||||
var retsocket = {};
|
||||
retsocket.handlerList = [];
|
||||
if (handler)
|
||||
retsocket.handlerList.push(handler);
|
||||
var wssocket = new WebSocket(wsurl);
|
||||
wssocket.onerror = function (error) {
|
||||
console.log(error);
|
||||
};
|
||||
|
||||
wssocket.onopen = onopen;
|
||||
var onmessage = function(event) {
|
||||
var obj = JSON.parse(event.data);
|
||||
switch (obj.action) {
|
||||
case 'sendNextSegment':
|
||||
retsocket.sendNextSegment();
|
||||
break;
|
||||
case 'sendSeg':
|
||||
retsocket.receiveSeg(obj);
|
||||
break;
|
||||
default:
|
||||
for (var i = 0; i < retsocket.handlerList.length; i++) {
|
||||
var h = retsocket.handlerList[i];
|
||||
h(event, wssocket);
|
||||
}
|
||||
}
|
||||
};
|
||||
wssocket.onopen = onopen;
|
||||
var onmessage = function (event) {
|
||||
var obj = JSON.parse(event.data);
|
||||
switch (obj.action) {
|
||||
case 'sendNextSegment':
|
||||
retsocket.sendNextSegment();
|
||||
break;
|
||||
case 'sendSeg':
|
||||
retsocket.receiveSeg(obj);
|
||||
break;
|
||||
default:
|
||||
for (var i = 0; i < retsocket.handlerList.length; i++) {
|
||||
var h = retsocket.handlerList[i];
|
||||
h(event, wssocket);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
var reconnect = function(error) {
|
||||
setTimeout(function() {
|
||||
console.log("[createWS.js] try to reconnect");
|
||||
wssocket = new WebSocket(wsurl);
|
||||
wssocket.onclose = reconnect;
|
||||
wssocket.onmessage = onmessage;
|
||||
wssocket.onopen = onopen;
|
||||
}, 1000);
|
||||
};
|
||||
wssocket.onclose = reconnect;
|
||||
var reconnect = function (error) {
|
||||
setTimeout(function () {
|
||||
console.log("[createWS.js] try to reconnect");
|
||||
wssocket = new WebSocket(wsurl);
|
||||
wssocket.onclose = reconnect;
|
||||
wssocket.onmessage = onmessage;
|
||||
wssocket.onopen = onopen;
|
||||
}, 1000);
|
||||
};
|
||||
wssocket.onclose = reconnect;
|
||||
|
||||
retsocket.receiveSeg = function(obj) {
|
||||
if (obj.cid == 'start') {
|
||||
retsocket.toReceive = "";
|
||||
}
|
||||
retsocket.toReceive += obj.data;
|
||||
if (obj.cid == 'done') {
|
||||
console.log("[receiveSeg] Received AllData:" + retsocket.toReceive);
|
||||
var event = {};
|
||||
event.data = retsocket.toReceive;
|
||||
retsocket.toReceive = "";
|
||||
handler(event);
|
||||
}
|
||||
};
|
||||
wssocket.onmessage = onmessage;
|
||||
retsocket.receiveSeg = function (obj) {
|
||||
if (obj.cid === 'start') {
|
||||
retsocket.toReceive = "";
|
||||
}
|
||||
retsocket.toReceive += obj.data;
|
||||
if (obj.cid === 'done') {
|
||||
console.log("[receiveSeg] Received AllData:" + retsocket.toReceive);
|
||||
var event = {};
|
||||
event.data = retsocket.toReceive;
|
||||
retsocket.toReceive = "";
|
||||
handler(event);
|
||||
}
|
||||
};
|
||||
wssocket.onmessage = onmessage;
|
||||
|
||||
retsocket.isSending = false;
|
||||
retsocket.sendList = [];
|
||||
retsocket.monitor = function() {
|
||||
if (!retsocket.isSending) {
|
||||
if (retsocket.sendList.length > 0) {
|
||||
retsocket.send(retsocket.sendList.pop());
|
||||
}
|
||||
}
|
||||
setTimeout(retsocket.monitor, 1000);
|
||||
};
|
||||
// TODO: we don't need monitor at all?
|
||||
retsocket.monitor();
|
||||
retsocket.send = function(str) {
|
||||
if (retsocket.isSending) {
|
||||
retsocket.sendList.push(str);
|
||||
return;
|
||||
}
|
||||
if (str.length > 1024) {
|
||||
retsocket.isSending = true;
|
||||
retsocket.toSend = str.substr(1024);
|
||||
var obj = {};
|
||||
obj.isSegment = true;
|
||||
obj.data = str.substr(0, 1024);
|
||||
wssocket.send(JSON.stringify(obj));
|
||||
} else
|
||||
wssocket.send(str);
|
||||
};
|
||||
retsocket.sendNextSegment = function() {
|
||||
var str = retsocket.toSend;
|
||||
if (str.length > 1024) {
|
||||
retsocket.toSend = str.substr(1024);
|
||||
var obj = {};
|
||||
obj.isSegment = true;
|
||||
obj.data = str.substr(0, 1024);
|
||||
wssocket.send(JSON.stringify(obj));
|
||||
} else {
|
||||
retsocket.toSend = "";
|
||||
var obj = {};
|
||||
obj.isSegment = false;
|
||||
obj.data = str;
|
||||
wssocket.send(JSON.stringify(obj));
|
||||
retsocket.isSending = false;
|
||||
if (retsocket.sendList.length > 0) {
|
||||
retsocket.send(retsocket.sendList.pop());
|
||||
}
|
||||
}
|
||||
};
|
||||
retsocket.isOpen = function() {
|
||||
return wssocket.readyState;
|
||||
}
|
||||
retsocket.addHandler = function(handler) {
|
||||
retsocket.handlerList.push(handler);
|
||||
}
|
||||
return retsocket;
|
||||
retsocket.isSending = false;
|
||||
retsocket.sendList = [];
|
||||
retsocket.monitor = function () {
|
||||
if (!retsocket.isSending) {
|
||||
if (retsocket.sendList.length > 0) {
|
||||
retsocket.send(retsocket.sendList.pop());
|
||||
}
|
||||
}
|
||||
setTimeout(retsocket.monitor, 1000);
|
||||
};
|
||||
// TODO: we don't need monitor at all?
|
||||
retsocket.monitor();
|
||||
retsocket.send = function (str) {
|
||||
if (retsocket.isSending) {
|
||||
retsocket.sendList.push(str);
|
||||
return;
|
||||
}
|
||||
if (str.length > 1024) {
|
||||
retsocket.isSending = true;
|
||||
retsocket.toSend = str.substr(1024);
|
||||
var obj = {};
|
||||
obj.isSegment = true;
|
||||
obj.data = str.substr(0, 1024);
|
||||
wssocket.send(JSON.stringify(obj));
|
||||
} else
|
||||
wssocket.send(str);
|
||||
};
|
||||
retsocket.sendNextSegment = function () {
|
||||
var str = retsocket.toSend;
|
||||
const obj = {};
|
||||
if (str.length > 1024) {
|
||||
retsocket.toSend = str.substr(1024);
|
||||
obj.isSegment = true;
|
||||
obj.data = str.substr(0, 1024);
|
||||
wssocket.send(JSON.stringify(obj));
|
||||
} else {
|
||||
retsocket.toSend = "";
|
||||
obj.isSegment = false;
|
||||
obj.data = str;
|
||||
wssocket.send(JSON.stringify(obj));
|
||||
retsocket.isSending = false;
|
||||
if (retsocket.sendList.length > 0) {
|
||||
retsocket.send(retsocket.sendList.pop());
|
||||
}
|
||||
}
|
||||
};
|
||||
retsocket.isOpen = function () {
|
||||
return wssocket.readyState;
|
||||
}
|
||||
retsocket.addHandler = function (handler) {
|
||||
retsocket.handlerList.push(handler);
|
||||
}
|
||||
return retsocket;
|
||||
};
|
||||
|
||||
var aesDecrypt = function(data) {
|
||||
data = cryptico.b64to256(data);
|
||||
var encryptedBlocks = cryptico.string2bytes(data);
|
||||
var exkey = global.aesKey.slice(0);
|
||||
aes.ExpandKey(exkey);
|
||||
aes.Decrypt(encryptedBlocks, exkey);
|
||||
return cryptico.bytes2string(encryptedBlocks);
|
||||
var aesDecrypt = function (data) {
|
||||
data = cryptico.b64to256(data);
|
||||
var encryptedBlocks = cryptico.string2bytes(data);
|
||||
var exkey = global.aesKey.slice(0);
|
||||
aes.ExpandKey(exkey);
|
||||
aes.Decrypt(encryptedBlocks, exkey);
|
||||
return cryptico.bytes2string(encryptedBlocks);
|
||||
|
||||
};
|
||||
var aesEncrypt = function(data, aesKey) {
|
||||
var key = aesKey;
|
||||
var exkey = key.slice(0);
|
||||
aes.ExpandKey(exkey);
|
||||
var blocks = my.string2bytes(data);
|
||||
blocks = my.pad16(blocks);
|
||||
aes.Encrypt(blocks, exkey);
|
||||
ciphertext = cryptico.bytes2string(blocks);
|
||||
ciphertext = cryptico.b256to64(ciphertext);
|
||||
return ciphertext;
|
||||
var aesEncrypt = function (data, aesKey) {
|
||||
var exkey = aesKey.slice(0);
|
||||
aes.ExpandKey(exkey);
|
||||
var blocks = my.string2bytes(data);
|
||||
blocks = my.pad16(blocks);
|
||||
aes.Encrypt(blocks, exkey);
|
||||
ciphertext = cryptico.bytes2string(blocks);
|
||||
ciphertext = cryptico.b256to64(ciphertext);
|
||||
return ciphertext;
|
||||
};
|
||||
var rsaEncrypt = function(data, rsaKey) {
|
||||
var rsa = new RSAKey();
|
||||
rsa.setPublic(rsaKey.n, rsaKey.e1);
|
||||
var result = rsa.encrypt(data);
|
||||
return result;
|
||||
var rsaEncrypt = function (data, rsaKey) {
|
||||
var rsa = new RSAKey();
|
||||
rsa.setPublic(rsaKey.n, rsaKey.e1);
|
||||
return rsa.encrypt(data);
|
||||
};
|
||||
var loadRSAKey = function(rsaKey) {
|
||||
var str = cryptico.b64to256(rsaKey);
|
||||
str = str.split(",");
|
||||
var ret = {};
|
||||
ret.n = str[0];
|
||||
ret.e1 = str[1];
|
||||
ret.e2 = str[2];
|
||||
return ret;
|
||||
var loadRSAKey = function (rsaKey) {
|
||||
var str = cryptico.b64to256(rsaKey);
|
||||
str = str.split(",");
|
||||
var ret = {};
|
||||
ret.n = str[0];
|
||||
ret.e1 = str[1];
|
||||
ret.e2 = str[2];
|
||||
return ret;
|
||||
};
|
||||
var testRSA = function() {
|
||||
pubKey = loadRSAKey(global.privKey);
|
||||
reqContent = {};
|
||||
reqContent.action = "main";
|
||||
reqContent.arg = "[{\"score\":20},{\"score\":20}]";
|
||||
reqContent.contractID = "abc";
|
||||
eReq = encryptReq(reqContent, pubKey);
|
||||
url = "http://localhost:8080/SCIDE/SCManager?action=executeContractEncrypted&contractRequest="
|
||||
+ encodeURIComponent(JSON.stringify(eReq));
|
||||
var testRSA = function () {
|
||||
pubKey = loadRSAKey(global.privKey);
|
||||
reqContent = {};
|
||||
reqContent.action = "main";
|
||||
reqContent.arg = "[{\"score\":20},{\"score\":20}]";
|
||||
reqContent.contractID = "abc";
|
||||
eReq = encryptReq(reqContent, pubKey);
|
||||
url = "http://localhost:8080/SCIDE/SCManager?action=executeContractEncrypted&contractRequest="
|
||||
+ encodeURIComponent(JSON.stringify(eReq));
|
||||
};
|
||||
|
||||
var encryptReq = function(reqContent, pubKey) {
|
||||
// global.pubKey = loadRSAKey(global.privKey);
|
||||
var aes = {};
|
||||
aes.key = cryptico.generateAESKey();
|
||||
var aesObj = JSON.stringify(aes);
|
||||
var rsa = new RSAKey();
|
||||
rsa.setPrivate(pubKey.n, pubKey.e1, pubKey.e2);
|
||||
var encrypedReq = {};
|
||||
encrypedReq.action = rsa.decrypt(aesObj);
|
||||
encrypedReq.contractID = reqContent.contractID;
|
||||
reqContent.contractID = undefined;
|
||||
encrypedReq.arg = JSON.stringify(reqContent);
|
||||
encrypedReq.arg = aesEncrypt(encrypedReq.arg, aes.key);
|
||||
encrypedReq.requester = pubKey.n + "," + pubKey.e1 + "," + "0";
|
||||
encrypedReq.requester = cryptico.b256to64(encrypedReq.requester);
|
||||
return encrypedReq;
|
||||
var encryptReq = function (reqContent, pubKey) {
|
||||
// global.pubKey = loadRSAKey(global.privKey);
|
||||
var aes = {};
|
||||
aes.key = cryptico.generateAESKey();
|
||||
var aesObj = JSON.stringify(aes);
|
||||
var rsa = new RSAKey();
|
||||
rsa.setPrivate(pubKey.n, pubKey.e1, pubKey.e2);
|
||||
var encrypedReq = {};
|
||||
encrypedReq.action = rsa.decrypt(aesObj);
|
||||
encrypedReq.contractID = reqContent.contractID;
|
||||
reqContent.contractID = undefined;
|
||||
encrypedReq.arg = JSON.stringify(reqContent);
|
||||
encrypedReq.arg = aesEncrypt(encrypedReq.arg, aes.key);
|
||||
encrypedReq.requester = pubKey.n + "," + pubKey.e1 + "," + "0";
|
||||
encrypedReq.requester = cryptico.b256to64(encrypedReq.requester);
|
||||
return encrypedReq;
|
||||
};
|
||||
|
||||
var onExecuteResultInternal = function(data) {
|
||||
console.log(data);
|
||||
global.executeResult = data;
|
||||
var reqId = data.responseID;
|
||||
var callback = global.cbs[data.responseID];
|
||||
if (callback != undefined) {
|
||||
callback(global.executeResult, data);
|
||||
}
|
||||
var onExecuteResultInternal = function (data) {
|
||||
console.log(data);
|
||||
global.executeResult = data;
|
||||
var reqId = data.responseID;
|
||||
var callback = global.cbs[data.responseID];
|
||||
if (callback) {
|
||||
callback(global.executeResult, data);
|
||||
}
|
||||
};
|
||||
window.executeContract = function(contractID, method, strarg, cb) {
|
||||
var sm2Key = global.sm2Key;
|
||||
var request = {};
|
||||
request.action = "executeContract";
|
||||
request.requestID = new Date().getTime() + "_"
|
||||
+ Math.floor(Math.random() * 10000);
|
||||
window.executeContract = function (contractID, method, strarg, cb) {
|
||||
var sm2Key = global.sm2Key;
|
||||
var request = {};
|
||||
request.action = "executeContract";
|
||||
request.requestID = new Date().getTime() + "_"
|
||||
+ Math.floor(Math.random() * 10000);
|
||||
|
||||
global.cbs[request.requestID] = cb;
|
||||
request.contractID = contractID;
|
||||
request.operation=method;
|
||||
request.arg = strarg;
|
||||
global.cbs[request.requestID] = cb;
|
||||
request.contractID = contractID;
|
||||
request.operation = method;
|
||||
request.arg = strarg;
|
||||
|
||||
if (sm2Key != undefined) {
|
||||
request.pubkey = sm2Key.publicKey;
|
||||
request.signature = sm2.doSignature(request.contractID + "|"
|
||||
+ method + "|" + strarg + "|" + sm2Key.publicKey,
|
||||
sm2Key.privateKey,{hash:true,der:true});
|
||||
}
|
||||
if (sm2Key) {
|
||||
request.pubkey = sm2Key.publicKey;
|
||||
request.signature = sm2.doSignature(request.contractID + "|"
|
||||
+ method + "|" + strarg + "|" + sm2Key.publicKey,
|
||||
sm2Key.privateKey, {hash: true, der: true});
|
||||
}
|
||||
|
||||
global.wssocket.send(JSON.stringify(request));
|
||||
global.wssocket.send(JSON.stringify(request));
|
||||
};
|
||||
|
||||
window.executeCurrentContract = function(method, strarg, cb) {
|
||||
console.log("executeCurrentContract");
|
||||
var contract = global.currentContract;
|
||||
executeContract(contract.id, method, strarg, cb);
|
||||
window.executeCurrentContract = function (method, strarg, cb) {
|
||||
console.log("executeCurrentContract");
|
||||
var contract = global.currentContract;
|
||||
executeContract(contract.id, method, strarg, cb);
|
||||
};
|
||||
|
||||
|
||||
window.loadBDWareWebSDK = function(url, onopen, handler) {
|
||||
if (window.global == undefined) {
|
||||
window.global = {};
|
||||
}
|
||||
if (global.sm2Key == undefined) {
|
||||
if (sm2 != undefined)
|
||||
global.sm2Key = sm2.generateKeyPairHex();
|
||||
}
|
||||
if (global.cbs == undefined) {
|
||||
global.cbs = {};
|
||||
}
|
||||
if (global.wssocket == undefined) {
|
||||
global.wssocket = createWssocket(url, onopen, function(event, wss) {
|
||||
var data = event.data;
|
||||
var obj = JSON.parse(data);
|
||||
switch (obj.action) {
|
||||
case 'onExecuteResult':
|
||||
onExecuteResultInternal(obj);
|
||||
default:
|
||||
}
|
||||
handler(event, global.wssocket);
|
||||
});
|
||||
} else {
|
||||
if (onopen != undefined)
|
||||
onopen();
|
||||
global.wssocket.addHandler(handler);
|
||||
}
|
||||
window.loadBDWareWebSDK = function (url, onopen, handler) {
|
||||
if (!window.global) {
|
||||
window.global = {};
|
||||
}
|
||||
if (!global.sm2Key) {
|
||||
if (sm2)
|
||||
global.sm2Key = sm2.generateKeyPairHex();
|
||||
}
|
||||
if (!global.cbs) {
|
||||
global.cbs = {};
|
||||
}
|
||||
if (!global.wssocket) {
|
||||
global.wssocket = createWssocket(url, onopen, function (event, wss) {
|
||||
var data = event.data;
|
||||
var obj = JSON.parse(data);
|
||||
switch (obj.action) {
|
||||
case 'onExecuteResult':
|
||||
onExecuteResultInternal(obj);
|
||||
default:
|
||||
break;
|
||||
}
|
||||
handler(event, global.wssocket);
|
||||
});
|
||||
} else {
|
||||
if (onopen)
|
||||
onopen();
|
||||
global.wssocket.addHandler(handler);
|
||||
}
|
||||
};
|
||||
// Usage:
|
||||
// Approach 1:
|
||||
|
||||
Reference in New Issue
Block a user