@@ -182,41 +182,42 @@ func getSessionKey(session *Session) []byte {
182
182
return session .LocalPublicKey .SerializeCompressed ()
183
183
}
184
184
185
- // NewSession creates a new session with the given user-defined parameters.
186
- //
187
- // NOTE: currently this purely a constructor of the Session type and does not
188
- // make any database calls. This will be changed in a future commit.
185
+ // NewSession creates and persists a new session with the given user-defined
186
+ // parameters. The initial state of the session will be Reserved until
187
+ // ShiftState is called with StateCreated.
189
188
//
190
189
// NOTE: this is part of the Store interface.
191
- func (db * BoltStore ) NewSession (id ID , localPrivKey * btcec.PrivateKey ,
192
- label string , typ Type , expiry time.Time , serverAddr string ,
193
- devServer bool , perms []bakery.Op , caveats []macaroon.Caveat ,
194
- featureConfig FeaturesConfig , privacy bool , linkedGroupID * ID ,
195
- flags PrivacyFlags ) (* Session , error ) {
196
-
197
- return buildSession (
198
- id , localPrivKey , label , typ , db .clock .Now (), expiry ,
199
- serverAddr , devServer , perms , caveats , featureConfig , privacy ,
200
- linkedGroupID , flags ,
201
- )
202
- }
190
+ func (db * BoltStore ) NewSession (label string , typ Type , expiry time.Time ,
191
+ serverAddr string , devServer bool , perms []bakery.Op ,
192
+ caveats []macaroon.Caveat , featureConfig FeaturesConfig , privacy bool ,
193
+ linkedGroupID * ID , flags PrivacyFlags ) (* Session , error ) {
203
194
204
- // CreateSession adds a new session to the store. If a session with the same
205
- // local public key already exists an error is returned.
206
- //
207
- // NOTE: this is part of the Store interface.
208
- func (db * BoltStore ) CreateSession (session * Session ) error {
209
- sessionKey := getSessionKey (session )
210
-
211
- return db .Update (func (tx * bbolt.Tx ) error {
195
+ var session * Session
196
+ err := db .Update (func (tx * bbolt.Tx ) error {
212
197
sessionBucket , err := getBucket (tx , sessionBucketKey )
213
198
if err != nil {
214
199
return err
215
200
}
216
201
202
+ id , localPrivKey , err := getUnusedIDAndKeyPair (sessionBucket )
203
+ if err != nil {
204
+ return err
205
+ }
206
+
207
+ session , err = buildSession (
208
+ id , localPrivKey , label , typ , db .clock .Now (), expiry ,
209
+ serverAddr , devServer , perms , caveats , featureConfig ,
210
+ privacy , linkedGroupID , flags ,
211
+ )
212
+ if err != nil {
213
+ return err
214
+ }
215
+
216
+ sessionKey := getSessionKey (session )
217
+
217
218
if len (sessionBucket .Get (sessionKey )) != 0 {
218
- return fmt .Errorf ("session with local public " +
219
- "key(%x) already exists" ,
219
+ return fmt .Errorf ("session with local public key(%x) " +
220
+ "already exists" ,
220
221
session .LocalPublicKey .SerializeCompressed ())
221
222
}
222
223
@@ -275,6 +276,11 @@ func (db *BoltStore) CreateSession(session *Session) error {
275
276
276
277
return putSession (sessionBucket , session )
277
278
})
279
+ if err != nil {
280
+ return nil , err
281
+ }
282
+
283
+ return session , nil
278
284
}
279
285
280
286
// UpdateSessionRemotePubKey can be used to add the given remote pub key
@@ -577,53 +583,35 @@ func (db *BoltStore) GetSessionByID(id ID) (*Session, error) {
577
583
return session , nil
578
584
}
579
585
580
- // GetUnusedIDAndKeyPair can be used to generate a new, unused, local private
586
+ // getUnusedIDAndKeyPair can be used to generate a new, unused, local private
581
587
// key and session ID pair. Care must be taken to ensure that no other thread
582
588
// calls this before the returned ID and key pair from this method are either
583
589
// used or discarded.
584
- //
585
- // NOTE: this is part of the Store interface.
586
- func (db * BoltStore ) GetUnusedIDAndKeyPair () (ID , * btcec.PrivateKey , error ) {
587
- var (
588
- id ID
589
- privKey * btcec.PrivateKey
590
- )
591
- err := db .Update (func (tx * bbolt.Tx ) error {
592
- sessionBucket , err := getBucket (tx , sessionBucketKey )
593
- if err != nil {
594
- return err
595
- }
596
-
597
- idIndexBkt := sessionBucket .Bucket (idIndexKey )
598
- if idIndexBkt == nil {
599
- return ErrDBInitErr
600
- }
590
+ func getUnusedIDAndKeyPair (bucket * bbolt.Bucket ) (ID , * btcec.PrivateKey ,
591
+ error ) {
601
592
602
- // Spin until we find a key with an ID that does not collide
603
- // with any of our existing IDs.
604
- for {
605
- // Generate a new private key and ID pair.
606
- privKey , id , err = NewSessionPrivKeyAndID ()
607
- if err != nil {
608
- return err
609
- }
593
+ idIndexBkt := bucket .Bucket (idIndexKey )
594
+ if idIndexBkt == nil {
595
+ return ID {}, nil , ErrDBInitErr
596
+ }
610
597
611
- // Check that no such ID exits in our id-to-key index.
612
- idBkt := idIndexBkt .Bucket (id [:])
613
- if idBkt != nil {
614
- continue
615
- }
598
+ // Spin until we find a key with an ID that does not collide with any of
599
+ // our existing IDs.
600
+ for {
601
+ // Generate a new private key and ID pair.
602
+ privKey , id , err := NewSessionPrivKeyAndID ()
603
+ if err != nil {
604
+ return ID {}, nil , err
605
+ }
616
606
617
- break
607
+ // Check that no such ID exits in our id-to-key index.
608
+ idBkt := idIndexBkt .Bucket (id [:])
609
+ if idBkt != nil {
610
+ continue
618
611
}
619
612
620
- return nil
621
- })
622
- if err != nil {
623
- return id , nil , err
613
+ return id , privKey , nil
624
614
}
625
-
626
- return id , privKey , nil
627
615
}
628
616
629
617
// GetGroupID will return the group ID for the given session ID.
0 commit comments