import { Converter } from "@iota/util.js" ; import type { ITipsResponse } from "../models/api/ITipsResponse" ; import { ED25519_ADDRESS_TYPE , IEd25519Address } from "../models/IEd25519Address" ; import { ED25519_SIGNATURE_TYPE , IEd25519Signature } from "../models/IEd25519Signature" ; import type { IIndexationPayload } from "../models/IIndexationPayload" ; import { INDEXATION_PAYLOAD_TYPE } from "../models/IIndexationPayload" ; import type { IMessage } from "../models/IMessage" ; import type { IMessageMetadata } from "../models/IMessageMetadata" ; import type { IMigratedFunds } from "../models/IMigratedFunds" ; import { IMilestonePayload , MILESTONE_PAYLOAD_TYPE } from "../models/IMilestonePayload" ; import type { INodeInfo } from "../models/INodeInfo" ; import { IReceiptPayload , RECEIPT_PAYLOAD_TYPE } from "../models/IReceiptPayload" ; import { IReferenceUnlockBlock , REFERENCE_UNLOCK_BLOCK_TYPE } from "../models/IReferenceUnlockBlock" ; import { ISigLockedDustAllowanceOutput , SIG_LOCKED_DUST_ALLOWANCE_OUTPUT_TYPE } from "../models/ISigLockedDustAllowanceOutput" ; import { ISigLockedSingleOutput , SIG_LOCKED_SINGLE_OUTPUT_TYPE } from "../models/ISigLockedSingleOutput" ; import { ISignatureUnlockBlock , SIGNATURE_UNLOCK_BLOCK_TYPE } from "../models/ISignatureUnlockBlock" ; import { TRANSACTION_ESSENCE_TYPE } from "../models/ITransactionEssence" ; import { ITransactionPayload , TRANSACTION_PAYLOAD_TYPE } from "../models/ITransactionPayload" ; import { ITreasuryInput , TREASURY_INPUT_TYPE } from "../models/ITreasuryInput" ; import { ITreasuryOutput , TREASURY_OUTPUT_TYPE } from "../models/ITreasuryOutput" ; import { ITreasuryTransactionPayload , TREASURY_TRANSACTION_PAYLOAD_TYPE } from "../models/ITreasuryTransactionPayload" ; import { IUTXOInput , UTXO_INPUT_TYPE } from "../models/IUTXOInput" ; param message param data returns let logger = ( message , data ) => data !== undefined ? console . log ( message , data ) : console . log ( message ) ; param log export function setLogger ( log ) { logger = log ; } param prefix param info export function logInfo ( prefix , info ) { logger ( `${ prefix }\tName:` , info . name ) ; logger ( `${ prefix }\tVersion:` , info . version ) ; logger ( `${ prefix }\tNetwork Id:` , info . networkId ) ; logger ( `${ prefix }\tIs Healthy:` , info . isHealthy ) ; logger ( `${ prefix }\tMin PoW Score:` , info . minPoWScore ) ; logger ( `${ prefix }\tBech32 HRP:` , info . bech32HRP ) ; logger ( `${ prefix }\tLatest Milestone Index:` , info . latestMilestoneIndex ) ; logger ( `${ prefix }\tLatest Milestone Timestamp:` , info . latestMilestoneTimestamp ) ; logger ( `${ prefix }\tConfirmed Milestone Index:` , info . confirmedMilestoneIndex ) ; logger ( `${ prefix }\tMessages Per Second:` , info . messagesPerSecond ) ; logger ( `${ prefix }\tReferenced Messages Per Second:` , info . referencedMessagesPerSecond ) ; logger ( `${ prefix }\tReferenced Rate:` , info . referencedRate ) ; logger ( `${ prefix }\tPruning Index:` , info . pruningIndex ) ; logger ( `${ prefix }\tFeatures:` , info . features ) ; } param prefix param tipsResponse export function logTips ( prefix , tipsResponse ) { if ( tipsResponse . tipMessageIds ) { for ( let i = 0 ; i < tipsResponse . tipMessageIds . length ; i ++ ) { logger ( `${ prefix }\tTip ${ i + 1 } Message Id:` , tipsResponse . tipMessageIds [ i ] ) ; } } } param prefix param message export function logMessage ( prefix , message ) { logger ( `${ prefix }\tNetwork Id:` , message . networkId ) ; if ( message . parentMessageIds ) { for ( let i = 0 ; i < message . parentMessageIds . length ; i ++ ) { logger ( `${ prefix }\tParent ${ i + 1 } Message Id:` , message . parentMessageIds [ i ] ) ; } } logPayload ( `${ prefix }\t` , message . payload ) ; if ( message . nonce !== undefined ) { logger ( `${ prefix }\tNonce:` , message . nonce ) ; } } param prefix param messageMetadata export function logMessageMetadata ( prefix , messageMetadata ) { logger ( `${ prefix }\tMessage Id:` , messageMetadata . messageId ) ; if ( messageMetadata . parentMessageIds ) { for ( let i = 0 ; i < messageMetadata . parentMessageIds . length ; i ++ ) { logger ( `${ prefix }\tParent ${ i + 1 } Message Id:` , messageMetadata . parentMessageIds [ i ] ) ; } } if ( messageMetadata . isSolid !== undefined ) { logger ( `${ prefix }\tIs Solid:` , messageMetadata . isSolid ) ; } if ( messageMetadata . milestoneIndex !== undefined ) { logger ( `${ prefix }\tMilestone Index:` , messageMetadata . milestoneIndex ) ; } if ( messageMetadata . referencedByMilestoneIndex !== undefined ) { logger ( `${ prefix }\tReferenced By Milestone Index:` , messageMetadata . referencedByMilestoneIndex ) ; } logger ( `${ prefix }\tLedger Inclusion State:` , messageMetadata . ledgerInclusionState ) ; if ( messageMetadata . conflictReason !== undefined ) { logger ( `${ prefix }\tConflict Reason:` , messageMetadata . conflictReason ) ; } if ( messageMetadata . shouldPromote !== undefined ) { logger ( `${ prefix }\tShould Promote:` , messageMetadata . shouldPromote ) ; } if ( messageMetadata . shouldReattach !== undefined ) { logger ( `${ prefix }\tShould Reattach:` , messageMetadata . shouldReattach ) ; } } param prefix param unknownPayload export function logPayload ( prefix , unknownPayload ? ) { if ( unknownPayload ) { if ( unknownPayload . type === TRANSACTION_PAYLOAD_TYPE ) { logTransactionPayload ( prefix , unknownPayload ) ; } else if ( unknownPayload . type === MILESTONE_PAYLOAD_TYPE ) { logMilestonePayload ( prefix , unknownPayload ) ; } else if ( unknownPayload . type === INDEXATION_PAYLOAD_TYPE ) { logIndexationPayload ( prefix , unknownPayload ) ; } else if ( unknownPayload . type === RECEIPT_PAYLOAD_TYPE ) { logReceiptPayload ( prefix , unknownPayload ) ; } else if ( unknownPayload . type === TREASURY_TRANSACTION_PAYLOAD_TYPE ) { logTreasuryTransactionPayload ( prefix , unknownPayload ) ; } } } param prefix param payload export function logTransactionPayload ( prefix , payload ? ) { if ( payload ) { logger ( `${ prefix }Transaction Payload` ) ; if ( payload . essence . type === TRANSACTION_ESSENCE_TYPE ) { if ( payload . essence . inputs ) { logger ( `${ prefix }\tInputs:` , payload . essence . inputs . length ) ; for ( const input of payload . essence . inputs ) { logInput ( `${ prefix }\t\t` , input ) ; } } if ( payload . essence . outputs ) { logger ( `${ prefix }\tOutputs:` , payload . essence . outputs . length ) ; for ( const output of payload . essence . outputs ) { logOutput ( `${ prefix }\t\t` , output ) ; } } } if ( payload . unlockBlocks ) { logger ( `${ prefix }\tUnlock Blocks:` , payload . unlockBlocks . length ) ; for ( const unlockBlock of payload . unlockBlocks ) { logUnlockBlock ( `${ prefix }\t\t` , unlockBlock ) ; } } } } param prefix param payload export function logIndexationPayload ( prefix , payload ? ) { if ( payload ) { logger ( `${ prefix }Indexation Payload` ) ; logger ( `${ prefix }\tIndex:` , Converter . hexToUtf8 ( payload . index ) ) ; logger ( `${ prefix }\tData:` , payload . data ? Converter . hexToUtf8 ( payload . data ) : "None" ) ; } } param prefix param payload export function logMilestonePayload ( prefix , payload ? ) { if ( payload ) { logger ( `${ prefix }Milestone Payload` ) ; logger ( `${ prefix }\tIndex:` , payload . index ) ; logger ( `${ prefix }\tTimestamp:` , payload . timestamp ) ; for ( let i = 0 ; i < payload . parentMessageIds . length ; i ++ ) { logger ( `${ prefix }\tParent ${ i + 1 }:` , payload . parentMessageIds [ i ] ) ; } logger ( `${ prefix }\tInclusion Merkle Proof:` , payload . inclusionMerkleProof ) ; if ( payload . nextPoWScore ) { logger ( `${ prefix }\tNext PoW Score:` , payload . nextPoWScore ) ; } if ( payload . nextPoWScoreMilestoneIndex ) { logger ( `${ prefix }\tNext PoW Score Milestone Index:` , payload . nextPoWScoreMilestoneIndex ) ; } logger ( `${ prefix }\tPublic Keys:` , payload . publicKeys ) ; logger ( `${ prefix }\tSignatures:` , payload . signatures ) ; logReceiptPayload ( `${ prefix }\t` , payload . receipt ) ; } } param prefix param payload export function logReceiptPayload ( prefix , payload ? ) { if ( payload ) { logger ( `${ prefix }Receipt Payload` ) ; logger ( `${ prefix }\tMigrated At:` , payload . migratedAt ) ; logger ( `${ prefix }\tFinal:` , payload . final ) ; logger ( `${ prefix }\tFunds:` , payload . funds . length ) ; for ( const funds of payload . funds ) { logFunds ( `${ prefix }\t\t` , funds ) ; } logTreasuryTransactionPayload ( `${ prefix }\t\t` , payload . transaction ) ; } } param prefix param payload export function logTreasuryTransactionPayload ( prefix , payload ? ) { if ( payload ) { logger ( `${ prefix }Treasury Transaction Payload` ) ; logInput ( prefix , payload . input ) ; logOutput ( prefix , payload . output ) ; } } param prefix param unknownAddress export function logAddress ( prefix , unknownAddress ? ) { if ( unknownAddress ?. type === ED25519_ADDRESS_TYPE ) { const address = unknownAddress ; logger ( `${ prefix }Ed25519 Address` ) ; logger ( `${ prefix }\tAddress:` , address . address ) ; } } param prefix param unknownSignature export function logSignature ( prefix , unknownSignature ? ) { if ( unknownSignature ?. type === ED25519_SIGNATURE_TYPE ) { const signature = unknownSignature ; logger ( `${ prefix }Ed25519 Signature` ) ; logger ( `${ prefix }\tPublic Key:` , signature . publicKey ) ; logger ( `${ prefix }\tSignature:` , signature . signature ) ; } } param prefix param unknownInput export function logInput ( prefix , unknownInput ? ) { if ( unknownInput ) { if ( unknownInput . type === UTXO_INPUT_TYPE ) { const input = unknownInput ; logger ( `${ prefix }UTXO Input` ) ; logger ( `${ prefix }\tTransaction Id:` , input . transactionId ) ; logger ( `${ prefix }\tTransaction Output Index:` , input . transactionOutputIndex ) ; } else if ( unknownInput . type === TREASURY_INPUT_TYPE ) { const input = unknownInput ; logger ( `${ prefix }Treasury Input` ) ; logger ( `${ prefix }\tMilestone Hash:` , input . milestoneId ) ; } } } param prefix param unknownOutput export function logOutput ( prefix , unknownOutput ? ) { if ( unknownOutput ) { if ( unknownOutput . type === SIG_LOCKED_SINGLE_OUTPUT_TYPE ) { const output = unknownOutput ; logger ( `${ prefix }Signature Locked Single Output` ) ; logAddress ( `${ prefix }\t\t` , output . address ) ; logger ( `${ prefix }\t\tAmount:` , output . amount ) ; } else if ( unknownOutput . type === SIG_LOCKED_DUST_ALLOWANCE_OUTPUT_TYPE ) { const output = unknownOutput ; logger ( `${ prefix }Signature Locked Dust Allowance Output` ) ; logAddress ( `${ prefix }\t\t` , output . address ) ; logger ( `${ prefix }\t\tAmount:` , output . amount ) ; } else if ( unknownOutput . type === TREASURY_OUTPUT_TYPE ) { const output = unknownOutput ; logger ( `${ prefix }Treasury Output` ) ; logger ( `${ prefix }\t\tAmount:` , output . amount ) ; } } } param prefix param unknownUnlockBlock export function logUnlockBlock ( prefix , unknownUnlockBlock ? ) { if ( unknownUnlockBlock ) { if ( unknownUnlockBlock . type === SIGNATURE_UNLOCK_BLOCK_TYPE ) { const unlockBlock = unknownUnlockBlock ; logger ( `${ prefix }\tSignature Unlock Block` ) ; logSignature ( `${ prefix }\t\t` , unlockBlock . signature ) ; } else if ( unknownUnlockBlock . type === REFERENCE_UNLOCK_BLOCK_TYPE ) { const unlockBlock = unknownUnlockBlock ; logger ( `${ prefix }\tReference Unlock Block` ) ; logger ( `${ prefix }\t\tReference:` , unlockBlock . reference ) ; } } } param prefix param fund export function logFunds ( prefix , fund ? ) { if ( fund ) { logger ( `${ prefix }\tFund` ) ; logger ( `${ prefix }\t\tTail Transaction Hash:` , fund . tailTransactionHash ) ; logAddress ( `${ prefix }\t\t` , fund . address ) ; logger ( `${ prefix }\t\tDeposit:` , fund . deposit ) ; } }