Skip to content

Commit b6efd33

Browse files
committed
Updated Zilliqa transactions for 0.3.4 support
1 parent 701e997 commit b6efd33

8 files changed

Lines changed: 148 additions & 57 deletions

File tree

package-lock.json

Lines changed: 19 additions & 19 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@
5656
},
5757
"homepage": "https://github.com/cryptolandtech/moonlet-core#readme",
5858
"dependencies": {
59-
"@zilliqa-js/zilliqa": "^0.3.3",
59+
"@zilliqa-js/zilliqa": "^0.3.4",
6060
"axios": "^0.16.2",
6161
"bignumber.js": "^7.2.1",
6262
"bip39": "^2.5.0",

src/blockchain/zilliqa/account.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,7 @@ export class ZilliqaAccount extends GenericAccount<ZilliqaTransaction, IZilliqaT
105105
const TXObject = transaction.toParams( this.publicKey.replace("0x", "") );
106106

107107
// the address should be checksummed and we need to lowercase it for signing
108+
// add 0x back to signing payload
108109
TXObject.toAddr = TXObject.toAddr.toLowerCase();
109110

110111
const bytes = transaction.getProtoEncodedTx(TXObject);

src/blockchain/zilliqa/node.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { Network } from "../../core/network";
33
import networks from "./networks";
44
import { BigNumber } from 'bignumber.js';
55
import { ZilliqaTransaction } from "./transaction";
6+
import * as ZilliqaJsCrypto from "@zilliqa-js/crypto";
67

78
export class ZilliqaNode extends GenericNode {
89

@@ -94,6 +95,9 @@ export class ZilliqaNode extends GenericNode {
9495
SendObject.gasPrice = SendObject.gasPrice.toString();
9596
SendObject.gasLimit = SendObject.gasLimit.toString();
9697

98+
// remove once core accepts 0x
99+
SendObject.toAddr = ZilliqaJsCrypto.toChecksumAddress( SendObject.toAddr ).replace("0x", "");
100+
97101
return this.sendRaw(SendObject);
98102
}
99103

src/blockchain/zilliqa/transaction.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { GenericTransaction, ITransactionOptions } from '../../core/transaction';
22
import { BN, Long } from '@zilliqa-js/util';
33
import { util as ZilliqaJsAccountUtil } from "@zilliqa-js/account";
4+
import * as ZilliqaJsCrypto from "@zilliqa-js/crypto";
45

56
export interface IZilliqaTransactionOptions extends ITransactionOptions {
67
gasPrice: number;
@@ -49,10 +50,9 @@ export class ZilliqaTransaction extends GenericTransaction<IZilliqaTransactionOp
4950
* @returns parameters object
5051
*/
5152
public toParams( subPubKey?: string ) {
52-
5353
return {
5454
version: ( this.chainId << 16 ) + this.version, // add replay protection
55-
toAddr: this.to.replace("0x", ""),
55+
toAddr: ZilliqaJsCrypto.toChecksumAddress( this.to ).replace("0x", ""),
5656
nonce: this.nonce,
5757
pubKey: subPubKey || "",
5858
amount: new BN( this.amount ),

src/core/account.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ export abstract class GenericAccount<
9494
}
9595

9696
/**
97-
* Sends generic account
97+
* Sends transaction
9898
* @param transaction
9999
* @param [cb]
100100
* @param [cbtype]

test/zilliqa/test.ts.disabled

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
const { Transaction } = require('@zilliqa-js/account');
2+
const { BN, Long, bytes, units } = require('@zilliqa-js/util');
3+
const { Zilliqa } = require('@zilliqa-js/zilliqa');
4+
const CP = require ('@zilliqa-js/crypto');
5+
6+
const zilliqa = new Zilliqa('https://api.zilliqa.com');
7+
8+
// These are set by the core protocol, and may vary per-chain.
9+
// These numbers are JUST AN EXAMPLE. They will NOT WORK on the public testnet
10+
// or mainnet. Please check what they are before proceeding, or your
11+
// transactions will simply be rejected.
12+
const CHAIN_ID = 62;
13+
const MSG_VERSION = 1;
14+
const VER = bytes.pack(CHAIN_ID, MSG_VERSION);
15+
16+
// Populate the wallet with an account
17+
const privkey = '891E98DBEF714F120958405F5CF1FA4F47496D0B287E514C1A7EC02805DA3C13';
18+
const testReceiverAddress = "0x99959F33842946AeF50A7573b8c3cBf04Df339c7";
19+
const receiverAddr = CP.toChecksumAddress( testReceiverAddress ).replace("0x", "");
20+
zilliqa.wallet.addByPrivateKey(privkey);
21+
22+
const add = CP.getAddressFromPrivateKey(privkey);
23+
console.log('Your account address is:');
24+
console.log(`0x${add}`);
25+
26+
async function testBlockchain() {
27+
try {
28+
// GetBalance
29+
const balance = await zilliqa.blockchain.getBalance(add);
30+
console.log('Your account balance is:');
31+
console.log(balance.result);
32+
let tx = zilliqa.transactions.new({
33+
version: VER,
34+
toAddr: receiverAddr,
35+
// Note all transactional values has to be converted to Qa (the smallest accounting unit) when using the Zilliqa protocol.
36+
// 1 Qa is 10^-12 Zil.
37+
amount: units.toQa('10', units.Units.Zil), // Sending an amount in Zil and converting the amount to Qa.
38+
gasPrice: units.toQa('1000', units.Units.Li), // gasPrice is measured in Li. 1 Li is 10^-6 Zil.
39+
gasLimit: Long.fromNumber(1),
40+
});
41+
42+
console.log("Params: \n", tx.txParams);
43+
console.log("Object: \n", tx);
44+
// Send a transaction to the network
45+
tx = await zilliqa.blockchain.createTransaction(tx, 35, 1000);
46+
console.log("The transaction status is:");
47+
console.log(tx.receipt);
48+
} catch (err) {
49+
console.log(err);
50+
}
51+
}
52+
53+
testBlockchain();
Lines changed: 67 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ import { GenericAccount, AccountType } from "../../src/core/account";
99
import { GenericNode } from "../../src/core/node";
1010
import { GenericTransaction, TransactionStatus } from "../../src/core/transaction";
1111
import { ZilliqaTransaction } from "../../src/blockchain/zilliqa/transaction";
12+
import { ZilliqaAccountUtils } from "../../src/blockchain/zilliqa/account-utils";
13+
const AccountUtils = new ZilliqaAccountUtils();
1214

1315
import Ethereum from "../../src/blockchain/ethereum/class.index";
1416
import Zilliqa from "../../src/blockchain/zilliqa/class.index";
@@ -17,21 +19,17 @@ import { Zilliqa as ZilliqaOfficial } from 'zilliqa-js';
1719
import { Zilliqa as OfficialZilliqa } from '@zilliqa-js/zilliqa';
1820

1921
import * as ZilliqaJsCrypto from "@zilliqa-js/crypto";
20-
import { BN, Long } from '@zilliqa-js/util';
22+
import { BN, Long, bytes, units } from '@zilliqa-js/util';
2123
import Signature from 'elliptic/lib/elliptic/ec/signature';
2224

23-
// const pk = "891E98DBEF714F120958405F5CF1FA4F47496D0B287E514C1A7EC02805DA3C13";
2425
const pk = "891E98DBEF714F120958405F5CF1FA4F47496D0B287E514C1A7EC02805DA3C13";
25-
26-
// ionut
27-
const testReceiverAddress = "0xee9d7b0a5cd3ff9fcdd3a44a6ee49ff9edfe382a";
26+
const testReceiverAddress = "0x99959F33842946AeF50A7573b8c3cBf04Df339C7";
2827

2928
const mapper = new DynamicClassMapper();
3029
mapper.collectClasses(Zilliqa.AvailableClasses);
3130
mapper.collectClasses(Ethereum.AvailableClasses);
3231
const mnemonic = "exchange neither monster ethics bless cancel ghost excite business record warfare invite";
3332

34-
/*
3533
describe("Zilliqa", async () => {
3634

3735
describe("Wallet with one Zilliqa account", async () => {
@@ -43,10 +41,10 @@ describe("Zilliqa", async () => {
4341
const blockchain = Blockchains.ZILLIQA;
4442

4543
const TestNode: GenericNode = defaultWallet.getNode( blockchain );
46-
TestNode.init( TestNode.NETWORKS[ 1 ] ); // https://api-scilla.zilliqa.com/
44+
// TestNode.init( TestNode.NETWORKS[ 1 ] ); // https://api-scilla.zilliqa.com/
4745

48-
// TestNode.init( TestNode.NETWORKS[ 0 ] );
49-
// TestNode.setCustomNetworkUrl("https://dev-test-api.aws.z7a.xyz/");
46+
TestNode.NETWORKS[ 0 ].chainId = 62;
47+
TestNode.init( TestNode.NETWORKS[ 0 ] );
5048

5149
const AccountClassTypeString = GenericAccount.getImplementedClassName( Blockchains[blockchain] );
5250
const NodeClassTypeString = GenericNode.getImplementedClassName( Blockchains[blockchain] );
@@ -63,26 +61,44 @@ describe("Zilliqa", async () => {
6361

6462
it("Should have a balance", async () => {
6563
myNonce = await account.getNonce();
66-
console.log( await account.getBalance() );
67-
console.log( myNonce );
64+
const balance = await account.getBalance();
6865

69-
});
66+
console.log( "Account: ", AccountUtils.toChecksumAddress( account.address ) );
67+
console.log( "Balance: ", balance.toString() );
68+
console.log( "Nonce: ", myNonce );
69+
70+
}).timeout(10000);
7071

7172
describe("send() signed transaction", async () => {
7273
it("debug", async () => {
7374
const nonce = await account.getNonce();
7475

76+
const amount = 10 * ( 10 ** 12);
77+
const txGasPrice = 1000 * (10 ** 6);
78+
const txGasLimit = 1;
79+
80+
// amount: units.toQa('10', units.Units.Zil), // Sending an amount in Zil and converting the amount to Qa.
81+
// gasPrice: units.toQa('10000', units.Units.Li), // gasPrice is measured in Li. 1 Li is 10^-6 Zil.
82+
// gasLimit: Long.fromNumber(1),
83+
84+
console.log( "amount: ", amount.toString() );
85+
console.log( "txGasPrice: ", txGasPrice.toString() );
86+
console.log( "txGasLimit: ", txGasLimit.toString() );
87+
88+
// testReceiverAddress.replace("0x", "").toLowerCase(),
7589
const transaction = account.buildTransferTransaction(
76-
testReceiverAddress.replace("0x", "").toLowerCase(),
77-
8, // amount
78-
nonce, // nonce
79-
10, // gasLimit
80-
100, // gasPrice
90+
AccountUtils.toChecksumAddress( testReceiverAddress ).replace("0x", ""),
91+
amount, // amount
92+
nonce + 1, // nonce
93+
txGasLimit, // gasLimit
94+
txGasPrice, // gasPrice
8195
) as ZilliqaTransaction;
8296

97+
console.log("\n", ">>>> Moonlet toParams:", "\n", transaction.toParams() );
98+
8399
await account.signTransaction( transaction );
84100

85-
console.log("\n", ">>>> Moonlet txn:", transaction.TXObject);
101+
console.log("\n", ">>>> Moonlet TXObject:", "\n", transaction.TXObject);
86102

87103
await account.send( transaction );
88104

@@ -94,38 +110,55 @@ describe("Zilliqa", async () => {
94110

95111
}).timeout(10000);
96112
});
113+
97114
});
98115

116+
/*
99117
describe("Official Transaction Test", async () => {
100118

101119
it("Should work", async () => {
102120

103-
const { BN, Long } = require('@zilliqa-js/util');
104-
105121
const { Transaction } = require('@zilliqa-js/account');
106-
107-
const zilliqa = new OfficialZilliqa('https://api-scilla.zilliqa.com/');
122+
const zilliqa = new OfficialZilliqa('https://api.zilliqa.com/');
108123

109124
zilliqa.wallet.addByPrivateKey(pk);
125+
const add = ZilliqaJsCrypto.getAddressFromPrivateKey(pk);
126+
console.log('\nYour account address is:');
127+
console.log(`0x${add}`);
128+
129+
const CHAIN_ID = 62;
130+
const MSG_VERSION = 1;
131+
const VER = ( CHAIN_ID << 16 ) + MSG_VERSION;
132+
133+
try {
110134

111-
const newtx = zilliqa.transactions.new({
112-
version: 1,
113-
toAddr: testReceiverAddress.replace("0x", "").toLowerCase(),
114-
amount: new BN(8),
115-
gasPrice: new BN(100),
116-
// can be `number` if size is <= 2^53 (i.e., window.MAX_SAFE_INTEGER)
117-
gasLimit: Long.fromNumber(10),
118-
});
135+
const balance = await zilliqa.blockchain.getBalance(add);
136+
console.log('Your account balance is:');
137+
console.log(balance.result);
119138

120-
console.log("newtx", newtx);
139+
const newtx = zilliqa.transactions.new({
140+
version: VER,
141+
toAddr: AccountUtils.toChecksumAddress( testReceiverAddress ).replace("0x", "").toLowerCase(),
142+
amount: units.toQa('10', units.Units.Zil), // Sending an amount in Zil and converting the amount to Qa.
143+
gasPrice: units.toQa('10000', units.Units.Li), // gasPrice is measured in Li. 1 Li is 10^-6 Zil.
144+
gasLimit: Long.fromNumber(1),
145+
});
121146

122-
const tx = await zilliqa.blockchain.createTransaction(newtx);
123-
console.log( tx );
147+
console.log("\nnewtx", newtx);
148+
149+
const tx = await zilliqa.blockchain.createTransaction(newtx);
150+
console.log("The transaction status is:");
151+
console.log(tx.receipt);
152+
153+
} catch (err) {
154+
console.log("\n", err);
155+
}
124156

125157
}).timeout(10000);
126158
});
159+
*/
127160
});
128-
*/
161+
129162
/*
130163
describe("Kaya - Moonlet Core - Wallet with one Zilliqa account", async () => {
131164

0 commit comments

Comments
 (0)