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),this.retryAsync((()=>this.requestWithSignature(`/SCManager?${c(s)}`)))}killAllContract(){return this.retryAsync((()=>this.requestWithSignature("/SCManager?action=killAllContract")))}applyNodeRole(e){const t={action:"applyNodeRole",role:e};return this.retryAsync((()=>this.requestWithSignature(`/SCManager?${c(t)}`)))}authNodeRole(e,t,s){const r={action:"authNodeRole",isAccept:e,authorizedPubKey:t};return this.retryAsync((()=>this.requestWithSignature(`/SCManager?${c(r)}`,void 0,s)))}distributeContract(e,t,s){const r=i.sm2.doSignature(`DistributeContract|${t}|${this.sm2Key.publicKey}`,this.sm2Key.privateKey,{hash:!0,der:!0}),n=`/SCManager?${c({action:"distributeContract",nodeIDs:e,projectName:t,isPrivate:s,signature:r})}`,a=`${this.baseUrl+n}${n.includes("?")?"&":"?"}pubKey=${this.sm2Key.publicKey}`,o=i.sm2.doSignature(a.substring(a.indexOf("?")+1),this.sm2Key.privateKey,{hash:!0,der:!0});new EventSource(`${this.baseUrl}${n}&pubKey=${this.sm2Key.publicKey}&sign=${o}`).addEventListener("message",(function(e){}),!1)}saveFile(e){return this.retryAsync((()=>this.requestWithSignature(`/SCManager?${c({action:"saveFile",...e})}`)))}listProjectPermission(e){return this.retryAsync((()=>this.requestWithSignature(`/SCManager?${c({action:"listProjectPermission",...e})}`)))}startContractMultiPoint(e,t,s,r,i,n){const a={peersID:e,type:t,selectUnitNum:s,projectName:r,isPrivate:i,sponsorPeerID:n};return this.retryAsync((()=>this.requestWithSignature(`/SCManager?${c({action:"startContractMultiPoint",...a})}`)))}async loadNodeConfig(){const e=await this.retryAsync((()=>this.requestWithSignature(`/SCManager?${c({action:"loadNodeConfig"})}`)));return{...e,data:200===e.status&&e.data?JSON.parse(e.data).data:{}}}async updateConfig(e,t){return this.retryAsync((()=>this.requestWithSignature(`/SCManager?${c({action:"updateConfig",key:e,val:t})}`)))}async resetNodeManager(){const e=await this.retryAsync((()=>this.requestWithSignature(`/SCManager?${c({action:"resetNodeManager"})}`)));if(!e.data)return!1;return"success"===JSON.parse(e.data).data}lockEdit(){return this.retryAsync((()=>this.requestWithSignature(`/SCManager?${c({action:"lockEdit"})}`)))}unlockEdit(){return this.retryAsync((()=>this.requestWithSignature(`/SCManager?${c({action:"unlockEdit"})}`)))}addNode(e){return this.retryAsync((()=>this.requestWithSignature(`?${c({action:"addNode",nodePubKey:e})}`)))}applyRole(e){return this.retryAsync((()=>this.requestWithSignature(`?${c({action:"applyRole",role:e})}`)))}authNodeManager(e,t){return this.retryAsync((()=>this.requestWithSignature(`?${c({action:"authNodeManager",isAccept:e,authorizedPubKey:t})}`)))}listAllUsers(){return this.retryAsync((()=>this.requestWithSignature(`?${c({action:"listAllUsers"})}`)))}listNodes(){return this.retryAsync((()=>this.requestWithSignature(`?${c({action:"listNodes"})}`)))}async createTrustUnit(e,t){const s=`action=createTrustUnit&data=${JSON.stringify(e)}&msg=${t}&pubKey=04303718771b9323c204e607639f14469f9a94e55b0964a408ad3b3864b0493b645d7070da0d550f0c54b934275a8e88dedc3024467b0566db5c1108b1baeaae27`,r={action:"createTrustUnit",data:e,msg:t,pubKey:"04303718771b9323c204e607639f14469f9a94e55b0964a408ad3b3864b0493b645d7070da0d550f0c54b934275a8e88dedc3024467b0566db5c1108b1baeaae27",sign:i.sm2.doSignature(s,this.sm2Key.privateKey,{hash:!0,der:!0})};return await this.retryAsync((()=>this.requestWithSignature("",{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify(r)})))}listTrustUnits(){return this.retryAsync((()=>this.requestWithSignature(`?${c({action:"listTrustUnits"})}`)))}listContractProcess(){return this.retryAsync((()=>this.requestWithSignature(`/SCManager?${c({action:"listContractProcess"})}`)))}downloadContract(e,t,s){const r={action:"downloadContract",projectName:e,isPrivate:t,timestamp:s};return this.retryAsync((()=>this.requestWithSignature(`/CMManager?${c(r)}`)))}async configNode(e){if(!await this.resetNodeManager())return!1;const t=Object.entries(e).filter((([e,t])=>t));await Promise.all([...t.map((([e,t])=>this.updateConfig(e,t))),this.applyNodeRole("ContractProvider"),this.applyNodeRole("ContractUser"),this.applyNodeRole("ContractInstanceManager"),this.authNodeRole(!0,this.sm2Key.publicKey)]);const s=(await this.loadNodeConfig()).data;return!!s&&t.every((([e,t])=>s[function(e){if("dataChain"===e)return"bdledger";return e}(e)]===t))}},e.WsClient=class{sm2Key;wssocket;promiseCallbackPairs={};sessionPromise;sessionResolve;loginPromise;loginResolve;constructor(e,t,s,r=i.sm2.generateKeyPairHex()){this.sm2Key=r,this.sessionPromise=new Promise(((e,t)=>{this.sessionResolve=e})),this.loginPromise=new Promise(((e,t)=>{this.loginResolve=e}));const n=this;this.wssocket=new u(e,t,(function(e,t){const r=JSON.parse(e.data);switch(r.action){case"onSessionID":n.sessionResolve(r.session);break;case"onLogin":{const e="string"==typeof r.status&&r.status.toLowerCase().includes("failed");n.loginResolve(!e)}}const i=r.responseID;let a;i&&(a=n.promiseCallbackPairs[i])&&(r?a.resolve(r):a.reject(r)),s(e,this)}))}status(){return this.wssocket.status()}sessionReceived(){return this.sessionPromise}async login(){const e=await this.sessionPromise,t={action:"login",pubKey:this.sm2Key.publicKey,signature:i.sm2.doSignature(e,this.sm2Key.privateKey,{hash:!0,der:!0})};return this.wssocket.send(JSON.stringify(t)),this.loginPromise}loggedIn(){return this.loginPromise}matchCID(e){const t=`${(new Date).getTime()}_${Math.floor(1e4*Math.random())}`,s={action:"matchCID",contractID:e};return this.wssocket.send(JSON.stringify(s)),new Promise(((e,s)=>{this.promiseCallbackPairs[t]={resolve:e,reject:s}}))}getMetabyCID(e){const t=`${(new Date).getTime()}_${Math.floor(1e4*Math.random())}`,s={action:"getMetabyCID",contractID:e,requestID:t};return this.wssocket.send(JSON.stringify(s)),new Promise(((e,s)=>{this.promiseCallbackPairs[t]={resolve:e,reject:s}}))}getMetabyReadme(e,t,s){const r=`${(new Date).getTime()}_${Math.floor(1e4*Math.random())}`,i={action:"getMetabyReadme",page:t,pageSize:s,keyword:e,requestID:r};return this.wssocket.send(JSON.stringify(i)),new Promise(((e,t)=>{this.promiseCallbackPairs[r]={resolve:e,reject:t}}))}getMetabyPubkey(e){const t=`${(new Date).getTime()}_${Math.floor(1e4*Math.random())}`,s={action:"getMetabyPubkey",pubkey:e,requestID:t};return this.wssocket.send(JSON.stringify(s)),new Promise(((e,s)=>{this.promiseCallbackPairs[t]={resolve:e,reject:s}}))}segmentWord(e){const t=`${(new Date).getTime()}_${Math.floor(1e4*Math.random())}`,s={words:e,action:"segmentWord",requestID:t};return this.wssocket.send(JSON.stringify(s)),new Promise(((e,s)=>{this.promiseCallbackPairs[t]={resolve:e,reject:s}}))}getMetabyOwner(e,t,s){const r=`${(new Date).getTime()}_${Math.floor(1e4*Math.random())}`,i={action:"getMetabyOwner",owner:e,page:t,pageSize:s,requestID:r};return this.wssocket.send(JSON.stringify(i)),new Promise(((e,t)=>{this.promiseCallbackPairs[r]={resolve:e,reject:t}}))}getDependentContract(e){const t=`${(new Date).getTime()}_${Math.floor(1e4*Math.random())}`,s={action:"getDependentContract",requestID:t,contractName:e};return this.wssocket.send(JSON.stringify(s)),new Promise(((e,s)=>{this.promiseCallbackPairs[t]={resolve:e,reject:s}}))}queryContractLogByDate(e){const t=`${(new Date).getTime()}_${Math.floor(1e4*Math.random())}`,s={action:"queryContractLogByDate",requestID:t,start:e};return this.wssocket.send(JSON.stringify(s)),new Promise(((e,s)=>{this.promiseCallbackPairs[t]={resolve:e,reject:s}}))}queryDataByHash(e){const t=`${(new Date).getTime()}_${Math.floor(1e4*Math.random())}`,s={action:"queryDataByHash",requestID:t,hash:e};return this.wssocket.send(JSON.stringify(s)),new Promise(((e,s)=>{this.promiseCallbackPairs[t]={resolve:e,reject:s}}))}executeContract(e,t,s){const r=this.sm2Key,n=`${(new Date).getTime()}_${Math.floor(1e4*Math.random())}`,a="object"==typeof s?JSON.stringify(s):s+"",o={action:"executeContract",requestID:n,contractID:e,operation:t,arg:s,...r?{pubkey:r.publicKey,signature:i.sm2.doSignature(`${e}|${t}|${a}|${r.publicKey}`,r.privateKey,{hash:!0,der:!0})}:{}};return this.wssocket.send(JSON.stringify(o)),new Promise(((e,t)=>{this.promiseCallbackPairs[n]={resolve:e,reject:t}}))}getSessionID(){const e=`${(new Date).getTime()}_${Math.floor(1e4*Math.random())}`,t={action:"getSessionID",requestID:e};return this.wssocket.send(JSON.stringify(t)),new Promise(((t,s)=>{this.promiseCallbackPairs[e]={resolve:t,reject:s}}))}listTheContractProcess(e){const t=this.sm2Key,s=`${(new Date).getTime()}_${Math.floor(1e4*Math.random())}`,r={action:"listTheContractProcess",requestID:s,contractID:e,...t?{pubkey:t.publicKey,signature:i.sm2.doSignature(`${e}|${t.publicKey}`,t.privateKey,{hash:!0,der:!0})}:{}};return this.wssocket.send(JSON.stringify(r)),new Promise(((e,t)=>{this.promiseCallbackPairs[s]={resolve:e,reject:t}}))}getMask(e){const t=this.sm2Key,s=`${(new Date).getTime()}_${Math.floor(1e4*Math.random())}`,r={action:"getMask",requestID:s,contractID:e,...t?{pubkey:t.publicKey,signature:i.sm2.doSignature(`${e}|${t.publicKey}`,t.privateKey,{hash:!0,der:!0})}:{}};return this.wssocket.send(JSON.stringify(r)),new Promise(((e,t)=>{this.promiseCallbackPairs[s]={resolve:e,reject:t}}))}setMask(e,t,s){const r=this.sm2Key,n=`${(new Date).getTime()}_${Math.floor(1e4*Math.random())}`,a={action:"setMask",requestID:n,contractID:e,operation:t,arg:s,...r?{pubkey:r.publicKey,signature:i.sm2.doSignature(`${e}|${r.publicKey}`,r.privateKey,{hash:!0,der:!0})}:{}};return this.wssocket.send(JSON.stringify(a)),new Promise(((e,t)=>{this.promiseCallbackPairs[n]={resolve:e,reject:t}}))}getMock(e){const t=this.sm2Key,s=`${(new Date).getTime()}_${Math.floor(1e4*Math.random())}`,r={action:"getMock",requestID:s,contractID:e,...t?{pubkey:t.publicKey,signature:i.sm2.doSignature(`${e}|${t.publicKey}`,t.privateKey,{hash:!0,der:!0})}:{}};return this.wssocket.send(JSON.stringify(r)),new Promise(((e,t)=>{this.promiseCallbackPairs[s]={resolve:e,reject:t}}))}setMock(e,t,s){const r=this.sm2Key,n=`${(new Date).getTime()}_${Math.floor(1e4*Math.random())}`,a={action:"setMock",requestID:n,contractID:e,operation:t,arg:s,...r?{pubkey:r.publicKey,signature:i.sm2.doSignature(`${e}|${r.publicKey}`,r.privateKey,{hash:!0,der:!0})}:{}};return this.wssocket.send(JSON.stringify(a)),new Promise(((e,t)=>{this.promiseCallbackPairs[n]={resolve:e,reject:t}}))}queryHashByOffset(e,t){const s=this.sm2Key,r=`${(new Date).getTime()}_${Math.floor(1e4*Math.random())}`,i={action:"queryHashByOffset",requestID:r,offset:e,count:t,...s?{pubkey:s.publicKey}:{}};return this.wssocket.send(JSON.stringify(i)),new Promise(((e,t)=>{this.promiseCallbackPairs[r]={resolve:e,reject:t}}))}loadNodeConfig(){const e=this.sm2Key,t=`${(new Date).getTime()}_${Math.floor(1e4*Math.random())}`,s={action:"loadNodeConfig",requestID:t,...e?{pubkey:e.publicKey,signature:i.sm2.doSignature(e.publicKey,e.privateKey,{hash:!0,der:!0})}:{}};return this.wssocket.send(JSON.stringify(s)),new Promise(((e,s)=>{this.promiseCallbackPairs[t]={resolve:e,reject:s}}))}queryUserStat(){const e=`${(new Date).getTime()}_${Math.floor(1e4*Math.random())}`,t={action:"queryUserStat",requestID:e};return this.wssocket.send(JSON.stringify(t)),new Promise(((t,s)=>{this.promiseCallbackPairs[e]={resolve:t,reject:s}}))}listNodes(){const e=`${(new Date).getTime()}_${Math.floor(1e4*Math.random())}`,t={action:"listNodes",requestID:e};return this.wssocket.send(JSON.stringify(t)),new Promise(((t,s)=>{this.promiseCallbackPairs[e]={resolve:t,reject:s}}))}killContractProcess(e){const t=`${(new Date).getTime()}_${Math.floor(1e4*Math.random())}`,s={action:"killContractProcess",requestID:t,id:e};return this.wssocket.send(JSON.stringify(s)),new Promise(((e,s)=>{this.promiseCallbackPairs[t]={resolve:e,reject:s}}))}distributeYPK(e,t){const s=this.sm2Key,r=`${(new Date).getTime()}_${Math.floor(1e4*Math.random())}`,n={action:"distributeYPK",requestID:r,pubKey:s.publicKey,projectName:e,nodeIDs:t,signature:i.sm2.doSignature(`DistributeYPK|${e}|${s.publicKey}`,s.privateKey,{hash:!0,der:!0})};return this.wssocket.send(JSON.stringify(n)),new Promise(((e,t)=>{this.promiseCallbackPairs[r]={resolve:e,reject:t}}))}listYPKs(){const e=`${(new Date).getTime()}_${Math.floor(1e4*Math.random())}`,t={action:"listYPKs",requestID:e};return this.wssocket.send(JSON.stringify(t)),new Promise(((t,s)=>{this.promiseCallbackPairs[e]={resolve:t,reject:s}}))}deleteFile(e){const t=`${(new Date).getTime()}_${Math.floor(1e4*Math.random())}`,s={action:"deleteFile",requestID:t,file:e};return this.wssocket.send(JSON.stringify(s)),new Promise(((e,s)=>{this.promiseCallbackPairs[t]={resolve:e,reject:s}}))}startContractByYPK(e){const t=this.sm2Key,s=`${(new Date).getTime()}_${Math.floor(1e4*Math.random())}`,r={action:"startContractByYPK",isPrivate:!0,requestID:s,owner:t.publicKey,path:`/${e}`,signature:i.sm2.doSignature(`Fixed|${e}|${t.publicKey}`,t.privateKey)};return this.wssocket.send(JSON.stringify(r)),new Promise(((e,t)=>{this.promiseCallbackPairs[s]={resolve:e,reject:t}}))}initBDServer(e,t,s,r,i){const n=`${(new Date).getTime()}_${Math.floor(1e4*Math.random())}`,a={action:"initBDServer",requestID:n,host:e,username:t,password:s,name:r,sm2Key:JSON.stringify(this.sm2Key),clusterHost:i};return this.wssocket.send(JSON.stringify(a)),new Promise(((e,t)=>{this.promiseCallbackPairs[n]={resolve:e,reject:t}}))}initBDCluster(e,t,s,r,i,n){const a=`${(new Date).getTime()}_${Math.floor(1e4*Math.random())}`,o={action:"initBDCluster",requestID:a,host:e,username:t,password:s,name:r,sm2Key:i,agents:n};return this.wssocket.send(JSON.stringify(o)),new Promise(((e,t)=>{this.promiseCallbackPairs[a]={resolve:e,reject:t}}))}listCompiledFiles(){const e=`${(new Date).getTime()}_${Math.floor(1e4*Math.random())}`,t={action:"listCompiledFiles",requestID:e,isPrivate:!0};return this.wssocket.send(JSON.stringify(t)),new Promise(((t,s)=>{this.promiseCallbackPairs[e]={resolve:t,reject:s}}))}getManagerPubkey(){const e=`${(new Date).getTime()}_${Math.floor(1e4*Math.random())}`,t={action:"getManagerPubkey",requestID:e};return this.wssocket.send(JSON.stringify(t)),new Promise(((t,s)=>{this.promiseCallbackPairs[e]={resolve:t,reject:s}}))}getClusterName(){const e=`${(new Date).getTime()}_${Math.floor(1e4*Math.random())}`,t={action:"getClusterName",requestID:e};return this.wssocket.send(JSON.stringify(t)),new Promise(((t,s)=>{this.promiseCallbackPairs[e]={resolve:t,reject:s}}))}setClusterName(e){const t=`${(new Date).getTime()}_${Math.floor(1e4*Math.random())}`,s={action:"setClusterName",requestID:t,name:e};return this.wssocket.send(JSON.stringify(s)),new Promise(((e,s)=>{this.promiseCallbackPairs[t]={resolve:e,reject:s}}))}},e.WsSocket=u,e.aesEncrypt=o,e.encryptReq=function(e,s){const r={key:t.cryptico.generateAESKey()},i=JSON.stringify(r),{contractID:n}=e,a={...e,contractID:void 0};return{action:s.decrypt(i),contractID:n,arg:o(JSON.stringify(a),r.key),requester:t.cryptico.b256to64(`${s.n.toString(16)},${s.e.toString(16)},0`)}},e.loadRSAKey=function(e){const s=t.cryptico.b64to256(e).split(","),r=new t.RSAKey;return r.setPrivate(s[0],s[0],s[0]),r},e.retryAsyncN=h,e.rsaEncrypt=function(e,s){const r=new t.RSAKey;return r.setPublic(s.n,s.e1),r.encrypt(e)},Object.defineProperty(e,"__esModule",{value:!0}),e}({},cryptico,axiosFetch,axios,smCrypto); //# sourceMappingURL=bdcontract-sdk.iife.js.map