Skip to content

Commit

Permalink
refactor: add Ed25519Signature and Ed25519PublicKey types
Browse files Browse the repository at this point in the history
  • Loading branch information
mkazlauskas committed Nov 24, 2021
1 parent 62c2a07 commit b2f5d77
Show file tree
Hide file tree
Showing 9 changed files with 69 additions and 22 deletions.
2 changes: 1 addition & 1 deletion packages/core/src/CSL/coreToCsl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/Cardano/types/AuxiliaryData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
1 change: 1 addition & 0 deletions packages/core/src/Cardano/types/Certificate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/Cardano/types/StakePool/StakePool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
41 changes: 35 additions & 6 deletions packages/core/src/Cardano/types/Transaction.ts
Original file line number Diff line number Diff line change
@@ -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';

/**
Expand All @@ -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;
Expand All @@ -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
}

/**
Expand All @@ -61,8 +87,11 @@ export interface Redeemer {

export type Witness = Omit<Partial<BlockBodyAlonzo['witness']>, 'redeemers' | 'signatures'> & {
redeemers?: Redeemer[];
/**
* Key type is Ed25519PublicKey
*/
signatures: Partial<{
[k: string]: Ed25519SignatureHash16;
[k: string]: Ed25519Signature;
}>;
};

Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/Cardano/types/Utxo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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];
19 changes: 17 additions & 2 deletions packages/core/test/Cardano/types/Transaction.test.ts
Original file line number Diff line number Diff line change
@@ -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();
});
});
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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) => {
Expand Down
5 changes: 3 additions & 2 deletions packages/wallet/src/KeyManagement/InMemoryKeyManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
};
}
Expand Down

0 comments on commit b2f5d77

Please sign in to comment.