import * as mocha from 'mocha'; import * as chai from 'chai'; import { Coin, Network } from "./types"; import { buildTransaction, extractSignature, getHolaParameters, getHolaParameters_LINK, verifyAddress } from '.'; const expect = chai.expect; describe('hw_theta address verification tests', () => { it('Should verify Theta address correctly' , () => { expect(verifyAddress("0xafcc901b0e8eac02f0e91bd12791888a0df8a252")) .to.equal(true); }); it('Should fail verify Theta address correctly' , () => { expect(verifyAddress("0x34c3e76432d9842cfe2bb6c882dad2b43505baa")) .to.equal(false); }); it('Should fail verify Theta address correctly' , () => { expect(verifyAddress("0x34c3e76432d9842cfe2bb6c882dad2b4350ybaaa")) .to.equal(false); }); }); describe('hw_theta transaction building tests', () => { it('Should build unsigned Theta transaction correctly' , () => { expect(buildTransaction({sequenceNumber: "1", amountTheta: "2", amountTfuel: "0", feeTfuel: "0.000001", fromAddress: "0x59c32D1F9fF59FE524aaA34B703C0aC8Fad4d4d0", toAddress: "0xB91f6163E6f1A60b6d932dcD1C190BD364e0df05", network: Network.Testnet, coin: Coin.Theta}).toString('hex')) .to.equal("02f853c78085e8d4a51000e8e79459c32d1f9ff59fe524aaa34b703c0ac8fad4d4d0cf881bc16d674ec8000085e8d4a510000180e1e094b91f6163e6f1a60b6d932dcd1c190bd364e0df05ca881bc16d674ec8000080"); }); let r = Buffer.from("8e925287a0ea0222371282484dc198a5a21247787a7357a89df11b1520a4f027", "hex"); let s = Buffer.from("02896bb94c7c11832896596d59c6461f9d34308c074e10fe5ea380f8d215bac6", "hex"); it('Should build signed Theta transaction correctly' , () => { expect(buildTransaction({sequenceNumber: "1", amountTheta: "2", amountTfuel: "0", feeTfuel: "0.000001", fromAddress: "0x59c32D1F9fF59FE524aaA34B703C0aC8Fad4d4d0", toAddress: "0xB91f6163E6f1A60b6d932dcD1C190BD364e0df05", network: Network.Testnet, signature: {v:0,r:r,s:s}, coin: Coin.Theta}).toString('hex')) .to.equal("02f897c78085e8d4a51000f86bf8699459c32d1f9ff59fe524aaa34b703c0ac8fad4d4d0cf881bc16d674ec8000085e8d4a5100001b8418e925287a0ea0222371282484dc198a5a21247787a7357a89df11b1520a4f02702896bb94c7c11832896596d59c6461f9d34308c074e10fe5ea380f8d215bac600e1e094b91f6163e6f1a60b6d932dcd1c190bd364e0df05ca881bc16d674ec8000080"); }); }); describe('hw_theta HOLa parameters generation tests', () => { it('Should generate HOLa parameters correctly' , () => { let parameters = getHolaParameters({sequenceNumber: "1", amountTheta: "3", amountTfuel: "0", feeTfuel: "0.000001", fromAddress: "0x59c32D1F9fF59FE524aaA34B703C0aC8Fad4d4d0", toAddress: "0xB91f6163E6f1A60b6d932dcD1C190BD364e0df05", network: Network.Testnet, coin: Coin.Theta}); expect(parameters.blk[0].toString('hex')) .to.equal("f87980808094000000000000000000000000000000000000000080b85e87746573746e657402f853c78085e8d4a51000e8e79459c32d1f9ff59fe524aaa34b703c0ac8fad4d4d0cf8829a2241af62c000085e8d4a510000180e1e094b91f6163e6f1a60b6d932dcd1c190bd364e0df05ca8829a2241af62c000080"); let correctU32Data = [1,6]; let u32cont = 0 for(let u32 of parameters.u32){ expect(u32).to.equal(correctU32Data[u32cont]); u32cont++; } }); }); describe('hw_theta HOLa parameters generation tests (LINK)', () => { it('Should generate HOLa parameters correctly' , () => { let parameters = getHolaParameters_LINK({sequenceNumber: "1", amountTheta: "3", amountTfuel: "0", feeTfuel: "0.000001", fromAddress: "0x59c32D1F9fF59FE524aaA34B703C0aC8Fad4d4d0", toAddress: "0xB91f6163E6f1A60b6d932dcD1C190BD364e0df05", network: Network.Testnet, coin: Coin.Theta}); expect(parameters.blk[0].toString('hex')) .to.equal("a360c9120d989ec5e371a9076be8abd0420274150e5639a5ea51d91209d0dad5"); }); }); describe('hw_theta transaction building tests', () => { it('Should build unsigned TFuel transaction correctly' , () => { expect(buildTransaction({sequenceNumber: "1", amountTheta: "0", amountTfuel: "2", feeTfuel: "0.03", fromAddress: "0x59c32D1F9fF59FE524aaA34B703C0aC8Fad4d4d0", toAddress: "0xB91f6163E6f1A60b6d932dcD1C190BD364e0df05", network: Network.Testnet, coin: Coin.TFuel}).toString('hex')) .to.equal("02f850c980876a94d74f430000e3e29459c32d1f9ff59fe524aaa34b703c0ac8fad4d4d0ca80881c2c023e9e0b00000180e1e094b91f6163e6f1a60b6d932dcd1c190bd364e0df05ca80881bc16d674ec80000"); }); let r = Buffer.from("8e925287a0ea0222371282484dc198a5a21247787a7357a89df11b1520a4f027", "hex"); let s = Buffer.from("02896bb94c7c11832896596d59c6461f9d34308c074e10fe5ea380f8d215bac6", "hex"); it('Should build signed Theta transaction correctly' , () => { expect(buildTransaction({sequenceNumber: "1", amountTheta: "0", amountTfuel: "2", feeTfuel: "0.03", fromAddress: "0x59c32D1F9fF59FE524aaA34B703C0aC8Fad4d4d0", toAddress: "0xB91f6163E6f1A60b6d932dcD1C190BD364e0df05", network: Network.Testnet, signature: {v:0,r:r,s:s}, coin: Coin.TFuel}).toString('hex')) .to.equal("02f894c980876a94d74f430000f866f8649459c32d1f9ff59fe524aaa34b703c0ac8fad4d4d0ca80881c2c023e9e0b000001b8418e925287a0ea0222371282484dc198a5a21247787a7357a89df11b1520a4f02702896bb94c7c11832896596d59c6461f9d34308c074e10fe5ea380f8d215bac600e1e094b91f6163e6f1a60b6d932dcd1c190bd364e0df05ca80881bc16d674ec80000"); }); }); describe('hw_theta TFuel HOLa parameters generation tests', () => { it('Should generate HOLa parameters correctly' , () => { let parameters = getHolaParameters({sequenceNumber: "1", amountTheta: "0", amountTfuel: "58.745", feeTfuel: "0.03", fromAddress: "0x59c32D1F9fF59FE524aaA34B703C0aC8Fad4d4d0", toAddress: "0xB91f6163E6f1A60b6d932dcD1C190BD364e0df05", network: Network.Testnet, coin: Coin.TFuel}); expect(parameters.blk[0].toString('hex')) .to.equal("f87880808094000000000000000000000000000000000000000080b85d87746573746e657402f852c980876a94d74f430000e4e39459c32d1f9ff59fe524aaa34b703c0ac8fad4d4d0cb8089032faabf18c19580000180e2e194b91f6163e6f1a60b6d932dcd1c190bd364e0df05cb8089032f402a4172528000"); let correctU32Data = [3,2]; let u32cont = 0 for(let u32 of parameters.u32){ expect(u32).to.equal(correctU32Data[u32cont]); u32cont++; } }); }); describe('hw_theta TFuel HOLa parameters generation tests (LINK)', () => { it('Should generate HOLa parameters correctly' , () => { let parameters = getHolaParameters_LINK({sequenceNumber: "1", amountTheta: "0", amountTfuel: "58.745", feeTfuel: "0.03", fromAddress: "0x59c32D1F9fF59FE524aaA34B703C0aC8Fad4d4d0", toAddress: "0xB91f6163E6f1A60b6d932dcD1C190BD364e0df05", network: Network.Testnet, coin: Coin.TFuel}); expect(parameters.blk[0].toString('hex')) .to.equal("c7a51f4016c3b7efe3d30bc15655c79b64e4f577e6e989b43e8cd9de4076ab2b"); }); }); describe('hw_theta signature extraction tests', () => { it('Should extract transaction signature correctly' , () => { let sig = Buffer.from("30450221009564b57ccbaa34243931230dc1d32dae01ca5a9a3ce4d35ed106c724970af20202205c02635ca43b82d550882f99c2325bdf08431fd5f74dfbd42e724c023bfb9345", "hex"); let tx = Buffer.from("f87980808094000000000000000000000000000000000000000080b85e87746573746e657402f853c78085e8d4a51000e8e794967161eb9bdb31a94c4762999a4b31333c7e728ecf8829a2241af62c000085e8d4a510000180e1e094b91f6163e6f1a60b6d932dcd1c190bd364e0df05ca8829a2241af62c000080", "hex"); let signature = extractSignature({signature: sig, transaction: tx, address: "0x967161eb9bdb31a94c4762999a4b31333c7e728e"}) expect(signature.v) .to.equal(0); expect(signature.r.toString('hex')) .to.equal("9564b57ccbaa34243931230dc1d32dae01ca5a9a3ce4d35ed106c724970af202"); expect(signature.s.toString('hex')) .to.equal("5c02635ca43b82d550882f99c2325bdf08431fd5f74dfbd42e724c023bfb9345"); }); });