Giter Club home page Giter Club logo

cosmos-ledger's Introduction

Cosmos Ledger

Cosmos Ledger is a library for interacting with the Cosmos Ledger Nano App. It provides a developer-friendly interface and user friendly error messages for an improved Ledger Nano development experience.

This library is based on ledger-cosmos-js by Juan Leni who also implemented the official Cosmos Ledger Nano App.

Thank you Juan! ๐Ÿ™Œ

Installation

yarn add @lunie/cosmos-ledger

Usage

Signing with the Ledger Nano

import Ledger from "@lunie/cosmos-ledger"

// generate messages with "@lunie/cosmos-api"
const signMessage = {} || ``
const ledger = await Ledger().connect()
const signature = await ledger.sign(signMessage)

Using with cosmos-api

import Ledger from "@lunie/cosmos-ledger"
import Cosmos from "@lunie/cosmos-api"

const privateKey = Buffer.from(...)
const publicKey = Buffer.from(...)

// init cosmos API object
const cosmos = Cosmos(API_URL, ADDRESS)

// create a message
const msg = cosmos
  .MsgSend({
    toAddress: 'cosmos1abcd09876', 
    amounts: [{ denom: 'stake', amount: 10 }]
  })

// create a signer
const ledgerSigner = async (signMessage) => {
  const ledger = new Ledger()
  await ledger.connect()
  const publicKey = await ledger.getPubKey()
  const signature = await ledger.sign(signMessage)

  return {
    signature,
    publicKey
  }
}

// send the transaction
const { included } = await msg.send({ gas: 200000 }, ledgerSigner)

// wait for the transaction to be included in a block
await included()

Constructor options

Ledger(
  { testModeAllowed = false },
  hdPath = [44, 118, 0, 0, 0], // HDPATH
  hrp = 'cosmos', // BECH32PREFIX
);

cosmos-ledger's People

Contributors

dependabot-preview[bot] avatar dependabot[bot] avatar faboweb avatar jbibla avatar willclarktech avatar williamchong avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

cosmos-ledger's Issues

connect throws randomly.

  1. the browser check often comes back internally as brave when in Chrome browser ( I saw this when debugging)
  2. connect method seems to often throw when a device is there and seems unstable. Errors thrown can be difficult to catch (not caught by try catch and just end script). I run the testdevice method which does not throw, and then on background thread immediately afterwards the connect method throws.

Should `testModeAllowed` have a default value?

Currently when I directly use Ledger() i.e.

const ledger = await Ledger().connect()

as instructed in readme, I will get the following error about testModeAllowed.

Property 'testModeAllowed' is missing in type '{}' but required in type '{ testModeAllowed: Boolean; }

Should the config object and testModeAllowed flag be optional instead for simpler syntax?

Remove Yarn

Is your feature request related to a problem? Please describe.

We have been using Yarn because of the lock file for quiet some time. This is not necessary anymore as npm already since a while has this feature as well. Yarn has some syntax advantages, but it doesn't value the weight of another tool.

Describe the solution you'd like

Remove Yarn from this repository:

  • lock file
  • package.json
  • continuous integration

Describe alternatives you've considered

Additional context

typescript definitions not exported by default

when I do import Ledger from '@lunie/cosmos-ledger';
I don't get the types. I have a red squiggly saying no types found.

Screen Shot 2021-12-20 at 1 47 58 PM

I opened the node_modules folder and found the types there and when I do import Ledger from '@lunie/cosmos-ledger/lib/cosmos-ledger'; it works.

You might need to change something to get these loaded by default

Allow override of HDPATH and HRP

In order to allow the use of this across different cosmos-sdk chains, we should allow the ability to define HDPATH and behc32 hrp on object instantiation, rather than hardcoding these as constants.

Errors while signing transactions

Hi,

I'm developing a block explorer with the ability to delegate tokens for the secret network. We forked our code from hubble but the code they were using to connect with a Ledger wallet was not functional. I decided to use this library along with some code taken from the big dipper to connect with the ledger. I also tried using your cosmos-api/cosmos-js library but I had a lot of problems figuring out how to use those libraries with this one. The main error I've been getting comes from the ledger.sign(msg) method. I've tried passing a few different things to this method that seem to work in other codebases, but I can't figure out what the issue is here. From looking at the library briefly, it seems like it's saying that ledger.hdPath isn't a buffer, array, etc. but I know it is from the testing I've done. This is the message I'm currently passing to ledger.sign():

      "msg": [
        {
          "type": `cosmos-sdk/Send`,
          "value": {
            "inputs": [
              {
                "address": this.publicAddress,
                "coins": [{ "denom": `STAKE`, "amount": `1` }]
              }
            ],
            "outputs": [
              {
                "address": App.config.validatorOperatorAddress,
                "coins": [{ "denom": `STAKE`, "amount": `1` }]
              }
            ]
          }
        }
      ],
      "fee": { "amount": [{ "denom": ``, "amount": `0` }], "gas": `21906` },
      "signatures": null,
      "memo": ``
    }

const txSkeleton = {
      "type": 'auth/StdTx',
      "value": {
        "msg": msgs,
        "fee": '',
        "memo": txContext.value.memo || this.MEMO,
        "signatures": [{
          "signature": 'N/A',
          "account_number": txContext.value.account_number.toString(),
          "sequence": txContext.value.sequence.toString(),
          "pub_key": {
            "type": 'tendermint/PubKeySecp256k1',
            "value": txContext.pk || 'PK',
          },
        }],
      },
    };```

I've been passing this to `ledger.sign()` like this: `ledger.sign(txSkeleton)` and I get `cosmos-ledger.js:658 Uncaught (in promise) Error: Ledger Native Error: TypeError: First argument must be a string, Buffer, ArrayBuffer, Array, or array-like object.` as an error. I decided to put the message in an array (`ledger.sign([txSkeleton])` and I get `cosmos-ledger.js:658 Uncaught (in promise) Error: Ledger Native Error: Data is invalid : JSON. Zero tokens`. I'm kind of at a loss as to what is happening here. Would very much appreciate any help that would put me on the right path

Incorrect typescript type

errorr from type script compile ( fairly strict mode)

ERROR {redacted}node_modules/@lunie/cosmos-ledger/lib/cosmos-ledger.d.ts(4,9):
TS2717: Subsequent property declarations must have the same type. Property 'chrome' must be of type 'typeof chrome', but here has type 'any'.

your Chrome type any is wrong on line 4 of cosmos-ledger.d.ts as it affects other modules that later declare more narrow types causing issues turn to typeof chrome

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.