From b2f5d77348a59f8ecb0c0b133a3b2cb6a841840a Mon Sep 17 00:00:00 2001 From: Martynas Kazlauskas Date: Wed, 24 Nov 2021 18:11:06 +0200 Subject: [PATCH] refactor: add Ed25519Signature and Ed25519PublicKey types --- packages/core/src/CSL/coreToCsl.ts | 2 +- .../core/src/Cardano/types/AuxiliaryData.ts | 2 +- .../core/src/Cardano/types/Certificate.ts | 1 + .../src/Cardano/types/StakePool/StakePool.ts | 2 +- .../core/src/Cardano/types/Transaction.ts | 41 ++++++++++++++++--- packages/core/src/Cardano/types/Utxo.ts | 2 +- .../test/Cardano/types/Transaction.test.ts | 19 ++++++++- .../getOnChainAddressBalances.ts | 17 ++++---- .../src/KeyManagement/InMemoryKeyManager.ts | 5 ++- 9 files changed, 69 insertions(+), 22 deletions(-) diff --git a/packages/core/src/CSL/coreToCsl.ts b/packages/core/src/CSL/coreToCsl.ts index 9af15d6f174..4f6b2841052 100644 --- a/packages/core/src/CSL/coreToCsl.ts +++ b/packages/core/src/CSL/coreToCsl.ts @@ -116,7 +116,7 @@ export const tx = ({ body, witness }: Cardano.NewTxAlonzo): Transaction => { if (Object.prototype.hasOwnProperty.call(witness.signatures, vkey)) { const signature = witness.signatures[vkey]!; const publicKey = PublicKey.from_bech32(vkey); - const vkeyWitness = Vkeywitness.new(Vkey.new(publicKey), Ed25519Signature.from_hex(signature)); + const vkeyWitness = Vkeywitness.new(Vkey.new(publicKey), Ed25519Signature.from_hex(signature.toString())); vkeyWitnesses.add(vkeyWitness); } } diff --git a/packages/core/src/Cardano/types/AuxiliaryData.ts b/packages/core/src/Cardano/types/AuxiliaryData.ts index 1885e508423..bdcb788da96 100644 --- a/packages/core/src/Cardano/types/AuxiliaryData.ts +++ b/packages/core/src/Cardano/types/AuxiliaryData.ts @@ -26,6 +26,6 @@ export interface AuxiliaryDataBody { } export interface AuxiliaryData { - hash?: Cardano.Hash16; + hash?: Cardano.Hash16; // TODO: Review: need to find an example of this to verify type and length body: AuxiliaryDataBody; } diff --git a/packages/core/src/Cardano/types/Certificate.ts b/packages/core/src/Cardano/types/Certificate.ts index 85c67409bf2..9af7da333e2 100644 --- a/packages/core/src/Cardano/types/Certificate.ts +++ b/packages/core/src/Cardano/types/Certificate.ts @@ -96,6 +96,7 @@ export interface MirCertificate { export interface GenesisKeyDelegationCertificate { __typename: CertificateType.GenesisKeyDelegation; + // Review: need to find examples of these hashes to figure out type and length genesisHash: Hash16; genesisDelegateHash: Hash16; vrfKeyHash: Hash16; diff --git a/packages/core/src/Cardano/types/StakePool/StakePool.ts b/packages/core/src/Cardano/types/StakePool/StakePool.ts index 8cd1c4023cd..eba6189017e 100644 --- a/packages/core/src/Cardano/types/StakePool/StakePool.ts +++ b/packages/core/src/Cardano/types/StakePool/StakePool.ts @@ -25,7 +25,7 @@ export interface Cip6MetadataFields { * the public Key for verification * optional, 68 Characters */ - extVkey?: Hash16; // TODO: figure out if this is the correct type alias + extVkey?: Hash16; // TODO: Review: need to find an example of this to verify type and length } export interface StakePoolMetadataFields { diff --git a/packages/core/src/Cardano/types/Transaction.ts b/packages/core/src/Cardano/types/Transaction.ts index 556520a71a8..ca2fab22df0 100644 --- a/packages/core/src/Cardano/types/Transaction.ts +++ b/packages/core/src/Cardano/types/Transaction.ts @@ -1,7 +1,8 @@ +/* eslint-disable @typescript-eslint/no-explicit-any */ import * as Cardano from '.'; import { AuxiliaryData } from './AuxiliaryData'; import { BlockBodyAlonzo } from '@cardano-ogmios/schema'; -import { OpaqueString, assertIsHexString } from '../util'; +import { OpaqueString, assertIsBech32WithPrefix, assertIsHexString } from '../util'; import { PartialBlockHeader } from './Block'; /** @@ -15,11 +16,36 @@ export type TransactionId = OpaqueString<'TransactionId'>; */ export const TransactionId = (value: string): TransactionId => { assertIsHexString(value, 64); - // eslint-disable-next-line @typescript-eslint/no-explicit-any return value as any as TransactionId; }; -export type Ed25519SignatureHash16 = string; +/** + * Ed25519 signature as hex string + */ +export type Ed25519Signature = OpaqueString<'Ed25519Signature'>; + +/** + * @param {string} value Ed25519 signature as hex string + * @throws InvalidStringError + */ +export const Ed25519Signature = (value: string): Ed25519Signature => { + assertIsHexString(value, 128); + return value as any as Ed25519Signature; +}; + +/** + * Ed25519 public key as bech32 string + */ +export type Ed25519PublicKey = OpaqueString<'Ed25519PublicKey'>; + +/** + * @param {string} value Ed25519 public key as bech32 string + * @throws InvalidStringError + */ +export const Ed25519PublicKey = (value: string): Ed25519PublicKey => { + assertIsBech32WithPrefix(value, 'ed25519_pk', 52); + return value as any as Ed25519PublicKey; +}; export interface Withdrawal { stakeAddress: Cardano.RewardAccount; @@ -34,8 +60,8 @@ export interface TxBodyAlonzo { withdrawals?: Withdrawal[]; certificates?: Cardano.Certificate[]; mint?: Cardano.TokenMap; - scriptIntegrityHash?: Cardano.Hash16; - requiredExtraSignatures?: Cardano.Hash16[]; + scriptIntegrityHash?: Cardano.Hash16; // TODO: Review: need to find an example of this to verify type and length + requiredExtraSignatures?: Cardano.Hash16[]; // TODO: Review: need to find an example of this to verify type and length } /** @@ -61,8 +87,11 @@ export interface Redeemer { export type Witness = Omit, 'redeemers' | 'signatures'> & { redeemers?: Redeemer[]; + /** + * Key type is Ed25519PublicKey + */ signatures: Partial<{ - [k: string]: Ed25519SignatureHash16; + [k: string]: Ed25519Signature; }>; }; diff --git a/packages/core/src/Cardano/types/Utxo.ts b/packages/core/src/Cardano/types/Utxo.ts index 1b24b9255f9..c26230ea6ea 100644 --- a/packages/core/src/Cardano/types/Utxo.ts +++ b/packages/core/src/Cardano/types/Utxo.ts @@ -10,7 +10,7 @@ export interface TxIn { export interface TxOut { address: Address; value: Value; - datum?: Hash16; + datum?: Hash16; // TODO: Review: need to find an example of this to verify type and length } export type Utxo = [TxIn, TxOut]; diff --git a/packages/core/test/Cardano/types/Transaction.test.ts b/packages/core/test/Cardano/types/Transaction.test.ts index 379481262f1..cf842fc1c4d 100644 --- a/packages/core/test/Cardano/types/Transaction.test.ts +++ b/packages/core/test/Cardano/types/Transaction.test.ts @@ -1,7 +1,22 @@ -import { TransactionId } from '../../../src/Cardano'; +import { Ed25519PublicKey, Ed25519Signature, TransactionId } from '../../../src/Cardano'; describe('Cardano/types/Transaction', () => { - it('TransactionId() accepts a valid transaction hash', () => { + it('TransactionId() accepts a valid transaction hash hex string', () => { expect(() => TransactionId('3e33018e8293d319ef5b3ac72366dd28006bd315b715f7e7cfcbd3004129b80d')).not.toThrow(); }); + + it('Ed25519Signature() accepts a valid signature hex string', () => { + expect(() => + Ed25519Signature( + // eslint-disable-next-line max-len + '709f937c4ce152c81f8406c03279ff5a8556a12a8657e40a578eaaa6223d2e6a2fece39733429e3ec73a6c798561b5c2d47d82224d656b1d964cfe8b5fdffe09' + ) + ).not.toThrow(); + }); + + it('Ed25519PublicKey() accepts a valid public key bech32 string', () => { + expect(() => + Ed25519PublicKey('ed25519_pk1vxv3s6km2xt5dyxhy37jv3sf05kxya3mwea49zqkldld8704t5usy6nye4') + ).not.toThrow(); + }); }); diff --git a/packages/golden-test-generator/src/AddressBalance/getOnChainAddressBalances.ts b/packages/golden-test-generator/src/AddressBalance/getOnChainAddressBalances.ts index b51714c5a2e..01025fd6ab9 100644 --- a/packages/golden-test-generator/src/AddressBalance/getOnChainAddressBalances.ts +++ b/packages/golden-test-generator/src/AddressBalance/getOnChainAddressBalances.ts @@ -1,20 +1,20 @@ /* eslint-disable complexity */ -import { dummyLogger, Logger } from 'ts-log'; import { - createChainSyncClient, + ChainSync, + ConnectionConfig, + Schema, StateQuery, + createChainSyncClient, + createInteractionContext, isAllegraBlock, isAlonzoBlock, - isShelleyBlock, isMaryBlock, - Schema, - ConnectionConfig, - createInteractionContext, - ChainSync + isShelleyBlock } from '@cardano-ogmios/client'; import { GeneratorMetadata } from '../Content'; -import { isByronStandardBlock } from '../util'; +import { Logger, dummyLogger } from 'ts-log'; import { applyValue } from './applyValue'; +import { isByronStandardBlock } from '../util'; export type AddressBalances = { [address: string]: Schema.Value; @@ -37,6 +37,7 @@ export const getOnChainAddressBalances = ( const trackedAddressBalances: AddressBalances = Object.fromEntries( addresses.map((address) => [address, { assets: {}, coins: 0 }]) ); + // TODO: Review: should this be refactored too? Haven't done anything with golden-test-generator yet const trackedTxs: ({ id: Schema.Hash16 } & Schema.Tx)[] = []; // eslint-disable-next-line sonarjs/cognitive-complexity return new Promise(async (resolve, reject) => { diff --git a/packages/wallet/src/KeyManagement/InMemoryKeyManager.ts b/packages/wallet/src/KeyManagement/InMemoryKeyManager.ts index 05a8c79a2ba..be140926ce4 100644 --- a/packages/wallet/src/KeyManagement/InMemoryKeyManager.ts +++ b/packages/wallet/src/KeyManagement/InMemoryKeyManager.ts @@ -80,11 +80,12 @@ export const createInMemoryKeyManager = ({ } const stakeVkeyWitness = CSL.make_vkey_witness(cslHash, privateStakeKeyRaw); return { - [publicStakeKeyRawBech32]: stakeVkeyWitness.signature().to_hex() + [publicStakeKeyRawBech32]: Cardano.Ed25519Signature(stakeVkeyWitness.signature().to_hex()) }; })(); + const paymentVkeyWitnessHex = Cardano.Ed25519Signature(paymentVkeyWitness.signature().to_hex()); return { - [publicParentPaymentKeyRawBech32]: paymentVkeyWitness.signature().to_hex(), + [publicParentPaymentKeyRawBech32]: paymentVkeyWitnessHex, ...stakeWitnesses }; }