New Research: Supply Chain Attack on Axios Pulls Malicious Dependency from npm.Details →
Socket
Book a DemoSign in
Socket

utf8-bytes-swap

Package Overview
Dependencies
Maintainers
1
Versions
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

utf8-bytes-swap

This module converts UTF8 <-> Bytes Array

latest
Source
npmnpm
Version
1.0.0
Version published
Maintainers
1
Created
Source

utf8-bytes-swap

return an utf8 string or bytes array

This module converts UTF8 <-> Bytes Array

Installation

Use the package manager npm to install utf8-bytes-swap.

npm install utf8-bytes-swap

example

Storing strings on the blockchain is expensive. Using string to bytes array significantly reduces transaction cost for storing IPFS CID hashes.

A IPFS cidV1 hash is 59 byte and fit inside a bytes32[2]. Solidity does not accept incomplete byteArrays: fill=true is required for the 5 unused bytes.

const { stringToBytesArray, bytesArrayToString } = require('utf8-bytes-swap');

let cidV1 = 'bafkreicizr7rctuvyki43dtpp7n23go56r6ouw3qxc3iq3r4dtkf2izv6i';
let bytesArray = stringToBytesArray(cidV1, 32, true);
/* 
[
  Uint32Array(32) [
     98,  97, 102, 107, 114, 101, 105,
     99, 105, 122, 114,  55, 114,  99,
    116, 117, 118, 121, 107, 105,  52,
     51, 100, 116, 112, 112,  55, 110,
     50,  51, 103, 111
  ],
  Uint32Array(32) [
     53,  54, 114,  54, 111, 117, 119,  51,
    113, 120,  99,  51, 105, 113,  51, 114,
     52, 100, 116, 107, 102,  50, 105, 122,
    118,  54, 105,   0,   0,   0,   0,   0
  ]
]
*/

let retrievedCidV1 = bytesArrayToString(bytesArray);
// bafkreicizr7rctuvyki43dtpp7n23go56r6ouw3qxc3iq3r4dtkf2izv6i

methods

// default: size to 32, fill partial bytesArray
function stringToBytesArray(str, size=32, fill=true){...};

// returns bytesArray to utf8 string
function bytesArrayToString(bytesArray){...};

stringToBytesArray(< string >)

Return an array of integers from 0 through 255, inclusive, representing the bytes in the unicode string str.

bytesArrayToString(<uint array | hex array>)

Return an unicode str from a hex or integer array of 0 through 255.

license

MIT,

Keywords

utf8

FAQs

Package last updated on 07 May 2021

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