Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Walletlib: Multi Account Auth Repo #753

Merged
merged 2 commits into from
Mar 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To clarify, the parentId of an account will always be the id of the "authRecord" that the account is associated with, correct?

Curios, why its called parentId instead of authRecordId? Is there another possible value it can be?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

to align with the schema. Parent id is used to indicate that this is a subtable


@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);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

intentionally commented out?


@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
Loading