Skip to content

Commit

Permalink
fix: Support multiple chain instead of multiple accounts and signing …
Browse files Browse the repository at this point in the history
…issue fix
  • Loading branch information
bibash28 committed Jul 14, 2023
1 parent c3c2696 commit e30d42d
Show file tree
Hide file tree
Showing 4 changed files with 111 additions and 64 deletions.
12 changes: 12 additions & 0 deletions lib/app/shared/constants/parameters.dart
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,16 @@ class Parameters {
static const ETH_SIGN_TYPE_DATA = 'eth_signTypedData';
static const ETH_SEND_TRANSACTION = 'eth_sendTransaction';
static const ETH_SIGN_TYPE_DATA_V4 = 'eth_signTypedData_v4';

static const walletConnectMethods = [
PERSONAL_SIGN,
ETH_SIGN,
ETH_SIGN,
ETH_SIGN_TRANSACTION,
ETH_SIGN_TYPE_DATA,
ETH_SEND_TRANSACTION,
ETH_SIGN_TYPE_DATA_V4,
];

static const walletConnectEvents = ['chainChanged', 'accountsChanged'];
}
112 changes: 61 additions & 51 deletions lib/connection_bridge/wallet_connect/cubit/wallet_connect_cubit.dart
Original file line number Diff line number Diff line change
Expand Up @@ -69,51 +69,11 @@ class WalletConnectCubit extends Cubit<WalletConnectState> {
.where((e) => e.blockchainType != BlockchainType.tezos)
.toList();

final events = ['chainChanged', 'accountsChanged'];

for (final accounts in eVMAccounts) {
log.i(accounts.blockchainType);

log.i('registerEventEmitter');
for (final String event in events) {
_web3Wallet!.registerEventEmitter(
chainId: accounts.blockchainType.chain,
event: event,
);
}

registerAccount(accounts);

log.i('registerRequestHandler');
_web3Wallet!.registerRequestHandler(
chainId: accounts.blockchainType.chain,
method: Parameters.PERSONAL_SIGN,
handler: personalSign,
);
_web3Wallet!.registerRequestHandler(
chainId: accounts.blockchainType.chain,
method: Parameters.ETH_SIGN,
handler: ethSign,
);
_web3Wallet!.registerRequestHandler(
chainId: accounts.blockchainType.chain,
method: Parameters.ETH_SIGN_TRANSACTION,
handler: ethSignTransaction,
);
_web3Wallet!.registerRequestHandler(
chainId: accounts.blockchainType.chain,
method: Parameters.ETH_SIGN_TYPE_DATA,
handler: ethSignTypedData,
);
_web3Wallet!.registerRequestHandler(
chainId: accounts.blockchainType.chain,
method: Parameters.ETH_SIGN_TYPE_DATA_V4,
handler: ethSignTypedDataV4,
);
_web3Wallet!.registerRequestHandler(
chainId: accounts.blockchainType.chain,
method: Parameters.ETH_SEND_TRANSACTION,
handler: ethSendTransaction,
log.i('registering acconts');
for (final evm in eVMAccounts) {
_web3Wallet!.registerAccount(
chainId: evm.blockchainType.chain,
accountAddress: evm.walletAddress,
);
}
}
Expand All @@ -127,6 +87,20 @@ class WalletConnectCubit extends Cubit<WalletConnectState> {
_web3Wallet!.onSessionConnect.subscribe(_onSessionConnect);
_web3Wallet!.onAuthRequest.subscribe(_onAuthRequest);

/// register request emitter and request handler for all supported evms
for (final blockchainType in BlockchainType.values) {
if (blockchainType == BlockchainType.tezos) {
continue;
}
log.i(blockchainType);
log.i('registerEventEmitter');
registerEventEmitter(blockchainType.chain);

log.i('registerRequestHandler');
registerRequestHandler(blockchainType.chain);
}

log.i('web3wallet init');
await _web3Wallet!.init();
log.i('metadata');
Expand All @@ -143,11 +117,45 @@ class WalletConnectCubit extends Cubit<WalletConnectState> {
}
}

void registerAccount(CryptoAccountData cryptoAccountData) {
log.i('registerAccount - $cryptoAccountData');
_web3Wallet!.registerAccount(
chainId: cryptoAccountData.blockchainType.chain,
accountAddress: cryptoAccountData.walletAddress,
void registerEventEmitter(String chain) {
for (final String event in Parameters.walletConnectEvents) {
_web3Wallet!.registerEventEmitter(
chainId: chain,
event: event,
);
}
}

void registerRequestHandler(String chain) {
_web3Wallet!.registerRequestHandler(
chainId: chain,
method: Parameters.PERSONAL_SIGN,
handler: personalSign,
);
_web3Wallet!.registerRequestHandler(
chainId: chain,
method: Parameters.ETH_SIGN,
handler: ethSign,
);
_web3Wallet!.registerRequestHandler(
chainId: chain,
method: Parameters.ETH_SIGN_TRANSACTION,
handler: ethSignTransaction,
);
_web3Wallet!.registerRequestHandler(
chainId: chain,
method: Parameters.ETH_SIGN_TYPE_DATA,
handler: ethSignTypedData,
);
_web3Wallet!.registerRequestHandler(
chainId: chain,
method: Parameters.ETH_SIGN_TYPE_DATA_V4,
handler: ethSignTypedDataV4,
);
_web3Wallet!.registerRequestHandler(
chainId: chain,
method: Parameters.ETH_SEND_TRANSACTION,
handler: ethSendTransaction,
);
}

Expand Down Expand Up @@ -196,7 +204,9 @@ class WalletConnectCubit extends Cubit<WalletConnectState> {
log.i(args);
//sessions.value.add(args.session);

final savedDappData = SavedDappData(sessionData: args.session);
final savedDappData = SavedDappData(
sessionData: args.session,
);

log.i(savedDappData.toJson());
connectedDappRepository.insert(savedDappData);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,9 +84,38 @@ class ConfirmConnectionCubit extends Cubit<ConfirmConnectionState> {
final SessionProposalEvent? sessionProposalEvent =
walletConnectState.sessionProposalEvent;

final eVMAccounts = walletCubit.state.cryptoAccount.data
.where((e) => e.blockchainType != BlockchainType.tezos)
.toList();

final accounts = <String>[];

final allowedNamespaces = [
...sessionProposalEvent!
.params.optionalNamespaces['eip155']!.chains!,
...sessionProposalEvent
.params.requiredNamespaces['eip155']!.chains!,
];

log.i(allowedNamespaces);

for (final evm in eVMAccounts) {
if (allowedNamespaces.contains(evm.blockchainType.chain)) {
accounts.add('${evm.blockchainType.chain}:${evm.walletAddress}');
}
}

final walletNamespaces = {
'eip155': Namespace(
accounts: accounts,
methods: Parameters.walletConnectMethods,
events: Parameters.walletConnectEvents,
),
};

await walletConnectCubit.web3Wallet!.approveSession(
id: sessionProposalEvent!.id,
namespaces: sessionProposalEvent.params.generatedNamespaces!,
namespaces: walletNamespaces,
);

/// dApp saved onSessionConnect function in wallet connect cubit
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -126,20 +126,20 @@ class ConfirmConnectionView extends StatelessWidget {
),
const SizedBox(height: Sizes.spaceXLarge),
const Permissions(),
const SizedBox(height: Sizes.spaceXLarge),
const SizedBox(height: Sizes.spaceNormal),
if (connectionBridgeType ==
ConnectionBridgeType.walletconnect) ...[
Text(
walletConnectCubit.state.sessionProposalEvent!.params
.proposer.metadata.url,
textAlign: TextAlign.center,
),
const SizedBox(height: Sizes.spaceLarge),
Text(
walletConnectCubit.state.sessionProposalEvent!.params
.generatedNamespaces!
.toString(),
),
const SizedBox(height: Sizes.spaceNormal),
// Text(
// walletConnectCubit.state.sessionProposalEvent!.params
// .generatedNamespaces!
// .toString(),
// ),
// ListView.builder(
// itemCount: walletConnectCubit
// .state
Expand Down Expand Up @@ -199,11 +199,7 @@ class ConfirmConnectionView extends StatelessWidget {
// },
// )
],
if (connectionBridgeType !=
ConnectionBridgeType.walletconnect) ...[
const SizedBox(height: Sizes.spaceXLarge),
SelectAccount(connectionBridgeType: connectionBridgeType),
],
SelectAccount(connectionBridgeType: connectionBridgeType),
const SizedBox(height: Sizes.spaceNormal),
],
),
Expand Down

0 comments on commit e30d42d

Please sign in to comment.