@@ -12,6 +12,7 @@ export const Aliases = ['accounts', 'contracts', 'artifacts', 'secrets', 'transa
1212export type AliasType = ( typeof Aliases ) [ number ] ;
1313
1414export class WalletDB {
15+ #store! : AztecAsyncKVStore ;
1516 #accounts! : AztecAsyncMap < string , Buffer > ;
1617 #aliases! : AztecAsyncMap < string , Buffer > ;
1718 #bridgedFeeJuice! : AztecAsyncMap < string , Buffer > ;
@@ -29,6 +30,7 @@ export class WalletDB {
2930 }
3031
3132 async init ( store : AztecAsyncKVStore ) {
33+ this . #store = store ;
3234 this . #accounts = store . openMap ( 'accounts' ) ;
3335 this . #aliases = store . openMap ( 'aliases' ) ;
3436 this . #bridgedFeeJuice = store . openMap ( 'bridgedFeeJuice' ) ;
@@ -41,14 +43,17 @@ export class WalletDB {
4143 }
4244
4345 async pushBridgedFeeJuice ( recipient : AztecAddress , secret : Fr , amount : bigint , leafIndex : bigint , log : LogFn ) {
44- let stackPointer = ( await this . #bridgedFeeJuice. getAsync ( `${ recipient . toString ( ) } :stackPointer` ) ) ?. readInt8 ( ) || 0 ;
45- stackPointer ++ ;
46- await this . #bridgedFeeJuice. set (
47- `${ recipient . toString ( ) } :${ stackPointer } ` ,
48- Buffer . from ( `${ amount . toString ( ) } :${ secret . toString ( ) } :${ leafIndex . toString ( ) } ` ) ,
49- ) ;
50- await this . #bridgedFeeJuice. set ( `${ recipient . toString ( ) } :stackPointer` , Buffer . from ( [ stackPointer ] ) ) ;
51- log ( `Pushed ${ amount } fee juice for recipient ${ recipient . toString ( ) } . Stack pointer ${ stackPointer } ` ) ;
46+ await this . #store. transactionAsync ( async ( ) => {
47+ let stackPointer =
48+ ( await this . #bridgedFeeJuice. getAsync ( `${ recipient . toString ( ) } :stackPointer` ) ) ?. readInt8 ( ) || 0 ;
49+ stackPointer ++ ;
50+ await this . #bridgedFeeJuice. set (
51+ `${ recipient . toString ( ) } :${ stackPointer } ` ,
52+ Buffer . from ( `${ amount . toString ( ) } :${ secret . toString ( ) } :${ leafIndex . toString ( ) } ` ) ,
53+ ) ;
54+ await this . #bridgedFeeJuice. set ( `${ recipient . toString ( ) } :stackPointer` , Buffer . from ( [ stackPointer ] ) ) ;
55+ log ( `Pushed ${ amount } fee juice for recipient ${ recipient . toString ( ) } . Stack pointer ${ stackPointer } ` ) ;
56+ } ) ;
5257 }
5358
5459 async popBridgedFeeJuice ( recipient : AztecAddress , log : LogFn ) {
@@ -76,19 +81,24 @@ export class WalletDB {
7681 } : { type : AccountType ; secretKey : Fr ; salt : Fr ; alias : string | undefined ; publicKey : string | undefined } ,
7782 log : LogFn ,
7883 ) {
79- if ( alias ) {
80- await this . #aliases. set ( `accounts:${ alias } ` , Buffer . from ( address . toString ( ) ) ) ;
81- }
82- await this . #accounts. set ( `${ address . toString ( ) } :type` , Buffer . from ( type ) ) ;
83- await this . #accounts. set ( `${ address . toString ( ) } :sk` , secretKey . toBuffer ( ) ) ;
84- await this . #accounts. set ( `${ address . toString ( ) } :salt` , salt . toBuffer ( ) ) ;
84+ let publicSigningKey : Buffer | undefined ;
8585 if ( type === 'ecdsasecp256r1ssh' && publicKey ) {
86- const publicSigningKey = extractECDSAPublicKeyFromBase64String ( publicKey ) ;
87- await this . storeAccountMetadata ( address , 'publicSigningKey' , publicSigningKey ) ;
86+ publicSigningKey = extractECDSAPublicKeyFromBase64String ( publicKey ) ;
8887 }
89- await this . #aliases. set ( 'accounts:last' , Buffer . from ( address . toString ( ) ) ) ;
90- log ( `Account stored in database with alias${ alias ? `es last & ${ alias } ` : ' last' } ` ) ;
9188
89+ await this . #store. transactionAsync ( async ( ) => {
90+ if ( alias ) {
91+ await this . #aliases. set ( `accounts:${ alias } ` , Buffer . from ( address . toString ( ) ) ) ;
92+ }
93+ await this . #accounts. set ( `${ address . toString ( ) } :type` , Buffer . from ( type ) ) ;
94+ await this . #accounts. set ( `${ address . toString ( ) } :sk` , secretKey . toBuffer ( ) ) ;
95+ await this . #accounts. set ( `${ address . toString ( ) } :salt` , salt . toBuffer ( ) ) ;
96+ if ( publicSigningKey ) {
97+ await this . #accounts. set ( `${ address . toString ( ) } :publicSigningKey` , publicSigningKey ) ;
98+ }
99+ await this . #aliases. set ( 'accounts:last' , Buffer . from ( address . toString ( ) ) ) ;
100+ } ) ;
101+ log ( `Account stored in database with alias${ alias ? `es last & ${ alias } ` : ' last' } ` ) ;
92102 await this . refreshAliasCache ( ) ;
93103 }
94104
@@ -100,35 +110,38 @@ export class WalletDB {
100110 }
101111
102112 async storeContract ( address : AztecAddress , artifactPath : string , log : LogFn , alias ?: string ) {
103- if ( alias ) {
104- await this . #aliases. set ( `contracts:${ alias } ` , Buffer . from ( address . toString ( ) ) ) ;
105- await this . #aliases. set ( `artifacts:${ alias } ` , Buffer . from ( artifactPath ) ) ;
106- }
107- await this . #aliases. set ( `contracts:last` , Buffer . from ( address . toString ( ) ) ) ;
108- await this . #aliases. set ( `artifacts:last` , Buffer . from ( artifactPath ) ) ;
109- await this . #aliases. set ( `artifacts:${ address . toString ( ) } ` , Buffer . from ( artifactPath ) ) ;
113+ await this . #store. transactionAsync ( async ( ) => {
114+ if ( alias ) {
115+ await this . #aliases. set ( `contracts:${ alias } ` , Buffer . from ( address . toString ( ) ) ) ;
116+ await this . #aliases. set ( `artifacts:${ alias } ` , Buffer . from ( artifactPath ) ) ;
117+ }
118+ await this . #aliases. set ( `contracts:last` , Buffer . from ( address . toString ( ) ) ) ;
119+ await this . #aliases. set ( `artifacts:last` , Buffer . from ( artifactPath ) ) ;
120+ await this . #aliases. set ( `artifacts:${ address . toString ( ) } ` , Buffer . from ( artifactPath ) ) ;
121+ } ) ;
110122 log ( `Contract stored in database with alias${ alias ? `es last & ${ alias } ` : ' last' } ` ) ;
111-
112123 await this . refreshAliasCache ( ) ;
113124 }
114125
115126 async storeAuthwitness ( authWit : AuthWitness , log : LogFn , alias ?: string ) {
116- if ( alias ) {
117- await this . #aliases. set ( `authwits:${ alias } ` , Buffer . from ( authWit . toString ( ) ) ) ;
118- }
119- await this . #aliases. set ( `authwits:last` , Buffer . from ( authWit . toString ( ) ) ) ;
127+ await this . #store. transactionAsync ( async ( ) => {
128+ if ( alias ) {
129+ await this . #aliases. set ( `authwits:${ alias } ` , Buffer . from ( authWit . toString ( ) ) ) ;
130+ }
131+ await this . #aliases. set ( `authwits:last` , Buffer . from ( authWit . toString ( ) ) ) ;
132+ } ) ;
120133 log ( `Authorization witness stored in database with alias${ alias ? `es last & ${ alias } ` : ' last' } ` ) ;
121-
122134 await this . refreshAliasCache ( ) ;
123135 }
124136
125137 async storeTx ( { txHash } : { txHash : TxHash } , log : LogFn , alias ?: string ) {
126- if ( alias ) {
127- await this . #aliases. set ( `transactions:${ alias } ` , Buffer . from ( txHash . toString ( ) ) ) ;
128- }
129- await this . #aliases. set ( `transactions:last` , Buffer . from ( txHash . toString ( ) ) ) ;
138+ await this . #store. transactionAsync ( async ( ) => {
139+ if ( alias ) {
140+ await this . #aliases. set ( `transactions:${ alias } ` , Buffer . from ( txHash . toString ( ) ) ) ;
141+ }
142+ await this . #aliases. set ( `transactions:last` , Buffer . from ( txHash . toString ( ) ) ) ;
143+ } ) ;
130144 log ( `Transaction hash stored in database with alias${ alias ? `es last & ${ alias } ` : ' last' } ` ) ;
131-
132145 await this . refreshAliasCache ( ) ;
133146 }
134147
0 commit comments