import * as mocha from 'mocha'; import * as chai from 'chai'; import { Network } from "./types"; import { buildERC20Transaction, buildTransaction, extractSignature, getERC20HolaParameters_LINK, getHolaParameters_LINK, verifyAddress } from '.'; import { hexToBytes } from './TransactionHelper'; const expect = chai.expect; describe('hw_ewt address verification tests', () => { it('Should verify EWT address correctly' , () => { expect(verifyAddress("0x34c3e76432d9842cfe2bb6c882dad2b43505baaa")) .to.equal(true); }); it('Should fail verify EWT address correctly' , () => { expect(verifyAddress("0x34c3e76432d9842cfe2bb6c882dad2b43505baa")) .to.equal(false); }); it('Should fail verify EWT address correctly' , () => { expect(verifyAddress("0x34c3e76432d9842cfe2bb6c882dad2b4350ybaaa")) .to.equal(false); }); }); describe('hw_ewt transaction building tests', () => { it('Should build unsigned EWT transaction correctly' , () => { expect(buildTransaction({nonce : "0", gasPrice: "50", gasLimit: 21000, toAddress: "0x34c3e76432d9842cfe2bb6c882dad2b43505baaa", amount: "0.05", network: Network.Testnet}).toString('hex')) .to.equal("ee80850ba43b74008252089434c3e76432d9842cfe2bb6c882dad2b43505baaa87b1a2bc2ec5000080830120478080"); }); let rD = hexToBytes("BBB2D06AB85B3AB1AF13560E2A18BB0E36DE6465FEE34C96953D28241F5E6724"); let sD = hexToBytes("57FB79AE09A7DA48693F4D8F861CA96CD71BD629DDEA972A181C5F65B5F56AF9"); it('Should build signed EWT transaction correctly' , () => { expect(buildTransaction({nonce : "0", gasPrice: "50", gasLimit: 21000, toAddress: "0x34c3e76432d9842cfe2bb6c882dad2b43505baaa", amount: "0.05", signature: {v:147633, r: rD, s: sD}, network: Network.Testnet}).toString('hex')) .to.equal("f86e80850ba43b74008252089434c3e76432d9842cfe2bb6c882dad2b43505baaa87b1a2bc2ec5000080830240b1a0bbb2d06ab85b3ab1af13560e2a18bb0e36de6465fee34c96953d28241f5e6724a057fb79ae09a7da48693f4d8f861ca96cd71bd629ddea972a181c5f65b5f56af9"); }); }); describe('hw_ewt transaction with data building tests', () => { it('Should build unsigned EWT transaction with data correctly' , () => { expect(buildTransaction({nonce : "338", gasPrice: "129.090672713", gasLimit: 142999, toAddress: "0x68b3465833fb72a70ecdf485e0e4c7bd8665fc45", amount: "0", data: Buffer.from("5ae401dc0000000000000000000000000000000000000000000000000000000061e16c0200000000000000000000000000000000000000000000000000000000000000400000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000e404e45aaf000000000000000000000000f4d2888d29d722226fafa5d9b24f9164c092421e000000000000000000000000c02aaa39b223fe8d0a0e5c4f27ead9083c756cc20000000000000000000000000000000000000000000000000000000000002710000000000000000000000000f4fe6141e8f9c614cfa6fd9c8f8b139ae3c249fe00000000000000000000000000000000000000000000001dfa72b234f63c29ee00000000000000000000000000000000000000000000000007df0f7fb8fd1856000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000", "hex"), network: Network.Mainnet}).toString('hex')) .to.equal("f901ce820152851e0e67584983022e979468b3465833fb72a70ecdf485e0e4c7bd8665fc4580b901a45ae401dc0000000000000000000000000000000000000000000000000000000061e16c0200000000000000000000000000000000000000000000000000000000000000400000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000e404e45aaf000000000000000000000000f4d2888d29d722226fafa5d9b24f9164c092421e000000000000000000000000c02aaa39b223fe8d0a0e5c4f27ead9083c756cc20000000000000000000000000000000000000000000000000000000000002710000000000000000000000000f4fe6141e8f9c614cfa6fd9c8f8b139ae3c249fe00000000000000000000000000000000000000000000001dfa72b234f63c29ee00000000000000000000000000000000000000000000000007df0f7fb8fd185600000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000081f68080"); }); let rD = hexToBytes("f250e73b59a556a2c4089018b9c85c75acaf7ced8a89b48a6abf1ebdfbbc7266"); let sD = hexToBytes("67095cab0e53940b96fe68a28d497d395074b7aca5e264f6222cf1161ee08c"); it('Should build signed EWT transaction with data correctly' , () => { expect(buildTransaction({nonce : "338", gasPrice: "129.090672713", gasLimit: 142999, toAddress: "0x68b3465833fb72a70ecdf485e0e4c7bd8665fc45", amount: "0", data: Buffer.from("5ae401dc0000000000000000000000000000000000000000000000000000000061e16c0200000000000000000000000000000000000000000000000000000000000000400000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000e404e45aaf000000000000000000000000f4d2888d29d722226fafa5d9b24f9164c092421e000000000000000000000000c02aaa39b223fe8d0a0e5c4f27ead9083c756cc20000000000000000000000000000000000000000000000000000000000002710000000000000000000000000f4fe6141e8f9c614cfa6fd9c8f8b139ae3c249fe00000000000000000000000000000000000000000000001dfa72b234f63c29ee00000000000000000000000000000000000000000000000007df0f7fb8fd1856000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000", "hex"), signature: {v:147633, r: rD, s: sD}, network: Network.Testnet}).toString('hex')) .to.equal("f9020f820152851e0e67584983022e979468b3465833fb72a70ecdf485e0e4c7bd8665fc4580b901a45ae401dc0000000000000000000000000000000000000000000000000000000061e16c0200000000000000000000000000000000000000000000000000000000000000400000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000e404e45aaf000000000000000000000000f4d2888d29d722226fafa5d9b24f9164c092421e000000000000000000000000c02aaa39b223fe8d0a0e5c4f27ead9083c756cc20000000000000000000000000000000000000000000000000000000000002710000000000000000000000000f4fe6141e8f9c614cfa6fd9c8f8b139ae3c249fe00000000000000000000000000000000000000000000001dfa72b234f63c29ee00000000000000000000000000000000000000000000000007df0f7fb8fd1856000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000830240b1a0f250e73b59a556a2c4089018b9c85c75acaf7ced8a89b48a6abf1ebdfbbc72669f67095cab0e53940b96fe68a28d497d395074b7aca5e264f6222cf1161ee08c"); }); }); describe('hw_ewt HOLa parameters generation tests (LINK)', () => { it('Should generate HOLa parameters correctly (LINK)' , () => { let parameters = getHolaParameters_LINK({nonce : "0", gasPrice: "50", gasLimit: 21000, toAddress: "0x34c3e76432d9842cfe2bb6c882dad2b43505baaa", amount: "0.05", network: Network.Testnet}); expect(parameters.blk[0].toString('hex')) .to.equal("70c874cf625ac0f464cf5c6c8bb205b120b6002537e25e9e4a02d439d3791b51"); }); }); describe('hw_ewt signature extraction tests', () => { it('Should extract v,r,s from a DER encoded signature' , () => { var e = Buffer.from("3045022100cbdfdf1c8e2ca101c513ec148d2debb56910c6c3811f5b65dc475fa3e2f61e08022078577310650f819271a8a13664838cc99ff583add6199a2988da4efbd94a7ce9", 'hex') var t = Buffer.from("F869038509502F900082C832946CE8DA28E2F864420840CF74474EFF5FD80E65B880B844A9059CBB00000000000000000000000034C3E76432D9842CFE2BB6C882DAD2B43505BAAA00000000000000000000000000000000000000000000000000038D7EA4C68000618080", 'hex') let sig = e; let message =t let signature = extractSignature({signature: sig, transaction: message, address: "0x1223de8a29a4820d3fed6a354f8d37c9efd9d943", net: Network.Testnet}) expect(signature.v) .to.equal(147633); expect(signature.r.toString('hex')) .to.equal("cbdfdf1c8e2ca101c513ec148d2debb56910c6c3811f5b65dc475fa3e2f61e08"); expect(signature.s.toString('hex')) .to.equal("78577310650f819271a8a13664838cc99ff583add6199a2988da4efbd94a7ce9"); }); }); // describe('hw_ewt erc20 transaction building tests', () => { it('Should build unsigned EWT erc20 transaction correctly' , () => { expect(buildERC20Transaction({nonce: "6", gasPrice: "25", gasLimit: 53979, contractAddress: "0x101848d5c5bbca18e6b4431eedf6b95e9adf82fa", toAddress: "0x34c3e76432d9842cfe2bb6c882dad2b43505baaa", amount: "0.25", contractDecimals: 18, contractName: "WEENUS", network: Network.Testnet}).toString('hex')) .to.equal("f86c068505d21dba0082d2db94101848d5c5bbca18e6b4431eedf6b95e9adf82fa80b844a9059cbb00000000000000000000000034c3e76432d9842cfe2bb6c882dad2b43505baaa00000000000000000000000000000000000000000000000003782dace9d90000830120478080"); }); let reD = hexToBytes("832524266CD28331296739BA9E899625374539256A5D522CF3E3B8D1C799FE99"); let seD = hexToBytes("0CAE1939CC630EB25A144EA2E8C0E54071EBC5E17D33C3F35959B2B32FDACDE2"); it('Should build signed EWT erc20 transaction correctly' , () => { expect(buildERC20Transaction({nonce: "6", gasPrice: "25", gasLimit: 53979, contractAddress: "0x101848d5c5bbca18e6b4431eedf6b95e9adf82fa", toAddress: "0x34c3e76432d9842cfe2bb6c882dad2b43505baaa", amount: "0.25", contractDecimals: 18, contractName: "WEENUS", signature: {v: 147633, r: reD ,s: seD}, network: Network.Testnet}).toString('hex')) .to.equal("f8ac068505d21dba0082d2db94101848d5c5bbca18e6b4431eedf6b95e9adf82fa80b844a9059cbb00000000000000000000000034c3e76432d9842cfe2bb6c882dad2b43505baaa00000000000000000000000000000000000000000000000003782dace9d90000830240b1a0832524266cd28331296739ba9e899625374539256a5d522cf3e3b8d1c799fe99a00cae1939cc630eb25a144ea2e8c0e54071ebc5e17d33c3f35959b2b32fdacde2"); }); }); describe('hw_ewt erc20 HOLa parameters generation tests (LINK)', () => { it('Should generate erc20 HOLa parameters correctly (LINK)' , () => { let parametersERC = getERC20HolaParameters_LINK({nonce: "6", gasPrice: "25", gasLimit: 53979, contractAddress: "0x101848d5c5bbca18e6b4431eedf6b95e9adf82fa", toAddress: "0x34c3e76432d9842cfe2bb6c882dad2b43505baaa", amount: "0.25", contractDecimals: 18, contractName: "WEENUS", network: Network.Testnet}); let correctBlkData = ["37c7b8072d773e946ead4db2b66a9e68a8ec9e8b48036cec4e96827918514381"]; let blkCont = 0; for(let blk of parametersERC.blk){ expect(blk.toString('hex')).to.equal(correctBlkData[blkCont]); blkCont++; } }); });