"use strict";
// Copyright 2023 IOTA Stiftung
// SPDX-License-Identifier: Apache-2.0
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
    var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
    if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
    else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
    return c > 3 && r && Object.defineProperty(target, key, r), r;
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.RestrictedAddress = exports.MultiAddress = exports.WeightedAddress = exports.ImplicitAccountCreationAddress = exports.AnchorAddress = exports.NftAddress = exports.AccountAddress = exports.Ed25519Address = exports.AddressType = exports.Address = exports.AddressDiscriminator = void 0;
const class_transformer_1 = require("class-transformer");
/**
 * Address type variants.
 */
var AddressType;
(function (AddressType) {
    /** An Ed25519 address. */
    AddressType[AddressType["Ed25519"] = 0] = "Ed25519";
    /** An Account address. */
    AddressType[AddressType["Account"] = 8] = "Account";
    /** An NFT address. */
    AddressType[AddressType["Nft"] = 16] = "Nft";
    /** An Anchor address. */
    AddressType[AddressType["Anchor"] = 24] = "Anchor";
    /** An implicit account creation address. */
    AddressType[AddressType["ImplicitAccountCreation"] = 32] = "ImplicitAccountCreation";
    /** A Multi address. */
    AddressType[AddressType["Multi"] = 40] = "Multi";
    /** An address with restricted capabilities. */
    AddressType[AddressType["Restricted"] = 48] = "Restricted";
})(AddressType || (AddressType = {}));
exports.AddressType = AddressType;
/**
 * The base class for addresses.
 */
class Address {
    /**
     * @param type The type of the address.
     */
    constructor(type) {
        this.type = type;
    }
    /**
     * Parse an address from a JSON string.
     */
    static parse(data) {
        if (data.type == AddressType.Ed25519) {
            return (0, class_transformer_1.plainToInstance)(Ed25519Address, data);
        }
        else if (data.type == AddressType.Account) {
            return (0, class_transformer_1.plainToInstance)(AccountAddress, data);
        }
        else if (data.type == AddressType.Nft) {
            return (0, class_transformer_1.plainToInstance)(NftAddress, data);
        }
        else if (data.type == AddressType.Anchor) {
            return (0, class_transformer_1.plainToInstance)(AnchorAddress, data);
        }
        else if (data.type == AddressType.ImplicitAccountCreation) {
            return (0, class_transformer_1.plainToInstance)(ImplicitAccountCreationAddress, data);
        }
        else if (data.type == AddressType.Restricted) {
            return (0, class_transformer_1.plainToInstance)(RestrictedAddress, data);
        }
        throw new Error('Invalid JSON');
    }
}
exports.Address = Address;
/**
 * An Ed25519 Address.
 */
class Ed25519Address extends Address {
    /**
     * @param pubKeyHash BLAKE2b-256 hash of an Ed25519 public key as hex-encoded string.
     */
    constructor(pubKeyHash) {
        super(AddressType.Ed25519);
        this.pubKeyHash = pubKeyHash;
    }
    toString() {
        return this.pubKeyHash;
    }
}
exports.Ed25519Address = Ed25519Address;
/**
 * An Account address.
 */
class AccountAddress extends Address {
    /**
     * @param address An account address as account ID.
     */
    constructor(address) {
        super(AddressType.Account);
        this.accountId = address;
    }
    toString() {
        return this.accountId;
    }
}
exports.AccountAddress = AccountAddress;
/**
 * An NFT address.
 */
class NftAddress extends Address {
    /**
     * @param address An NFT address as NFT ID.
     */
    constructor(address) {
        super(AddressType.Nft);
        this.nftId = address;
    }
    toString() {
        return this.nftId;
    }
}
exports.NftAddress = NftAddress;
/**
 * An Anchor address.
 */
class AnchorAddress extends Address {
    /**
     * @param address An anchor address as anchor ID.
     */
    constructor(address) {
        super(AddressType.Anchor);
        this.anchorId = address;
    }
    toString() {
        return this.anchorId;
    }
}
exports.AnchorAddress = AnchorAddress;
/**
 * An implicit account creation address that can be used to convert a Basic Output to an Account Output.
 */
