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

Add session update event for resolve disconnect from dapp #54

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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
59 changes: 20 additions & 39 deletions example/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,7 @@ class MyHomePage extends StatefulWidget {
_MyHomePageState createState() => _MyHomePageState();
}

const rpcUri =
'https://rpc-mainnet.maticvigil.com/v1/140d92ff81094f0f3d7babde06603390d7e581be';
const rpcUri = 'https://rpc-mainnet.maticvigil.com/v1/140d92ff81094f0f3d7babde06603390d7e581be';

enum MenuItems {
PREVIOUS_SESSION,
Expand Down Expand Up @@ -208,8 +207,7 @@ class _MyHomePageState extends State<MyHomePage> {
],
),
body: InAppWebView(
initialUrlRequest:
URLRequest(url: Uri.parse('https://example.walletconnect.org')),
initialUrlRequest: URLRequest(url: Uri.parse('https://example.walletconnect.org')),
initialOptions: InAppWebViewGroupOptions(
crossPlatform: InAppWebViewOptions(
useShouldOverrideUrlLoading: true,
Expand All @@ -222,8 +220,7 @@ class _MyHomePageState extends State<MyHomePage> {
final url = navAction.request.url.toString();
debugPrint('URL $url');
if (url.contains('wc?uri=')) {
final wcUri = Uri.parse(
Uri.decodeFull(Uri.parse(url).queryParameters['uri']!));
final wcUri = Uri.parse(Uri.decodeFull(Uri.parse(url).queryParameters['uri']!));
_qrScanHandler(wcUri.toString());
return NavigationActionPolicy.CANCEL;
} else if (url.startsWith('wc:')) {
Expand All @@ -245,9 +242,7 @@ class _MyHomePageState extends State<MyHomePage> {
name: "Example Wallet",
url: "https://example.wallet",
description: "Example Wallet",
icons: [
"https://gblobscdn.gitbook.com/spaces%2F-LJJeCjcLrr53DcT1Ml7%2Favatar.png"
],
icons: ["https://gblobscdn.gitbook.com/spaces%2F-LJJeCjcLrr53DcT1Ml7%2Favatar.png"],
);
_wcClient.connectNewSession(session: session, peerMeta: peerMeta);
}
Expand All @@ -256,9 +251,7 @@ class _MyHomePageState extends State<MyHomePage> {
_connectToPreviousSession() {
final _sessionSaved = _prefs.getString('session');
debugPrint('_sessionSaved $_sessionSaved');
_sessionStore = _sessionSaved != null
? WCSessionStore.fromJson(jsonDecode(_sessionSaved))
: null;
_sessionStore = _sessionSaved != null ? WCSessionStore.fromJson(jsonDecode(_sessionSaved)) : null;
if (_sessionStore != null) {
debugPrint('_sessionStore $_sessionStore');
_wcClient.connectFromSessionStore(_sessionStore!);
Expand All @@ -275,15 +268,15 @@ class _MyHomePageState extends State<MyHomePage> {
});
}

_onSwitchNetwork(int id, int chainId) async {
_onSwitchNetwork(String id, int chainId) async {
await _wcClient.updateSession(chainId: chainId);
_wcClient.approveRequest<Null>(id: id, result: null);
ScaffoldMessenger.of(context).showSnackBar(SnackBar(
content: Text('Changed network to $chainId.'),
));
}

_onSessionRequest(int id, WCPeerMeta peerMeta) {
_onSessionRequest(String id, WCPeerMeta peerMeta) {
showDialog(
context: context,
builder: (_) => SessionRequestView(
Expand All @@ -294,8 +287,7 @@ class _MyHomePageState extends State<MyHomePage> {
chainId: chainId,
);
_sessionStore = _wcClient.sessionStore;
await _prefs.setString(
'session', jsonEncode(_wcClient.sessionStore.toJson()));
await _prefs.setString('session', jsonEncode(_wcClient.sessionStore.toJson()));
Navigator.pop(context);
},
onReject: () {
Expand Down Expand Up @@ -383,7 +375,7 @@ class _MyHomePageState extends State<MyHomePage> {
}

_onSignTransaction(
int id,
String id,
WCEthereumTransaction ethereumTransaction,
) {
_onTransaction(
Expand Down Expand Up @@ -411,7 +403,7 @@ class _MyHomePageState extends State<MyHomePage> {
}

_onSendTransaction(
int id,
String id,
WCEthereumTransaction ethereumTransaction,
) {
_onTransaction(
Expand Down Expand Up @@ -440,7 +432,7 @@ class _MyHomePageState extends State<MyHomePage> {
}

_onTransaction({
required int id,
required String id,
required WCEthereumTransaction ethereumTransaction,
required String title,
required VoidCallback onConfirm,
Expand Down Expand Up @@ -552,8 +544,7 @@ class _MyHomePageState extends State<MyHomePage> {
),
),
Theme(
data:
Theme.of(context).copyWith(dividerColor: Colors.transparent),
data: Theme.of(context).copyWith(dividerColor: Colors.transparent),
child: Padding(
padding: const EdgeInsets.only(bottom: 8.0),
child: ExpansionTile(
Expand Down Expand Up @@ -606,7 +597,7 @@ class _MyHomePageState extends State<MyHomePage> {
}

_onSign(
int id,
String id,
WCEthereumSignMessage ethereumSignMessage,
) {
showDialog(
Expand Down Expand Up @@ -645,8 +636,7 @@ class _MyHomePageState extends State<MyHomePage> {
),
),
Theme(
data:
Theme.of(context).copyWith(dividerColor: Colors.transparent),
data: Theme.of(context).copyWith(dividerColor: Colors.transparent),
child: Padding(
padding: const EdgeInsets.only(bottom: 8.0),
child: ExpansionTile(
Expand Down Expand Up @@ -677,19 +667,16 @@ class _MyHomePageState extends State<MyHomePage> {
),
onPressed: () async {
String signedDataHex;
if (ethereumSignMessage.type ==
WCSignType.TYPED_MESSAGE) {
if (ethereumSignMessage.type == WCSignType.TYPED_MESSAGE) {
signedDataHex = EthSigUtil.signTypedData(
privateKey: privateKey,
jsonData: ethereumSignMessage.data!,
version: TypedDataVersion.V4,
);
} else {
final creds = EthPrivateKey.fromHex(privateKey);
final encodedMessage =
hexToBytes(ethereumSignMessage.data!);
final signedData =
await creds.signPersonalMessage(encodedMessage);
final encodedMessage = hexToBytes(ethereumSignMessage.data!);
final signedData = await creds.signPersonalMessage(encodedMessage);
signedDataHex = bytesToHex(signedData, include0x: true);
}
debugPrint('SIGNED $signedDataHex');
Expand Down Expand Up @@ -728,17 +715,11 @@ class _MyHomePageState extends State<MyHomePage> {
return Transaction(
from: EthereumAddress.fromHex(ethereumTransaction.from),
to: EthereumAddress.fromHex(ethereumTransaction.to!),
maxGas: ethereumTransaction.gasLimit != null
? int.tryParse(ethereumTransaction.gasLimit!)
: null,
gasPrice: ethereumTransaction.gasPrice != null
? EtherAmount.inWei(BigInt.parse(ethereumTransaction.gasPrice!))
: null,
maxGas: ethereumTransaction.gasLimit != null ? int.tryParse(ethereumTransaction.gasLimit!) : null,
gasPrice: ethereumTransaction.gasPrice != null ? EtherAmount.inWei(BigInt.parse(ethereumTransaction.gasPrice!)) : null,
value: EtherAmount.inWei(BigInt.parse(ethereumTransaction.value ?? '0')),
data: hexToBytes(ethereumTransaction.data!),
nonce: ethereumTransaction.nonce != null
? int.tryParse(ethereumTransaction.nonce!)
: null,
nonce: ethereumTransaction.nonce != null ? int.tryParse(ethereumTransaction.nonce!) : null,
);
}
}
8 changes: 3 additions & 5 deletions lib/models/exception/exceptions.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,10 @@ class HmacException implements Exception {
}

class InvalidJsonRpcParamsException implements Exception {
final int requestId;
final String requestId;
final String message;

InvalidJsonRpcParamsException(this.requestId)
: this.message = 'Invalid JSON RPC Request.';
InvalidJsonRpcParamsException(this.requestId) : this.message = 'Invalid JSON RPC Request.';

String toString() {
return "InvalidJsonRpcParamsException: $message";
Expand All @@ -27,8 +26,7 @@ class InvalidSessionException implements Exception {
}

class HandshakeException implements Exception {
final String message =
"handshakeId must be greater than 0 on session approve/reject.";
final String message = "handshakeId must be greater than 0 on session approve/reject.";

String toString() {
return "HandshakeException: $message";
Expand Down
5 changes: 2 additions & 3 deletions lib/models/jsonrpc/json_rpc_error_response.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,15 @@ part 'json_rpc_error_response.g.dart';

@JsonSerializable()
class JsonRpcErrorResponse {
final int id;
final String id;
final String jsonrpc = JSONRPC_VERSION;
final JsonRpcError error;
JsonRpcErrorResponse({
required this.id,
required this.error,
});

factory JsonRpcErrorResponse.fromJson(Map<String, dynamic> json) =>
_$JsonRpcErrorResponseFromJson(json);
factory JsonRpcErrorResponse.fromJson(Map<String, dynamic> json) => _$JsonRpcErrorResponseFromJson(json);
Map<String, dynamic> toJson() => _$JsonRpcErrorResponseToJson(this);

@override
Expand Down
10 changes: 3 additions & 7 deletions lib/models/jsonrpc/json_rpc_error_response.g.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 2 additions & 3 deletions lib/models/jsonrpc/json_rpc_request.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ part 'json_rpc_request.g.dart';

@JsonSerializable()
class JsonRpcRequest {
final int id;
final String id;
final String jsonrpc;
@JsonKey(unknownEnumValue: JsonKey.nullForUndefinedEnumValue)
final WCMethod? method;
Expand All @@ -18,8 +18,7 @@ class JsonRpcRequest {
required this.params,
});

factory JsonRpcRequest.fromJson(Map<String, dynamic> json) =>
_$JsonRpcRequestFromJson(json);
factory JsonRpcRequest.fromJson(Map<String, dynamic> json) => _$JsonRpcRequestFromJson(json);
Map<String, dynamic> toJson() => _$JsonRpcRequestToJson(this);

@override
Expand Down
11 changes: 4 additions & 7 deletions lib/models/jsonrpc/json_rpc_request.g.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 3 additions & 5 deletions lib/models/jsonrpc/json_rpc_response.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ part 'json_rpc_response.g.dart';

@JsonSerializable(genericArgumentFactories: true)
class JsonRpcResponse<T> {
final int id;
final String id;
final String jsonrpc;
final T result;
JsonRpcResponse({
Expand All @@ -14,11 +14,9 @@ class JsonRpcResponse<T> {
required this.result,
});

factory JsonRpcResponse.fromJson(Map<String, dynamic> json) =>
_$JsonRpcResponseFromJson(json, (object) => object as T);
factory JsonRpcResponse.fromJson(Map<String, dynamic> json) => _$JsonRpcResponseFromJson(json, (object) => object as T);
Map<String, dynamic> toJson() => _$JsonRpcResponseToJson(this, (t) => t);

@override
String toString() =>
'JsonRpcResponse(id: $id, jsonrpc: $jsonrpc, result: $result)';
String toString() => 'JsonRpcResponse(id: $id, jsonrpc: $jsonrpc, result: $result)';
}
2 changes: 1 addition & 1 deletion lib/models/jsonrpc/json_rpc_response.g.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading