From 9d943dc4c6dd1ae4a82e8eed1d53ca3c44acb9b6 Mon Sep 17 00:00:00 2001 From: Brad Decker Date: Fri, 21 May 2021 15:07:06 -0500 Subject: [PATCH] implement safer to buffer (#11159) --- shared/modules/buffer-utils.js | 16 +++++ shared/modules/buffer-utils.test.js | 69 +++++++++++++++++++ .../confirm-deploy-contract.component.js | 2 +- .../confirm-transaction-base.component.js | 2 +- 4 files changed, 87 insertions(+), 2 deletions(-) create mode 100644 shared/modules/buffer-utils.js create mode 100644 shared/modules/buffer-utils.test.js diff --git a/shared/modules/buffer-utils.js b/shared/modules/buffer-utils.js new file mode 100644 index 000000000000..38a10b09462a --- /dev/null +++ b/shared/modules/buffer-utils.js @@ -0,0 +1,16 @@ +import { toBuffer as ethUtilToBuffer, isHexString } from 'ethereumjs-util'; + +/** + * Returns a buffer from the provided input, via ethereumjs-util.toBuffer but + * additionally handling non hex strings. This is a failsafe as in most cases + * we should be primarily dealing with hex prefixed strings with this utility + * but we do not want to break the extension for users. + * @param {import('ethereumjs-util').ToBufferInputTypes | string} input + * @returns {Buffer} + */ +export function toBuffer(input) { + if (typeof input === 'string' && isHexString(input) === false) { + return Buffer.from(input); + } + return ethUtilToBuffer(input); +} diff --git a/shared/modules/buffer-utils.test.js b/shared/modules/buffer-utils.test.js new file mode 100644 index 000000000000..2f5290cca5be --- /dev/null +++ b/shared/modules/buffer-utils.test.js @@ -0,0 +1,69 @@ +import { strict as assert } from 'assert'; +import BN from 'bn.js'; +import { toBuffer } from './buffer-utils'; + +describe('buffer utils', function () { + describe('toBuffer', function () { + it('should work with prefixed hex strings', function () { + const result = toBuffer('0xe'); + assert.equal(result.length, 1); + }); + + it('should work with non prefixed hex strings', function () { + const result = toBuffer('e'); + assert.equal(result.length, 1); + }); + + it('should work with weirdly 0x prefixed non-hex strings', function () { + const result = toBuffer('0xtest'); + assert.equal(result.length, 6); + }); + + it('should work with regular strings', function () { + const result = toBuffer('test'); + assert.equal(result.length, 4); + }); + + it('should work with BN', function () { + const result = toBuffer(new BN(100)); + assert.equal(result.length, 1); + }); + + it('should work with Buffer', function () { + const result = toBuffer(Buffer.from('test')); + assert.equal(result.length, 4); + }); + + it('should work with a number', function () { + const result = toBuffer(100); + assert.equal(result.length, 1); + }); + + it('should work with null or undefined', function () { + const result = toBuffer(null); + const result2 = toBuffer(undefined); + assert.equal(result.length, 0); + assert.equal(result2.length, 0); + }); + + it('should work with UInt8Array', function () { + const uint8 = new Uint8Array(2); + const result = toBuffer(uint8); + assert.equal(result.length, 2); + }); + + it('should work with objects that have a toBuffer property', function () { + const result = toBuffer({ + toBuffer: () => Buffer.from('hi'), + }); + assert.equal(result.length, 2); + }); + + it('should work with objects that have a toArray property', function () { + const result = toBuffer({ + toArray: () => ['hi'], + }); + assert.equal(result.length, 1); + }); + }); +}); diff --git a/ui/pages/confirm-deploy-contract/confirm-deploy-contract.component.js b/ui/pages/confirm-deploy-contract/confirm-deploy-contract.component.js index 0c37ca083972..63026bfaa22f 100644 --- a/ui/pages/confirm-deploy-contract/confirm-deploy-contract.component.js +++ b/ui/pages/confirm-deploy-contract/confirm-deploy-contract.component.js @@ -1,7 +1,7 @@ import React, { Component } from 'react'; import PropTypes from 'prop-types'; -import { toBuffer } from 'ethereumjs-util'; import ConfirmTransactionBase from '../confirm-transaction-base'; +import { toBuffer } from '../../../shared/modules/buffer-utils'; export default class ConfirmDeployContract extends Component { static contextTypes = { diff --git a/ui/pages/confirm-transaction-base/confirm-transaction-base.component.js b/ui/pages/confirm-transaction-base/confirm-transaction-base.component.js index 8d523dbe8af8..8a8e9e47e96d 100644 --- a/ui/pages/confirm-transaction-base/confirm-transaction-base.component.js +++ b/ui/pages/confirm-transaction-base/confirm-transaction-base.component.js @@ -1,4 +1,3 @@ -import { toBuffer } from 'ethereumjs-util'; import React, { Component } from 'react'; import PropTypes from 'prop-types'; import { ENVIRONMENT_TYPE_NOTIFICATION } from '../../../shared/constants/app'; @@ -29,6 +28,7 @@ import { } from '../../../shared/constants/transaction'; import { getTransactionTypeTitle } from '../../helpers/utils/transactions.util'; import ErrorMessage from '../../components/ui/error-message'; +import { toBuffer } from '../../../shared/modules/buffer-utils'; export default class ConfirmTransactionBase extends Component { static contextTypes = {