import { Buffer } from '@stacks/common' ; import { LengthPrefixedString , createAddress , createLPString } from '../../postcondition-types' ; import { Address , addressToString } from '../../common' ; import { ClarityType } from '../constants' ; type PrincipalCV = StandardPrincipalCV | ContractPrincipalCV ; interface StandardPrincipalCV { readonly type : ClarityType . PrincipalStandard ; readonly address : Address ; } interface ContractPrincipalCV { readonly type : ClarityType . PrincipalContract ; readonly address : Address ; readonly contractName : LengthPrefixedString ; } function principalToString ( principal ) { if ( principal . type === ClarityType . PrincipalStandard ) { return addressToString ( principal . address ) ; } else if ( principal . type === ClarityType . PrincipalContract ) { const address = addressToString ( principal . address ) ; return `${ address }.${ principal . contractName . content }` ; } else { throw new Error ( `Unexpected principal data: ${ JSON . stringify ( principal ) }` ) ; } } function principalCV ( principal ) { if ( principal . includes ( '.' ) ) { const [ address , contractName ] = principal . split ( '.' ) ; return contractPrincipalCV ( address , contractName ) ; } else { return standardPrincipalCV ( principal ) ; } } function standardPrincipalCV ( addressString ) { const addr = createAddress ( addressString ) ; return { type : ClarityType . PrincipalStandard , address : addr } ; } function standardPrincipalCVFromAddress ( address ) { return { type : ClarityType . PrincipalStandard , address } ; } function contractPrincipalCV ( addressString , contractName ) { const addr = createAddress ( addressString ) ; const lengthPrefixedContractName = createLPString ( contractName ) ; return contractPrincipalCVFromAddress ( addr , lengthPrefixedContractName ) ; } function contractPrincipalCVFromAddress ( address , contractName ) { if ( Buffer . byteLength ( contractName . content ) >= 128 ) { throw new Error ( 'Contract name must be less than 128 bytes' ) ; } return { type : ClarityType . PrincipalContract , address , contractName } ; } function contractPrincipalCVFromStandard ( sp , contractName ) { const lengthPrefixedContractName = createLPString ( contractName ) ; return { type : ClarityType . PrincipalContract , address : sp . address , contractName : lengthPrefixedContractName , } ; } export { PrincipalCV , StandardPrincipalCV , ContractPrincipalCV , principalCV , principalToString , standardPrincipalCV , standardPrincipalCVFromAddress , contractPrincipalCV , contractPrincipalCVFromAddress , contractPrincipalCVFromStandard , } ;