2023-11-19 17:29:17 +00:00
|
|
|
package walletconnect
|
|
|
|
|
|
|
|
import (
|
|
|
|
"database/sql"
|
2023-11-26 15:50:12 +00:00
|
|
|
"errors"
|
2023-11-19 17:29:17 +00:00
|
|
|
)
|
|
|
|
|
2023-12-13 14:05:55 +00:00
|
|
|
type DbSession struct {
|
|
|
|
Topic Topic `json:"topic"`
|
|
|
|
PairingTopic Topic `json:"pairingTopic"`
|
|
|
|
Expiry int64 `json:"expiry"`
|
|
|
|
Active bool `json:"active"`
|
|
|
|
DappName string `json:"dappName"`
|
|
|
|
DappURL string `json:"dappUrl"`
|
|
|
|
DappDescription string `json:"dappDescription"`
|
|
|
|
DappIcon string `json:"dappIcon"`
|
|
|
|
DappVerifyURL string `json:"dappVerifyUrl"`
|
|
|
|
DappPublicKey string `json:"dappPublicKey"`
|
2023-11-19 17:29:17 +00:00
|
|
|
}
|
|
|
|
|
2023-12-13 14:05:55 +00:00
|
|
|
func UpsertSession(db *sql.DB, session DbSession) error {
|
|
|
|
insertSQL := `
|
|
|
|
INSERT OR IGNORE INTO
|
|
|
|
wallet_connect_sessions (topic, pairing_topic, expiry, active)
|
|
|
|
VALUES
|
|
|
|
(?, ?, ?, ?);
|
|
|
|
|
|
|
|
UPDATE
|
|
|
|
wallet_connect_sessions
|
|
|
|
SET
|
|
|
|
pairing_topic = ?,
|
|
|
|
expiry = ?,
|
|
|
|
active = ?,
|
|
|
|
dapp_name = ?,
|
|
|
|
dapp_url = ?,
|
|
|
|
dapp_description = ?,
|
|
|
|
dapp_icon = ?,
|
|
|
|
dapp_verify_url = ?,
|
|
|
|
dapp_publicKey = ?
|
|
|
|
WHERE
|
|
|
|
topic = ?;`
|
|
|
|
|
|
|
|
_, err := db.Exec(insertSQL,
|
|
|
|
session.Topic,
|
|
|
|
session.PairingTopic,
|
|
|
|
session.Expiry,
|
|
|
|
session.Active,
|
|
|
|
session.PairingTopic,
|
|
|
|
session.Expiry,
|
|
|
|
session.Active,
|
|
|
|
session.DappName,
|
|
|
|
session.DappURL,
|
|
|
|
session.DappDescription,
|
|
|
|
session.DappIcon,
|
|
|
|
session.DappVerifyURL,
|
|
|
|
session.DappPublicKey,
|
|
|
|
session.Topic,
|
|
|
|
)
|
2023-11-19 17:29:17 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-12-13 14:05:55 +00:00
|
|
|
func ChangeSessionState(db *sql.DB, topic Topic, active bool) error {
|
|
|
|
stmt, err := db.Prepare("UPDATE wallet_connect_sessions SET active = ? WHERE topic = ?")
|
2023-11-26 15:50:12 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
res, err := stmt.Exec(active, topic)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
rowsAffected, err := res.RowsAffected()
|
2023-11-29 13:34:08 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-11-26 15:50:12 +00:00
|
|
|
if rowsAffected == 0 {
|
2023-12-13 14:05:55 +00:00
|
|
|
return errors.New("unable to locate session for DB state change")
|
2023-11-26 15:50:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-12-13 14:05:55 +00:00
|
|
|
func GetSessionByTopic(db *sql.DB, topic Topic) (*DbSession, error) {
|
|
|
|
querySQL := `
|
|
|
|
SELECT *
|
|
|
|
FROM
|
|
|
|
wallet_connect_sessions
|
|
|
|
WHERE
|
|
|
|
topic = ?`
|
2023-11-19 17:29:17 +00:00
|
|
|
|
|
|
|
row := db.QueryRow(querySQL, topic)
|
|
|
|
|
2023-12-13 14:05:55 +00:00
|
|
|
var session DbSession
|
|
|
|
err := row.Scan(&session.Topic,
|
|
|
|
&session.PairingTopic,
|
|
|
|
&session.Expiry,
|
|
|
|
&session.Active,
|
|
|
|
&session.DappName,
|
|
|
|
&session.DappURL,
|
|
|
|
&session.DappDescription,
|
|
|
|
&session.DappIcon,
|
|
|
|
&session.DappVerifyURL,
|
|
|
|
&session.DappPublicKey)
|
2023-11-19 17:29:17 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2023-12-13 14:05:55 +00:00
|
|
|
return &session, nil
|
2023-11-19 17:29:17 +00:00
|
|
|
}
|
|
|
|
|
2023-12-13 14:05:55 +00:00
|
|
|
func GetSessionsByPairingTopic(db *sql.DB, pairingTopic Topic) ([]DbSession, error) {
|
|
|
|
querySQL := `
|
|
|
|
SELECT *
|
|
|
|
FROM
|
|
|
|
wallet_connect_sessions
|
|
|
|
WHERE
|
|
|
|
pairing_topic = ?`
|
2023-11-19 17:29:17 +00:00
|
|
|
|
2023-12-13 14:05:55 +00:00
|
|
|
rows, err := db.Query(querySQL, pairingTopic)
|
2023-11-19 17:29:17 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer rows.Close()
|
|
|
|
|
2023-12-13 14:05:55 +00:00
|
|
|
sessions := make([]DbSession, 0, 2)
|
2023-11-19 17:29:17 +00:00
|
|
|
for rows.Next() {
|
2023-12-13 14:05:55 +00:00
|
|
|
var session DbSession
|
|
|
|
err := rows.Scan(&session.Topic,
|
|
|
|
&session.PairingTopic,
|
|
|
|
&session.Expiry,
|
|
|
|
&session.Active,
|
|
|
|
&session.DappName,
|
|
|
|
&session.DappURL,
|
|
|
|
&session.DappDescription,
|
|
|
|
&session.DappIcon,
|
|
|
|
&session.DappVerifyURL,
|
|
|
|
&session.DappPublicKey)
|
2023-11-19 17:29:17 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2023-12-13 14:05:55 +00:00
|
|
|
|
|
|
|
sessions = append(sessions, session)
|
2023-11-19 17:29:17 +00:00
|
|
|
}
|
2023-12-13 14:05:55 +00:00
|
|
|
|
2023-11-19 17:29:17 +00:00
|
|
|
if err := rows.Err(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2023-12-13 14:05:55 +00:00
|
|
|
return sessions, nil
|
2023-11-19 17:29:17 +00:00
|
|
|
}
|
|
|
|
|
2023-12-13 14:05:55 +00:00
|
|
|
// GetActiveSessions returns all active sessions (active and not expired) that have an expiry timestamp newer or equal to the given timestamp.
|
|
|
|
func GetActiveSessions(db *sql.DB, expiryNotOlderThanTimestamp int64) ([]DbSession, error) {
|
|
|
|
querySQL := `
|
|
|
|
SELECT *
|
|
|
|
FROM
|
|
|
|
wallet_connect_sessions
|
|
|
|
WHERE
|
|
|
|
active != 0 AND
|
|
|
|
expiry >= ?
|
|
|
|
ORDER BY
|
|
|
|
expiry DESC`
|
2023-11-19 17:29:17 +00:00
|
|
|
|
2023-12-13 14:05:55 +00:00
|
|
|
rows, err := db.Query(querySQL, expiryNotOlderThanTimestamp)
|
2023-11-19 17:29:17 +00:00
|
|
|
if err != nil {
|
2023-12-13 14:05:55 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer rows.Close()
|
|
|
|
|
|
|
|
sessions := make([]DbSession, 0, 2)
|
|
|
|
for rows.Next() {
|
|
|
|
var session DbSession
|
|
|
|
err := rows.Scan(&session.Topic,
|
|
|
|
&session.PairingTopic,
|
|
|
|
&session.Expiry,
|
|
|
|
&session.Active,
|
|
|
|
&session.DappName,
|
|
|
|
&session.DappURL,
|
|
|
|
&session.DappDescription,
|
|
|
|
&session.DappIcon,
|
|
|
|
&session.DappVerifyURL,
|
|
|
|
&session.DappPublicKey)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
sessions = append(sessions, session)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := rows.Err(); err != nil {
|
|
|
|
return nil, err
|
2023-11-19 17:29:17 +00:00
|
|
|
}
|
|
|
|
|
2023-12-13 14:05:55 +00:00
|
|
|
return sessions, nil
|
2023-11-19 17:29:17 +00:00
|
|
|
}
|