|
1 |
| -import { biguint, BigUint, bytes, Bytes, internal, TransactionType, uint64, Uint64 } from '@algorandfoundation/algorand-typescript' |
| 1 | +import { biguint, BigUint, bytes, internal, TransactionType, uint64, Uint64 } from '@algorandfoundation/algorand-typescript' |
2 | 2 | import { OnCompleteAction } from '@algorandfoundation/algorand-typescript/arc4'
|
3 | 3 | import { AccountCls } from './impl/account'
|
4 | 4 | import { ApplicationCls } from './impl/application'
|
5 | 5 | import { AssetCls } from './impl/asset'
|
| 6 | +import { arc4Encoders, getArc4Encoder } from './impl/encoded-types' |
| 7 | +import { DeliberateAny } from './typescript-helpers' |
| 8 | +import { asBytes, asUint8Array } from './util' |
6 | 9 |
|
7 |
| -export interface TypeInfo { |
| 10 | +export type TypeInfo = { |
8 | 11 | name: string
|
9 | 12 | genericArgs?: TypeInfo[] | Record<string, TypeInfo>
|
10 | 13 | }
|
11 | 14 |
|
12 |
| -type fromBytes<T> = (val: Uint8Array, typeInfo: TypeInfo) => T |
| 15 | +export type fromBytes<T> = (val: Uint8Array | internal.primitives.StubBytesCompat, typeInfo: TypeInfo, prefix?: 'none' | 'log') => T |
13 | 16 |
|
14 | 17 | const booleanFromBytes: fromBytes<boolean> = (val) => {
|
15 |
| - return internal.encodingUtil.uint8ArrayToBigInt(val) > 0n |
| 18 | + return internal.encodingUtil.uint8ArrayToBigInt(asUint8Array(val)) > 0n |
16 | 19 | }
|
17 | 20 |
|
18 | 21 | const bigUintFromBytes: fromBytes<biguint> = (val) => {
|
19 |
| - return BigUint(internal.encodingUtil.uint8ArrayToBigInt(val)) |
| 22 | + return BigUint(internal.encodingUtil.uint8ArrayToBigInt(asUint8Array(val))) |
20 | 23 | }
|
21 | 24 |
|
22 | 25 | const bytesFromBytes: fromBytes<bytes> = (val) => {
|
23 |
| - return Bytes(val) |
| 26 | + return asBytes(val) |
24 | 27 | }
|
25 | 28 |
|
26 | 29 | const stringFromBytes: fromBytes<string> = (val) => {
|
27 |
| - return Bytes(val).toString() |
| 30 | + return asBytes(val).toString() |
28 | 31 | }
|
29 | 32 |
|
30 | 33 | const uint64FromBytes: fromBytes<uint64> = (val) => {
|
31 |
| - return Uint64(internal.encodingUtil.uint8ArrayToBigInt(val)) |
| 34 | + return Uint64(internal.encodingUtil.uint8ArrayToBigInt(asUint8Array(val))) |
32 | 35 | }
|
33 | 36 |
|
34 | 37 | const onCompletionFromBytes: fromBytes<OnCompleteAction> = (val) => {
|
35 |
| - return Uint64(internal.encodingUtil.uint8ArrayToBigInt(val)) as OnCompleteAction |
| 38 | + return Uint64(internal.encodingUtil.uint8ArrayToBigInt(asUint8Array(val))) as OnCompleteAction |
36 | 39 | }
|
37 | 40 |
|
38 | 41 | const transactionTypeFromBytes: fromBytes<TransactionType> = (val) => {
|
39 |
| - return Uint64(internal.encodingUtil.uint8ArrayToBigInt(val)) as TransactionType |
| 42 | + return Uint64(internal.encodingUtil.uint8ArrayToBigInt(asUint8Array(val))) as TransactionType |
40 | 43 | }
|
41 | 44 |
|
42 |
| -export const encoders = { |
| 45 | +export const encoders: Record<string, fromBytes<DeliberateAny>> = { |
43 | 46 | account: AccountCls.fromBytes,
|
44 | 47 | application: ApplicationCls.fromBytes,
|
45 | 48 | asset: AssetCls.fromBytes,
|
46 |
| - 'bool(ean)?': booleanFromBytes, |
| 49 | + boolean: booleanFromBytes, |
47 | 50 | biguint: bigUintFromBytes,
|
48 | 51 | bytes: bytesFromBytes,
|
49 | 52 | string: stringFromBytes,
|
50 | 53 | uint64: uint64FromBytes,
|
51 | 54 | OnCompleteAction: onCompletionFromBytes,
|
52 | 55 | TransactionType: transactionTypeFromBytes,
|
53 |
| - // 'Tuple<*>': tupleFromBytes, |
| 56 | + ...arc4Encoders, |
54 | 57 | }
|
55 | 58 |
|
56 | 59 | export const getEncoder = <T>(typeInfo: TypeInfo): fromBytes<T> => {
|
57 |
| - const encoder = Object.entries(encoders).find(([k, _]) => new RegExp(`^${k}$`, 'i').test(typeInfo.name))?.[1] |
58 |
| - if (!encoder) { |
59 |
| - throw new Error(`No encoder found for type ${typeInfo.name}`) |
60 |
| - } |
61 |
| - return encoder as fromBytes<T> |
| 60 | + return getArc4Encoder<T>(typeInfo, encoders) |
62 | 61 | }
|
0 commit comments