diff --git a/token/services/db/sql/wallet.go b/token/services/db/sql/wallet.go index 3c65a6202..8091b03fc 100644 --- a/token/services/db/sql/wallet.go +++ b/token/services/db/sql/wallet.go @@ -50,6 +50,10 @@ func NewWalletDB(kvs *kvs.KVS, tmsID token.TMSID, db *sql.DB, tablePrefix, name } func (db *WalletDB) StoreWalletID(wID driver.WalletID) error { + if db.walletExists(wID) { + logger.Debugf("Wallet already stored.") + return nil + } query := fmt.Sprintf("INSERT INTO %s (wallet_id) VALUES ($1)", db.table.Wallets) logger.Debug(query) @@ -163,6 +167,19 @@ func (db *WalletDB) LoadMeta(identity view.Identity, meta any) error { } +func (db *WalletDB) walletExists(wID driver.WalletID) bool { + result, err := QueryUnique[driver.WalletID](db.db, + fmt.Sprintf("SELECT meta FROM %s WHERE wallet_id=$1", db.table.Wallets), + wID, + ) + if err != nil { + logger.Errorf("DB err: Failed looking up identity [%s]: %w", wID, err) + } + logger.Infof("Found identity for [%v]: %v", wID, result) + + return result != "" +} + func (db *WalletDB) IdentityExists(identity view.Identity, wID driver.WalletID) bool { result, err := QueryUnique[driver.WalletID](db.db, fmt.Sprintf("SELECT meta FROM %s WHERE identity_id=$1 AND wallet_id=$2", db.table.Wallets),