"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.RawAddress = exports.SvmAddress = exports.EvmAddress = exports.Address = exports.toAddressType = exports.isValidEvmAddress = exports.toEvmAddress = exports.toBytes32 = exports.compareAddressesSimple = exports.compareAddresses = exports.isContractDeployedToAddress = void 0;
var tslib_1 = require("tslib");
var ethers_1 = require("ethers");
var bs58_1 = tslib_1.__importDefault(require("bs58"));
var _1 = require("./");
function isContractDeployedToAddress(address, provider) {
    return tslib_1.__awaiter(this, void 0, void 0, function () {
        var code;
        return tslib_1.__generator(this, function (_a) {
            switch (_a.label) {
                case 0:
                    if (!address || !ethers_1.utils.isAddress(address)) {
                        return [2, false];
                    }
                    return [4, provider.getCode(address)];
                case 1:
                    code = _a.sent();
                    return [2, code !== "0x"];
            }
        });
    });
}
exports.isContractDeployedToAddress = isContractDeployedToAddress;
function compareAddresses(addressA, addressB) {
    var bnAddressA = _1.BigNumber.from(addressA);
    var bnAddressB = _1.BigNumber.from(addressB);
    if (bnAddressA.gt(bnAddressB)) {
        return 1;
    }
    else if (bnAddressA.lt(bnAddressB)) {
        return -1;
    }
    else {
        return 0;
    }
}
exports.compareAddresses = compareAddresses;
function compareAddressesSimple(addressA, addressB) {
    if (addressA === undefined || addressB === undefined) {
        return false;
    }
    return addressA.toLowerCase() === addressB.toLowerCase();
}
exports.compareAddressesSimple = compareAddressesSimple;
function toBytes32(address) {
    return ethers_1.utils.hexZeroPad(address, 32).toLowerCase();
}
exports.toBytes32 = toBytes32;
function toEvmAddress(hexString) {
    var rawAddress = ethers_1.utils.hexZeroPad(ethers_1.utils.hexStripZeros(hexString), 20);
    return ethers_1.utils.getAddress(rawAddress);
}
exports.toEvmAddress = toEvmAddress;
function isValidEvmAddress(address) {
    if (ethers_1.utils.isAddress(address)) {
        return true;
    }
    try {
        var evmAddress = ethers_1.utils.hexZeroPad(ethers_1.utils.hexStripZeros(address), 20);
        return ethers_1.utils.isAddress(ethers_1.utils.getAddress(evmAddress));
    }
    catch (_e) {
        return false;
    }
}
exports.isValidEvmAddress = isValidEvmAddress;
function toAddressType(address, chainId) {
    var rawAddress = address.startsWith("0x") ? ethers_1.utils.arrayify(address) : bs58_1.default.decode(address);
    if ((0, _1.chainIsEvm)(chainId) && EvmAddress.validate(rawAddress))
        return new EvmAddress(rawAddress);
    else if ((0, _1.chainIsSvm)(chainId) && SvmAddress.validate(rawAddress))
        return new SvmAddress(rawAddress);
    return new RawAddress(rawAddress);
}
exports.toAddressType = toAddressType;
var Address = (function () {
    function Address(_rawAddress) {
        this.__address_type_brand = true;
        this.evmAddress = undefined;
        this.bytes32Address = undefined;
        this.svmAddress = undefined;
        this.bnAddress = undefined;
        if (_rawAddress.length > 32) {
            throw new Error("Address ".concat(ethers_1.utils.hexlify(_rawAddress), " cannot be longer than 32 bytes."));
        }
        this.rawAddress = ethers_1.utils.zeroPad(_rawAddress, 32);
    }
    Address.isAddress = function (obj) {
        return "__address_type_brand" in obj;
    };
    Address.prototype.toBytes32 = function () {
        var _a;
        return ((_a = this.bytes32Address) !== null && _a !== void 0 ? _a : (this.bytes32Address = ethers_1.utils.hexZeroPad(ethers_1.utils.hexlify(this.rawAddress), 32).toLowerCase()));
    };
    Address.prototype.truncateToBytes20 = function () {
        var bytes20 = this.rawAddress.slice(-20);
        return toEvmAddress(ethers_1.utils.hexlify(bytes20));
    };
    Address.prototype.toBase58 = function () {
        var _a;
        return ((_a = this.svmAddress) !== null && _a !== void 0 ? _a : (this.svmAddress = bs58_1.default.encode(this.rawAddress)));
    };
    Address.prototype.toBigNumber = function () {
        var _a;
        return ((_a = this.bnAddress) !== null && _a !== void 0 ? _a : (this.bnAddress = _1.BigNumber.from(this.toBytes32())));
    };
    Address.prototype.toEvmAddress = function () {
        var _this = this;
        var _a;
        var parseRawAddress = function () {
            var hexString = ethers_1.utils.hexlify(_this.rawAddress);
            var rawAddress = ethers_1.utils.hexZeroPad(ethers_1.utils.hexStripZeros(hexString), 20);
            return ethers_1.utils.getAddress(rawAddress);
        };
        return ((_a = this.evmAddress) !== null && _a !== void 0 ? _a : (this.evmAddress = parseRawAddress()));
    };
    Address.prototype.toNative = function () {
        return this.toBytes32();
    };
    Address.prototype.toHexString = function () {
        return this.toBytes32();
    };
    Address.prototype.isValidOn = function (chainId) {
        if ((0, _1.chainIsEvm)(chainId))
            return EvmAddress.validate(this.rawAddress);
        if ((0, _1.chainIsSvm)(chainId))
            return SvmAddress.validate(this.rawAddress);
        return false;
    };
    Address.prototype.toString = function () {
        return this.toNative();
    };
    Address.prototype.isZeroAddress = function () {
        return ethers_1.utils.stripZeros(this.rawAddress).length === 0;
    };
    Address.prototype.forceSvmAddress = function () {
        return SvmAddress.from(this.toBase58());
    };
    Address.prototype.eq = function (other) {
        return this.toString() === other.toString();
    };
    Address.prototype.compare = function (otherAddress) {
        var bnAddressA = this.toBigNumber();
        var bnAddressB = otherAddress.toBigNumber();
        if (bnAddressA.gt(bnAddressB)) {
            return 1;
        }
        else if (bnAddressA.lt(bnAddressB)) {
            return -1;
        }
        else {
            return 0;
        }
    };
    Address.prototype.isEVM = function () {
        return false;
    };
    Address.prototype.isSVM = function () {
        return false;
    };
    return Address;
}());
exports.Address = Address;
var EvmAddress = (function (_super) {
    tslib_1.__extends(EvmAddress, _super);
    function EvmAddress(rawAddress) {
        var _this = this;
        if (!EvmAddress.validate(rawAddress)) {
            throw new Error("".concat(ethers_1.utils.hexlify(rawAddress), " is not a valid EVM address"));
        }
        _this = _super.call(this, rawAddress) || this;
        _this._type = "evm";
        _this._type;
        return _this;
    }
    EvmAddress.validate = function (rawAddress) {
        return (rawAddress.length == 20 || (rawAddress.length === 32 && rawAddress.slice(0, 12).every(function (field) { return field === 0; })));
    };
    EvmAddress.prototype.isEVM = function () {
        return true;
    };
    EvmAddress.prototype.toNative = function () {
        return this.toEvmAddress();
    };
    EvmAddress.from = function (address) {
        return address.startsWith("0x") ? new this(ethers_1.utils.arrayify(address)) : new this(bs58_1.default.decode(address));
    };
    return EvmAddress;
}(Address));
exports.EvmAddress = EvmAddress;
var SvmAddress = (function (_super) {
    tslib_1.__extends(SvmAddress, _super);
    function SvmAddress(rawAddress) {
        var _this = this;
        if (!SvmAddress.validate(rawAddress)) {
            throw new Error("".concat(ethers_1.utils.hexlify(rawAddress), " is not a valid SVM address"));
        }
        _this = _super.call(this, rawAddress) || this;
        _this._type = "svm";
        _this._type;
        return _this;
    }
    SvmAddress.validate = function (rawAddress) {
        return (rawAddress.length === 32 &&
            (!rawAddress.slice(0, 12).every(function (field) { return field === 0; }) || rawAddress.every(function (field) { return field === 0; })));
    };
    SvmAddress.prototype.isSVM = function () {
        return true;
    };
    SvmAddress.prototype.toNative = function () {
        return this.toBase58();
    };
    SvmAddress.from = function (address) {
        return address.startsWith("0x") ? new this(ethers_1.utils.arrayify(address)) : new this(bs58_1.default.decode(address));
    };
    return SvmAddress;
}(Address));
exports.SvmAddress = SvmAddress;
var RawAddress = (function (_super) {
    tslib_1.__extends(RawAddress, _super);
    function RawAddress(rawAddress) {
        var _this = _super.call(this, rawAddress) || this;
        _this._type = "raw";
        _this._type;
        return _this;
    }
    return RawAddress;
}(Address));
exports.RawAddress = RawAddress;
//# sourceMappingURL=AddressUtils.js.map