Coder Social home page Coder Social logo

multiformats / js-multibase Goto Github PK

View Code? Open in Web Editor NEW
23.0 17.0 24.0 1.47 MB

JavaScript implementation of the multibase specification

Home Page: https://multiformats.github.io/js-multibase/

License: MIT License

JavaScript 100.00%
multibase encoding decoding rfc4648 js-multibase

js-multibase's Introduction

⛔️ DEPRECATED: This module has been superseded by the multiformats module

js-multibase

pl project irc codecov GitHub Workflow Status

JavaScript implementation of the multibase specification

Lead Maintainer

Hugo Dias

Table of Contents

Install

NPM

$ npm install --save multibase

Browser through <script> tag

Loading this module through a script tag will make the Multibase obj available in the global namespace.

<script src="https://unpkg.com/multibase/dist/index.min.js"></script>

Usage

Example

const multibase = require('multibase')

const bytes = multibase.encode('base58btc', new TextEncoder().encode('hey, how is it going'))

const decodedBytes = multibase.decode(bytes)
console.log(decodedBytes.toString())
// hey, how is it going

API

https://multiformats.github.io/js-multibase/

Contribute

Contributions welcome. Please check out the issues.

Check out our contributing document for more information on how we work, and about contributing in general. Please be aware that all interactions related to multiformats are subject to the IPFS Code of Conduct.

Small note: If editing the README, please conform to the standard-readme specification.

License

MIT © Protocol Labs Inc.

js-multibase's People

Contributors

achingbrain avatar alanshaw avatar boramalper avatar carsonfarmer avatar daviddias avatar dependabot-preview[bot] avatar dignifiedquire avatar gozala avatar greenkeeper[bot] avatar hacdias avatar hugomrdias avatar kylemaas avatar npmcdn-to-unpkg-bot avatar olizilla avatar richardlitt avatar theobat avatar victorb avatar vmx avatar wolfgang42 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

js-multibase's Issues

Recent release broke our build

Describe the bug
All of a sudden today the build in our CI system started failing with the error:

 FAIL  src/__tests__/doc-id.test.ts
  ● Test suite failed to run

    ReferenceError: TextDecoder is not defined

    > 1 | import CID from 'cids';
        | ^
      2 | import multibase from 'multibase';
      3 | import * as doctypes from './doctypes';
      4 | import varint from 'varint';

      at Object.<anonymous> (node_modules/multibase/src/util.js:3:21)
      at Object.<anonymous> (node_modules/multibase/src/base.js:3:24)
      at Object.<anonymous> (node_modules/multibase/src/constants.js:4:14)
      at Object.<anonymous> (node_modules/multibase/src/index.js:7:19)
      at Object.<anonymous> (node_modules/cids/node_modules/multihashes/src/index.js:6:19)
      at Object.<anonymous> (node_modules/cids/src/index.js:3:12)
      at Object.<anonymous> (src/doc-id.ts:1:1)
      at Object.<anonymous> (src/__tests__/doc-id.test.ts:1:1)

Note that I didn't manually update package.json in any way to get to the newest version, we just use the ^ and ~ in some places to pull in newer minor versions of packages when they get released, but minor versions aren't supposed to include breaking changes. It seems like when multibase v4.0.3 got released, npm picked it up and started using it in CI, which broke our build. Note our package.json specifies "multibase": "~4.0.2".

I'm currently in the process of going through all our package.json files and pinning to version 4.0.2 to get things working again, but ideally this wouldn't be necessary as the semantic versioning system should already protect us against pulling in unexpected breaking changes.

Desktop (please complete the following information):

  • OS: uname -a: Linux Sonny 5.8.0-43-generic #49~20.04.1-Ubuntu SMP Fri Feb 5 09:57:56 UTC 2021 x86_64 x86_64 x86_64 GNU/Linux

Additional context
Add any other context about the problem here.

`web-encoding` package causes problems with jest in env=node and env=jsdom

Describe the bug
When running the test in jest (with either env=jsdom or env=node setting) the TextDecoder or TextEncoder is not a constructor error makes it impossible to write unit tests using this module

To Reproduce
This simple test and code fails to run in jest
code:
https://github.com/Uniswap/uniswap-interface/blob/0aabf4856e6e90041caf4233db4b7defb01574a2/src/utils/contenthashToUri.ts
test:
https://github.com/Uniswap/uniswap-interface/blob/0aabf4856e6e90041caf4233db4b7defb01574a2/src/utils/contenthashToUri.test.skip.ts

