-
Notifications
You must be signed in to change notification settings - Fork 5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
ac35e7d
commit 9d943dc
Showing
4 changed files
with
87 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
}); | ||
}); | ||
}); |
2 changes: 1 addition & 1 deletion
2
ui/pages/confirm-deploy-contract/confirm-deploy-contract.component.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters