"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.AlgorandAddress = exports.AlgorandZeroAddress = void 0;
const connect_sdk_1 = require("@wormhole-foundation/connect-sdk");
const algosdk_1 = require("algosdk");
const platform_1 = require("./platform");
const types_1 = require("./types");
exports.AlgorandZeroAddress = "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAY5HFKQ";
// Note: for ASA/App IDs we encode them as 8 bytes at the start of
// the 32 byte adddress bytes.
class AlgorandAddress {
    static byteSize = 32;
    static platform = types_1._platform;
    type = "Native";
    address;
    constructor(address) {
        if (AlgorandAddress.instanceof(address)) {
            this.address = address.address;
        }
        else if (connect_sdk_1.UniversalAddress.instanceof(address)) {
            this.address = address.toUint8Array();
        }
        else if (address instanceof Uint8Array && address.byteLength === AlgorandAddress.byteSize) {
            this.address = address;
        }
        else if (typeof address === "string" && (0, algosdk_1.isValidAddress)(address)) {
            this.address = (0, algosdk_1.decodeAddress)(address).publicKey;
        }
        else if (typeof address === "string" && !isNaN(parseInt(address))) {
            this.address = connect_sdk_1.encoding.bytes.zpad(connect_sdk_1.encoding.bignum.toBytes(BigInt(address), 8), AlgorandAddress.byteSize);
        }
        else if (typeof address === "bigint") {
            this.address = connect_sdk_1.encoding.bytes.zpad(connect_sdk_1.encoding.bignum.toBytes(address, 8), AlgorandAddress.byteSize);
        }
        else if (address instanceof Uint8Array && address.byteLength === 8) {
            this.address = connect_sdk_1.encoding.bytes.zpad(address, AlgorandAddress.byteSize);
        }
        else
            throw new Error(`Invalid Algorand address or ASA ID: ${address}`);
    }
    unwrap() {
        return this.toString();
    }
    toString() {
        return (0, algosdk_1.encodeAddress)(this.address);
    }
    toNative() {
        return this;
    }
    toUint8Array() {
        return this.address;
    }
    toUniversalAddress() {
        return new connect_sdk_1.UniversalAddress(this.toUint8Array());
    }
    toBigInt() {
        return connect_sdk_1.encoding.bignum.decode(this.toUint8Array().slice(24, 32));
    }
    toInt() {
        return (0, types_1.safeBigIntToNumber)(this.toBigInt());
    }
    equals(other) {
        if (AlgorandAddress.instanceof(other)) {
            return other.address === this.address;
        }
        else {
            return this.toUniversalAddress().equals(other);
        }
    }
    static instanceof(address) {
        return address.constructor.platform === platform_1.AlgorandPlatform._platform;
    }
}
exports.AlgorandAddress = AlgorandAddress;
(0, connect_sdk_1.registerNative)(types_1._platform, AlgorandAddress);
//# sourceMappingURL=address.js.map