error:
TypeError: TextDecoder is not a constructor


    TypeError: TextDecoder is not a constructor

    > 1 | import CID from 'cids'
        | ^
      2 | import { getCodec, rmPrefix } from 'multicodec'
      3 | import { decode, toB58String } from 'multihashes'
      4 |

      at Object.<anonymous> (node_modules/multibase/src/util.js:6:21)
      at Object.<anonymous> (node_modules/multibase/src/base.js:4:24)
      at Object.<anonymous> (node_modules/multibase/src/constants.js:5:14)
      at Object.<anonymous> (node_modules/multibase/src/index.js:9:19)
      at Object.<anonymous> (node_modules/multihashes/src/index.js:10:19)
      at Object.<anonymous> (node_modules/cids/src/index.js:3:12)
      at Object.<anonymous> (src/utils/contenthashToUri.ts:1:1)
      at Object.<anonymous> (src/utils/contenthashToUri.test.ts:1:1)

Expected behavior
TextDecoder from the environment (specifically, global for node and window for jsdom jest setting) is successfully loaded

Screenshots
image

Desktop (please complete the following information):

  • OS: MacOS

ReferenceError: TextDecoder is not defined

When running jest i get this error :
image

The code that fails is here util.js
image

As far as I know the nodejs env doesn't offer TextDecoder API. Is this intended only for the browsers?

Export the names and codes as object (instead of array)

Currently constants.names and constants.codes are exported as Arrays, which means looking-up if a particular name or code exists requires scanning the whole array.

exports.names = Object.freeze(Object.keys(constants.names))
exports.codes = Object.freeze(Object.keys(constants.codes))

Please export those name and codes as objects directly so that callers can lookup directly, and also get the name from the code, and vice-versa (since the object structure has rich info on relations between them, which is lost when Object.keys is performed).

Something like below:

 exports.names = Object.freeze(constants.names)
 exports.codes = Object.freeze(constants.codes)

QUESTION: how to get the string from the encode function

in version 1.0.0 we could use encode(buff).toString() and then we would get something similar to this bfuws2ljnijcuoskoebieoubakbkuetcjimqewrkzebbeyt2djmws2ljnfugquvtfojzws33ohiqe64dfnzieoubonjzsa5rufyytalrybufeg33nnvsw45b2ebuhi5dqom5c6l3pobsw44dhobvhgltpojtq2csdn5ww2zlooq5cavdinfzsa2ltebqsa4tfozxwgylunfxw4iddmvzhi2lgnfrwc5dfbufa2ctxnztukskclf.....

how to get the same thing in current version?
Thanks

Incompatibility with "zii"

Describe the bug
If the package "zii" is installed due to a dependency somewhere else in an npm project including js-multibase (in my case, from ipfs-repo-migrations from ipfs-core), this function:

