LogoLogo
WebsiteDiscordTelegramTwitter
  • 🏠Witnet Oracle Docs
  • Introduction
    • ❓What is Witnet?
      • ⚙️Oracle Architecture
      • 🪙The Witnet Coin (Wit)
      • 👛Wallets
      • 🤔Frequently Asked Questions
      • 😎Awesome Witnet
    • 🚀Quick Tutorials
      • 📈Price Feeds
      • 🎲Randomness
      • 🌐APIs and HTTP GET/POST
      • ⛏️Mining Wit
  • Smart contracts developers
    • ⛓️Supported chains
    • 🔮Wit/Oracle
      • HTTP GET Requests in Solidity
      • HTTP POST Requests in Solidity
      • Query GraphQL APIs in Solidity
      • Dynamic Requests in Solidity
      • UsingWitnet Inheritance
      • API Reference
        • Solidity API
          • Solidity Contracts
            • WitnetRequestBoard
        • Javascript API
        • RADON API
          • RADON Type System
          • RADON Errors
      • Multi-chain Addresses
    • 💹Wit/Price Feeds
      • How To Use Witnet Price Feeds
      • Update Conditions
      • Code Examples
      • API Reference
      • Multi-chain Addresses
        • Arbitrum Price Feeds
        • Avalanche Price Feeds
        • Base Price Feeds
        • Boba Price Feeds
        • Celo Price Feeds
        • Conflux Price Feeds
        • Cronos Price Feeds
        • Dogechain Price Feeds
        • Elastos Price Feeds
        • Ethereum Price Feeds
        • Gnosis Chain Price Feeds
        • Kaia Price Feeds
        • Kava Price Feeds
        • KCC Price Feeds
        • Mantle Price Feeds
        • Meter Price Feeds
        • Metis Price Feeds
        • Moonbeam Price Feeds
        • OKX Price Feeds
        • Optimism Price Feeds
        • Polygon Price Feeds
        • Reef Price Feeds
        • Scroll Price Feeds
        • Syscoin Price Feeds
        • Ultron Price Feeds
        • Request new price feed or chain support
    • 🎲Wit/Randomness
      • Generating Randomness
      • WitnetRandomness Contract
      • Low-level Requests
      • Code Examples
      • API Reference
      • Multi-chain Addresses
    • 🏗️Guides
      • 📖Solidity Contracts
        • Appliances
          • 📃WitnetPriceFeeds
          • 📃WitnetRandomness
        • Core
          • 📃WitnetOracle
          • 📃WitnetRadonRegistry
          • 📃WitnetRequest
          • 📃WitnetRequestFactory
          • 📃WitnetRequestTemplate
        • Mockups
          • 📃UsingWitnet
          • 📃UsingWitnetRandomness
          • 📃UsingWitnetRequest
          • 📃UsingWitnetRequestTemplate
          • 📃WitnetRandomnessRequestConsumer
          • 📃WitnetRequestConsumer
          • 📃WitnetRequestTemplateConsumer
      • 🧙Solidity Wizard
    • 🎓Tutorials
      • Building a Satoshi/Wei custom price feed
  • Witnet SDK
    • 👋Introduction
    • 🚀Getting Started
    • 🏗️How-to Guides
      • Connect to a Wit/RPC provider
      • Query stake entries in Witnet
      • Manage Witnet wallets
      • Manage Witnet UTXOs
      • Manage Witnet transactions
    • 🎓Tutorials
    • 📚Reference Material
      • 🔌APIs
        • Wit/Node JSON-RPC API
  • Witnet Validators
    • 💻Node Requirements
    • 🚀Getting Started (Docker)
    • 🔎Next steps
    • 🤓Advanced Setups
      • Run node as a systemd service
      • Run node with docker compose
      • Paranoid mode (Witnet over proxies and Tor)
      • Configuration file
    • ⌨️CLI Reference
  • 🗜️Compile from Source Code
Powered by GitBook
On this page
  • Deriving HD-wallet accounts
  • Deriving the coinbase address
  • Exploring wallet accounts
  • Searching for a specific address
  • Getting total stake delegated from some wallet account
  • Getting total $WIT treasury of a wallet

Was this helpful?

Edit on GitHub
  1. Witnet SDK
  2. How-to Guides

Manage Witnet wallets

Create HD-wallets and check Witnet account balances using the Witnet SDK.

PreviousQuery stake entries in WitnetNextManage Witnet UTXOs

Last updated 28 days ago

Was this helpful?

