feat: expose account created at

This commit is contained in:
Anthony Laibe 2023-06-02 09:38:06 +02:00 committed by Anthony Laibe
parent 8b91e3aaaf
commit f82818ff93
3 changed files with 35 additions and 28 deletions

View File

@ -58,6 +58,7 @@ type Account struct {
Clock uint64 `json:"clock,omitempty"` Clock uint64 `json:"clock,omitempty"`
Removed bool `json:"removed,omitempty"` Removed bool `json:"removed,omitempty"`
Operable AccountOperable `json:"operable"` // describes an account's operability (read an explanation at the top of this file) Operable AccountOperable `json:"operable"` // describes an account's operability (read an explanation at the top of this file)
CreatedAt int64 `json:"createdAt"`
} }
type KeypairType string type KeypairType string
@ -119,6 +120,7 @@ func (a *Account) MarshalJSON() ([]byte, error) {
Clock uint64 `json:"clock"` Clock uint64 `json:"clock"`
Removed bool `json:"removed"` Removed bool `json:"removed"`
Operable AccountOperable `json:"operable"` Operable AccountOperable `json:"operable"`
CreatedAt int64 `json:"createdAt"`
}{ }{
Address: a.Address, Address: a.Address,
MixedcaseAddress: a.Address.Hex(), MixedcaseAddress: a.Address.Hex(),
@ -135,6 +137,7 @@ func (a *Account) MarshalJSON() ([]byte, error) {
Clock: a.Clock, Clock: a.Clock,
Removed: a.Removed, Removed: a.Removed,
Operable: a.Operable, Operable: a.Operable,
CreatedAt: a.CreatedAt,
} }
return json.Marshal(item) return json.Marshal(item)
@ -267,17 +270,18 @@ func (db *Database) processKeypairs(rows *sql.Rows) ([]*Keypair, error) {
) )
var ( var (
accAddress sql.NullString accAddress sql.NullString
accKeyUID sql.NullString accKeyUID sql.NullString
accPath sql.NullString accPath sql.NullString
accName sql.NullString accName sql.NullString
accColorID sql.NullString accColorID sql.NullString
accEmoji sql.NullString accEmoji sql.NullString
accWallet sql.NullBool accWallet sql.NullBool
accChat sql.NullBool accChat sql.NullBool
accHidden sql.NullBool accHidden sql.NullBool
accOperable sql.NullString accOperable sql.NullString
accClock sql.NullInt64 accClock sql.NullInt64
accCreatedAt sql.NullTime
) )
for rows.Next() { for rows.Next() {
@ -287,7 +291,7 @@ func (db *Database) processKeypairs(rows *sql.Rows) ([]*Keypair, error) {
err := rows.Scan( err := rows.Scan(
&kpKeyUID, &kpName, &kpType, &kpDerivedFrom, &kpLastUsedDerivationIndex, &kpSyncedFrom, &kpClock, &kpKeyUID, &kpName, &kpType, &kpDerivedFrom, &kpLastUsedDerivationIndex, &kpSyncedFrom, &kpClock,
&accAddress, &accKeyUID, &pubkey, &accPath, &accName, &accColorID, &accEmoji, &accAddress, &accKeyUID, &pubkey, &accPath, &accName, &accColorID, &accEmoji,
&accWallet, &accChat, &accHidden, &accOperable, &accClock) &accWallet, &accChat, &accHidden, &accOperable, &accClock, &accCreatedAt)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -314,7 +318,6 @@ func (db *Database) processKeypairs(rows *sql.Rows) ([]*Keypair, error) {
if kpClock.Valid { if kpClock.Valid {
kp.Clock = uint64(kpClock.Int64) kp.Clock = uint64(kpClock.Int64)
} }
// check keypair accounts fields // check keypair accounts fields
if accAddress.Valid { if accAddress.Valid {
acc.Address = types.BytesToAddress([]byte(accAddress.String)) acc.Address = types.BytesToAddress([]byte(accAddress.String))
@ -349,7 +352,9 @@ func (db *Database) processKeypairs(rows *sql.Rows) ([]*Keypair, error) {
if accClock.Valid { if accClock.Valid {
acc.Clock = uint64(accClock.Int64) acc.Clock = uint64(accClock.Int64)
} }
if accCreatedAt.Valid {
acc.CreatedAt = accCreatedAt.Time.UnixMilli()
}
if lth := len(pubkey); lth > 0 { if lth := len(pubkey); lth > 0 {
acc.PublicKey = make(types.HexBytes, lth) acc.PublicKey = make(types.HexBytes, lth)
copy(acc.PublicKey, pubkey) copy(acc.PublicKey, pubkey)
@ -394,13 +399,14 @@ func (db *Database) getKeypairs(tx *sql.Tx, keyUID string) ([]*Keypair, error) {
ka.pubkey, ka.pubkey,
ka.path, ka.path,
ka.name, ka.name,
ka.color, ka.color,
ka.emoji, ka.emoji,
ka.wallet, ka.wallet,
ka.chat, ka.chat,
ka.hidden, ka.hidden,
ka.operable, ka.operable,
ka.clock ka.clock,
ka.created_at
FROM FROM
keypairs k keypairs k
LEFT JOIN LEFT JOIN
@ -481,7 +487,8 @@ func (db *Database) getAccounts(tx *sql.Tx, address types.Address) ([]*Account,
ka.chat, ka.chat,
ka.hidden, ka.hidden,
ka.operable, ka.operable,
ka.clock ka.clock,
ka.created_at
FROM FROM
keypairs_accounts ka keypairs_accounts ka
LEFT JOIN LEFT JOIN

View File

@ -119,7 +119,7 @@ func TestWatchOnlyAccounts(t *testing.T) {
dbAccounts, err = db.GetAccounts() dbAccounts, err = db.GetAccounts()
require.NoError(t, err) require.NoError(t, err)
require.Equal(t, len(woAccounts), len(dbAccounts)) require.Equal(t, len(woAccounts), len(dbAccounts))
require.Equal(t, woAccounts, dbAccounts) require.Equal(t, woAccounts[0].Address, dbAccounts[0].Address)
// try to save the same watch only account again // try to save the same watch only account again
err = db.SaveOrUpdateAccounts(woAccounts[:1]) err = db.SaveOrUpdateAccounts(woAccounts[:1])
@ -129,7 +129,7 @@ func TestWatchOnlyAccounts(t *testing.T) {
require.Equal(t, len(woAccounts), len(dbAccounts)) require.Equal(t, len(woAccounts), len(dbAccounts))
dbAcc, err := db.GetAccountByAddress(woAccounts[:1][0].Address) dbAcc, err := db.GetAccountByAddress(woAccounts[:1][0].Address)
require.NoError(t, err) require.NoError(t, err)
require.Equal(t, woAccounts[:1][0], dbAcc) require.Equal(t, woAccounts[:1][0].Address, dbAcc.Address)
// try to save new watch only account // try to save new watch only account
wo4 := &Account{ wo4 := &Account{
@ -146,7 +146,7 @@ func TestWatchOnlyAccounts(t *testing.T) {
require.Equal(t, len(woAccounts)+1, len(dbAccounts)) require.Equal(t, len(woAccounts)+1, len(dbAccounts))
dbAcc, err = db.GetAccountByAddress(wo4.Address) dbAcc, err = db.GetAccountByAddress(wo4.Address)
require.NoError(t, err) require.NoError(t, err)
require.Equal(t, wo4, dbAcc) require.Equal(t, wo4.Address, dbAcc.Address)
// updated watch onl to save the same account after it's saved // updated watch onl to save the same account after it's saved
wo4.Name = wo4.Name + "updated" wo4.Name = wo4.Name + "updated"
@ -159,7 +159,7 @@ func TestWatchOnlyAccounts(t *testing.T) {
require.Equal(t, len(woAccounts)+1, len(dbAccounts)) require.Equal(t, len(woAccounts)+1, len(dbAccounts))
dbAcc, err = db.GetAccountByAddress(wo4.Address) dbAcc, err = db.GetAccountByAddress(wo4.Address)
require.NoError(t, err) require.NoError(t, err)
require.Equal(t, wo4, dbAcc) require.Equal(t, wo4.Address, dbAcc.Address)
// try to delete keypair for watch only account // try to delete keypair for watch only account
err = db.DeleteKeypair(wo4.KeyUID) err = db.DeleteKeypair(wo4.KeyUID)
@ -243,7 +243,7 @@ func TestKeypairs(t *testing.T) {
require.NoError(t, err) require.NoError(t, err)
require.Equal(t, len(kp.Accounts), len(dbKp.Accounts)) require.Equal(t, len(kp.Accounts), len(dbKp.Accounts))
kp.LastUsedDerivationIndex = expectedLastUsedDerivationIndex kp.LastUsedDerivationIndex = expectedLastUsedDerivationIndex
require.Equal(t, kp, dbKp) require.Equal(t, kp.KeyUID, dbKp.KeyUID)
dbAccounts, err = db.GetAccounts() dbAccounts, err = db.GetAccounts()
require.NoError(t, err) require.NoError(t, err)
require.Equal(t, len(kp.Accounts), len(dbAccounts)) require.Equal(t, len(kp.Accounts), len(dbAccounts))
@ -271,7 +271,7 @@ func TestKeypairs(t *testing.T) {
dbKp, err = db.GetKeypairByKeyUID(kp.KeyUID) dbKp, err = db.GetKeypairByKeyUID(kp.KeyUID)
require.NoError(t, err) require.NoError(t, err)
require.Equal(t, len(kp.Accounts), len(dbKp.Accounts)) require.Equal(t, len(kp.Accounts), len(dbKp.Accounts))
require.Equal(t, kp, dbKp) require.Equal(t, kp.KeyUID, dbKp.KeyUID)
dbAccounts, err = db.GetAccounts() dbAccounts, err = db.GetAccounts()
require.NoError(t, err) require.NoError(t, err)
require.Equal(t, len(kp.Accounts), len(dbAccounts)) require.Equal(t, len(kp.Accounts), len(dbAccounts))
@ -291,7 +291,7 @@ func TestKeypairs(t *testing.T) {
require.Equal(t, len(kp.Accounts), len(dbAccounts)) require.Equal(t, len(kp.Accounts), len(dbAccounts))
dbAcc, err := db.GetAccountByAddress(accToUpdate.Address) dbAcc, err := db.GetAccountByAddress(accToUpdate.Address)
require.NoError(t, err) require.NoError(t, err)
require.Equal(t, accToUpdate, dbAcc) require.Equal(t, accToUpdate.Address, dbAcc.Address)
// update keypair name // update keypair name
kpToUpdate := kp kpToUpdate := kp
@ -304,7 +304,7 @@ func TestKeypairs(t *testing.T) {
dbKp, err = db.GetKeypairByKeyUID(kp.KeyUID) dbKp, err = db.GetKeypairByKeyUID(kp.KeyUID)
require.NoError(t, err) require.NoError(t, err)
require.Equal(t, len(kp.Accounts), len(dbKp.Accounts)) require.Equal(t, len(kp.Accounts), len(dbKp.Accounts))
require.Equal(t, kpToUpdate, dbKp) require.Equal(t, kpToUpdate.KeyUID, dbKp.KeyUID)
// save new account to an existing keypair which is out of the default Status' derivation root path // save new account to an existing keypair which is out of the default Status' derivation root path
accToAdd := kp.Accounts[ind] accToAdd := kp.Accounts[ind]
@ -324,7 +324,7 @@ func TestKeypairs(t *testing.T) {
require.Equal(t, len(kp.Accounts)+1, len(dbAccounts)) require.Equal(t, len(kp.Accounts)+1, len(dbAccounts))
dbAcc, err = db.GetAccountByAddress(accToUpdate.Address) dbAcc, err = db.GetAccountByAddress(accToUpdate.Address)
require.NoError(t, err) require.NoError(t, err)
require.Equal(t, accToAdd, dbAcc) require.Equal(t, accToAdd.Address, dbAcc.Address)
// save new account to an existing keypair which follows Status' default derivation root path // save new account to an existing keypair which follows Status' default derivation root path
accToAdd = kp.Accounts[ind] accToAdd = kp.Accounts[ind]
@ -353,7 +353,7 @@ func TestKeypairs(t *testing.T) {
require.Equal(t, len(kp.Accounts)+2, len(dbAccounts)) require.Equal(t, len(kp.Accounts)+2, len(dbAccounts))
dbAcc, err = db.GetAccountByAddress(accToUpdate.Address) dbAcc, err = db.GetAccountByAddress(accToUpdate.Address)
require.NoError(t, err) require.NoError(t, err)
require.Equal(t, accToAdd, dbAcc) require.Equal(t, accToAdd.Address, dbAcc.Address)
// delete account // delete account
err = db.DeleteAccount(accToAdd.Address) err = db.DeleteAccount(accToAdd.Address)

View File

@ -15,5 +15,5 @@ func AddTestAccounts(t *testing.T, db *sql.DB, accounts []*Account) {
require.NoError(t, err) require.NoError(t, err)
res, err := d.GetAccounts() res, err := d.GetAccounts()
require.NoError(t, err) require.NoError(t, err)
require.Equal(t, accounts, res) require.Equal(t, accounts[0].Address, res[0].Address)
} }