Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add conway era cert support #38

Merged
merged 3 commits into from
Dec 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
93 changes: 88 additions & 5 deletions src/internal-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,24 +55,107 @@ export type EncodedOutput = Map<
Buffer | EncodedAmount | EncodedDatumOption | CborTag
>;
export type EncodedWithdrawals = Map<Buffer, BigNumber>;
export type EncodedStakeCredential = [HashType, Buffer];
export type EncodedCredential = [HashType, Buffer];
export type EncodedCommitteeHotCredential = EncodedCredential;
export type EncodedCommitteeColdCredential = EncodedCredential;
export type EncodedDRepCredential = EncodedCredential;
export type EncodedDRep = [0, Buffer] | [1, Buffer] | [2] | [3];
export type EncodedAnchor = [string, Buffer] | null;
export type EncodedStakeRegistrationCertificate = [
CertificateType.STAKE_REGISTRATION,
EncodedStakeCredential,
EncodedCredential
];
export type EncodedStakeDeRegistrationCertificate = [
CertificateType.STAKE_DE_REGISTRATION,
EncodedStakeCredential,
EncodedCredential
];
export type EncodedStakeDelegationCertificate = [
CertificateType.STAKE_DELEGATION,
EncodedStakeCredential,
EncodedCredential,
Buffer
];
export type EncodedStakeKeyRegistrationCertificate = [
CertificateType.STAKE_KEY_REGISTRATION,
EncodedCredential,
BigNumber
];
export type EncodedStakeKeyDeRegistrationCertificate = [
CertificateType.STAKE_KEY_DE_REGISTRATION,
EncodedCredential,
BigNumber
];
export type EncodedVoteDelegationCertificate = [
CertificateType.VOTE_DELEGATION,
EncodedCredential,
EncodedDRep
];
export type EncodedStakeVoteDelegationCertificate = [
CertificateType.STAKE_VOTE_DELEG,
EncodedCredential,
Buffer,
EncodedDRep
];
export type EncodedStakeRegDelegationCertificate = [
CertificateType.STAKE_REG_DELEG,
EncodedCredential,
Buffer,
BigNumber
];
export type EncodedVoteRegDelegationCertificate = [
CertificateType.VOTE_REG_DELEG,
EncodedCredential,
EncodedDRep,
BigNumber
];
export type EncodedStakeVoteRegDelegationCertificate = [
CertificateType.STAKE_VOTE_REG_DELEG,
EncodedCredential,
Buffer,
EncodedDRep,
BigNumber
];
export type EncodedCommitteeAuthHotCertificate = [
CertificateType.COMMITTEE_AUTH_HOT,
EncodedCommitteeColdCredential,
EncodedCommitteeHotCredential
];
export type EncodedCommitteeResignColdCertificate = [
CertificateType.COMMITTEE_RESIGN_COLD,
EncodedCommitteeColdCredential,
EncodedAnchor
];
export type EncodedDRepRegCertificate = [
CertificateType.DREP_REG,
EncodedDRepCredential,
BigNumber,
EncodedAnchor
];
export type EncodedDRepDeRegCertificate = [
CertificateType.DREP_DE_REG,
EncodedDRepCredential,
BigNumber
];
export type EncodedDRepUpdateCertificate = [
CertificateType.DREP_UPDATE,
EncodedDRepCredential,
EncodedAnchor
];
export type EncodedCertificate =
| EncodedStakeRegistrationCertificate
| EncodedStakeDeRegistrationCertificate
| EncodedStakeDelegationCertificate;
| EncodedStakeDelegationCertificate
| EncodedStakeKeyRegistrationCertificate
| EncodedStakeKeyDeRegistrationCertificate
| EncodedVoteDelegationCertificate
| EncodedStakeVoteDelegationCertificate
| EncodedStakeRegDelegationCertificate
| EncodedVoteRegDelegationCertificate
| EncodedStakeVoteRegDelegationCertificate
| EncodedCommitteeAuthHotCertificate
| EncodedCommitteeResignColdCertificate
| EncodedDRepRegCertificate
| EncodedDRepDeRegCertificate
| EncodedDRepUpdateCertificate;

