Skip to content

Commit

Permalink
Merge pull request #1720 from TalaoDAO/walletconnectV2
Browse files Browse the repository at this point in the history
Walletconnect v2 implementation (except tezos)
  • Loading branch information
bibash28 authored Jul 5, 2023
2 parents 1b51f8d + 2d81c11 commit e3cb5c1
Show file tree
Hide file tree
Showing 33 changed files with 1,035 additions and 641 deletions.
7 changes: 7 additions & 0 deletions lib/app/shared/constants/parameters.dart
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,11 @@ class Parameters {
static const MUMBAI_ID_STATE_CONTRACT_ADDR =
'0x134B1BE34911E39A8397ec6289782989729807a4';
static const MUMBAI_PUSH_URL = 'https://push-staging.polygonid.com/api/v1';

static const NAMESPACE = 'eip155';
static const PERSONAL_SIGN = 'personal_sign';
static const ETH_SIGN = 'eth_sign';
static const ETH_SIGN_TRANSACTION = 'eth_signTransaction';
static const ETH_SIGN_TYPE_DATA = 'eth_signTypedData';
static const ETH_SEND_TRANSACTION = 'eth_sendTransaction';
}
1 change: 1 addition & 0 deletions lib/app/shared/enum/status/credentials_status.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
enum CredentialsStatus {
init,
idle,
populate,
loading,
insert,
Expand Down
25 changes: 25 additions & 0 deletions lib/app/shared/enum/type/blockchain_type.dart
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,31 @@ extension BlockchainTypeX on BlockchainType {
}
}

String get chain {
String name = '';
switch (this) {
case BlockchainType.tezos:
throw Exception();

case BlockchainType.ethereum:
name = '1';
break;
case BlockchainType.fantom:
name = '250';
break;

case BlockchainType.polygon:
name = '137';
break;

case BlockchainType.binance:
name = '56';
break;
}

return '${Parameters.NAMESPACE}:$name';
}

int get chainId {
switch (this) {
case BlockchainType.tezos:
Expand Down
11 changes: 11 additions & 0 deletions lib/app/shared/helper_functions/helper_functions.dart
Original file line number Diff line number Diff line change
Expand Up @@ -417,3 +417,14 @@ List<String> generateUriList(String url) {

return uriList ?? [];
}

String getUtf8Message(String maybeHex) {
if (maybeHex.startsWith('0x')) {
final List<int> decoded = hex.decode(
maybeHex.substring(2),
);
return utf8.decode(decoded);
}

return maybeHex;
}
2 changes: 2 additions & 0 deletions lib/app/view/app.dart
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ class App extends StatelessWidget {
),
BlocProvider<WalletConnectCubit>(
create: (context) => WalletConnectCubit(
secureStorageProvider: secure_storage.getSecureStorage,
connectedDappRepository:
ConnectedDappRepository(secure_storage.getSecureStorage),
),
Expand Down Expand Up @@ -125,6 +126,7 @@ class App extends StatelessWidget {
homeCubit: context.read<HomeCubit>(),
keyGenerator: KeyGenerator(),
credentialsCubit: context.read<CredentialsCubit>(),
walletConnectCubit: context.read<WalletConnectCubit>(),
),
),
BlocProvider<PolygonIdCubit>(
Expand Down
19 changes: 19 additions & 0 deletions lib/connection_bridge/model/ethereum_sign_message.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
enum WCV2SignType {
message,
personalMessage,
typedMessageV2,
typedMessageV3,
typedMessageV4,
}

class EthereumSignMessage {
const EthereumSignMessage({
required this.data,
required this.address,
required this.type,
});

final String data;
final String address;
final WCV2SignType type;
}
39 changes: 39 additions & 0 deletions lib/connection_bridge/model/ethereum_transaction.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import 'package:json_annotation/json_annotation.dart';

part 'ethereum_transaction.g.dart';

@JsonSerializable(includeIfNull: false)
class EthereumTransaction {
EthereumTransaction({
required this.from,
required this.to,
required this.value,
this.nonce,
this.gasPrice,
this.maxFeePerGas,
this.maxPriorityFeePerGas,
this.gas,
this.gasLimit,
this.data,
});

factory EthereumTransaction.fromJson(Map<String, dynamic> json) =>
_$EthereumTransactionFromJson(json);
final String from;
final String to;
final String value;
final String? nonce;
final String? gasPrice;
final String? maxFeePerGas;
final String? maxPriorityFeePerGas;
final String? gas;
final String? gasLimit;
final String? data;

Map<String, dynamic> toJson() => _$EthereumTransactionToJson(this);

@override
String toString() {
return 'WCEthereumTransaction(from: $from, to: $to, nonce: $nonce, gasPrice: $gasPrice, maxFeePerGas: $maxFeePerGas, maxPriorityFeePerGas: $maxPriorityFeePerGas, gas: $gas, gasLimit: $gasLimit, value: $value, data: $data)';
}
}
2 changes: 2 additions & 0 deletions lib/connection_bridge/model/model.dart
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
export 'ethereum_sign_message.dart';
export 'ethereum_transaction.dart';
export 'saved_dapp_data.dart';
20 changes: 10 additions & 10 deletions lib/connection_bridge/model/saved_dapp_data.dart
Original file line number Diff line number Diff line change
@@ -1,31 +1,31 @@
import 'package:altme/app/app.dart';
import 'package:beacon_flutter/beacon_flutter.dart';
import 'package:equatable/equatable.dart';
import 'package:json_annotation/json_annotation.dart';
import 'package:wallet_connect/wallet_connect.dart';
import 'package:walletconnect_flutter_v2/walletconnect_flutter_v2.dart';

part 'saved_dapp_data.g.dart';

@JsonSerializable()
class SavedDappData extends Equatable {
const SavedDappData({
this.peer,
required this.walletAddress,
required this.blockchainType,
this.wcSessionStore,
this.walletAddress,
this.sessionData,
});

factory SavedDappData.fromJson(Map<String, dynamic> json) =>
_$SavedDappDataFromJson(json);

final P2PPeer? peer;
final String walletAddress;
final BlockchainType blockchainType;
final WCSessionStore? wcSessionStore;
final String? walletAddress;
final SessionData? sessionData;

Map<String, dynamic> toJson() => _$SavedDappDataToJson(this);

@override
List<Object?> get props =>
[peer, walletAddress, blockchainType, wcSessionStore];
List<Object?> get props => [
peer,
walletAddress,
sessionData,
];
}
67 changes: 21 additions & 46 deletions lib/connection_bridge/repository/connected_dapp_repository.dart
Original file line number Diff line number Diff line change
Expand Up @@ -64,23 +64,13 @@ class ConnectedDappRepository {

Future<bool> delete(SavedDappData savedDappData) async {
late String id;
switch (savedDappData.blockchainType) {
case BlockchainType.ethereum:
id = savedDappData.wcSessionStore!.session.topic;
break;
case BlockchainType.tezos:
id = savedDappData.peer!.publicKey;
break;
case BlockchainType.fantom:
id = savedDappData.wcSessionStore!.session.topic;
break;
case BlockchainType.polygon:
id = savedDappData.wcSessionStore!.session.topic;
break;
case BlockchainType.binance:
id = savedDappData.wcSessionStore!.session.topic;
break;

if (savedDappData.walletAddress != null) {
id = savedDappData.peer!.publicKey;
} else {
id = savedDappData.sessionData!.pairingTopic;
}

log.i('deleteing dapp data - ${SecureStorageKeys.savedDaaps}/$id');
await _secureStorageProvider.delete('${SecureStorageKeys.savedDaaps}/$id');
return true;
Expand All @@ -89,41 +79,26 @@ class ConnectedDappRepository {
Future<int> insert(SavedDappData savedDappData) async {
final List<SavedDappData> savedPeerDatas = await findAll();

final SavedDappData? matchedData = savedPeerDatas.firstWhereOrNull(
(SavedDappData savedData) {
switch (savedDappData.blockchainType) {
case BlockchainType.ethereum:
case BlockchainType.fantom:
case BlockchainType.polygon:
case BlockchainType.binance:
return savedData.walletAddress == savedDappData.walletAddress &&
savedData.wcSessionStore!.remotePeerMeta.name ==
savedDappData.wcSessionStore!.remotePeerMeta.name;
case BlockchainType.tezos:
return savedData.walletAddress == savedDappData.walletAddress &&
savedData.peer!.name == savedDappData.peer!.name;
}
},

/// Note: Assumption - name is always unique
);
if (savedDappData.walletAddress != null) {
final SavedDappData? matchedData = savedPeerDatas.firstWhereOrNull(
(SavedDappData savedData) =>
savedData.walletAddress == savedDappData.walletAddress &&
savedData.peer!.name == savedDappData.peer!.name,

if (matchedData != null) {
await delete(matchedData);
/// Note: Assumption - name is always unique
);
if (matchedData != null) {
await delete(matchedData);
}
}

log.i('saving dapp Data');
late String id;
switch (savedDappData.blockchainType) {
case BlockchainType.ethereum:
case BlockchainType.fantom:
case BlockchainType.polygon:
case BlockchainType.binance:
id = savedDappData.wcSessionStore!.session.topic;
break;
case BlockchainType.tezos:
id = savedDappData.peer!.publicKey;
break;

if (savedDappData.walletAddress != null) {
id = savedDappData.peer!.publicKey;
} else {
id = savedDappData.sessionData!.pairingTopic;
}

await _secureStorageProvider.set(
Expand Down
Loading

0 comments on commit e3cb5c1

Please sign in to comment.