All files / src/Wallets util.ts

90% Statements 18/20
77.77% Branches 7/9
100% Functions 4/4
87.5% Lines 14/16

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 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 6136x 36x               36x   36x   36x 4x       4x                               36x 120x               120x     36x 2x         2x           2x    
import { AddressDiscovery, HDSequentialDiscovery } from '../services';
import {
  BaseWallet,
  BaseWalletDependencies,
  BaseWalletProps,
  PublicCredentialsManager,
  PublicCredentialsManagerType
} from './BaseWallet';
import { Bip32Account } from '@cardano-sdk/key-management';
import { Cardano } from '@cardano-sdk/core';
 
export const DEFAULT_LOOK_AHEAD_SEARCH = 20;
 
export const isValidSharedWalletScript = (script: Cardano.NativeScript): boolean => {
  switch (script.kind) {
    case Cardano.NativeScriptKind.RequireAllOf:
    case Cardano.NativeScriptKind.RequireAnyOf:
    case Cardano.NativeScriptKind.RequireNOf:
      return script.scripts.every((nativeScript) => nativeScript.kind === Cardano.NativeScriptKind.RequireSignature);
    default:
      return false;
  }
};
 
export type PersonalWalletDependencies = Omit<BaseWalletDependencies, 'publicCredentialsManager'> & {
  bip32Account: Bip32Account;
  addressDiscovery?: AddressDiscovery;
};
 
export type ScriptWalletDependencies = Omit<BaseWalletDependencies, 'publicCredentialsManager'> & {
  paymentScript: Cardano.RequireAllOfScript | Cardano.RequireAnyOfScript | Cardano.RequireAtLeastScript;
  stakingScript: Cardano.RequireAllOfScript | Cardano.RequireAnyOfScript | Cardano.RequireAtLeastScript;
};
 
export const createPersonalWallet = (props: BaseWalletProps, dependencies: PersonalWalletDependencies): BaseWallet => {
  const publicCredentialsManager: PublicCredentialsManager = {
    __type: PublicCredentialsManagerType.BIP32_CREDENTIALS_MANAGER,
    addressDiscovery: dependencies.addressDiscovery
      ? dependencies.addressDiscovery
      : new HDSequentialDiscovery(dependencies.chainHistoryProvider, DEFAULT_LOOK_AHEAD_SEARCH),
    bip32Account: dependencies.bip32Account
  };
 
  return new BaseWallet(props, { ...dependencies, publicCredentialsManager });
};
 
export const createSharedWallet = (props: BaseWalletProps, dependencies: ScriptWalletDependencies): BaseWallet => {
  Iif (!isValidSharedWalletScript(dependencies.paymentScript) || !isValidSharedWalletScript(dependencies.stakingScript))
    throw new Error(
      'SharedWallet requires the scripts to be of type "RequireAllOfScript" or "RequireAnyOfScript" or "RequireAtLeastScript"'
    );
 
  const publicCredentialsManager: PublicCredentialsManager = {
    __type: PublicCredentialsManagerType.SCRIPT_CREDENTIALS_MANAGER,
    paymentScript: dependencies.paymentScript,
    stakingScript: dependencies.stakingScript
  };
 
  return new BaseWallet(props, { ...dependencies, publicCredentialsManager });
};