UNPKG

address-auth-sdk

Version:

3 lines (2 loc) 2.88 kB
import e from"axios";import{MetaMaskSDK as r}from"@metamask/sdk";function n(e,r){try{var n=e()}catch(e){return r(e)}return n&&n.then?n.then(void 0,r):n}var t=/*#__PURE__*/function(){function t(t){var o=this,i=this,s=this,u=this,c=this;this.provider=void 0,this.api=void 0,this.sdk=void 0,this.ethereum=void 0,this.signWithNonce=function(e,r){try{var t,i=n(function(){return Promise.resolve(o.ethereum.request({method:"personal_sign",params:[o.stringToHex(e),r[0]]})).then(function(e){return console.log("result = ",e),t=1,e})},function(e){console.log(e)});return Promise.resolve(i&&i.then?i.then(function(e){return t?e:null}):t?i:null)}catch(e){return Promise.reject(e)}},this.getNonce=function(e){try{return Promise.resolve(i.api.get("/message/"+e)).then(function(e){return console.log("nonce = ",e.data),e.data})}catch(e){return Promise.reject(e)}},this.signIn=function(e){try{return console.log("information = ",e),Promise.resolve(s.api.post("/signin",e)).then(function(e){return console.log("signIn = ",e.data),e.data})}catch(e){return Promise.reject(e)}},this.resetNonce=function(e){try{return Promise.resolve(u.api.get("/reset/"+e)).then(function(){})}catch(e){return Promise.reject(e)}},this.authenticate=function(){try{return Promise.resolve(n(function(){return Promise.resolve(c.ethereum.request({method:"eth_requestAccounts",params:[]})).then(function(e){return e?(console.log("account = ",e),function(r,t){try{var o=n(function(){return Promise.resolve(c.getNonce(e)).then(function(r){return console.log(r),n(function(){return Promise.resolve(c.signWithNonce(r,e)).then(function(r){return console.log(r),n(function(){return Promise.resolve(c.signIn({publicAddress:e[0],signedMessage:r}))},function(e){return c.provider.handleError("Error during sign-in",e),null})})},function(e){return c.provider.handleError("Error during signing with nonce",e),null})})},function(e){return c.provider.handleError("Error fetching nonce",e),null})}catch(e){return t(!0,e)}return o&&o.then?o.then(t.bind(null,!1),t.bind(null,!0)):t(!1,o)}(0,function(r,n){return Promise.resolve(c.resetNonce(e)).then(function(){if(r)throw n;return n})})):(c.provider.handleError("No accounts found"),null)})},function(e){return c.provider.handleError("Error requesting Ethereum accounts",e),null}))}catch(e){return Promise.reject(e)}},this.provider=t,this.api=e.create({baseURL:"http://localhost:8080/api/",headers:{"Content-Type":"application/json","api-key":"someKey"}}),this.sdk=new r({forceInjectProvider:!0,dappMetadata:{name:"JavaScript address authenticator helper"}}),this.ethereum=window.ethereum}return t.prototype.stringToHex=function(e){for(var r="",n=0;n<e.length;n++)r+=e.charCodeAt(n).toString(16).padStart(2,"0");return console.log("hex = ",r),r},t}(),o=function(e){this.api=void 0,this.api=new t(e)};export{o as AuthenticationProvider}; //# sourceMappingURL=main.modern.module.js.map