@@ -9,12 +9,10 @@ import { validateStacksAddress } from '@stacks/transactions';
99
1010export class StacksAccountDO extends DurableObject < Env > {
1111 private accountDataService : StacksAccountDataService ;
12- private address : string ;
1312
1413 constructor ( ctx : DurableObjectState , env : Env ) {
1514 super ( ctx , env ) ;
1615 this . env = env ;
17- this . address = ctx . id . toString ( ) ;
1816
1917 const config = AppConfig . getInstance ( env ) . getConfig ( ) ;
2018 const hiroConfig = config . HIRO_API_RATE_LIMIT ;
@@ -31,24 +29,26 @@ export class StacksAccountDO extends DurableObject<Env> {
3129
3230 async fetch ( request : Request ) : Promise < Response > {
3331 const url = new URL ( request . url ) ;
32+ const path = url . pathname ;
33+ const address = path . split ( '/' ) [ 2 ] ;
3434 // e.g., /stacks-account/{address}/nonce -> /nonce
35- const endpoint = url . pathname . replace ( `/stacks-account/${ this . address } ` , '' ) || '/' ;
35+ const endpoint = url . pathname . replace ( `/stacks-account/${ address } ` , '' ) || '/' ;
3636 const method = request . method ;
3737
3838 return handleRequest (
3939 async ( ) => {
40- if ( ! validateStacksAddress ( this . address ) ) {
41- throw new ApiError ( ErrorCode . INVALID_CONTRACT_ADDRESS , { address : this . address } ) ;
40+ if ( ! validateStacksAddress ( address ) ) {
41+ throw new ApiError ( ErrorCode . INVALID_CONTRACT_ADDRESS , { address : address } ) ;
4242 }
4343
4444 // Route to different functions based on the endpoint
4545 if ( endpoint . startsWith ( '/nonce' ) ) {
46- return this . handleNonceRequest ( request , endpoint ) ;
46+ return this . handleNonceRequest ( request , endpoint , address ) ;
4747 }
4848
4949 // Default response for the root of the DO
5050 if ( endpoint === '/' ) {
51- return { message : `StacksAccountDO for ${ this . address } . Supported endpoints: /nonce` } ;
51+ return { message : `StacksAccountDO for ${ address } . Supported endpoints: /nonce` } ;
5252 }
5353
5454 throw new ApiError ( ErrorCode . NOT_FOUND , { resource : endpoint } ) ;
@@ -58,17 +58,17 @@ export class StacksAccountDO extends DurableObject<Env> {
5858 ) ;
5959 }
6060
61- private async handleNonceRequest ( request : Request , endpoint : string ) : Promise < { nonce : number } > {
61+ private async handleNonceRequest ( request : Request , endpoint : string , address : string ) : Promise < { nonce : number } > {
6262 const url = new URL ( request . url ) ;
6363 const method = request . method ;
6464
6565 if ( endpoint === '/nonce' && method === 'GET' ) {
6666 const bustCache = url . searchParams . get ( 'bustCache' ) === 'true' ;
67- return this . getNonce ( bustCache ) ;
67+ return this . getNonce ( address , bustCache ) ;
6868 }
6969
7070 if ( endpoint === '/nonce/sync' && method === 'POST' ) {
71- return this . syncNonce ( ) ;
71+ return this . syncNonce ( address ) ;
7272 }
7373
7474 if ( endpoint === '/nonce/update' && method === 'POST' ) {
@@ -82,19 +82,19 @@ export class StacksAccountDO extends DurableObject<Env> {
8282 throw new ApiError ( ErrorCode . INVALID_REQUEST , { reason : `Method ${ method } not supported for ${ endpoint } ` } ) ;
8383 }
8484
85- private async getNonce ( bustCache : boolean ) : Promise < { nonce : number } > {
85+ private async getNonce ( address : string , bustCache : boolean ) : Promise < { nonce : number } > {
8686 if ( ! bustCache ) {
8787 const storedNonce = await this . ctx . storage . get < number > ( 'nonce' ) ;
8888 if ( storedNonce !== undefined ) {
8989 return { nonce : storedNonce } ;
9090 }
9191 }
9292 // If cache is busted or nonce is not in storage, sync it
93- return this . syncNonce ( ) ;
93+ return this . syncNonce ( address ) ;
9494 }
9595
96- private async syncNonce ( ) : Promise < { nonce : number } > {
97- const nonce = await this . accountDataService . fetchNonce ( this . address ) ;
96+ private async syncNonce ( address : string ) : Promise < { nonce : number } > {
97+ const nonce = await this . accountDataService . fetchNonce ( address ) ;
9898 await this . ctx . storage . put ( 'nonce' , nonce ) ;
9999 return { nonce } ;
100100 }
0 commit comments