Code Monkey home page Code Monkey logo

magic-js's Introduction

Logo

Magic JavaScript SDK

<MagicLabs>

The Magic JavaScript SDK empowers developers to provide frictionless web3 onboarding to their end-users while preserving their security and privacy using non-custodial wallets.

License ยท Changelog ยท Contributing Guide

๐Ÿ“– Documentation

  • See the developer documentation to learn how you can master the Magic SDK in a matter of minutes.
  • See the @magic-sdk/react-native-bare README for Bare React Native package specific information.
  • See the @magic-sdk/react-native-expo README for Expo React Native package specific information.

โšก๏ธ Quick Start

Installation

Integrating your app with Magic will require our client-side NPM package:

# Via NPM:
npm install --save magic-sdk # If you're targeting web browsers
npm install --save @magic-sdk/react-native-bare # If you're targeting Bare React Native
npm install --save @magic-sdk/react-native-expo # If you're targeting Expo React Native

# Via Yarn:
yarn add magic-sdk # If you're targeting web browsers
yarn add @magic-sdk/react-native-bare # If you're targeting Bare React Native
yarn add @magic-sdk/react-native-expo # If you're targeting Expo React Native

Alternatively, you can load via CDN by adding a script tag to your appโ€™s <head>:

<script src="https://cdn.jsdelivr.net/npm/magic-sdk/dist/magic.js"></script>

Usage

Sign up or log in to the developer dashboard to receive API keys that will allow your application to interact with Magic's authentication APIs.

Then, you can start authenticating users with just one method! Magic works across all modern desktop, mobile Chrome, Safari and Firefox browsers.

import { Magic } from 'magic-sdk';
import Web3 from 'web3';

const magic = new Magic('YOUR_API_KEY', {
  network: 'goerli',
});

const web3 = new Web3(magic.rpcProvider);
const accounts = await magic.wallet.connectWithUI();

๐Ÿ“ฆ Package Ecosystem

Entry points

These are packages you can install to enable Magic JS SDK functionality for your client-side application.

Package Name Changelog Description
magic-sdk CHANGELOG Web/browser entry-point for Magic SDK.
@magic-sdk/react-native-bare CHANGELOG Bare React Native entry-point for Magic SDK.
@magic-sdk/react-native-expo CHANGELOG Expo React Native entry-point for Magic SDK.

Extensions

Extend Magic JS SDK functionality for your use-case through @magic-ext/* packages.

Internals

These are packages Magic JS SDK uses internally to work seamlessly across platforms.

Package Name Changelog Description
@magic-sdk/types CHANGELOG Core typings shared between JavaScript entry-points of Magic SDK.
@magic-sdk/pnp CHANGELOG A lightweight connector that wraps Magic JS authentication with a beautiful, functional out-of-the-box login form.
@magic-sdk/provider CHANGELOG Core business logic shared between JavaScript entry-points of Magic SDK.
@magic-sdk/commons CHANGELOG Exposes a listing of common public APIs from @magic-sdk/provider and @magic-sdk/types to the platform-specific entry points.
@magic-sdk/types CHANGELOG Core typings for Magic SDK packages.

๐Ÿšฆ Testing

Run tests for all packages

yarn test

Test an individual package

PKG=magic-sdk yarn test
PKG=@magic-sdk/react-native-bare yarn test
PKG=@magic-sdk/react-native-expo yarn test

Test specific files

yarn test /test/**/constructor.spec.ts

magic-js's People

Contributors

adenekan41 avatar am-hernandez avatar ariflo avatar bcleary06 avatar bengriffin1 avatar bmeeder22 avatar chrisdakin-magic avatar dependabot[bot] avatar dgerrellsmagic avatar dizigen avatar ethanthatonekid avatar ethella avatar fyjen avatar harryeth avatar hcote avatar joshuascan avatar kgrubb avatar makrandgupta avatar mattupham avatar mushfichowdhury-magic avatar octave08 avatar patjacobs-magic avatar rjwebb avatar romin-halltari avatar sandeepsuresh2020 avatar smithki avatar snyk-bot avatar srinjoyc avatar thebolliwood avatar ysm-dev 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  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  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  avatar  avatar  avatar  avatar  avatar  avatar  avatar

magic-js's Issues

iframe performance with showUI: false

โœจ Feature Request

Load minimal lightweight iframe if showUI is set to false. Loading React + external CSS is unnecessary for custom loading UI's

๐Ÿงฉ Context

