Skip to content

Commit

Permalink
impl
Browse files Browse the repository at this point in the history
  • Loading branch information
jaskp committed Dec 3, 2023
1 parent fa1a4be commit 252ac6f
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 1 deletion.
40 changes: 40 additions & 0 deletions packages/connect/src/api/cardano/cardanoCertificate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import type {
CardanoPoolOwner,
CardanoPoolRelay,
CardanoPoolMetadata,
CardanoDRep,
} from '../../types/api/cardano';

const ipv4AddressToHex = (ipv4Address: string) =>
Expand Down Expand Up @@ -114,6 +115,16 @@ const validatePoolParameters = (poolParameters: CardanoPoolParameters) => {
}
};

const validateDRep = (drep: CardanoDRep) => {
validateParams(drep, [{ name: 'type', type: 'number', required: true }]);

if (drep.type === PROTO.CardanoDRepType.KEY_HASH) {
validateParams(drep, [{ name: 'keyHash', type: 'string', required: true }]);
} else if (drep.type === PROTO.CardanoDRepType.SCRIPT_HASH) {
validateParams(drep, [{ name: 'scriptHash', type: 'string', required: true }]);
}
};

export type CertificateWithPoolOwnersAndRelays = {
certificate: PROTO.CardanoTxCertificate;
poolOwners: PROTO.CardanoPoolOwner[];
Expand Down Expand Up @@ -164,6 +175,20 @@ const transformPoolParameters = (
};
};

const transformDRep = (drep?: CardanoDRep): PROTO.CardanoDRep | undefined => {
if (!drep) {
return undefined;
}

validateDRep(drep);

return {
type: drep.type,
key_hash: drep.keyHash,
script_hash: drep.scriptHash,
};
};

// transform incoming certificate object to protobuf messages format
export const transformCertificate = (
certificate: CardanoCertificate,
Expand All @@ -185,12 +210,25 @@ export const transformCertificate = (
paramsToValidate.push({ name: 'poolParameters', type: 'object', required: true });
}

if (
certificate.type === PROTO.CardanoCertificateType.STAKE_REGISTRATION_CONWAY ||
certificate.type === PROTO.CardanoCertificateType.STAKE_DEREGISTRATION_CONWAY
) {
paramsToValidate.push({ name: 'deposit', type: 'string', required: true });
}

if (certificate.type === PROTO.CardanoCertificateType.VOTE_DELEGATION) {
paramsToValidate.push({ name: 'drep', type: 'object', required: true });
}

validateParams(certificate, paramsToValidate);

const { poolParameters, poolOwners, poolRelays } = transformPoolParameters(
certificate.poolParameters,
);

const drep = transformDRep(certificate.drep);

return {
certificate: {
type: certificate.type,
Expand All @@ -199,6 +237,8 @@ export const transformCertificate = (
key_hash: certificate.keyHash,
pool: certificate.pool,
pool_parameters: poolParameters,
deposit: certificate.deposit,
drep,
},
poolOwners,
poolRelays,
Expand Down
1 change: 1 addition & 0 deletions packages/connect/src/api/cardano/cardanoUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ export const prepareCertificates = (certs: CardanoCertificate[]) => {
convertedCerts.push({
type: cert.type,
});
// conway certificates not supported by coin-selection lib yet
break;
// no default
}
Expand Down
5 changes: 4 additions & 1 deletion packages/connect/src/api/cardano/cardanoWitnesses.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,10 @@ export const gatherWitnessPaths = (
if (
certificate.path &&
(certificate.type === PROTO.CardanoCertificateType.STAKE_DELEGATION ||
certificate.type === PROTO.CardanoCertificateType.STAKE_DEREGISTRATION)
certificate.type === PROTO.CardanoCertificateType.STAKE_DEREGISTRATION ||
certificate.type === PROTO.CardanoCertificateType.STAKE_REGISTRATION_CONWAY ||
certificate.type === PROTO.CardanoCertificateType.STAKE_DEREGISTRATION_CONWAY ||
certificate.type === PROTO.CardanoCertificateType.VOTE_DELEGATION)
) {
_insert(certificate.path);
}
Expand Down

0 comments on commit 252ac6f

Please sign in to comment.