Skip to content

Commit

Permalink
feat(core): added fromCredential and toNetworkId util functions to th…
Browse files Browse the repository at this point in the history
…e RewardAccount type
  • Loading branch information
AngelCastilloB committed Jan 19, 2024
1 parent 03e84bb commit a515431
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 1 deletion.
22 changes: 21 additions & 1 deletion packages/core/src/Cardano/Address/RewardAccount.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Address, CredentialType } from './Address';
import { Address, Credential, CredentialType } from './Address';
import { Ed25519KeyHashHex, Hash28ByteBase16 } from '@cardano-sdk/crypto';
import { NetworkId } from '../ChainId';
import { OpaqueString, typedBech32 } from '@cardano-sdk/util';
Expand All @@ -15,6 +15,26 @@ export const RewardAccount = (value: string): RewardAccount => typedBech32(value
RewardAccount.toHash = (rewardAccount: RewardAccount): Ed25519KeyHashHex =>
Ed25519KeyHashHex(Address.fromBech32(rewardAccount).asReward()!.getPaymentCredential().hash);

/**
* Creates a reward account from a given credential and network id.
*
* @param credential The credential.
* @param networkId The network id.
*/
RewardAccount.fromCredential = (credential: Credential, networkId: NetworkId): RewardAccount =>
RewardAccount(
RewardAddress.fromCredentials(networkId, { hash: credential.hash, type: credential.type }).toAddress().toBech32()
);

/**
* Returns the network id encoded in the given reward account.
*
* @param rewardAccount The reward account.
* @returns The network id.
*/
RewardAccount.toNetworkId = (rewardAccount: RewardAccount): NetworkId =>
Address.fromBech32(rewardAccount).asReward()!.toAddress().getNetworkId();

/**
* Creates a reward account from a given key hash and network id.
*
Expand Down
19 changes: 19 additions & 0 deletions packages/core/test/Cardano/Address/RewardAccount.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import * as Crypto from '@cardano-sdk/crypto';
import * as cip19TestVectors from '../../../../util-dev/src/Cip19TestVectors';
import { Cardano } from '../../../src';
import { typedBech32 } from '@cardano-sdk/util';

Expand Down Expand Up @@ -40,4 +41,22 @@ describe('Cardano/Address/RewardAccount', () => {
expect(rewardAccount.startsWith('stake_test')).toBe(true);
});
});

describe('fromCredential', () => {
it('creates can create a reward account given a credential and a network id', () => {
const rewardAccount = Cardano.RewardAccount.fromCredential(
cip19TestVectors.KEY_STAKE_CREDENTIAL,
Cardano.NetworkId.Mainnet
);
expect(rewardAccount).toBe(cip19TestVectors.rewardKey);
});
});

describe('toNetworkId', () => {
it('get the correct network id from a reward account', () => {
expect(Cardano.RewardAccount.toNetworkId(Cardano.RewardAccount(cip19TestVectors.rewardKey))).toBe(
Cardano.NetworkId.Mainnet
);
});
});
});

0 comments on commit a515431

Please sign in to comment.