Skip to content

Commit

Permalink
Merge main into release
Browse files Browse the repository at this point in the history
  • Loading branch information
github-actions[bot] authored Jun 13, 2024
2 parents ce535f4 + 86532a5 commit 0bc7d76
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 30 deletions.
14 changes: 7 additions & 7 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

45 changes: 22 additions & 23 deletions src/transform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -131,28 +131,7 @@ function extractTransactionFromBlockTransaction(
closeAmount?: number
logs?: Uint8Array[]
} {
const txn = { ...blockTransaction.txn }

// https://github.com/algorand/js-algorand-sdk/blob/develop/examples/block_fetcher/index.ts
// Remove nulls (mainly where an appl txn contains a null app arg)
removeNulls(txn)

// Add genesisId (gen) as the transaction was processed with it, and is required to generate the correct txID.
if ('hgi' in blockTransaction && blockTransaction.hgi === true) {
txn.gen = genesisId
}

// Add genesisHash (gh) as the transaction was processed with it, and is required to generate the correct txID.
// gh is mandatory on MainNet and TestNet (see https://forum.algorand.org/t/calculating-transaction-id/3119/7), so set gh unless hgh is explicitly false.
if (!('hgh' in blockTransaction) || blockTransaction.hgh !== false) {
txn.gh = genesisHash
}

if (txn.type === TransactionType.axfer && !txn.arcv) {
// from_obj_for_encoding expects arcv to be set, which may not be defined when performing an opt out.
txn.arcv = Buffer.from(ALGORAND_ZERO_ADDRESS_BYTES)
}

const txn = extractAndNormaliseTransaction(blockTransaction, genesisHash, genesisId)
const t = Transaction.from_obj_for_encoding(txn)
return {
transaction: t,
Expand Down Expand Up @@ -196,7 +175,11 @@ function concatArrays(...arrs: ArrayLike<number>[]) {
return c
}

function getTxIdFromBlockTransaction(blockTransaction: BlockTransaction, genesisHash: Buffer, genesisId: string): string {
function extractAndNormaliseTransaction(
blockTransaction: BlockTransaction | BlockInnerTransaction,
genesisHash: Buffer,
genesisId: string,
) {
const txn = { ...blockTransaction.txn }

// https://github.com/algorand/js-algorand-sdk/blob/develop/examples/block_fetcher/index.ts
Expand All @@ -214,6 +197,22 @@ function getTxIdFromBlockTransaction(blockTransaction: BlockTransaction, genesis
txn.gh = genesisHash
}

if (txn.type === TransactionType.axfer && !txn.arcv) {
// from_obj_for_encoding expects arcv to be set, which may not be defined when performing an opt out.
txn.arcv = Buffer.from(ALGORAND_ZERO_ADDRESS_BYTES)
}

if (txn.type === TransactionType.pay && !txn.rcv) {
// from_obj_for_encoding expects rcv to be set, which may not be defined when closing an account.
txn.rcv = Buffer.from(ALGORAND_ZERO_ADDRESS_BYTES)
}

return txn
}

function getTxIdFromBlockTransaction(blockTransaction: BlockTransaction, genesisHash: Buffer, genesisId: string): string {
const txn = extractAndNormaliseTransaction(blockTransaction, genesisHash, genesisId)

// Translated from algosdk.Transaction.txID()
const ALGORAND_TRANSACTION_LENGTH = 52
const encodedMessage = msgpack.encode(txn, { sortKeys: true, ignoreUndefined: true })
Expand Down
8 changes: 8 additions & 0 deletions tests/scenarios/transform-complex-txn.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -640,6 +640,14 @@ describe('Complex transaction with many nested inner transactions', () => {
expect(algosdk.encodeAddress(blockTransactions[5].transaction.to.publicKey)).toBe(ALGORAND_ZERO_ADDRESS)
})

it('Transforms pay without a rcv address', async () => {
const blocks = await getBlocksBulk({ startRound: 39723800, maxRound: 39723800 }, algod) // Contains a pay close account inner transaction without a rcv address
const blockTransactions = blocks.flatMap((b) => getBlockTransactions(b.block))

expect(blockTransactions.length).toBe(486)
expect(algosdk.encodeAddress(blockTransactions[371].transaction.to.publicKey)).toBe(ALGORAND_ZERO_ADDRESS)
})

it('Produces the correct txID for a non hgi transaction', async () => {
const blocks = await getBlocksBulk({ startRound: 39430981, maxRound: 39430981 }, algod)
const blockTransactions = blocks.flatMap((b) => getBlockTransactions(b.block))
Expand Down

0 comments on commit 0bc7d76

Please sign in to comment.