class ImplicitAccountCreationAddress extends Address {
    /**
     * @param pubKeyHash BLAKE2b-256 hash of an Ed25519 public key as hex-encoded string.
     */
    constructor(pubKeyHash) {
        super(AddressType.ImplicitAccountCreation);
        this.pubKeyHash = pubKeyHash;
    }
    address() {
        return new Ed25519Address(this.pubKeyHash);
    }
    toString() {
        return this.address.toString();
    }
}
exports.ImplicitAccountCreationAddress = ImplicitAccountCreationAddress;
const RestrictedAddressDiscriminator = {
    property: 'type',
    subTypes: [
        { value: Ed25519Address, name: AddressType.Ed25519 },
        { value: AccountAddress, name: AddressType.Account },
        { value: NftAddress, name: AddressType.Nft },
    ],
};
/**
 * An address with restricted capabilities.
 */
class RestrictedAddress extends Address {
    /**
     * @param address An address.
     */
    constructor(address) {
        super(AddressType.Restricted);
        this.address = address;
    }
    setAllowedCapabilities(allowedCapabilities) {
        if (allowedCapabilities.some((c) => c != 0)) {
            this.allowedCapabilities =
                '0x' +
                    Buffer.from(allowedCapabilities.buffer, allowedCapabilities.byteOffset, allowedCapabilities.byteLength).toString('hex');
        }
        else {
            this.allowedCapabilities = undefined;
        }
    }
    withAllowedCapabilities(allowedCapabilities) {
        this.setAllowedCapabilities(allowedCapabilities);
        return this;
    }
    getAllowedCapabilities() {
        return this.allowedCapabilities !== undefined
            ? Uint8Array.from(Buffer.from(this.allowedCapabilities.substring(2), 'hex'))
            : new Uint8Array();
    }
    toString() {
        return (this.address.toString() +
            (this.allowedCapabilities !== undefined
                ? this.allowedCapabilities.substring(2)
                : ''));
    }
}
__decorate([
    (0, class_transformer_1.Type)(() => Address, {
        discriminator: RestrictedAddressDiscriminator,
    })
], RestrictedAddress.prototype, "address", void 0);
exports.RestrictedAddress = RestrictedAddress;
/**
 * A weighted address.
 */
class WeightedAddress {
    /**
     * @param address The unlocked address.
     * @param weight The weight of the unlocked address.
     */
    constructor(address, weight) {
        this.address = address;
        this.weight = weight;
    }
}
__decorate([
    (0, class_transformer_1.Type)(() => Address, {
        discriminator: {
            property: 'type',
            subTypes: [
                { value: Ed25519Address, name: AddressType.Ed25519 },
                { value: AccountAddress, name: AddressType.Account },
                { value: NftAddress, name: AddressType.Nft },
                { value: AnchorAddress, name: AddressType.Anchor },
            ],
        },
    })
], WeightedAddress.prototype, "address", void 0);
exports.WeightedAddress = WeightedAddress;
/**
 * An address that consists of addresses with weights and a threshold value.
 * The Multi Address can be unlocked if the cumulative weight of all unlocked addresses is equal to or exceeds the
 * threshold.
 */
class MultiAddress extends Address {
    /**
     * @param addresses The weighted unlocked addresses.
     * @param threshold The threshold that needs to be reached by the unlocked addresses in order to unlock the multi address.
     */
    constructor(addresses, threshold) {
        super(AddressType.Multi);
        this.addresses = addresses;
        this.threshold = threshold;
    }
    toString() {
        return JSON.stringify(this);
    }
}
exports.MultiAddress = MultiAddress;
const AddressDiscriminator = {
    property: 'type',
    subTypes: [
        { value: Ed25519Address, name: AddressType.Ed25519 },
        { value: AccountAddress, name: AddressType.Account },
        { value: NftAddress, name: AddressType.Nft },
        { value: AnchorAddress, name: AddressType.Anchor },
        {
            value: ImplicitAccountCreationAddress,
            name: AddressType.ImplicitAccountCreation,
        },
        { value: MultiAddress, name: AddressType.Multi },
        { value: RestrictedAddress, name: AddressType.Restricted },
    ],
};
exports.AddressDiscriminator = AddressDiscriminator;
//# sourceMappingURL=address.js.map