|
1 | | -import type { bytes } from '@algorandfoundation/algorand-typescript' |
| 1 | +import type { biguint, bytes, uint64 } from '@algorandfoundation/algorand-typescript' |
2 | 2 | import { randomBytes } from 'crypto' |
3 | 3 | import { BITS_IN_BYTE, MAX_BYTES_SIZE, MAX_UINT512, MAX_UINT8, UINT512_SIZE } from './constants' |
4 | 4 | import { AssertError, AvmError, InternalError } from './errors' |
5 | 5 | import type { StubBigUintCompat, StubBytesCompat, StubUint64Compat } from './impl/primitives' |
6 | 6 | import { BigUintCls, Bytes, BytesCls, Uint64Cls } from './impl/primitives' |
7 | 7 | import type { DeliberateAny } from './typescript-helpers' |
8 | 8 |
|
| 9 | +/** |
| 10 | + * Converts internal Algorand type representations to their external primitive values. |
| 11 | + * |
| 12 | + * @overload |
| 13 | + * @param {uint64} val - A uint64 value to convert |
| 14 | + * @returns {bigint} The uint64 value as a bigint |
| 15 | + * |
| 16 | + * @overload |
| 17 | + * @param {biguint} val - A biguint value to convert |
| 18 | + * @returns {bigint} The biguint value as a bigint |
| 19 | + * |
| 20 | + * @overload |
| 21 | + * @param {bytes} val - A bytes value to convert |
| 22 | + * @returns {Uint8Array} The bytes value as a Uint8Array |
| 23 | + * |
| 24 | + * @overload |
| 25 | + * @param {string} val - A string value to pass through |
| 26 | + * @returns {string} The original string value unchanged |
| 27 | + * |
| 28 | + * @example |
| 29 | + * ```ts |
| 30 | + * const uint64Val = Uint64(123n) |
| 31 | + * toExternalValue(uint64Val) // returns 123n |
| 32 | + * |
| 33 | + * const bytesVal = Bytes.fromBase64("SGVsbG8="); |
| 34 | + * toExternalValue(bytesVal) // returns Uint8Array([72, 101, 108, 108, 111]) |
| 35 | + * ``` |
| 36 | + */ |
| 37 | +export function toExternalValue(val: uint64): bigint |
| 38 | +export function toExternalValue(val: biguint): bigint |
| 39 | +export function toExternalValue(val: bytes): Uint8Array |
| 40 | +export function toExternalValue(val: string): string |
| 41 | +export function toExternalValue(val: uint64 | biguint | bytes | string) { |
| 42 | + const instance = val as unknown |
| 43 | + if (instance instanceof BytesCls) return instance.asUint8Array() |
| 44 | + if (instance instanceof Uint64Cls) return instance.asBigInt() |
| 45 | + if (instance instanceof BigUintCls) return instance.asBigInt() |
| 46 | + if (typeof val === 'string') return val |
| 47 | +} |
| 48 | + |
9 | 49 | export function* iterBigInt(start: bigint, end: bigint): Generator<bigint> { |
10 | 50 | for (let i = start; i < end; i++) { |
11 | 51 | yield BigInt(i) |
|
0 commit comments