import { buildTransaction, getHolaParameters, buildBEP20Transaction, getBEP20HolaParameters, extractSignature, getHolaParameters_LINK, getBEP20HolaParameters_LINK, verifyAddress } from "."; import * as mocha from 'mocha'; import * as chai from 'chai'; import { Network } from "./types"; const expect = chai.expect; describe('hw_bnb address verification tests', () => { it('Should verify Ethereum address correctly' , () => { expect(verifyAddress("0x34c3e76432d9842cfe2bb6c882dad2b43505baaa")) .to.equal(true); }); it('Should fail verify Ethereum address correctly' , () => { expect(verifyAddress("0x34c3e76432d9842cfe2bb6c882dad2b43505baa")) .to.equal(false); }); it('Should fail verify Ethereum address correctly' , () => { expect(verifyAddress("0x34c3e76432d9842cfe2bb6c882dad2b4350ybaaa")) .to.equal(false); }); }); describe('hw_bnb transaction building tests', () => { it('Should build unsigned BNB transaction correctly' , () => { expect(buildTransaction({nonce : "2", gasPrice: "40", gasLimit: 21000, toAddress: "0x34c3e76432d9842cfe2bb6c882dad2b43505baaa", "amount": "0.05", network: Network.Testnet}).toString('hex')) .to.equal("eb028509502f90008252089434c3e76432d9842cfe2bb6c882dad2b43505baaa87b1a2bc2ec5000080618080"); }); let rD = hexToBytes("1621C1FE56C31F46332E7C5034BB366B648B0B68C0287F1CA956782B1E6DD19C"); let sD = hexToBytes("7287B54A036B901AEA4B531DBEC3CF09692A5714F463904FFA675CB749F19E7B"); it('Should build signed BNB transaction correctly' , () => { expect(buildTransaction({nonce : "2", gasPrice: "40", gasLimit: 21000, toAddress: "0x34c3e76432d9842cfe2bb6c882dad2b43505baaa", "amount": "0.05", signature: {v:0xe5, r: rD, s: sD}, network: Network.Testnet}).toString('hex')) .to.equal("f86c028509502f90008252089434c3e76432d9842cfe2bb6c882dad2b43505baaa87b1a2bc2ec500008081e5a01621c1fe56c31f46332e7c5034bb366b648b0b68c0287f1ca956782b1e6dd19ca07287b54a036b901aea4b531dbec3cf09692a5714f463904ffa675cb749f19e7b"); }); }); describe('hw_eth transaction with data building tests', () => { it('Should build unsigned BNB 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("f901cd820152851e0e67584983022e979468b3465833fb72a70ecdf485e0e4c7bd8665fc4580b901a45ae401dc0000000000000000000000000000000000000000000000000000000061e16c0200000000000000000000000000000000000000000000000000000000000000400000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000e404e45aaf000000000000000000000000f4d2888d29d722226fafa5d9b24f9164c092421e000000000000000000000000c02aaa39b223fe8d0a0e5c4f27ead9083c756cc20000000000000000000000000000000000000000000000000000000000002710000000000000000000000000f4fe6141e8f9c614cfa6fd9c8f8b139ae3c249fe00000000000000000000000000000000000000000000001dfa72b234f63c29ee00000000000000000000000000000000000000000000000007df0f7fb8fd1856000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000388080"); }); let rD = hexToBytes("f250e73b59a556a2c4089018b9c85c75acaf7ced8a89b48a6abf1ebdfbbc7266"); let sD = hexToBytes("67095cab0e53940b96fe68a28d497d395074b7aca5e264f6222cf1161ee08c"); it('Should build signed BNB transaction with data correctly' , () => { expect(buildTransaction({nonce : "338", gasPrice: "129.090672713", gasLimit: 142999, toAddress: "0x68b3465833fb72a70ecdf485e0e4c7bd8665fc45", amount: "0", data: Buffer.from("5ae401dc0000000000000000000000000000000000000000000000000000000061e16c0200000000000000000000000000000000000000000000000000000000000000400000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000e404e45aaf000000000000000000000000f4d2888d29d722226fafa5d9b24f9164c092421e000000000000000000000000c02aaa39b223fe8d0a0e5c4f27ead9083c756cc20000000000000000000000000000000000000000000000000000000000002710000000000000000000000000f4fe6141e8f9c614cfa6fd9c8f8b139ae3c249fe00000000000000000000000000000000000000000000001dfa72b234f63c29ee00000000000000000000000000000000000000000000000007df0f7fb8fd1856000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000", "hex"), signature: {v:0x00, r: rD, s: sD}, network: Network.Mainnet}).toString('hex')) .to.equal("f9020c820152851e0e67584983022e979468b3465833fb72a70ecdf485e0e4c7bd8665fc4580b901a45ae401dc0000000000000000000000000000000000000000000000000000000061e16c0200000000000000000000000000000000000000000000000000000000000000400000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000e404e45aaf000000000000000000000000f4d2888d29d722226fafa5d9b24f9164c092421e000000000000000000000000c02aaa39b223fe8d0a0e5c4f27ead9083c756cc20000000000000000000000000000000000000000000000000000000000002710000000000000000000000000f4fe6141e8f9c614cfa6fd9c8f8b139ae3c249fe00000000000000000000000000000000000000000000001dfa72b234f63c29ee00000000000000000000000000000000000000000000000007df0f7fb8fd185600000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000a0f250e73b59a556a2c4089018b9c85c75acaf7ced8a89b48a6abf1ebdfbbc72669f67095cab0e53940b96fe68a28d497d395074b7aca5e264f6222cf1161ee08c"); }); }); describe('hw_bnb HOLa parameters generation tests', () => { it('Should generate HOLa parameters correctly' , () => { let parameters = getHolaParameters({nonce : "2", gasPrice: "40", gasLimit: 21000, toAddress: "0x34c3e76432d9842cfe2bb6c882dad2b43505baaa", "amount": "0.05", network: Network.Testnet}); expect(parameters.blk[0].toString('hex')) .to.equal("eb028509502f90008252089434c3e76432d9842cfe2bb6c882dad2b43505baaa87b1a2bc2ec5000080618080"); let correctU32Data = [2,5]; let u32cont = 0 for(let u32 of parameters.u32){ expect(u32).to.equal(correctU32Data[u32cont]); u32cont++; } }); }); describe('hw_eth 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("08f61d481f63795ee1e2c61639943611790e020b9fc86875cbe75f53d42f8ffa"); }); }); describe('hw_bnb bep20 transaction building tests', () => { it('Should build unsigned BNB bep20 transaction correctly' , () => { expect(buildBEP20Transaction({nonce: "3", gasPrice: "40", gasLimit: 51250, contractAddress: "0x6ce8da28e2f864420840cf74474eff5fd80e65b8", toAddress: "0x34c3e76432d9842cfe2bb6c882dad2b43505baaa", amount: "0.001", contractDecimals: 18, contractName: "BUSD", network: Network.Testnet}).toString('hex')) .to.equal("f869038509502f900082c832946ce8da28e2f864420840cf74474eff5fd80e65b880b844a9059cbb00000000000000000000000034c3e76432d9842cfe2bb6c882dad2b43505baaa00000000000000000000000000000000000000000000000000038d7ea4c68000618080"); }); let reD = hexToBytes("CBDFDF1C8E2CA101C513EC148D2DEBB56910C6C3811F5B65DC475FA3E2F61E08"); let seD = hexToBytes("78577310650F819271A8A13664838CC99FF583ADD6199A2988DA4EFBD94A7CE9"); it('Should build signed BNB bep20 transaction correctly' , () => { expect(buildBEP20Transaction({nonce: "3", gasPrice: "40", gasLimit: 51250, contractAddress: "0x6ce8da28e2f864420840cf74474eff5fd80e65b8", toAddress: "0x34c3e76432d9842cfe2bb6c882dad2b43505baaa", amount: "0.001", contractDecimals: 18, contractName: "BUSD", signature: {v: 0xe5, r: reD ,s: seD}, network: Network.Testnet}).toString('hex')) .to.equal("f8aa038509502f900082c832946ce8da28e2f864420840cf74474eff5fd80e65b880b844a9059cbb00000000000000000000000034c3e76432d9842cfe2bb6c882dad2b43505baaa00000000000000000000000000000000000000000000000000038d7ea4c6800081e5a0cbdfdf1c8e2ca101c513ec148d2debb56910c6c3811f5b65dc475fa3e2f61e08a078577310650f819271a8a13664838cc99ff583add6199a2988da4efbd94a7ce9"); }); }); describe('hw_bnb bep20 HOLa parameters generation tests', () => { it('Should generate bep20 HOLa parameters correctly' , () => { let parametersERC = getBEP20HolaParameters({nonce: "3", gasPrice: "40", gasLimit: 51250, contractAddress: "0x6ce8da28e2f864420840cf74474eff5fd80e65b8", toAddress: "0x34c3e76432d9842cfe2bb6c882dad2b43505baaa", amount: "0.001", contractDecimals: 18, contractName: "BUSD", network: Network.Testnet}); let correctBlkData = ["f869038509502f900082c832946ce8da28e2f864420840cf74474eff5fd80e65b880b844a9059cbb00000000000000000000000034c3e76432d9842cfe2bb6c882dad2b43505baaa00000000000000000000000000000000000000000000000000038d7ea4c68000618080","0de0b6b3a7640000", "2042555344"]; let blkCont = 0; for(let blk of parametersERC.blk){ expect(blk.toString('hex')).to.equal(correctBlkData[blkCont]); blkCont++; } let correctU32Data = [3,5,27]; let u32cont = 0 for(let u32 of parametersERC.u32){ expect(u32).to.equal(correctU32Data[u32cont]); u32cont++; } }); }); describe('hw_bnb bep20 HOLa parameters generation tests (LINK)', () => { it('Should generate bep20 HOLa parameters correctly (LINK)' , () => { let parametersERC = getBEP20HolaParameters_LINK({nonce: "3", gasPrice: "40", gasLimit: 51250, contractAddress: "0x6ce8da28e2f864420840cf74474eff5fd80e65b8", toAddress: "0x34c3e76432d9842cfe2bb6c882dad2b43505baaa", amount: "0.001", contractDecimals: 18, contractName: "BUSD", network: Network.Testnet}); let correctBlkData = ["9390d75e44775dbe5ffbe2e287f1230b8d3f5bc1d2dd262cc30a75ed95811490"]; let blkCont = 0; for(let blk of parametersERC.blk){ expect(blk.toString('hex')).to.equal(correctBlkData[blkCont]); blkCont++; } }); }); describe('hw_bnb big transaction building tests', () => { it('Should build unsigned BNB transaction with big amount correctly' , () => { expect(buildTransaction({nonce : "2", gasPrice: "40", gasLimit: 21000, toAddress: "0x34c3e76432d9842cfe2bb6c882dad2b43505baaa", "amount": "168137036", network: Network.Testnet}).toString('hex')) .to.equal("ef028509502f90008252089434c3e76432d9842cfe2bb6c882dad2b43505baaa8b8b1466fc3e2c1955b0000080618080"); }); let rD = hexToBytes("1621C1FE56C31F46332E7C5034BB366B648B0B68C0287F1CA956782B1E6DD19C"); let sD = hexToBytes("7287B54A036B901AEA4B531DBEC3CF09692A5714F463904FFA675CB749F19E7B"); it('Should build signed BNB transaction with big amount correctly' , () => { expect(buildTransaction({nonce : "2", gasPrice: "40", gasLimit: 21000, toAddress: "0x34c3e76432d9842cfe2bb6c882dad2b43505baaa", "amount": "168137036", signature: {v:0xe5, r: rD, s: sD}, network: Network.Testnet}).toString('hex')) .to.equal("f870028509502f90008252089434c3e76432d9842cfe2bb6c882dad2b43505baaa8b8b1466fc3e2c1955b000008081e5a01621c1fe56c31f46332e7c5034bb366b648b0b68c0287f1ca956782b1e6dd19ca07287b54a036b901aea4b531dbec3cf09692a5714f463904ffa675cb749f19e7b"); }); }); describe('hw_bnb small transaction building tests', () => { it('Should build unsigned BNB transaction with small amount correctly' , () => { expect(buildTransaction({nonce : "2", gasPrice: "40", gasLimit: 21000, toAddress: "0x34c3e76432d9842cfe2bb6c882dad2b43505baaa", "amount": "0.000000000000000001", network: Network.Testnet}).toString('hex')) .to.equal("e4028509502f90008252089434c3e76432d9842cfe2bb6c882dad2b43505baaa0180618080"); }); let rD = hexToBytes("1621C1FE56C31F46332E7C5034BB366B648B0B68C0287F1CA956782B1E6DD19C"); let sD = hexToBytes("7287B54A036B901AEA4B531DBEC3CF09692A5714F463904FFA675CB749F19E7B"); it('Should build signed BNB transaction with small amount correctly' , () => { expect(buildTransaction({nonce : "2", gasPrice: "40", gasLimit: 21000, toAddress: "0x34c3e76432d9842cfe2bb6c882dad2b43505baaa", "amount": "0.000000000000000001", signature: {v:0xe5, r: rD, s: sD}, network: Network.Testnet}).toString('hex')) .to.equal("f865028509502f90008252089434c3e76432d9842cfe2bb6c882dad2b43505baaa018081e5a01621c1fe56c31f46332e7c5034bb366b648b0b68c0287f1ca956782b1e6dd19ca07287b54a036b901aea4b531dbec3cf09692a5714f463904ffa675cb749f19e7b"); }); }); describe('hw_bnb bep20 big transaction building tests', () => { it('Should build unsigned BNB bep20 transaction with big amount correctly' , () => { expect(buildBEP20Transaction({nonce: "3", gasPrice: "40", gasLimit: 51250, contractAddress: "0x6ce8da28e2f864420840cf74474eff5fd80e65b8", toAddress: "0x34c3e76432d9842cfe2bb6c882dad2b43505baaa", amount: "168137036", contractDecimals: 18, contractName: "BUSD", network: Network.Testnet}).toString('hex')) .to.equal("f869038509502f900082c832946ce8da28e2f864420840cf74474eff5fd80e65b880b844a9059cbb00000000000000000000000034c3e76432d9842cfe2bb6c882dad2b43505baaa0000000000000000000000000000000000000000008b1466fc3e2c1955b00000618080"); }); let reD = hexToBytes("CBDFDF1C8E2CA101C513EC148D2DEBB56910C6C3811F5B65DC475FA3E2F61E08"); let seD = hexToBytes("78577310650F819271A8A13664838CC99FF583ADD6199A2988DA4EFBD94A7CE9"); it('Should build signed BNB bep20 transaction with big amount correctly' , () => { expect(buildBEP20Transaction({nonce: "3", gasPrice: "40", gasLimit: 51250, contractAddress: "0x6ce8da28e2f864420840cf74474eff5fd80e65b8", toAddress: "0x34c3e76432d9842cfe2bb6c882dad2b43505baaa", amount: "168137036", contractDecimals: 18, contractName: "BUSD", signature: {v: 0xe5, r: reD ,s: seD}, network: Network.Testnet}).toString('hex')) .to.equal("f8aa038509502f900082c832946ce8da28e2f864420840cf74474eff5fd80e65b880b844a9059cbb00000000000000000000000034c3e76432d9842cfe2bb6c882dad2b43505baaa0000000000000000000000000000000000000000008b1466fc3e2c1955b0000081e5a0cbdfdf1c8e2ca101c513ec148d2debb56910c6c3811f5b65dc475fa3e2f61e08a078577310650f819271a8a13664838cc99ff583add6199a2988da4efbd94a7ce9"); }); }); describe('hw_bnb bep20 small transaction building tests', () => { it('Should build unsigned BNB bep20 transaction with small amount correctly' , () => { expect(buildBEP20Transaction({nonce: "3", gasPrice: "40", gasLimit: 51250, contractAddress: "0x6ce8da28e2f864420840cf74474eff5fd80e65b8", toAddress: "0x34c3e76432d9842cfe2bb6c882dad2b43505baaa", amount: "0.000000000000000001", contractDecimals: 18, contractName: "BUSD", network: Network.Testnet}).toString('hex')) .to.equal("f869038509502f900082c832946ce8da28e2f864420840cf74474eff5fd80e65b880b844a9059cbb00000000000000000000000034c3e76432d9842cfe2bb6c882dad2b43505baaa0000000000000000000000000000000000000000000000000000000000000001618080"); }); let reD = hexToBytes("CBDFDF1C8E2CA101C513EC148D2DEBB56910C6C3811F5B65DC475FA3E2F61E08"); let seD = hexToBytes("78577310650F819271A8A13664838CC99FF583ADD6199A2988DA4EFBD94A7CE9"); it('Should build signed BNB bep20 transaction with small amount correctly' , () => { expect(buildBEP20Transaction({nonce: "3", gasPrice: "40", gasLimit: 51250, contractAddress: "0x6ce8da28e2f864420840cf74474eff5fd80e65b8", toAddress: "0x34c3e76432d9842cfe2bb6c882dad2b43505baaa", amount: "0.000000000000000001", contractDecimals: 18, contractName: "BUSD", signature: {v: 0xe5, r: reD ,s: seD}, network: Network.Testnet}).toString('hex')) .to.equal("f8aa038509502f900082c832946ce8da28e2f864420840cf74474eff5fd80e65b880b844a9059cbb00000000000000000000000034c3e76432d9842cfe2bb6c882dad2b43505baaa000000000000000000000000000000000000000000000000000000000000000181e5a0cbdfdf1c8e2ca101c513ec148d2debb56910c6c3811f5b65dc475fa3e2f61e08a078577310650f819271a8a13664838cc99ff583add6199a2988da4efbd94a7ce9"); }); }); describe('hw_bnb signature extraction tests', () => { it('Should extract v,r,s from a DER encoded signature' , () => { let sig = hexToBytes("3045022100cbdfdf1c8e2ca101c513ec148d2debb56910c6c3811f5b65dc475fa3e2f61e08022078577310650f819271a8a13664838cc99ff583add6199a2988da4efbd94a7ce9"); let message = hexToBytes("F869038509502F900082C832946CE8DA28E2F864420840CF74474EFF5FD80E65B880B844A9059CBB00000000000000000000000034C3E76432D9842CFE2BB6C882DAD2B43505BAAA00000000000000000000000000000000000000000000000000038D7EA4C68000618080") let signature = extractSignature({signature: sig, transaction: message, address: "0x1223de8a29a4820d3fed6a354f8d37c9efd9d943", net: Network.Testnet}) expect(signature.v) .to.equal(229); expect(signature.r.toString('hex')) .to.equal("cbdfdf1c8e2ca101c513ec148d2debb56910c6c3811f5b65dc475fa3e2f61e08"); expect(signature.s.toString('hex')) .to.equal("78577310650f819271a8a13664838cc99ff583add6199a2988da4efbd94a7ce9"); }); }); function hexToBytes(hex: string) : Buffer{ for (var bytes = [], c = 0; c < hex.length; c += 2) bytes.push(parseInt(hex.substr(c, 2), 16)); return Buffer.from(bytes); }