The code examples below assume that the environment is properly set up as described in . You can easily adapt the examples to pass the URL of the Wit/RPC provider to use, and/or the wallet master key (and the password, if encrypted).

Deriving HD-wallet accounts

const { utils, Witnet } = require('@witnet/sdk')

main()

async function main () {
    // create wallet by reading decrypted XPRV string from environment
    // and connecting to the Wit/RPC provider set on environment as well.
    const wallets = [
        // create a wallet with 5 accounts, by passing decrypted XPRV string
        await Witnet.Wallet.fromXprv("xprv1...", { limit: 5 }),
        // create a wallet with 5 accounts, by passing encrypted XPRV and password,
        await Witnet.Wallet.fromEncryptedXprv("xprv1...", "passwd...", { limit: 5 }),
        // create a wallet with 5 accounts, by using XPRV master key string set on environment
        await Witnet.Wallet.fromEnv({ limit: 5 }),
    ];
    // ...
}
import { utils, Witnet } from "@witnet/sdk"

// ...

// create wallet by reading decrypted XPRV string from environment
// and connecting to the Wit/RPC provider set on environment as well.
const wallets: Array<Witnet.Wallet> = [
    // create a wallet with 5 accounts, by passing decrypted XPRV string
    await Witnet.Wallet.fromXprv("xprv1...", { limit: 5 }),
    // create a wallet with 5 accounts, by passing encrypted XPRV and password,
    await Witnet.Wallet.fromEncryptedXprv("xprv1...", "passwd...", { limit: 5 }),
    // create a wallet with 5 accounts, by using XPRV master key string set on environment
    await Witnet.Wallet.fromEnv({ limit: 5 }),
];
$ npx witnet wallet accounts --limit 5

Deriving the coinbase address

The wallet's coinbase address corresponds to the address that a node using the wallet's master key would derive for signing block and super-block proposals, data witnessing commits and data reveal transactions.

Account addresses in Witnet are also referred as "public key hashes", or "pkh". Witnet addresses are encoded in Bech32. Mainnet addresses are prefixed with wit1, and Testnet addresses with twit1.

// validator's pkh address derived from a master key
const validator = wallet.coinbase.pkh
// validator's pkh address derived from a master key
const validator: string = wallet.coinbase.pkh

Exploring wallet accounts

When importing a wallet you may rather derive only the accounts holding some $WIT balance. In these cases, a "gap" number can be provided, standing for the number of consecutively derived accounts with no available funds required before stopping the search. The search can also be limited to a maximum number of entries.

// search derivation max gap
let gap = 5

// max number of wallets to explore
let limit = 5

// on existing wallet:
let accounts = await wallet.exploreAccounts(limit, gap)

// when creating a new wallet:
const wallet2 = await Witnet.Wallet.fromEnv({ limit, gap, onlyWithFunds: true })
search derivation max gap
let gap = 5

// max number of wallets to explore
let limit = 5

// on existing wallet:
let accounts: Array<Witnet.Account> = await wallet.exploreAccounts(limit, gap)

// or when creating a new wallet object:
const wallet2: Witnet.Wallet = await Witnet.Wallet.fromEnv({ 
    limit, gap, 
    onlyWithFunds: true 
})
$ npx witnet wallet accounts --gap 5

Searching for a specific address

You can also search for some specific address within a wallet, even if holding no current balance. A "gap" parameter can also be provided, as to set the number of consecutive accounts with no funds to derive before giving up the search.

const someAccount = wallet.getAccount("wit1...", gap)
const someAccount: Witnet.Account = wallet.getAccount("wit1...", gap)
$ npx witnet accounts wit1...

Getting total stake delegated from some wallet account

The balance of any Witnet address is divided in three different fields:

locked

Time-locked balance, in nanowits. Cannot be spent at this moment.

staked

Funds currently staked into one or more validators, in nanowits. Delegated stake can be eventually withdrawn. Once withdrawn, deposits will remain time-locked for at least two weeks.

unlocked

Currently available funds, in nanowits.

let balance = (await wallet.getAccount("wit1...").getBalance()).staked
let balance = (await wallet.getAccount("wit1...").getBalance()).staked
$ npx witnet accounts wit1... --verbose

Getting total $WIT treasury of a wallet

const { Witnet } = require("@witnet/sdk")
// ...
let treasury = Witnet.Coins.fromBalance(await wallet.getBalance())
import { Witnet } from "@witnet/sdk"
// ...
let treasury = Witnet.Coins.fromBalance(await wallet.getBalance())
$ npx witnet accounts --verbose
🏗️
Getting Started