Skip to content

Commit

Permalink
feat!: implement ogmiosToCore certificates mapping
Browse files Browse the repository at this point in the history
feat(core): add addressNetworkId and createRewardAccount utils

BREAKING CHANGE: rename block types
- CompactBlock -> BlockInfo
- Block -> ExtendedBlockInfo

BREAKING CHANGE: hoist ogmiosToCore to ogmios package
  • Loading branch information
mkazlauskas committed Nov 18, 2022
1 parent 10d1f2b commit aef2e8d
Show file tree
Hide file tree
Showing 24 changed files with 599 additions and 226 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ export class DbSyncChainHistoryProvider extends DbSyncProvider() implements Chai
});
}

public async blocksByHashes({ ids }: BlocksByIdsArgs): Promise<Cardano.Block[]> {
public async blocksByHashes({ ids }: BlocksByIdsArgs): Promise<Cardano.ExtendedBlockInfo[]> {
if (ids.length > this.#paginationPageSizeLimit) {
throw new ProviderError(
ProviderFailure.BadRequest,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,11 @@ export const mapTxAlonzo = (
}
});

export const mapBlock = (blockModel: BlockModel, blockOutputModel: BlockOutputModel, tip: TipModel): Cardano.Block => ({
export const mapBlock = (
blockModel: BlockModel,
blockOutputModel: BlockOutputModel,
tip: TipModel
): Cardano.ExtendedBlockInfo => ({
confirmations: tip.block_no - blockModel.block_no,
date: new Date(blockModel.time),
epoch: blockModel.epoch_no,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ describe('chain history mappers', () => {
};
test('map BlockModel to Cardano.Block', () => {
const result = mappers.mapBlock(blockModel, blockOutputModel, tipModel);
expect(result).toEqual<Cardano.Block>({
expect(result).toEqual<Cardano.ExtendedBlockInfo>({
confirmations: 100,
date: new Date(datetime),
epoch: 12,
Expand All @@ -165,7 +165,7 @@ describe('chain history mappers', () => {
blockOutputModel,
tipModel
);
expect(result).toEqual<Cardano.Block>({
expect(result).toEqual<Cardano.ExtendedBlockInfo>({
confirmations: 100,
date: new Date(datetime),
epoch: 12,
Expand Down
15 changes: 15 additions & 0 deletions packages/core/src/CML/address.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { Address, Ed25519KeyHash, NetworkId, RewardAccount } from '../Cardano';
import { CML } from './CML';
import { parseCmlAddress } from './parseCmlAddress';
import { usingAutoFree } from '@cardano-sdk/util';

export const addressNetworkId = (address: RewardAccount | Address): NetworkId =>
usingAutoFree((scope) => parseCmlAddress(scope, address.toString())!.network_id());

export const createRewardAccount = (stakeKeyHash: Ed25519KeyHash, networkId: NetworkId) =>
usingAutoFree((scope) => {
const keyHash = scope.manage(CML.Ed25519KeyHash.from_hex(stakeKeyHash.toString()));
const stakeCredential = scope.manage(CML.StakeCredential.from_keyhash(keyHash));
const rewardAccount = scope.manage(CML.RewardAddress.new(networkId, stakeCredential));
return RewardAccount(scope.manage(rewardAccount.to_address()).to_bech32());
});
2 changes: 1 addition & 1 deletion packages/core/src/CML/cmlToCore/certificate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ export const createCertificate = (cmlCertificate: CML.Certificate): Certificate
case CML.CertificateKind.PoolRetirement:
return poolRetirement(scope.manage(cmlCertificate.as_pool_retirement()!));
case CML.CertificateKind.GenesisKeyDelegation:
return genesisKeyDelegaation(scope.manage(cmlCertificate.as_genesis_key_delegation()!));
return genesisKeyDelegation(scope.manage(cmlCertificate.as_genesis_key_delegation()!));
case CML.CertificateKind.MoveInstantaneousRewardsCert:
throw new NotImplementedError('MIR certificate conversion'); // TODO: support this certificate type
default:
Expand Down
1 change: 1 addition & 0 deletions packages/core/src/CML/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ export * as cmlUtil from './util';
export * as cmlToCore from './cmlToCore';
export * as coreToCml from './coreToCml';
export * from './parseCmlAddress';
export * from './address';
17 changes: 11 additions & 6 deletions packages/core/src/Cardano/types/Block.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import { CML } from '../..';
import { Ed25519PublicKey } from '.';
import { Ed25519PublicKey } from './Key';
import { Hash28ByteBase16, Hash32ByteBase16, OpaqueString, typedBech32 } from '../util/primitives';
import { InvalidStringError } from '../../errors';
import { Lovelace } from './Value';
import { PoolId } from './StakePool/primitives';
import { TxAlonzo } from './Transaction';

/**
* The block size in bytes
Expand Down Expand Up @@ -89,7 +90,7 @@ export const VrfVkBech32FromBase64 = (value: string) =>
/** Minimal Block type meant as a base for the more complete version `Block` */
// TODO: optionals (except previousBlock) are there because they are not calculated for Byron yet.
// Remove them once calculation is done and remove the Required<BlockMinimal> from interface Block
export interface BlockMinimal {
export interface BlockInfo {
header: PartialBlockHeader;
/** Byron blocks fee not calculated yet */
fees?: Lovelace;
Expand All @@ -106,14 +107,18 @@ export interface BlockMinimal {
issuerVk?: Ed25519PublicKey;
}

export interface Block
extends Required<Omit<BlockMinimal, 'issuerVk' | 'previousBlock'>>,
Pick<BlockMinimal, 'previousBlock'> {
export interface Block extends BlockInfo {
body: TxAlonzo[];
}

export interface ExtendedBlockInfo
extends Required<Omit<BlockInfo, 'issuerVk' | 'previousBlock'>>,
Pick<BlockInfo, 'previousBlock'> {
/**
* In case of blocks produced by BFT nodes, the SlotLeader the issuerVk hash
* For blocks produced by stake pools, it is the Bech32 encoded value of issuerVk hash
*/
slotLeader: SlotLeader;
slotLeader: SlotLeader; // TODO: move to CompactBlockInfo and make nullable
date: Date;
epoch: EpochNo;
epochSlot: number;
Expand Down
6 changes: 0 additions & 6 deletions packages/core/src/Ogmios/index.ts

This file was deleted.

1 change: 0 additions & 1 deletion packages/core/src/Ogmios/ogmiosToCore/index.ts

This file was deleted.

4 changes: 2 additions & 2 deletions packages/core/src/Provider/ChainHistoryProvider/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ export interface ChainHistoryProvider extends Provider {
* Gets the blocks matching the provided hashes.
*
* @param {Cardano.BlockId[]} ids array of block ids
* @returns {Cardano.Block[]} an array of blocks, same length and in the same order as `hashes` argument.
* @returns {Cardano.ExtendedBlockInfo[]} an array of blocks, same length and in the same order as `hashes` argument.
*/
blocksByHashes: (args: BlocksByIdsArgs) => Promise<Cardano.Block[]>;
blocksByHashes: (args: BlocksByIdsArgs) => Promise<Cardano.ExtendedBlockInfo[]>;
}
1 change: 0 additions & 1 deletion packages/core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,3 @@ export * from './CML';
export * from './util';
export * from './errors';
export * from './CardanoNode';
export * from './Ogmios';
58 changes: 58 additions & 0 deletions packages/core/test/CML/address.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import { Cardano, addressNetworkId, createRewardAccount } from '../../src';

describe('address', () => {
describe('addressNetworkId', () => {
it('parses testnet address', () => {
expect(addressNetworkId(Cardano.Address('addr_test1wrsexavz37208qda7mwwu4k7hcpg26cz0ce86f5e9kul3hqzlh22t'))).toBe(
Cardano.NetworkId.testnet
);
});

it('parses testnet reward account', () => {
expect(
addressNetworkId(Cardano.RewardAccount('stake_test1urpklgzqsh9yqz8pkyuxcw9dlszpe5flnxjtl55epla6ftqktdyfz'))
).toBe(Cardano.NetworkId.testnet);
});

it('parses mainnet address', () => {
expect(
addressNetworkId(
Cardano.Address(
'addr1qx52knza2h5x090n4a5r7yraz3pwcamk9ppvuh7e26nfks7pnmhxqavtqy02zezklh27jt9r6z62sav3mugappdc7xnskxy2pn'
)
)
).toBe(Cardano.NetworkId.mainnet);
});

it('parses mainnet reward account', () => {
expect(
addressNetworkId(Cardano.RewardAccount('stake1u89sasnfyjtmgk8ydqfv3fdl52f36x3djedfnzfc9rkgzrcss5vgr'))
).toBe(Cardano.NetworkId.mainnet);
});

it('parses mainnet byron address', () => {
expect(
addressNetworkId(
Cardano.Address(
'DdzFFzCqrht4PWfBGtmrQz4x1GkZHYLVGbK7aaBkjWxujxzz3L5GxCgPiTsks5RjUr3yX9KvwKjNJBt7ZzPCmS3fUQrGeRvo9Y1YBQKQ'
)
)
).toBe(Cardano.NetworkId.mainnet);
});
});

describe('createRewardAccount', () => {
const keyHash = Cardano.Ed25519KeyHash('f15db05f56035465bf8900a09bdaa16c3d8b8244fea686524408dd80');

it('creates a mainnet address', () => {
const rewardAccount = createRewardAccount(keyHash, Cardano.NetworkId.mainnet);
expect(rewardAccount.startsWith('stake')).toBe(true);
expect(rewardAccount.startsWith('stake_test')).toBe(false);
});

it('creates a testnet address', () => {
const rewardAccount = createRewardAccount(keyHash, Cardano.NetworkId.testnet);
expect(rewardAccount.startsWith('stake_test')).toBe(true);
});
});
});
133 changes: 0 additions & 133 deletions packages/core/test/Ogmios/ogmiosToCore.test.ts

This file was deleted.

6 changes: 5 additions & 1 deletion packages/ogmios/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,9 @@
"prepack": "yarn build"
},
"devDependencies": {
"@cardano-ogmios/schema": "5.5.7",
"@cardano-sdk/util-dev": "^0.5.0",
"@types/lodash": "^4.14.182",
"eslint": "^7.32.0",
"get-port-please": "^2.5.0",
"jest": "^28.1.3",
Expand All @@ -61,10 +63,12 @@
"ws": "^8.5.0"
},
"dependencies": {
"@cardano-ogmios/client": "5.5.5",
"@cardano-ogmios/client": "5.5.7",
"@cardano-sdk/core": "^0.6.0",
"@cardano-sdk/util": "^0.6.0",
"buffer": "5.7.1",
"fraction.js": "^4.2.0",
"lodash": "^4.17.21",
"ts-log": "^2.2.3"
},
"files": [
Expand Down
1 change: 1 addition & 0 deletions packages/ogmios/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
export * from './Provider';
export * from './CardanoNode';
export * from './util';
export * as ogmiosToCore from './ogmiosToCore';
export * as Ogmios from '@cardano-ogmios/client';
Loading

0 comments on commit aef2e8d

Please sign in to comment.