11import { bytes , internal , uint64 } from '@algorandfoundation/algorand-typescript'
22import { MAX_BOX_SIZE } from '../constants'
33import { lazyContext } from '../context-helpers/internal-context'
4- import { asBytes , asBytesCls , asNumber , conactUint8Arrays , toBytes } from '../util'
4+ import { asBytes , asBytesCls , asNumber , asUint8Array , conactUint8Arrays , toBytes } from '../util'
55
66export const Box : internal . opTypes . BoxType = {
77 create ( a : internal . primitives . StubBytesCompat , b : internal . primitives . StubUint64Compat ) : boolean {
@@ -35,11 +35,7 @@ export const Box: internal.opTypes.BoxType = {
3535 throw new internal . errors . InternalError ( 'Box does not exist' )
3636 }
3737 const boxContent = lazyContext . ledger . getBox ( app , name )
38- if ( boxContent instanceof Uint8Array ) {
39- return toBytes ( boxContent . slice ( start , start + length ) )
40- }
41- const result = toBytes ( boxContent ) . slice ( start , start + length )
42- return result
38+ return toBytes ( boxContent . slice ( start , start + length ) )
4339 } ,
4440 get ( a : internal . primitives . StubBytesCompat ) : readonly [ bytes , boolean ] {
4541 const name = asBytes ( a )
@@ -52,43 +48,34 @@ export const Box: internal.opTypes.BoxType = {
5248 const app = lazyContext . activeApplication
5349 const boxContent = lazyContext . ledger . getBox ( app , name )
5450 const exists = lazyContext . ledger . boxExists ( app , name )
55- if ( boxContent instanceof Uint8Array ) {
56- return [ boxContent . length , exists ]
57- }
58- const bytesContent = toBytes ( boxContent )
59- return [ bytesContent . length , exists ]
51+ return [ boxContent . length , exists ]
6052 } ,
6153 put ( a : internal . primitives . StubBytesCompat , b : internal . primitives . StubBytesCompat ) : void {
6254 const name = asBytes ( a )
6355 const app = lazyContext . activeApplication
64- const newContent = asBytes ( b )
56+ const newContent = asBytesCls ( b )
6557 if ( lazyContext . ledger . boxExists ( app , name ) ) {
6658 const boxContent = lazyContext . ledger . getBox ( app , name )
67- const length = boxContent instanceof Uint8Array ? boxContent . length : toBytes ( boxContent ) . length
59+ const length = boxContent . length
6860 if ( asNumber ( length ) !== asNumber ( newContent . length ) ) {
6961 throw new internal . errors . InternalError ( 'New content length does not match existing box length' )
7062 }
7163 }
72- lazyContext . ledger . setBox ( app , name , newContent )
64+ lazyContext . ledger . setBox ( app , name , newContent . asUint8Array ( ) )
7365 } ,
7466 replace ( a : internal . primitives . StubBytesCompat , b : internal . primitives . StubUint64Compat , c : internal . primitives . StubBytesCompat ) : void {
7567 const name = asBytes ( a )
7668 const start = asNumber ( b )
77- const newContent = asBytesCls ( c ) . asUint8Array ( )
69+ const newContent = asUint8Array ( c )
7870 const app = lazyContext . activeApplication
7971 if ( ! lazyContext . ledger . boxExists ( app , name ) ) {
8072 throw new internal . errors . InternalError ( 'Box does not exist' )
8173 }
8274 const boxContent = lazyContext . ledger . getBox ( app , name )
83- const uint8ArrayContent = boxContent instanceof Uint8Array ? boxContent : asBytesCls ( toBytes ( boxContent ) ) . asUint8Array ( )
84- if ( start + newContent . length > uint8ArrayContent . length ) {
75+ if ( start + newContent . length > boxContent . length ) {
8576 throw new internal . errors . InternalError ( 'Replacement content exceeds box size' )
8677 }
87- const updatedContent = conactUint8Arrays (
88- uint8ArrayContent . slice ( 0 , start ) ,
89- newContent ,
90- uint8ArrayContent . slice ( start + newContent . length ) ,
91- )
78+ const updatedContent = conactUint8Arrays ( boxContent . slice ( 0 , start ) , newContent , boxContent . slice ( start + newContent . length ) )
9279 lazyContext . ledger . setBox ( app , name , updatedContent )
9380 } ,
9481 resize ( a : internal . primitives . StubBytesCompat , b : internal . primitives . StubUint64Compat ) : void {
@@ -99,13 +86,12 @@ export const Box: internal.opTypes.BoxType = {
9986 throw new internal . errors . InternalError ( 'Box does not exist' )
10087 }
10188 const boxContent = lazyContext . ledger . getBox ( app , name )
102- const uint8ArrayContent = boxContent instanceof Uint8Array ? boxContent : asBytesCls ( toBytes ( boxContent ) ) . asUint8Array ( )
103- const size = uint8ArrayContent . length
89+ const size = boxContent . length
10490 let updatedContent
10591 if ( newSize > size ) {
106- updatedContent = conactUint8Arrays ( uint8ArrayContent , new Uint8Array ( Array ( newSize - size ) . fill ( 0 ) ) )
92+ updatedContent = conactUint8Arrays ( boxContent , new Uint8Array ( Array ( newSize - size ) . fill ( 0 ) ) )
10793 } else {
108- updatedContent = uint8ArrayContent . slice ( 0 , newSize )
94+ updatedContent = boxContent . slice ( 0 , newSize )
10995 }
11096 lazyContext . ledger . setBox ( app , name , updatedContent )
11197 } ,
@@ -118,19 +104,18 @@ export const Box: internal.opTypes.BoxType = {
118104 const name = asBytes ( a )
119105 const start = asNumber ( b )
120106 const length = asNumber ( c )
121- const newContent = asBytesCls ( d ) . asUint8Array ( )
107+ const newContent = asUint8Array ( d )
122108 const app = lazyContext . activeApplication
123109 if ( ! lazyContext . ledger . boxExists ( app , name ) ) {
124110 throw new internal . errors . InternalError ( 'Box does not exist' )
125111 }
126112 const boxContent = lazyContext . ledger . getBox ( app , name )
127- const uint8ArrayContent = boxContent instanceof Uint8Array ? boxContent : asBytesCls ( toBytes ( boxContent ) ) . asUint8Array ( )
128- const size = uint8ArrayContent . length
113+ const size = boxContent . length
129114 if ( start > size ) {
130115 throw new internal . errors . InternalError ( 'Start index exceeds box size' )
131116 }
132117 const end = Math . min ( start + length , size )
133- let updatedContent = conactUint8Arrays ( uint8ArrayContent . slice ( 0 , start ) , newContent , uint8ArrayContent . slice ( end ) )
118+ let updatedContent = conactUint8Arrays ( boxContent . slice ( 0 , start ) , newContent , boxContent . slice ( end ) )
134119 // Adjust the size if necessary
135120 if ( updatedContent . length > size ) {
136121 updatedContent = updatedContent . slice ( 0 , size )
0 commit comments