Skip to content
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
5 changes: 4 additions & 1 deletion js_api/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { WsProvider, ApiPromise } from "@polkadot/api";
import type { RegistryTypes } from '@polkadot/types-codec/types';
import { subscribeMessage, getNetworkConst, getNetworkProperties } from "./service/setting";
import keyring from "./service/keyring";
import account from "./service/account";
Expand Down Expand Up @@ -32,15 +33,17 @@ async function connectAll(nodes: string[]) {
* connect to a specific node.
*
* @param {string} nodeEndpoint
* @param {RegistryTypes} types for ApiOptions
*/
async function connect(nodes: string[]) {
async function connect(nodes: string[], types: RegistryTypes | undefined = undefined) {
(<any>window).api = undefined;

return new Promise(async (resolve, reject) => {
const wsProvider = new WsProvider(nodes);
try {
const res = await ApiPromise.create({
provider: wsProvider,
types: types,
});
if (!(<any>window).api) {
(<any>window).api = res;
Expand Down
5 changes: 3 additions & 2 deletions lib/api/api.dart
Original file line number Diff line number Diff line change
Expand Up @@ -91,9 +91,10 @@ class PolkawalletApi {

/// connect to a list of nodes, return null if connect failed.
Future<NetworkParams?> connectNode(
Keyring keyringStorage, List<NetworkParams> nodes) async {
Keyring keyringStorage, List<NetworkParams> nodes,
[Map<String, dynamic>? types]) async {
_connectedNode = null;
final NetworkParams? res = await service.webView!.connectNode(nodes);
final NetworkParams? res = await service.webView!.connectNode(nodes, types);
if (res != null) {
_connectedNode = res;

Expand Down
7 changes: 6 additions & 1 deletion lib/plugin/index.dart
Original file line number Diff line number Diff line change
Expand Up @@ -155,10 +155,15 @@ abstract class PolkawalletPlugin implements PolkawalletPluginBase {
/// 3. subscribe balances & set balancesStore.
Future<NetworkParams?> start(Keyring keyring,
{List<NetworkParams>? nodes,
Map<String, dynamic>? types,
KeyringEVM? keyringEVM,
NetworkParams? nodeEVM}) async {
if (nodeEVM == null) {
final res = await sdk.api.connectNode(keyring, nodes ?? nodeList);
final res = await sdk.api.connectNode(
keyring,
nodes ?? nodeList,
types,
);
if (res == null) return null;

keyring.setSS58(res.ss58);
Expand Down
15 changes: 10 additions & 5 deletions lib/service/webViewRunner.dart
Original file line number Diff line number Diff line change
Expand Up @@ -227,16 +227,21 @@ class WebViewRunner {
return c.future;
}

Future<NetworkParams?> connectNode(List<NetworkParams> nodes) async {
Future<NetworkParams?> connectNode(
List<NetworkParams> nodes,
Map<String, dynamic>? types,
) async {
final isAvatarSupport = (await evalJavascript(
'settings.connectAll ? {}:null',
wrapPromise: false)) !=
null;

final nodesEncoded = jsonEncode(nodes.map((e) => e.endpoint).toList());
final typesEncoded = types != null ? ', ${jsonEncode(types)}' : '';

final dynamic res = await (isAvatarSupport
? evalJavascript(
'settings.connectAll(${jsonEncode(nodes.map((e) => e.endpoint).toList())})')
: evalJavascript(
'settings.connect(${jsonEncode(nodes.map((e) => e.endpoint).toList())})'));
? evalJavascript('settings.connectAll($nodesEncoded$typesEncoded)')
: evalJavascript('settings.connect($nodesEncoded$typesEncoded)'));
if (res != null) {
final index = nodes.indexWhere((e) => e.endpoint!.trim() == res.trim());
if (_webViewOOMReload) {
Expand Down