Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixbug on ItxnField opcode #550

Merged
merged 8 commits into from
Jan 18, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

## Unreleased

### Improvements

- Support rekeying in `@algo-builder/runtime`

### Bug Fixes

- `Runtime` env is enforcing address use in ItxnField already `exist`. However, those addresses used in `acfg` or create asset transactions the addresses use for config may not exist. We fixed it in this [PR](https://github.com/scale-it/algo-builder/pull/550). Reported by @patrick
Expand Down
18 changes: 14 additions & 4 deletions packages/runtime/src/lib/itxn.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,13 @@ const byteTxnFields = new Set([
'ConfigAssetMetadataHash', 'ConfigAssetURL'
]);

const addrTxnFields = new Set([
const acfgAddrTxnFields = new Set([
'ConfigAssetManager', 'ConfigAssetReserve', 'ConfigAssetFreeze', 'ConfigAssetClawback'
]);

const otherTxnFields = new Set([
'Sender', 'Receiver', 'CloseRemainderTo', 'AssetSender', 'AssetCloseTo',
'AssetReceiver', 'FreezeAssetAccount', 'ConfigAssetManager',
'ConfigAssetReserve', 'ConfigAssetFreeze', 'ConfigAssetClawback'
'AssetReceiver', 'FreezeAssetAccount'
]);

/**
Expand Down Expand Up @@ -64,7 +67,14 @@ export function setInnerTxField (
txValue = convertToString(assertedVal);
}

if (addrTxnFields.has(field)) {
if (otherTxnFields.has(field)) {
const assertedVal = op.assertBytes(val, line);
const accountState = interpreter.getAccount(assertedVal, line);
txValue = Buffer.from(decodeAddress(accountState.address).publicKey);
}

// if address use for acfg we only check address is valid
if (acfgAddrTxnFields.has(field)) {
txValue = op.assertAlgorandAddress(val, line);
}

Expand Down
25 changes: 14 additions & 11 deletions packages/runtime/test/src/interpreter/inner-transaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -384,19 +384,22 @@ describe("TEALv5: Inner Transactions", function () {
});

describe("TestFieldTypes", function () {
it('should pass: teal understand 32 bytes value is address', () => {
tealCode = `
const ConfigAddresses = ['ConfigAssetManager', 'ConfigAssetReserve', 'ConfigAssetFreeze', 'ConfigAssetClawback'];

it('should pass: teal understand 32 bytes value is address with acfg address', () => {
ConfigAddresses.forEach((configAddr) => {
tealCode = `
itxn_begin
byte "01234567890123456789012345678901"
itxn_field AssetReceiver
itxn_field ${configAddr}
int 1
`;

assert.doesNotThrow(() => executeTEAL(tealCode));
assert.doesNotThrow(() => executeTEAL(tealCode));
});
});

it("should pass: use random address with asa config transaction", () => {
const ConfigAddresses = ['ConfigAssetManager', 'ConfigAssetReserve', 'ConfigAssetFreeze', 'ConfigAssetClawback'];
it("should pass: use random address with asa config transaction with acfg address", () => {
ConfigAddresses.forEach((configAddress) => {
tealCode = `
itxn_begin
Expand All @@ -416,7 +419,7 @@ describe("TEALv5: Inner Transactions", function () {
byte "pay"
itxn_field Sender
`;
expectRuntimeError(() => executeTEAL(tealCode), RUNTIME_ERRORS.TEAL.INVALID_ADDR);
expectRuntimeError(() => executeTEAL(tealCode), RUNTIME_ERRORS.TEAL.ADDR_NOT_VALID);

tealCode = `
itxn_begin
Expand All @@ -430,14 +433,14 @@ describe("TEALv5: Inner Transactions", function () {
byte ""
itxn_field CloseRemainderTo
`;
expectRuntimeError(() => executeTEAL(tealCode), RUNTIME_ERRORS.TEAL.INVALID_ADDR);
expectRuntimeError(() => executeTEAL(tealCode), RUNTIME_ERRORS.TEAL.ADDR_NOT_VALID);

tealCode = `
itxn_begin
byte ""
itxn_field AssetSender
`;
expectRuntimeError(() => executeTEAL(tealCode), RUNTIME_ERRORS.TEAL.INVALID_ADDR);
expectRuntimeError(() => executeTEAL(tealCode), RUNTIME_ERRORS.TEAL.ADDR_NOT_VALID);
});

it(`should fail: invalid ref, not a valid account address`, function () {
Expand All @@ -447,15 +450,15 @@ describe("TEALv5: Inner Transactions", function () {
byte "GAYTEMZUGU3DOOBZGAYTEMZUGU3DOOBZGAYTEMZUGU3DOOBZGAYZIZD42E"
itxn_field AssetCloseTo
`;
expectRuntimeError(() => executeTEAL(tealCode), RUNTIME_ERRORS.TEAL.INVALID_ADDR);
expectRuntimeError(() => executeTEAL(tealCode), RUNTIME_ERRORS.TEAL.ADDR_NOT_VALID);

// b31 => not vaild account
tealCode = `
itxn_begin
byte "0123456789012345678901234567890"
itxn_field AssetCloseTo
`;
expectRuntimeError(() => executeTEAL(tealCode), RUNTIME_ERRORS.TEAL.INVALID_ADDR);
expectRuntimeError(() => executeTEAL(tealCode), RUNTIME_ERRORS.TEAL.ADDR_NOT_VALID);

// int number => invaild type (should be bytes)
tealCode = `
Expand Down