npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@polkadot-api/ledger-signer

v0.1.9

Published

## `Buffer` polyfill

Downloads

851

Readme

@polkadot-api/ledger-signer

Buffer polyfill

Ledger libraries require Buffer global to exist in scope. If you are on a NodeJS (or Bun) environment you are good to go. If you are on a browser environment, make sure your bundler exposes it or polyfill it yourself with buffer package.

Usage

An example on how to create and use a LedgerSigner could be found in our repo. In this case it is in a NodeJS environment but similar code could be used for browser as we will see in the next section.

Install

In order to consume this library, the consumer has to install both @polkadot-api/ledger-signer and a Ledger Transport. Feel free to walk through Ledger documentation if you want to know more. For example, for NodeJS environment @ledgerhq/hw-transport-node-hid is used, and in web browsers @ledgerhq/hw-transport-webhid or @ledgerhq/hw-transport-webusb could be used.

In order to create an instance of LedgerSigner, the consumer has to create a Ledger Transport and open it. The initial boilerplate for using a Ledger in a NodeJS or browser environment is as follows:

// use any other transport, we give the node example here
import TransportNodeHid from "@ledgerhq/hw-transport-node-hid"
import { LedgerSigner } from "@polkadot-api/ledger-signer"

// this will take the first available device
const transport = await TransportNodeHid.create()
const ledger = new LedgerSigner(transport)

API

First of all, it is important to mention that the class prevents race conditions, and only sends data to the device when it is free and available to receive data. Then, multiple calls could be sent from the consumer and the library would queue them to avoid clashes.

Let's walk through the different methods available in a LedgerSigner instance.

appInfo

This method will query the device to know the app that the device is running. Let's see an example:

const info = await ledger.appInfo()
// { appName: "Polkadot", appVersion: "100.0.8" }

deviceId

This method creates a deterministic number from the device's seed. It could be useful to ensure that the device that was connected is the same one that was connected in previous sessions of the dApp. This method is cached, and calling it multiple times only triggers data exchange with the device the first time.

const id = await ledger.deviceId()
// 67518578523

getPubkey

In Polkadot ecosystem, accounts derived using Ledger are identified with two numbers. In PolkadotJS Wallet, for example, are called Account Index and Address Offset. Other wallets just offer the option to modify the first one. All the methods that will follow take both numbers as arguments, and the second one is optional and defaults to 0. The recommendation to derive new addresses is to modify just the first one and sequentially derive addresses, starting from 0. Let's see an example:

const first = await ledger.getPubkey(0) // exactly the same as `getPubkey(0, 0)`
const second = await ledger.getPubkey(1) // `getPubkey(1, 0)`
const third = await ledger.getPubkey(2) // `getPubkey(2, 0)`
// ...

We only return the pubkey, the address could be created from the pubkey using any SS58 library (such as @polkadot-api/substrate-bindings).

seeAddressInDevice

This method behaves exactly like the previous one, just returning the pubkey, but allows to show the address on a device screen. In this case, it takes also as argument the SS58 prefix to encode the address with, since the device shows in the screen both the SS58-encoded address and the pubkey.

// 42 is the ss58 prefix in this case
const first = await ledger.seeAddressInDevice(42, 0) // exactly the same as `seeAddressInDevice(42, 0, 0)`

getPolkadotSigner

This method create a PolkadotSigner interface that then could be passed to a polkadot-api typedApi to sign transactions with it. It requires some off-chain network information from the consumer that the library cannot get from the metadata, generally found in the chainspec; this requirement is a result of RFC#0078. It also takes the two indices mentioned in the previous sections.

More information about PolkadotSigner is found at our docs.

// example for Polkadot
const info = { tokenSymbol: "DOT", decimals: 10 }
const signer = await ledger.getPolkadotSigner(info, 0) // same as `getPolkadotSigner(info, 0, 0)`

// got from a `polkadot-api` client
api.tx.System.remark({
  remark: Binary.fromText("Signed from ledger!"),
}).sign(signer)