3 lines
18 KiB
JavaScript
3 lines
18 KiB
JavaScript
|
import{aes as e,cryptico as t,RSAKey as s}from"@daotl/cryptico";import{buildAxiosFetch as i}from"@lifeomic/axios-fetch";import r from"axios";import{sm2 as a}from"sm-crypto";function n(s,i){const r=i.slice(0);e.ExpandKey(r);let a=t.string2bytes(s);a=t.pad16(a),e.Encrypt(a,r);let n=t.bytes2string(a);return n=t.b256to64(n),n}function o(e,t){const i=new s;return i.setPublic(t.n,t.e1),i.encrypt(e)}function c(e){const i=t.b64to256(e).split(","),r=new s;return r.setPrivate(i[0],i[0],i[0]),r}function h(e,s){const i={key:t.generateAESKey()},r=JSON.stringify(i),{contractID:a}=e,o={...e,contractID:void 0};return{action:s.decrypt(r),contractID:a,arg:n(JSON.stringify(o),i.key),requester:t.b256to64(`${s.n.toString(16)},${s.e.toString(16)},0`)}}const u=e=>Object.entries(e).map((([e,t])=>`${e}=${String(t)}`)).join("&");class l{baseUrl;sm2Key;fetch;constructor(e,t,s={}){this.baseUrl=e,this.sm2Key=t;const a=r.create({baseURL:"https://some-domain.com/api/",timeout:1e4,...s});this.fetch=i(a)}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 a.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?${u(t)}`)}startContractByYPK(e){return this.requestWithSignature(`/SCManager?${u({action:"startContractByYPK",...e,owner:this.sm2Key.publicKey,aim:"onStartContract",signature:a.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,withDynamicAnalysis:r,arg:s},a&&(n={...n,pubkey:this.sm2Key.publicKey,signature:this.sign(`${e}|${t}|${s??""}|${this.sm2Key.publicKey}`)}),this.retryRequestWithSignature(3,"/SCManager"+("GET"===i?`?${u(n)}`:""),{method:i,..."POST"===i?{body:n}:{}})}killContractProcess(e,t){const s={action:"killContractProcess",id:e};return t&&(s.requestID=t),this.requestWithSignature(`/SCManager?${u(s)}`)}killAllContract(){return this.requestWithSignature("/SCManager?action=killAllContract")}applyNodeRole(e){const t={action:"applyNodeRole",role:e};return this.requestWithSignature(`/SCManager?${u(t)}`)}authNodeRole(e,t,s){const i={action:"authNodeRole",isAccept:e,authorizedPubKey:t};return this.requestWithSignature(`/SCManager?${u(i)}`,void 0,s)}distributeContract(e,t,s){const i=a.doSignature(`DistributeContract|${t}|${this.sm2Key.publicKey}`,this.sm2Key.privateKey,{hash:!0,der:!0}),r=`/SCManager?${u({action:"distributeContract",nodeIDs:e,projectName:t,isPrivate:s,signature:i})}`,n=`${this.baseUrl+r}${r.includes("?")?"&":"?"}pubKey=${this.sm2Key.publicKey}`,o=a.doSignature(n.substring(n.indexOf("?")+1),this.sm2Key.privateKey,{hash:!0,der:!0});new EventSource(`${this.baseUrl}${r}&pubKey=${this.sm2Key.publicKey}&sign=${o}`).addEventListener("message",(function(e){}),!1)}saveFile(e){return this.requestWithSignature(`/SCManager?${u({action:"saveFile",...e})}`)}listProjectPermission(e){return this.requestWithSignature(`/SCManager?${u({action:"listProjectPermission",...e})}`)}startContractMultiPoint(e,t,s,i,r,a){const n={peersID:e,type:t,selectUnitNum:s,projectName:i,isPrivate:r,sponsorPeerID:a};return this.requestWithSignature(`/SCManager?${u({action:"startContractMultiPoint",...n})}`)}asyn
|
||
|
//# sourceMappingURL=index.mjs.map
|