diff --git a/aperturedb/sqlc/lnc_sessions.sql.go b/aperturedb/sqlc/lnc_sessions.sql.go new file mode 100644 index 0000000..fa6b479 --- /dev/null +++ b/aperturedb/sqlc/lnc_sessions.sql.go @@ -0,0 +1,99 @@ +// Code generated by sqlc. DO NOT EDIT. +// versions: +// sqlc v1.18.0 +// source: lnc_sessions.sql + +package sqlc + +import ( + "context" + "database/sql" + "time" +) + +const getSession = `-- name: GetSession :one +SELECT id, passphrase_words, passphrase_entropy, remote_static_pub_key, local_static_priv_key, mailbox_addr, created_at, expiry, dev_server +FROM lnc_sessions +WHERE passphrase_entropy = $1 +` + +func (q *Queries) GetSession(ctx context.Context, passphraseEntropy []byte) (LncSession, error) { + row := q.db.QueryRowContext(ctx, getSession, passphraseEntropy) + var i LncSession + err := row.Scan( + &i.ID, + &i.PassphraseWords, + &i.PassphraseEntropy, + &i.RemoteStaticPubKey, + &i.LocalStaticPrivKey, + &i.MailboxAddr, + &i.CreatedAt, + &i.Expiry, + &i.DevServer, + ) + return i, err +} + +const insertSession = `-- name: InsertSession :exec +INSERT INTO lnc_sessions ( + passphrase_words, passphrase_entropy, local_static_priv_key, mailbox_addr, + created_at, expiry, dev_server +) VALUES ( + $1, $2, $3, $4, $5, $6, $7 +) +` + +type InsertSessionParams struct { + PassphraseWords string + PassphraseEntropy []byte + LocalStaticPrivKey []byte + MailboxAddr string + CreatedAt time.Time + Expiry sql.NullTime + DevServer bool +} + +func (q *Queries) InsertSession(ctx context.Context, arg InsertSessionParams) error { + _, err := q.db.ExecContext(ctx, insertSession, + arg.PassphraseWords, + arg.PassphraseEntropy, + arg.LocalStaticPrivKey, + arg.MailboxAddr, + arg.CreatedAt, + arg.Expiry, + arg.DevServer, + ) + return err +} + +const setExpiry = `-- name: SetExpiry :exec +UPDATE lnc_sessions +SET expiry=$1 +WHERE passphrase_entropy=$2 +` + +type SetExpiryParams struct { + Expiry sql.NullTime + PassphraseEntropy []byte +} + +func (q *Queries) SetExpiry(ctx context.Context, arg SetExpiryParams) error { + _, err := q.db.ExecContext(ctx, setExpiry, arg.Expiry, arg.PassphraseEntropy) + return err +} + +const setRemotePubKey = `-- name: SetRemotePubKey :exec +UPDATE lnc_sessions +SET remote_static_pub_key=$1 +WHERE passphrase_entropy=$2 +` + +type SetRemotePubKeyParams struct { + RemoteStaticPubKey []byte + PassphraseEntropy []byte +} + +func (q *Queries) SetRemotePubKey(ctx context.Context, arg SetRemotePubKeyParams) error { + _, err := q.db.ExecContext(ctx, setRemotePubKey, arg.RemoteStaticPubKey, arg.PassphraseEntropy) + return err +} diff --git a/aperturedb/sqlc/migrations/000003_lnc_sessions.down.sql b/aperturedb/sqlc/migrations/000003_lnc_sessions.down.sql new file mode 100644 index 0000000..0eef9c1 --- /dev/null +++ b/aperturedb/sqlc/migrations/000003_lnc_sessions.down.sql @@ -0,0 +1,3 @@ +DROP INDEX IF EXISTS lnc_sessions_passphrase_entropy_idx; +DROP INDEX IF EXISTS lnc_sessions_label_idx; +DROP TABLE IF EXISTS lnc_sessions; diff --git a/aperturedb/sqlc/migrations/000003_lnc_sessions.up.sql b/aperturedb/sqlc/migrations/000003_lnc_sessions.up.sql new file mode 100644 index 0000000..e2d5ac0 --- /dev/null +++ b/aperturedb/sqlc/migrations/000003_lnc_sessions.up.sql @@ -0,0 +1,32 @@ +-- lnc_sessions is table used to store data about LNC sesssions. +CREATE TABLE IF NOT EXISTS lnc_sessions ( + id INTEGER PRIMARY KEY, + + -- The passphrase words used to derive the passphrase entropy. + passphrase_words TEXT NOT NULL UNIQUE, + + -- The entropy bytes to be used for mask the local ephemeral key during the + -- first step of the Noise XX handshake. + passphrase_entropy BLOB NOT NULL UNIQUE, + + -- The remote static key being used for the connection. + remote_static_pub_key BLOB UNIQUE, + + -- The local static key being used for the connection. + local_static_priv_key BLOB NOT NULL, + + -- mailbox_addr is the address of the mailbox used for the session. + mailbox_addr TEXT NOT NULL, + + -- created_at is the time the session was created. + created_at TIMESTAMP NOT NULL, + + -- expiry is the time the session will expire. + expiry TIMESTAMP, + + -- dev_server signals if we need to skip the verification of the server's + -- tls certificate. + dev_server BOOL NOT NULL +); + +CREATE INDEX IF NOT EXISTS lnc_sessions_passphrase_entropy_idx ON lnc_sessions(passphrase_entropy); diff --git a/aperturedb/sqlc/models.go b/aperturedb/sqlc/models.go index 8d70fdd..651eb47 100644 --- a/aperturedb/sqlc/models.go +++ b/aperturedb/sqlc/models.go @@ -5,9 +5,22 @@ package sqlc import ( + "database/sql" "time" ) +type LncSession struct { + ID int32 + PassphraseWords string + PassphraseEntropy []byte + RemoteStaticPubKey []byte + LocalStaticPrivKey []byte + MailboxAddr string + CreatedAt time.Time + Expiry sql.NullTime + DevServer bool +} + type Onion struct { PrivateKey []byte CreatedAt time.Time diff --git a/aperturedb/sqlc/querier.go b/aperturedb/sqlc/querier.go index 50ffea5..b457178 100644 --- a/aperturedb/sqlc/querier.go +++ b/aperturedb/sqlc/querier.go @@ -12,8 +12,12 @@ type Querier interface { DeleteOnionPrivateKey(ctx context.Context) error DeleteSecretByHash(ctx context.Context, hash []byte) (int64, error) GetSecretByHash(ctx context.Context, hash []byte) ([]byte, error) + GetSession(ctx context.Context, passphraseEntropy []byte) (LncSession, error) InsertSecret(ctx context.Context, arg InsertSecretParams) (int32, error) + InsertSession(ctx context.Context, arg InsertSessionParams) error SelectOnionPrivateKey(ctx context.Context) ([]byte, error) + SetExpiry(ctx context.Context, arg SetExpiryParams) error + SetRemotePubKey(ctx context.Context, arg SetRemotePubKeyParams) error UpsertOnion(ctx context.Context, arg UpsertOnionParams) error } diff --git a/aperturedb/sqlc/queries/lnc_sessions.sql b/aperturedb/sqlc/queries/lnc_sessions.sql new file mode 100644 index 0000000..d9be1ec --- /dev/null +++ b/aperturedb/sqlc/queries/lnc_sessions.sql @@ -0,0 +1,22 @@ +-- name: InsertSession :exec +INSERT INTO lnc_sessions ( + passphrase_words, passphrase_entropy, local_static_priv_key, mailbox_addr, + created_at, expiry, dev_server +) VALUES ( + $1, $2, $3, $4, $5, $6, $7 +); + +-- name: GetSession :one +SELECT * +FROM lnc_sessions +WHERE passphrase_entropy = $1; + +-- name: SetRemotePubKey :exec +UPDATE lnc_sessions +SET remote_static_pub_key=$1 +WHERE passphrase_entropy=$2; + +-- name: SetExpiry :exec +UPDATE lnc_sessions +SET expiry=$1 +WHERE passphrase_entropy=$2;