GithubHelp home page GithubHelp logo

blocto / fcl-android Goto Github PK

View Code? Open in Web Editor NEW
21.0 5.0 4.0 572 KB

🌊 Flow Client Library for Android

License: MIT License

Kotlin 99.81% Shell 0.19%
blockchain fcl flow flow-blockchain flow-sdk

fcl-android's Introduction

FCL

Maven Central Github Action

The Flow Client Library (FCL) is a package used to interact with user wallets and the Flow blockchain. When using FCL for authentication, DApps are able to support all FCL-compatible wallets on Flow and their users without any custom integrations or changes needed to the DApp code.

Requirements

  • Min SDK 21+

Download

In build.gradle of app module, include this dependency

implementation("com.portto.fcl:fcl:x.y.z")

Add maven("https://jitpack.io") or maven { url 'https://jitpack.io' } to your project's build.gradle if you haven't. This is required by flow-jvm-sdk.

Initialization

Add FCL content provider to AndroidManifest.xml.

<application>
    <provider android:authorities="com.portto.fcl.context"
        android:name="com.portto.fcl.lifecycle.FCLContentProvider" android:exported="false" />
</application>

To initialize FCL, use the following function:

Fcl.init(
    env = Network.TESTNET,
    supportedWallets = getWalletList(isMainnet),
    appDetail = AppDetail(),
)
  • env: The network on which the app was built. i.e. Network.TESTNET or Network.MAINNET
  • supportedWallets: A list of wallet providers supported by the app. See Wallet Discovery for more info.
  • appDetail(Optional): The information of the app.

Wallet Discovery

Knowing all the wallets available to users on a blockchain can be challenging. FCL's Discovery mechanism relieves much of the burden of integrating with Flow compatible wallets and lets developers focus on building their DApp and providing as many options as possible to their users.

When you initialize FCL, it's required to provide a list of wallet providers. The following is an example of adding Blocto and Dapper as wallet providers.

listOf(Blocto.getInstance(BLOCTO_TESTNET_APP_ID), Dapper)

Current Wallet Providers

Once you've initialized FCL, you may use Discovery directly from Discovery UI or Config.

Discovery UI

The simplest way to integrate Wallet Discovery is to utilize Discovery UI.

// MainActivity.kt
showConnectWalletDialog(this) {
    // authentication
}

Config

If you want to create your own authentication UI, the wallet providers are exposed as a list which can be retrieved from Fcl.config.

// Create a custom UI by supplying wallet provider list as data
val providerList = Fcl.config.supportedWallets
// Once you get the user's selected provider, you need to add it to config
Fcl.config.put(Config.Option.SelectedWalletProvider(provider))

Authentication

In order to interact with Flow blockchain, the current user's information is required. The user's account address can be easily retrieved by calling Fcl.authenticate().

when (val result = Fcl.authenticate()) {
    is Result.Success -> {
        val address = result.value
    }
    is Result.Failure -> {
        // Handle error
    }
}

Proof of Authentication

A common desire that application developers have is to be able to prove that a user controls an on-chain account. Proving ownership of an on-chain account is a way to authenticate a user with an application backend. Fortunately, FCL provides a way to achieve this.

Authenticating a user using AccountProof

By supplying AccountProofData to authenticate, the signatures will be acquired and set to the current user in config .

when (val result = Fcl.authenticate(accountProofData)) {
    is Result.Success -> {
        val address = result.value
    }
    is Result.Failure -> {
        // Handle error
    }
}

Verify AccountProof

If you've acquired account proof signed data from authenticate, you may retrieve the signatures from config.

val accountProofData = Fcl.currentUser?.accountProofData

Then you can verify the signatures by calling Fcl.verifyAccountProof().

when (val result = Fcl.verifyAccountProof(FLOW_APP_IDENTIFIER, accountProofData)) {
    is Result.Success -> {
        val isValid = reuslt.value
    }
    is Result.Failure -> {
        // Handle error
    }
}

Learn more about account proof

Sign a message

Cryptographic signatures are a key part of the blockchain. They are used to prove ownership of an address without exposing its private key. While primarily used for signing transactions, cryptographic signatures can also be used to sign arbitrary messages.

To sign a message, simply call Fcl.signUserMessage.

when (val result = Fcl.signUserMessage(message)) {
    is Result.Success -> {
        val userSignatures = result.value
    }
    is Result.Failure -> {
        // Handle error
    }
}

Verify user signatures

when (val result = Fcl.verifyUserSignatures(userMessage, userSignatures)) {
    is Result.Success -> {
        val isValid = reuslt.value
    }
    is Result.Failure -> {
        // Handle error 
    }
}

Blockchain Interactions

  • Fcl.query(): Send arbitrary Cadence scripts to the chain and receive back decoded values
  • Fcl.mutate(): Send arbitrary transactions with your own signatures or via a user's wallet to perform state changes on chain

Learn more about on-chain interactions

Utilities

  • Get account details from any Flow address
// suspending function
val account: FlowAccount = AppUtils.getAccount(address)
  • Get the latest block
// suspending function
val latestBlock: FlowBlock = AppUtils.getLatestBlock()
  • Get transaction status
// suspending function
val result = Fcl.getTransactionStatus(transactionId)

Support

To report a specific problem or feature request, open a new issue on Github. For questions, suggestions, or anything else, feel free to join FCL-Android's Discussion.

License

See the LICENSE file for details.

fcl-android's People

Contributors

doge-is-dope avatar imjacklai avatar kihonyoo avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

fcl-android's Issues

Support local network for the app build

Is your feature request related to a problem? Please describe.

The SDK only support mainnet and testnet endpoint, in our case we want to build the app on local environment but there's no way to custom host and port to access blockchain emulator

ζˆͺεœ– 2022-10-28 δΈ‹εˆ1 56 05

Describe the solution you'd like

Expose a function for user to set the custom endpoint and port on local environment, so when we new the flow access api will looks below.

Flow.newAccessApi(
    host = "10.0.2.2",
    port = 3569
)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    πŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. πŸ“ŠπŸ“ˆπŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❀️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.