address-auth-sdk
Version:
3 lines (2 loc) • 2.96 kB
JavaScript
var e=require("axios"),r=require("@metamask/sdk");function n(e){return e&&"object"==typeof e&&"default"in e?e:{default:e}}var t=/*#__PURE__*/n(e);function o(e,r){try{var n=e()}catch(e){return r(e)}return n&&n.then?n.then(void 0,r):n}var i=/*#__PURE__*/function(){function e(e){var n=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=o(function(){return Promise.resolve(n.ethereum.request({method:"personal_sign",params:[n.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(o(function(){return Promise.resolve(c.ethereum.request({method:"eth_requestAccounts",params:[]})).then(function(e){return e?(console.log("account = ",e),function(r,n){try{var t=o(function(){return Promise.resolve(c.getNonce(e)).then(function(r){return console.log(r),o(function(){return Promise.resolve(c.signWithNonce(r,e)).then(function(r){return console.log(r),o(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 n(!0,e)}return t&&t.then?t.then(n.bind(null,!1),n.bind(null,!0)):n(!1,t)}(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=e,this.api=t.default.create({baseURL:"http://localhost:8080/api/",headers:{"Content-Type":"application/json","api-key":"someKey"}}),this.sdk=new r.MetaMaskSDK({forceInjectProvider:!0,dappMetadata:{name:"JavaScript address authenticator helper"}}),this.ethereum=window.ethereum}return e.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},e}();exports.AuthenticationProvider=function(e){this.api=void 0,this.api=new i(e)};
//# sourceMappingURL=main.js.map