"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});constU=require("web3"),d=require("web3-eth-accounts"),f={TYPE_TG:1,TYPE_FP:2};letl=class{constructor({rpc_url:e="https://dev.rpc.aonet.ai",type:s=f.TYPE_FP,server:n="https://dev.rpc.aonet.ai",password:i="",storeage_url:o="",timeout:r=3e3,force_aon:c=!0}={}){if(this.rpc_url=e,this.type=s,this.server=n,this.password=i,this.storeage_url=o,this.timeout=r,this.force_aon=c,this.type==f.TYPE_FP&&this.storeage_url=="")thrownewError("storeage_url must be set when type = TYPE_FP")}staticgetLoginTypes(){returnf}};varw;(function(t){t.NUMBER="NUMBER_NUMBER",t.HEX="NUMBER_HEX",t.STR="NUMBER_STR",t.BIGINT="NUMBER_BIGINT"})(w||(w={}));vary;(function(t){t.HEX="BYTES_HEX",t.UINT8ARRAY="BYTES_UINT8ARRAY"})(y||(y={}));w.BIGINT,y.HEX;w.HEX,y.HEX;varv;(function(t){t.EARLIEST="earliest",t.LATEST="latest",t.PENDING="pending",t.SAFE="safe",t.FINALIZED="finalized"})(v||(v={}));varP;(function(t){t.chainstart="chainstart",t.frontier="frontier",t.homestead="homestead",t.dao="dao",t.tangerineWhistle="tangerineWhistle",t.spuriousDragon="spuriousDragon",t.byzantium="byzantium",t.constantinople="constantinople",t.petersburg="petersburg",t.istanbul="istanbul",t.muirGlacier="muirGlacier",t.berlin="berlin",t.london="london",t.altair="altair",t.arrowGlacier="arrowGlacier",t.grayGlacier="grayGlacier",t.bellatrix="bellatrix",t.merge="merge",t.capella="capella",t.shanghai="shanghai"})(P||(P={}));varE=function(t,e,s,n){functioni(o){returnoinstanceofs?o:news(function(r){r(o)})}returnnew(s||(s=Promise))(function(o,r){functionc(h){try{p(n.next(h))}catch(m){r(m)}}functiona(h){try{p(n.throw(h))}catch(m){r(m)}}functionp(h){h.done?o(h.value):i(h.value).then(c,a)}p((n=n.apply(t,e||[])).next())})};constI=Symbol.for("web3/base-provider");classT{staticisWeb3Provider(e){returneinstanceofT||!!(e&&e[I])}get[I](){return!0}send(e,s){this.request(e).then(n=>{s(null,n)}).catch(n=>{s(n)})}sendAsync(e){returnE(this,void0,void0,function*(){returnthis.request(e)})}asEIP1193Provider(){conste=Object.create(this),s=e.request;returne.request=function(i){returnE(this,void0,void0,function*(){return(yields(i)).result})},e.asEIP1193Provider=void0,e}}classq{constructor(e=newOptions){try{this.options=e}catch(s){throws}}asyncsetItem(e,s,n){try{leti=localStorage.getItem("fingerprint"),o={key:e+"_"+i,value:s},r=localStorage.getItem("token");r=r.replace(/^\"|\"$/g,"");letc={Authorization:"Bearer "+r},a=this.options.storeage_url+"/kvapi/v1/storage/set";consth=await(awaitfetch(a,{method:"POST",headers:c,body:JSON.stringify(o)})).json();h&&h.success?n(null):n(newError(JSON.stringify(h)||""))}catch(i){n(i)}}asyncgetItem(e,s){try{letn=localStorage.getItem("token");n=n.replace(/^\"|\"$/g,"");leti={Authorization:"Bearer "+n},o=localStorage.getItem("fingerprint");e=e+"_"+o;letr=this.options.storeage_url+"/kvapi/v1/storage/get?key="+e;consta=await(awaitfetch(r,{method:"GET",headers:i})).json();a&&a.success?s(null,a.value):s(newError(JSON.stringify(a)||""),null)}catch(n){s(n,null)}}}letg;constD=newUint8Array(16);functionR(){if(!g&&(g=typeofcrypto<"u"&&crypto.getRandomValues&&crypto.getRandomValues.bind(crypto),!g))thrownewError("crypto.getRandomValues() not supported. See https://github.com/uuidjs/uuid#getrandomvalues-not-supported");returng(D)}constu=[];for(lett=0;t<256;++t)u.push((t+256).toString(16).slice(1));functionx(t,e=0){returnu[t[e+0]]+u[t[e+1]]+u[t[e+2]]+u[t[e+3]]+"-"+u[t[e+4]]+u[t[e+5]]+"-"+u[t[e+6]]+u[t[e+7]]+"-"+u[t[e+8]]+u[t[e+9]]+"-"+u[t[e+10]]+u[t[e+11]]+u[t[e+12]]+u[t[e+13]]+u[t[e+14]]+u[t[e+15]]}constY=typeofcrypto<"u"&&crypto.randomUUID&&crypto.randomUUID.bind(crypto),b={randomUUID:Y};functionG(t,e,s){if(b.randomUUID&&!e&&!t)returnb.randomUUID();t=t||{};constn=t.random||(t.rng||R)();returnn[6]=n[6]&15|64,n[8]=n[8]&63|128,x(n)}classBextendsT{constructor(e=newl){try{super(),this.options=e,this.isAON=!0,window.Telegram&&window.Telegram.WebApp&&window.Telegram.WebApp.initData&&(e.type=l.LoginTypes.TYPE_TG),e.type==l.LoginTypes.TYPE_TG?(this.webapp=window.Telegram.WebApp,this.storage=window.Telegram.WebApp.CloudStorage):e.type==l.LoginTypes.TYPE_FP&&(this.storage=newq(e)),this.keystore=null,this.account=null,this._nextId=0,this.provider=newU.Web3.providers.HttpProvider(e.rpc_url)}catch(s){throws}}async_getPassword(){returnawaitnewPromise((s,n)=>{this.storage.getItem("aon_web_sdk_password",(i,o)=>{i&&n(i),s(o)})})}asynccheck(e){try{lets={};if(e.type==l.LoginTypes.TYPE_TG)s={platform:"telegram",initData:this.webapp.initData};elseif(e.type==l.LoginTypes.TYPE_FP){letr=localStorage.getItem("fingerprint");r||(r=awaitnewPromise((c,a)=>{constp=G();c(p)}),localStorage.setItem("fingerprint",r)),s={platform:"fingerprint",visitorId:r}}letn=e.server+"/api/v1/user/check";consto=await(awaitfetch(n,{method:"POST",body:JSON.stringify(s)})).json();if(o.code==0&&o.data){constr=o.data.token;returnr&&this.setAuth(r),o.data}returno}catch(s){throws}}asyncsetPassword(e){awaitnewPromise((s,n)=>{this.storage.setItem("aon_web_sdk_password",e,i=>{i&&n(i),s()})})}generateRandomPassword(e){consts="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789!@#$%^&*()_+{}|:<>?-=[];',./";letn="";for(leti=0;i<e;i++){consto=Math.floor(Math.random()*s.length);n+=s[o]}returnn}asyncgetAuth(){returnthis.options.type==l.LoginTypes.TYPE_TG?awaitnewPromise((s,n)=>{this.storage.getItem("token",(i,o)=>{i&&n(i),s(o)})}):localStorage.getItem("token")}asyncsetAuth(e){this.options.type==l.LoginTypes.TYPE_TG?awaitnewPromise((s,n)=>{this.storage.setItem("token",JSON.stringify(e),i=>{i&&n(i),s()})}):localStorage.setItem("token",e)}asynccreateUserToServer(e,s){letn={keystore:JSON.stringify(e),signature:s},i=awaitthis.getAuth();i=i.replace(/^\"|\"$/g,"");leto={Authorization:"Bearer "+i},r=this.options.server+"/api/v1/user/create";returnawait(awaitfetch(r,{method:"POST",headers:o,body:JSON.stringify(n)})).json()}asynccreateUser(){lete=this.generateRandomPassword(20),s=d.create(),n=awaitd.encrypt(s.privateKey,e),o=d.sign(JSON.stringify(n),s.privateKey).signature,r=awaitthis.createUserToServer(n,o);if(r.code==0)returnawaitthis.setPassword(e),{keystore:n,account:s};thrownewError(r.error||r.msg)}asyncloadKeystore(){try{lete=awaitthis.check(this.options);if(e.code)thrownewError(e.msg||e.error);if(e.isNewUser){letn=awaitthis.createUser();this.account=n.account,this.keystore=n.keystore;return}lets=awaitthis._getPassword();this.keystore=JSON.parse(e.keystore),this.account=awaitd.decrypt(this.keystore,s)}catch(e){console.log("loadKeystore error",e)}}request(e){returnconsole.log("request",e),e.method=="eth_accounts"||e.method=="eth_requestAccounts"?newPromise((s,n)=>{try{if(this.account){s([this.account.address]);return}s([])}catch(i){n(i)}}):e.method=="eth_sendTransaction"?(e.method="eth_signTransaction",newPromise((s,n)=>{leti=e.params&&e.params.length&&e.params[0];i.gasLimit||(i.gasLimit=5e5),this.provider.request({method:"eth_chainId"}).then(o=>{console.log("eth_chainId ",o);letr=100199;o.result&&(r=parseInt(o.result,16)),i.chainId||(i.chainId=r),i.v=27+i.chainId*2+8,this.provider.request({method:"eth_getTransactionCount",params:[this.account.address]}).then(c=>{console.log("eth_getTransactionCount ",c),i.nonce||(i.nonce=c.result),console.log("before signTransaction param",i),d.signTransaction(newd.Transaction(i),this.account.privateKey).then(a=>{if(console.log("signTransaction ",a),a&&a.rawTransaction&&a.rawTransaction.length){constp={method:"eth_sendRawTransaction",params:[a.rawTransaction],id:this._nextId++,jsonrpc:"2.0"};this.provider.request(p).then(h=>{console.log("eth_sendRawTransaction ",h),s(h)}).catch(h=>{console.log("eth_sendRawTransaction error",h),n(h)})}elsen(newError("signTransaction error"))}).catch(a=>{console.log("signTransaction error",a),n(a)})}).catch(c=>{console.log("eth_getTransactionCount error",c),n(c)})}).catch(o=>{console.log("eth_chainId error",o),n(o)})})):this.provider.request(e)}asyncsend(e,s){console.log("send",e,s);constn={method:e,params:s,id:this._nextId++,jsonrpc:"2.0"};returnthis.request(n)}}asyncfunction_(t=newl){console.log("detectEthereumProvider in");lete=!1;returnawaitnewPromise(async(n,i)=>{if(window.ethereum){if(t.force_aon&&!window.ethereum.isAON){letr=awaito(t);n(r)}n(window.ethereum)}else{letr=awaito(t);n(r)}asyncfunctiono(r){if(console.log("createProvider in"),e)return;e=!0;constc=newB(r);returnawaitc.loadKeystore(),window.ethereum=c,console.log("createProvider instance",c),c}})}classA{constructor(e=newl){this.options=e}asynclogin(){return(await_(this.options)).request({method:"eth_accounts"})}}constL=1,S=2,W={1:"/api",2:"/jwt"};classN{constructor(e){if(!(e&&e.auth&&e.auth.length))thrownewError("options.auth is not found");this.auth=e.auth,this.options=e,this.init()}init(){lete="/v1";this.options&&this.options.api_version&&this.options.api_version.length&&(e=this.options.api_version),this.auth_type=this.parseAuthType(this.auth),this.path=W[this.auth_type]+e}parseAuthType(e){consts=this.decodeBase64(e);returnconsole.log("parseAuthType",s),s.indexOf("JWT")>-1?S:L}decodeBase64(e){returntypeofwindow<"u"&&typeofwindow.atob=="function"?window.atob(e):Buffer.from(e,"base64").toString("utf-8")}asyncprediction(e,s){try{letn="https://api.aonet.ai";this.options&&this.options.host&&this.options.host.length&&(n=this.options.host);consti=n+this.path+e;leto={apikey:this.auth};if(this.auth_type==S&&(o={Authorization:"Bearer "+this.auth}),this.options&&this.options.headers)for(constainthis.options.headers)a!="apikey"&&a!="Authorization"&&(o[a]=this.options.headers[a]);constr=awaitfetch(i,{method:"POST",headers:o,body:JSON.stringify(s)});returnr?awaitr.json():{task:{is_success:!1,task_error:"network error",exec_code:"5000"}}}catch(n){console.error("Error:",n)}}}(function(t,e){typeofdefine=="function"&&define.amd?define(["web3","web3-eth-accounts"],e):typeofmodule=="object"&&module.exports?module.exports=e(require("web3"),require("web3-eth-accounts")):t.aonweb=e(t.Web3,t.Web3EthAccounts)})(typeofself<"u"?self:void0,function(t,e){return{Options:l,User:A,AI:N,detectEthereumProvider:_}});exports.AI=N;exports.Options=l;exports.User=A;exports.detectEthereumProvider=_;
varaonweb=function(d,D,g){"use strict";constf={TYPE_TG:1,TYPE_FP:2};letl=class{constructor({rpc_url:e="https://dev.rpc.aonet.ai",type:i=f.TYPE_FP,server:n="https://dev.rpc.aonet.ai",password:s="",storeage_url:o="",timeout:r=3e3,force_aon:c=!0}={}){if(this.rpc_url=e,this.type=i,this.server=n,this.password=s,this.storeage_url=o,this.timeout=r,this.force_aon=c,this.type==f.TYPE_FP&&this.storeage_url=="")thrownewError("storeage_url must be set when type = TYPE_FP")}staticgetLoginTypes(){returnf}};varw;(function(t){t.NUMBER="NUMBER_NUMBER",t.HEX="NUMBER_HEX",t.STR="NUMBER_STR",t.BIGINT="NUMBER_BIGINT"})(w||(w={}));vary;(function(t){t.HEX="BYTES_HEX",t.UINT8ARRAY="BYTES_UINT8ARRAY"})(y||(y={})),w.BIGINT,y.HEX,w.HEX,y.HEX;varP;(function(t){t.EARLIEST="earliest",t.LATEST="latest",t.PENDING="pending",t.SAFE="safe",t.FINALIZED="finalized"})(P||(P={}));varI;(function(t){t.chainstart="chainstart",t.frontier="frontier",t.homestead="homestead",t.dao="dao",t.tangerineWhistle="tangerineWhistle",t.spuriousDragon="spuriousDragon",t.byzantium="byzantium",t.constantinople="constantinople",t.petersburg="petersburg",t.istanbul="istanbul",t.muirGlacier="muirGlacier",t.berlin="berlin",t.london="london",t.altair="altair",t.arrowGlacier="arrowGlacier",t.grayGlacier="grayGlacier",t.bellatrix="bellatrix",t.merge="merge",t.capella="capella",t.shanghai="shanghai"})(I||(I={}));varb=function(t,e,i,n){functions(o){returnoinstanceofi?o:newi(function(r){r(o)})}returnnew(i||(i=Promise))(function(o,r){functionc(u){try{p(n.next(u))}catch(v){r(v)}}functiona(u){try{p(n.throw(u))}catch(v){r(v)}}functionp(u){u.done?o(u.value):s(u.value).then(c,a)}p((n=n.apply(t,e||[])).next())})};constE=Symbol.for("web3/base-provider");classT{staticisWeb3Provider(e){returneinstanceofT||!!(e&&e[E])}get[E](){return!0}send(e,i){this.request(e).then(n=>{i(null,n)}).catch(n=>{i(n)})}sendAsync(e){returnb(this,void0,void0,function*(){returnthis.request(e)})}asEIP1193Provider(){conste=Object.create(this),i=e.request;returne.request=function(s){returnb(this,void0,void0,function*(){return(yieldi(s)).result})},e.asEIP1193Provider=void0,e}}classR{constructor(e=newOptions){try{this.options=e}catch(i){throwi}}asyncsetItem(e,i,n){try{lets=localStorage.getItem("fingerprint"),o={key:e+"_"+s,value:i},r=localStorage.getItem("token");r=r.replace(/^\"|\"$/g,"");letc={Authorization:"Bearer "+r},a=this.options.storeage_url+"/kvapi/v1/storage/set";constu=await(awaitfetch(a,{method:"POST",headers:c,body:JSON.stringify(o)})).json();u&&u.success?n(null):n(newError(JSON.stringify(u)||""))}catch(s){n(s)}}asyncgetItem(e,i){try{letn=localStorage.getItem("token");n=n.replace(/^\"|\"$/g,"");lets={Authorization:"Bearer "+n},o=localStorage.getItem("fingerprint");e=e+"_"+o;letr=this.options.storeage_url+"/kvapi/v1/storage/get?key="+e;consta=await(awaitfetch(r,{method:"GET",headers:s})).json();a&&a.success?i(null,a.value):i(newError(JSON.stringify(a)||""),null)}catch(n){i(n,null)}}}letm;constq=newUint8Array(16);functionY(){if(!m&&(m=typeofcrypto<"u"&&crypto.getRandomValues&&crypto.getRandomValues.bind(crypto),!m))thrownewError("crypto.getRandomValues() not supported. See https://github.com/uuidjs/uuid#getrandomvalues-not-supported");returnm(q)}consth=[];for(lett=0;t<256;++t)h.push((t+256).toString(16).slice(1));functionG(t,e=0){returnh[t[e+0]]+h[t[e+1]]+h[t[e+2]]+h[t[e+3]]+"-"+h[t[e+4]]+h[t[e+5]]+"-"+h[t[e+6]]+h[t[e+7]]+"-"+h[t[e+8]]+h[t[e+9]]+"-"+h[t[e+10]]+h[t[e+11]]+h[t[e+12]]+h[t[e+13]]+h[t[e+14]]+h[t[e+15]]}constS={randomUUID:typeofcrypto<"u"&&crypto.randomUUID&&crypto.randomUUID.bind(crypto)};functionW(t,e,i){if(S.randomUUID&&!e&&!t)returnS.randomUUID();t=t||{};constn=t.random||(t.rng||Y)();returnn[6]=n[6]&15|64,n[8]=n[8]&63|128,G(n)}classxextendsT{constructor(e=newl){try{super(),this.options=e,this.isAON=!0,window.Telegram&&window.Telegram.WebApp&&window.Telegram.WebApp.initData&&(e.type=l.LoginTypes.TYPE_TG),e.type==l.LoginTypes.TYPE_TG?(this.webapp=window.Telegram.WebApp,this.storage=window.Telegram.WebApp.CloudStorage):e.type==l.LoginTypes.TYPE_FP&&(this.storage=newR(e)),this.keystore=null,this.account=null,this._nextId=0,this.provider=newD.Web3.providers.HttpProvider(e.rpc_url)}catch(i){throwi}}async_getPassword(){returnawaitnewPromise((i,n)=>{this.storage.getItem("aon_web_sdk_password",(s,o)=>{s&&n(s),i(o)})})}asynccheck(e){try{leti={};if(e.type==l.LoginTypes.TYPE_TG)i={platform:"telegram",initData:this.webapp.initData};elseif(e.type==l.LoginTypes.TYPE_FP){letr=localStorage.getItem("fingerprint");r||(r=awaitnewPromise((c,a)=>{constp=W();c(p)}),localStorage.setItem("fingerprint",r)),i={platform:"fingerprint",visitorId:r}}letn=e.server+"/api/v1/user/check";consto=await(awaitfetch(n,{method:"POST",body:JSON.stringify(i)})).json();if(o.code==0&&o.data){constr=o.data.token;returnr&&this.setAuth(r),o.data}returno}catch(i){throwi}}asyncsetPassword(e){awaitnewPromise((i,n)=>{this.storage.setItem("aon_web_sdk_password",e,s=>{s&&n(s),i()})})}generateRandomPassword(e){consti="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789!@#$%^&*()_+{}|:<>?-=[];',./";letn="";for(lets=0;s<e;s++){consto=Math.floor(Math.random()*i.length);n+=i[o]}returnn}asyncgetAuth(){returnthis.options.type==l.LoginTypes.TYPE_TG?awaitnewPromise((i,n)=>{this.storage.getItem("token",(s,o)=>{s&&n(s),i(o)})}):localStorage.getItem("token")}asyncsetAuth(e){this.options.type==l.LoginTypes.TYPE_TG?awaitnewPromise((i,n)=>{this.storage.setItem("token",JSON.stringify(e),s=>{s&&n(s),i()})}):localStorage.setItem("token",e)}asynccreateUserToServer(e,i){letn={keystore:JSON.stringify(e),signature:i},s=awaitthis.getAuth();s=s.replace(/^\"|\"$/g,"");leto={Authorization:"Bearer "+s},r=this.options.server+"/api/v1/user/create";returnawait(awaitfetch(r,{method:"POST",headers:o,body:JSON.stringify(n)})).json()}asynccreateUser(){lete=this.generateRandomPassword(20),i=g.create(),n=awaitg.encrypt(i.privateKey,e),o=g.sign(JSON.stringify(n),i.privateKey).signature,r=awaitthis.createUserToServer(n,o);if(r.code==0)returnawaitthis.setPassword(e),{keystore:n,account:i};thrownewError(r.error||r.msg)}asyncloadKeystore(){try{lete=awaitthis.check(this.options);if(e.code)thrownewError(e.msg||e.error);if(e.isNewUser){letn=awaitthis.createUser();this.account=n.account,this.keystore=n.keystore;return}leti=awaitthis._getPassword();this.keystore=JSON.parse(e.keystore),this.account=awaitg.decrypt(this.keystore,i)}catch(e){console.log("loadKeystore error",e)}}request(e){returnconsole.log("request",e),e.method=="eth_accounts"||e.method=="eth_requestAccounts"?newPromise((i,n)=>{try{if(this.account){i([this.account.address]);return}i([])}catch(s){n(s)}}):e.method=="eth_sendTransaction"?(e.method="eth_signTransaction",newPromise((i,n)=>{lets=e.params&&e.params.length&&e.params[0];s.gasLimit||(s.gasLimit=5e5),this.provider.request({method:"eth_chainId"}).then(o=>{console.log("eth_chainId ",o);letr=100199;o.result&&(r=parseInt(o.result,16)),s.chainId||(s.chainId=r),s.v=27+s.chainId*2+8,this.provider.request({method:"eth_getTransactionCount",params:[this.account.address]}).then(c=>{console.log("eth_getTransactionCount ",c),s.nonce||(s.nonce=c.result),console.log("before signTransaction param",s),g.signTransaction(newg.Transaction(s),this.account.privateKey).then(a=>{if(console.log("signTransaction ",a),a&&a.rawTransaction&&a.rawTransaction.length){constp={method:"eth_sendRawTransaction",params:[a.rawTransaction],id:this._nextId++,jsonrpc:"2.0"};this.provider.request(p).then(u=>{console.log("eth_sendRawTransaction ",u),i(u)}).catch(u=>{console.log("eth_sendRawTransaction error",u),n(u)})}elsen(newError("signTransaction error"))}).catch(a=>{console.log("signTransaction error",a),n(a)})}).catch(c=>{console.log("eth_getTransactionCount error",c),n(c)})}).catch(o=>{console.log("eth_chainId error",o),n(o)})})):this.provider.request(e)}asyncsend(e,i){console.log("send",e,i);constn={method:e,params:i,id:this._nextId++,jsonrpc:"2.0"};returnthis.request(n)}}asyncfunction_(t=newl){console.log("detectEthereumProvider in");lete=!1;returnawaitnewPromise(async(n,s)=>{if(window.ethereum){if(t.force_aon&&!window.ethereum.isAON){letr=awaito(t);n(r)}n(window.ethereum)}else{letr=awaito(t);n(r)}asyncfunctiono(r){if(console.log("createProvider in"),e)return;e=!0;constc=newx(r);returnawaitc.loadKeystore(),window.ethereum=c,console.log("createProvider instance",c),c}})}classA{constructor(e=newl){this.options=e}asynclogin(){return(await_(this.options)).request({method:"eth_accounts"})}}constB=1,U=2,L={1:"/api",2:"/jwt"};classN{constructor(e){if(!(e&&e.auth&&e.auth.length))thrownewError("options.auth is not found");this.auth=e.auth,this.options=e,this.init()}init(){lete="/v1";this.options&&this.options.api_version&&this.options.api_version.length&&(e=this.options.api_version),this.auth_type=this.parseAuthType(this.auth),this.path=L[this.auth_type]+e}parseAuthType(e){consti=this.decodeBase64(e);returnconsole.log("parseAuthType",i),i.indexOf("JWT")>-1?U:B}decodeBase64(e){returntypeofwindow<"u"&&typeofwindow.atob=="function"?window.atob(e):Buffer.from(e,"base64").toString("utf-8")}asyncprediction(e,i){try{letn="https://api.aonet.ai";this.options&&this.options.host&&this.options.host.length&&(n=this.options.host);consts=n+this.path+e;leto={apikey:this.auth};if(this.auth_type==U&&(o={Authorization:"Bearer "+this.auth}),this.options&&this.options.headers)for(constainthis.options.headers)a!="apikey"&&a!="Authorization"&&(o[a]=this.options.headers[a]);constr=awaitfetch(s,{method:"POST",headers:o,body:JSON.stringify(i)});returnr?awaitr.json():{task:{is_success:!1,task_error:"network error",exec_code:"5000"}}}catch(n){console.error("Error:",n)}}}returnfunction(t,e){typeofdefine=="function"&&define.amd?define(["web3","web3-eth-accounts"],e):typeofmodule=="object"&&module.exports?module.exports=e(require("web3"),require("web3-eth-accounts")):t.aonweb=e(t.Web3,t.Web3EthAccounts)}(typeofself<"u"?self:void0,function(t,e){return{Options:l,User:A,AI:N,detectEthereumProvider:_}}),d.AI=N,d.Options=l,d.User=A,d.detectEthereumProvider=_,Object.defineProperty(d,Symbol.toStringTag,{value:"Module"}),d}({},Web3,Web3EthAccounts);
(function(p,w){typeofexports=="object"&&typeofmodule<"u"?w(exports,require("web3"),require("web3-eth-accounts")):typeofdefine=="function"&&define.amd?define(["exports","web3","web3-eth-accounts"],w):(p=typeofglobalThis<"u"?globalThis:p||self,w(p.aonweb={},p.Web3,p.Web3EthAccounts))})(this,function(p,w,g){"use strict";constT={TYPE_TG:1,TYPE_FP:2};letl=class{constructor({rpc_url:e="https://dev.rpc.aonet.ai",type:i=T.TYPE_FP,server:n="https://dev.rpc.aonet.ai",password:s="",storeage_url:o="",timeout:r=3e3,force_aon:c=!0}={}){if(this.rpc_url=e,this.type=i,this.server=n,this.password=s,this.storeage_url=o,this.timeout=r,this.force_aon=c,this.type==T.TYPE_FP&&this.storeage_url=="")thrownewError("storeage_url must be set when type = TYPE_FP")}staticgetLoginTypes(){returnT}};vary;(function(t){t.NUMBER="NUMBER_NUMBER",t.HEX="NUMBER_HEX",t.STR="NUMBER_STR",t.BIGINT="NUMBER_BIGINT"})(y||(y={}));varf;(function(t){t.HEX="BYTES_HEX",t.UINT8ARRAY="BYTES_UINT8ARRAY"})(f||(f={})),y.BIGINT,f.HEX,y.HEX,f.HEX;varI;(function(t){t.EARLIEST="earliest",t.LATEST="latest",t.PENDING="pending",t.SAFE="safe",t.FINALIZED="finalized"})(I||(I={}));varb;(function(t){t.chainstart="chainstart",t.frontier="frontier",t.homestead="homestead",t.dao="dao",t.tangerineWhistle="tangerineWhistle",t.spuriousDragon="spuriousDragon",t.byzantium="byzantium",t.constantinople="constantinople",t.petersburg="petersburg",t.istanbul="istanbul",t.muirGlacier="muirGlacier",t.berlin="berlin",t.london="london",t.altair="altair",t.arrowGlacier="arrowGlacier",t.grayGlacier="grayGlacier",t.bellatrix="bellatrix",t.merge="merge",t.capella="capella",t.shanghai="shanghai"})(b||(b={}));varE=function(t,e,i,n){functions(o){returnoinstanceofi?o:newi(function(r){r(o)})}returnnew(i||(i=Promise))(function(o,r){functionc(u){try{d(n.next(u))}catch(P){r(P)}}functiona(u){try{d(n.throw(u))}catch(P){r(P)}}functiond(u){u.done?o(u.value):s(u.value).then(c,a)}d((n=n.apply(t,e||[])).next())})};constS=Symbol.for("web3/base-provider");class_{staticisWeb3Provider(e){returneinstanceof_||!!(e&&e[S])}get[S](){return!0}send(e,i){this.request(e).then(n=>{i(null,n)}).catch(n=>{i(n)})}sendAsync(e){returnE(this,void0,void0,function*(){returnthis.request(e)})}asEIP1193Provider(){conste=Object.create(this),i=e.request;returne.request=function(s){returnE(this,void0,void0,function*(){return(yieldi(s)).result})},e.asEIP1193Provider=void0,e}}classq{constructor(e=newOptions){try{this.options=e}catch(i){throwi}}asyncsetItem(e,i,n){try{lets=localStorage.getItem("fingerprint"),o={key:e+"_"+s,value:i},r=localStorage.getItem("token");r=r.replace(/^\"|\"$/g,"");letc={Authorization:"Bearer "+r},a=this.options.storeage_url+"/kvapi/v1/storage/set";constu=await(awaitfetch(a,{method:"POST",headers:c,body:JSON.stringify(o)})).json();u&&u.success?n(null):n(newError(JSON.stringify(u)||""))}catch(s){n(s)}}asyncgetItem(e,i){try{letn=localStorage.getItem("token");n=n.replace(/^\"|\"$/g,"");lets={Authorization:"Bearer "+n},o=localStorage.getItem("fingerprint");e=e+"_"+o;letr=this.options.storeage_url+"/kvapi/v1/storage/get?key="+e;consta=await(awaitfetch(r,{method:"GET",headers:s})).json();a&&a.success?i(null,a.value):i(newError(JSON.stringify(a)||""),null)}catch(n){i(n,null)}}}letm;constR=newUint8Array(16);functionx(){if(!m&&(m=typeofcrypto<"u"&&crypto.getRandomValues&&crypto.getRandomValues.bind(crypto),!m))thrownewError("crypto.getRandomValues() not supported. See https://github.com/uuidjs/uuid#getrandomvalues-not-supported");returnm(R)}consth=[];for(lett=0;t<256;++t)h.push((t+256).toString(16).slice(1));functionY(t,e=0){returnh[t[e+0]]+h[t[e+1]]+h[t[e+2]]+h[t[e+3]]+"-"+h[t[e+4]]+h[t[e+5]]+"-"+h[t[e+6]]+h[t[e+7]]+"-"+h[t[e+8]]+h[t[e+9]]+"-"+h[t[e+10]]+h[t[e+11]]+h[t[e+12]]+h[t[e+13]]+h[t[e+14]]+h[t[e+15]]}constA={randomUUID:typeofcrypto<"u"&&crypto.randomUUID&&crypto.randomUUID.bind(crypto)};functionG(t,e,i){if(A.randomUUID&&!e&&!t)returnA.randomUUID();t=t||{};constn=t.random||(t.rng||x)();returnn[6]=n[6]&15|64,n[8]=n[8]&63|128,Y(n)}classWextends_{constructor(e=newl){try{super(),this.options=e,this.isAON=!0,window.Telegram&&window.Telegram.WebApp&&window.Telegram.WebApp.initData&&(e.type=l.LoginTypes.TYPE_TG),e.type==l.LoginTypes.TYPE_TG?(this.webapp=window.Telegram.WebApp,this.storage=window.Telegram.WebApp.CloudStorage):e.type==l.LoginTypes.TYPE_FP&&(this.storage=newq(e)),this.keystore=null,this.account=null,this._nextId=0,this.provider=neww.Web3.providers.HttpProvider(e.rpc_url)}catch(i){throwi}}async_getPassword(){returnawaitnewPromise((i,n)=>{this.storage.getItem("aon_web_sdk_password",(s,o)=>{s&&n(s),i(o)})})}asynccheck(e){try{leti={};if(e.type==l.LoginTypes.TYPE_TG)i={platform:"telegram",initData:this.webapp.initData};elseif(e.type==l.LoginTypes.TYPE_FP){letr=localStorage.getItem("fingerprint");r||(r=awaitnewPromise((c,a)=>{constd=G();c(d)}),localStorage.setItem("fingerprint",r)),i={platform:"fingerprint",visitorId:r}}letn=e.server+"/api/v1/user/check";consto=await(awaitfetch(n,{method:"POST",body:JSON.stringify(i)})).json();if(o.code==0&&o.data){constr=o.data.token;returnr&&this.setAuth(r),o.data}returno}catch(i){throwi}}asyncsetPassword(e){awaitnewPromise((i,n)=>{this.storage.setItem("aon_web_sdk_password",e,s=>{s&&n(s),i()})})}generateRandomPassword(e){consti="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789!@#$%^&*()_+{}|:<>?-=[];',./";letn="";for(lets=0;s<e;s++){consto=Math.floor(Math.random()*i.length);n+=i[o]}returnn}asyncgetAuth(){returnthis.options.type==l.LoginTypes.TYPE_TG?awaitnewPromise((i,n)=>{this.storage.getItem("token",(s,o)=>{s&&n(s),i(o)})}):localStorage.getItem("token")}asyncsetAuth(e){this.options.type==l.LoginTypes.TYPE_TG?awaitnewPromise((i,n)=>{this.storage.setItem("token",JSON.stringify(e),s=>{s&&n(s),i()})}):localStorage.setItem("token",e)}asynccreateUserToServer(e,i){letn={keystore:JSON.stringify(e),signature:i},s=awaitthis.getAuth();s=s.replace(/^\"|\"$/g,"");leto={Authorization:"Bearer "+s},r=this.options.server+"/api/v1/user/create";returnawait(awaitfetch(r,{method:"POST",headers:o,body:JSON.stringify(n)})).json()}asynccreateUser(){lete=this.generateRandomPassword(20),i=g.create(),n=awaitg.encrypt(i.privateKey,e),o=g.sign(JSON.stringify(n),i.privateKey).signature,r=awaitthis.createUserToServer(n,o);if(r.code==0)returnawaitthis.setPassword(e),{keystore:n,account:i};thrownewError(r.error||r.msg)}asyncloadKeystore(){try{lete=awaitthis.check(this.options);if(e.code)thrownewError(e.msg||e.error);if(e.isNewUser){letn=awaitthis.createUser();this.account=n.account,this.keystore=n.keystore;return}leti=awaitthis._getPassword();this.keystore=JSON.parse(e.keystore),this.account=awaitg.decrypt(this.keystore,i)}catch(e){console.log("loadKeystore error",e)}}request(e){returnconsole.log("request",e),e.method=="eth_accounts"||e.method=="eth_requestAccounts"?newPromise((i,n)=>{try{if(this.account){i([this.account.address]);return}i([])}catch(s){n(s)}}):e.method=="eth_sendTransaction"?(e.method="eth_signTransaction",newPromise((i,n)=>{lets=e.params&&e.params.length&&e.params[0];s.gasLimit||(s.gasLimit=5e5),this.provider.request({method:"eth_chainId"}).then(o=>{console.log("eth_chainId ",o);letr=100199;o.result&&(r=parseInt(o.result,16)),s.chainId||(s.chainId=r),s.v=27+s.chainId*2+8,this.provider.request({method:"eth_getTransactionCount",params:[this.account.address]}).then(c=>{console.log("eth_getTransactionCount ",c),s.nonce||(s.nonce=c.result),console.log("before signTransaction param",s),g.signTransaction(newg.Transaction(s),this.account.privateKey).then(a=>{if(console.log("signTransaction ",a),a&&a.rawTransaction&&a.rawTransaction.length){constd={method:"eth_sendRawTransaction",params:[a.rawTransaction],id:this._nextId++,jsonrpc:"2.0"};this.provider.request(d).then(u=>{console.log("eth_sendRawTransaction ",u),i(u)}).catch(u=>{console.log("eth_sendRawTransaction error",u),n(u)})}elsen(newError("signTransaction error"))}).catch(a=>{console.log("signTransaction error",a),n(a)})}).catch(c=>{console.log("eth_getTransactionCount error",c),n(c)})}).catch(o=>{console.log("eth_chainId error",o),n(o)})})):this.provider.request(e)}asyncsend(e,i){console.log("send",e,i);constn={method:e,params:i,id:this._nextId++,jsonrpc:"2.0"};returnthis.request(n)}}asyncfunctionv(t=newl){console.log("detectEthereumProvider in");lete=!1;returnawaitnewPromise(async(n,s)=>{if(window.ethereum){if(t.force_aon&&!window.ethereum.isAON){letr=awaito(t);n(r)}n(window.ethereum)}else{letr=awaito(t);n(r)}asyncfunctiono(r){if(console.log("createProvider in"),e)return;e=!0;constc=newW(r);returnawaitc.loadKeystore(),window.ethereum=c,console.log("createProvider instance",c),c}})}classU{constructor(e=newl){this.options=e}asynclogin(){return(awaitv(this.options)).request({method:"eth_accounts"})}}constB=1,N=2,L={1:"/api",2:"/jwt"};classD{constructor(e){if(!(e&&e.auth&&e.auth.length))thrownewError("options.auth is not found");this.auth=e.auth,this.options=e,this.init()}init(){lete="/v1";this.options&&this.options.api_version&&this.options.api_version.length&&(e=this.options.api_version),this.auth_type=this.parseAuthType(this.auth),this.path=L[this.auth_type]+e}parseAuthType(e){consti=this.decodeBase64(e);returnconsole.log("parseAuthType",i),i.indexOf("JWT")>-1?N:B}decodeBase64(e){returntypeofwindow<"u"&&typeofwindow.atob=="function"?window.atob(e):Buffer.from(e,"base64").toString("utf-8")}asyncprediction(e,i){try{letn="https://api.aonet.ai";this.options&&this.options.host&&this.options.host.length&&(n=this.options.host);consts=n+this.path+e;leto={apikey:this.auth};if(this.auth_type==N&&(o={Authorization:"Bearer "+this.auth}),this.options&&this.options.headers)for(constainthis.options.headers)a!="apikey"&&a!="Authorization"&&(o[a]=this.options.headers[a]);constr=awaitfetch(s,{method:"POST",headers:o,body:JSON.stringify(i)});returnr?awaitr.json():{task:{is_success:!1,task_error:"network error",exec_code:"5000"}}}catch(n){console.error("Error:",n)}}}(function(t,e){typeofdefine=="function"&&define.amd?define(["web3","web3-eth-accounts"],e):typeofmodule=="object"&&module.exports?module.exports=e(require("web3"),require("web3-eth-accounts")):t.aonweb=e(t.Web3,t.Web3EthAccounts)})(typeofself<"u"?self:void0,function(t,e){return{Options:l,User:U,AI:D,detectEthereumProvider:v}}),p.AI=D,p.Options=l,p.User=U,p.detectEthereumProvider=v,Object.defineProperty(p,Symbol.toStringTag,{value:"Module"})});