import { RawPoolKeyHash, PoolParams, RawPoolParams, parsePoolParams } from "./pool"; import { Anchor, RawAnchor, RawCredential } from "../types"; import { DRep, RawDRep } from "./drep"; import { Credential, StakeCredential } from "./address"; export type RawCertificate = | RawStakeRegistration | RawStakeDeregistration | RawStakeDelegation | RawPoolRegistration | RawPoolRetirement | RawDelegationRegistration | RawDelegationDeRegistration | RawVoteDelegation | RawStakeVoteDelegation | RawStakeRegistrationDelegation | RawVoteRegistrationDelegation | RawStakeVoteRegistrationDelegation | RawAuthCommitteeHot | RawResignCommitteeCold | RawRegisterDRep | RawUnregisterDRep | RawUpdateDRep; type RawStakeRegistration = [0, RawCredential]; type RawStakeDeregistration = [1, RawCredential]; type RawStakeDelegation = [2, RawCredential, RawPoolKeyHash]; type RawPoolRegistration = [3, RawPoolParams]; type RawPoolRetirement = [4, RawPoolKeyHash, number | bigint]; type RawDelegationRegistration = [7, RawCredential, bigint]; type RawDelegationDeRegistration = [8, RawCredential, bigint]; type RawVoteDelegation = [9, RawCredential, RawDRep]; type RawStakeVoteDelegation = [10, RawCredential, RawPoolKeyHash, RawDRep]; type RawStakeRegistrationDelegation = [11, RawCredential, RawPoolKeyHash, bigint]; type RawVoteRegistrationDelegation = [12, RawCredential, RawDRep, bigint]; type RawStakeVoteRegistrationDelegation = [13, RawCredential, RawPoolKeyHash, RawDRep, bigint]; type RawAuthCommitteeHot = [14, RawCredential, RawCredential]; type RawResignCommitteeCold = [15, RawCredential, RawAnchor?]; type RawRegisterDRep = [16, RawCredential, bigint, RawAnchor?]; type RawUnregisterDRep = [17, RawCredential, bigint]; type RawUpdateDRep = [18, RawCredential, RawAnchor?]; export class Certificate { type!: string; stakeCredential?: StakeCredential; poolKeyHash?: RawPoolKeyHash; poolParams?: PoolParams; deposit?: bigint; epoch?: bigint; dRep?: DRep; credential?: Credential; coldCredential?: Credential; hotCredential?: Credential; anchor?: Anchor; static fromCborObject(obj: RawCertificate): Certificate { throw new Error("Method 'fromCborObject' must be implemented in derived classes"); } } export class StakeRegistrationCertificate extends Certificate { declare stakeCredential: StakeCredential; declare type: "StakeRegistration"; constructor(credential: StakeCredential) { super(); this.stakeCredential = credential; this.type = "StakeRegistration"; } static override fromCborObject(obj: RawCertificate): StakeRegistrationCertificate { if (obj[0] == 0) { return new StakeRegistrationCertificate(StakeCredential.fromCborObject(obj[1])); } else throw new Error("Invalid CBOR type for StakeRegistrationCertificate"); } } export class StakeDeregistrationCertificate extends Certificate { declare stakeCredential: StakeCredential; constructor(credential: StakeCredential) { super(); this.stakeCredential = credential; this.type = "StakeRegistration"; } static override fromCborObject(obj: RawCertificate): StakeDeregistrationCertificate { if (obj[0] == 1) { return new StakeDeregistrationCertificate(StakeCredential.fromCborObject(obj[1])); } else throw new Error("Invalid CBOR type for StakeDeregistrationCertificate"); } } class StakeDelegationCertificate extends Certificate { declare stakeCredential: StakeCredential; declare poolKeyHash: RawPoolKeyHash; constructor(credential: StakeCredential, poolKeyHash: RawPoolKeyHash) { super(); this.stakeCredential = credential; this.poolKeyHash = poolKeyHash; this.type = "StakeDelegation"; } static override fromCborObject(obj: RawCertificate): StakeDelegationCertificate { if (obj[0] == 2) { return new StakeDelegationCertificate(StakeCredential.fromCborObject(obj[1]), obj[2]); } else throw new Error("Invalid CBOR type for StakeDelegationCertificate"); } } export class PoolRegistrationCertificate extends Certificate { declare poolParams: PoolParams; constructor(poolParams: PoolParams) { super(); this.poolParams = poolParams; this.type = "PoolRegistration"; } static override fromCborObject(obj: RawCertificate): PoolRegistrationCertificate { if (obj[0] == 3) { return new PoolRegistrationCertificate(parsePoolParams(obj.slice(1))); } else throw new Error("Invalid CBOR type for PoolRegistrationCertificate"); } } export class PoolRetirementCertificate extends Certificate { declare poolKeyHash: RawPoolKeyHash; declare epoch: bigint; constructor(poolKeyHash: RawPoolKeyHash, epoch: number | bigint) { super(); this.poolKeyHash = poolKeyHash; this.epoch = typeof epoch == "number" ? BigInt(epoch) : epoch; this.type = "PoolRetirement"; } static override fromCborObject(obj: RawCertificate): PoolRetirementCertificate { if (obj[0] == 4) { return new PoolRetirementCertificate(obj[1], obj[2]); } else throw new Error("Invalid CBOR type for PoolRetirementCertificate"); } } export class DelegationRegistrationCertificate extends Certificate { declare stakeCredential: StakeCredential; declare deposit: bigint; constructor(credential: StakeCredential, deposit: number | bigint) { super(); this.stakeCredential = credential; this.deposit = typeof deposit == "number" ? BigInt(deposit) : deposit; this.type = "DelegationRegistration"; } static override fromCborObject(obj: RawCertificate): DelegationRegistrationCertificate { if (obj[0] == 7) { return new DelegationRegistrationCertificate(StakeCredential.fromCborObject(obj[1]), obj[2]); } else throw new Error("Invalid CBOR type for DelegationRegistrationCertificate"); } } export class DelegationDeRegistrationCertificate extends Certificate { declare stakeCredential: StakeCredential; declare deposit: bigint; constructor(credential: StakeCredential, deposit: number | bigint) { super(); this.stakeCredential = credential; this.deposit = typeof deposit == "number" ? BigInt(deposit) : deposit; this.type = "DelegationDeRegistration"; } static override fromCborObject(obj: RawCertificate): DelegationDeRegistrationCertificate { if (obj[0] == 8) { return new DelegationDeRegistrationCertificate(StakeCredential.fromCborObject(obj[1]), obj[2]); } else throw new Error("Invalid CBOR type for DelegationDeRegistrationCertificate"); } } export class VoteDelegationCertificate extends Certificate { declare stakeCredential: StakeCredential; declare dRep: DRep; constructor(credential: StakeCredential, dRep: DRep) { super(); this.stakeCredential = credential; this.dRep = dRep; this.type = "VoteDelegation"; } static override fromCborObject(obj: RawCertificate): VoteDelegationCertificate { if (obj[0] == 9) { return new VoteDelegationCertificate(StakeCredential.fromCborObject(obj[1]), DRep.fromCborObject(obj[2])); } else throw new Error("Invalid CBOR type for VoteDelegationCertificate"); } } export class StakeVoteDelegationCertificate extends Certificate { declare stakeCredential: StakeCredential; declare poolKeyHash: RawPoolKeyHash; declare dRep: DRep; constructor(credential: StakeCredential, poolKeyHash: RawPoolKeyHash, dRep: DRep) { super(); this.stakeCredential = credential; this.poolKeyHash = poolKeyHash; this.dRep = dRep; this.type = "StakeVoteDelegation"; } static override fromCborObject(obj: RawCertificate): StakeVoteDelegationCertificate { if (obj[0] == 10) { return new StakeVoteDelegationCertificate( StakeCredential.fromCborObject(obj[1]), obj[2], DRep.fromCborObject(obj[3]) ); } else throw new Error("Invalid CBOR type for StakeVoteDelegationCertificate"); } } export class StakeRegistrationDelegationCertificate extends Certificate { declare stakeCredential: StakeCredential; declare poolKeyHash: RawPoolKeyHash; declare deposit: bigint; constructor(credential: StakeCredential, poolKeyHash: RawPoolKeyHash, deposit: bigint) { super(); this.stakeCredential = credential; this.poolKeyHash = poolKeyHash; this.deposit = deposit; this.type = "StakeRegistrationDelegation"; } static override fromCborObject(obj: RawCertificate): StakeRegistrationDelegationCertificate { if (obj[0] == 11) { return new StakeRegistrationDelegationCertificate(StakeCredential.fromCborObject(obj[1]), obj[2], obj[3]); } else throw new Error("Invalid CBOR type for StakeRegistrationDelegationCertificate"); } } export class VoteRegistrationDelegationCertificate extends Certificate { declare stakeCredential: StakeCredential; declare dRep: DRep; declare deposit: bigint; constructor(credential: StakeCredential, dRep: DRep, deposit: bigint) { super(); this.stakeCredential = credential; this.dRep = dRep; this.deposit = deposit; this.type = "VoteRegistrationDelegation"; } static override fromCborObject(obj: RawCertificate): VoteRegistrationDelegationCertificate { if (obj[0] == 12) { return new VoteRegistrationDelegationCertificate( StakeCredential.fromCborObject(obj[1]), DRep.fromCborObject(obj[2]), obj[3] ); } else throw new Error("Invalid CBOR type for VoteRegistrationDelegationCertificate"); } } export class StakeVoteRegistrationDelegationCertificate extends Certificate { declare stakeCredential: StakeCredential; declare poolKeyHash: RawPoolKeyHash; declare dRep: DRep; declare deposit: bigint; constructor(credential: StakeCredential, poolKeyHash: RawPoolKeyHash, dRep: DRep, deposit: bigint) { super(); this.stakeCredential = credential; this.poolKeyHash = poolKeyHash; this.dRep = dRep; this.deposit = deposit; this.type = "StakeVoteRegistrationDelegation"; } static override fromCborObject(obj: RawCertificate): StakeVoteRegistrationDelegationCertificate { if (obj[0] == 13) { return new StakeVoteRegistrationDelegationCertificate( StakeCredential.fromCborObject(obj[1]), obj[2], DRep.fromCborObject(obj[3]), obj[4] ); } else throw new Error("Invalid CBOR type for StakeVoteRegistrationDelegationCertificate"); } } export class AuthCommitteeHotCertificate extends Certificate { declare coldCredential: Credential; declare hotCredential: Credential; constructor(coldCredential: Credential, hotCredential: Credential) { super(); this.coldCredential = coldCredential; this.hotCredential = hotCredential; this.type = "AuthCommitteeHot"; } static override fromCborObject(obj: RawCertificate): AuthCommitteeHotCertificate { if (obj[0] == 14) { return new AuthCommitteeHotCertificate(Credential.fromCborObject(obj[1]), Credential.fromCborObject(obj[2])); } else throw new Error("Invalid CBOR type for AuthCommitteeHotCertificate"); } } export class ResignCommitteeColdCertificate extends Certificate { declare coldCredential: Credential; declare anchor?: Anchor; constructor(coldCredential: Credential, anchor?: Anchor) { super(); this.coldCredential = coldCredential; this.anchor = anchor; this.type = "ResignCommitteeCold"; } static override fromCborObject(obj: RawCertificate): ResignCommitteeColdCertificate { if (obj[0] == 15) { return new ResignCommitteeColdCertificate( Credential.fromCborObject(obj[1]), obj[2] ? Anchor.fromCborObject(obj[2]) : undefined ); } else throw new Error("Invalid CBOR type for ResignCommitteeColdCertificate"); } } export class RegisterDRepCertificate extends Certificate { declare credential: Credential; declare deposit: bigint; declare anchor?: Anchor; constructor(credential: Credential, deposit: bigint, anchor?: Anchor) { super(); this.credential = credential; this.deposit = deposit; this.anchor = anchor; this.type = "RegisterDRep"; } static override fromCborObject(obj: RawCertificate): RegisterDRepCertificate { if (obj[0] == 16) { return new RegisterDRepCertificate( Credential.fromCborObject(obj[1]), obj[2], obj[3] ? Anchor.fromCborObject(obj[3]) : undefined ); } else throw new Error("Invalid CBOR type for RegisterDRepCertificate"); } } export class UnregisterDRepCertificate extends Certificate { declare credential: Credential; declare deposit: bigint; constructor(credential: Credential, deposit: bigint) { super(); this.credential = credential; this.deposit = deposit; this.type = "UnregisterDRep"; } static override fromCborObject(obj: RawCertificate): UnregisterDRepCertificate { if (obj[0] == 17) { return new UnregisterDRepCertificate(Credential.fromCborObject(obj[1]), obj[2]); } else throw new Error("Invalid CBOR type for UnregisterDRepCertificate"); } } export class UpdateDRepCertificate extends Certificate { declare credential: Credential; declare anchor?: Anchor; constructor(credential: Credential, anchor?: Anchor) { super(); this.credential = credential; this.anchor = anchor; this.type = "UpdateDRep"; } static override fromCborObject(obj: RawCertificate): UpdateDRepCertificate { if (obj[0] == 18) { return new UpdateDRepCertificate( Credential.fromCborObject(obj[1]), obj[2] ? Anchor.fromCborObject(obj[2]) : undefined ); } else throw new Error("Invalid CBOR type for UpdateDRepCertificate"); } } Certificate.fromCborObject = function (obj: RawCertificate): Certificate { switch (obj[0]) { case 0: return StakeRegistrationCertificate.fromCborObject(obj); case 1: return StakeDeregistrationCertificate.fromCborObject(obj); case 2: return StakeDelegationCertificate.fromCborObject(obj); case 3: return PoolRegistrationCertificate.fromCborObject(obj); case 4: return PoolRetirementCertificate.fromCborObject(obj); case 7: return DelegationRegistrationCertificate.fromCborObject(obj); case 8: return DelegationDeRegistrationCertificate.fromCborObject(obj); case 9: return VoteDelegationCertificate.fromCborObject(obj); case 10: return StakeVoteDelegationCertificate.fromCborObject(obj); case 11: return StakeRegistrationDelegationCertificate.fromCborObject(obj); case 12: return VoteRegistrationDelegationCertificate.fromCborObject(obj); case 13: return StakeVoteRegistrationDelegationCertificate.fromCborObject(obj); case 14: return AuthCommitteeHotCertificate.fromCborObject(obj); case 15: return ResignCommitteeColdCertificate.fromCborObject(obj); case 16: return RegisterDRepCertificate.fromCborObject(obj); case 17: return UnregisterDRepCertificate.fromCborObject(obj); case 18: return UpdateDRepCertificate.fromCborObject(obj); default: throw new Error("Unknown Certificate type"); } }; export function parseCertificates(rawCertificates: RawCertificate[]): Certificate[] { const certificates: Certificate[] = []; rawCertificates.forEach((certificate) => { certificates.push(Certificate.fromCborObject(certificate)); }); return certificates; }