Payment Processing

Payment Processing

Zen Protocol is a multi-token blockchain, any contract can issue any number of tokens. Each token (called an asset) is represented by the contract identifier and optional sub type (of 32 bytes).
Zen assets are represented by one byte of the value 0 (zero).
Each asset also has a string representation, which is being used in the UI and API. Zen tokens are represented by the string 00 (zero zero).
Unlike bitcoin, when sending an asset we have to specify which asset are we spending. Therefore the API always requires an additional asset parameter on the wallet/send and wallet/execute API calls.

Hot wallet payment processing

The Zen-Node exposes an API for accepting payments.
For each payment we generate a new unique address and provide it to the end-user. Then periodically check the amount received for each address and save it. When the amount for address changed between calls you know the user made a payment. You can also specify the minimum confirmations required to consider a payment as complete.
All the assets belong to the wallet and can be spent by the wallet/send API call.
You first have to create a wallet, either through the UI or via the zen-cli. The private key is encrypted, however it is still on the hot zen-node machine, therefore this solution should be treated as a hot wallet.

API Calls

post
http://127.0.0.1:31567
/wallet/send
Send Assets
post
http://127.0.0.1:31567
/wallet/getnewaddress
Get New Address
get
http://127.0.0.1:31567
/wallet/receivedbyaddress
Received By Address
post
http://127.0.0.1:31567
/wallet/restorenewaddresses
Account Discovery

Cold wallet payment processing

You can also use cold wallet for the payment processing.
You will have to use zen.js library for the process.
Generate an extended private key on a secure computer and import the extended public key.
1
import {Mnemonic, ExtendedKey} from '@zen/zenjs'
2
3
const mnemonic = Mnemonic.generateMnemonic(24); // Generate once
4
console.log('mnemonic phrase', mnemonic);
5
6
const publicKey = ExtendedKey.fromMnemonic(mnemonic).neutered();
7
8
console.log('Extended Public Key', publicKey.toString());
Copied!
On a hot machine generate a unique address by deriving the key with a unique index. Save the index to later sign the transaction on the secure computer.
1
import {ExtendedKey} from '@zen/zenjs'
2
import {post} from 'axios'
3
4
const publicKey = ExtendedKey.fromString(encodedString);
5
const address = publicKey.derive(uniqueIndex).toAddress();
6
7
// import the address to zen node
8
post('http://127.0.0.1/:31567/wallet/importwatchonlyaddress',address,{ headers: { 'Content-Type': 'application/json' }});
Copied!
Share the address with the user and track the address for income payments the same as for the hot wallet, by calling wallet/receivedbyaddress
To make a payment you will to get the outputs from the zen-node and assemble the transaction on the secure computer. Retrieve the available outputs by calling wallet/addressoutputs for each address.
Example of assembling and publishing transaction:
1
import {TransactionBuilder,ExtendedKey} from '@zen/zenjs'
2
import {post} from 'axios'
3
4
const tb = new TransactionBuilder('main');
5
6
// Get the private key by matching the unique index for the input address
7
const privateKey = ExtendedKey.fromMnemonic(mnemonic).derive(inputAddressIndex).getPrivateKey();
8
9
// An example input, use one you retrieved by calling `wallet/addressoutputs`
10
tb.addInput('0000000000000000000000000000000000000000000000000000000000000000',0, privateKey);
11
12
// Address, amount and token (Zen)
13
tb.addOutput('tp1qfyplhxql09lvvg53dxg7t77tkkxhsp3l6q8xjjpj85hvqlw0ttqswjdapx', 100, '00');
14
15
const tx = tb.sign();
16
17
// You can take the hex to an hot computer
18
const hex = tx.toHex();
19
20
// Transaction is ready to be published
21
post('http://127.0.0.1/:31567/blockchain/publishpublishtransaction',hex,{ headers: { 'Content-Type': 'application/json' }});
22
Copied!
Last modified 11mo ago