"use strict";var e=this&&this.__awaiter||function(e,r,t,n){return new(t||(t=Promise))((function(i,o){function s(e){try{a(n.next(e))}catch(e){o(e)}}function u(e){try{a(n.throw(e))}catch(e){o(e)}}function a(e){var r;e.done?i(e.value):(r=e.value,r instanceof t?r:new t((function(e){e(r)}))).then(s,u)}a((n=n.apply(e,r||[])).next())}))},r=this&&this.__importDefault||function(e){return e&&e.__esModule?e:{default:e}};Object.defineProperty(exports,"__esModule",{value:!0}),exports.loginRequest=void 0;const t=require("uuid"),n=r(require("node-fetch")),i=require("./proxy"),o=require("./registry"),s=require("../../config"),u=r(require("fs-extra")),a=require("../../util"),l=require("../../util/FsUtil"),c=r(require("../../log")),h=r(require("crypto")),f=require("../../tools/readline"),y=require("./types"),p=require("./handleResponseError"),d=require("../../common/message");function g(r,t){return e(this,void 0,void 0,(function*(){try{const e=yield(0,f.questionHideable)("what is your passphrase of the private key: ");return e===f.manualExit&&(c.default.info("","Interrupted manually."),process.exit(1)),r.sign({key:t,passphrase:e},"base64")}catch(e){return c.default.error("",`The content of private key in the ${s.types.KEY_PATH} error.`),y.signError}}))}exports.loginRequest=function(r){return e(this,void 0,void 0,(function*(){const f=function(){const e=s.config.get(s.types.KEY_PATH);if(null==e||!e.length)throw new Error(d.Messages.Publish.KeyPathIsEmpty);if(!u.default.existsSync(e))throw new Error((0,d.format)(d.Messages.Publish.PrivateKeyFileNotExist,{keyPath:e}));if(a.FsBlockingUtil.statSync(e).isDirectory())throw new Error(d.Messages.Publish.KeyPathIsDirError);return e}(),v=function(){const e=s.config.get(s.types.PUBLISH_ID);if(null==e||!e.length)throw new Error(d.Messages.Publish.PublishIdIsEmpty);return e}(),P=Date.now(),E=(0,t.v4)().replace(/-/g,""),w=yield function(r,t,n,i){return e(this,void 0,void 0,(function*(){const e=yield l.FsUtil.readFile(r,"utf-8");if(null==e||!e.length)throw new Error((0,d.format)(d.Messages.Publish.PrivateKeyContentIsEmpty,{keyPath:r}));const o=h.default.createSign("RSA-SHA256");if(o.update(`v1-${t}-${n}-${i}`),-1===e.indexOf("ENCRYPTED"))throw c.default.error("","Private key without passphrase is not supported."),c.default.error("","You must config a encrypted private key using a non-empty passphrase."),new Error(d.Messages.Publish.NotSupportPrivateKey);try{let r=y.signError;for(;r===y.signError;)r=yield g(o,e);return r}catch(e){throw new Error(d.Messages.Publish.SignatureUnknownError)}}))}(f,v,P,E),m={};m.publishId=v,m.timestamp=P,m.nonce=E,m.signature=w,m.version="v1";const q=yield(0,n.default)(r.concat("login"),{headers:{command:"login",version:"v1","user-agent":(0,o.getUserAgent)(),authorization:"","Content-Type":"application/json; charset=UTF-8"},body:JSON.stringify(m),method:"POST",agent:(0,i.getProxyAgent)(r)});return q.ok||(yield(0,p.handleResponseError)(q,"Login")),q.json()}))};