Links

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

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/publishpublishtransaction',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[]);
}