function encoding (nameOrCode) {

...no longer works for types which use a code of "z". This happens in my case from here:

https://github.com/ipfs/js-ipfs-repo-migrations/blob/425d53c5f2ef2d89b11d2cfc33362d5ebfad81a5/migrations/migration-9/utils.js#L10

To Reproduce
Steps to reproduce the behavior:

  1. npm install js-multibase
  2. npm install zii
  3. Require both so they are pulled into your project.
  4. Try to decode "zQmdfTbBqBPQ7VNxZEYEj14VmRuZBkqFbiwReogJgS1zR1n"

Expected behavior
It decodes.

Actual behavior
I get this error: TypeError: enc.decode is not a function

Desktop (please complete the following information):

  • OS: Gentoo Linux
  • Browser: Chromium
  • Version: 87

ReferenceError: TextDecoder is not defined

I get the following error when trying to run code using this. It didn't use to fail so suspect this is in result to a recent update

node_modules\multibase\src\util.js:3
const textDecoder = new TextDecoder()

ReferenceError: TextDecoder is not defined

React Native support is dropped

Describe the bug
The TextEncoder/TextDecoder reference errors occur on React Native environment because the environment doesn't provide them.
The multibase package drops the RN support at the version 4.0.3 release when it stops depending on the web-encoding library.
These APIs are provided by the web-encoding package on the RN environment from its 1.1.0 version release.

To Reproduce

Steps to reproduce the behavior:

Prerequisite: React Native development environment

  1. npx react-native init MultibaseIssue98App (create a React Native project)
  2. yarn add [email protected]
  3. use this package (example: somay/MultibaseIssue98App@6bf7e62 )
  4. yarn ios or yarn android (Run the application on mobile devices or emulators)
  5. Get the error message on console: ERROR ReferenceError: Can't find variable: TextDecoder

This repository is the result of the above steps' 1 to 3: https://github.com/somay/MultibaseIssue98App

Expected behavior
Able to use them.

Smartphone (please complete the following information):

  • Android (emulator of Pixel_3a_API_30_x86(AVD) - 11) and iOS applications (emulator of iPhone 12 - iOS 14.5)

Additional context

This PR actually removed web-encoding: #89

I'm grateful if you resume to support React Native even if such universality of this package actually isn't intended by the maintainers.
The web-encoding library intentionally starts to support the React Native platform in order to help RN developers use libraries depending on web-encoding such as multibase and uint8arrays.
I currently works on a domain (the decentralized identity domain) where this kind of support is quite helpful because many libraries providing core functionalities in this area are implemented in Javascript although it's very important to provide users with mobile applications.

[Bug] Incorrect implementation for base58btc

It seems the decoding of base58btc is incorrect.

Test Cases:

import {decode, encode} from 'multibase';

let encoding = 'base64';
console.log('testing', encoding);
let text = "123";
let encoded = encode(encoding, text).toString();
let result = decode(encoded).toString();
let matched = text === result;
console.log({matched});
if (!matched) {
  console.log({text, result, encoded})
}


encoding = 'base58btc';
console.log('testing', encoding);
text = "123";
encoded = encode(encoding, text).toString();
result = decode(encoded).toString();
matched = text === result;
console.log({matched});
if (!matched) {
  console.log({text, result, encoded})
}

Result:

testing base64
{ matched: true }
testing base58btc
{ matched: false }
{ text: '123', result: '\\��', encoded: 'zY8zb' }

More description error text

While using ipfs.dag.get() for a list of CIDs, encountered the below errors on some of them:

Error: invalid base16 character
    at Object.decode ( node_modules\multibase\src\base16.js:14:17)
    at Base.decode ( node_modules\multibase\src\base.js:18:24)
    at Function.decode ( node_modules\multibase\src\index.js:75:27)
    at new CID ( node_modules\cids\src\index.js:79:31)
    at new CID ( node_modules\class-is\index.js:15:17)
    at Function.get.promisify ( node_modules\ipfs\src\core\components\dag.js:94:17)
    at Object.get ( node_modules\promisify-es6\index.js:32:27)

The error is fine, except it is very difficult to identify which exact CID was causing this error (when dealing with multiple CIDs, across multiple asynchronous calls).

Wondering if you would be open to make the errors much descriptive, something like below: at line 14 of base16.js

for (let char of input) {
  if (alphabet.indexOf(char) < 0) {
    throw new Error(`invalid base16 character '${char}' in '${input}'`) //<- more descriptive
  }
}

This helps the callers / users troubleshoot the problems more easily.

base32 is broken

It seems base-x is not working as well as we thought it would. Probably other bases are broken as well

Todo:

  • Write more extensive tests
  • Fix encoding

Failing example for base32

const raw = new Buffer('1220120f6af601d46e10b2d2e11ed71c55d25f3042c22501e41d1246e7a1e9d3d8ec', 'hex')
const expected = 'bciqbed3k6ya5i3qqwljochwxdrk5exzqilbckapedujenz5b5hj5r3a'
const actual = 'bjcaeqpnl3advdoccznfyi624oflus7gbbmejib4qorerxhuhu5hwhm'

cc @diasdavid

Ambigous encoding due to underlying design

The truncation of leading zeros in the base-x package this library is built with creates an ambiguous encoding. This is especially noticeable in base-2 where the example test case "yes mani !" encodes to "01111001011001010111001100100000011011010110000101101110011010010010000000100001", while a strict encoding would be "001111001011001010111001100100000011011010110000101101110011010010010000000100001". This means that decoding "\x00yes mani !" results in "yes mani !", an erroneous interpretation in many contexts.

More info: multiformats/multibase#34

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.