luniehq / cosmos-keys

Library for creating keys and signing messages on Cosmos 🔑
https://lunie.io
28 stars 19 forks source link

Signing doesn't work in Node #13

Open kincaidoneil opened 5 years ago

kincaidoneil commented 5 years ago

This is building cosmos-keys from 81917a898f874d2fc28f13c4c472475d4a95ae96.

I'm running the code below, adapted from this test:

const { signWithPrivateKey } = require("@lunie/cosmos-keys");

signWithPrivateKey(
  {
    account_number: "1",
    chain_id: "tendermint_test",
    fee: { amount: [{ amount: "0", denom: "" }], gas: "21906" },
    memo: "",
    msgs: [
      {
        type: "cosmos-sdk/Send",
        value: {
          inputs: [
            {
              address: "cosmos1qperwt9wrnkg5k9e5gzfgjppzpqhyav5j24d66",
              coins: [{ amount: "1", denom: "STAKE" }]
            }
          ],
          outputs: [
            {
              address: "cosmos1yeckxz7tapz34kjwnjxvmxzurerquhtrmxmuxt",
              coins: [{ amount: "1", denom: "STAKE" }]
            }
          ]
        }
      }
    ],
    sequence: "0"
  },
  Buffer.from(
    "2afc5a66b30e7521d553ec8e6f7244f906df97477248c30c103d7b3f2c671fef",
    "hex"
  )
);

Which errors with: Error: Signing failed: private key should be a Buffer

I hypothesize this has to do with the way Webpack shims Buffers, which causes issues when using secp256k1 in Node, because the native version requires buffers, but is probably fine when used in a browser, since it falls back to using elliptic. (And since the test uses ts-jest rather than the Webpack bundle, the issue doesn't show up there.)

The native code in secp256k1 seems to complicate cross-platform compatibility. We've had a great experience using this WebAssembly alternative that's more portable (works seamlessly in Electron) and has decent performance, too. (One downside is the API might need to be async to ensure the WASM module is loaded). I'd be happy to PR that if you're interested!

faboweb commented 5 years ago

If you could create a PR that would be amazing! Very interested in the approach.

jnaviask commented 5 years ago

I was also facing this problem and couldn't find a simple workaround.

okwme commented 4 years ago

Running into this problem using lambda functions to sign transactions.