contract-java-example/front/html/bdcontract-sdk.iife.js

3 lines
19 KiB
JavaScript
Raw Normal View History

2023-03-18 13:57:34 +00:00
var bdcontract=function(e,t,s,r,i){"use strict";function n(e){return e&&"object"==typeof e&&"default"in e?e:{default:e}}var a=n(r);function o(e,s){const r=s.slice(0);t.aes.ExpandKey(r);let i=t.cryptico.string2bytes(e);i=t.cryptico.pad16(i),t.aes.Encrypt(i,r);let n=t.cryptico.bytes2string(i);return n=t.cryptico.b256to64(n),n}const c=e=>Object.entries(e).map((([e,t])=>`${e}=${String(t)}`)).join("&"),h=e=>async t=>{let s=0;for(;;)try{return await t()}catch(t){if(s++===e)throw t}};class u{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 r=this,i=this.wssocket=new WebSocket(e);i.onerror=function(e){console.log(e)},i.onopen=t;const n=function(e){const t=JSON.parse(e.data);switch(t.action){case"sendNextSegment":r.sendNextSegment();break;case"sendSeg":r.receiveSeg(t);break;default:r.handlerList.forEach((t=>t(e,this)))}};i.onmessage=n;const a=function(){setTimeout((()=>{console.log("[createWS.js] try to reconnect");const s=r.wssocket=new WebSocket(e);s.onclose=a,s.onmessage=n,s.onopen=t}),1e3)};i.onclose=a}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)}}return e.HttpClient=class{baseUrl;sm2Key;fetch;retryAsync;constructor(e,t,r={axios:{},maxRetries:3}){this.baseUrl=e,this.sm2Key=t;const i=a.default.create({baseURL:"https://some-domain.com/api/",timeout:1e4,...r.axios});this.fetch=s.buildAxiosFetch(i),this.retryAsync=h(r.maxRetries)}async requestWithSignature(e,t,s){return new Promise(((r,i)=>{const n=this.baseUrl+e,a=`${n}${e.includes("?")?"&":"?"}pubKey=${s?.publicKey??this.sm2Key.publicKey}`,o=this.sign(a.substring(a.indexOf("?")+1),s?.privateKey);this.fetch("post"===t?.method?.toLowerCase()?n:`${encodeURI(a)}&sign=${o}`,{...t,..."POST"===t?.method?{body:{...t?.body,sign:o}}:{}}).then((async e=>{const t=e;t.data=await e.text(),setTimeout((()=>{r(t)}),1)})).catch((e=>{setTimeout((()=>{i(e)}),1)}))}))}sign(e,t){return i.sm2.doSignature(e,t??this.sm2Key.privateKey,{hash:!0,der:!0})}ping(){return this.retryAsync((()=>this.requestWithSignature("/SCManager?action=ping")))}startContract(e){const t={action:"startContract",script:e};return this.retryAsync((()=>this.requestWithSignature(`/SCManager?${c(t)}`)))}startContractByYPK(e){return this.retryAsync((()=>this.requestWithSignature(`/SCManager?${c({action:"startContractByYPK",...e,owner:this.sm2Key.publicKey,aim:"onStartContract",signature:i.sm2.doSignature(`Fixed|${e.path}|${this.sm2Key.publicKey}`,this.sm2Key.privateKey,{hash:!0,der:!0})})}`)))}executeContract(e,t,s,{method:r="POST",withDynamicAnalysis:i=!1,withSignature:n=!1}={}){let a={};return a=void 0===i?{action:"executeContract",contractID:e,operation:t,arg:s}:{action:"executeContract",contractID:e,operation:t,withDynamicAnalysis:i,arg:s},n&&(a={...a,pubkey:this.sm2Key.publicKey,signature:this.sign(`${e}|${t}|${s??""}|${this.sm2Key.publicKey}`)}),this.retryAsync((()=>this.requestWithSignature("/SCManager"+("GET"===r?`?${c(a)}`:""),{method:r,..."POST"===r?{body:a}:{}})))}killContractProcess(e,t){const s={action:"killContractProcess",id:e};return t&&(s.requestID=t)
//# sourceMappingURL=bdcontract-sdk.iife.js.map