-
Notifications
You must be signed in to change notification settings - Fork 110
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
05920f3
commit 6ffd522
Showing
15 changed files
with
376 additions
and
61 deletions.
There are no files selected for viewing
41 changes: 41 additions & 0 deletions
41
...b/src/main/java/com/solana/mobilewalletadapter/walletlib/authorization/AccountRecord.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package com.solana.mobilewalletadapter.walletlib.authorization; | ||
|
||
import android.net.Uri; | ||
|
||
import androidx.annotation.IntRange; | ||
import androidx.annotation.NonNull; | ||
import androidx.annotation.Nullable; | ||
|
||
/* package */ class AccountRecord { | ||
@IntRange(from = 1) | ||
final int id; | ||
|
||
@NonNull | ||
final byte[] publicKeyRaw; | ||
|
||
@Nullable | ||
final String accountLabel; | ||
|
||
@Nullable | ||
final Uri icon; | ||
|
||
@Nullable | ||
final String[] chains; | ||
|
||
@Nullable | ||
final String[] features; | ||
|
||
AccountRecord(@IntRange(from = 1) int id, | ||
@NonNull byte[] publicKeyRaw, | ||
@Nullable String accountLabel, | ||
@Nullable Uri icon, | ||
@Nullable String[] chains, | ||
@Nullable String[] features) { | ||
this.id = id; | ||
this.publicKeyRaw = publicKeyRaw; | ||
this.accountLabel = accountLabel; | ||
this.icon = icon; | ||
this.chains = chains; | ||
this.features = features; | ||
} | ||
} |
99 changes: 99 additions & 0 deletions
99
...c/main/java/com/solana/mobilewalletadapter/walletlib/authorization/AccountRecordsDao.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
package com.solana.mobilewalletadapter.walletlib.authorization; | ||
|
||
import android.content.ContentValues; | ||
import android.database.Cursor; | ||
import android.database.sqlite.SQLiteCursor; | ||
import android.database.sqlite.SQLiteDatabase; | ||
import android.database.sqlite.SQLiteStatement; | ||
import android.net.Uri; | ||
import android.text.TextUtils; | ||
|
||
import androidx.annotation.IntRange; | ||
import androidx.annotation.NonNull; | ||
import androidx.annotation.Nullable; | ||
|
||
public class AccountRecordsDao extends DbContentProvider<AccountRecord> | ||
implements AccountRecordsDaoInterface, AccountRecordsSchema { | ||
|
||
public AccountRecordsDao(SQLiteDatabase db) { super(db); } | ||
|
||
@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 = Uri.parse(accountIconStr); | ||
final String[] chains = deserialize(chainsString); | ||
final String[] features = deserialize(featuresString); | ||
return new AccountRecord(publicKeyId, publicKey, accountLabel, accountIcon, chains, features); | ||
} | ||
|
||
@Override | ||
public long insert(@NonNull byte[] publicKey, | ||
@Nullable String accountLabel, | ||
@Nullable Uri accountIcon, | ||
@Nullable String[] chains, | ||
@Nullable String[] features) { | ||
final ContentValues accountContentValues = new ContentValues(4); | ||
accountContentValues.put(COLUMN_ACCOUNTS_PUBLIC_KEY_RAW, publicKey); | ||
accountContentValues.put(COLUMN_ACCOUNTS_LABEL, accountLabel); | ||
accountContentValues.put(COLUMN_ACCOUNTS_ICON, accountIcon != null ? accountIcon.toString() : null); | ||
accountContentValues.put(COLUMN_ACCOUNTS_CHAINS, chains != null ? serialize(chains) : null); | ||
accountContentValues.put(COLUMN_ACCOUNTS_FEATURES, features != null ? serialize(features) : null); | ||
return super.insert(TABLE_ACCOUNTS, accountContentValues); | ||
} | ||
|
||
@Nullable | ||
@Override | ||
public AccountRecord query(@NonNull byte[] publicKey) { | ||
final SQLiteDatabase.CursorFactory accountCursorFactory = (db1, masterQuery, editTable, query) -> { | ||
query.bindBlob(1, publicKey); | ||
return new SQLiteCursor(masterQuery, editTable, query); | ||
}; | ||
try (final Cursor cursor = super.queryWithFactory(accountCursorFactory, | ||
TABLE_ACCOUNTS, | ||
ACCOUNTS_COLUMNS, | ||
COLUMN_ACCOUNTS_PUBLIC_KEY_RAW + "=?", | ||
null)) { | ||
if (!cursor.moveToNext()) { | ||
return null; | ||
} | ||
return cursorToEntity(cursor); | ||
} | ||
} | ||
|
||
@Override | ||
public void deleteUnreferencedAccounts() { | ||
final SQLiteStatement deleteUnreferencedPublicKeys = super.compileStatement( | ||
"DELETE FROM " + TABLE_ACCOUNTS + | ||
" WHERE " + COLUMN_ACCOUNTS_ID + " NOT IN " + | ||
"(SELECT DISTINCT " + AuthorizationsSchema.COLUMN_AUTHORIZATIONS_ACCOUNT_ID + | ||
" FROM " + AuthorizationsSchema.TABLE_AUTHORIZATIONS + ')'); | ||
deleteUnreferencedPublicKeys.executeUpdateDelete(); | ||
} | ||
|
||
// using a long alphanumeric divider reduces the chance of an array element matching the divider | ||
private static final String ARRAY_DIVIDER = "#a1r2ra5yd2iv1i9der"; | ||
|
||
private String serialize(String[] content){ return TextUtils.join(ARRAY_DIVIDER, content); } | ||
|
||
private static String[] deserialize(String content){ | ||
return content.split(ARRAY_DIVIDER); | ||
} | ||
|
||
/*package*/ static AccountRecord buildAccountRecordFromRaw(@IntRange(from = 1) int id, | ||
@NonNull byte[] publicKeyRaw, | ||
@Nullable String accountLabel, | ||
@Nullable String iconStr, | ||
@Nullable String chainsStr, | ||
@Nullable String featuresStr) { | ||
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); | ||
} | ||
} |
19 changes: 19 additions & 0 deletions
19
...va/com/solana/mobilewalletadapter/walletlib/authorization/AccountRecordsDaoInterface.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package com.solana.mobilewalletadapter.walletlib.authorization; | ||
|
||
import android.net.Uri; | ||
|
||
import androidx.annotation.IntRange; | ||
import androidx.annotation.NonNull; | ||
import androidx.annotation.Nullable; | ||
|
||
/*package*/ interface AccountRecordsDaoInterface { | ||
|
||
@IntRange(from = -1) | ||
long insert(@NonNull byte[] publicKey, @Nullable String accountLabel, @Nullable Uri accountIcon, | ||
@Nullable String[] chains, @Nullable String[] features); | ||
|
||
@Nullable | ||
AccountRecord query(@NonNull byte[] publicKey); | ||
|
||
void deleteUnreferencedAccounts(); | ||
} |
29 changes: 29 additions & 0 deletions
29
...ain/java/com/solana/mobilewalletadapter/walletlib/authorization/AccountRecordsSchema.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package com.solana.mobilewalletadapter.walletlib.authorization; | ||
|
||
/*package*/ interface AccountRecordsSchema { | ||
String TABLE_ACCOUNTS = "accounts"; | ||
String COLUMN_ACCOUNTS_ID = "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 | ||
String COLUMN_ACCOUNTS_CHAINS = "chains"; // type: String | ||
String COLUMN_ACCOUNTS_FEATURES = "features"; // type: String | ||
|
||
String CREATE_TABLE_ACCOUNTS = | ||
"CREATE TABLE " + TABLE_ACCOUNTS + " (" + | ||
COLUMN_ACCOUNTS_ID + " INTEGER NOT NULL PRIMARY KEY," + | ||
COLUMN_ACCOUNTS_PUBLIC_KEY_RAW + " BLOB NOT NULL," + | ||
COLUMN_ACCOUNTS_LABEL + " TEXT," + | ||
COLUMN_ACCOUNTS_ICON + " TEXT," + | ||
COLUMN_ACCOUNTS_CHAINS + " TEXT," + | ||
COLUMN_ACCOUNTS_FEATURES + " TEXT)"; | ||
|
||
String[] ACCOUNTS_COLUMNS = new String[]{ | ||
COLUMN_ACCOUNTS_ID, | ||
COLUMN_ACCOUNTS_PUBLIC_KEY_RAW, | ||
COLUMN_ACCOUNTS_LABEL, | ||
COLUMN_ACCOUNTS_ICON, | ||
COLUMN_ACCOUNTS_CHAINS, | ||
COLUMN_ACCOUNTS_FEATURES | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.