From 6b0ea0405439c4eab4d7c119a3d3457255c512a5 Mon Sep 17 00:00:00 2001 From: Bradley Hart Date: Wed, 12 Feb 2020 08:21:25 -0500 Subject: [PATCH 1/3] Switching to browser compatible .toArrayLike Adjusting tests to avoid duplicate transactions --- src/PrivateKey.ts | 2 +- src/PublicKey.ts | 4 ++-- src/tests/node.test.ts | 2 +- src/tests/web.html | 5 +++-- 4 files changed, 7 insertions(+), 6 deletions(-) diff --git a/src/PrivateKey.ts b/src/PrivateKey.ts index 34346223c..d68923cf2 100644 --- a/src/PrivateKey.ts +++ b/src/PrivateKey.ts @@ -18,7 +18,7 @@ export class PrivateKey { } return new PrivateKey({ type: keyType, - data: privKey.getPrivate().toBuffer(), + data: privKey.getPrivate().toArrayLike(Buffer, 'be', 32), }, ec); } diff --git a/src/PublicKey.ts b/src/PublicKey.ts index d21882171..7764fef0d 100644 --- a/src/PublicKey.ts +++ b/src/PublicKey.ts @@ -22,8 +22,8 @@ export class PublicKey { /** Instantiate public key from an `elliptic`-format public key */ public static fromElliptic(publicKey: EC.KeyPair, keyType: KeyType, ec?: EC): PublicKey { - const x = publicKey.getPublic().getX().toArray(); - const y = publicKey.getPublic().getY().toArray(); + const x = publicKey.getPublic().getX().toArray('be', 32); + const y = publicKey.getPublic().getY().toArray('be', 32); if (!ec) { ec = constructElliptic(keyType); } diff --git a/src/tests/node.test.ts b/src/tests/node.test.ts index a128d360e..933cd331b 100644 --- a/src/tests/node.test.ts +++ b/src/tests/node.test.ts @@ -6,7 +6,7 @@ describe('Node JS environment', () => { let failedAsPlanned: boolean; beforeEach(async () => { - await new Promise((resolve) => setTimeout(resolve, 500)); + await new Promise((resolve) => setTimeout(resolve, 1500)); }); it('transacts with configuration object containing blocksBehind', async () => { diff --git a/src/tests/web.html b/src/tests/web.html index 338d0e7a2..5de45fe84 100644 --- a/src/tests/web.html +++ b/src/tests/web.html @@ -21,7 +21,7 @@ const EXECUTING = 'Executing Test', SUCCESS = 'Success', FAILED = 'Failed'; let resultsLabel, transactionResponse, transactionSignatures, failedAsPlanned; - const waitOneSecond = () => new Promise(resolve => setTimeout(resolve, 1000)) + const waitOneSecond = () => new Promise(resolve => setTimeout(resolve, 1500)) const transactWithConfig = async (config) => await api.transact({ actions: [{ @@ -193,7 +193,8 @@ resultsLabel.innerText = EXECUTING; try { - transactionSignatures = await transactWithoutBroadcast(); + transactionSignatures = + await transactWithConfig({ broadcast: false, blocksBehind: 3, expireSeconds: 30 }); transactionResponse = await broadcastResult(transactionSignatures); } catch (error) { resultsLabel.className = 'failed'; From cad435dc9310c3dd6676aea7eaa6476d9cb7ce40 Mon Sep 17 00:00:00 2001 From: Bradley Hart Date: Wed, 12 Feb 2020 09:41:48 -0500 Subject: [PATCH 2/3] Adjusting tests to not need delay --- src/tests/node.js | 8 ++++---- src/tests/node.test.ts | 34 +++++++++++++++++++++++----------- src/tests/web.html | 23 ++++++++++------------- 3 files changed, 37 insertions(+), 28 deletions(-) diff --git a/src/tests/node.js b/src/tests/node.js index e8ceb0634..c54ccf278 100644 --- a/src/tests/node.js +++ b/src/tests/node.js @@ -15,7 +15,7 @@ const rpc = new JsonRpc('http://localhost:8888', { fetch }); const signatureProvider = new JsSignatureProvider([privateKey]); const api = new Api({ rpc, signatureProvider, textDecoder: new TextDecoder(), textEncoder: new TextEncoder() }); -const transactWithConfig = async (config) => await api.transact({ +const transactWithConfig = async (config, memo) => await api.transact({ actions: [{ account: 'eosio.token', name: 'transfer', @@ -27,13 +27,13 @@ const transactWithConfig = async (config) => await api.transact({ from: 'bob', to: 'alice', quantity: '0.0001 SYS', - memo: '', + memo, }, }] }, config); const transactWithoutConfig = async () => { - const transactionResponse = await transactWithConfig({ blocksBehind: 3, expireSeconds: 30}); + const transactionResponse = await transactWithConfig({ blocksBehind: 3, expireSeconds: 30}, 'transactWithoutConfig'); const blockInfo = await rpc.get_block(transactionResponse.processed.block_num - 3); const currentDate = new Date(); const timePlusTen = currentDate.getTime() + 10000; @@ -55,7 +55,7 @@ const transactWithoutConfig = async () => { from: 'bob', to: 'alice', quantity: '0.0001 SYS', - memo: '', + memo: 'transactWithoutConfig2', }, }] }); diff --git a/src/tests/node.test.ts b/src/tests/node.test.ts index 933cd331b..542903e64 100644 --- a/src/tests/node.test.ts +++ b/src/tests/node.test.ts @@ -5,17 +5,19 @@ describe('Node JS environment', () => { let transactionSignatures: any; let failedAsPlanned: boolean; - beforeEach(async () => { - await new Promise((resolve) => setTimeout(resolve, 1500)); - }); - it('transacts with configuration object containing blocksBehind', async () => { - transactionResponse = await tests.transactWithConfig({ blocksBehind: 3, expireSeconds: 30 }); + transactionResponse = await tests.transactWithConfig({ + blocksBehind: 3, + expireSeconds: 30 + }, 'transactWithBlocksBehind'); expect(Object.keys(transactionResponse)).toContain('transaction_id'); }); it('transacts with configuration object containing useLastIrreversible', async () => { - transactionResponse = await tests.transactWithConfig({ useLastIrreversible: true, expireSeconds: 30 }); + transactionResponse = await tests.transactWithConfig({ + useLastIrreversible: true, + expireSeconds: 30 + }, 'transactWithUseLastIrreversible'); expect(Object.keys(transactionResponse)).toContain('transaction_id'); }); @@ -25,20 +27,30 @@ describe('Node JS environment', () => { }, 10000); it('transacts with compressed transaction', async () => { - transactionResponse = await tests.transactWithConfig({ blocksBehind: 3, expireSeconds: 30, compression: true }); + transactionResponse = await tests.transactWithConfig({ + blocksBehind: 3, + expireSeconds: 30, + compression: true + }, 'transactWithCompression'); expect(Object.keys(transactionResponse)).toContain('transaction_id'); }); it('transacts without broadcasting, returning signatures and packed transaction', async () => { - transactionSignatures = - await tests.transactWithConfig({ broadcast: false, blocksBehind: 3, expireSeconds: 30 }); + transactionSignatures = await tests.transactWithConfig({ + broadcast: false, + blocksBehind: 3, + expireSeconds: 30 + }, 'transactWithoutBroadcast'); expect(Object.keys(transactionSignatures)).toContain('signatures'); expect(Object.keys(transactionSignatures)).toContain('serializedTransaction'); }); it('broadcasts packed transaction, given valid signatures', async () => { - transactionSignatures = - await tests.transactWithConfig({ broadcast: false, blocksBehind: 3, expireSeconds: 30 }); + transactionSignatures = await tests.transactWithConfig({ + broadcast: false, + blocksBehind: 3, + expireSeconds: 30 + }, 'transactWithoutBroadcast2'); transactionResponse = await tests.broadcastResult(transactionSignatures); expect(Object.keys(transactionResponse)).toContain('transaction_id'); }); diff --git a/src/tests/web.html b/src/tests/web.html index 5de45fe84..f88014c25 100644 --- a/src/tests/web.html +++ b/src/tests/web.html @@ -20,10 +20,8 @@ const EXECUTING = 'Executing Test', SUCCESS = 'Success', FAILED = 'Failed'; let resultsLabel, transactionResponse, transactionSignatures, failedAsPlanned; - - const waitOneSecond = () => new Promise(resolve => setTimeout(resolve, 1500)) - - const transactWithConfig = async (config) => await api.transact({ + + const transactWithConfig = async (config, memo) => await api.transact({ actions: [{ account: 'eosio.token', name: 'transfer', @@ -35,7 +33,7 @@ from: 'bob', to: 'alice', quantity: '0.0001 SYS', - memo: '', + memo, }, }] }, config); @@ -45,7 +43,7 @@ resultsLabel.innerText = EXECUTING; try { - transactionResponse = await transactWithConfig({ blocksBehind: 3, expireSeconds: 30 }); + transactionResponse = await transactWithConfig({ blocksBehind: 3, expireSeconds: 30 }, 'transactWithBlocksBehind'); } catch (error) { resultsLabel.className = 'failed'; resultsLabel.innerText = FAILED; @@ -68,7 +66,7 @@ resultsLabel.innerText = EXECUTING; try { - transactionResponse = await transactWithConfig({ useLastIrreversible: true, expireSeconds: 30 }); + transactionResponse = await transactWithConfig({ useLastIrreversible: true, expireSeconds: 30 }, 'transactWithUseLastIrreversible'); } catch (error) { resultsLabel.className = 'failed'; resultsLabel.innerText = FAILED; @@ -87,7 +85,7 @@ } const transactWithoutConfig = async () => { - const transactionResponse = await transactWithConfig({ blocksBehind: 3, expireSeconds: 30 }); + const transactionResponse = await transactWithConfig({ blocksBehind: 3, expireSeconds: 30 }, 'transactWithoutConfig'); const blockInfo = await rpc.get_block(transactionResponse.processed.block_num - 3); const currentDate = new Date(); const timePlusTen = currentDate.getTime() + 10000; @@ -109,7 +107,7 @@ from: 'bob', to: 'alice', quantity: '0.0001 SYS', - memo: '', + memo: 'transactWithoutConfig2', }, }] }); @@ -144,7 +142,7 @@ try { transactionResponse = - await transactWithConfig({ blocksBehind: 3, expireSeconds: 30, compression: true }); + await transactWithConfig({ blocksBehind: 3, expireSeconds: 30, compression: true }, 'transactWithCompression'); } catch (error) { resultsLabel.className = 'failed'; resultsLabel.innerText = FAILED; @@ -168,7 +166,7 @@ try { transactionSignatures = - await transactWithConfig({ broadcast: false, blocksBehind: 3, expireSeconds: 30 }); + await transactWithConfig({ broadcast: false, blocksBehind: 3, expireSeconds: 30 }, 'transactWithoutBroadcast'); } catch (error) { resultsLabel.className = 'failed'; resultsLabel.innerText = FAILED; @@ -194,7 +192,7 @@ try { transactionSignatures = - await transactWithConfig({ broadcast: false, blocksBehind: 3, expireSeconds: 30 }); + await transactWithConfig({ broadcast: false, blocksBehind: 3, expireSeconds: 30 }, 'transactWithoutBroadcast2'); transactionResponse = await broadcastResult(transactionSignatures); } catch (error) { resultsLabel.className = 'failed'; @@ -273,7 +271,6 @@ for (var i = 1; i < buttons.length; i++) { var button = buttons[i]; button.click(); - await waitOneSecond(); } return; } From f66a7e2da791d95f0b8b1f9fea94b58aedc0173d Mon Sep 17 00:00:00 2001 From: Bradley Hart Date: Wed, 12 Feb 2020 10:12:23 -0500 Subject: [PATCH 3/3] Re-adding a short delay to avoid conflicts between tests --- src/tests/web.html | 1 + 1 file changed, 1 insertion(+) diff --git a/src/tests/web.html b/src/tests/web.html index f88014c25..7d35e952d 100644 --- a/src/tests/web.html +++ b/src/tests/web.html @@ -271,6 +271,7 @@ for (var i = 1; i < buttons.length; i++) { var button = buttons[i]; button.click(); + await new Promise(resolve => setTimeout(resolve, 150)) } return; }