address-auth-sdk
Version:
3 lines (2 loc) • 3.18 kB
JavaScript
!function(e,n){"object"==typeof exports&&"undefined"!=typeof module?n(exports,require("axios"),require("@metamask/sdk")):"function"==typeof define&&define.amd?define(["exports","axios","@metamask/sdk"],n):n((e||self).addressAuthSdk={},e.axios,e.sdk)}(this,function(e,n,t){function r(e){return e&&"object"==typeof e&&"default"in e?e:{default:e}}var o=/*#__PURE__*/r(n);function i(e,n){try{var t=e()}catch(e){return n(e)}return t&&t.then?t.then(void 0,n):t}var s=/*#__PURE__*/function(){function e(e){var n=this,r=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,t){try{var r,o=i(function(){return Promise.resolve(n.ethereum.request({method:"personal_sign",params:[n.stringToHex(e),t[0]]})).then(function(e){return console.log("result = ",e),r=1,e})},function(e){console.log(e)});return Promise.resolve(o&&o.then?o.then(function(e){return r?e:null}):r?o:null)}catch(e){return Promise.reject(e)}},this.getNonce=function(e){try{return Promise.resolve(r.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(i(function(){return Promise.resolve(c.ethereum.request({method:"eth_requestAccounts",params:[]})).then(function(e){return e?(console.log("account = ",e),function(n,t){try{var r=i(function(){return Promise.resolve(c.getNonce(e)).then(function(n){return console.log(n),i(function(){return Promise.resolve(c.signWithNonce(n,e)).then(function(n){return console.log(n),i(function(){return Promise.resolve(c.signIn({publicAddress:e[0],signedMessage:n}))},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 r&&r.then?r.then(t.bind(null,!1),t.bind(null,!0)):t(!1,r)}(0,function(n,t){return Promise.resolve(c.resetNonce(e)).then(function(){if(n)throw t;return t})})):(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=e,this.api=o.default.create({baseURL:"http://localhost:8080/api/",headers:{"Content-Type":"application/json","api-key":"someKey"}}),this.sdk=new t.MetaMaskSDK({forceInjectProvider:!0,dappMetadata:{name:"JavaScript address authenticator helper"}}),this.ethereum=window.ethereum}return e.prototype.stringToHex=function(e){for(var n="",t=0;t<e.length;t++)n+=e.charCodeAt(t).toString(16).padStart(2,"0");return console.log("hex = ",n),n},e}();e.AuthenticationProvider=function(e){this.api=void 0,this.api=new s(e)}});
//# sourceMappingURL=main.umd.js.map