Skip to content
This repository has been archived by the owner on Aug 2, 2022. It is now read-only.

Switching to browser compatible .toArrayLike #691

Merged
merged 3 commits into from
Feb 12, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/PrivateKey.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export class PrivateKey {
}
return new PrivateKey({
type: keyType,
data: privKey.getPrivate().toBuffer(),
data: privKey.getPrivate().toArrayLike(Buffer, 'be', 32),
}, ec);
}

Expand Down
4 changes: 2 additions & 2 deletions src/PublicKey.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
8 changes: 4 additions & 4 deletions src/tests/node.js
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand All @@ -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;
Expand All @@ -55,7 +55,7 @@ const transactWithoutConfig = async () => {
from: 'bob',
to: 'alice',
quantity: '0.0001 SYS',
memo: '',
memo: 'transactWithoutConfig2',
},
}]
});
Expand Down
34 changes: 23 additions & 11 deletions src/tests/node.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,19 @@ describe('Node JS environment', () => {
let transactionSignatures: any;
let failedAsPlanned: boolean;

beforeEach(async () => {
await new Promise((resolve) => setTimeout(resolve, 500));
});

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');
});

Expand All @@ -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');
});
Expand Down
25 changes: 12 additions & 13 deletions src/tests/web.html
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,8 @@

const EXECUTING = 'Executing Test', SUCCESS = 'Success', FAILED = 'Failed';
let resultsLabel, transactionResponse, transactionSignatures, failedAsPlanned;

const waitOneSecond = () => new Promise(resolve => setTimeout(resolve, 1000))

const transactWithConfig = async (config) => await api.transact({

const transactWithConfig = async (config, memo) => await api.transact({
actions: [{
account: 'eosio.token',
name: 'transfer',
Expand All @@ -35,7 +33,7 @@
from: 'bob',
to: 'alice',
quantity: '0.0001 SYS',
memo: '',
memo,
},
}]
}, config);
Expand All @@ -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;
Expand All @@ -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;
Expand All @@ -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;
Expand All @@ -109,7 +107,7 @@
from: 'bob',
to: 'alice',
quantity: '0.0001 SYS',
memo: '',
memo: 'transactWithoutConfig2',
},
}]
});
Expand Down Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -193,7 +191,8 @@
resultsLabel.innerText = EXECUTING;

try {
transactionSignatures = await transactWithoutBroadcast();
transactionSignatures =
await transactWithConfig({ broadcast: false, blocksBehind: 3, expireSeconds: 30 }, 'transactWithoutBroadcast2');
transactionResponse = await broadcastResult(transactionSignatures);
} catch (error) {
resultsLabel.className = 'failed';
Expand Down Expand Up @@ -272,7 +271,7 @@
for (var i = 1; i < buttons.length; i++) {
var button = buttons[i];
button.click();
await waitOneSecond();
await new Promise(resolve => setTimeout(resolve, 150))
}
return;
}
Expand Down