import { Bip32Path } from "@iota/crypto.js" ; import { Converter } from "@iota/util.js" ; import { Ed25519Address } from "../addressTypes/ed25519Address" ; import { SingleNodeClient } from "../clients/singleNodeClient" ; import type { IBip44GeneratorState } from "../models/IBip44GeneratorState" ; import type { IClient } from "../models/IClient" ; import { ED25519_ADDRESS_TYPE } from "../models/IEd25519Address" ; import type { ISeed } from "../models/ISeed" ; import { Bech32Helper } from "../utils/bech32Helper" ; import { generateBip44Address } from "./addresses" ; param client param seed param accountIndex param addressOptions param addressOptions . startIndex param addressOptions . zeroCount param addressOptions . requiredCount returns export async function getUnspentAddresses ( client , seed , accountIndex , addressOptions ? ) { return getUnspentAddressesWithAddressGenerator < IBip44GeneratorState > ( client , seed , { accountIndex , addressIndex : addressOptions ?. startIndex ?? 0 , isInternal : false } , generateBip44Address , addressOptions ) ; } param client param seed param initialAddressState param nextAddressPath param addressOptions param addressOptions . startIndex param addressOptions . zeroCount param addressOptions . requiredCount returns export async function getUnspentAddressesWithAddressGenerator < T > ( client , seed , initialAddressState , nextAddressPath , addressOptions ? ) { const localClient = typeof client === "string" ? new SingleNodeClient ( client ) : client ; const nodeInfo = await localClient . info ( ) ; const localRequiredLimit = addressOptions ?. requiredCount ?? Number . MAX_SAFE_INTEGER ; const localZeroCount = addressOptions ?. zeroCount ?? 20 ; let finished = false ; const allUnspent = [ ] ; let zeroBalance = 0 ; do { const path = nextAddressPath ( initialAddressState ) ; const addressSeed = seed . generateSeedFromPath ( new Bip32Path ( path ) ) ; const ed25519Address = new Ed25519Address ( addressSeed . keyPair ( ) . publicKey ) ; const addressBytes = ed25519Address . toAddress ( ) ; const addressHex = Converter . bytesToHex ( addressBytes ) ; const addressResponse = await localClient . addressEd25519 ( addressHex ) ; if ( addressResponse . balance === 0 ) { zeroBalance ++ ; if ( zeroBalance >= localZeroCount ) { finished = true ; } } else { allUnspent . push ( { address : Bech32Helper . toBech32 ( ED25519_ADDRESS_TYPE , addressBytes , nodeInfo . bech32HRP ) , path , balance : addressResponse . balance } ) ; if ( allUnspent . length === localRequiredLimit ) { finished = true ; } } } while ( ! finished ) ; return allUnspent ; }