Zen.js

Zen.JS is the library for interacting with the Zen Protocol node via JavaScript.

Zen.js is helps in:

  • Generate Mnemonic phrase

  • Secure Mnemonic phrase

  • Serialise and deserialise Consensus types

  • Sign transactions

  • Defining the contract message body

  • Create a wallet instance to interact with the blockchain

  • Encode and decode extendend keys to address

Install

npm config set @zen:registry https://www.myget.org/F/zenprotocol/npm/ 
npm install --save @zen/zenjs

Generating mnemonic phrase, keys and accepting payments

import {Mnemonic, ExtendedKey} from '@zen/zenjs'
const mnemonic = Mnemonic.generateMnemonic(24);
const extendedKey = ExtendedKey.fromMnemonic(mnemonic);
const derivedExtendedKey = extendedKey.derivePath("m/44'/258'/0'/0/0");
const privateKey = derivedExtendedKey.getPrivateKey();
const publicKey = derivedExtendedKey.getPublicKey();
const address = publicKey.toAddress('main');
console.log(address, publicKey.toAddress('main'));

Creating and signing transactions

To create a transaction using a TransactionBuilder, you need to:

  • Add an input: This involves providing the transaction hash (txHash) of a previous transaction that contains the unspent transaction output (UTXO) you want to use as an input, along with the index of the UTXO in that transaction. Additionally, you need to provide the private key associated with the UTXO's address.

  • Add an output: This involves specifying the receiver's address (where you want to send the funds), the amount of Kalapas (the cryptocurrency) to send, and the asset ID (the identifier of the asset being transferred).

Once you have added the input and output to the transaction, you can proceed to sign it. The signing process involves using the private key associated with the input UTXO to create a digital signature for the transaction. The signature ensures the integrity and authenticity of the transaction.

After the transaction is signed, it needs to be transformed into hexadecimal format (hex) so that it can be published on the network. This encoding ensures that the transaction can be transmitted and understood by the blockchain network.

Here an example:

import {TransactionBuilder,ExtendedKey} from '@zen/zenjs'
import {post} from 'axios'

const mnemonic = 'one one one one one one one one one one one one one one one one one one one one one one one one';
const privateKey = ExtendedKey.fromMnemonic(mnemonic).derivePath("m/44'/258'/0'/0/0").getPrivateKey();
const tb = new TransactionBuilder('test');
tb.addInput('0000000000000000000000000000000000000000000000000000000000000000',0, privateKey);
tb.addOutput('tp1qfyplhxql09lvvg53dxg7t77tkkxhsp3l6q8xjjpj85hvqlw0ttqswjdapx', 100, '00');
const tx = tb.sign();
console.log(tx.hash(), tx.toJson());
const hex = tx.toHex();// Transaction is ready to be published 
post('http://127.0.0.1/:31567/blockchain/publishtransaction',hex,{ headers: { 'Content-Type': 'application/json' }});

Wallet functionalities

export class Wallet {
    constructor(extendedKey: ExtendedKey, actions: WalletActions, index?: number);
    static fromMnemonic(key: string, actions: WalletActions): Wallet;
    getExternalPublicKey(): PublicKey;
    getExternalPublicKeyHash(): Hash;
    getExternalAddress(): string;
    connectWallet();
    getActiveContracts(): Promise<ActiveContracts[]>;
    getBalance(addresses?: string[]): Promise<{}>;
    submitRepoVote(repoVotingContract: string, commitID: string, phase: "Contestant" | "Candidate", currentInterval: number, privates: PrivateKey[], publish?: boolean): Promise<string>;
    submitCGPBallot(cgpVotingContract: string, command: string, ballotData: Payout | Allocation, isNomination: boolean, currentInterval: number, privates: PrivateKey[], publish?: boolean): Promise<string>;
    signMessage(msg: Buffer, path: string, privates: PrivateKey[]): Signature;
    getAddress(path: string): string;
    getTransactions(skip?: number, take?: number): Promise<Transactions>;
    getTransactionCount(): Promise<number>;
    send(outputs: Array<SpendType>, privates: PrivateKey[], publish?: boolean): Promise<string>;
    sendRaw(outputs: Array<SpendType>): Promise<RawTransaction>;
    executeContract({ address, contractData, privates, publish }?: any): Promise<string>;
    extendContract({ contractId, numberOfBlocks, privates, publish }?: any): Promise<string>;
    activateContract({ code, limit, numberOfBlocks, privates, publish }?: any): Promise<string>;
    signTransaction(unspentTx: Transaction | string, privateKeys: PrivateKey[]): Promise<string>;
    signContractExecution(unspentTx: Transaction | string, sign: string | undefined, privateKeys: PrivateKey[]): Promise<string>;
    collectRaw(requiredAmounts: {[s: string]: string;}, addresses?: string[]);
}

Last updated