1
0
mirror of https://github.com/kremalicious/metamask-extension.git synced 2024-11-22 01:47:00 +01:00
metamask-extension/shared/modules/buffer-utils.js

18 lines
642 B
JavaScript
Raw Permalink Normal View History

2021-05-21 22:07:06 +02:00
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.
*
2021-05-21 22:07:06 +02:00
* @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);
}