一个用于与 ArchethicWallet RPC API 交互的客户端库。

什么是 ArchethicWallet RPC

查看 AEIP-4 以了解更多信息。

用法

添加依赖

$ flutter pub add archethic-wallet-client

设置 Deeplink

如果您的应用程序要在 AndroidiOS 上运行,则必须在您的应用程序上设置一个 Deeplink 端点。

这是 DeeplinkRPC 工作所必需的。

Android

必须将一个 queries 元素作为根元素的子元素添加到您的 manifest 中。

    <queries>
        <!-- AEWallet deeplink support -->
        <intent>
            <action android:name="android.intent.action.VIEW" />
            <data
                android:scheme="aewallet"
                android:host="archethic.tech" />
        </intent>
    </queries>

AndroidManifest.xml 文件中,在名为 “.MainActivity” 的 activity 标签内添加一个 meta-data 标签和一个 intent filter

<!-- AEWallet deeplink support -->
<meta-data
    android:name="flutter_deeplinking_enabled"
    android:value="true" />
<intent-filter android:autoVerify="true">
    <action android:name="android.intent.action.VIEW" />
    <category android:name="android.intent.category.DEFAULT" />
    <category android:name="android.intent.category.BROWSABLE" />
    <!-- Replace `flutterdappexample` by your custom deeplink scheme -->
    <!-- Replace `dapp.example` by your custom deeplink host -->
    <!-- These will be used to compose the replyUrl when sending RPCs -->
    <data
        android:scheme="flutterdappexample"  
        android:host="dapp.example" />
</intent-filter>

iOS

Info.plist 文件中添加 LSApplicationQueriesSchemes 条目。

<key>LSApplicationQueriesSchemes</key>
<array>
  <string>aewallet</string>
</array>

ios/Runner 目录中的 Info.plist 中添加两个新键。

<key>FlutterDeepLinkingEnabled</key>
<true/>
<key>CFBundleURLTypes</key>
<array>
    <dict>
    <key>CFBundleTypeRole</key>
    <string>Editor</string>
    <!-- Replace `flutterdappexample` by your custom deeplink scheme -->
    <!-- Replace `dapp.example` by your custom deeplink host -->
    <!-- These will be used to compose the replyUrl when sending RPCs -->
    <key>CFBundleURLName</key>
    <string>dapp.example</string>
    <key>CFBundleURLSchemes</key>
    <array>
    <string>flutterdappexample</string>
    </array>
    </dict>
</array>

其他原生设置

MacOS

添加

<key>com.apple.security.network.client</key>
<true/>

macos/Runner/DebugProfile.entitlementsmacos/Runner/Release.entitlements

客户端设置

实例化一个客户端

import 'package:archethic-wallet-client/archethic-wallet-client.dart';

// 1. Instanciate a Client
final _aewalletClient = ArchethicDAppClient.auto(
  origin: const RequestOrigin(        // Sets Dapp identity informations. Might be displayed to the user.
    name: 'FlutterDappExample',
  ),
  replyBaseUrl: 'flutterdappexample://dapp.example',    // Deeplink Dapp endpoint
);

[仅限 Deeplink] 监听 deeplink 响应

onGenerateRoute 方法中处理传入的 deeplinks。

// 2. Listens to deeplink responses
class MyApp extends StatelessWidget {
  const MyApp({super.key});

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Dapp Demo',
      home: MyHome(),
      onGenerateRoute: (settings) {
        if ((_aewalletClient as DeeplinkArchethicDappClient)
            .handleRoute(settings.name)) return;

        //... do everything else needed by your application
        return null;
      },
    );
  }
}

发出请求

final response = await _aewalletClient.sendTransaction(
    transactionJsonData,
);

response.when(
    failure: (failure) {
        log(
            'Transaction failed',
            error: failure,
        );
    },
    success: (result) {
        log(
            'Transaction succeed : ${json.encode(result)}',
        );
    },
);

GitHub

查看 Github