QiHDWallet
The Qi HD wallet is a BIP44-compliant hierarchical deterministic wallet used for managing a set of addresses in the Qi ledger. This is wallet implementation is the primary way to interact with the Qi UTXO ledger on the Quai network.
The Qi HD wallet supports:
- Adding accounts to the wallet heierchy
- Generating addresses for a specific account in any Zone
- Signing and sending transactions for any address in the wallet
- Serializing the wallet to JSON and deserializing it back to a wallet instance.
Example
import { QiHDWallet, Zone } from 'quais';
const wallet = new QiHDWallet();
const cyrpus1Address = await wallet.getNextAddress(0, Zone.Cyrpus1); // get the first address in the Cyrpus1 zone
await wallet.sendTransaction({ txInputs: [...], txOutputs: [...] }); // send a transaction
const serializedWallet = wallet.serialize(); // serialize current (account/address) state of the wallet
.
.
.
const deserializedWallet = QiHDWallet.deserialize(serializedWallet); // create a new wallet instance from the serialized data
Extends
AbstractHDWallet
Accessors
xPub
get xPub(): string
Returns the extended public key of the root node of the HD wallet.
Returns
string
The extended public key.
Source
Methods
_scan()
private _scan(zone, account?): Promise<void>
Internal method to scan the specified zone for addresses with unspent outputs. This method handles the actual scanning logic, generating new addresses until the gap limit is reached for both gap and change addresses.
Parameters
Parameter | Type | Default value | Description |
---|---|---|---|
zone | Zone | undefined | The zone in which to scan for addresses. |
account ? | number | 0 | The index of the account to scan. Default is 0 |
Returns
Promise
<void
>
A promise that resolves when the scan is complete.
Throws
If the provider is not set.
Source
addAddress()
addAddress(
account,
addressIndex,
isChange?): NeuteredAddressInfo
Adds an address to the wallet.
Parameters
Parameter | Type | Default value | Description |
---|---|---|---|
account | number | undefined | The account number. |
addressIndex | number | undefined | The address index. |
isChange ? | boolean | false | Whether the address is a change address. Default is false |
Returns
NeuteredAddressInfo
The added address info.
Inherited from
AbstractHDWallet.addAddress
Source
connect()
connect(provider): void
Connects the wallet to a provider.
Parameters
Parameter | Type | Description |
---|---|---|
provider | Provider | The provider. |
Returns
void
Inherited from
AbstractHDWallet.connect
Source
getAddressInfo()
getAddressInfo(address): null | NeuteredAddressInfo
Gets the address info for a given address.
Parameters
Parameter | Type | Description |
---|---|---|
address | string | The address. |
Returns
null
| NeuteredAddressInfo
The address info or null if not found.
Inherited from
AbstractHDWallet.getAddressInfo
Source
getAddressesForAccount()
getAddressesForAccount(account): NeuteredAddressInfo[]
Gets the addresses for a given account.
Parameters
Parameter | Type | Description |
---|---|---|
account | number | The account number. |
Returns
NeuteredAddressInfo
[]
The addresses for the account.
Inherited from
AbstractHDWallet.getAddressesForAccount
Source
getAddressesForZone()
getAddressesForZone(zone): NeuteredAddressInfo[]
Gets the addresses for a given zone.
Parameters
Parameter | Type | Description |
---|---|---|
zone | Zone | The zone. |
Returns
NeuteredAddressInfo
[]
The addresses for the zone.
Inherited from
AbstractHDWallet.getAddressesForZone
Source
getChangeAddressesForZone()
getChangeAddressesForZone(zone): NeuteredAddressInfo[]
Gets the change addresses for the specified zone.
Parameters
Parameter | Type | Description |
---|---|---|
zone | Zone | The zone. |
Returns
NeuteredAddressInfo
[]
The change addresses for the zone.
Source
getGapAddressesForZone()
getGapAddressesForZone(zone): NeuteredAddressInfo[]
Gets the gap addresses for the specified zone.
Parameters
Parameter | Type | Description |
---|---|---|
zone | Zone | The zone. |
Returns
NeuteredAddressInfo
[]
The gap addresses for the zone.
Source
getGapChangeAddressesForZone()
getGapChangeAddressesForZone(zone): NeuteredAddressInfo[]
Gets the gap change addresses for the specified zone.
Parameters
Parameter | Type | Description |
---|---|---|
zone | Zone | The zone. |
Returns
NeuteredAddressInfo
[]
The gap change addresses for the zone.
Source
getNextAddress()
getNextAddress(account, zone): Promise<NeuteredAddressInfo>
Promise that resolves to the next address for the specified account and zone.
Parameters
Parameter | Type | Description |
---|---|---|
account | number | The index of the account for which to retrieve the next address. |
zone | Zone | The zone in which to retrieve the next address. |
Returns
Promise
<NeuteredAddressInfo
>
The next neutered address information.
Inherited from
AbstractHDWallet.getNextAddress
Source
getNextAddressSync()
getNextAddressSync(account, zone): NeuteredAddressInfo
Synchronously retrieves the next address for the specified account and zone.
Parameters
Parameter | Type | Description |
---|---|---|
account | number | The index of the account for which to retrieve the next address. |
zone | Zone | The zone in which to retrieve the next address. |
Returns
NeuteredAddressInfo
The next neutered address information.
Inherited from
AbstractHDWallet.getNextAddressSync
Source
getNextChangeAddress()
getNextChangeAddress(account, zone): Promise<NeuteredAddressInfo>
Promise that resolves to the next change address for the specified account and zone.
Parameters
Parameter | Type | Description |
---|---|---|
account | number | The index of the account for which to retrieve the next change address. |
zone | Zone | The zone in which to retrieve the next change address. |
Returns
Promise
<NeuteredAddressInfo
>
The next change neutered address information.
Source
getNextChangeAddressSync()
getNextChangeAddressSync(account, zone): NeuteredAddressInfo
Synchronously retrieves the next change address for the specified account and zone.
Parameters
Parameter | Type | Description |
---|---|---|
account | number | The index of the account for which to retrieve the next change address. |
zone | Zone | The zone in which to retrieve the next change address. |
Returns
NeuteredAddressInfo
The next change neutered address information.
Source
getOutpoints()
getOutpoints(zone): OutpointInfo[]
Gets the outpoints for the specified zone.
Parameters
Parameter | Type | Description |
---|---|---|
zone | Zone | The zone. |
Returns
OutpointInfo
[]
The outpoints for the zone.
Source
getPrivateKey()
getPrivateKey(address): string
Returns the private key for a given address. This method should be used with caution as it exposes the private key to the user.
Parameters
Parameter | Type | Description |
---|---|---|
address | string | The address associated with the desired private key. |
Returns
string
The private key.
Inherited from
AbstractHDWallet.getPrivateKey
Source
importOutpoints()
importOutpoints(outpoints): void
Imports an array of outpoints.
Parameters
Parameter | Type | Description |
---|---|---|
outpoints | OutpointInfo [] | The outpoints to import. |
Returns
void
Source
scan()
scan(zone, account?): Promise<void>
Scans the specified zone for addresses with unspent outputs. Starting at index 0, it will generate new addresses until the gap limit is reached for both gap and change addresses.
Parameters
Parameter | Type | Default value | Description |
---|---|---|---|
zone | Zone | undefined | The zone in which to scan for addresses. |
account ? | number | 0 | The index of the account to scan. Default is 0 |
Returns
Promise
<void
>
A promise that resolves when the scan is complete.
Throws
If the zone is invalid.
Source
scanAddress()
private scanAddress(
zone,
account,
isChange,
addressesCount): Promise<number>
Scans for the next address in the specified zone and account, checking for associated outpoints, and updates the address count and gap addresses accordingly.
Parameters
Parameter | Type | Description |
---|---|---|
zone | Zone | The zone in which the address is being scanned. |
account | number | The index of the account for which the address is being scanned. |
isChange | boolean | A flag indicating whether the address is a change address. |
addressesCount | number | The current count of addresses scanned. |
Returns
Promise
<number
>
A promise that resolves to the updated address count.
Throws
If an error occurs during the address scanning or outpoints retrieval process.
Source
sendTransaction()
sendTransaction(tx): Promise<TransactionResponse>
Sends a Qi transaction.
Parameters
Parameter | Type | Description |
---|---|---|
tx | QiTransactionRequest | The transaction to send. |
Returns
Promise
<TransactionResponse
>
The transaction response.
Overrides
AbstractHDWallet.sendTransaction
Throws
If the provider is not set or if the transaction has no inputs.
Source
serialize()
serialize(): SerializedQiHDWallet
Serializes the HD wallet state into a format suitable for storage or transmission.
Returns
SerializedQiHDWallet
An object representing the serialized state of the HD wallet, including outpoints, change addresses, gap addresses, and other inherited properties.
Overrides
AbstractHDWallet.serialize
Source
signMessage()
signMessage(address, message): Promise<string>
Signs a message using the private key associated with the given address.
Parameters
Parameter | Type | Description |
---|---|---|
address | string | The address for which the message is to be signed. |
message | string | Uint8Array | The message to be signed, either as a string or Uint8Array. |
Returns
Promise
<string
>
A promise that resolves to the signature of the message in hexadecimal string format.
Overrides
AbstractHDWallet.signMessage
Throws
If the address does not correspond to a valid HD node or if signing fails.
Source
signTransaction()
signTransaction(tx): Promise<string>
Signs a Qi transaction and returns the serialized transaction.
Parameters
Parameter | Type | Description |
---|---|---|
tx | QiTransactionRequest | The transaction to sign. |
Returns
Promise
<string
>
The serialized transaction.
Overrides
AbstractHDWallet.signTransaction
Throws
If the UTXO transaction is invalid.
Source
sync()
sync(zone, account?): Promise<void>
Scans the specified zone for addresses with unspent outputs. Starting at the last address index, it will generate new addresses until the gap limit is reached for both gap and change addresses. If no account is specified, it will scan all accounts known to the wallet.
Parameters
Parameter | Type | Description |
---|---|---|
zone | Zone | The zone in which to sync addresses. |
account ? | number | The index of the account to sync. If not specified, all accounts will be scanned. |
Returns
Promise
<void
>
A promise that resolves when the sync is complete.
Throws
If the zone is invalid.
Source
createRandom()
static createRandom<T>(
this,
password?,
wordlist?): T
Creates a random HD wallet.
Type parameters
Type parameter |
---|
T extends AbstractHDWallet <T > |
Parameters
Parameter | Type | Description |
---|---|---|
this | Object | The constructor of the HD wallet. |
password ? | string | The password. |
wordlist ? | Wordlist | The wordlist. |
Returns
T
The created instance.
Inherited from
AbstractHDWallet.createRandom
Source
deserialize()
static deserialize(serialized): Promise<QiHDWallet>
Deserializes a serialized QiHDWallet object and reconstructs the wallet instance.
Parameters
Parameter | Type | Description |
---|---|---|
serialized | SerializedQiHDWallet | The serialized object representing the state of a QiHDWallet. |
Returns
Promise
<QiHDWallet
>
A promise that resolves to a reconstructed QiHDWallet instance.
Overrides
AbstractHDWallet.deserialize
Throws
If the serialized data is invalid or if any addresses in the gap addresses or gap change addresses do not exist in the wallet.
Source
fromMnemonic()
static fromMnemonic<T>(this, mnemonic): T
Creates an HD wallet from a mnemonic.
Type parameters
Type parameter |
---|
T extends AbstractHDWallet <T > |
Parameters
Parameter | Type | Description |
---|---|---|
this | Object | The constructor of the HD wallet. |
mnemonic | Mnemonic | The mnemonic. |
Returns
T
The created instance.
Inherited from
AbstractHDWallet.fromMnemonic
Source
fromPhrase()
static fromPhrase<T>(
this,
phrase,
password?,
wordlist?): T
Creates an HD wallet from a phrase.
Type parameters
Type parameter |
---|
T extends AbstractHDWallet <T > |
Parameters
Parameter | Type | Description |
---|---|---|
this | Object | The constructor of the HD wallet. |
phrase | string | The phrase. |
password ? | string | The password. |
wordlist ? | Wordlist | The wordlist. |
Returns
T
The created instance.
Inherited from
AbstractHDWallet.fromPhrase
Source
Was this page helpful?