|
| 1 | +import { bytes, internal, uint64 } from '@algorandfoundation/algorand-typescript' |
| 2 | +import { MAX_BOX_SIZE } from '../constants' |
| 3 | +import { lazyContext } from '../context-helpers/internal-context' |
| 4 | +import { asBytes, asBytesCls, asNumber, asUint8Array, conactUint8Arrays, toBytes } from '../util' |
| 5 | + |
| 6 | +export const Box: internal.opTypes.BoxType = { |
| 7 | + create(a: internal.primitives.StubBytesCompat, b: internal.primitives.StubUint64Compat): boolean { |
| 8 | + const name = asBytes(a) |
| 9 | + const size = asNumber(b) |
| 10 | + if (name.length === 0 || size > MAX_BOX_SIZE) { |
| 11 | + throw new internal.errors.InternalError('Invalid box name or size') |
| 12 | + } |
| 13 | + const app = lazyContext.activeApplication |
| 14 | + if (lazyContext.ledger.boxExists(app, name)) { |
| 15 | + return false |
| 16 | + } |
| 17 | + lazyContext.ledger.setBox(app, name, new Uint8Array(size)) |
| 18 | + return true |
| 19 | + }, |
| 20 | + delete(a: internal.primitives.StubBytesCompat): boolean { |
| 21 | + const name = asBytes(a) |
| 22 | + const app = lazyContext.activeApplication |
| 23 | + if (!lazyContext.ledger.boxExists(app, name)) { |
| 24 | + return false |
| 25 | + } |
| 26 | + lazyContext.ledger.deleteBox(app, name) |
| 27 | + return true |
| 28 | + }, |
| 29 | + extract(a: internal.primitives.StubBytesCompat, b: internal.primitives.StubUint64Compat, c: internal.primitives.StubUint64Compat): bytes { |
| 30 | + const name = asBytes(a) |
| 31 | + const start = asNumber(b) |
| 32 | + const length = asNumber(c) |
| 33 | + const app = lazyContext.activeApplication |
| 34 | + if (!lazyContext.ledger.boxExists(app, name)) { |
| 35 | + throw new internal.errors.InternalError('Box does not exist') |
| 36 | + } |
| 37 | + const boxContent = lazyContext.ledger.getBox(app, name) |
| 38 | + return toBytes(boxContent.slice(start, start + length)) |
| 39 | + }, |
| 40 | + get(a: internal.primitives.StubBytesCompat): readonly [bytes, boolean] { |
| 41 | + const name = asBytes(a) |
| 42 | + const app = lazyContext.activeApplication |
| 43 | + const boxContent = lazyContext.ledger.getBox(app, name) |
| 44 | + return [toBytes(boxContent), lazyContext.ledger.boxExists(app, name)] |
| 45 | + }, |
| 46 | + length(a: internal.primitives.StubBytesCompat): readonly [uint64, boolean] { |
| 47 | + const name = asBytes(a) |
| 48 | + const app = lazyContext.activeApplication |
| 49 | + const boxContent = lazyContext.ledger.getBox(app, name) |
| 50 | + const exists = lazyContext.ledger.boxExists(app, name) |
| 51 | + return [boxContent.length, exists] |
| 52 | + }, |
| 53 | + put(a: internal.primitives.StubBytesCompat, b: internal.primitives.StubBytesCompat): void { |
| 54 | + const name = asBytes(a) |
| 55 | + const app = lazyContext.activeApplication |
| 56 | + const newContent = asBytesCls(b) |
| 57 | + if (lazyContext.ledger.boxExists(app, name)) { |
| 58 | + const boxContent = lazyContext.ledger.getBox(app, name) |
| 59 | + const length = boxContent.length |
| 60 | + if (asNumber(length) !== asNumber(newContent.length)) { |
| 61 | + throw new internal.errors.InternalError('New content length does not match existing box length') |
| 62 | + } |
| 63 | + } |
| 64 | + lazyContext.ledger.setBox(app, name, newContent.asUint8Array()) |
| 65 | + }, |
| 66 | + replace(a: internal.primitives.StubBytesCompat, b: internal.primitives.StubUint64Compat, c: internal.primitives.StubBytesCompat): void { |
| 67 | + const name = asBytes(a) |
| 68 | + const start = asNumber(b) |
| 69 | + const newContent = asUint8Array(c) |
| 70 | + const app = lazyContext.activeApplication |
| 71 | + if (!lazyContext.ledger.boxExists(app, name)) { |
| 72 | + throw new internal.errors.InternalError('Box does not exist') |
| 73 | + } |
| 74 | + const boxContent = lazyContext.ledger.getBox(app, name) |
| 75 | + if (start + newContent.length > boxContent.length) { |
| 76 | + throw new internal.errors.InternalError('Replacement content exceeds box size') |
| 77 | + } |
| 78 | + const updatedContent = conactUint8Arrays(boxContent.slice(0, start), newContent, boxContent.slice(start + newContent.length)) |
| 79 | + lazyContext.ledger.setBox(app, name, updatedContent) |
| 80 | + }, |
| 81 | + resize(a: internal.primitives.StubBytesCompat, b: internal.primitives.StubUint64Compat): void { |
| 82 | + const name = asBytes(a) |
| 83 | + const newSize = asNumber(b) |
| 84 | + const app = lazyContext.activeApplication |
| 85 | + if (!lazyContext.ledger.boxExists(app, name)) { |
| 86 | + throw new internal.errors.InternalError('Box does not exist') |
| 87 | + } |
| 88 | + const boxContent = lazyContext.ledger.getBox(app, name) |
| 89 | + const size = boxContent.length |
| 90 | + let updatedContent |
| 91 | + if (newSize > size) { |
| 92 | + updatedContent = conactUint8Arrays(boxContent, new Uint8Array(newSize - size)) |
| 93 | + } else { |
| 94 | + updatedContent = boxContent.slice(0, newSize) |
| 95 | + } |
| 96 | + lazyContext.ledger.setBox(app, name, updatedContent) |
| 97 | + }, |
| 98 | + splice( |
| 99 | + a: internal.primitives.StubBytesCompat, |
| 100 | + b: internal.primitives.StubUint64Compat, |
| 101 | + c: internal.primitives.StubUint64Compat, |
| 102 | + d: internal.primitives.StubBytesCompat, |
| 103 | + ): void { |
| 104 | + const name = asBytes(a) |
| 105 | + const start = asNumber(b) |
| 106 | + const length = asNumber(c) |
| 107 | + const newContent = asUint8Array(d) |
| 108 | + const app = lazyContext.activeApplication |
| 109 | + if (!lazyContext.ledger.boxExists(app, name)) { |
| 110 | + throw new internal.errors.InternalError('Box does not exist') |
| 111 | + } |
| 112 | + const boxContent = lazyContext.ledger.getBox(app, name) |
| 113 | + const size = boxContent.length |
| 114 | + if (start > size) { |
| 115 | + throw new internal.errors.InternalError('Start index exceeds box size') |
| 116 | + } |
| 117 | + const end = Math.min(start + length, size) |
| 118 | + let updatedContent = conactUint8Arrays(boxContent.slice(0, start), newContent, boxContent.slice(end)) |
| 119 | + // Adjust the size if necessary |
| 120 | + if (updatedContent.length > size) { |
| 121 | + updatedContent = updatedContent.slice(0, size) |
| 122 | + } else if (updatedContent.length < size) { |
| 123 | + updatedContent = conactUint8Arrays(updatedContent, new Uint8Array(size - asNumber(updatedContent.length))) |
| 124 | + } |
| 125 | + lazyContext.ledger.setBox(app, name, updatedContent) |
| 126 | + }, |
| 127 | +} |
0 commit comments