@@ -69,29 +69,61 @@ async def get_privy_embedded_wallet(
6969 resp .raise_for_status ()
7070 data = resp .json ()
7171
72- # First, try to find app-first embedded wallet (SDK-created)
73- for acct in data .get ("linked_accounts" , []):
72+ linked_accounts = data .get ("linked_accounts" , [])
73+ logger .info (f"Privy user { user_id } has { len (linked_accounts )} linked accounts" )
74+
75+ # Log all account types for debugging
76+ for i , acct in enumerate (linked_accounts ):
77+ acct_type = acct .get ("type" , "unknown" )
78+ connector_type = acct .get ("connector_type" , "none" )
79+ chain_type = acct .get ("chain_type" , "none" )
80+ delegated = acct .get ("delegated" , False )
81+ has_id = "id" in acct
82+ has_address = "address" in acct
83+ has_public_key = "public_key" in acct
84+ logger .info (
85+ f" Account { i } : type={ acct_type } , connector_type={ connector_type } , "
86+ f"chain_type={ chain_type } , delegated={ delegated } , "
87+ f"has_id={ has_id } , has_address={ has_address } , has_public_key={ has_public_key } "
88+ )
89+
90+ # First, try to find embedded wallet with delegation
91+ for acct in linked_accounts :
7492 if acct .get ("connector_type" ) == "embedded" and acct .get ("delegated" ):
75- return {"wallet_id" : acct ["id" ], "public_key" : acct ["public_key" ]}
93+ wallet_id = acct .get ("id" )
94+ # Use 'address' field if 'public_key' is null (common for API-created wallets)
95+ address = acct .get ("address" ) or acct .get ("public_key" )
96+ if wallet_id and address :
97+ logger .info (
98+ f"Found embedded delegated wallet: { wallet_id } , address: { address } "
99+ )
100+ return {"wallet_id" : wallet_id , "public_key" : address }
76101
77102 # Then, try to find bot-first wallet (API-created via privy_create_wallet)
78103 # These have type == "wallet" and include chain_type
79- for acct in data . get ( " linked_accounts" , []) :
104+ for acct in linked_accounts :
80105 acct_type = acct .get ("type" , "" )
81106 # Check for Solana embedded wallets created via API
82107 if acct_type == "wallet" and acct .get ("chain_type" ) == "solana" :
83108 wallet_id = acct .get ("id" )
84109 # API wallets use "address" field, SDK wallets use "public_key"
85110 address = acct .get ("address" ) or acct .get ("public_key" )
86111 if wallet_id and address :
112+ logger .info (
113+ f"Found bot-first wallet: { wallet_id } , address: { address } "
114+ )
87115 return {"wallet_id" : wallet_id , "public_key" : address }
88116 # Also check for solana_embedded_wallet type
89117 if "solana" in acct_type .lower () and "embedded" in acct_type .lower ():
90118 wallet_id = acct .get ("id" )
91119 address = acct .get ("address" ) or acct .get ("public_key" )
92120 if wallet_id and address :
121+ logger .info (
122+ f"Found solana_embedded_wallet: { wallet_id } , address: { address } "
123+ )
93124 return {"wallet_id" : wallet_id , "public_key" : address }
94125
126+ logger .warning (f"No suitable wallet found for user { user_id } " )
95127 return None
96128
97129
0 commit comments