conversion.js 678 B

123456789101112131415161718192021222324252627282930
  1. function toBytes32(type, value) {
  2. switch (type) {
  3. case 'bytes32':
  4. return value;
  5. case 'uint256':
  6. return `bytes32(${value})`;
  7. case 'address':
  8. return `bytes32(uint256(uint160(${value})))`;
  9. default:
  10. throw new Error(`Conversion from ${type} to bytes32 not supported`);
  11. }
  12. }
  13. function fromBytes32(type, value) {
  14. switch (type) {
  15. case 'bytes32':
  16. return value;
  17. case 'uint256':
  18. return `uint256(${value})`;
  19. case 'address':
  20. return `address(uint160(uint256(${value})))`;
  21. default:
  22. throw new Error(`Conversion from bytes32 to ${type} not supported`);
  23. }
  24. }
  25. module.exports = {
  26. toBytes32,
  27. fromBytes32,
  28. };