Skip to content

Commit

Permalink
save drivekey in memory and don't break upload if fails to load the a…
Browse files Browse the repository at this point in the history
…rns names
  • Loading branch information
thiagocarvalhodev committed Nov 18, 2024
1 parent b981fe3 commit b43a78a
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 14 deletions.
17 changes: 13 additions & 4 deletions lib/blocs/upload/upload_cubit.dart
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ class UploadCubit extends Cubit<UploadState> {
fileId: manifestModels[i].existingManifestFileId,
)
.getSingle();

await _createManifestCubit.prepareManifestTx(
manifestName: manifestFileEntry.name,
folderId: manifestFileEntry.parentFolderId,
Expand Down Expand Up @@ -886,6 +886,10 @@ class UploadCubit extends Cubit<UploadState> {
if (state is UploadReady) {
emit((state as UploadReady).copyWith(arnsRecords: value));
}
}).catchError((e) {
logger.e(
'Error getting ant records for wallet. Proceeding with the upload...',
e);
});

_files
Expand Down Expand Up @@ -1032,9 +1036,14 @@ class UploadCubit extends Cubit<UploadState> {
}

if (manifestFileEntries.isNotEmpty) {
// load arns names
await _arnsRepository
.getAntRecordsForWallet(_auth.currentUser.walletAddress);
try {
await _arnsRepository
.getAntRecordsForWallet(_auth.currentUser.walletAddress);
} catch (e) {
logger.e(
'Error getting ant records for wallet. Proceeding with the upload...',
e);
}
}

emit(
Expand Down
6 changes: 5 additions & 1 deletion lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ late ConfigService configService;
late ArweaveService arweave;
late TurboUploadService _turboUpload;
late PaymentService _turboPayment;
late Database db;

void main() async {
await runZonedGuarded(() async {
Expand Down Expand Up @@ -129,11 +130,14 @@ Future<void> _initializeServices() async {

final config = configService.config;

db = Database();

arweave = ArweaveService(
Arweave(
gatewayUrl: Uri.parse(config.defaultArweaveGatewayForDataRequest.url),
),
ArDriveCrypto(),
db.driveDao,
configService,
);
_turboUpload = config.useTurboUpload
Expand Down Expand Up @@ -395,7 +399,7 @@ class AppState extends State<App> {
),
),
),
RepositoryProvider<Database>(create: (_) => Database()),
RepositoryProvider<Database>(create: (_) => db),
RepositoryProvider<ProfileDao>(
create: (context) => context.read<Database>().profileDao),
RepositoryProvider<DriveDao>(
Expand Down
29 changes: 20 additions & 9 deletions lib/services/arweave/arweave_service.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import 'dart:convert';

import 'package:ardrive/core/crypto/crypto.dart';
import 'package:ardrive/entities/entities.dart';
import 'package:ardrive/models/daos/drive_dao/drive_dao.dart';
import 'package:ardrive/services/arweave/arweave_service_exception.dart';
import 'package:ardrive/services/arweave/error/gateway_error.dart';
import 'package:ardrive/services/arweave/get_segmented_transaction_from_drive_strategy.dart';
Expand Down Expand Up @@ -42,12 +43,13 @@ const kMaxNumberOfTransactionsPerPage = 100;
class ArweaveService {
Arweave client;
final ArDriveCrypto _crypto;

final DriveDao _driveDao;
final ArtemisClient _gql;

ArweaveService(
this.client,
this._crypto,
this._driveDao,
ConfigService configService, {
ArtemisClient? artemisClient,
}) : _gql = artemisClient ??
Expand Down Expand Up @@ -588,15 +590,24 @@ class ArweaveService {
continue;
}

final driveKey =
driveTx.getTag(EntityTag.drivePrivacy) == DrivePrivacyTag.private
? await _crypto.deriveDriveKey(
wallet,
driveTx.getTag(EntityTag.driveId)!,
password,
)
: null;
SecretKey? driveKey;

if (driveTx.getTag(EntityTag.drivePrivacy) == DrivePrivacyTag.private) {
driveKey = await _driveDao.getDriveKeyFromMemory(
driveTx.getTag(EntityTag.driveId)!,
);

driveKey ??= await _crypto.deriveDriveKey(
wallet,
driveTx.getTag(EntityTag.driveId)!,
password,
);

_driveDao.putDriveKeyInMemory(
driveID: driveTx.getTag(EntityTag.driveId)!,
driveKey: driveKey,
);
}
try {
final drive = await DriveEntity.fromTransaction(
driveTx,
Expand Down

0 comments on commit b43a78a

Please sign in to comment.