"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const tslib_1 = require("tslib");
const point_1 = tslib_1.__importDefault(require("@dedis/kyber/curve/edwards25519/point"));
const light_1 = require("protobufjs/light");
const protobuf_1 = require("../protobuf");
const identity_darc_1 = tslib_1.__importDefault(require("./identity-darc"));
const identity_did_1 = tslib_1.__importDefault(require("./identity-did"));
const identity_ed25519_1 = tslib_1.__importDefault(require("./identity-ed25519"));
/**
 * Protobuf representation of an identity
 */
class IdentityWrapper extends light_1.Message {
    /**
     * @see README#Message classes
     */
    static register() {
        protobuf_1.registerMessage("Identity", IdentityWrapper, identity_ed25519_1.default, identity_darc_1.default, identity_did_1.default);
    }
    /**
     * fromIdentity returns an IdentityWrapper for a given Identity
     */
    static fromIdentity(id) {
        return this.fromString(id.toString());
    }
    /**
     * fromString returns an IdentityWrapper for a given Identity represented as string
     */
    static fromString(idStr) {
        if (idStr.startsWith("ed25519:")) {
            const point = new point_1.default();
            point.unmarshalBinary(Buffer.from(idStr.slice(8), "hex"));
            const id = identity_ed25519_1.default.fromPoint(point);
            return new IdentityWrapper({ ed25519: id });
        }
        if (idStr.startsWith("darc:")) {
            const id = new identity_darc_1.default({ id: Buffer.from(idStr.slice(5), "hex") });
            return new IdentityWrapper({ darc: id });
        }
        if (idStr.startsWith("did:")) {
            const field = idStr.split(":", 3);
            const id = new identity_did_1.default({ method: Buffer.from(field[1]), did: Buffer.from(field[2]) });
            return new IdentityWrapper({ did: id });
        }
    }
    /**
     * fromEd25519 returns an IdentityWrapper for a given IdentityDarc
     */
    static fromEd25519(id) {
        return new IdentityWrapper({ ed25519: id });
    }
    /**
     * Get the inner identity as bytes
     * @returns the bytes
     */
    toBytes() {
        if (this.ed25519) {
            return this.ed25519.public.marshalBinary();
        }
        if (this.darc) {
            return this.darc.toBytes();
        }
        if (this.did) {
            return this.did.toBytes();
        }
        return Buffer.from([]);
    }
    /**
     * Get the string representation of the identity
     * @returns a string of the identity
     */
    toString() {
        if (this.ed25519) {
            return this.ed25519.toString();
        }
        if (this.darc) {
            return this.darc.toString();
        }
        if (this.did) {
            return this.did.toString();
        }
        return "empty signer";
    }
}
exports.default = IdentityWrapper;
IdentityWrapper.register();
