Flutter

It is strongly discouraged to use private key or mnemonic import/generate function, if you use it, you need to secure the data yourself, Particle's SDK has no relationship with the imported/generated mnemonic or private key.

1.Add the Connect Service SDK to Your Flutter App

Run this command:

With Flutter:

flutter pub add particle_connect

click here to get the demo source code

2.Configure Android project

open ${your flutter project} /example/app/build.gradle. reference click here

2.1 add project config and update the minSdkVersion to 23

//Modify these configurations
defaultConfig {
    // TODO: Specify your own unique Application ID (https://developer.android.com/studio/build/application-id.html).
    applicationId "com.example.particle_auth_test"
    // You can update the following values to match your application needs.
    // For more information, see: https://docs.flutter.dev/deployment/android#reviewing-the-build-configuration.
    minSdkVersion flutter.minSdkVersion
    targetSdkVersion flutter.targetSdkVersion
    versionCode flutterVersionCode.toInteger()
    versionName flutterVersionName
}

//After modification๏ผš
defaultConfig {
   // TODO: Specify your own unique Application ID (https://developer.android.com/studio/build/application-id.html).
   applicationId "com.example.particle_auth_test"
   // You can update the following values to match your application needs.
   // For more information, see: https://docs.flutter.dev/deployment/android#reviewing-the-build-configuration.
   minSdkVersion 23 // required by particle sdk
   targetSdkVersion flutter.targetSdkVersion
   versionCode flutterVersionCode.toInteger()
   versionName flutterVersionName

   //get from https://dashboard.particle.network/
   manifestPlaceholders["PN_PROJECT_ID"] = "772f7499-1d2e-40f4-8e2c-7b6dd47db9de" //your project id
   manifestPlaceholders["PN_PROJECT_CLIENT_KEY"] = "ctWeIc2UBA6sYTKJknT9cu9LBikF00fbk1vmQjsV" //your project client key
   manifestPlaceholders["PN_APP_ID"] = "01a23ce8-d2e9-4b37-9eab-bf477279e53e" //your app id
}

//Modify these configurations
dependencies {
   ...
}
//After modification๏ผš
dependencies {
    modules {
        module("org.bouncycastle:bcprov-jdk15to18") {
            replacedBy("org.bouncycastle:bcprov-jdk15on")
        }
    }
    ...
}

2.2 update Java Version to 11 and add dataBinding config

//Modify these configurations
compileOptions {
  sourceCompatibility JavaVersion.VERSION_1_8
  targetCompatibility JavaVersion.VERSION_1_8
}

kotlinOptions {
  jvmTarget = '1.8'
}

//After modification๏ผš
compileOptions {
  sourceCompatibility JavaVersion.VERSION_11
  targetCompatibility JavaVersion.VERSION_11
}

kotlinOptions {
  jvmTarget = JavaVersion.VERSION_11
}

dataBinding {
  enabled = true
}

Now,Android configuration is complete!

3.Configure iOS project

Prerequisites

  • Install the following:

    • Xcode 14.1 or later.

  • Make sure that your project meets the following requirements:

    • Your project must target these platform versions or later:

      • iOS 14

3.1 After export iOS project, open Runner.xcworkspace under ios folder.

3.2 Create a ParticleNetwork-Info.plist into the root of your Xcode project, and make sure the file is checked under Target Membership.

3.3 Copy the following text into this file:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
	<key>PROJECT_UUID</key>
	<string>YOUR_PROJECT_UUID</string>
	<key>PROJECT_CLIENT_KEY</key>
	<string>YOUR_PROJECT_CLIENT_KEY</string>
	<key>PROJECT_APP_UUID</key>
	<string>YOUR_PROJECT_APP_UUID</string>
</dict>
</plist>

3.4 Replace YOUR_PROJECT_UUID, YOUR_PROJECT_CLIENT_KEY, and YOUR_PROJECT_APP_UUID with the new values created in your Dashboard

3.5. Import the ParticleAuthService module in your AppDelegate.swift file.

import ParticleConnect

3.6. Add the scheme URL handle in your app's application(_:open:options:) method

override func application(_ app: UIApplication, open url: URL, options: [UIApplication.OpenURLOptionsKey: Any] = [:]) -> Bool {
    if ParticleConnect.handleUrl(url) {
        return true
    } else {
        return super.application(app, open: url, options: options)
    }
}

3.7. Configure your app scheme URL, select your app from TARGETS, under Info section, click + to add the URL types, and paste your scheme in URL Schemes

Your scheme URL should be "pn" + your project app uuid.

For example, if your project app id is "63bfa427-cf5f-4742-9ff1-e8f5a1b9828f", your scheme URL is "pn63bfa427-cf5f-4742-9ff1-e8f5a1b9828f".

