diff --git a/docs/web3-utils.rst b/docs/web3-utils.rst index 67054d57d8f..77faac7f85e 100644 --- a/docs/web3-utils.rst +++ b/docs/web3-utils.rst @@ -833,7 +833,8 @@ 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. ---------- @@ -841,6 +842,7 @@ 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 @@ -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. ------------------------------------------------------------------------------ diff --git a/packages/web3-core-subscriptions/lib/subscriptions/AbstractSubscription.js b/packages/web3-core-subscriptions/lib/subscriptions/AbstractSubscription.js index f77a631ff73..e46d8fa9379 100644 --- a/packages/web3-core-subscriptions/lib/subscriptions/AbstractSubscription.js +++ b/packages/web3-core-subscriptions/lib/subscriptions/AbstractSubscription.js @@ -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; @@ -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; diff --git a/packages/web3-core-subscriptions/tests/lib/subscriptions/AbstractSubscriptionTest.js b/packages/web3-core-subscriptions/tests/lib/subscriptions/AbstractSubscriptionTest.js index dc9298cfff8..a87097c7381 100644 --- a/packages/web3-core-subscriptions/tests/lib/subscriptions/AbstractSubscriptionTest.js +++ b/packages/web3-core-subscriptions/tests/lib/subscriptions/AbstractSubscriptionTest.js @@ -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'); }); @@ -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'); diff --git a/packages/web3-core-subscriptions/tests/src/subscriptions/eth/NewHeadsSubscriptionTest.js b/packages/web3-core-subscriptions/tests/src/subscriptions/eth/NewHeadsSubscriptionTest.js index e7d2f75d74e..70e6971017e 100644 --- a/packages/web3-core-subscriptions/tests/src/subscriptions/eth/NewHeadsSubscriptionTest.js +++ b/packages/web3-core-subscriptions/tests/src/subscriptions/eth/NewHeadsSubscriptionTest.js @@ -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); diff --git a/packages/web3-core-subscriptions/tests/src/subscriptions/eth/NewPendingTransactionsSubscriptionTest.js b/packages/web3-core-subscriptions/tests/src/subscriptions/eth/NewPendingTransactionsSubscriptionTest.js index 0a9ab19b1b6..5385a97c78d 100644 --- a/packages/web3-core-subscriptions/tests/src/subscriptions/eth/NewPendingTransactionsSubscriptionTest.js +++ b/packages/web3-core-subscriptions/tests/src/subscriptions/eth/NewPendingTransactionsSubscriptionTest.js @@ -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({}); diff --git a/packages/web3-core-subscriptions/tests/src/subscriptions/eth/SyncingSubscriptionTest.js b/packages/web3-core-subscriptions/tests/src/subscriptions/eth/SyncingSubscriptionTest.js index 5b52f42087c..9a0b63fa272 100644 --- a/packages/web3-core-subscriptions/tests/src/subscriptions/eth/SyncingSubscriptionTest.js +++ b/packages/web3-core-subscriptions/tests/src/subscriptions/eth/SyncingSubscriptionTest.js @@ -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({}); diff --git a/packages/web3-utils/src/index.js b/packages/web3-utils/src/index.js index 7865e1f80c3..4c1380f084f 100644 --- a/packages/web3-utils/src/index.js +++ b/packages/web3-utils/src/index.js @@ -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); }; /** diff --git a/packages/web3-utils/tests/src/UtilsTest.js b/packages/web3-utils/tests/src/UtilsTest.js index b604ba923cd..0237568fa8c 100644 --- a/packages/web3-utils/tests/src/UtilsTest.js +++ b/packages/web3-utils/tests/src/UtilsTest.js @@ -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Œ', diff --git a/packages/web3-utils/types/index.d.ts b/packages/web3-utils/types/index.d.ts index d767a5a6603..1bdc8986491 100644 --- a/packages/web3-utils/types/index.d.ts +++ b/packages/web3-utils/types/index.d.ts @@ -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; @@ -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; diff --git a/packages/web3-utils/types/tests/ascii-to-hex-test.ts b/packages/web3-utils/types/tests/ascii-to-hex-test.ts index db98431373b..3e2a4537394 100644 --- a/packages/web3-utils/types/tests/ascii-to-hex-test.ts +++ b/packages/web3-utils/types/tests/ascii-to-hex-test.ts @@ -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