All files / src/Wallets internals.ts

100% Statements 10/10
100% Branches 0/0
100% Functions 1/1
100% Lines 9/9

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44  35x 35x                   35x         24x 24x   24x         24x         24x   24x                      
import * as Crypto from '@cardano-sdk/crypto';
import { AddressType, GroupedAddress } from '@cardano-sdk/key-management';
import { Cardano, nativeScriptPolicyId } from '@cardano-sdk/core';
 
/**
 * Method to derive the wallet's grouped address from the script and network ID.
 *
 * @param {Cardano.NativeScript} paymentScript - The native script to be used for the payment credential.
 * @param {Cardano.NativeScript} stakingScript - The native script to be used for the staking credential.
 * @param {Cardano.NetworkId} networkId - The network identifier.
 * @returns {}GroupedAddress} The derived grouped address.
 */
export const getScriptAddress = (
  paymentScript: Cardano.NativeScript,
  stakingScript: Cardano.NativeScript,
  networkId: Cardano.NetworkId
): GroupedAddress => {
  const paymentScriptHash = nativeScriptPolicyId(paymentScript) as unknown as Crypto.Hash28ByteBase16;
  const stakingScriptHash = nativeScriptPolicyId(stakingScript) as unknown as Crypto.Hash28ByteBase16;
 
  const paymentScriptCredential = {
    hash: paymentScriptHash,
    type: Cardano.CredentialType.ScriptHash
  };
 
  const stakeScriptCredential = {
    hash: stakingScriptHash,
    type: Cardano.CredentialType.ScriptHash
  };
 
  const baseAddress = Cardano.BaseAddress.fromCredentials(networkId, paymentScriptCredential, stakeScriptCredential);
 
  return {
    accountIndex: 0,
    address: baseAddress.toAddress().toBech32() as Cardano.PaymentAddress,
    index: 0,
    networkId,
    rewardAccount: Cardano.RewardAddress.fromCredentials(networkId, stakeScriptCredential)
      .toAddress()
      .toBech32() as Cardano.RewardAccount,
    type: AddressType.External
  };
};