With our shift from Proof-of-Work (PoW) to Proof-of-Stake (PoS), the Nimiq Web Client has undergone significant updates. This comparison page is meticulously crafted to guide you through the enhancements implemented in the new version. We'll highlight critical changes in configuration, client instantiation, wallet creation, transaction handling, and more.
The Wallet class has been removed and thus the following functions, too. Here is how you can now create and manage a KeyPair:
The Wallet class is no longer available. Now you generate a wallet by generating a key pair and use the address.
const wallet = Nimiq.Wallet.generate();
const address = wallet.address;
const keyPair = Nimiq.KeyPair.generate();
const address = keyPair.toAddress();
Previously, loading a wallet from a plain private key involved using the Wallet.loadPlain()
function. Now, you derive the key pair from the private key.
const wallet = Nimiq.Wallet.loadPlain(privateKeyHex);
const address = wallet.address;
const privateKey = Nimiq.PrivateKey.fromHex(privateKeyHex);
const keyPair = Nimiq.KeyPair.derive(privateKey);
const address = keyPair.toAddress();
Previously, you could use the Wallet.loadEncrypted()
function to derive a wallet from an encrypted private key. This function is no longer available.
const wallet = await Wallet.loadEncrypted(encryptedHex, password);
// No equivalent yet
To export the key pair or private key as a plain Uint8Array
, you need to serialize the key pair or private key.
const keypairBytes = wallet.exportPlain(); // [privatekey, publickey]
const privatekeyBytes = wallet.keyPair.privateKey.serialize();
const keypairBytes = keyPair.serialize(); // [publickey, privatekey] - to be reversed in 0.22.0
const privatekeyBytes = keyPair.privateKey.serialize();
Previously, you could export the private key as an encrypted Uint8Array with the Wallet.exportEncrypted()
function. This function is no longer available.
const encryptedKey = await wallet.exportEncrypted(password);
// No equivalent yet
In order to send a transaction, you need to create a transaction builder and sign it with the key pair.
const transaction = wallet.createTransaction(
Nimiq.Address.fromString(recipient),
Nimiq.Policy.coinsToLunas(amount), // Convert from NIM to luna
0, // Fee, optional
await client.getHeadHeight() // Current blockchain height
);
const txDetails = await client.sendTransaction(transaction);
const transaction = Nimiq.TransactionBuilder.newBasic(
keyPair.toAddress(),
Nimiq.Address.fromString(recipient),
BigInt(amount * 1e5), // Convert from NIM to luna
BigInt(0), // Fee, optional
await client.getHeadHeight(),
await client.getNetworkId(),
);
transaction.sign(keyPair);
const txDetails = await client.sendTransaction(transaction);
Convert the public and private keys to their hexadecimal string representations.
const publickeyHex = wallet.publicKey.toHex(); // or wallet.keyPair.publicKey.toHex()
const privatekeyHex = wallet.keyPair.privateKey.toHex();
const publickeyHex = keyPair.publicKey.toHex();
const privatekeyHex = keyPair.privateKey.toHex();
The Client class is a central component of the Nimiq's new Web Client framework and is essential for interacting with the Nimiq blockchain network under the PoS consensus model.
import Nimiq from '@nimiq/core-web';
await Nimiq.init();
Nimiq.GenesisConfig.test(); // Select testnet
const configBuilder = Nimiq.Client.Configuration.builder();
const client = configBuilder.instantiateClient();
When using the /web package export, you need to manually call the init function:
// When using the /web package export
import init, * as Nimiq from '@nimiq/core-web/web';
await init();
Then for all package exports, this is how you start a client:
// When using the /web package export
const config = new Nimiq.ClientConfiguration();
config.network('TestAlbatross'); // Select testnet
const client = await Nimiq.Client.create(config.build());
public resetConsensus()
public getBlockTemplate(minerAddress: Address | string, extraData?: Uint8Array | string)
public submitBlock(block: Block)
public getTransactionReceipt(hash: Hash | string)
public getTransactionReceiptsByHashes(hashes: Array<Hash | string>)
public addBlockListener(listener: BlockListener)
In addition to the existing account types basic (0)
, vesting (1)
, and htlc (2)
, Nimiq now has a fourth account type staking (3)
. Only the staking contract can be type staking
, no other account can have this type.
The new library doesn’t yet support the withSpaces
parameter. User-friendly addresses are always returned with spaces.
public toUserFriendlyAddress(withSpaces?: boolean): string;
toUserFriendlyAddress(): string;
Nimiq.Policy.lunasToCoins(account.balance);
You now have to manually divide any amount in luna by 10.000 to get NIM:
function lunasToCoins(lunas: number): number {
return lunas / 1e5;
}
Timestamps of transactions and blocks are now in milliseconds, where they were in seconds (UNIX) before. That means you no longer have to multiply them by 1000 to use with new Date()
in JavaScript:
const txDate = new Date(transaction.timestamp * 1000);
const txDate = new Date(transaction.timestamp);
New capability: Can now handle UintArray
MINED
is replace by INCLUDED