3.8 In Xcode right-click your Info.plist file and choose "Open As Source Code".

Copy & Paste the XML snippet into the body of your file (<dict>...</dict>).

<key>LSApplicationQueriesSchemes</key>
<array>
    <string>imtokenv2</string>
    <string>metamask</string>
    <string>phantom</string>
    <string>bitkeep</string>
    <string>trust</string>
    <string>rainbow</string>
    <string>zerion</string>
    <string>mathwallet</string>
    <string>1inch</string>
    <string>awallet</string>
    <string>okex</string>
</array>
<key>NSPhotoLibraryUsageDescription</key>
    <string>We need access in order to open photos of barcodes</string>
<key>NSCameraUsageDescription</key>
    <string>We use the camera to scan barcodes</string>

3.9 Edit Podfile, you should follow Podfile required to edit Podfile.

Initialize the SDK

Before using the SDK you have to call init(Required)

// Get your project id and client key from dashboard, 
// https://dashboard.particle.network
const projectId = ""; // your project id
const clientK = ""; // your client key 
ParticleInfo.set(projectId, clientK);

Migrating to WalletConnect v2

Starting from version 0.14.0, WalletConnectV2 is supported.

// Init particle connect SDK,
// dappInfo is your app info, will pass to wallet when wallet connect.
final dappInfo = DappMetaData(
        "your wallet connect project id",
        "Particle Connect",
        "https://connect.particle.network/icons/512.png",
        "https://connect.particle.network",
        "Particle Connect Flutter Demo");
ParticleConnect.init(currChainInfo, dappInfo, Env.dev);

// set wallet connect support chaininfos, if you dont call this method
// default value is current chaininfo.
List<ChainInfo> chainInfos = <ChainInfo>[
      ChainInfo.Ethereum,
      ChainInfo.Polygon
    ];
    ParticleConnect.setWalletConnectV2SupportChainInfos(chainInfos);

Connect

final account = await ParticleConnect.connect(walletType);

Disconnect

String result = await ParticleConnect.disconnect(walletType, getPublicAddress());

IsConnected

bool isConnected = await ParticleConnect.isConnected(walletType, getPublicAddress());

Sign-In With Ethereum or Solana

const domain = "particle.network";
const uri = "https://docs.particle.network/";
ConnectLogic.siwe = await ParticleConnect.signInWithEthereum(walletType, getPublicAddress(), domain, uri);

Verify

bool result = await ParticleConnect.verify(walletType, getPublicAddress(),
          ConnectLogic.siwe.message, ConnectLogic.siwe.signature);

Sign message

In EVM chain requires a hexadecimal string, in Solana chain requires a human readable string.

final messageHex = "0x${StringUtils.toHexString("Hello Particle")}";
String signature = await ParticleConnect.signMessage(walletType, getPublicAddress(), messageHex);

Sign transaction

Only support Solana chain, in Solana chain requires a base58 string.

Please explore our GitHub. In the example/transaction_mock.dart file, you can learn how to mock a test transaction.

String signature = await ParticleConnect.signTransaction(walletType, getPublicAddress(), transaction);

Sign all transactions

Only support Solana chain, in Solana chain requires a base58 string.

Please explore our GitHub. In the example/transaction_mock.dart file, you can learn how to mock a test transaction.

String signature = await ParticleConnect.signAllTransactions(walletType, getPublicAddress(), transactions);

Sign and send transaction

In EVM chain requires a hexadecimal string, in Solana chain requires a base58 string.

Please explore our GitHub. In the example/transaction_mock.dart file, you can learn how to mock a test transaction.

String signature = await ParticleConnect.signAndSendTransaction(walletType, getPublicAddress(), transaction);

Sign typed data

In EVM chain requires a hexadecimal string, not support Solana.

String typedDataHex = "0x${StringUtils.toHexString(typedData)}";
String signature = await ParticleConnect.signTypedData(walletType, getPublicAddress(), typedDataHex);

Import private key

Support both EVM and Solana private keys.

final account = await ParticleConnect.importPrivateKey(walletType, privateKey);

Import mnemonic

Support both EVM and Solana mnemonics.

final account = await ParticleConnect.importMnemonic(walletType, mnemonic);

Export private key

Private keys can only be exported from accounts that have been imported through private keys or mnemonics.

String privateKey = await ParticleConnect.exportPrivateKey(walletType, getPublicAddress());

Reconnect wallet connect wallet

Only support iOS, works for extend wallet connect connection.

ParticleConnect.reconnectIfNeeded(walletType, getPublicAddress());

Last updated