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

Enhancements on Wallet #263

Merged
merged 1 commit into from
Feb 8, 2024
Merged
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
## 2.2.0-beta03
## 2.2.0-beta02

- Improvements on example wallet
Expand Down
174 changes: 113 additions & 61 deletions example/wallet/lib/dependencies/chains/evm_service.dart
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ class EVMService {
event: event,
);
}

// Supported methods
Map<String, dynamic Function(String, dynamic)> methodsHandlers = {
'personal_sign': personalSign,
Expand All @@ -65,13 +66,16 @@ class EVMService {
}
}

Future<dynamic> personalSign(String topic, dynamic parameters) async {
Future<void> personalSign(String topic, dynamic parameters) async {
debugPrint('[$runtimeType] personalSign request: $parameters');
dynamic result;

// final pRequest = _web3Wallet.pendingRequests.getAll().first;
final pRequest = _web3Wallet.pendingRequests.getAll().first;
final data = EthUtils.getDataFromParamsList(parameters);
final message = EthUtils.getUtf8Message(data.toString());
var response = JsonRpcResponse(
id: pRequest.id,
jsonrpc: '2.0',
);

if (await requestApproval(message)) {
try {
Expand All @@ -81,33 +85,43 @@ class EVMService {
);
final credentials = EthPrivateKey.fromHex(keys[0].privateKey);

final String signature = hex.encode(
final signature = hex.encode(
credentials.signPersonalMessageToUint8List(
Uint8List.fromList(utf8.encode(message)),
),
);

result = '0x$signature';
response = response.copyWith(result: '0x$signature');
} catch (e) {
debugPrint('[$runtimeType] personalSign error $e');
result = JsonRpcError(code: 0, message: e.toString());
response = response.copyWith(
error: JsonRpcError(code: 0, message: e.toString()),
);
}
} else {
result = const JsonRpcError(code: 5001, message: 'User rejected method');
response = response.copyWith(
error: const JsonRpcError(code: 5001, message: 'User rejected method'),
);
}

_goBackToDapp(topic, result);
_goBackToDapp(topic, response.result ?? response.error);

return result;
return _web3Wallet.respondSessionRequest(
topic: topic,
response: response,
);
}

Future<dynamic> ethSign(String topic, dynamic parameters) async {
Future<void> ethSign(String topic, dynamic parameters) async {
debugPrint('[$runtimeType] ethSign request: $parameters');
dynamic result;

// final pRequest = _web3Wallet.pendingRequests.getAll().first;
final pRequest = _web3Wallet.pendingRequests.getAll().first;
final data = EthUtils.getDataFromParamsList(parameters);
final message = EthUtils.getUtf8Message(data.toString());
var response = JsonRpcResponse(
id: pRequest.id,
jsonrpc: '2.0',
);

if (await requestApproval(message)) {
try {
Expand All @@ -123,74 +137,79 @@ class EVMService {
),
);

result = '0x$signature';
response = response.copyWith(result: '0x$signature');
} catch (e) {
debugPrint('[$runtimeType] ethSign error $e');
result = JsonRpcError(code: 0, message: e.toString());
response = response.copyWith(
error: JsonRpcError(code: 0, message: e.toString()),
);
}
} else {
result = const JsonRpcError(code: 5001, message: 'User rejected method');
response = response.copyWith(
error: const JsonRpcError(code: 5001, message: 'User rejected method'),
);
}

_goBackToDapp(topic, result);
_goBackToDapp(topic, response.result ?? response.error);

return result;
return _web3Wallet.respondSessionRequest(
topic: topic,
response: response,
);
}

Future<dynamic> ethSignTypedData(String topic, dynamic parameters) async {
Future<void> ethSignTypedData(String topic, dynamic parameters) async {
debugPrint('[$runtimeType] ethSignTypedData request: $parameters');
dynamic result;

// final pRequest = _web3Wallet.pendingRequests.getAll().first;
final pRequest = _web3Wallet.pendingRequests.getAll().first;
final data = parameters[1] as String;
var response = JsonRpcResponse(
id: pRequest.id,
jsonrpc: '2.0',
);
if (await requestApproval(data)) {
try {
final keys = GetIt.I<IKeyService>().getKeysForChain(
chainSupported.chainId,
);

result = EthSigUtil.signTypedData(
final signature = EthSigUtil.signTypedData(
privateKey: keys[0].privateKey,
jsonData: data,
version: TypedDataVersion.V4,
);
response = response.copyWith(result: signature);
} catch (e) {
debugPrint('[$runtimeType] ethSignTypedData error $e');
result = JsonRpcError(code: 0, message: e.toString());
response = response.copyWith(
error: JsonRpcError(code: 0, message: e.toString()),
);
}
} else {
result = const JsonRpcError(code: 5001, message: 'User rejected method');
response = response.copyWith(
error: const JsonRpcError(code: 5001, message: 'User rejected method'),
);
}

_goBackToDapp(topic, result);

return result;
}
_goBackToDapp(topic, response.result ?? response.error);

Future<void> switchChain(String topic, dynamic parameters) async {
debugPrint('received switchChain request: $topic $parameters');
final params = (parameters as List).first as Map<String, dynamic>;
final hexChainId = params['chainId'].toString().replaceFirst('0x', '');
final chainId = int.parse(hexChainId, radix: 16);
final web3wallet = _web3WalletService.getWeb3Wallet();
await web3wallet.emitSessionEvent(
return _web3Wallet.respondSessionRequest(
topic: topic,
chainId: 'eip155:$chainId',
event: SessionEventParams(
name: 'chainChanged',
data: chainId,
),
response: response,
);
}

Future<dynamic> ethSignTransaction(String topic, dynamic parameters) async {
debugPrint('[$runtimeType] ethSignTransaction request: $parameters');
dynamic result;
final pRequest = _web3Wallet.pendingRequests.getAll().first;
var response = JsonRpcResponse(
id: pRequest.id,
jsonrpc: '2.0',
);

// final pRequest = _web3Wallet.pendingRequests.getAll().first;
final tJson = parameters[0] as Map<String, dynamic>;
final transaction = await approveTransaction(tJson);
if (transaction != null) {
final result = await approveTransaction(tJson);
if (result is Transaction) {
try {
// Load the private key
final keys = GetIt.I<IKeyService>().getKeysForChain(
Expand All @@ -202,37 +221,47 @@ class EVMService {

final signature = await ethClient.signTransaction(
credentials,
transaction,
result,
chainId: int.parse(chainId),
);
// Sign the transaction
final signedTx = hex.encode(signature);

result = '0x$signedTx';
response = response.copyWith(result: '0x$signedTx');
} on RPCError catch (e) {
debugPrint('[$runtimeType] ethSignTransaction error $e');
result = JsonRpcError(code: e.errorCode, message: e.message);
response = response.copyWith(
error: JsonRpcError(code: e.errorCode, message: e.message),
);
} catch (e) {
debugPrint('[$runtimeType] ethSignTransaction error $e');
result = JsonRpcError(code: 0, message: e.toString());
response = response.copyWith(
error: JsonRpcError(code: 0, message: e.toString()),
);
}
} else {
result = const JsonRpcError(code: 5001, message: 'User rejected method');
response = response.copyWith(error: result as JsonRpcError);
}

_goBackToDapp(topic, result);
_goBackToDapp(topic, response.result ?? response.error);

return result;
return _web3Wallet.respondSessionRequest(
topic: topic,
response: response,
);
}

Future<dynamic> ethSendTransaction(String topic, dynamic parameters) async {
debugPrint('[$runtimeType] ethSendTransaction request: $parameters');
dynamic result;
final pRequest = _web3Wallet.pendingRequests.getAll().first;
var response = JsonRpcResponse(
id: pRequest.id,
jsonrpc: '2.0',
);

// final pRequest = _web3Wallet.pendingRequests.getAll().first;
final tJson = parameters[0] as Map<String, dynamic>;
final transaction = await approveTransaction(tJson);
if (transaction is Transaction) {
final result = await approveTransaction(tJson);
if (result is Transaction) {
try {
// Load the private key
final keys = GetIt.I<IKeyService>().getKeysForChain(
Expand All @@ -244,25 +273,48 @@ class EVMService {

final signedTx = await ethClient.sendTransaction(
credentials,
transaction,
result,
chainId: int.parse(chainId),
);

result = '0x$signedTx';
response = response.copyWith(result: '0x$signedTx');
} on RPCError catch (e) {
debugPrint('[$runtimeType] ethSendTransaction error $e');
result = JsonRpcError(code: e.errorCode, message: e.message);
response = response.copyWith(
error: JsonRpcError(code: e.errorCode, message: e.message),
);
} catch (e) {
debugPrint('[$runtimeType] ethSendTransaction error $e');
result = JsonRpcError(code: 0, message: e.toString());
response = response.copyWith(
error: JsonRpcError(code: 0, message: e.toString()),
);
}
} else {
result = transaction as JsonRpcError;
response = response.copyWith(error: result as JsonRpcError);
}

_goBackToDapp(topic, result);
_goBackToDapp(topic, response.result ?? response.error);

return result;
return _web3Wallet.respondSessionRequest(
topic: topic,
response: response,
);
}

Future<void> switchChain(String topic, dynamic parameters) async {
debugPrint('received switchChain request: $topic $parameters');
final params = (parameters as List).first as Map<String, dynamic>;
final hexChainId = params['chainId'].toString().replaceFirst('0x', '');
final chainId = int.parse(hexChainId, radix: 16);
final web3wallet = _web3WalletService.getWeb3Wallet();
await web3wallet.emitSessionEvent(
topic: topic,
chainId: 'eip155:$chainId',
event: SessionEventParams(
name: 'chainChanged',
data: chainId,
),
);
}

void _goBackToDapp(String topic, dynamic result) {
Expand Down
8 changes: 8 additions & 0 deletions example/wallet/lib/models/chain_data.dart
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,14 @@ class ChainData {
color: Colors.orange,
rpc: ['https://api.avax.network/ext/bc/C/rpc'],
),
const ChainMetadata(
type: ChainType.eip155,
chainId: 'eip155:56',
name: 'BNB Smart Chain Mainnet',
logo: '/chain-logos/eip155-56.png',
color: Colors.orange,
rpc: ['https://bsc-dataseed1.bnbchain.org'],
),
const ChainMetadata(
type: ChainType.eip155,
chainId: 'eip155:42220',
Expand Down
2 changes: 1 addition & 1 deletion example/wallet/lib/utils/namespace_model_builder.dart
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ class ConnectionWidgetBuilder {
}
models.add(
WCConnectionModel(
title: StringConstants.events,
title: '${StringConstants.events} (Tap to send)',
elements: ns.events,
elementActions: actions,
),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,24 +51,34 @@ class WCConnectionWidgetInfo extends StatelessWidget {
}

Widget _buildElement(String text) {
return InkWell(
onTap: model.elementActions != null ? model.elementActions![text] : null,
child: Container(
decoration: BoxDecoration(
color: StyleConstants.layerColor4,
borderRadius: BorderRadius.circular(
StyleConstants.linear16,
),
return ElevatedButton(
onPressed:
model.elementActions != null ? model.elementActions![text] : null,
style: ButtonStyle(
elevation: model.elementActions != null
? MaterialStateProperty.all(4.0)
: MaterialStateProperty.all(0.0),
padding: MaterialStateProperty.all(const EdgeInsets.all(0.0)),
visualDensity: VisualDensity.compact,
backgroundColor: MaterialStateProperty.all(
StyleConstants.layerColor4,
),
overlayColor: MaterialStateProperty.all(Colors.white),
shape: MaterialStateProperty.resolveWith<RoundedRectangleBorder>(
(states) {
return RoundedRectangleBorder(
borderRadius: BorderRadius.circular(StyleConstants.linear16),
);
},
),
// margin: const EdgeInsets.all(2),
),
child: Container(
padding: const EdgeInsets.all(
StyleConstants.linear8,
),
child: Text(
text,
style: StyleConstants.layerTextStyle4,
maxLines: 10,
overflow: TextOverflow.ellipsis,
),
),
);
Expand All @@ -78,7 +88,6 @@ class WCConnectionWidgetInfo extends StatelessWidget {
return Text(
model.text!,
style: StyleConstants.layerTextStyle3,
// textAlign: TextAlign.center,
);
}
}
2 changes: 1 addition & 1 deletion lib/src/version.dart

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

Loading
Loading