Zen Protocol Documentation
WebsiteTelegramBlogForum
  • Zen Protocol Docs
  • Apps
    • Headless Full Node
      • Full Node NPM Package
      • Build from Source
      • CLI
      • API
        • Wallet
        • Contracts
        • General
        • Blockchain
        • AddressDB
      • Deploy
        • Systemd
        • Shell script
        • Docker
      • Technical Model
      • AddressDB
    • Wallet
      • Desktop Wallet
        • Installers
      • Web Wallet
        • Run Locally
      • Wallet User Guide
        • Connect a Wallet
          • Create a Wallet
          • Import a Wallet
          • Watch Mode
        • My Wallet
          • Navigation Bar
          • Portfolio
          • Receive
          • Send / Execute
          • Transaction History
        • Contracts
          • Active Contracts
          • Execute a Contract
            • Message Body Field
          • Extend a Contract
          • Activate a Contract
        • Voting
          • Common Goods Pool
            • Generating a Ballot ID
          • Governance
        • Signer
        • Settings
          • Account Settings
          • Node Connectivity
      • Deprecated Desktop Wallet
        • Executable Installers
        • Wallet Structure
          • Video Tutorials
    • Explorer
    • Zen.js
      • Payment Processing
    • Oracle
      • Oracle GUI
      • Oracle Service
      • Oracle's API
      • Deploy
        • Systemd
        • Docker
      • How the Oracle contract works
      • How to create an Attestation token
    • Dex
      • Dex User Guide
        • Traded Pairs
        • Search Pairs
        • Order Book
        • Operations
        • My Wallet
        • Settings
      • How DEX contract works
    • Fixed Payout
      • Fixed Payout Generator User Guide
        • Issue
        • Redeem
        • Cancel
        • Verify
        • Settings
      • How the FP Contract works
      • How are the asset named?
  • Smart Contracts
  • Contract Structure
  • Contract Cost
  • Contract Activation
  • Contract Examples
  • Smart Contracts SDK
  • Contracts Language ZF*
  • Named Token Tutorial
  • Consensus
  • Common Goods Pool
  • Block Validation
  • Transaction Validation
  • Serialization
  • Use Cases
    • Create Unsigned Transaction
    • Secure Sign Transaction
    • Cold Storage using Full Node
  • Troubleshooting
    • Responsible Disclosure
    • Bug Bounty
    • Known Bugs
  • For Miners
    • Pools
    • GPU Mining
    • GPU Bounties
  • Check Crowdsale Contribution
  • Alpha call option
Powered by GitBook
On this page
  • Install
  • Generating mnemonic phrase, keys and accepting payments
  • Creating and signing transactions
  • Wallet functionalities
  1. Apps

Zen.js

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

PreviousExplorerNextPayment Processing

Last updated 1 year ago

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[]);
}

zenprotocol / zen.jsGitLab
Logo