1
0
mirror of https://github.com/kremalicious/metamask-extension.git synced 2024-10-22 19:26:13 +02:00
metamask-extension/shared/modules/buffer-utils.js
2021-05-21 15:07:06 -05:00

17 lines
639 B
JavaScript

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