import { bigFromBufBE , bigToBufBE } from "@node-lightning/bufio" ; param num param mod export function divmod ( num , mod ) { return [ num / mod , num % mod ] ; } /**
 * Base58 encoding and decoding utility
 */ export class Base58 { public static alphabet : string = "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz" ; param buf public static encode ( buf ) { let prefix = "" ; for ( let i = 0 ; i < buf . length ; i ++ ) { if ( buf [ i ] === 0 ) prefix += "1" ; else break ; } let num = bigFromBufBE ( buf ) ; let result = "" ; while ( num > 0n ) { const [ n , mod ] = divmod ( num , 58n ) ; result = Base58 . alphabet [ Number ( mod ) ] + result ; num = n ; } return prefix + result ; } param input public static decode ( input ) { let prefix = 0 ; for ( let i = 0 ; i < input . length ; i ++ ) { if ( input [ i ] === "1" ) prefix += 1 ; else break ; } let num = 0n ; for ( const char of input ) { num *= 58n ; num += BigInt ( Base58 . alphabet . indexOf ( char ) ) ; } return Buffer . concat ( [ Buffer . alloc ( prefix ) , bigToBufBE ( num ) ] ) ; } }