diff --git a/.gitignore b/.gitignore index 0c627e1..bf86125 100644 --- a/.gitignore +++ b/.gitignore @@ -26,6 +26,7 @@ Icon Network Trash Folder Temporary Items .apdisk +.idea ### Node ### @@ -76,4 +77,3 @@ jspm_packages # Yarn Integrity file .yarn-integrity - diff --git a/.travis.yml b/.travis.yml index e4f4915..50140d5 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,7 +1,7 @@ language: node_js node_js: - - "6" - "8" + - "10" env: - CXX=g++-4.8 addons: @@ -21,16 +21,16 @@ matrix: fast_finish: true include: - os: linux - node_js: "6" + node_js: "8" env: CXX=g++-4.8 TEST_SUITE=coveralls - os: linux - node_js: "6" + node_js: "8" env: CXX=g++-4.8 TEST_SUITE=lint - os: linux - node_js: "8" + node_js: "10" env: CXX=g++-4.8 TEST_SUITE=coveralls - os: linux - node_js: "8" + node_js: "10" env: CXX=g++-4.8 TEST_SUITE=lint # - os: linux # node_js: "4" diff --git a/test/fake.js b/test/fake.js index 5da76f8..bb94008 100644 --- a/test/fake.js +++ b/test/fake.js @@ -20,6 +20,7 @@ var txData = { tape('[FakeTransaction]: Basic functions', function (t) { t.test('instantiate with from / create a hash', function (st) { st.plan(3) + var tx = new FakeTransaction(txData) var hash = tx.hash() var cmpHash = Buffer.from('f74b039f6361c4351a99a7c6a10867369fe6701731d85dc07c15671ac1c1b648', 'hex') @@ -74,4 +75,86 @@ tape('[FakeTransaction]: Basic functions', function (t) { () => new FakeTransaction(txData, txOptsInvalid) ) }) + + t.test('should return toCreationAddress', st => { + const tx = new FakeTransaction(txData) + const txNoTo = new FakeTransaction({ ...txData, to: '' }) + st.plan(2) + st.equal(tx.toCreationAddress(), false, 'tx is not "to" creation address') + st.equal(txNoTo.toCreationAddress(), true, 'tx is "to" creation address') + }) + + t.test('should return getChainId', st => { + const tx = new FakeTransaction(txData) + const txWithV = new FakeTransaction({ ...txData, v: '0x28' }) + const txWithChainId = new FakeTransaction({ ...txData, chainId: 4 }) + st.plan(3) + st.equal(tx.getChainId(), 0, 'should return correct chainId') + st.equal(txWithV.getChainId(), 2, 'should return correct chainId') + st.equal(txWithChainId.getChainId(), 4, 'should return correct chainId') + }) + + t.test('should getSenderAddress and getSenderPublicKey', st => { + const tx = new FakeTransaction(txData) + st.plan(2) + st.equal(tx._from.toString('hex'), '7e5f4552091a69125d5dfcb7b8c2659029395bdf', 'this._from is set in FakeTransaction') + st.equal(tx.getSenderAddress().toString('hex'), '7e5f4552091a69125d5dfcb7b8c2659029395bdf', 'should return correct address') + }) + + t.test('should verifySignature', st => { + const tx = new FakeTransaction(txData) + const txWithWrongSignature = new FakeTransaction({ + ...txData, + r: Buffer.from('abcd1558260ac737ea6d800906c6d085a801e5e0f0952bf93978d6fa468fbdff', 'hex') + }) + st.plan(2) + st.true(tx.verifySignature(), 'signature is valid') + st.false(txWithWrongSignature.verifySignature(), 'signature is not valid') + }) + + t.test('should sign', st => { + const tx = new FakeTransaction(txData) + tx.sign(Buffer.from('164122e5d39e9814ca723a749253663bafb07f6af91704d9754c361eb315f0c1', 'hex')) + st.plan(3) + st.equal(tx.r.toString('hex'), 'c10062450d68caa5a688e2b6930f34f8302064afe6e1ba7f6ca459115a31d3b8', 'r should be valid') + st.equal(tx.s.toString('hex'), '31718e6bf821a98d35b0d9cd66ea86f91f420c3c4658f60c607222de925d222a', 's should be valid') + st.equal(tx.v.toString('hex'), '1c', 'v should be valid') + }) + + t.test('should getDataFee', st => { + const tx = new FakeTransaction({...txData, data: '0x00000001'}) + st.plan(1) + st.equal(tx.getDataFee().toString(), '80', 'data fee should be correct') + }) + + t.test('should getBaseFee', st => { + const tx = new FakeTransaction({...txData, data: '0x00000001'}) + st.plan(1) + st.equal(tx.getBaseFee().toString(), '21080', 'base fee should be correct') + }) + + t.test('should getUpfrontCost', st => { + const tx = new FakeTransaction({...txData, gasLimit: '0x6464', gasPrice: '0x2'}) + st.plan(1) + st.equal(tx.getUpfrontCost().toString(), '51400', 'base fee should be correct') + }) + + t.test('should validate', st => { + const tx = new FakeTransaction(txData) + const txWithWrongSignature = new FakeTransaction({ + ...txData, + r: Buffer.from('abcd1558260ac737ea6d800906c6d085a801e5e0f0952bf93978d6fa468fbdff', 'hex') + }) + const txWithLowLimit = new FakeTransaction({ + ...txData, + gasLimit: '0x1' + }) + st.plan(6) + st.true(tx.validate(), 'tx should be valid') + st.false(txWithWrongSignature.validate(), 'tx should be invalid') + st.false(txWithLowLimit.validate(), 'tx should be invalid') + st.equal(tx.validate(true), '', 'tx should return no errors') + st.equal(txWithWrongSignature.validate(true), 'Invalid Signature', 'tx should return correct error') + st.equal(txWithLowLimit.validate(true), 'gas limit is too low. Need at least 21464', 'tx should return correct error') + }) })