import * as mocha from 'mocha'; import * as chai from 'chai'; import { Network } from "./types"; import { buildERC20Transaction, buildTransaction, extractSignature, getERC20HolaParameters, getERC20HolaParameters_LINK, getHolaParameters, getHolaParameters_LINK, verifyAddress } from '.'; import { hexToBytes } from './TransactionHelper'; const expect = chai.expect; describe('hw_op address verification tests', () => { it('Should verify Optimism address correctly' , () => { expect(verifyAddress("0x34c3e76432d9842cfe2bb6c882dad2b43505baaa")) .to.equal(true); }); it('Should fail verify Optimism address correctly' , () => { expect(verifyAddress("0x34c3e76432d9842cfe2bb6c882dad2b43505baa")) .to.equal(false); }); it('Should fail verify Optimism address correctly' , () => { expect(verifyAddress("0x34c3e76432d9842cfe2bb6c882dad2b4350ybaaa")) .to.equal(false); }); }); describe('hw_op transaction building tests', () => { it('Should build unsigned Optimism transaction correctly' , () => { expect(buildTransaction({nonce : "0", gasPrice: "50", gasLimit: 21000, toAddress: "0x34c3e76432d9842cfe2bb6c882dad2b43505baaa", amount: "0.05", network: Network.Testnet}).toString('hex')) .to.equal("02f28201a480850ba43b7400850ba43b74008252089434c3e76432d9842cfe2bb6c882dad2b43505baaa87b1a2bc2ec5000080c0"); }); let rD = hexToBytes("BBB2D06AB85B3AB1AF13560E2A18BB0E36DE6465FEE34C96953D28241F5E6724"); let sD = hexToBytes("57FB79AE09A7DA48693F4D8F861CA96CD71BD629DDEA972A181C5F65B5F56AF9"); it('Should build signed Optimism transaction correctly' , () => { expect(buildTransaction({nonce : "0", gasPrice: "50", gasLimit: 21000, toAddress: "0x34c3e76432d9842cfe2bb6c882dad2b43505baaa", amount: "0.05", signature: {v:0x1C, r: rD, s: sD}, network: Network.Testnet}).toString('hex')) .to.equal("02f8758201a480850ba43b7400850ba43b74008252089434c3e76432d9842cfe2bb6c882dad2b43505baaa87b1a2bc2ec5000080c01ca0bbb2d06ab85b3ab1af13560e2a18bb0e36de6465fee34c96953d28241f5e6724a057fb79ae09a7da48693f4d8f861ca96cd71bd629ddea972a181c5f65b5f56af9"); }); }); describe('hw_op transaction with data building tests', () => { it('Should build unsigned Optimism 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("02f901d20a820152851e0e675849851e0e67584983022e979468b3465833fb72a70ecdf485e0e4c7bd8665fc4580b901a45ae401dc0000000000000000000000000000000000000000000000000000000061e16c0200000000000000000000000000000000000000000000000000000000000000400000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000e404e45aaf000000000000000000000000f4d2888d29d722226fafa5d9b24f9164c092421e000000000000000000000000c02aaa39b223fe8d0a0e5c4f27ead9083c756cc20000000000000000000000000000000000000000000000000000000000002710000000000000000000000000f4fe6141e8f9c614cfa6fd9c8f8b139ae3c249fe00000000000000000000000000000000000000000000001dfa72b234f63c29ee00000000000000000000000000000000000000000000000007df0f7fb8fd1856000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000c0"); }); let rD = hexToBytes("f250e73b59a556a2c4089018b9c85c75acaf7ced8a89b48a6abf1ebdfbbc7266"); let sD = hexToBytes("67095cab0e53940b96fe68a28d497d395074b7aca5e264f6222cf1161ee08c"); it('Should build signed Optimism 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("02f902140a820152851e0e675849851e0e67584983022e979468b3465833fb72a70ecdf485e0e4c7bd8665fc4580b901a45ae401dc0000000000000000000000000000000000000000000000000000000061e16c0200000000000000000000000000000000000000000000000000000000000000400000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000e404e45aaf000000000000000000000000f4d2888d29d722226fafa5d9b24f9164c092421e000000000000000000000000c02aaa39b223fe8d0a0e5c4f27ead9083c756cc20000000000000000000000000000000000000000000000000000000000002710000000000000000000000000f4fe6141e8f9c614cfa6fd9c8f8b139ae3c249fe00000000000000000000000000000000000000000000001dfa72b234f63c29ee00000000000000000000000000000000000000000000000007df0f7fb8fd1856000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000c080a0f250e73b59a556a2c4089018b9c85c75acaf7ced8a89b48a6abf1ebdfbbc72669f67095cab0e53940b96fe68a28d497d395074b7aca5e264f6222cf1161ee08c"); }); }); describe('hw_op HOLa parameters generation tests', () => { it('Should generate HOLa parameters correctly' , () => { let parameters = getHolaParameters({nonce : "0", gasPrice: "50", gasLimit: 21000, toAddress: "0x34c3e76432d9842cfe2bb6c882dad2b43505baaa", amount: "0.05", network: Network.Testnet}); expect(parameters.blk[0].toString('hex')) .to.equal("02f28201a480850ba43b7400850ba43b74008252089434c3e76432d9842cfe2bb6c882dad2b43505baaa87b1a2bc2ec5000080c0"); let correctU32Data = [2,5]; let u32cont = 0 for(let u32 of parameters.u32){ expect(u32).to.equal(correctU32Data[u32cont]); u32cont++; } }); }); describe('hw_op 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("523a887f3db12298d58cc13a13d8820bcca3b167defb597c42853049af51d287"); }); }); describe('hw_op erc20 transaction building tests', () => { it('Should build unsigned Optimism 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("02f8708201a4068505d21dba008505d21dba0082d2db94101848d5c5bbca18e6b4431eedf6b95e9adf82fa80b844a9059cbb00000000000000000000000034c3e76432d9842cfe2bb6c882dad2b43505baaa00000000000000000000000000000000000000000000000003782dace9d90000c0"); }); let reD = hexToBytes("832524266CD28331296739BA9E899625374539256A5D522CF3E3B8D1C799FE99"); let seD = hexToBytes("0CAE1939CC630EB25A144EA2E8C0E54071EBC5E17D33C3F35959B2B32FDACDE2"); it('Should build signed Optimism erc20 transaction correctly' , () => { expect(buildERC20Transaction({nonce: "6", gasPrice: "25", gasLimit: 53979, contractAddress: "0x101848d5c5bbca18e6b4431eedf6b95e9adf82fa", toAddress: "0x34c3e76432d9842cfe2bb6c882dad2b43505baaa", amount: "0.25", contractDecimals: 18, contractName: "WEENUS", signature: {v: 0x25, r: reD ,s: seD}, network: Network.Testnet}).toString('hex')) .to.equal("02f8b38201a4068505d21dba008505d21dba0082d2db94101848d5c5bbca18e6b4431eedf6b95e9adf82fa80b844a9059cbb00000000000000000000000034c3e76432d9842cfe2bb6c882dad2b43505baaa00000000000000000000000000000000000000000000000003782dace9d90000c025a0832524266cd28331296739ba9e899625374539256a5d522cf3e3b8d1c799fe99a00cae1939cc630eb25a144ea2e8c0e54071ebc5e17d33c3f35959b2b32fdacde2"); }); }); describe('hw_op erc20 HOLa parameters generation tests', () => { it('Should generate erc20 HOLa parameters correctly' , () => { let parametersERC = getERC20HolaParameters({nonce: "6", gasPrice: "25", gasLimit: 53979, contractAddress: "0x101848d5c5bbca18e6b4431eedf6b95e9adf82fa", toAddress: "0x34c3e76432d9842cfe2bb6c882dad2b43505baaa", amount: "0.25", contractDecimals: 18, contractName: "WEENUS", network: Network.Testnet}); let correctBlkData = ["02f8708201a4068505d21dba008505d21dba0082d2db94101848d5c5bbca18e6b4431eedf6b95e9adf82fa80b844a9059cbb00000000000000000000000034c3e76432d9842cfe2bb6c882dad2b43505baaa00000000000000000000000000000000000000000000000003782dace9d90000c0","0de0b6b3a7640000", "205745454e5553"]; let blkCont = 0; for(let blk of parametersERC.blk){ expect(blk.toString('hex')).to.equal(correctBlkData[blkCont]); blkCont++; } let correctU32Data = [2,9,27]; let u32cont = 0 for(let u32 of parametersERC.u32){ expect(u32).to.equal(correctU32Data[u32cont]); u32cont++; } }); }); describe('hw_op 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 = ["33a7f7aa00e68d3fc0b231024c65d2b219297474f96b2af28f2abde4e4c159f2"]; let blkCont = 0; for(let blk of parametersERC.blk){ expect(blk.toString('hex')).to.equal(correctBlkData[blkCont]); blkCont++; } }); }); describe('hw_op big transaction building tests', () => { it('Should build unsigned Optimism transaction with big amount correctly' , () => { expect(buildTransaction({nonce : "0", gasPrice: "50", gasLimit: 21000, toAddress: "0x34c3e76432d9842cfe2bb6c882dad2b43505baaa", "amount": "117084665.25", network: Network.Testnet}).toString('hex')) .to.equal("02f68201a480850ba43b7400850ba43b74008252089434c3e76432d9842cfe2bb6c882dad2b43505baaa8b60d9a46be373e25e1d000080c0"); }); let rD = hexToBytes("BBB2D06AB85B3AB1AF13560E2A18BB0E36DE6465FEE34C96953D28241F5E6724"); let sD = hexToBytes("57FB79AE09A7DA48693F4D8F861CA96CD71BD629DDEA972A181C5F65B5F56AF9"); it('Should build signed Optimism transaction with big amount correctly' , () => { expect(buildTransaction({nonce : "0", gasPrice: "50", gasLimit: 21000, toAddress: "0x34c3e76432d9842cfe2bb6c882dad2b43505baaa", "amount": "117084665.25", signature: {v:0x1C, r: rD, s: sD}, network: Network.Testnet}).toString('hex')) .to.equal("02f8798201a480850ba43b7400850ba43b74008252089434c3e76432d9842cfe2bb6c882dad2b43505baaa8b60d9a46be373e25e1d000080c01ca0bbb2d06ab85b3ab1af13560e2a18bb0e36de6465fee34c96953d28241f5e6724a057fb79ae09a7da48693f4d8f861ca96cd71bd629ddea972a181c5f65b5f56af9"); }); }); describe('hw_op small transaction building tests', () => { it('Should build unsigned Optimism transaction with small amount correctly' , () => { expect(buildTransaction({nonce : "0", gasPrice: "50", gasLimit: 21000, toAddress: "0x34c3e76432d9842cfe2bb6c882dad2b43505baaa", "amount": "0.000000000000000001", network: Network.Testnet}).toString('hex')) .to.equal("02eb8201a480850ba43b7400850ba43b74008252089434c3e76432d9842cfe2bb6c882dad2b43505baaa0180c0"); }); let rD = hexToBytes("BBB2D06AB85B3AB1AF13560E2A18BB0E36DE6465FEE34C96953D28241F5E6724"); let sD = hexToBytes("57FB79AE09A7DA48693F4D8F861CA96CD71BD629DDEA972A181C5F65B5F56AF9"); it('Should build signed Optimism transaction with small amount correctly' , () => { expect(buildTransaction({nonce : "0", gasPrice: "50", gasLimit: 21000, toAddress: "0x34c3e76432d9842cfe2bb6c882dad2b43505baaa", "amount": "0.000000000000000001", signature: {v:0x1C, r: rD, s: sD}, network: Network.Testnet}).toString('hex')) .to.equal("02f86e8201a480850ba43b7400850ba43b74008252089434c3e76432d9842cfe2bb6c882dad2b43505baaa0180c01ca0bbb2d06ab85b3ab1af13560e2a18bb0e36de6465fee34c96953d28241f5e6724a057fb79ae09a7da48693f4d8f861ca96cd71bd629ddea972a181c5f65b5f56af9"); }); }); describe('hw_op big transaction building tests', () => { it('Should build unsigned Optimism erc20 transaction with big amount correctly' , () => { expect(buildERC20Transaction({nonce: "6", gasPrice: "25", gasLimit: 53979, contractAddress: "0x101848d5c5bbca18e6b4431eedf6b95e9adf82fa", toAddress: "0x34c3e76432d9842cfe2bb6c882dad2b43505baaa", amount: "117084665.25", contractDecimals: 18, contractName: "WEENUS", network: Network.Testnet}).toString('hex')) .to.equal("02f8708201a4068505d21dba008505d21dba0082d2db94101848d5c5bbca18e6b4431eedf6b95e9adf82fa80b844a9059cbb00000000000000000000000034c3e76432d9842cfe2bb6c882dad2b43505baaa00000000000000000000000000000000000000000060d9a46be373e25e1d0000c0"); }); let reD = hexToBytes("832524266CD28331296739BA9E899625374539256A5D522CF3E3B8D1C799FE99"); let seD = hexToBytes("0CAE1939CC630EB25A144EA2E8C0E54071EBC5E17D33C3F35959B2B32FDACDE2"); it('Should build signed Optimism erc20 transaction with big amount correctly' , () => { expect(buildERC20Transaction({nonce: "6", gasPrice: "25", gasLimit: 53979, contractAddress: "0x101848d5c5bbca18e6b4431eedf6b95e9adf82fa", toAddress: "0x34c3e76432d9842cfe2bb6c882dad2b43505baaa", amount: "117084665.25", contractDecimals: 18, contractName: "WEENUS", signature: {v: 0x25, r: reD ,s: seD}, network: Network.Testnet}).toString('hex')) .to.equal("02f8b38201a4068505d21dba008505d21dba0082d2db94101848d5c5bbca18e6b4431eedf6b95e9adf82fa80b844a9059cbb00000000000000000000000034c3e76432d9842cfe2bb6c882dad2b43505baaa00000000000000000000000000000000000000000060d9a46be373e25e1d0000c025a0832524266cd28331296739ba9e899625374539256a5d522cf3e3b8d1c799fe99a00cae1939cc630eb25a144ea2e8c0e54071ebc5e17d33c3f35959b2b32fdacde2"); }); }); describe('hw_op erc20 small transaction building tests', () => { it('Should build unsigned Optimism erc20 transaction with small amount correctly' , () => { expect(buildERC20Transaction({nonce: "6", gasPrice: "25", gasLimit: 53979, contractAddress: "0x101848d5c5bbca18e6b4431eedf6b95e9adf82fa", toAddress: "0x34c3e76432d9842cfe2bb6c882dad2b43505baaa", amount: "0.000000000000000001", contractName: "WEENUS", contractDecimals: 18, network: Network.Testnet}).toString('hex')) .to.equal("02f8708201a4068505d21dba008505d21dba0082d2db94101848d5c5bbca18e6b4431eedf6b95e9adf82fa80b844a9059cbb00000000000000000000000034c3e76432d9842cfe2bb6c882dad2b43505baaa0000000000000000000000000000000000000000000000000000000000000001c0"); }); let reD = hexToBytes("832524266CD28331296739BA9E899625374539256A5D522CF3E3B8D1C799FE99"); let seD = hexToBytes("0CAE1939CC630EB25A144EA2E8C0E54071EBC5E17D33C3F35959B2B32FDACDE2"); it('Should build signed Optimism erc20 transaction with small amount correctly' , () => { expect(buildERC20Transaction({nonce: "6", gasPrice: "25", gasLimit: 53979, contractAddress: "0x101848d5c5bbca18e6b4431eedf6b95e9adf82fa", toAddress: "0x34c3e76432d9842cfe2bb6c882dad2b43505baaa", amount: "0.000000000000000001", contractName: "WEENUS", contractDecimals: 18, signature: {v: 0x25, r: reD ,s: seD}, network: Network.Testnet}).toString('hex')) .to.equal("02f8b38201a4068505d21dba008505d21dba0082d2db94101848d5c5bbca18e6b4431eedf6b95e9adf82fa80b844a9059cbb00000000000000000000000034c3e76432d9842cfe2bb6c882dad2b43505baaa0000000000000000000000000000000000000000000000000000000000000001c025a0832524266cd28331296739ba9e899625374539256a5d522cf3e3b8d1c799fe99a00cae1939cc630eb25a144ea2e8c0e54071ebc5e17d33c3f35959b2b32fdacde2"); }); }); describe('hw_op 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(0); expect(signature.r.toString('hex')) .to.equal("cbdfdf1c8e2ca101c513ec148d2debb56910c6c3811f5b65dc475fa3e2f61e08"); expect(signature.s.toString('hex')) .to.equal("78577310650f819271a8a13664838cc99ff583add6199a2988da4efbd94a7ce9"); }); });