Manage Witnet wallets
Create HD-wallets and check Witnet account balances using the Witnet SDK.
The code examples below assume that the environment is properly set up as described in Getting Started. 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 }),
];
// ...
}
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.
// validator's pkh address derived from a master key
const validator = 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 })
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)
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
Getting total $WIT treasury of a wallet
const { Witnet } = require("@witnet/sdk")
// ...
let treasury = Witnet.Coins.fromBalance(await wallet.getBalance())
Last updated
Was this helpful?