From a51543106396348be130cfbbc61cdf05f67ac7d6 Mon Sep 17 00:00:00 2001 From: Angel Castillo Date: Wed, 17 Jan 2024 06:05:11 +0800 Subject: [PATCH] feat(core): added fromCredential and toNetworkId util functions to the RewardAccount type --- .../core/src/Cardano/Address/RewardAccount.ts | 22 ++++++++++++++++++- .../Cardano/Address/RewardAccount.test.ts | 19 ++++++++++++++++ 2 files changed, 40 insertions(+), 1 deletion(-) diff --git a/packages/core/src/Cardano/Address/RewardAccount.ts b/packages/core/src/Cardano/Address/RewardAccount.ts index 447f11d71df..af74433978a 100644 --- a/packages/core/src/Cardano/Address/RewardAccount.ts +++ b/packages/core/src/Cardano/Address/RewardAccount.ts @@ -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'; @@ -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. * diff --git a/packages/core/test/Cardano/Address/RewardAccount.test.ts b/packages/core/test/Cardano/Address/RewardAccount.test.ts index 7eef8febe67..98a034045df 100644 --- a/packages/core/test/Cardano/Address/RewardAccount.test.ts +++ b/packages/core/test/Cardano/Address/RewardAccount.test.ts @@ -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'; @@ -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 + ); + }); + }); });