import CardanoWasm , { Address , ByronAddress , StakeCredential } from 'cardano-serialization-lib' ; import { Logger } from 'fastify' ; import { NetworkIdentifier , EraAddressType , AddressPrefix , StakeAddressPrefix , NonStakeAddressPrefix , PREFIX_LENGTH } from '../constants' ; import { hexStringToBuffer } from '../formatters' ; param network param addressPrefix export const getAddressPrefix = ( network , addressPrefix = NonStakeAddressPrefix ) : string => network === NetworkIdentifier . CARDANO_MAINNET_NETWORK ? addressPrefix . MAIN : addressPrefix . TEST ; param logger param network param paymentCredential export const generateRewardAddress = ( logger , network , paymentCredential ) : string => { logger . info ( '[generateRewardAddress] Deriving cardano reward address from valid public staking key' ) ; const rewardAddress = CardanoWasm . RewardAddress . new ( network , paymentCredential ) ; const bech32address = rewardAddress . to_address ( ) . to_bech32 ( getAddressPrefix ( network , StakeAddressPrefix ) ) ; logger . info ( `[generateRewardAddress] reward address is ${ bech32address }` ) ; return bech32address ; } ; param logger param network param paymentCredential param stakingCredential export const generateBaseAddress = ( logger , network , paymentCredential , stakingCredential ) : string => { logger . info ( '[generateAddress] Deriving cardano address from valid public key and staking key' ) ; const baseAddress = CardanoWasm . BaseAddress . new ( network , paymentCredential , stakingCredential ) ; const bech32address = baseAddress . to_address ( ) . to_bech32 ( getAddressPrefix ( network ) ) ; logger . info ( `[generateAddress] base address is ${ bech32address }` ) ; return bech32address ; } ; param logger param network param paymentCredential export const generateEnterpriseAddress = ( logger , network , paymentCredential ) : string => { logger . info ( '[generateAddress] Deriving cardano enterprise address from valid public key' ) ; const enterpriseAddress = CardanoWasm . EnterpriseAddress . new ( network , paymentCredential ) ; const bech32enterpriseAddress = enterpriseAddress . to_address ( ) . to_bech32 ( getAddressPrefix ( network ) ) ; logger . info ( `[generateAddress] enterprise address is ${ bech32enterpriseAddress }` ) ; return bech32enterpriseAddress ; } ; param address export const getEraAddressType = ( address ) : EraAddressType => { if ( CardanoWasm . ByronAddress . is_valid ( address ) ) { return EraAddressType . Byron ; } CardanoWasm . Address . from_bech32 ( address ) ; return EraAddressType . Shelley ; } ; param address export const generateAddress = ( address ) : Address => { const addressType = getEraAddressType ( address ) ; if ( addressType === EraAddressType . Byron ) { const byronAddress = ByronAddress . from_base58 ( address ) ; return byronAddress . to_address ( ) ; } return Address . from_bech32 ( address ) ; } ; param address param addressPrefix export const parseAddress = ( address , addressPrefix ? ) : string => { const byronAddress = ByronAddress . from_address ( address ) ; return byronAddress ? byronAddress . to_base58 ( ) : address . to_bech32 ( addressPrefix ) ; } ; param address export const parseToRewardAddress = ( address ) : CardanoWasm . RewardAddress | undefined => { const wasmAddress = CardanoWasm . Address . from_bech32 ( address ) ; return CardanoWasm . RewardAddress . from_address ( wasmAddress ) ; } ; param hex export const getAddressFromHexString = ( hex ) : Address => Address . from_bytes ( hexStringToBuffer ( hex ) ) ; param address export const isStakeAddress = ( address ) : boolean => { const addressPrefix = address . slice ( 0 , PREFIX_LENGTH ) ; return [ StakeAddressPrefix . MAIN as string , StakeAddressPrefix . TEST as string ] . some ( type => addressPrefix . includes ( type ) ) ; } ;