OKX Web3 JS Wallet SDK

repository·main·Indexed 18 days ago

https://github.com/okx/js-wallet-sdk

A TypeScript/JavaScript solution for building multi-chain wallet applications. The SDK enables offline transaction signing, account management, and support for various blockchain protocols. It includes specialized packages such as @okxweb3/coin-base for core cryptographic functions and MPC/Hardware wallet workflows, @okxweb3/coin-bitcoin for Bitcoin-based standards including BRC-20, Doginals, and Runes, and @okxweb3/coin-aptos for Aptos blockchain operations including smart contract interactions and NFT operations.

Tokens
144.3K
Snippets
486
Records
600
Agent score
63%

What's inside okx-js-wallet-sdk

  1. Use @okxweb3/coin-aptos for Aptos blockchain operations

    main
    The @okxweb3/coin-aptos package provides specialized tools for interacting with the Aptos blockchain. It allows developers to perform key operations such as generating private keys and addresses, verifying addresses, and executing transfers (both native APTOS and token-based) as well as interacting with DEX contracts.
  2. Advanced Aptos transaction types

    main

    The SDK supports specialized transaction patterns:

    Fee Payer Transactions

    Enable withFeePayer: true in the base object to allow a third party to pay for gas.

    Script Payload Transactions

    Use type: "dapp" with a specific type: 3 in the data object to sign transactions containing bytecode and functionArguments.

    Multi-Agent Transactions

    For transactions requiring multiple signers, include secondarySignerAddresses (an array of addresses) in the base object.

    // Multi-agent transaction example
    let res = await wallet.signTransaction({
        privateKey: "",
        data: {
            "type": "dapp",
            "base": {
                "expirationTimestampSecs": 1817258177,
                "sequenceNumber": "42",
                "chainId": 2,
                "gasUnitPrice": "100",
                "maxGasAmount": "8000",
                "reserveFeeRatio": "1.1",
                "sender": "0x986d10e6ffde60518ab0664f70339196f914f96255f31a1c6b226670c73d3f92",
                "secondarySignerAddresses": ["0x79b2d1f85c5d644b2f1c24e435ea80d72ae8c800a4ed5e0b01a7f40445c37b2d", "0x318717be403fefbed1e1cd2567a215514c524d0f315b743d664abb137d9ed8ae"]
            },
            "data": {
                data:{
                    bytecode: multiSignerScriptBytecode,
                    functionArguments: funcParam,
                },
                type:"3",
            }
        },
    });
  3. How the OKX Web3 Wallet SDK architecture works

    main

    The SDK is designed with an extensible three-tier architecture:

    1. crypto-lib: The foundation layer that manages general security and signature algorithms.
    2. coin-base: The abstraction layer that defines a common interface used by all supported coins to ensure consistency.
    3. coin-specific packages: The implementation layer where the unique transaction creation and signing logic for each specific blockchain (e.g., Ethereum, Bitcoin, Solana) resides.
  4. Use the WaxWallet class for EOS and WAX

    main

    The WaxWallet class is the primary interface for interacting with the EOS and WAX blockchains. It provides methods for key management, address derivation, and transaction signing.

    import { WaxWallet } from "@okxweb3/coin-eos";
    
    let wallet = new WaxWallet();
  5. Sign Transactions with Hardware Wallets

    main

    For hardware wallet integration, the process is split into two steps: building a raw transaction and then building the final signed transaction using the signature components (r, s, v) provided by the device.

    1. Build Raw Transaction: Use getHardWareRawTransaction(signParams) to get the unsigned transaction data.
    2. Build Signed Transaction: Use getHardWareSignedTransaction(hardwareRawTransactionParam) passing the raw transaction and the signature components.
    import { EthWallet } from "@okxweb3/coin-ethereum"
    
    let wallet = new EthWallet();
    
    // 1. Build raw transaction
    let signParams = {
        data: {
            to: "0xee7c7f76795cd0cab3885fee6f2c50def89f48a3",
            value: 1,
            nonce: 5,
            gasPrice: "100000000000",
            gasLimit: 21000,
            chainId: 42,
        }
    };
    const rawTx = await wallet.getHardWareRawTransaction(signParams);
    
    // 2. Build signed transaction
    const hardwareRawTransactionParam = {
        raw: rawTx,
        r: r, // provided by hardware wallet
        s: s, // provided by hardware wallet
        v: v, // provided by hardware wallet
    }
    const signedTx = await wallet.getHardWareSignedTransaction(hardwareRawTransactionParam);