1
0
mirror of https://github.com/kremalicious/metamask-extension.git synced 2024-11-23 18:41:38 +01:00
metamask-extension/shared/modules/buffer-utils.js
2021-05-24 15:16:41 -07: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);
}