GithubHelp home page GithubHelp logo

paulmillr / scure-bip32 Goto Github PK

View Code? Open in Web Editor NEW
50.0 4.0 8.0 373 KB

Secure, audited & minimal implementation of BIP32 hierarchical deterministic (HD) wallets.

Home Page: https://paulmillr.com/noble/#scure

License: MIT License

TypeScript 99.93% JavaScript 0.07%
bip32 bip39 hdkey wallet deterministic mnemonic mnemonic-phrase

scure-bip32's People

Contributors

arobsn avatar paulmillr 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

Watchers

 avatar  avatar  avatar  avatar

scure-bip32's Issues

Add a package.lock.

This allows build systems and auditors to do npm ci instead of npm install, which will validate that the dependencies used to build the project are identical to those checked into the repository.

Support for ed25519 and NIST P-256 as per SLIP-10

SLIP-10 describes a way to derive keys from a master seed in a way that is mostly the same as BIP-32, but has support for other curves like ed25519 and NIST P-256 too. It would be great if this was supported by the @scure/bip32 library.

Using scure-bip32 with Litecoin

Is it possible to use this lib for Litecoin? For HDKey class I see VERSION property but it changes nothing.

import { generateMnemonic as _generateMnemonic, mnemonicToSeed } from '@scure/bip39';
import { wordlist } from '@scure/bip39/wordlists/english';
import { HDKey } from '@scure/bip32';
import { network } from './constants';
import * as crypto from '@scure/btc-signer';

type AllowedKeyEntropyBits = 128 | 256;

export const network = {
    testnet: {
        private: 0x043587cf,
        public: 0x04358394,
    },
};

export function generateMnemonic(entropy: AllowedKeyEntropyBits = 256): string {
    if (entropy !== 256 && entropy !== 128)
      throw TypeError(
        `Incorrect entropy bits provided, expected 256 or 128 (24 or 12 word results), got: "${String(
          entropy
        )}".`
      );
    return _generateMnemonic(wordlist, entropy);
}

async function generateWallet() {
    const mnemonic = generateMnemonic();
    const masterseed = await mnemonicToSeed(mnemonic);
    const hdkey = HDKey.fromMasterSeed(masterseed, network.testnet);

    const addresses = []
    for (let i = 0; i < 5; i++) {
        const path = `m/44'/2'/0'/0/${i}`;
        const node = hdkey.derive(path);
        const address = crypto.getAddress('wpkh', node.privateKey!);
        addresses.push(address);
    }

    console.log("Addresses:", addresses);
}

generateWallet();

It returns these addresses (apparently taking some default BTC values for network keys):

Addresses: [
  'bc1q6jrqgz7vk7nlqk5z8nlfpksycjpxtgf3r90a8e',
  'bc1qwm9py9nh7ml0p623u9cj64ps97aykd96j84x60',
  'bc1q57utqrw02h0vcuk4txathp2gktlvw75wap9e9q',
  'bc1qekjw3qsk7r4qpe83rggu823pd7lz7amc2jnrhe',
  'bc1qkj9cx5x49knq59ux5q9zf8gkq9apgrxxd2twru'
]

Possible discrepancy with the cryptocoinjs hdkey lib

This could be a problem with me and not the lib, but I seem to be getting unexpected results when incrementing the account number portion of the path. I have some code that looks like this:

    const mnemonic = formControls.mnemonic.value;
    const passphrase = formControls.passphrase.value;
    const accountNumber = Number(formControls.accountNumber.value);
    const masterSeed = mnemonicToSeedSync(mnemonic, passphrase);
    
    const keys = HDKey.fromMasterSeed(masterSeed).derive(`m/44'/0'/${accountNumber}'/0/0`);

When I use account number 0, I get the exact same public key that I get from the cryptocoin hdkey lib. However, if I use any other account number: 1, 2, 3, etc. I do not seem to be seeing the same public key that I get from the other lib. Any ideas where I might be going wrong?

Type check issue

Now I am making my own ethereum wallet for my dapp.
But I found something that may be considered as problematic or it could be considered as a problem because of my lack of blockchain knowledge.
While I was doing a debugging test for my wallet, I found that buffer type checking was not occurring in the process of creating hdwallet connect.

I set the argument of fromMasterSeed func with seedToHex, not the buffer version.
But, the function generates the value. Even the value was not the same as when the argument is a buffer type.

Can you explain about this situation?

HDKey derive returns Invalid index

Hi,
I'm implementing LNURL LUD-05 in a react-native mobile app
I have started my code based on this codepen and I adapted it using scure-bip32/39 (version 1.1.0 for both)

Using the same MNEMONIC and LNURL string from the codepen, I got the right derivation path m/138'/2770944631/3393836580/3568300899/2659809102 but the derive method returns me "Invalid index"

Any idea if it can be an issue from scure-bip32? Thanks

Code example

import { HDKey } from '@scure/bip32'
import { HMAC as sha256HMAC } from 'fast-sha256'
import secp256k1 from 'secp256k1'
import * as lnurl from '@zerologin/lnurl'
import { mnemonicToSeed } from "@scure/bip39"

const stringToUint8Array = (str: string) => {
    return Uint8Array.from(str, (x) => x.charCodeAt(0))
}
const seedFromWords = async (mnemonic: string) => {
    const seed = await mnemonicToSeed(mnemonic)
    return Buffer.from(seed)
}

...

const lnurlObject = lnurl.decode('lightning:LNURL1DP68GURN8GHJ7MRFVA58GMNFDENKCMM8D9HZUMRFWEJJ7MR0VA5KU0MTXY7NWCNYXSMKVCEKX3JRSCF4X3SKXWTXXASNGVE5XQ6RZDMXXC6KXDE3VYCRZCENXF3NQVF5XCEXZE3JXVMRGVRY8YURJVNYV43RGDRRVGN8GCT884KX7EMFDCV8DETA')
const lnurlDecoded = lnurlObject.decoded
const k1 = lnurlObject.k1
const domain = lnurlObject.domain


const mn = 'praise you muffin lion enable neck grocery crumble super myself license ghost'
const seed = await seedFromWords(mn)
const root = HDKey.fromMasterSeed(seed)

const hashingKey = root.derive(`m/138'/0`)
const hashingPrivKey = hashingKey.privateKey
console.log({ hashingPrivKey })


if (!hashingPrivKey) throw new Error('Cannot derive pub key')
const derivationMaterial = new sha256HMAC(hashingPrivKey).update(stringToUint8Array(domain)).digest()
console.log({ derivationMaterial })
const pathSuffix = new Uint32Array(derivationMaterial.buffer.slice(0, 16))
console.log({ pathSuffix })
const path = `m/138'/${pathSuffix.join('/')}`
console.log({ path }) // m/138'/2770944631/3393836580/3568300899/2659809102

const linkingKey = root.derive(path) // throw an error "Invalid index"

EDIT: I made some tests using npm lib "bip32" from bitcoinjs and it looks like there is no issue on this lib
Here is a repo to try it: https://github.com/Dolu89/bip32-temp

yarn install
node index.cjs #run bip32
node index.mjs #run @scure/bip32

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.