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
zenprotocol / zen.js
GitLab

Install

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

Generating mnemonic phrase, keys and accepting payments

1
import {Mnemonic, ExtendedKey} from '@zen/zenjs'
2
const mnemonic = Mnemonic.generateMnemonic(24);
3
const extendedKey = ExtendedKey.fromMnemonic(mnemonic);
4
const privateKey = extendedKey.derivePath("m/44'/258'/0'/0/0").getPrivateKey();
5
const publicKey = extendedKey.getPublicKey();
6
console.log(publicKey.toAddress('main'));
Copied!

Creating and signing transactions

1
import {TransactionBuilder,ExtendedKey} from '@zen/zenjs'
2
import {post} from 'axios'
3
4
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';
5
const privateKey = ExtendedKey.fromMnemonic(mnemonic).derivePath("m/44'/258'/0'/0/0").getPrivateKey();
6
const tb = new TransactionBuilder('test');
7
tb.addInput('0000000000000000000000000000000000000000000000000000000000000000',0, privateKey);
8
tb.addOutput('tp1qfyplhxql09lvvg53dxg7t77tkkxhsp3l6q8xjjpj85hvqlw0ttqswjdapx', 100, '00');
9
const tx = tb.sign();
10
console.log(tx.hash(), tx.toJson());
11
const hex = tx.toHex();// Transaction is ready to be published
12
post('http://127.0.0.1/:31567/blockchain/publishpublishtransaction',hex,{ headers: { 'Content-Type': 'application/json' }});
Copied!

Wallet functionalities

1
export class Wallet {
2
constructor(extendedKey: ExtendedKey, actions: WalletActions, index?: number);
3
static fromMnemonic(key: string, actions: WalletActions): Wallet;
4
getExternalPublicKey(): PublicKey;
5
getExternalPublicKeyHash(): Hash;
6
getExternalAddress(): string;
7
connectWallet();
8
getActiveContracts(): Promise<ActiveContracts[]>;
9
getBalance(addresses?: string[]): Promise<{}>;
10
submitRepoVote(repoVotingContract: string, commitID: string, phase: "Contestant" | "Candidate", currentInterval: number, privates: PrivateKey[], publish?: boolean): Promise<string>;
11
submitCGPBallot(cgpVotingContract: string, command: string, ballotData: Payout | Allocation, isNomination: boolean, currentInterval: number, privates: PrivateKey[], publish?: boolean): Promise<string>;
12
signMessage(msg: Buffer, path: string, privates: PrivateKey[]): Signature;
13
getAddress(path: string): string;
14
getTransactions(skip?: number, take?: number): Promise<Transactions>;
15
getTransactionCount(): Promise<number>;
16
send(outputs: Array<SpendType>, privates: PrivateKey[], publish?: boolean): Promise<string>;
17
sendRaw(outputs: Array<SpendType>): Promise<RawTransaction>;
18
executeContract({ address, contractData, privates, publish }?: any): Promise<string>;
19
extendContract({ contractId, numberOfBlocks, privates, publish }?: any): Promise<string>;
20
activateContract({ code, limit, numberOfBlocks, privates, publish }?: any): Promise<string>;
21
signTransaction(unspentTx: Transaction | string, privateKeys: PrivateKey[]): Promise<string>;
22
signContractExecution(unspentTx: Transaction | string, sign: string | undefined, privateKeys: PrivateKey[]): Promise<string>;
23
collectRaw(requiredAmounts: {[s: string]: string;}, addresses?: string[]);
24
}
Copied!
Last modified 10mo ago