Skip to content

Commit 3a34b50

Browse files
committed
refactor: use encoding utils from puya-ts instead of redefining them
1 parent 28b5198 commit 3a34b50

File tree

11 files changed

+109
-179
lines changed

11 files changed

+109
-179
lines changed

src/encoders.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import type { biguint, bytes, TransactionType, uint64 } from '@algorandfoundation/algorand-typescript'
22
import type { OnCompleteAction } from '@algorandfoundation/algorand-typescript/arc4'
33
import { ARC4Encoded } from '@algorandfoundation/algorand-typescript/arc4'
4-
import { uint8ArrayToBigInt } from './encoding-util'
4+
import { encodingUtil } from '@algorandfoundation/puya-ts'
55
import { InternalError } from './errors'
66
import { BytesBackedCls, Uint64BackedCls } from './impl/base'
77
import { arc4Encoders, encodeArc4Impl, getArc4Encoder } from './impl/encoded-types'
@@ -18,11 +18,11 @@ export type TypeInfo = {
1818
export type fromBytes<T> = (val: Uint8Array | StubBytesCompat, typeInfo: TypeInfo, prefix?: 'none' | 'log') => T
1919

2020
const booleanFromBytes: fromBytes<boolean> = (val) => {
21-
return uint8ArrayToBigInt(asUint8Array(val)) > 0n
21+
return encodingUtil.uint8ArrayToBigInt(asUint8Array(val)) > 0n
2222
}
2323

2424
const bigUintFromBytes: fromBytes<biguint> = (val) => {
25-
return BigUint(uint8ArrayToBigInt(asUint8Array(val)))
25+
return BigUint(encodingUtil.uint8ArrayToBigInt(asUint8Array(val)))
2626
}
2727

2828
const bytesFromBytes: fromBytes<bytes> = (val) => {
@@ -34,15 +34,15 @@ const stringFromBytes: fromBytes<string> = (val) => {
3434
}
3535

3636
const uint64FromBytes: fromBytes<uint64> = (val) => {
37-
return Uint64(uint8ArrayToBigInt(asUint8Array(val)))
37+
return Uint64(encodingUtil.uint8ArrayToBigInt(asUint8Array(val)))
3838
}
3939

4040
const onCompletionFromBytes: fromBytes<OnCompleteAction> = (val) => {
41-
return Uint64(uint8ArrayToBigInt(asUint8Array(val))) as OnCompleteAction
41+
return Uint64(encodingUtil.uint8ArrayToBigInt(asUint8Array(val))) as OnCompleteAction
4242
}
4343

4444
const transactionTypeFromBytes: fromBytes<TransactionType> = (val) => {
45-
return Uint64(uint8ArrayToBigInt(asUint8Array(val))) as TransactionType
45+
return Uint64(encodingUtil.uint8ArrayToBigInt(asUint8Array(val))) as TransactionType
4646
}
4747

4848
export const encoders: Record<string, fromBytes<DeliberateAny>> = {

src/encoding-util.ts

Lines changed: 0 additions & 67 deletions
This file was deleted.

src/impl/contract.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import type { arc4, bytes } from '@algorandfoundation/algorand-typescript'
2+
import { encodingUtil } from '@algorandfoundation/puya-ts'
23
import { captureMethodConfig } from '../abi-metadata'
3-
import { utf8ToUint8Array } from '../encoding-util'
44
import type { DeliberateAny } from '../typescript-helpers'
55
import { BaseContract } from './base-contract'
66
import { sha512_256 } from './crypto'
@@ -39,5 +39,5 @@ export function baremethod<TContract extends Contract>(config?: arc4.BareMethodC
3939
}
4040

4141
export const methodSelector: typeof arc4.methodSelector = (methodSignature: string): bytes => {
42-
return sha512_256(Bytes(utf8ToUint8Array(methodSignature))).slice(0, 4)
42+
return sha512_256(Bytes(encodingUtil.utf8ToUint8Array(methodSignature))).slice(0, 4)
4343
}

src/impl/encoded-types.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,9 @@ import {
3333
} from '../constants'
3434
import { lazyContext } from '../context-helpers/internal-context'
3535
import type { fromBytes, TypeInfo } from '../encoders'
36-
import { uint8ArrayToNumber } from '../encoding-util'
3736
import { AvmError, avmInvariant, CodeError } from '../errors'
3837
import type { DeliberateAny } from '../typescript-helpers'
39-
import { asBigInt, asBigUint, asBigUintCls, asBytesCls, asUint64, asUint8Array, conactUint8Arrays } from '../util'
38+
import { asBigInt, asBigUint, asBigUintCls, asBytesCls, asUint64, asUint8Array, conactUint8Arrays, uint8ArrayToNumber } from '../util'
4039
import type { StubBytesCompat } from './primitives'
4140
import { AlgoTsPrimitiveCls, arrayUtil, BigUintCls, Bytes, BytesCls, getUint8Array, isBytes, Uint64Cls } from './primitives'
4241
import { Account, AccountCls, ApplicationCls, AssetCls } from './reference'

src/impl/global.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import type { Account as AccountType, Application as ApplicationType, bytes, op, uint64 } from '@algorandfoundation/algorand-typescript'
2+
import { encodingUtil } from '@algorandfoundation/puya-ts'
23
import {
34
DEFAULT_ACCOUNT_MIN_BALANCE,
45
DEFAULT_ASSET_CREATE_MIN_BALANCE,
@@ -9,7 +10,6 @@ import {
910
ZERO_ADDRESS,
1011
} from '../constants'
1112
import { lazyContext } from '../context-helpers/internal-context'
12-
import { bigIntToUint8Array } from '../encoding-util'
1313
import { InternalError } from '../errors'
1414
import { getObjectReference } from '../util'
1515
import { sha256 } from './crypto'
@@ -150,7 +150,7 @@ export const Global: typeof op.Global = {
150150
const data = getGlobalData()
151151
if (data.groupId !== undefined) return data.groupId
152152
const reference = getObjectReference(lazyContext.activeGroup)
153-
const referenceBytes = Bytes(bigIntToUint8Array(reference))
153+
const referenceBytes = Bytes(encodingUtil.bigIntToUint8Array(reference))
154154
return sha256(referenceBytes)
155155
},
156156

src/impl/primitives.ts

Lines changed: 11 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,5 @@
11
import type { biguint, BigUintCompat, bytes, BytesCompat, uint64, Uint64Compat } from '@algorandfoundation/algorand-typescript'
2-
import {
3-
base64ToUint8Array,
4-
bigIntToUint8Array,
5-
hexToUint8Array,
6-
uint8ArrayToBigInt,
7-
uint8ArrayToHex,
8-
uint8ArrayToUtf8,
9-
utf8ToUint8Array,
10-
} from '../encoding-util'
2+
import { encodingUtil } from '@algorandfoundation/puya-ts'
113
import { avmError, AvmError, avmInvariant, CodeError, InternalError } from '../errors'
124
import { nameOfType } from '../typescript-helpers'
135
import { base32ToUint8Array } from './base-32'
@@ -309,7 +301,7 @@ export class Uint64Cls extends AlgoTsPrimitiveCls {
309301
}
310302

311303
toBytes(): BytesCls {
312-
return new BytesCls(bigIntToUint8Array(this.#value, 8))
304+
return new BytesCls(encodingUtil.bigIntToUint8Array(this.#value, 8))
313305
}
314306

315307
asAlgoTs(): uint64 {
@@ -348,7 +340,7 @@ export class BigUintCls extends AlgoTsPrimitiveCls {
348340
}
349341

350342
toBytes(): BytesCls {
351-
return new BytesCls(bigIntToUint8Array(this.#value))
343+
return new BytesCls(encodingUtil.bigIntToUint8Array(this.#value))
352344
}
353345

354346
asAlgoTs(): biguint {
@@ -435,7 +427,7 @@ export class BytesCls extends AlgoTsPrimitiveCls {
435427
checkBytes(this.#v)
436428
// Add an enumerable property for debugging code to show
437429
Object.defineProperty(this, 'bytes', {
438-
value: uint8ArrayToHex(this.#v),
430+
value: encodingUtil.uint8ArrayToHex(this.#v),
439431
writable: false,
440432
enumerable: true,
441433
})
@@ -507,7 +499,7 @@ export class BytesCls extends AlgoTsPrimitiveCls {
507499
}
508500

509501
valueOf(): string {
510-
return uint8ArrayToHex(this.#v)
502+
return encodingUtil.uint8ArrayToHex(this.#v)
511503
}
512504

513505
static [Symbol.hasInstance](x: unknown): x is BytesCls {
@@ -516,7 +508,7 @@ export class BytesCls extends AlgoTsPrimitiveCls {
516508

517509
static fromCompat(v: StubBytesCompat | Uint8Array | undefined): BytesCls {
518510
if (v === undefined) return new BytesCls(new Uint8Array())
519-
if (typeof v === 'string') return new BytesCls(utf8ToUint8Array(v))
511+
if (typeof v === 'string') return new BytesCls(encodingUtil.utf8ToUint8Array(v))
520512
if (v instanceof BytesCls) return v
521513
if (v instanceof Uint8Array) return new BytesCls(v)
522514
throw new InternalError(`Cannot convert ${nameOfType(v)} to bytes`)
@@ -535,27 +527,27 @@ export class BytesCls extends AlgoTsPrimitiveCls {
535527
}
536528

537529
static fromHex(hex: string): BytesCls {
538-
return new BytesCls(hexToUint8Array(hex))
530+
return new BytesCls(encodingUtil.hexToUint8Array(hex))
539531
}
540532

541533
static fromBase64(b64: string): BytesCls {
542-
return new BytesCls(base64ToUint8Array(b64))
534+
return new BytesCls(encodingUtil.base64ToUint8Array(b64))
543535
}
544536

545537
static fromBase32(b32: string): BytesCls {
546538
return new BytesCls(base32ToUint8Array(b32))
547539
}
548540

549541
toUint64(): Uint64Cls {
550-
return new Uint64Cls(uint8ArrayToBigInt(this.#v))
542+
return new Uint64Cls(encodingUtil.uint8ArrayToBigInt(this.#v))
551543
}
552544

553545
toBigUint(): BigUintCls {
554-
return new BigUintCls(uint8ArrayToBigInt(this.#v))
546+
return new BigUintCls(encodingUtil.uint8ArrayToBigInt(this.#v))
555547
}
556548

557549
toString(): string {
558-
return uint8ArrayToUtf8(this.#v)
550+
return encodingUtil.uint8ArrayToUtf8(this.#v)
559551
}
560552

561553
asAlgoTs(): bytes {

src/runtime-helpers.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { ARC4Encoded } from '@algorandfoundation/algorand-typescript/arc4'
2+
import { encodingUtil } from '@algorandfoundation/puya-ts'
23
import { MAX_UINT64 } from './constants'
34
import type { TypeInfo } from './encoders'
4-
import { uint8ArrayToBigInt } from './encoding-util'
55
import { AvmError, CodeError, InternalError } from './errors'
66
import { Uint64BackedCls } from './impl/base'
77
import { AlgoTsPrimitiveCls, BigUintCls, BytesCls, checkBigUint, checkBytes, Uint64Cls } from './impl/primitives'
@@ -229,8 +229,8 @@ function bigUintBinaryOp(left: DeliberateAny, right: DeliberateAny, op: BinaryOp
229229
function bytesBinaryOp(left: DeliberateAny, right: DeliberateAny, op: BinaryOps): DeliberateAny {
230230
const lbb = checkBytes(BytesCls.fromCompat(left).asUint8Array())
231231
const rbb = checkBytes(BytesCls.fromCompat(right).asUint8Array())
232-
const lbi = uint8ArrayToBigInt(lbb)
233-
const rbi = uint8ArrayToBigInt(rbb)
232+
const lbi = encodingUtil.uint8ArrayToBigInt(lbb)
233+
const rbi = encodingUtil.uint8ArrayToBigInt(rbb)
234234

235235
const result = (function () {
236236
switch (op) {

src/util.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,10 @@ export const conactUint8Arrays = (...values: Uint8Array[]): Uint8Array => {
155155
return result
156156
}
157157

158+
export const uint8ArrayToNumber = (value: Uint8Array): number => {
159+
return value.reduce((acc, x) => acc * 256 + x, 0)
160+
}
161+
158162
/**
159163
* Runtime assertion function that throws if condition is falsy.
160164
*

0 commit comments

Comments
 (0)