Trying to build a login page -- the external UI dependencies for magic link largely outweigh my custom front-end. Performance is a priority, especially for other projects I'd like to incorporate magic link with!

image

Is there any way that an alternative mostly-empty iframe can be loaded if showUI is false? Happy to help, and thanks for providing an awesome developer-experience with this platform!

Listen to Client whist in headless Mode / showUI:false

โœ… Prerequisites

  • Did you perform a cursory search of open issues? Is this feature already requested elsewhere?
  • Are you reporting to the correct repository (magic-sdk)?

โœจ Feature Request

When using loginWithMagicLink({showUI: false}) have a method of monitoring the state of the client so a custom UI could be implemented with the same logic as the MagicLinks Clients

๐Ÿงฉ Context

My thoughts for this is that in some use cases users may get confused as that it may be easier for a developer to build a custom interface instead of the one provided by magic-sdk.

Currently i believe there is no way to watch the client to give return the states so that could be implemented into a custom UI.

  • Email DNS Warning
  • Email Sent to email Address
  • Waiting for the client to accept the link

Support for Observables when sending and listening for chain events

โœ… Prerequisites

  • Did you perform a cursory search of open issues? Is this feature already requested elsewhere?
  • Are you reporting to the correct repository (magic-sdk)?

โœจ Feature Request

Clash of Coins would like to use a custom websocket(observables) endpoint to achieve higher performance when sending transactions and listening to chain events.

Unit test React Native changes

Before we release React Native features, we should update the current unit tests to reflect changes in core business logic. We also need to get coverage back to 100% by testing all the new code related to our React Native implementation.

Certain globals need polyfilling for React Native

โœ… Prerequisites

  • Did you perform a cursory search of open issues? Is this bug already reported elsewhere?
  • Are you running the latest SDK version?
  • Are you reporting to the correct repository (magic-sdk)?

๐Ÿ› Description

atob, btoa, and process is not defined in React Native.

๐Ÿงฉ Steps to Reproduce

  1. Set up React Native Project
  2. Import/instantiate Magic
  3. Broken ๐Ÿ˜ข

๐Ÿค” Expected behavior

React Native should work!

๐Ÿ˜ฎ Actual behavior

Undefined globals if the environment is not correctly polyfilled.

๐Ÿ’ป Code Sample

None.

๐ŸŒŽ Environment

Software Version(s)
magic-sdk
Browser
yarn
Operating System

Pulling correct Magic Type / Redwood.js Integration

โœ… Prerequisites

  • Did you perform a cursory search of open issues? Is this question already asked elsewhere?
  • Are you reporting to the correct repository (magic-sdk)?

โ“ Question

