3 lines
19 KiB
JavaScript
3 lines
19 KiB
JavaScript
|
|
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports,require("@daotl/cryptico"),require("@lifeomic/axios-fetch"),require("axios"),require("sm-crypto")):"function"==typeof define&&define.amd?define(["exports","@daotl/cryptico","@lifeomic/axios-fetch","axios","sm-crypto"],t):t((e="undefined"!=typeof globalThis?globalThis:e||self).bdcontract={},e.cryptico,e.axiosFetch,e.axios,e.smCrypto)}(this,(function(e,t,s,i,r){"use strict";function a(e){return e&&"object"==typeof e&&"default"in e?e:{default:e}}var n=a(i);function o(e,s){const i=s.slice(0);t.aes.ExpandKey(i);let r=t.cryptico.string2bytes(e);r=t.cryptico.pad16(r),t.aes.Encrypt(r,i);let a=t.cryptico.bytes2string(r);return a=t.cryptico.b256to64(a),a}const c=e=>Object.entries(e).map((([e,t])=>`${e}=${String(t)}`)).join("&");class h{handlerList=[];toSend="";isSending=!1;sendList=[];toReceive="";wssocket;constructor(e,t,s){console.log(`[createWS.js] createWssocket : ${e}`),s&&this.handlerList.push(s),this.monitor();const i=this,r=this.wssocket=new WebSocket(e);r.onerror=function(e){console.log(e)},r.onopen=t;const a=function(e){const t=JSON.parse(e.data);switch(t.action){case"sendNextSegment":i.sendNextSegment();break;case"sendSeg":i.receiveSeg(t);break;default:i.handlerList.forEach((t=>t(e,this)))}};r.onmessage=a;const n=function(){setTimeout((()=>{console.log("[createWS.js] try to reconnect");const s=i.wssocket=new WebSocket(e);s.onclose=n,s.onmessage=a,s.onopen=t}),1e3)};r.onclose=n}status(){return this.wssocket.readyState}sendNextSegment(){const e=this.toSend;if(e.length>1024){this.toSend=e.substr(1024);const t={isSegment:!0,data:e.substr(0,1024)};this.wssocket.send(JSON.stringify(t))}else{this.toSend="";const t={isSegment:!1,data:e};let s;this.wssocket.send(JSON.stringify(t)),this.isSending=!1,(s=this.sendList.pop())&&this.send(s)}}receiveSeg(e){if("start"===e.cid&&(this.toReceive=""),this.toReceive+=e.data,"done"===e.cid){console.log(`[receiveSeg] Received AllData:${this.toReceive}`);const e={data:this.toReceive};this.toReceive="",this.handlerList.forEach((t=>t(e,this.wssocket)))}}monitor(){if(!this.isSending){let e;(e=this.sendList.pop())&&this.send(e)}setTimeout((()=>this.monitor()),1e3)}send(e){if(this.isSending)this.sendList.push(e);else if(e.length>1024){this.isSending=!0,this.toSend=e.substr(1024);const t={isSegment:!0,data:e.substr(0,1024)};this.wssocket.send(JSON.stringify(t))}else this.wssocket.send(e)}addHandler(e){this.handlerList.push(e)}}e.HttpClient=class{baseUrl;sm2Key;fetch;constructor(e,t,i={}){this.baseUrl=e,this.sm2Key=t;const r=n.default.create({baseURL:"https://some-domain.com/api/",timeout:1e4,...i});this.fetch=s.buildAxiosFetch(r)}async requestWithSignature(e,t,s){return new Promise(((i,r)=>{const a=this.baseUrl+e,n=`${a}${e.includes("?")?"&":"?"}pubKey=${s?.publicKey??this.sm2Key.publicKey}`,o=this.sign(n.substring(n.indexOf("?")+1),s?.privateKey);this.fetch("post"===t?.method?.toLowerCase()?a:`${encodeURI(n)}&sign=${o}`,{...t,..."POST"===t?.method?{body:{...t?.body,sign:o}}:{}}).then((async e=>{const t=e;t.data=await e.text(),setTimeout((()=>{i(t)}),1)})).catch((e=>{setTimeout((()=>{r(e)}),1)}))}))}async retryRequestWithSignature(e,t,s,i){let r=new Error;for(let a=0;a<e;a++)try{return await this.requestWithSignature(t,s,i)}catch(t){if(a<e){console.log("1");continue}r=t}throw r}sign(e,t){return r.sm2.doSignature(e,t??this.sm2Key.privateKey,{hash:!0,der:!0})}ping(){return this.requestWithSignature("/SCManager?action=ping")}startContract(e){const t={action:"startContract",script:e};return this.requestWithSignature(`/SCManager?${c(t)}`)}startContractByYPK(e){return this.requestWithSignature(`/SCManager?${c({action:"startContractByYPK",...e,owner:this.sm2Key.publicKey,aim:"onStartContract",signature:r.sm2.doSignature(`Fixed|${e.path}|${this.sm2Key.publicKey}`,this.sm2Key.privateKey,{hash:!0,der:!0})})}`)}executeContract(e,t,s,{method:i="POST",withDynamicAnalysis:r=!1,withSignature:a=!1}={}){let n={};return n=void 0===r?{action:"executeContract",contractID:e,operation:t,arg:s}:{action:"executeContract",contractID:e,operation:t
|
||
|
|
//# sourceMappingURL=index.js.map
|