export type EncodedExUnits = [number, number];
export type EncodedVKeyWitness = [Buffer, Buffer];
Expand Down
163 changes: 147 additions & 16 deletions src/transaction/Transaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,8 @@ import {
} from "../utils/encoder";
import { hash32 } from "../utils/crypto";
import { calculateMinUtxoAmountBabbage } from "../utils/utils";
import transactionBuilder from "./transactionBuilder";
import { paymentTransaction } from "./paymentTransaction";
import transactionBuilder from "./helpers/transactionBuilder";
import { paymentTransaction } from "./helpers/paymentTransaction";
import { TransactionBodyItemType } from "../internal-types";

export class Transaction {
Expand Down Expand Up @@ -194,22 +194,135 @@ export class Transaction {
}
}

/**
* The method will add a certificate to the transaction to be included in cbor
* This method will automatically scan and include each unique required witnesses in the map
* to help sign the transaction
* @param certificate a certificate to include in the transaction
*/
addCertificate(certificate: Certificate): void {
if (certificate.certType === CertificateType.STAKE_DELEGATION) {
if (certificate.stakeCredential.type === HashType.ADDRESS) {
this.requiredWitnesses.set(
certificate.stakeCredential.hash.toString("hex"),
certificate.stakeCredential.bipPath
);
switch (certificate.type) {
case CertificateType.STAKE_DELEGATION: {
if (certificate.cert.stakeCredential.type === HashType.ADDRESS) {
this.requiredWitnesses.set(
certificate.cert.stakeCredential.hash.toString("hex"),
certificate.cert.stakeCredential.bipPath
);
}
break;
}
} else if (certificate.certType === CertificateType.STAKE_DE_REGISTRATION) {
if (certificate.stakeCredential.type === HashType.ADDRESS) {
this.requiredWitnesses.set(
certificate.stakeCredential.hash.toString("hex"),
certificate.stakeCredential.bipPath
);
case CertificateType.STAKE_REGISTRATION: {
if (certificate.cert.stakeCredential.type === HashType.ADDRESS) {
this.requiredWitnesses.set(
certificate.cert.stakeCredential.hash.toString("hex"),
certificate.cert.stakeCredential.bipPath
);
}
break;
}
case CertificateType.STAKE_DE_REGISTRATION: {
if (certificate.cert.stakeCredential.type === HashType.ADDRESS) {
this.requiredWitnesses.set(
certificate.cert.stakeCredential.hash.toString("hex"),
certificate.cert.stakeCredential.bipPath
);
}
break;
}
case CertificateType.STAKE_KEY_REGISTRATION: {
if (certificate.cert.stakeCredential.type === HashType.ADDRESS) {
this.requiredWitnesses.set(
certificate.cert.stakeCredential.hash.toString("hex"),
certificate.cert.stakeCredential.bipPath
);
}
break;
}
case CertificateType.STAKE_KEY_DE_REGISTRATION: {
if (certificate.cert.stakeCredential.type === HashType.ADDRESS) {
this.requiredWitnesses.set(
certificate.cert.stakeCredential.hash.toString("hex"),
certificate.cert.stakeCredential.bipPath
);
}
break;
}
case CertificateType.VOTE_DELEGATION: {
if (certificate.cert.stakeCredential.type === HashType.ADDRESS) {
this.requiredWitnesses.set(
certificate.cert.stakeCredential.hash.toString("hex"),
certificate.cert.stakeCredential.bipPath
);
}
break;
}
case CertificateType.STAKE_VOTE_DELEG: {
if (certificate.cert.stakeCredential.type === HashType.ADDRESS) {
this.requiredWitnesses.set(
certificate.cert.stakeCredential.hash.toString("hex"),
certificate.cert.stakeCredential.bipPath
);
}
break;
}
case CertificateType.STAKE_REG_DELEG: {
if (certificate.cert.stakeCredential.type === HashType.ADDRESS) {
this.requiredWitnesses.set(
certificate.cert.stakeCredential.hash.toString("hex"),
certificate.cert.stakeCredential.bipPath
);
}
break;
}
case CertificateType.VOTE_REG_DELEG: {
if (certificate.cert.stakeCredential.type === HashType.ADDRESS) {
this.requiredWitnesses.set(
certificate.cert.stakeCredential.hash.toString("hex"),
certificate.cert.stakeCredential.bipPath
);
}
break;
}
case CertificateType.STAKE_VOTE_REG_DELEG: {
if (certificate.cert.stakeCredential.type === HashType.ADDRESS) {
this.requiredWitnesses.set(
certificate.cert.stakeCredential.hash.toString("hex"),
certificate.cert.stakeCredential.bipPath
);
}
break;
}
case CertificateType.DREP_REG: {
if (certificate.cert.dRepCredential.type === HashType.ADDRESS) {
this.requiredWitnesses.set(
certificate.cert.dRepCredential.hash.toString("hex"),
certificate.cert.dRepCredential.bipPath
);
}
break;
}
case CertificateType.DREP_DE_REG: {
if (certificate.cert.dRepCredential.type === HashType.ADDRESS) {
this.requiredWitnesses.set(
certificate.cert.dRepCredential.hash.toString("hex"),
certificate.cert.dRepCredential.bipPath
);
}
break;
}
case CertificateType.DREP_UPDATE: {
if (certificate.cert.dRepCredential.type === HashType.ADDRESS) {
this.requiredWitnesses.set(
certificate.cert.dRepCredential.hash.toString("hex"),
certificate.cert.dRepCredential.bipPath
);
}
break;
}
default:
break;
}

this.certificates.push(certificate);
}

Expand Down Expand Up @@ -667,11 +780,20 @@ export class Transaction {
};
}

/**
* This method scans the certificates added in the transaction to calculate
* additional ADA required for transaction validity. Essentially ADA to be used
* in the deposit for stake key registration etc.
* @returns additional ADA required for a valid transaction
*/
getAdditionalOutputAda(): BigNumber {
return _.reduce(
this.certificates,
(result, cert) => {
if (cert.certType === CertificateType.STAKE_REGISTRATION) {
if (cert.type === CertificateType.STAKE_REGISTRATION) {
return result.plus(this._protocolParams.stakeKeyDeposit);
}
if (cert.type === CertificateType.STAKE_KEY_REGISTRATION) {
return result.plus(this._protocolParams.stakeKeyDeposit);
}
return result;
Expand All @@ -680,11 +802,20 @@ export class Transaction {
);
}

/**
* This method scans the certificates added in the transaction to calculate
* additional ADA available in inputs as part of the deposit refund.
* Essentially ADA to be considered as additional input deu to deposit refunds.
* @returns additional ADA available as input
*/
getAdditionalInputAda(): BigNumber {
const certDeposit = _.reduce(
this.certificates,
(result, cert) => {
if (cert.certType === CertificateType.STAKE_DE_REGISTRATION) {
if (cert.type === CertificateType.STAKE_DE_REGISTRATION) {
return result.plus(this._protocolParams.stakeKeyDeposit);
}
if (cert.type === CertificateType.STAKE_KEY_DE_REGISTRATION) {
return result.plus(this._protocolParams.stakeKeyDeposit);
}
return result;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import Transaction from "./Transaction";
import { CardanoAddress, Output, Input, ProtocolParams, AuxiliaryData } from "../types";
import Transaction from "../Transaction";
import { CardanoAddress, Output, Input, ProtocolParams, AuxiliaryData } from "../../types";

export const paymentTransaction = ({
inputs,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import BigNumber from "bignumber.js";
import _ = require("lodash");
import { CardanoAddress, Input, Output, CollateralInput } from "../types";
import { getTokenDiff } from "../utils/helpers";
import { CardanoAddress, Input, Output, CollateralInput } from "../../types";
import { getTokenDiff } from "../../utils/helpers";
import {
calculateMinUtxoAmountBabbage,
getAddressFromHex,
getMaximumTokenSets,
} from "../utils/utils";
import Transaction from "./Transaction";
} from "../../utils/utils";
import Transaction from "../Transaction";

export function transactionBuilder({
transaction,
Expand Down
Loading