From 8d0fe4152109467e4015555b1b56091ac7367aac Mon Sep 17 00:00:00 2001 From: Neil Campbell Date: Fri, 2 Aug 2024 01:03:18 +0800 Subject: [PATCH] fix: correct program encoding in application transactions --- src/transform.ts | 11 ++++++++--- tests/scenarios/transform-complex-txn.spec.ts | 14 ++++++++++++++ 2 files changed, 22 insertions(+), 3 deletions(-) diff --git a/src/transform.ts b/src/transform.ts index 049b5d8..a3aaa57 100644 --- a/src/transform.ts +++ b/src/transform.ts @@ -280,7 +280,6 @@ export function getIndexerTransactionFromAlgodTransaction( const getChildOffset = t.getChildOffset ? t.getChildOffset : () => ++childOffset const encoder = new TextEncoder() - const decoder = new TextDecoder() // The types in algosdk for state proofs are incorrect, so override them const stateProof = transaction.stateProof as unknown as StateProof | undefined @@ -365,8 +364,14 @@ export function getIndexerTransactionFromAlgodTransaction( ? { 'application-transaction': { 'application-id': transaction.appIndex, - 'approval-program': decoder.decode(transaction.appApprovalProgram), - 'clear-state-program': decoder.decode(transaction.appClearProgram), + 'approval-program': + transaction.appApprovalProgram && transaction.appApprovalProgram.length > 0 + ? Buffer.from(transaction.appApprovalProgram).toString('base64') + : '', + 'clear-state-program': + transaction.appClearProgram && transaction.appClearProgram.length > 0 + ? Buffer.from(transaction.appClearProgram).toString('base64') + : '', 'on-completion': algodOnCompleteToIndexerOnComplete(transaction.appOnComplete), 'application-args': transaction.appArgs?.map((a) => Buffer.from(a).toString('base64')), 'extra-program-pages': transaction.extraPages, diff --git a/tests/scenarios/transform-complex-txn.spec.ts b/tests/scenarios/transform-complex-txn.spec.ts index f71b536..b42cab4 100644 --- a/tests/scenarios/transform-complex-txn.spec.ts +++ b/tests/scenarios/transform-complex-txn.spec.ts @@ -705,6 +705,7 @@ describe('Complex transaction with many nested inner transactions', () => { expect(transaction.id).toBe('HHQHASIF2YLCSUYIPE6LIMLSNLCVMQBQHF3X46SKTX6F7ZSFKFCQ') expect(transaction.id).toBe(blockTransactions[0].transaction.txID()) }) + it('Produces the correct state deltas in an app call transaction', async () => { const blocks = await getBlocksBulk({ startRound: 39430981, maxRound: 39430981 }, algod) const blockTransactions = blocks.flatMap((b) => getBlockTransactions(b.block)) @@ -755,4 +756,17 @@ describe('Complex transaction with many nested inner transactions', () => { }, ]`) }) + + it('Produces base64 encoded programs for an application create transaction', async () => { + const blocks = await getBlocksBulk({ startRound: 34632059, maxRound: 34632059 }, algod) // Contains a appl create transaction with approval and clear state programs + const blockTransactions = blocks.flatMap((b) => getBlockTransactions(b.block)) + + expect(blockTransactions.length).toBe(14) + + const transaction = getIndexerTransactionFromAlgodTransaction(blockTransactions[5]) + expect(transaction['application-transaction']!['approval-program']).toBe( + 'CSAFAAGAgKSPxPlaEAImDwpsYXN0X21pbmVyDmhhbHZpbmdfc3VwcGx5EWxhc3RfbWluZXJfZWZmb3J0BmVmZm9ydAV0b2tlbgdoYWx2aW5nDG1pbmVkX3N1cHBseQxtaW5lcl9yZXdhcmQUY3VycmVudF9taW5lcl9lZmZvcnQFYmxvY2sMdG90YWxfZWZmb3J0EnRvdGFsX3RyYW5zYWN0aW9ucw1jdXJyZW50X21pbmVyD3N0YXJ0X3RpbWVzdGFtcAAxGCINgQYLMRkIjQgDCgMrAAAAAAAAAAADHQMsAIgAAiNDigAAJwQiZycJImcnCiJnJwsiZycFImcpgYCA0ofivC1nJwYiZycHgYCAgDJnKDIDZyoiZycMMgNnJwgiZycNgYCByKwGZ4mKAAAnBLGBA7IQgAZPcmFuZ2WyJoADT1JBsiUyCrIpMgqyKjIDsisyA7IsJLIigQiyI4A6aXBmczovL1FtVWl0eEp1UEpKcmN1QWRBaVZkRUVwdXpHbXNFTEdnQXZoTGQ1RmlYUlNoRXUjYXJjM7IngCDT/VG+LujCsXp66CbTScDfIP6rik1oAwNAHHQVYMMkNrIoIrIBgKgBSm9obiBBbGFuIFdvb2RzIDAxL0RlYy8yMDIzIFlvdSBrbm93LCBJIGNhbiBwdWxsIG1ldHJpY3Mgb3V0IG9mIHRoZSBhaXIgdG9vLCB3aGF0ZXZlciwgOCBtaWxsaW9uIHRyYW5zYWN0aW9ucyBvdmVyIHRoZSBsYXN0IHdlZWssIEkgZG9uJ3Qga25vdywgbXkgbW9tIGhhcyBmb3VyIG9yYW5nZXMusgWztDxniYgAAiNDigAAJwRkIhJBAAOI/qQxACsiZomKAwAyBjIGgQUYCYz/Jwlki/8TQQC3KGQnBGRwAExIQQB0KWQnB2QNQQAGJwdkQgACKWSM/rGBBLIQJwRkshEoZLIUi/6yEiKyAbMoZCpkFlCwJwYnBmSL/ghnKSlki/4JZylkIhJBAC0nBScFZCMIZycFZCUPQQAKKSQnBmQJZ0IAEykkJwZkCSEECmcnBycHZCEECmcoZDYyAGFBABsoZCtijP0oZCuL/SpkDUEACIv9KmQJQgABImYnCYv/ZygnDGRnKicIZGcnCCJniScOSTYaAUkVgSASRIgAAiNDigMAi/82MgArY0xIRDIHJw1kD0QnBWQlDkQxAYGgnAEORCcORwKI/vgnCicKZDEBCGcnCycLZCMIZ4v/K2IxAQiM/ov/K4v+Zov+jP0oZIv/EkEAE4v9KmQNQQAIi/0qZAlCAAEijP2L/ScIZA1BAAonDIv/ZycIi/1niTEbQfzygAS4RHs2NhoAjgH85QCABKsjcMw2GgCOAf9TAAAxG0H+ZQA=', + ) + expect(transaction['application-transaction']!['clear-state-program']).toBe('CQ==') + }) })