Skip to content

Commit

Permalink
multi account auth repo working (#753)
Browse files Browse the repository at this point in the history
  • Loading branch information
Funkatronics authored Mar 27, 2024
1 parent be1ddb3 commit 31055cc
Show file tree
Hide file tree
Showing 15 changed files with 335 additions and 138 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,15 @@
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import java.util.Arrays;

/* package */ class AccountRecord {
@IntRange(from = 1)
final int id;

@IntRange(from = 1)
final int parentId;

@NonNull
final byte[] publicKeyRaw;

Expand All @@ -26,16 +31,27 @@
final String[] features;

AccountRecord(@IntRange(from = 1) int id,
@IntRange(from = 1) int parentId,
@NonNull byte[] publicKeyRaw,
@Nullable String accountLabel,
@Nullable Uri icon,
@Nullable String[] chains,
@Nullable String[] features) {
this.id = id;
this.parentId = parentId;
this.publicKeyRaw = publicKeyRaw;
this.accountLabel = accountLabel;
this.icon = icon;
this.chains = chains;
this.features = features;
}

@NonNull
@Override
public String toString() {
return "AccountRecord{" +
"label=" + accountLabel +
", publicKey=" + Arrays.toString(publicKeyRaw) +
'}';
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import java.util.ArrayList;
import java.util.List;

public class AccountRecordsDao extends DbContentProvider<AccountRecord>
implements AccountRecordsDaoInterface, AccountRecordsSchema {

Expand All @@ -20,25 +23,18 @@ public class AccountRecordsDao extends DbContentProvider<AccountRecord>
@NonNull
@Override
protected AccountRecord cursorToEntity(@NonNull Cursor cursor) {
final int publicKeyId = cursor.getInt(cursor.getColumnIndexOrThrow(COLUMN_ACCOUNTS_ID));
final byte[] publicKey = cursor.getBlob(cursor.getColumnIndexOrThrow(COLUMN_ACCOUNTS_PUBLIC_KEY_RAW));
final String accountLabel = cursor.getString(cursor.getColumnIndexOrThrow(COLUMN_ACCOUNTS_LABEL));
final String accountIconStr = cursor.getString(cursor.getColumnIndexOrThrow(COLUMN_ACCOUNTS_ICON));
final String chainsString = cursor.getString(cursor.getColumnIndexOrThrow(COLUMN_ACCOUNTS_CHAINS));
final String featuresString = cursor.getString(cursor.getColumnIndexOrThrow(COLUMN_ACCOUNTS_FEATURES));
final Uri accountIcon = accountIconStr != null ? Uri.parse(accountIconStr) : null;
final String[] chains = chainsString != null ? deserialize(chainsString) : null;
final String[] features = featuresString != null ? deserialize(featuresString) : null;
return new AccountRecord(publicKeyId, publicKey, accountLabel, accountIcon, chains, features);
return buildAccountRecordFromCursor(cursor);
}

@Override
public long insert(@NonNull byte[] publicKey,
public long insert(@NonNull long parentId,
@NonNull byte[] publicKey,
@Nullable String accountLabel,
@Nullable Uri accountIcon,
@Nullable String[] chains,
@Nullable String[] features) {
final ContentValues accountContentValues = new ContentValues(4);
final ContentValues accountContentValues = new ContentValues(6);
accountContentValues.put(COLUMN_ACCOUNTS_PARENT_ID, parentId);
accountContentValues.put(COLUMN_ACCOUNTS_PUBLIC_KEY_RAW, publicKey);
accountContentValues.put(COLUMN_ACCOUNTS_LABEL, accountLabel);
accountContentValues.put(COLUMN_ACCOUNTS_ICON, accountIcon != null ? accountIcon.toString() : null);
Expand Down Expand Up @@ -70,8 +66,8 @@ public AccountRecord query(@NonNull byte[] publicKey) {
public void deleteUnreferencedAccounts() {
final SQLiteStatement deleteUnreferencedAccounts = super.compileStatement(
"DELETE FROM " + TABLE_ACCOUNTS +
" WHERE " + COLUMN_ACCOUNTS_ID + " NOT IN " +
"(SELECT DISTINCT " + AuthorizationsSchema.COLUMN_AUTHORIZATIONS_ACCOUNT_ID +
" WHERE " + COLUMN_ACCOUNTS_PARENT_ID + " NOT IN " +
"(SELECT DISTINCT " + AuthorizationsSchema.COLUMN_AUTHORIZATIONS_ID +
" FROM " + AuthorizationsSchema.TABLE_AUTHORIZATIONS + ')');
deleteUnreferencedAccounts.executeUpdateDelete();
}
Expand All @@ -86,6 +82,7 @@ private static String[] deserialize(String content){
}

/*package*/ static AccountRecord buildAccountRecordFromRaw(@IntRange(from = 1) int id,
@IntRange(from = 1) int parentId,
@NonNull byte[] publicKeyRaw,
@Nullable String accountLabel,
@Nullable String iconStr,
Expand All @@ -94,6 +91,20 @@ private static String[] deserialize(String content){
final Uri icon = iconStr != null ? Uri.parse(iconStr) : null;
final String[] chains = chainsStr != null ? deserialize(chainsStr) : null;
final String[] features = featuresStr != null ? deserialize(featuresStr) : null;
return new AccountRecord(id, publicKeyRaw, accountLabel, icon, chains, features);
return new AccountRecord(id, parentId, publicKeyRaw, accountLabel, icon, chains, features);
}

/*package*/ static AccountRecord buildAccountRecordFromCursor(@NonNull Cursor cursor) {
final int id = cursor.getInt(cursor.getColumnIndexOrThrow(COLUMN_ACCOUNTS_ID));
final int parentId = cursor.getInt(cursor.getColumnIndexOrThrow(COLUMN_ACCOUNTS_PARENT_ID));
final byte[] publicKey = cursor.getBlob(cursor.getColumnIndexOrThrow(COLUMN_ACCOUNTS_PUBLIC_KEY_RAW));
final String accountLabel = cursor.getString(cursor.getColumnIndexOrThrow(COLUMN_ACCOUNTS_LABEL));
final String accountIconStr = cursor.getString(cursor.getColumnIndexOrThrow(COLUMN_ACCOUNTS_ICON));
final String chainsString = cursor.getString(cursor.getColumnIndexOrThrow(COLUMN_ACCOUNTS_CHAINS));
final String featuresString = cursor.getString(cursor.getColumnIndexOrThrow(COLUMN_ACCOUNTS_FEATURES));
final Uri accountIcon = accountIconStr != null ? Uri.parse(accountIconStr) : null;
final String[] chains = chainsString != null ? deserialize(chainsString) : null;
final String[] features = featuresString != null ? deserialize(featuresString) : null;
return new AccountRecord(id, parentId, publicKey, accountLabel, accountIcon, chains, features);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,18 @@
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import java.util.List;

/*package*/ interface AccountRecordsDaoInterface {

@IntRange(from = -1)
long insert(@NonNull byte[] publicKey, @Nullable String accountLabel, @Nullable Uri accountIcon,
long insert(@NonNull long parentId, @NonNull byte[] publicKey,
@Nullable String accountLabel, @Nullable Uri accountIcon,
@Nullable String[] chains, @Nullable String[] features);

// @Nullable
// List<AccountRecord> query(int parentId);

@Nullable
AccountRecord query(@NonNull byte[] publicKey);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
/*package*/ interface AccountRecordsSchema {
String TABLE_ACCOUNTS = "accounts";
String COLUMN_ACCOUNTS_ID = "id"; // type: long
String COLUMN_ACCOUNTS_PARENT_ID = "parent_id"; // type: long
String COLUMN_ACCOUNTS_PUBLIC_KEY_RAW = "public_key_raw"; // type: byte[]
String COLUMN_ACCOUNTS_LABEL = "label"; // type: String
String COLUMN_ACCOUNTS_ICON = "icon"; // type: String
Expand All @@ -12,6 +13,7 @@
String CREATE_TABLE_ACCOUNTS =
"CREATE TABLE " + TABLE_ACCOUNTS + " (" +
COLUMN_ACCOUNTS_ID + " INTEGER NOT NULL PRIMARY KEY," +
COLUMN_ACCOUNTS_PARENT_ID + " INTEGER NOT NULL," +
COLUMN_ACCOUNTS_PUBLIC_KEY_RAW + " BLOB NOT NULL," +
COLUMN_ACCOUNTS_LABEL + " TEXT," +
COLUMN_ACCOUNTS_ICON + " TEXT," +
Expand All @@ -20,6 +22,7 @@

String[] ACCOUNTS_COLUMNS = new String[]{
COLUMN_ACCOUNTS_ID,
COLUMN_ACCOUNTS_PARENT_ID,
COLUMN_ACCOUNTS_PUBLIC_KEY_RAW,
COLUMN_ACCOUNTS_LABEL,
COLUMN_ACCOUNTS_ICON,
Expand Down
Loading

0 comments on commit 31055cc

Please sign in to comment.