Skip to content

Commit acfb4f1

Browse files
boblatneilcampbell
andauthored
feat: add support for ABI validation (#99)
--------- Co-authored-by: Neil Campbell <[email protected]>
1 parent 99f3a00 commit acfb4f1

File tree

8 files changed

+26
-15
lines changed

8 files changed

+26
-15
lines changed

docs/tg-concepts.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,3 +70,8 @@ As explained in the [introduction](testing-guide.md), `algorand-typescript-testi
7070
3. **Mockable**: Not implemented, but can be mocked or patched. For example, `op.onlineStake` can be mocked to return specific values or behaviours; otherwise, it raises a `NotImplementedError`. This category covers cases where native or emulated implementation in a unit test context is impractical or overly complex.
7171

7272
For a full list of all public `algorand-typescript` types and their corresponding implementation category, refer to the [Coverage](./coverage.md) section.
73+
74+
## Data Validation
75+
76+
Algorand TypeScript and the puya compiler have functionality to perform validation of transaction inputs via the `--validate-abi-args`, `--validate-abi-return` CLI arguments, `arc4.abimethod({validateEncoding: ...})` decorator, `convertBytes(..., { strategy: 'validate' }) method` and `validateEncoding(...)` method.
77+
The Algorand TypeScript Testing library does _NOT_ implement this validation behaviour, as you should test invalid inputs using an integrated test against a real Algorand network.

package-lock.json

Lines changed: 9 additions & 9 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,8 +68,8 @@
6868
"vitest": "3.2.4"
6969
},
7070
"dependencies": {
71-
"@algorandfoundation/algorand-typescript": "1.0.0-alpha.87",
72-
"@algorandfoundation/puya-ts": "1.0.0-alpha.87",
71+
"@algorandfoundation/algorand-typescript": "1.0.0-alpha.90",
72+
"@algorandfoundation/puya-ts": "1.0.0-alpha.90",
7373
"elliptic": "^6.6.1",
7474
"js-sha256": "^0.11.0",
7575
"js-sha3": "^0.9.3",

src/impl/encoded-types/encoded-types.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1220,7 +1220,7 @@ export function decodeArc4<T>(
12201220
export function convertBytes<T extends _ARC4Encoded>(
12211221
typeInfoString: string,
12221222
bytes: StubBytesCompat,
1223-
options: { prefix?: 'none' | 'log'; strategy: 'unsafe-cast' },
1223+
options: { prefix?: 'none' | 'log'; strategy: 'unsafe-cast' | 'validate' },
12241224
): T {
12251225
const typeInfo = JSON.parse(typeInfoString)
12261226
return getEncoder<T>(typeInfo)(bytes, typeInfo, options.prefix)

src/impl/validate-encoding.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
/** @internal */
2+
export function validateEncoding<T>(_value: T) {}

src/internal/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ export { BaseContract, contract } from '../impl/base-contract'
55
/** @internal */
66
export { clone } from '../impl/clone'
77
/** @internal */
8+
export { validateEncoding } from '../impl/validate-encoding'
9+
/** @internal */
810
export { compile } from '../impl/compiled'
911
/** @internal */
1012
export { abimethod, baremethod, Contract, readonly } from '../impl/contract'

tests/arc4/address.algo.spec.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,8 @@ describe('arc4.Address', () => {
7171

7272
test.each(testData)('fromBytes method', (value) => {
7373
const sdkResult = getABIEncodedValue(asUint8Array(value), abiTypeString, {})
74-
const result = convertBytes<Address>(value, { strategy: 'unsafe-cast' })
74+
// no actual validation in testing lib, just making sure 'validate' strategy value can be passed
75+
const result = convertBytes<Address>(value, { strategy: 'validate' })
7576
expect(result.bytes).toEqual(sdkResult)
7677
})
7778

tests/artifacts/arc4-primitive-ops/contract.algo.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
11
import type { biguint, bytes, uint64 } from '@algorandfoundation/algorand-typescript'
2-
import { arc4, BigUint, clone, emit } from '@algorandfoundation/algorand-typescript'
2+
import { arc4, BigUint, clone, emit, validateEncoding } from '@algorandfoundation/algorand-typescript'
33
import type { Bool, UFixed } from '@algorandfoundation/algorand-typescript/arc4'
44
import { Byte, Contract, convertBytes, Str, Uint } from '@algorandfoundation/algorand-typescript/arc4'
55

66
export class Arc4PrimitiveOpsContract extends Contract {
7-
@arc4.abimethod()
7+
@arc4.abimethod({ validateEncoding: 'unsafe-disabled' })
88
public verify_uintn_uintn_eq(a: bytes, b: bytes): boolean {
99
const aBiguint = BigUint(a)
1010
const bBiguint = BigUint(b)
1111
const aUintN = new Uint<64>(aBiguint)
1212
const bUintN = new Uint<64>(bBiguint)
13+
validateEncoding(aUintN)
1314
return aUintN === bUintN
1415
}
1516
@arc4.abimethod()

0 commit comments

Comments
 (0)