Best Method to pull types for Magic Client (New ...

I have been working on the auth implementation of magic links for Redwood.js!

We have most of the implementation done but i could not find an easy way to pull the client type into the auth wrapper.

is there a simpler type than

SDKBase<MagicSDKAdditionalConfiguration<string, any>> & {
    [x: string]: Pick<Extension<string>, never>;
}

if you wanted to quickly look through the pull request and check that i have implemented it all properly please do! I would love some insight. Pull request - here

Thanks

Seemingly too many dependencies

โœ… Prerequisites

  • [โœ…] Did you perform a cursory search of open issues? Is this feature already requested elsewhere?
  • [โœ…] Are you reporting to the correct repository (magic-sdk)?

โœจ Feature Request

This is a neat product. I went ahead and tried it out, and then I got to the step of installing it via NPM.

On a new project, this was the result of npm install magic-sdk:

Screenshot from 2020-05-24 14-17-25

515 packages added. Warnings (useless and irrelevant, but still there). 4 vulnerabilities (most likely nothing, but still). Deprecation notice.

The lovely image you built up with simplicity, security, and professionalism, faded more and more the longer installation log got.

I'm not sure what causes it. You only have 3 direct dependencies. Seems like snyk is the one causing the ruckus? Perhaps it should be a devDependency, if at all?

[React-native] Buffer found undefined

โœ… Prerequisites

  • Did you perform a cursory search of open issues? Is this bug already reported elsewhere?
  • Are you running the latest SDK version?
  • Are you reporting to the correct repository (magic-sdk)?

๐Ÿ› Description

Buffer found undefined when starting React Native app with Magic

๐Ÿงฉ Steps to Reproduce

๐Ÿค” Expected behavior

It shall have no error

๐Ÿ˜ฎ Actual behavior

[What actually happened? Please include any error stack traces you encounter.]

๐Ÿ’ป Code Sample

const magic = Magic('api_key')

๐ŸŒŽ Environment

Software Version(s)
magic-sdk 1.3.1
Browser RN
yarn
Operating System IOS

Test Bug #2

โœ… Prerequisites

  • Did you perform a cursory search of open issues? Is this bug already reported elsewhere?
  • Are you running the latest SDK version?
  • Are you reporting to the correct repository (magic-sdk)?

๐Ÿ› Description

[Description of the bug.]

๐Ÿงฉ Steps to Reproduce

  1. [First Step]
  2. [Second Step]
  3. [and so on...]

๐Ÿค” Expected behavior

[What you expected to happen?]

๐Ÿ˜ฎ Actual behavior

[What actually happened? Please include any error stack traces you encounter.]

๐Ÿ’ป Code Sample

[If possible, please provide a code repository, gist, code snippet or sample files to reproduce the issue.]

๐Ÿ’ป Environment

Software Version(s)
magic-sdk
Browser
yarn
Operating System

Refactor to a Lerna Monorepo

โœ… Prerequisites

  • Did you perform a cursory search of open issues? Is this feature already requested elsewhere?
  • Are you reporting to the correct repository (magic-sdk)?

โœจ Feature Request

Soon we will be offering open-source Magic SDK extensions. To support these upcoming new libraries more easily, we should refactor to a monorepo using Lerna.

EDIT:

Another great reason for this feature is the blockers encountered during the Redwood JS integration. See the open PR for a more detailed explanation.

Test Bug

โœ… Prerequisites

  • Did you perform a cursory search of open issues? Is this bug already reported elsewhere?
  • Are you running the latest SDK version?
  • Are you reporting to the correct repository (magic-sdk)?

๐Ÿ› Description

[Description of the bug.]

๐Ÿงฉ Steps to Reproduce

  1. [First Step]
  2. [Second Step]
  3. [and so on...]

๐Ÿค” Expected behavior

[What you expected to happen?]

๐Ÿ˜ฎ Actual behavior

[What actually happened? Please include any error stack traces you encounter.]

๐Ÿ’ป Code Sample

[If possible, please provide a code repository, gist, code snippet or sample files to reproduce the issue.]

๐Ÿ’ป Environment

Software Version(s)
magic-sdk
Browser
yarn
Operating System

Support Expo Snack Sandbox

โœ… Prerequisites

  • [*] Did you perform a cursory search of open issues? Is this feature already requested elsewhere?
  • [*] Are you reporting to the correct repository (magic-sdk)?

โœจ Feature Request

[Description of the feature.]
magic-sdk/react-native can't be loaded in Expo snack, due to submodule importation is not supported in Expo.

๐Ÿงฉ Context

This ticket is used to backtrack the progress of expo snack support.

๐Ÿ’ป Examples

https://snack.expo.io/@jerry-liu/magic-link-test

Add support for Matic testnet V3

โœ… Prerequisites

  • Did you perform a cursory search of open issues? Is this feature already requested elsewhere?
  • Are you reporting to the correct repository (magic-sdk)?

โœจ Feature Request

Feature request from Clash of Coins
Currently only Matic testnet V2 is supported.

Add 'preload' method

We've had several partners request this feature and it turns out that having it will also unblock React Native integration for mgbox.io

Incorrect type signature for `RPCProviderModule.enable`

โœ… Prerequisites

  • Did you perform a cursory search of open issues? Is this bug already reported elsewhere?
  • Are you running the latest SDK version?
  • Are you reporting to the correct repository (magic-sdk)?

๐Ÿ› Description

The return type for RPCProviderModule.enable is Promise<string>, but the actual value returned is Promise<string[]>.

๐Ÿงฉ Steps to Reproduce

  1. [First Step]
  2. [Second Step]
  3. [and so on...]

๐Ÿค” Expected behavior

The type signature should reflect the actual return type.

๐Ÿ˜ฎ Actual behavior

The type signature reflects string instead of the correct string[].

๐Ÿ’ป Code Sample

None.

๐ŸŒŽ Environment

Software Version(s)
magic-sdk
Browser
yarn
Operating System

Add support for ethers.js v5

ethers.js v5 is currently in beta, but I've used it with other providers such as MetaMask and haven't had any issues with it. However, it seems Magic does not support ethers v5, as the following steps produced the error shown when using ethers 5.0.0-beta.186 but not when using ether v4.

This error showed up when loading the page. If you ignored it and proceeded to use the provider and signer, you would find they are invalid and unable to send transactions or read contract data. The resulting error in this case is similar the one below, but with a method of eth_accounts instead of eth_chainId

The Error

image

Steps to Reproduce

Relevant portions shown below

// ConnectWallet.vue ===========================================
// Initialize Magic
const magic = new Magic(process.env.MAGIC_API_KEY, { network: 'kovan' });
magic.preload();

// Check if user is logged in
const isLoggedIn = await magic.user.isLoggedIn();
if (isLoggedIn) {
  await this.$store.dispatch('main/setEthereumData', magic);
  return
}

// If not logged in, log them in
await magic.auth.loginWithMagicLink({ email: this.email });
await this.$store.dispatch('main/setEthereumData', magic);

// Contents of the `setEthereumData` action ======================
export async function setEthereumData({ commit }, magic) {
  const provider = magic.rpcProvider;
  const ethersProvider = new ethers.providers.Web3Provider(provider);
  const signer = ethersProvider.getSigner();
  const metadata = await magic.user.getMetadata();
  const { email, publicAddress } = metadata;
})

