Wallet API

Integrate SDK#

This is a case of how to call related methods after completing the npm installation of @okxweb3/coin-bitcoin (client-side assembly).

1. npm install#

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

2. Import the package, create an BTC wallet object, and initialize an address#

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

// btc wallet
let wallet = new BtcWallet();

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

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

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

3. Verify the legitimacy of the address and construct a transaction#

// Verify address
let valid = await wallet.validAddress({
    address: newAddress.address
});
console.log("verify address isValid:", valid.isValid);

// Sign a transaction
let signParams = {
    privateKey: derivePrivateKey,
    data: {
        to: newAddress.address,
        value: new BigNumber(0),
        nonce: 5,
        gasPrice: new BigNumber(100 * 1000000000),
        gasLimit: new BigNumber(21000),
        chainId: 42
    }
};

let signedTx = await wallet.signTransaction(signParams);
console.log("signed tx:", signedTx);

For more detailed instructions for using the coin-bitcoin SDK, you can visit our documentation on GitHub: Instructions for using the coin-bitcoin SDK. This document will provide you with detailed steps and examples to help you better understand and use our SDK.