Skip to content

Commit

Permalink
Merge branch 'enhancement/update-provider-resolver-to-latest-EIP1193-…
Browse files Browse the repository at this point in the history
…spec' of github.com:ethereum/web3.js into enhancement/update-provider-resolver-to-latest-EIP1193-spec
  • Loading branch information
Samuel Furter committed Feb 8, 2019
2 parents 4efc7c5 + 10f1803 commit a656f31
Show file tree
Hide file tree
Showing 10 changed files with 63 additions and 15 deletions.
13 changes: 11 additions & 2 deletions docs/web3-utils.rst
Original file line number Diff line number Diff line change
Expand Up @@ -833,14 +833,16 @@ asciiToHex
web3.utils.fromAscii(string) // ALIAS, deprecated
Returns the HEX representation of a given ASCII string.
Returns the HEX representation of a given ASCII string. If you would like to transform an ASCII string into a valid
``bytes4``, ``bytes8`` etc. value then please pass the correct length as the second parameter.


----------
Parameters
----------

1. ``string`` - ``String``: A ASCII string to convert to a HEX string.
2. ``length`` - ``Number``: The length of the returned hex string. The default size is ``32`` e.g.: ``bytes32``.

-------
Returns
Expand All @@ -855,8 +857,15 @@ Example
.. code-block:: javascript
web3.utils.asciiToHex('I have 100!');
> "0x4920686176652031303021"
> "0x4920686176652031303021000000000000000000000000000000000000000000"
// transforming to a bytes4 value:
web3.utils.asciiToHex('yes', 4);
// transforming to a bytes8 value:
web3.utils.asciiToHex('yes', 8);
//etc.
------------------------------------------------------------------------------

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ export default class AbstractSubscription extends EventEmitter {
super();
this.type = type;
this.method = method;
this.options = options || {};
this.options = options || null;
this.utils = utils;
this.formatters = formatters;
this.moduleInstance = moduleInstance;
Expand Down Expand Up @@ -82,9 +82,14 @@ export default class AbstractSubscription extends EventEmitter {
*/
subscribe(callback) {
this.beforeSubscription(this.moduleInstance);
let subscriptionParameters = [];

if (this.options !== null) {
subscriptionParameters = [this.options];
}

this.moduleInstance.currentProvider
.subscribe(this.type, this.method, [this.options])
.subscribe(this.type, this.method, subscriptionParameters)
.then((subscriptionId) => {
this.id = subscriptionId;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,11 @@ describe('AbstractSubscriptionTest', () => {

expect(method).toEqual(abstractSubscription.method);

expect(parameters).toEqual([abstractSubscription.options]);
if (abstractSubscription.options !== null) {
expect(parameters).toEqual([abstractSubscription.options]);
} else {
expect(parameters).toEqual([]);
}

return Promise.resolve('MY_ID');
});
Expand Down Expand Up @@ -81,6 +85,32 @@ describe('AbstractSubscriptionTest', () => {
});
});

it('calls subscribe with options set to null and returns a Subscription object', (done) => {
moduleInstanceMock.currentProvider.on = jest.fn((id, callback) => {
expect(id).toEqual('MY_ID');

callback({result: 'SUBSCRIPTION_ITEM'});
});

const callback = jest.fn((error, response) => {
expect(abstractSubscription.id).toEqual('MY_ID');

expect(error).toEqual(false);

expect(response).toEqual('SUBSCRIPTION_ITEM');

done();
});

abstractSubscription.options = null;

const subscription = abstractSubscription.subscribe(callback);

subscription.on('data', (data) => {
expect(data).toEqual('SUBSCRIPTION_ITEM');
});
});

it('calls unsubscribe and returns with a resolved promise', async () => {
moduleInstanceMock.currentProvider.unsubscribe = jest.fn((id, type) => {
expect(id).toEqual('ID');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ describe('NewHeadsSubscriptionTest', () => {

expect(newHeadsSubscription.type).toEqual('eth_subscribe');

expect(newHeadsSubscription.options).toEqual({});
expect(newHeadsSubscription.options).toEqual(null);

expect(newHeadsSubscription.utils).toEqual(Utils);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ describe('NewPendingTransactionsSubscriptionTest', () => {

expect(newPendingTransactionsSubscription.type).toEqual('eth_subscribe');

expect(newPendingTransactionsSubscription.options).toEqual({});
expect(newPendingTransactionsSubscription.options).toEqual(null);

expect(newPendingTransactionsSubscription.utils).toEqual({});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ describe('SyncingSubscriptionTest', () => {

expect(syncingSubscription.type).toEqual('eth_subscribe');

expect(syncingSubscription.options).toEqual({});
expect(syncingSubscription.options).toEqual(null);

expect(syncingSubscription.utils).toEqual({});

Expand Down
7 changes: 4 additions & 3 deletions packages/web3-utils/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -126,19 +126,20 @@ export const hexToAscii = (hex) => {
* @method asciiToHex
*
* @param {String} str
* @param {Number} length
*
* @returns {String} hex representation of input string
*/
export const asciiToHex = (str) => {
if (!str) return '0x00';
export const asciiToHex = (str, length = 32) => {
let hex = '';

for (let i = 0; i < str.length; i++) {
const code = str.charCodeAt(i);
const n = code.toString(16);
hex += n.length < 2 ? `0${n}` : n;
}

return `0x${hex}`;
return '0x' + utils.rightPad(hex, length * 2);
};

/**
Expand Down
4 changes: 2 additions & 2 deletions packages/web3-utils/tests/src/UtilsTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ import {
describe('UtilsTest', () => {
it('calls asciiToHex and returns the expected results', () => {
const tests = [
{value: 'myString', expected: '0x6d79537472696e67'},
{value: 'myString\u0000', expected: '0x6d79537472696e6700'},
{value: 'myString', expected: '0x6d79537472696e67000000000000000000000000000000000000000000000000'},
{value: 'myString\u0000', expected: '0x6d79537472696e67000000000000000000000000000000000000000000000000'},
{
value:
'\u0003\u0000\u0000\u00005èÆÕL]\u0012|Î¾ž\u001a7«›\u00052\u0011(ЗY\n<\u0010\u0000\u0000\u0000\u0000\u0000\u0000e!ßd/ñõì\f:z¦Î¦±ç·÷Í¢Ëß\u00076*…\bŽ—ñžùC1ÉUÀé2\u001aӆBŒ',
Expand Down
4 changes: 2 additions & 2 deletions packages/web3-utils/types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ export function toTwosComplement(value: number | string | BN): string;
export function isAddress(address: string): boolean;
export function isHex(hex: Hex): boolean;
export function isHexStrict(hex: Hex): boolean;
export function asciiToHex(string: string): string;
export function asciiToHex(string: string, length?: number): string;
export function hexToAscii(string: string): string;
export function toAscii(string: string): string;
export function bytesToHex(bytes: number[]): string;
Expand Down Expand Up @@ -130,7 +130,7 @@ export interface Utils {
isAddress(address: string): boolean;
isHex(hex: Hex): boolean;
isHexStrict(hex: Hex): boolean;
asciiToHex(string: string): string;
asciiToHex(string: string, length?: number): string;
hexToAscii(string: string): string;
toAscii(string: string): string;
bytesToHex(bytes: number[]): string;
Expand Down
3 changes: 3 additions & 0 deletions packages/web3-utils/types/tests/ascii-to-hex-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ import {asciiToHex, BN} from 'web3-utils';
// $ExpectType string
asciiToHex('I have 100!');

// $ExpectType string
asciiToHex('I have 100!', 32);

// $ExpectError
asciiToHex(345);
// $ExpectError
Expand Down

0 comments on commit a656f31

Please sign in to comment.