Wallet API
Preparations and create a wallet

Preparations and create a wallet#

This chapter will introduce how to quickly use the wallet API. In just three steps, you can use Javascript to quickly build a program instance that uses the OKX WaaS service to create a wallet.

Step 1: Preparations#

Before you can start using the wallet API, you need to create the project and generate the API key in the developer management platform. Here are the detailed steps and related resources:

  1. Log in to the developer management platform.
  2. Create a new project.
  3. Generate API keys in project Settings

We have prepared a detailed guide for you to help you through these steps. You can read the Developer Management Platform Guide (./introduction-to-developer-portal-interface) to get started faster.

Next,you shall do some configuration as following:

REST request authentication configuration#

When you send a REST request, you need to verify it. We have provided a detailed REST Request authentication Guide to help you understand and implement this process.

Node.js environment Settings#

Import the necessary Node.js libraries and set your environment variables as well as define helper functions and assembly parameters Node.js environment Settings

Step 2: Create the wallet private key and address#

Use the WaaS Wallet SDK to create wallet mnemonics and addresses. If you have already generated your own wallet private key and address, you can skip to the next step.

A quick start uses the Javascript sdk js-wallet-sdk as an example, and SDKS for other languages can refer to OKX WaaS Wallet SDK Details.

npm install#

To use the SDK, you first need to install it, and you can use the npm installation to get the latest version.

Our SDK supports two types of packages: Public packages and single currency modules.

Public package, for all currencies

npm install @okxweb3/crypto-lib
npm install @okxweb3/coin-base

Integration of a single currency,using ETH as an example:

npm install @okxweb3/coin-ethereum

To see how other linked javascript SDKS are built, go here. This chapter uses ETH to perform subsequent operations.

Local build#

  1. Download the project source code
git clone https://github.com/okx/js-wallet-sdk.git
  1. Run the build script
sh build.sh

Taking the EVM network as an example, you can use the following code to create an EVM wallet object using the SDK and derive the corresponding address.

import { bip39, BigNumber } from "@okxweb3/crypto-lib";
import { EthWallet } from "@okxweb3/coin-ethereum";

// eth wallet
let wallet = new EthWallet();

// get mnemonic
let mnemonic = await bip39.generateMnemonic();
console.log("generate mnemonic:", mnemonic);

// get derived key
const hdPath = await wallet.getDerivedPath({ index: 0 });
let derivePrivateKey = await wallet.getDerivedPrivateKey({ mnemonic: mnemonic, hdPath: hdPath });
console.log("generate derived private key:", derivePrivateKey, ",derived path: ", hdPath);

// get new address
let newAddress = await wallet.getNewAddress({ privateKey: derivePrivateKey });
console.log("generate new address:", newAddress.address);

Demo program#

We have prepared an Open source demo that demonstrates our SDK features above. To get the demo source code click here.

Step 3: Create a wallet subscribition#

Generate walletId#

The UUID v4 algorithm is used to generate WalletId.

const { v4: uuidv4 } = require('uuid');

// Generate a version 4 (random) UUID
const walletId = uuidv4();
console.log(walletId);

Call create wallet interface#

Through the access to the walletId, chainId, address information, call POST /api/v5/waas/wallet/create-wallet interface to sync wallet related data.

//Define your parameters
const addresses = [
    {
      "chainId": "1",
      "address": "0xdf54b6c6195ea4d948d03bfd818d365cf175cfc2"
    },
    {
      "chainId": "56",
      "address": "0xdf54b6c6195ea4d948d03bfd818d365cf175cfc2"
    },
    {
      "chainId": "66",
      "address": "0xdf54b6c6195ea4d948d03bfd818d365cf175cfc2"
    }
  ];
const getCreateWalletBody = {
    addresses: addresses,
    walletId: '13886e05-1265-4b79-8ac3-b7ab46211001',
  };

//Define helper functions
const getCreateWalletData = async () => {
    const apiRequestUrl = getRequestUrl(
      apiBaseUrl,
      '/api/v5/waas/wallet/create-wallet'
    );
    return fetch(apiRequestUrl, {
      method: 'post',
      headers: headersParams,
      body: JSON.stringify(getCreateWalletBody),
    })
      .then((res) => res.json())
      .then((res) => {
        return res;
      });
  };

const { data: createWalletData } = await getCreateWalletData();

When the call succeeds, you will receive the following response:

{
    "code": 0,
    "data": [{
            "walletId": "13886e05-1265-4b79-8ac3-b7ab46217655"
    }],
    "msg": "success"
}

Next, if you want to develop multi-chain applications compatible with EVM and other account models for blockchain, you can proceed to Building Multi-Chain Applications. Or if you are interested in the Bitcoin ecosystem as a developer, such as building applications based on scripting, you can proceed to Building Bitcoin Applications.

If you want a more comprehensive understanding of the capabilities provided by the wallet API, you can proceed to view the API Reference and SDK Reference.