"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
}) : (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
    Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
    o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
    if (mod && mod.__esModule) return mod;
    var result = {};
    if (mod != null) for (var k in mod) if (k !== "default" && Object.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
    __setModuleDefault(result, mod);
    return result;
};
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
    return new (P || (P = Promise))(function (resolve, reject) {
        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
        function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.createAPISig = exports.signUp = exports.createKey = exports.confirmEmail = exports.createEmail = exports.createUsername = void 0;
const axios_1 = __importDefault(require("axios"));
const delay_1 = __importDefault(require("delay"));
const fast_sha256_1 = require("fast-sha256");
const multibase_1 = __importDefault(require("multibase"));
const pb = __importStar(require("@textile/hub-grpc/hub_pb"));
const hub_pb_service_1 = require("@textile/hub-grpc/hub_pb_service");
exports.createUsername = (size = 12) => {
    return Array(size)
        .fill(0)
        .map(() => Math.random().toString(36).charAt(2))
        .join('');
};
exports.createEmail = () => {
    return `${exports.createUsername()}@doe.com`;
};
exports.confirmEmail = (gurl, secret) => __awaiter(void 0, void 0, void 0, function* () {
    yield delay_1.default(500);
    const resp = yield axios_1.default.get(`${gurl}/confirm/${secret}`);
    if (resp.status !== 200) {
        throw new Error(resp.statusText);
    }
    return true;
});
exports.createKey = (ctx, kind) => {
    return new Promise((resolve, reject) => {
        const req = new pb.CreateKeyRequest();
        req.setType(pb.KeyType[kind]);
        const client = new hub_pb_service_1.APIClient(ctx.host, { transport: ctx.transport, debug: ctx.debug });
        client.createKey(req, ctx.toMetadata(), (err, message) => {
            if (err)
                reject(err);
            resolve(message === null || message === void 0 ? void 0 : message.toObject());
        });
    });
};
exports.signUp = (ctx, addrGatewayUrl, sessionSecret) => {
    const username = exports.createUsername();
    const email = exports.createEmail();
    return new Promise((resolve, reject) => {
        const req = new pb.SignupRequest();
        req.setEmail(email);
        req.setUsername(username);
        const client = new hub_pb_service_1.APIClient(ctx.host, { transport: ctx.transport, debug: ctx.debug });
        client.signup(req, ctx.toMetadata(), (err, message) => {
            if (err)
                reject(err);
            resolve({ user: message === null || message === void 0 ? void 0 : message.toObject(), username, email });
        });
        exports.confirmEmail(addrGatewayUrl, sessionSecret).catch((err) => reject(err));
    });
};
exports.createAPISig = (secret, date = new Date(Date.now() + 1000 * 60)) => __awaiter(void 0, void 0, void 0, function* () {
    const sec = multibase_1.default.decode(secret);
    const msg = (date !== null && date !== void 0 ? date : new Date()).toISOString();
    const hash = new fast_sha256_1.HMAC(sec);
    const mac = hash.update(Buffer.from(msg)).digest();
    const sig = multibase_1.default.encode('base32', Buffer.from(mac)).toString();
    return { sig, msg };
});
//# sourceMappingURL=spec.util.js.map