11import {
2- State ,
3- state ,
42 UInt64 ,
5- Bool ,
6- SmartContract ,
73 Mina ,
84 AccountUpdate ,
9- method ,
105 PublicKey ,
11- Permissions ,
12- VerificationKey ,
136 Field ,
14- Int64 ,
157 TokenId ,
16- TokenContract as TokenContractBase ,
17- AccountUpdateForest ,
188 PrivateKey ,
199} from 'o1js' ;
20- import { test , describe , it , before } from 'node:test' ;
10+ import { describe , it , before , beforeEach , afterEach } from 'node:test' ;
2111import { expect } from 'expect' ;
2212
23-
24-
2513const defaultNetwork = Mina . Network ( {
26- networkId : " testnet" ,
27- mina : " https://example.com/graphql" ,
28- archive : " https://example.com//graphql"
14+ networkId : ' testnet' ,
15+ mina : ' https://example.com/graphql' ,
16+ archive : ' https://example.com//graphql' ,
2917} ) ;
3018
3119const enforcedNetwork = Mina . Network ( {
32- networkId : " testnet" ,
33- mina : " https://example.com/graphql" ,
34- archive : " https://example.com//graphql" ,
35- bypassTransactionLimits : false
20+ networkId : ' testnet' ,
21+ mina : ' https://example.com/graphql' ,
22+ archive : ' https://example.com//graphql' ,
23+ bypassTransactionLimits : false ,
3624} ) ;
3725
3826const unlimitedNetwork = Mina . Network ( {
39- networkId : "testnet" ,
40- mina : "https://unlimited.com/graphql" ,
41- archive : "https://unlimited.com//graphql" ,
42- bypassTransactionLimits : true
27+ networkId : 'testnet' ,
28+ mina : 'https://unlimited.com/graphql' ,
29+ archive : 'https://unlimited.com//graphql' ,
30+ bypassTransactionLimits : true ,
31+ } ) ;
32+
33+ const networkWithHeaders = Mina . Network ( {
34+ networkId : 'testnet' ,
35+ mina : 'https://mina.dummy/graphql' ,
36+ archive : 'https://archive.dummy/graphql' ,
37+ minaDefaultHeaders : {
38+ Authorization : 'Bearer mina-default-token' ,
39+ 'X-Test' : 'mina-test' ,
40+ } ,
41+ archiveDefaultHeaders : {
42+ Authorization : 'Bearer archive-default-token' ,
43+ 'X-Test' : 'archive-test' ,
44+ } ,
4345} ) ;
4446
4547describe ( 'Test default network' , ( ) => {
46- let bobAccount : PublicKey ,
47- bobKey : PrivateKey ;
48+ let bobAccount : PublicKey , bobKey : PrivateKey ;
4849
4950 before ( async ( ) => {
50-
5151 Mina . setActiveInstance ( defaultNetwork ) ;
5252 bobKey = PrivateKey . random ( ) ;
5353 bobAccount = bobKey . toPublicKey ( ) ;
5454 } ) ;
5555
56-
5756 it ( 'Simple account update' , async ( ) => {
58-
5957 let txn = await Mina . transaction ( async ( ) => {
6058 const accountUpdateBob = AccountUpdate . create ( bobAccount , Field . from ( 1 ) ) ;
6159 accountUpdateBob . account . balance . requireEquals ( UInt64 . zero ) ;
6260 accountUpdateBob . balance . addInPlace ( UInt64 . one ) ;
6361 } ) ;
6462 await txn . prove ( ) ;
6563 await txn . sign ( [ bobKey ] ) . safeSend ( ) ;
66-
6764 } ) ;
6865
6966 it ( 'Multiple account update' , async ( ) => {
70-
7167 let txn = await Mina . transaction ( async ( ) => {
7268 for ( let index = 0 ; index < 2 ; index ++ ) {
73- const accountUpdateBob = AccountUpdate . create ( bobAccount , Field . from ( index ) ) ;
69+ const accountUpdateBob = AccountUpdate . create (
70+ bobAccount ,
71+ Field . from ( index )
72+ ) ;
7473 accountUpdateBob . account . balance . requireEquals ( UInt64 . zero ) ;
7574 accountUpdateBob . balance . addInPlace ( UInt64 . one ) ;
7675 }
7776 } ) ;
7877 await txn . prove ( ) ;
7978 await txn . sign ( [ bobKey ] ) . safeSend ( ) ;
80-
8179 } ) ;
8280
8381 it ( 'More than limit account update' , async ( ) => {
84-
8582 let txn = await Mina . transaction ( async ( ) => {
8683 for ( let index = 0 ; index < 12 ; index ++ ) {
87- const accountUpdateBob = AccountUpdate . create ( bobAccount , Field . from ( index ) ) ;
84+ const accountUpdateBob = AccountUpdate . create (
85+ bobAccount ,
86+ Field . from ( index )
87+ ) ;
8888 accountUpdateBob . account . balance . requireEquals ( UInt64 . zero ) ;
8989 accountUpdateBob . balance . addInPlace ( UInt64 . one ) ;
9090 }
@@ -96,48 +96,46 @@ describe('Test default network', () => {
9696} ) ;
9797
9898describe ( 'Test enforced network' , ( ) => {
99- let bobAccount : PublicKey ,
100- bobKey : PrivateKey ;
99+ let bobAccount : PublicKey , bobKey : PrivateKey ;
101100
102101 before ( async ( ) => {
103-
104102 Mina . setActiveInstance ( enforcedNetwork ) ;
105103 bobKey = PrivateKey . random ( ) ;
106104 bobAccount = bobKey . toPublicKey ( ) ;
107105 } ) ;
108106
109-
110107 it ( 'Simple account update' , async ( ) => {
111-
112108 let txn = await Mina . transaction ( async ( ) => {
113109 const accountUpdateBob = AccountUpdate . create ( bobAccount , Field . from ( 1 ) ) ;
114110 accountUpdateBob . account . balance . requireEquals ( UInt64 . zero ) ;
115111 accountUpdateBob . balance . addInPlace ( UInt64 . one ) ;
116112 } ) ;
117113 await txn . prove ( ) ;
118114 await txn . sign ( [ bobKey ] ) . safeSend ( ) ;
119-
120115 } ) ;
121116
122117 it ( 'Multiple account update' , async ( ) => {
123-
124118 let txn = await Mina . transaction ( async ( ) => {
125119 for ( let index = 0 ; index < 2 ; index ++ ) {
126- const accountUpdateBob = AccountUpdate . create ( bobAccount , Field . from ( index ) ) ;
120+ const accountUpdateBob = AccountUpdate . create (
121+ bobAccount ,
122+ Field . from ( index )
123+ ) ;
127124 accountUpdateBob . account . balance . requireEquals ( UInt64 . zero ) ;
128125 accountUpdateBob . balance . addInPlace ( UInt64 . one ) ;
129126 }
130127 } ) ;
131128 await txn . prove ( ) ;
132129 await txn . sign ( [ bobKey ] ) . safeSend ( ) ;
133-
134130 } ) ;
135131
136132 it ( 'More than limit account update' , async ( ) => {
137-
138133 let txn = await Mina . transaction ( async ( ) => {
139134 for ( let index = 0 ; index < 12 ; index ++ ) {
140- const accountUpdateBob = AccountUpdate . create ( bobAccount , Field . from ( index ) ) ;
135+ const accountUpdateBob = AccountUpdate . create (
136+ bobAccount ,
137+ Field . from ( index )
138+ ) ;
141139 accountUpdateBob . account . balance . requireEquals ( UInt64 . zero ) ;
142140 accountUpdateBob . balance . addInPlace ( UInt64 . one ) ;
143141 }
@@ -149,48 +147,46 @@ describe('Test enforced network', () => {
149147} ) ;
150148
151149describe ( 'Test unlimited network' , ( ) => {
152- let bobAccount : PublicKey ,
153- bobKey : PrivateKey ;
150+ let bobAccount : PublicKey , bobKey : PrivateKey ;
154151
155152 before ( async ( ) => {
156-
157153 Mina . setActiveInstance ( unlimitedNetwork ) ;
158154 bobKey = PrivateKey . random ( ) ;
159155 bobAccount = bobKey . toPublicKey ( ) ;
160156 } ) ;
161157
162-
163158 it ( 'Simple account update' , async ( ) => {
164-
165159 let txn = await Mina . transaction ( async ( ) => {
166160 const accountUpdateBob = AccountUpdate . create ( bobAccount , Field . from ( 1 ) ) ;
167161 accountUpdateBob . account . balance . requireEquals ( UInt64 . zero ) ;
168162 accountUpdateBob . balance . addInPlace ( UInt64 . one ) ;
169163 } ) ;
170164 await txn . prove ( ) ;
171165 await txn . sign ( [ bobKey ] ) . safeSend ( ) ;
172-
173166 } ) ;
174167
175168 it ( 'Multiple account update' , async ( ) => {
176-
177169 let txn = await Mina . transaction ( async ( ) => {
178170 for ( let index = 0 ; index < 2 ; index ++ ) {
179- const accountUpdateBob = AccountUpdate . create ( bobAccount , Field . from ( index ) ) ;
171+ const accountUpdateBob = AccountUpdate . create (
172+ bobAccount ,
173+ Field . from ( index )
174+ ) ;
180175 accountUpdateBob . account . balance . requireEquals ( UInt64 . zero ) ;
181176 accountUpdateBob . balance . addInPlace ( UInt64 . one ) ;
182177 }
183178 } ) ;
184179 await txn . prove ( ) ;
185180 await txn . sign ( [ bobKey ] ) . safeSend ( ) ;
186-
187181 } ) ;
188182
189183 it ( 'More than limit account update' , async ( ) => {
190-
191184 let txn = await Mina . transaction ( async ( ) => {
192185 for ( let index = 0 ; index < 12 ; index ++ ) {
193- const accountUpdateBob = AccountUpdate . create ( bobAccount , Field . from ( index ) ) ;
186+ const accountUpdateBob = AccountUpdate . create (
187+ bobAccount ,
188+ Field . from ( index )
189+ ) ;
194190 accountUpdateBob . account . balance . requireEquals ( UInt64 . zero ) ;
195191 accountUpdateBob . balance . addInPlace ( UInt64 . one ) ;
196192 }
@@ -199,4 +195,123 @@ describe('Test unlimited network', () => {
199195 // success with bypassTransactionLimits = true
200196 await txn . sign ( [ bobKey ] ) . safeSend ( ) ;
201197 } ) ;
202- } ) ;
198+ } ) ;
199+
200+ describe ( 'Test network with headers' , ( ) => {
201+ let bobAccount : PublicKey , bobKey : PrivateKey ;
202+ let originalFetch : typeof global . fetch ;
203+ let lastFetchOptions : any = null ;
204+
205+ before ( async ( ) => {
206+ Mina . setActiveInstance ( networkWithHeaders ) ;
207+ bobKey = PrivateKey . random ( ) ;
208+ bobAccount = bobKey . toPublicKey ( ) ;
209+ } ) ;
210+
211+ beforeEach ( ( ) => {
212+ originalFetch = global . fetch ;
213+ lastFetchOptions = undefined ;
214+ global . fetch = ( (
215+ input : RequestInfo | URL ,
216+ init ?: RequestInit
217+ ) : Promise < Response > => {
218+ lastFetchOptions = init ;
219+ let url : string ;
220+ if ( typeof input === 'string' ) {
221+ url = input ;
222+ } else if ( input instanceof URL ) {
223+ url = input . toString ( ) ;
224+ } else {
225+ url = input . url ;
226+ }
227+
228+ if ( url . includes ( 'archive.dummy' ) ) {
229+ return Promise . resolve ( {
230+ ok : true ,
231+ json : async ( ) => ( {
232+ data : {
233+ events : [ ] ,
234+ } ,
235+ } ) ,
236+ } as Response ) ;
237+ } else {
238+ return Promise . resolve ( {
239+ ok : true ,
240+ json : async ( ) => ( {
241+ data : { } ,
242+ } ) ,
243+ } as Response ) ;
244+ }
245+ } ) as typeof fetch ;
246+ } ) ;
247+
248+ afterEach ( ( ) => {
249+ global . fetch = originalFetch ;
250+ lastFetchOptions = null ;
251+ } ) ;
252+
253+ it ( 'Simple account update' , async ( ) => {
254+ let txn = await Mina . transaction ( async ( ) => {
255+ const accountUpdateBob = AccountUpdate . create ( bobAccount , Field . from ( 1 ) ) ;
256+ accountUpdateBob . account . balance . requireEquals ( UInt64 . zero ) ;
257+ accountUpdateBob . balance . addInPlace ( UInt64 . one ) ;
258+ } ) ;
259+ await txn . prove ( ) ;
260+ await txn . sign ( [ bobKey ] ) . safeSend ( ) ;
261+
262+ // we can check the headers here too
263+ // expect(lastFetchOptions.headers).toEqual({
264+ // Authorization: 'Bearer archive-default-token',
265+ // 'Content-Type': 'application/json',
266+ // 'X-Test': 'archive-test',
267+ // });
268+ } ) ;
269+
270+ it ( 'Multiple account update' , async ( ) => {
271+ let txn = await Mina . transaction ( async ( ) => {
272+ for ( let index = 0 ; index < 2 ; index ++ ) {
273+ const accountUpdateBob = AccountUpdate . create (
274+ bobAccount ,
275+ Field . from ( index )
276+ ) ;
277+ accountUpdateBob . account . balance . requireEquals ( UInt64 . zero ) ;
278+ accountUpdateBob . balance . addInPlace ( UInt64 . one ) ;
279+ }
280+ } ) ;
281+ await txn . prove ( ) ;
282+ await txn . sign ( [ bobKey ] ) . safeSend ( ) ;
283+ } ) ;
284+
285+ it ( 'More than limit account update' , async ( ) => {
286+ let txn = await Mina . transaction ( async ( ) => {
287+ for ( let index = 0 ; index < 12 ; index ++ ) {
288+ const accountUpdateBob = AccountUpdate . create (
289+ bobAccount ,
290+ Field . from ( index )
291+ ) ;
292+ accountUpdateBob . account . balance . requireEquals ( UInt64 . zero ) ;
293+ accountUpdateBob . balance . addInPlace ( UInt64 . one ) ;
294+ }
295+ } ) ;
296+ await txn . prove ( ) ;
297+ await expect ( txn . sign ( [ bobKey ] ) . safeSend ( ) ) . rejects . toThrow ( ) ;
298+ } ) ;
299+
300+ it ( 'Archive default headers with fetchActions' , async ( ) => {
301+ await Mina . fetchActions ( bobAccount ) ;
302+ expect ( lastFetchOptions . headers ) . toMatchObject ( {
303+ 'Content-Type' : 'application/json' ,
304+ Authorization : 'Bearer archive-default-token' ,
305+ 'X-Test' : 'archive-test' ,
306+ } ) ;
307+ } ) ;
308+
309+ it ( 'Archive default headers with fetchEvents' , async ( ) => {
310+ await Mina . fetchEvents ( bobAccount , TokenId . empty ( ) ) ;
311+ expect ( lastFetchOptions . headers ) . toMatchObject ( {
312+ 'Content-Type' : 'application/json' ,
313+ Authorization : 'Bearer archive-default-token' ,
314+ 'X-Test' : 'archive-test' ,
315+ } ) ;
316+ } ) ;
317+ } ) ;
0 commit comments