Giter Site home page Giter Site logo

vaporyjs-ledger's Introduction

A wrapper library around the official Ledger JavaScript library that attempts to simplify usage and handle various failure modes/problems.

Usage

import { LedgerVapory, BrowserLedgerConnectionFactory, Network } from "vaporyjs-ledger";

async function doStuff() {
	const onConnectLedgerRequest = async () => { await promptUserToConnectLedger(); }
	const onOpenVaporyAppRequest = async () => { await promptUserToOpenVaporyAppOnLedger(); }
	const onSwitchLedgerModeRequest = async () => { await promptUserToSwitchVaporyAppToBrowserModeAndRestartVaporyApp(); }
	const onEnableContractSupportRequest = async () => { await promptUserToEnableContractSupportInVaporyAppAndRestartVaporyApp(); }

	const ledgerVapory = new LedgerVapory(Network.Main, BrowserLedgerConnectionFactory, onConnectLedgerRequest, onOpenVaporyAppRequest, onSwitchLedgerModeRequest);
	const address = await ledgerVapory.getAddressByBip44Index(0);
	const firstSignedMessagePromise = ledgerVapory.signTransactionByBip44Index("e8018504e3b292008252089428ee52a8f3d6e5d15f8b131996950d7f296c7952872bd72a2487400080", 7);
	const secondSignedMessagePromise = ledgerVapory.signTransactionByBip32Path("e8018504e3b292008252089428ee52a8f3d6e5d15f8b131996950d7f296c7952872bd72a2487400080", "m/44'/60'/0'/0/7");

	// this will block until both first and second messages are done because the library handles ordering internally
	const secondSignedMessage = await secondSignedMessage;

	// if the ledger isn't connected with the Vapory app open in browser mode, the on*Request callbacks above will be called before the signing promises return
	const firstSignedMessage = await firstSignedMessage;

	// BIP44 index 7 is the same as `m/44'/60'/0'/0/7`; it is strongly recommended to use index 0 if you don't support multi-address wallets
	assert.equal(firstSignedMessage, secondSignedMessage);
}

Development

Note 1

package-lock.json force updates node-hid, a transitive dependency of ledgerco to 0.6.0. This is necessary to get things working on Windows without requiring python as well as get tests working in Docker until ledgerco upgrades its dependency on node-hid to 0.6.0.

Testing with a physical ledger

You can test in node by building the TypeScript files and then running node output/scripts/node.js.

You can test in browser (chrome only, look at developer console) by building the TypeScript files and then running npx budo output/scripts/browser.js --ssl (note: you need openssl binaries on your path or in the root of your project).

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.