Skip to content

Commit

Permalink
[submit_to_js] integrate extrinsic sending with dart
Browse files Browse the repository at this point in the history
  • Loading branch information
clangenb committed Oct 22, 2023
1 parent 4e596c0 commit 0be05c6
Show file tree
Hide file tree
Showing 10 changed files with 233 additions and 103 deletions.
37 changes: 18 additions & 19 deletions app/js_service_encointer/src/service/account.js
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,7 @@ export function sendTxWithPair (keyPair, txInfo, paramList) {
balanceHuman = paramList[2];
}

return getMaybeSignedXt(keyPair, txInfo, paramList)
return _getXt(keyPair, txInfo, paramList)
.then(function (tx) {
// The promise syntax is necessary because we need fine-grained control over when the overarching
// future returns based on what happens within the `onStatusChange` callback. I could not find another
Expand Down Expand Up @@ -278,10 +278,24 @@ export function sendTxWithPair (keyPair, txInfo, paramList) {
});
}

export async function getXt (txInfo, paramList){
const keyPair = keyring.getPair(hexToU8a(txInfo.pubKey));
try {
keyPair.decodePkcs8(txInfo.password);
} catch (err) {
return new Promise((resolve, reject) => {
resolve({ error: 'password check failed' });
});
}

// The dart<>JS interface expects a map.
return { xt: await _getXt(keyPair, txInfo, paramList) };
}

/**
* Creates a extrinsic and signs it if the txInfo says so.
* Creates an extrinsic and signs it if the txInfo says so.
*/
export async function getMaybeSignedXt (keyPair, txInfo, paramList) {
export async function _getXt (keyPair, txInfo, paramList) {
if (txInfo.module === encointerBalances && txInfo.call === transfer) {
paramList[2] = stringNumberToEncointerBalanceU8a(paramList[2]);
}
Expand Down Expand Up @@ -313,20 +327,6 @@ export async function getMaybeSignedXt (keyPair, txInfo, paramList) {
return tx.signAsync(keyPair, signerOptions);
}

/**
* Creates a extrinsic, signs it according to txInfo, and returns it as a scale-encoded hex value.
*/
export function getMaybeSignedXtHex (keyPair, txInfo, paramList) {
// unused currently, will be used when #1474 is implemented.
try {
return getMaybeSignedXt(keyPair, txInfo, paramList).toHex();
} catch (e) {
console.log(`[js-account/getMaybeSignedXtHex] error while creating xt: ${e.message}`);
// this is intended for the Dart<>JS interface, which expects errors like that.
return { error: e.message };
}
}

export function _sendTrustedTx (keyPair, txInfo, paramList) {
const cid = api.createType('CommunityIdentifier', txInfo.cid);
window.send('js-trustedTx', 'sending trusted tx for cid: ' + cid);
Expand Down Expand Up @@ -440,8 +440,7 @@ export default {
txFeeEstimate,
sendTx,
sendTxWithPair,
getMaybeSignedXt,
getMaybeSignedXtHex,
getXt,
sendFaucetTx,
checkPassword,
changePassword,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import 'dart:async';

import 'package:encointer_wallet/service/tx/lib/src/send_tx_dart.dart';
import 'package:ew_polkadart/generated/encointer_kusama/types/sp_runtime/dispatch_error.dart';
import 'package:ew_test_keys/ew_test_keys.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
Expand Down Expand Up @@ -157,14 +159,14 @@ class _PaymentConfirmationPageState extends State<PaymentConfirmationPage> {
}
}

void onFinish(BuildContext txPageContext, Map res) {
Log.d('Transfer result $res', 'PaymentConfirmationPage');
void onFinish(BuildContext txPageContext, ExtrinsicReport report) {
Log.d('Transfer result $report', 'PaymentConfirmationPage');
_transferState = TransferState.finished;
_blockTimestamp = DateTime.fromMillisecondsSinceEpoch(res['time'] as int);
_blockTimestamp = DateTime.fromMillisecondsSinceEpoch(report.timestamp as int);
}

void onError(dynamic errorMessage) {
Log.d('Error sending transfer $errorMessage', 'PaymentConfirmationPage');
void onError(DispatchError error) {
Log.d('Error sending transfer $error', 'PaymentConfirmationPage');
_transferState = TransferState.failed;
}

Expand Down
17 changes: 11 additions & 6 deletions app/lib/service/substrate_api/account_api.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,17 @@ import 'dart:convert';
import 'package:encointer_wallet/service/log/log_service.dart';
import 'package:encointer_wallet/service/notification/lib/notification.dart';
import 'package:encointer_wallet/service/substrate_api/core/js_api.dart';
import 'package:encointer_wallet/service/tx/lib/src/send_tx_dart.dart';
import 'package:encointer_wallet/store/account/account.dart';
import 'package:encointer_wallet/store/account/types/account_data.dart';
import 'package:encointer_wallet/store/app.dart';
import 'package:ew_polkadart/ew_polkadart.dart';

class AccountApi {
AccountApi(this.store, this.jsApi);
AccountApi(this.store, this.jsApi, this.provider);

final JSApi jsApi;
final Provider provider;
final AppStore store;
void Function()? fetchAccountData;

Expand Down Expand Up @@ -51,13 +54,15 @@ class AccountApi {
if (fetchData) fetchAccountData?.call();
}

Future<Map<String, dynamic>> sendTxAndShowNotification(
Future<ExtrinsicReport> sendTxAndShowNotification(
Map<String, dynamic> txInfo,
List<dynamic>? params, {
String? rawParam,
String? cid,
}) async {
final res = await sendTx(txInfo, params, rawParam: rawParam);
final res = await getSignedTx(txInfo, params, rawParam: rawParam);

final report = EWAuthorApi(provider).submitAndWatchExtrinsicWithReport(Extrinsic.fromHex(res['xt'] as String));

if (res['hash'] != null) {
final hash = res['hash'] as String;
Expand All @@ -68,12 +73,12 @@ class AccountApi {
cid: cid,
));
}
return res;
return report;
}

Future<Map<String, dynamic>> sendTx(Map txInfo, List? params, {String? rawParam}) async {
Future<Map<String, dynamic>> getSignedTx(Map txInfo, List? params, {String? rawParam}) async {
final param = rawParam ?? jsonEncode(params);
final call = 'account.sendTx(${jsonEncode(txInfo)}, $param)';
final call = 'account.getXt(${jsonEncode(txInfo)}, $param)';
Log.d('sendTx call: $call', 'AccountApi');
return jsApi.evalJavascript<Map<String, dynamic>>(call);
}
Expand Down
4 changes: 3 additions & 1 deletion app/lib/service/substrate_api/api.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import 'package:encointer_wallet/service/substrate_api/chain_api.dart';
import 'package:encointer_wallet/service/substrate_api/core/dart_api.dart';
import 'package:encointer_wallet/service/substrate_api/core/js_api.dart';
import 'package:encointer_wallet/service/substrate_api/encointer/encointer_api.dart';
import 'package:ew_polkadart/ew_polkadart.dart';

/// Global api instance
///
Expand Down Expand Up @@ -40,11 +41,12 @@ class Api {
String jsServiceEncointer, {
bool isIntegrationTest = false,
}) {
final provider = Provider.fromUri(Uri.parse(store.settings.endpoint.value!));
return Api(
store,
js,
dartApi,
AccountApi(store, js),
AccountApi(store, js, provider),
AssetsApi(store, js),
ChainApi(store, js),
EncointerApi(store, js, dartApi, ewHttp),
Expand Down
Loading

0 comments on commit 0be05c6

Please sign in to comment.