Git Product home page Git Product logo

Comments (9)

paulmillr avatar paulmillr commented on June 6, 2024

bip39 is fine, but bip32 is not.

You need to fork bip32 and replace secp256k1 with stark curve. It should be simple.

from scure-starknet.

PhilippeR26 avatar PhilippeR26 commented on June 6, 2024

I made this fork : https://github.com/PhilippeR26/scure-bip32/tree/Starknet
I added :

import { _starkCurve} from '@scure/starknet';

and replaced all secp by _starkCurve.

I have this error :

Error: Invalid private key
    at new HDKey (/D/crypto/scure-bip32/index.ts:160:15)
    at Function.fromMasterSeed (/D/crypto/scure-bip32/index.ts:97:12)

With ethers v5.5.0, I have :

const entropy = hdnode.mnemonicToEntropy(mnemonic);

Result :

0x74dd478e34bf148550143040124cd042

Same result with :

const entropy: Uint8Array = bip39.mnemonicToEntropy(mnemonic, english);

So the problem is not there.

I have some doubt about these constants :

const MASTER_SECRET = utf8ToBytes('Bitcoin seed');
// Bitcoin hardcoded by default
const BITCOIN_VERSIONS: Versions = { private: 0x0488ade4, public: 0x0488b21e };
export const HARDENED_OFFSET: number = 0x80000000;

Could it be there?

from scure-starknet.

PhilippeR26 avatar PhilippeR26 commented on June 6, 2024

In addition :
In the ethers code, I found a salt, set to "mnemonic".
So I modified the code to :

const hdKey = bip32s.HDKey.fromMasterSeed(bip39.mnemonicToSeedSync(mnemonic,"mnemonic"))

But same error...

from scure-starknet.

paulmillr avatar paulmillr commented on June 6, 2024

BIP32 standard was made for secp256k1 (btc, eth). Stark curve has much smaller order, which means many private keys from $2^{256}$ keyspace are invalid. So, the error is correct.

So you can't naïvely replace one with other, it requires further research.

See SLIP-0010 and ERC-2333 for standards which tackle similar problems.

from scure-starknet.

PhilippeR26 avatar PhilippeR26 commented on June 6, 2024

Thanks for the answer.
What is strange is that it works perfectly in an old Ethers version....
Starknet curves are only necessary at the end to grind key.

from scure-starknet.

paulmillr avatar paulmillr commented on June 6, 2024

What is the issue with your second code in starting post? It runs perfectly fine for me and produces 0x4010f2af9d822caed4cf86022aae79008567642cd9b6aa93dc1e242e8165eed key.

from scure-starknet.

PhilippeR26 avatar PhilippeR26 commented on June 6, 2024

Should be 0x40021c889366ff205847e1531fa07e12a01649e37c2e77827cbad4dc8a71f97

from scure-starknet.

PhilippeR26 avatar PhilippeR26 commented on June 6, 2024

Paul,
I think I got it.
After hours of debug in Ethers.js to understand what's happening inside, I have understood what it makes when processing my first code.
Then I tried to do the same thing using only @Scure libs.
The equivalence is :

import * as mStarknet from '@scure/starknet';
import * as bip32 from "@scure/bip32";
import * as bip39 from '@scure/bip39';

const mnemonic = "inquiry tuition toe harvest vanish dress doctor maid divorce mystery cross loyal";

const masterSeed = bip39.mnemonicToSeedSync(mnemonic);
const hdKey1 = bip32.HDKey.fromMasterSeed(masterSeed).derive("m/44'/60'/0'/0/0");
const hdKey2 = bip32.HDKey.fromMasterSeed(hdKey1.privateKey!)
const pathBase = "m/44'/9004'/0'/0/";

for (let i = 0; i < 2; i++) {
    const path = pathBase + String(i);
    const hdKeyi = hdKey2.derive(path);
    console.log("path =", path);
    const starknetPrivateKey = "0x" + mStarknet.grindKey(hdKeyi.privateKey!);
    console.log("privateKey =", starknetPrivateKey);
}

And this time the result is good :
0x40021c889366ff205847e1531fa07e12a01649e37c2e77827cbad4dc8a71f97

It was painful, but at the end, it works...

from scure-starknet.

paulmillr avatar paulmillr commented on June 6, 2024

good to hear

from scure-starknet.

Related Issues (3)

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.