Skip to content

Commit

Permalink
feat(core): partial support for ShelleyGenesis PoolId
Browse files Browse the repository at this point in the history
  • Loading branch information
mkazlauskas committed Jan 18, 2022
1 parent 0f4cb28 commit 51bcfb6
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 3 deletions.
1 change: 1 addition & 0 deletions packages/core/src/CSL/certificate.ts
Expand Up @@ -124,6 +124,7 @@ export const poolRetirement = (poolId: Cardano.PoolId, epoch: number) =>

export const stakeDelegation = (rewardAccount: Cardano.RewardAccount, delegatee: Cardano.PoolId) =>
Certificate.new_stake_delegation(
// TODO: add coreToCsl support for genesis pool IDs
StakeDelegation.new(stakeAddressToCredential(rewardAccount), Ed25519KeyHash.from_bech32(delegatee.toString()))
);

Expand Down
17 changes: 14 additions & 3 deletions packages/core/src/Cardano/types/StakePool/primitives.ts
@@ -1,16 +1,27 @@
/* eslint-disable @typescript-eslint/no-explicit-any */
import { Hash28ByteBase16, OpaqueString, typedBech32, typedHex } from '../../util';
import { InvalidStringError } from '../../..';

/**
* pool operator verification key hash as bech32 string
* pool operator verification key hash as bech32 string or a genesis pool ID
*/
export type PoolId = OpaqueString<'PoolId'>;

/**
* @param {string} value blake2b_224 digest of an operator verification key hash
* @param {string} value blake2b_224 digest of an operator verification key hash or a genesis pool ID
* @throws InvalidStringError
*/
export const PoolId = (value: string): PoolId => typedBech32(value, 'pool', 45);
export const PoolId = (value: string): PoolId => {
try {
return typedBech32(value, 'pool', 45);
} catch (error: unknown) {
// eslint-disable-next-line prettier/prettier
if ((/^ShelleyGenesis-[\dA-Fa-f]{16}$/).test(value)) {
return value as unknown as PoolId;
}
throw new InvalidStringError('Expected PoolId to be either bech32 or genesis stake pool', error);
}
};

/**
* pool operator verification key hash as hex string
Expand Down
4 changes: 4 additions & 0 deletions packages/core/test/Cardano/types/StakePool.test.ts
Expand Up @@ -5,6 +5,10 @@ describe('Cardano/types/StakePool', () => {
expect(() => Cardano.PoolId('pool1zuevzm3xlrhmwjw87ec38mzs02tlkwec9wxpgafcaykmwg7efhh')).not.toThrow();
});

it('PoolId() accepts a valid genesis pool ID', () => {
expect(() => Cardano.PoolId('ShelleyGenesis-eff1b5b26e65b791')).not.toThrow();
});

it('PoolIdHex() accepts a valid pool id hex string', () => {
expect(() => Cardano.PoolIdHex('e4b1c8ec89415ce6349755a1aa44b4affbb5f1248ff29943d190c715')).not.toThrow();
});
Expand Down

0 comments on commit 51bcfb6

Please sign in to comment.