🚀 DAY 5 OF LAUNCH WEEK: Introducing Socket Firewall Enterprise.Learn more →
Socket
Book a DemoInstallSign in
Socket

@juanelas/solidity-types-from-json

Package Overview
Dependencies
Maintainers
0
Versions
2
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@juanelas/solidity-types-from-json

Compute solidity types from an arbitrary JS object so that we can use eth_signTypedData (EIP-712) with arbitrary objects

latest
Source
npmnpm
Version
1.0.1
Version published
Maintainers
0
Created
Source

License: EUPL_1.2 Contributor Covenant JavaScript Style Guide Node.js CI Coverage Status

@juanelas/solidity-types-from-json

Compute solidity types from an arbitrary JS object so that we can use eth_signTypedData (EIP-712) with arbitrary objects

Install and use

@juanelas/solidity-types-from-json can be imported to your project with npm:

npm install @juanelas/solidity-types-from-json

Then either require (Node.js CJS):

const solidityTypesFromJson = require('@juanelas/solidity-types-from-json')

or import (JavaScript ES module):

import * as solidityTypesFromJson from '@juanelas/solidity-types-from-json'

The appropriate version for browser or node should be automatically chosen when importing. However, if your bundler does not import the appropriate module version (node esm, node cjs or browser esm), you can force it to use a specific one by just importing one of the followings:

  • @juanelas/solidity-types-from-json/dist/cjs/index.node: for Node.js CJS module
  • @juanelas/solidity-types-from-json/dist/esm/index.node: for Node.js ESM module
  • @juanelas/solidity-types-from-json/dist/esm/index.browser: for browser ESM module

If you are coding TypeScript, types will not be automatically detected when using the specific versions. You can easily get the types in by creating and importing to your TS project a new types declaration file solidity-types-from-json.d.ts with the following line:

declare module '@juanelas/solidity-types-from-json/dist/esm/index.browser' // use the specific module file you are importing

You can also download browser ESM, IIFE and UMD bundles directly from the releases' page and manually add them to your project.

Usage example

const exampleObject = {
  name: 'Alice',
  age: 30,
  isActive: true,
  ethAddress: '0xe688b84b23f322a994A53dbF8E15FA82CDB71127',
  address: {
    street: '123 Main St',
    city: 'Wonderland',
    arr: [2, 3],
    obj: {
      a: 1,
      b: false
    }
  },
  scores: [100, 200, 300],
  preferences: [
    { key: 'color', value: 'blue' },
    { key: 'size', value: 'medium' }
  ],
  arrAlt: [
    { a: 4, b: true }
  ]
}
const {mainType, types} = solidityTypesFromJson.jsonToSolidityTypes(exampleObject, { mainTypeName: 'JWT' })

// mainType = 'JWT'
// types = {
//   Obj: [{ name: 'a', type: 'uint256' }, { name: 'b', type: 'bool' }],
//   Address: [
//     { name: 'street', type: 'string' },
//     { name: 'city', type: 'string' },
//     { name: 'arr', type: 'uint256[]' },
//     { name: 'obj', type: 'Obj' }
//   ],
//   Preference: [
//     { name: 'key', type: 'string' },
//     { name: 'value', type: 'string' }
//   ],
//   ArrAltItem: [{ name: 'a', type: 'uint256' }, { name: 'b', type: 'bool' }],
//   JWT: [
//     { name: 'name', type: 'string' },
//     { name: 'age', type: 'uint256' },
//     { name: 'isActive', type: 'bool' },
//     { name: 'ethAddress', type: 'address' },
//     { name: 'address', type: 'Address' },
//     { name: 'scores', type: 'uint256[]' },
//     { name: 'preferences', type: 'Preference[]' },
//     { name: 'arrAlt', type: 'ArrAltItem[]' }
//   ]
// }

API reference documentation

Check the API

Keywords

EIP-712

FAQs

Package last updated on 20 Jul 2024

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts