cic-custodial/internal/keystore/postgres.go
Mohammed Sohail 4d13a14dc2
refactor: breaking API changes
Squashed commit of the following:

commit 05e1396121
Author: Mohammed Sohail <sohailsameja@gmail.com>
Date:   Wed Feb 15 10:03:44 2023 +0300

    feat: add status types to dispatcher

commit 397cd78ca9
Author: Mohammed Sohail <sohailsameja@gmail.com>
Date:   Wed Feb 15 09:39:31 2023 +0300

    deps: bump -> cic-celo-sdk

commit f2ba079232
Author: Mohammed Sohail <sohailsameja@gmail.com>
Date:   Sun Feb 12 16:53:53 2023 +0300

    snapshot: 12-ebening

commit 4f7909e4ee
Author: Mohammed Sohail <sohailsameja@gmail.com>
Date:   Sun Feb 12 12:50:43 2023 +0300

    xnapshot: 12-02

commit 773474cad9
Author: Mohammed Sohail <sohailsameja@gmail.com>
Date:   Thu Feb 9 14:23:37 2023 +0300

    update: deps initializers

commit 8a0880fcfc
Author: Mohammed Sohail <sohailsameja@gmail.com>
Date:   Thu Feb 9 10:42:15 2023 +0300

    wip: refactor taskers

commit 8676450122
Author: Mohammed Sohail <sohailsameja@gmail.com>
Date:   Fri Feb 3 12:29:27 2023 +0300

    refactor: decouple sql queries, remove transfer

    * add inline docs
    * removed transfer taks in prep for re-write

commit b4c09cd11a
Author: Mohammed Sohail <sohailsameja@gmail.com>
Date:   Thu Feb 2 12:29:43 2023 +0000

    refactor: cmd/service/* and api
2023-02-15 10:05:43 +03:00

62 lines
1.4 KiB
Go

package keystore
import (
"context"
"crypto/ecdsa"
eth_crypto "github.com/celo-org/celo-blockchain/crypto"
"github.com/grassrootseconomics/cic-custodial/internal/queries"
"github.com/grassrootseconomics/cic-custodial/pkg/keypair"
"github.com/jackc/pgx/v5/pgxpool"
)
type (
Opts struct {
PostgresPool *pgxpool.Pool
Queries *queries.Queries
}
PostgresKeystore struct {
db *pgxpool.Pool
queries *queries.Queries
}
)
func NewPostgresKeytore(o Opts) Keystore {
return &PostgresKeystore{
db: o.PostgresPool,
queries: o.Queries,
}
}
// WriteKeyPair inserts a keypair into the db and returns the linked id.
func (ks *PostgresKeystore) WriteKeyPair(ctx context.Context, keypair keypair.Key) (uint, error) {
var (
id uint
)
if err := ks.db.QueryRow(ctx, ks.queries.WriteKeyPair, keypair.Public, keypair.Private).Scan(&id); err != nil {
return id, err
}
return id, nil
}
// LoadPrivateKey loads a private key as a crypto primitive for direct use. An id is used to search for the private key.
func (ks *PostgresKeystore) LoadPrivateKey(ctx context.Context, publicKey string) (*ecdsa.PrivateKey, error) {
var (
privateKeyString string
)
if err := ks.db.QueryRow(ctx, ks.queries.LoadKeyPair, publicKey).Scan(&privateKeyString); err != nil {
return nil, err
}
privateKey, err := eth_crypto.HexToECDSA(privateKeyString)
if err != nil {
return nil, err
}
return privateKey, nil
}