DApp connects to Nabox Wallet (BCH/FCH)

Nabox Chrome Extension

Install the App

Acquire Nabox Extension in Google store

Download

Acquire source code

Acquire the latest source code from GitHub: https://github.com/naboxwallet

Installation dependency

npm install

Runing locally

npm run serve

Packaging locally

npm run build

Nabox Extension add the local package BCH/FCH Provider Integration Protocol

Key Variables Description

The Provider Object is used by the DApp business side and offers two ways to interact:

  • window.naboxBch | window.naboxFch

  • window.NaboxWallet.bch | window.NaboxWallet.fch

Methods Required via postMessage

  1. getPub

    • Parameters: None

    • Returns: The public key of the current user's address

  2. getBalance

    • Parameters: None

    • Returns: The balance of the current user's address

  3. signMessage

    • Parameters: A string that needs to be signed

    • Returns: The signed string

  4. sendTransaction

    • Parameters: A JSON object

      • to: Transfer address

      • amount: Transfer amount

      • msg: Attached remark

    • Returns: Transaction hash (txHash)

Wallet Address Injection into the DApp Page

After the DApp page loads, the current wallet address is passed to the page object using the following method:

window.naboxFch.setConfig({selectedAddress: addr});

Download fch-provider-min.js


Getting Started

  1. Select the BCH or FCH network in Nabox Wallet.

  2. Open the DApp page in the browser.

    • Nabox Wallet will inject a naboxBch or naboxFch object into the DApp page's window object.

    • The DApp can communicate with Nabox Wallet through this object.

    Example:

    window.naboxFch.getAddress(); // Get the current user's address

API Introduction

Connect Wallet

  • Method: createSession

  • Parameters: None

  • Returns: {”selectedAddress”: address}

Note: In the app, this method doesn't need to be called. The wallet is connected by default. If there is no address, call this method:

if (!window.naboxFch.getAddress()) {
    await window.naboxFch.createSession();
}

Get Current User's Account Address

  • Method: getAddress

  • Parameters: None

  • Returns: string

Example:

window.naboxFch.getAddress();
// Returns: F6xvjtdWstcBqBZyt9rEFTimfazFhJcvxC

Get Current Address Public Key

  • Method: getPub

  • Parameters: None

  • Returns: string

Example:

window.naboxFch.getPub();
// Returns: 02e62cfe906452fb94f981a5d13fe7086fa33088bd5f41c9f63d15071fadb33fc0

Get Current User's Account Balance

  • Method: getBalance

  • Parameters: None

  • Returns: number

Example:

window.naboxFch.getBalance();
// Returns: 10009999202 (smallest unit, equals 1000.9999202 coins)

Send Transaction

  • Method: sendTransaction

  • Parameters: JSON object

    • to: Recipient address

    • amount: Transfer amount

    • msg: Additional transaction info (remark)

  • Returns: Transaction hash (string)

Example:

window.naboxFch.sendTransaction({
    "to": "F6xvjtdWstcBqBZyt9rEFTimfazFhJcvxC",
    "amount": 0.1,
    "msg": "this is remark"
});
// Returns: df3269102690620010d754c18d03a7ad59cd8cee7a4d86da6b4aa46c19398f66

Sign a String

  • Method: signMessage

  • Parameters: A string to be signed

  • Returns: Signed string (hex format)

Example:

window.naboxFch.signMessage("123456");
// Returns: 20cdf84efc80c8b4c28c7e845e5906ce067a0f93dd3a74a3b3bc5589118ff9281e56e0bcc0142f4c0ead720199785e7cd45cf5174521db68c3ce2542b8c5e9580c

Demo

Download

To run the demo, you need to install the Node.js environment. After extracting, run:

  • npm install

  • npm run start

Steps:

cd fch-provider-demo
npm install
npm run start

Access: http://localhost:8080

In the top input box, import a test private key to begin testing the following use cases.

Note: The demo simulates a Nabox Wallet environment. In actual business environments, you don't need to reference fch-min.js, fch-provider-min.js, or app-mock.js in the HTML file.

Last updated