๐ŸŒŽ Environment

Software Version(s)
magic-sdk 1.3.2
ethers.js 5.0.0-beta.186 (did not work) and 4.0.47 (worked)

[RN]Tried to register two views with the same name RNCWebView

Reported by Decrypt

So the issue I'm having with implementing @magic-sdk/react-native is that the SDK and our app both have react-native-webview as a dependency. Even when I match the version numbers (8.1.2 for RNWebView) I run into the following issue...

Invariant Violation: Tried to register two views with the same name RNCWebView

If I manually go into node_modules/@magic-sdk/react-native/node_modules/ and delete the react-native-webview dependency folder I am able to resolve the issue. Magic works and seems to use the top level RNWebView dependency.

The error is caused by having two identical classes with identical names imported to the app and itโ€™s thrown by the IOS builder.

Will fix it by putting React-native-webview as a peer dependency with a restricted range of versions

Document `PromiEvent` interface

๐Ÿงฉ Context

PromiEvent was added in v1.4.0, but we have no documentation on how best to use it. We should document the specific APIs, as well as events for each relevant SDK method. Finally, we should also add an example of 100% "whitelabeled" authentication using Magic SDK.

Optimize bundling for RN and Web

@Ethella commented on Thu Feb 27 2020


@Ethella commented on Fri Apr 10 2020

The new idea is to install the needed packages in the RN SDK package, for a reason that all these packages will be downloaded to the app that doesn't need to be downloaded per loading. So size is not a concern in this case.

    "whatwg-url": "^8.0.0"
    "react-native-webview": "^8.1.2", 

In this way, the developer doesn't need to add theses dependencies.

It might be a good choice to manage two package.json, but install and build into one fortmatic package.
Or manage in one package.json, as long web SDK doesn't import these dependencies, which would keep the small size as it used to be.

These need to be installed in the developer's app

    "process": "^0.11.10", // required by web3
    "web3": "^1.2.6",

URL Constructor Throws TypeError on Safari

โœ… Prerequisites

  • Did you perform a cursory search of open issues? Is this bug already reported elsewhere?
  • Are you running the latest SDK version?
  • Are you reporting to the correct repository (magic-sdk)?

๐Ÿ› Description

On Safari, the URL constructor throws a TypeError if the second parameter is explicitly undefined. This does not occur if the second parameter is omitted entirely. This is breaking Safari implementations via the CDN bundle.

Add `chainType` parameter to `EthNetworkConfiguration` to support Harmony

โœ… Prerequisites

  • Did you perform a cursory search of open issues? Is this feature already requested elsewhere?
  • Are you reporting to the correct repository (magic-sdk)?

โœจ Feature Request

To support Harmony network, we'll need a new configuration optionโ€”chainTypeโ€”to be added to the EthNetworkConfiguration interface. It would be helpful to also add an enumโ€”EthChainTypeโ€”to support this interface.

"URL" is not supported in RN

@Ethella commented on Sat Feb 15 2020

https://github.com/facebook/react-native/blob/0.59-stable/Libraries/Blob/URL.js#L157

RN does not support URL, need to rewrite it in a different format


@Ethella commented on Wed Feb 19 2020

Two options,

  1. Adding hacks on global.js
    import { URL, URLSearchParams } from "whatwg-url";
    global.URL = URL;
    global.URLSearchParams = URLSearchParams;

This is will create overhead to developers

  1. Or rewrite new URL in SDK
    Losing tidiness on SDK, and reinvent the wheel of an existing package

