const _mask = 31; const _shift = 5 export function decodeBase32(address: string): Buffer{ return fromBase32String(address); } function charToInt(c: string): number{ switch (c) { case 'A': return 0; case 'B': return 1; case 'C': return 2; case 'D': return 3; case 'E': return 4; case 'F': return 5; case 'G': return 6; case 'H': return 7; case 'I': return 8; case 'J': return 9; case 'K': return 10; case 'L': return 11; case 'M': return 12; case 'N': return 13; case 'O': return 14; case 'P': return 15; case 'Q': return 16; case 'R': return 17; case 'S': return 18; case 'T': return 19; case 'U': return 20; case 'V': return 21; case 'W': return 22; case 'X': return 23; case 'Y': return 24; case 'Z': return 25; case '2': return 26; case '3': return 27; case '4': return 28; case '5': return 29; case '6': return 30; case '7': return 31; } return -1; } function fromBase32String(encoded: string): Buffer{ if(encoded == null) { throw Error("Invalid data"); }; encoded = encoded.trim().toUpperCase(); if(encoded.length == 0){ return Buffer.alloc(0); } let outLength = encoded.length * _shift /8; let result = Buffer.alloc(outLength); let buffer = 0; let next = 0; let bitsLeft = 0; let charValue = 0; for(let c of encoded){ charValue = charToInt(c); if(charValue < 0) { throw Error("Illegal character: '" + c + "'"); } buffer <<= _shift; buffer |= charValue & _mask; bitsLeft += _shift; if(bitsLeft >= 8){ result[next++] = buffer >> (bitsLeft - 8); bitsLeft -=8; } } return result; } export interface EncodedAssetParams { /** * assetTotal */ t: number | bigint; /** * assetDefaultFrozen */ df: boolean; /** * assetDecimals */ dc: number; /** * assetManager */ m?: Buffer; /** * assetReserve */ r?: Buffer; /** * assetFreeze */ f?: Buffer; /** * assetClawback */ c?: Buffer; /** * assetName */ an?: string; /** * assetUnitName */ un?: string; /** * assetURL */ au?: string; /** * assetMetadataHash */ am?: Buffer; } export interface EncodedLocalStateSchema { /** * appLocalInts */ nui: number; /** * appLocalByteSlices */ nbs: number; } export interface EncodedGlobalStateSchema { /** * appGlobalInts */ nui: number; /** * appGlobalByteSlices */ nbs: number; } export interface EncodedBoxReference { /** * index of the app ID in the foreign apps array */ i: number; /** * box name */ n: Uint8Array; } export interface EncodedTransaction { /** * fee */ fee?: number; /** * firstRound */ fv?: number; /** * lastRound */ lv: number; /** * note */ note?: Buffer; /** * from */ snd: Buffer; /** * type */ type: string; /** * genesisID */ gen: string; /** * genesisHash */ gh: Buffer; /** * lease */ lx?: Buffer; /** * group */ grp?: Buffer; /** * amount */ amt?: number | bigint; /** * amount (but for asset transfers) */ aamt?: number | bigint; /** * closeRemainderTo */ close?: Buffer; /** * closeRemainderTo (but for asset transfers) */ aclose?: Buffer; /** * reKeyTo */ rekey?: Buffer; /** * to */ rcv?: Buffer; /** * to (but for asset transfers) */ arcv?: Buffer; /** * voteKey */ votekey?: Buffer; /** * selectionKey */ selkey?: Buffer; /** * stateProofKey */ sprfkey?: Buffer; /** * voteFirst */ votefst?: number; /** * voteLast */ votelst?: number; /** * voteKeyDilution */ votekd?: number; /** * nonParticipation */ nonpart?: boolean; /** * assetIndex */ caid?: number; /** * assetIndex (but for asset transfers) */ xaid?: number; /** * assetIndex (but for asset freezing/unfreezing) */ faid?: number; /** * freezeState */ afrz?: boolean; /** * freezeAccount */ fadd?: Buffer; /** * assetRevocationTarget */ asnd?: Buffer; /** * See EncodedAssetParams type */ apar?: EncodedAssetParams; /** * appIndex */ apid?: number; /** * appOnComplete */ apan?: number; /** * See EncodedLocalStateSchema type */ apls?: EncodedLocalStateSchema; /** * See EncodedGlobalStateSchema type */ apgs?: EncodedGlobalStateSchema; /** * appForeignApps */ apfa?: number[]; /** * appForeignAssets */ apas?: number[]; /** * appApprovalProgram */ apap?: Buffer; /** * appClearProgram */ apsu?: Buffer; /** * appArgs */ apaa?: Buffer[]; /** * appAccounts */ apat?: Buffer[]; /** * extraPages */ apep?: number; /** * boxes */ apbx?: EncodedBoxReference[]; /* * stateProofType */ sptype?: number | bigint; /** * stateProof */ sp?: Buffer; /** * stateProofMessage */ spmsg?: Buffer; } export interface EncodedSubsig { /** * The public key */ pk: Uint8Array; /** * The signature provided by the public key, if any */ s?: Uint8Array; } /** * A rough structure for the encoded multi signature transaction object. * Every property is labelled with its associated `MultisigMetadata` type property */ export interface EncodedMultisig { /** * version */ v: number; /** * threshold */ thr: number; /** * Subset of signatures. A threshold of `thr` signors is required. */ subsig: EncodedSubsig[]; } export interface EncodedLogicSig { l: Uint8Array; arg?: Uint8Array[]; sig?: Uint8Array; msig?: EncodedMultisig; } export interface EncodedLogicSigAccount { lsig: EncodedLogicSig; sigkey?: Uint8Array; } /** * A structure for an encoded signed transaction object */ export interface EncodedSignedTransaction { /** * Transaction signature */ sig?: Buffer; /** * The transaction that was signed */ txn: EncodedTransaction; /** * Multisig structure */ msig?: EncodedMultisig; /** * Logic signature */ lsig?: EncodedLogicSig; /** * The signer, if signing with a different key than the Transaction type `from` property indicates */ sgnr?: Buffer; }