Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions Runtime/codebase/SessionWallet.cs
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,24 @@ public void SignInitSessionTx(Transaction tx)
{
tx.PartialSign(new[] { _externalWallet.Account, Account });
}

public static SessionWallet GetSessionWallet(string publicKey, string privateKey, PublicKey targetProgram)
{
_externalWallet = Web3.Wallet;

var sessionAccount = new Account(privateKey, publicKey);

// TODO: ActiveRpcClient can be null, get node address some other way
var sessionWallet = new SessionWallet(_externalWallet.RpcCluster, _externalWallet.ActiveRpcClient.NodeAddress.ToString())
{
TargetProgram = targetProgram,
EncryptedKeystoreKey = $"{_externalWallet.Account.PublicKey}_SessionKeyStore",
SessionTokenPDA = FindSessionToken(targetProgram, sessionAccount, _externalWallet.Account),
Account = sessionAccount,
};

return sessionWallet;
}
Comment on lines +69 to +85
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🔴 Critical

Missing singleton guard — will throw if Instance is already set.

The other GetSessionWallet overloads (lines 97 and 129) check if (Instance != null) return Instance; before constructing a new SessionWallet. This overload skips that check, so calling it when a session wallet already exists will hit the throw new Exception("SessionWallet already exists") in the constructor (line 37).

Additionally, Web3.Wallet (line 71) and _externalWallet.ActiveRpcClient (line 76) can both be null, which would cause a NullReferenceException.

Proposed fix
 public static SessionWallet GetSessionWallet(string publicKey, string privateKey, PublicKey targetProgram)
 {
+    if (Instance != null) return Instance;
+
     _externalWallet = Web3.Wallet;
+    if (_externalWallet?.ActiveRpcClient == null)
+        throw new InvalidOperationException("External wallet or its RPC client is not initialized.");

     var sessionAccount = new Account(privateKey, publicKey); 
     
-    // TODO: ActiveRpcClient can be null, get node address some other way
     var sessionWallet = new SessionWallet(_externalWallet.RpcCluster, _externalWallet.ActiveRpcClient.NodeAddress.ToString())
🤖 Prompt for AI Agents
In `@Runtime/codebase/SessionWallet.cs` around lines 69 - 85,
GetSessionWallet(string publicKey, string privateKey, PublicKey targetProgram)
lacks the singleton guard and null checks: add the same early return used in the
other overloads (if (Instance != null) return Instance;) before constructing a
new SessionWallet, and validate Web3.Wallet and its ActiveRpcClient (e.g., if
(Web3.Wallet == null) throw/new error or handle; if
(_externalWallet.ActiveRpcClient == null) get node address another way or throw)
to avoid NullReferenceException; ensure you still set _externalWallet =
Web3.Wallet, create sessionAccount, compute SessionTokenPDA via
FindSessionToken, and populate SessionWallet fields only after these checks.


/// <summary>
/// Creates a new SessionWallet instance based on the signature we get from signing a specific message.
Expand Down
Loading