Will come back to find best solution


@smithki commented on Fri Feb 21 2020

This can be worked around using NodeJS url module: facebook/react-native#16434

There is also a polyfill specifically for this: https://github.com/charpeni/react-native-url-polyfill


@Ethella commented on Mon Feb 24 2020

@smithki I agree, there is quite a lot of workaround.

But the decision is if we proceed with the solution of 2 in 1 bundle in fortmatic-js,

  • Is it small enough that we can just refactor to code that's platform-agnostic.
  • Or do we delegate this to the RN developer to add configuration their app?
    • For example, in the doc we write to use fortmatic-js please download react-native-url-polyfill and require ('react-native-url-polyfill')
    • There have been quite a lot of environment setup for RN to run formatic-js

Here's a copy of what I add to RN to make fortmatic-js work so far, some of them are not necessary. I'll prune them later.

if (typeof __dirname === 'undefined') global.__dirname = '/'
if (typeof __filename === 'undefined') global.__filename = ''
if (typeof process === 'undefined') {
    global.process = require('process')
} else {
    const bProcess = require('process')
    for (var p in bProcess) {
        if (!(p in process)) {
            process[p] = bProcess[p]
        }
    }
}
global.Buffer = require('buffer').Buffer
global.process = require('process')
global.process.env.NODE_ENV = __DEV__ ? 'development' : 'production'
if (typeof btoa === 'undefined') {
    global.btoa = function (str) {
        return new Buffer(str, 'binary').toString('base64')
    }
}
if (typeof atob === 'undefined') {
    global.atob = function (b64Encoded) {
        return new Buffer(b64Encoded, 'base64').toString('binary')
    }
}
process.browser = false
if (typeof Buffer === 'undefined') global.Buffer = require('buffer').Buffer
global.location = { protocol: 'file:' }
const isDev = typeof __DEV__ === 'boolean' && __DEV__
process.env['NODE_ENV'] = isDev ? 'development' : 'production'
if (typeof localStorage !== 'undefined') {
    localStorage.debug = isDev ? '*' : ''
}

import { URL, URLSearchParams } from "whatwg-url";
global.URL = URL;
global.URLSearchParams = URLSearchParams;

/* To inject window object for window.addEventListener */
if (global.window === undefined) {
    global.window = global;
}

Raise error if React Native peer dependencies are missing

We want to avoid adding react and react-native to peerDependencies so that developers working with the browser bundle (which is likely to be the vast majority) do not receive superfluous warnings when they run npm install. Another way to tackle the issue of peer dependencies in this case would be to raise an error from the SDK constructor.

Personally, I think it may be unnecessary since React and React Native dependencies are implied if the developer is importing our React Native-compatible bundle. Iโ€™m curious to hear more perspectives on this idea, though.

Document React Native features

โœ… Prerequisites

  • Did you perform a cursory search of open issues? Is this feature already requested elsewhere?
  • Are you reporting to the correct repository (magic-sdk)?

โœจ Feature Request

Add documentation to https://docs.magic.link/ about support for React Native.

๐Ÿงฉ Context

None.

๐Ÿ’ป Examples

None.

[OSS Prep] README Updates

A README.md that includes but not limited to the following sections:

  • Introduction
  • Table of Content
  • Installation
  • Quick Start
  • How to Contribute
  • Changelog
  • License

Update type signature of `RpcProviderModule` to implement Web3's `AbstractProvider`

โœ… Prerequisites

  • Did you perform a cursory search of open issues? Is this feature already requested elsewhere?
  • Are you reporting to the correct repository (magic-sdk)?

โœจ Feature Request

Currently, to use Web3 features in TypeScript, the magic.rpcProvider member must be cast to any, like this:

new Web3(magic.rpcProvider as any);

But, now that web3/web3.js#3380 is resolved, we can implement the Web3 AbstractProvider interface to gain strong typing. Once this is implemented, consumers of rpcProvider can simply do this:

new Web3(magic.rpcProvider); // No compiler error! Yay! ๐Ÿš€

Audit our EIP1193 Implementation; Add support for events

โœ… Prerequisites

  • Did you perform a cursory search of open issues? Is this feature already requested elsewhere?
  • Are you reporting to the correct repository (magic-sdk)?

โœจ Feature Request

The EIP1193 spec is finally stable. We already, through total accident, implement request to the specification via BaseModule.request. We should offer an implementation of Web3 events. This could be a great addition for any extension APIs to have as well, so I think we should implement it at the BaseModule layer.

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.